func (self *CoordinatorImpl) runQuerySpec(querySpec *parser.QuerySpec, seriesWriter SeriesWriter) error { shards := self.clusterConfiguration.GetShards(querySpec) shouldAggregateLocally := true var processor cluster.QueryProcessor var responseChan chan *protocol.Response var seriesClosed chan bool for _, s := range shards { // If the aggregation is done at the shard level, we don't need to // do it here at the coordinator level. if !s.ShouldAggregateLocally(querySpec) { seriesClosed = make(chan bool) shouldAggregateLocally = false responseChan = make(chan *protocol.Response) if querySpec.SelectQuery() != nil { processor = engine.NewQueryEngine(querySpec.SelectQuery(), responseChan) } else { bufferSize := 100 processor = engine.NewPassthroughEngine(responseChan, bufferSize) } go func() { for { res := <-responseChan if *res.Type == endStreamResponse || *res.Type == accessDeniedResponse { seriesWriter.Close() seriesClosed <- true return } if res.Series != nil && len(res.Series.Points) > 0 { seriesWriter.Write(res.Series) } } }() break } } responses := make([]chan *protocol.Response, 0) for _, shard := range shards { responseChan := make(chan *protocol.Response, self.config.QueryShardBufferSize) go shard.Query(querySpec, responseChan) responses = append(responses, responseChan) } for i, responseChan := range responses { log.Debug("READING: shard: ", shards[i].String()) for { response := <-responseChan log.Debug("GOT RESPONSE: ", response.Type, response.Series) if *response.Type == endStreamResponse || *response.Type == accessDeniedResponse { break } if shouldAggregateLocally { log.Debug("WRITING: ", len(response.Series.Points)) seriesWriter.Write(response.Series) log.Debug("WRITING (done)") continue } // if the data wasn't aggregated at the shard level, aggregate // the data here log.Debug("YIELDING: ", len(response.Series.Points)) if response.Series != nil { for _, p := range response.Series.Points { processor.YieldPoint(response.Series.Name, response.Series.Fields, p) } } } log.Debug("DONE: shard: ", shards[i].String()) } if !shouldAggregateLocally { processor.Close() <-seriesClosed return nil } seriesWriter.Close() return nil }