func (self *Coordinator) RunQueryWithContext(user common.User, database string, queryString string, p engine.Processor, requestContext *api.RequestContext) (err error) { log.Info("Start Query: db: %s, u: %s, q: %s", database, user.GetName(), queryString) runningQuery := NewRunningQuery(user.GetName(), database, queryString, time.Now(), requestContext.RemoteAddr()) self.monitor.StartQuery(runningQuery) defer func() { self.monitor.EndQuery(runningQuery) }() defer func(t time.Time) { log.Debug("End Query: db: %s, u: %s, q: %s, t: %s", database, user.GetName(), queryString, time.Now().Sub(t)) }(time.Now()) // don't let a panic pass beyond RunQuery defer common.RecoverFunc(database, queryString, nil) q, err := parser.ParseQuery(queryString) if err != nil { return err } for _, query := range q { err := self.runSingleQuery(user, database, query, p) if err != nil { return err } } return nil }
func (self *Coordinator) InterpolateValuesAndCommit(query string, db string, series *protocol.Series, targetName string, assignSequenceNumbers bool) error { defer common.RecoverFunc(db, query, nil) targetName = strings.Replace(targetName, ":series_name", *series.Name, -1) type sequenceKey struct { seriesName string timestamp int64 } sequenceMap := make(map[sequenceKey]int) r, _ := regexp.Compile(`\[.*?\]`) // get the fields that are used in the target name fieldsInTargetName := r.FindAllString(targetName, -1) fieldsIndeces := make([]int, 0, len(fieldsInTargetName)) for i, f := range fieldsInTargetName { f = f[1 : len(f)-1] fieldsIndeces = append(fieldsIndeces, series.GetFieldIndex(f)) fieldsInTargetName[i] = f } fields := make([]string, 0, len(series.Fields)-len(fieldsIndeces)) // remove the fields used in the target name from the series fields nextfield: for i, f := range series.Fields { for _, fi := range fieldsIndeces { if fi == i { continue nextfield } } fields = append(fields, f) } if r.MatchString(targetName) { serieses := map[string]*protocol.Series{} for _, point := range series.Points { fieldIndex := 0 targetNameWithValues := r.ReplaceAllStringFunc(targetName, func(_ string) string { value := point.GetFieldValueAsString(fieldsIndeces[fieldIndex]) fieldIndex++ return value }) p := &protocol.Point{ Values: make([]*protocol.FieldValue, 0, len(point.Values)-len(fieldsIndeces)), Timestamp: point.Timestamp, SequenceNumber: point.SequenceNumber, } // remove the fields used in the target name from the series fields nextvalue: for i, v := range point.Values { for _, fi := range fieldsIndeces { if fi == i { continue nextvalue } } p.Values = append(p.Values, v) } if assignSequenceNumbers { key := sequenceKey{targetNameWithValues, *p.Timestamp} sequenceMap[key] += 1 sequenceNumber := uint64(sequenceMap[key]) p.SequenceNumber = &sequenceNumber } newSeries := serieses[targetNameWithValues] if newSeries == nil { newSeries = &protocol.Series{Name: &targetNameWithValues, Fields: fields, Points: []*protocol.Point{p}} serieses[targetNameWithValues] = newSeries continue } newSeries.Points = append(newSeries.Points, p) } seriesSlice := make([]*protocol.Series, 0, len(serieses)) for _, s := range serieses { seriesSlice = append(seriesSlice, s) } if e := self.CommitSeriesData(db, seriesSlice, true); e != nil { log.Error("Couldn't write data for continuous query: ", e) } } else { newSeries := &protocol.Series{Name: &targetName, Fields: fields, Points: series.Points} if assignSequenceNumbers { for _, point := range newSeries.Points { sequenceMap[sequenceKey{targetName, *point.Timestamp}] += 1 sequenceNumber := uint64(sequenceMap[sequenceKey{targetName, *point.Timestamp}]) point.SequenceNumber = &sequenceNumber } } if e := self.CommitSeriesData(db, []*protocol.Series{newSeries}, true); e != nil { log.Error("Couldn't write data for continuous query: ", e) } } return nil }
func (self *ShardData) Query(querySpec *parser.QuerySpec, response chan<- *p.Response) { log.Debug("QUERY: shard %d, query '%s'", self.Id(), querySpec.GetQueryStringWithTimeCondition()) defer common.RecoverFunc(querySpec.Database(), querySpec.GetQueryStringWithTimeCondition(), func(err interface{}) { response <- &p.Response{ Type: p.Response_ERROR.Enum(), ErrorMessage: p.String(fmt.Sprintf("%s", err)), } }) // This is only for queries that are deletes or drops. They need to be sent everywhere as opposed to just the local or one of the remote shards. // But this boolean should only be set to true on the server that receives the initial query. if querySpec.RunAgainstAllServersInShard { if querySpec.IsDeleteFromSeriesQuery() { self.logAndHandleDeleteQuery(querySpec, response) } else if querySpec.IsDropSeriesQuery() { self.logAndHandleDropSeriesQuery(querySpec, response) } } if self.IsLocal { var processor engine.Processor = NewResponseChannelProcessor(NewResponseChannelWrapper(response)) var err error processor = NewShardIdInserterProcessor(self.Id(), processor) processor, err = self.getProcessor(querySpec, processor) if err != nil { response <- &p.Response{ Type: p.Response_ERROR.Enum(), ErrorMessage: p.String(err.Error()), } log.Error("Error while creating engine: %s", err) return } shard, err := self.store.GetOrCreateShard(self.id) if err != nil { response <- &p.Response{ Type: p.Response_ERROR.Enum(), ErrorMessage: p.String(err.Error()), } log.Error("Error while getting shards: %s", err) return } defer self.store.ReturnShard(self.id) log.Debug("Processor chain: %s\n", engine.ProcessorChain(processor)) err = shard.Query(querySpec, processor) // if we call Close() in case of an error it will mask the error if err != nil { response <- &p.Response{ Type: p.Response_ERROR.Enum(), ErrorMessage: p.String(err.Error()), } return } processor.Close() response <- &p.Response{Type: p.Response_END_STREAM.Enum()} return } if server := self.randomHealthyServer(); server != nil { log.Debug("Querying server %d for shard %d", server.GetId(), self.Id()) request := self.createRequest(querySpec) server.MakeRequest(request, response) return } message := fmt.Sprintf("No servers up to query shard %d", self.id) response <- &p.Response{ Type: p.Response_ERROR.Enum(), ErrorMessage: &message, } log.Error(message) }