| 
									
										
										
										
											2016-06-06 16:31:21 +08:00
										 |  |  | package tsdb | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-10-03 15:38:03 +08:00
										 |  |  | import "context" | 
					
						
							| 
									
										
										
										
											2016-07-20 20:28:02 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-10-03 15:38:03 +08:00
										 |  |  | type HandleRequestFunc func(ctx context.Context, req *Request) (*Response, error) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | func HandleRequest(ctx context.Context, req *Request) (*Response, error) { | 
					
						
							| 
									
										
										
										
											2016-06-06 16:31:21 +08:00
										 |  |  | 	context := NewQueryContext(req.Queries, req.TimeRange) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	batches, err := getBatches(req) | 
					
						
							|  |  |  | 	if err != nil { | 
					
						
							|  |  |  | 		return nil, err | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	currentlyExecuting := 0 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	for _, batch := range batches { | 
					
						
							|  |  |  | 		if len(batch.Depends) == 0 { | 
					
						
							|  |  |  | 			currentlyExecuting += 1 | 
					
						
							|  |  |  | 			batch.Started = true | 
					
						
							| 
									
										
										
										
											2016-10-03 15:38:03 +08:00
										 |  |  | 			go batch.process(ctx, context) | 
					
						
							| 
									
										
										
										
											2016-06-06 16:31:21 +08:00
										 |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	response := &Response{} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	for currentlyExecuting != 0 { | 
					
						
							|  |  |  | 		select { | 
					
						
							|  |  |  | 		case batchResult := <-context.ResultsChan: | 
					
						
							|  |  |  | 			currentlyExecuting -= 1 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 			response.BatchTimings = append(response.BatchTimings, batchResult.Timings) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-06-06 23:11:46 +08:00
										 |  |  | 			if batchResult.Error != nil { | 
					
						
							|  |  |  | 				return nil, batchResult.Error | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-06-06 16:31:21 +08:00
										 |  |  | 			for refId, result := range batchResult.QueryResults { | 
					
						
							|  |  |  | 				context.Results[refId] = result | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 			for _, batch := range batches { | 
					
						
							|  |  |  | 				// not interested in started batches
 | 
					
						
							|  |  |  | 				if batch.Started { | 
					
						
							|  |  |  | 					continue | 
					
						
							|  |  |  | 				} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 				if batch.allDependenciesAreIn(context) { | 
					
						
							|  |  |  | 					currentlyExecuting += 1 | 
					
						
							|  |  |  | 					batch.Started = true | 
					
						
							| 
									
										
										
										
											2016-10-03 15:38:03 +08:00
										 |  |  | 					go batch.process(ctx, context) | 
					
						
							| 
									
										
										
										
											2016-06-06 16:31:21 +08:00
										 |  |  | 				} | 
					
						
							|  |  |  | 			} | 
					
						
							| 
									
										
										
										
											2016-10-03 20:33:47 +08:00
										 |  |  | 		case <-ctx.Done(): | 
					
						
							|  |  |  | 			return nil, ctx.Err() | 
					
						
							| 
									
										
										
										
											2016-06-06 16:31:21 +08:00
										 |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	response.Results = context.Results | 
					
						
							|  |  |  | 	return response, nil | 
					
						
							|  |  |  | } |