queryIngesterStream queries the ingesters using the new streaming API.
(ctx context.Context, replicationSet ring.ReplicationSet, req *ingester_client.QueryRequest, partialDataEnabled bool)
| 223 | |
| 224 | // queryIngesterStream queries the ingesters using the new streaming API. |
| 225 | func (d *Distributor) queryIngesterStream(ctx context.Context, replicationSet ring.ReplicationSet, req *ingester_client.QueryRequest, partialDataEnabled bool) (*ingester_client.QueryStreamResponse, error) { |
| 226 | var ( |
| 227 | queryLimiter = limiter.QueryLimiterFromContextWithFallback(ctx) |
| 228 | reqStats = stats.FromContext(ctx) |
| 229 | ) |
| 230 | |
| 231 | // Fetch samples from multiple ingesters |
| 232 | results, err := replicationSet.Do(ctx, d.cfg.ExtraQueryDelay, false, partialDataEnabled, func(ctx context.Context, ing *ring.InstanceDesc) (any, error) { |
| 233 | client, err := d.ingesterPool.GetClientFor(ing.Addr) |
| 234 | if err != nil { |
| 235 | return nil, err |
| 236 | } |
| 237 | |
| 238 | ingesterId, err := d.ingestersRing.GetInstanceIdByAddr(ing.Addr) |
| 239 | if err != nil { |
| 240 | level.Warn(d.log).Log("msg", "instance not found in the ring", "addr", ing.Addr, "err", err) |
| 241 | } |
| 242 | |
| 243 | d.ingesterQueries.WithLabelValues(ingesterId).Inc() |
| 244 | |
| 245 | stream, err := client.(ingester_client.IngesterClient).QueryStream(ctx, req) |
| 246 | if err != nil { |
| 247 | d.ingesterQueryFailures.WithLabelValues(ingesterId).Inc() |
| 248 | return nil, err |
| 249 | } |
| 250 | defer stream.CloseSend() //nolint:errcheck |
| 251 | |
| 252 | result := &ingester_client.QueryStreamResponse{} |
| 253 | for { |
| 254 | resp, err := stream.Recv() |
| 255 | if err == io.EOF { |
| 256 | break |
| 257 | } else if err != nil { |
| 258 | // Do not track a failure if the context was canceled. |
| 259 | if !grpcutil.IsGRPCContextCanceled(err) { |
| 260 | d.ingesterQueryFailures.WithLabelValues(ingesterId).Inc() |
| 261 | } |
| 262 | |
| 263 | return nil, err |
| 264 | } |
| 265 | |
| 266 | // Enforce the max chunks limits. |
| 267 | if chunkLimitErr := queryLimiter.AddChunks(resp.ChunksCount()); chunkLimitErr != nil { |
| 268 | return nil, validation.LimitError(chunkLimitErr.Error()) |
| 269 | } |
| 270 | |
| 271 | s := make([][]cortexpb.LabelAdapter, 0, len(resp.Chunkseries)) |
| 272 | for _, series := range resp.Chunkseries { |
| 273 | s = append(s, series.Labels) |
| 274 | } |
| 275 | |
| 276 | if limitErr := queryLimiter.AddSeries(s...); limitErr != nil { |
| 277 | return nil, validation.LimitError(limitErr.Error()) |
| 278 | } |
| 279 | |
| 280 | if chunkBytesLimitErr := queryLimiter.AddChunkBytes(resp.ChunksSize()); chunkBytesLimitErr != nil { |
| 281 | return nil, validation.LimitError(chunkBytesLimitErr.Error()) |
| 282 | } |
no test coverage detected