-
Notifications
You must be signed in to change notification settings - Fork 528
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
sampling: fix pubsub implementation (#5126)
* sampling: fix pubsub implementation The initial implementation was written as a ~quick hack, with the expectation that it would be replaced by the Changes API. It was broken due to its ignorance of data streams, and multi-shard indices. Sequence numbers are only comparable within a single shard. Given that there is no known delivery date for the Changes API, we propose to instead revise the pubsub implementation to address the problems by: - enforcing single-shard indices for sampled trace data streams - searching (now single-shard) backing indices individually In addition, we now use global checkpoints to bound searches, and use PIT (point in time) for paging through results. Querying underlying indices and global checkpoints requires an additional "monitor" index privilege. * sampling/pubsub: remove PIT again Simplify by just using direct searches with a rnage on _seq_no, using the most recently observed _seq_no value as the lower bound. We can do this within the loop as well (i.e. until there are no more results, or we've observed the global checkpoint.) * sampling/pubsub: only query get metric from _stats * pubsub: force-refresh indices Refresh indices after observing an updated global checkpoint to ensure document visibility is correct up to the observed global checkpoint. * Update changelog * systemtest: fix spurious test failure
- Loading branch information
Showing
9 changed files
with
619 additions
and
297 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
// Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
// or more contributor license agreements. Licensed under the Elastic License; | ||
// you may not use this file except in compliance with the Elastic License. | ||
|
||
package pubsub | ||
|
||
import ( | ||
"context" | ||
"encoding/json" | ||
"fmt" | ||
"io/ioutil" | ||
"net/http" | ||
|
||
"github.com/pkg/errors" | ||
|
||
"github.com/elastic/go-elasticsearch/v7/esapi" | ||
|
||
"github.com/elastic/apm-server/elasticsearch" | ||
) | ||
|
||
// getGlobalCheckpoints returns the current global checkpoint for each index | ||
// underlying dataStream. Each index is required to have a single (primary) shard. | ||
func getGlobalCheckpoints( | ||
ctx context.Context, | ||
client elasticsearch.Client, | ||
dataStream string, | ||
) (map[string]int64, error) { | ||
indexGlobalCheckpoints := make(map[string]int64) | ||
resp, err := esapi.IndicesStatsRequest{ | ||
Index: []string{dataStream}, | ||
Level: "shards", | ||
// By default all metrics are returned; query just the "get" metric, | ||
// which is very cheap. | ||
Metric: []string{"get"}, | ||
}.Do(ctx, client) | ||
if err != nil { | ||
return nil, errors.New("index stats request failed") | ||
} | ||
defer resp.Body.Close() | ||
if resp.IsError() { | ||
switch resp.StatusCode { | ||
case http.StatusNotFound: | ||
// Data stream does not yet exist. | ||
return indexGlobalCheckpoints, nil | ||
} | ||
message, _ := ioutil.ReadAll(resp.Body) | ||
return nil, fmt.Errorf("index stats request failed: %s", message) | ||
} | ||
|
||
var stats dataStreamStats | ||
if err := json.NewDecoder(resp.Body).Decode(&stats); err != nil { | ||
return nil, err | ||
} | ||
|
||
for index, indexStats := range stats.Indices { | ||
if n := len(indexStats.Shards); n > 1 { | ||
return nil, fmt.Errorf("expected 1 shard, got %d for index %q", n, index) | ||
} | ||
for _, shardStats := range indexStats.Shards { | ||
for _, shardStats := range shardStats { | ||
if shardStats.Routing.Primary { | ||
indexGlobalCheckpoints[index] = shardStats.SeqNo.GlobalCheckpoint | ||
break | ||
} | ||
} | ||
} | ||
} | ||
return indexGlobalCheckpoints, nil | ||
} | ||
|
||
type dataStreamStats struct { | ||
Indices map[string]indexStats `json:"indices"` | ||
} | ||
|
||
type indexStats struct { | ||
Shards map[string][]shardStats `json:"shards"` | ||
} | ||
|
||
type shardStats struct { | ||
Routing struct { | ||
Primary bool `json:"primary"` | ||
} `json:"routing"` | ||
SeqNo struct { | ||
GlobalCheckpoint int64 `json:"global_checkpoint"` | ||
} `json:"seq_no"` | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.