Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make ephemeral storage queryable #3961

Merged
merged 8 commits into from
Jan 17, 2023
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
101 changes: 87 additions & 14 deletions pkg/ingester/ingester.go
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,18 @@ const (

// Prefix used in Prometheus registry for ephemeral storage.
ephemeralPrometheusMetricsPrefix = "ephemeral_"

// Label name used to select queried storage type.
StorageLabelName = "__mimir_storage__"
EphemeralStorageLabelValue = "ephemeral"
PersistentStorageLabelValue = "persistent"

errInvalidStorageLabelValue = "invalid value of " + StorageLabelName + " label: %s"
)

var (
errInvalidStorageMatcherType = fmt.Errorf("invalid matcher used together with %s label, only equality check supported", StorageLabelName)
errMultipleStorageMatchersFound = fmt.Errorf("multiple matchers for %s label found, only one matcher supported", StorageLabelName)
)

// BlocksUploader interface is used to have an easy way to mock it in tests.
Expand Down Expand Up @@ -1099,7 +1111,7 @@ func (i *Ingester) LabelValues(ctx context.Context, req *client.LabelValuesReque
return &client.LabelValuesResponse{}, nil
}

q, err := db.Querier(ctx, startTimestampMs, endTimestampMs)
q, err := db.Querier(ctx, startTimestampMs, endTimestampMs, false)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -1135,7 +1147,7 @@ func (i *Ingester) LabelNames(ctx context.Context, req *client.LabelNamesRequest
return nil, err
}

q, err := db.Querier(ctx, mint, maxt)
q, err := db.Querier(ctx, mint, maxt, false)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -1174,7 +1186,7 @@ func (i *Ingester) MetricsForLabelMatchers(ctx context.Context, req *client.Metr
}

mint, maxt := req.StartTimestampMs, req.EndTimestampMs
q, err := db.Querier(ctx, mint, maxt)
q, err := db.Querier(ctx, mint, maxt, false)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -1363,7 +1375,26 @@ func (i *Ingester) QueryStream(req *client.QueryRequest, stream client.Ingester_
return err
}

i.metrics.queries.Inc()
storageValue, matchers, err := removeStorageMatcher(matchers)
if err != nil {
return err
}

ephemeral := false
if storageValue == "" || storageValue == PersistentStorageLabelValue {
// storageValue is logged later, so set it to proper value.
storageValue = PersistentStorageLabelValue
} else if storageValue == EphemeralStorageLabelValue {
ephemeral = true
} else {
return fmt.Errorf(errInvalidStorageLabelValue, storageValue)
}
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[nit] To keep this function cleaner, you may consider moving all of this to a dedicated function detectStorageMatcher().

Copy link
Member Author

@pstibrany pstibrany Jan 17, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've moved check for accepted label values to removeStorageMatcherAndGetStorageType (previously removeStorageMatcher) in 2c15e15


if ephemeral {
i.metrics.ephemeralQueries.Inc()
} else {
i.metrics.queries.Inc()
}

db := i.getTSDB(userID)
if db == nil {
Expand Down Expand Up @@ -1392,23 +1423,28 @@ func (i *Ingester) QueryStream(req *client.QueryRequest, stream client.Ingester_

if streamType == QueryStreamChunks {
level.Debug(spanlog).Log("msg", "using queryStreamChunks")
numSeries, numSamples, err = i.queryStreamChunks(ctx, db, int64(from), int64(through), matchers, shard, stream)
numSeries, numSamples, err = i.queryStreamChunks(ctx, db, int64(from), int64(through), matchers, shard, stream, ephemeral)
} else {
level.Debug(spanlog).Log("msg", "using queryStreamSamples")
numSeries, numSamples, err = i.queryStreamSamples(ctx, db, int64(from), int64(through), matchers, shard, stream)
numSeries, numSamples, err = i.queryStreamSamples(ctx, db, int64(from), int64(through), matchers, shard, stream, ephemeral)
}
if err != nil {
return err
}

i.metrics.queriedSeries.Observe(float64(numSeries))
i.metrics.queriedSamples.Observe(float64(numSamples))
level.Debug(spanlog).Log("series", numSeries, "samples", numSamples)
if ephemeral {
i.metrics.ephemeralQueriedSeries.Observe(float64(numSeries))
i.metrics.ephemeralQueriedSamples.Observe(float64(numSamples))
} else {
i.metrics.queriedSeries.Observe(float64(numSeries))
i.metrics.queriedSamples.Observe(float64(numSamples))
}
level.Debug(spanlog).Log("series", numSeries, "samples", numSamples, "storage", storageValue)
return nil
}

func (i *Ingester) queryStreamSamples(ctx context.Context, db *userTSDB, from, through int64, matchers []*labels.Matcher, shard *sharding.ShardSelector, stream client.Ingester_QueryStreamServer) (numSeries, numSamples int, _ error) {
q, err := db.Querier(ctx, from, through)
func (i *Ingester) queryStreamSamples(ctx context.Context, db *userTSDB, from, through int64, matchers []*labels.Matcher, shard *sharding.ShardSelector, stream client.Ingester_QueryStreamServer, ephemeral bool) (numSeries, numSamples int, _ error) {
q, err := db.Querier(ctx, from, through, ephemeral)
if err != nil {
return 0, 0, err
}
Expand Down Expand Up @@ -1485,13 +1521,13 @@ func (i *Ingester) queryStreamSamples(ctx context.Context, db *userTSDB, from, t
}

// queryStreamChunks streams metrics from a TSDB. This implements the client.IngesterServer interface
func (i *Ingester) queryStreamChunks(ctx context.Context, db *userTSDB, from, through int64, matchers []*labels.Matcher, shard *sharding.ShardSelector, stream client.Ingester_QueryStreamServer) (numSeries, numSamples int, _ error) {
func (i *Ingester) queryStreamChunks(ctx context.Context, db *userTSDB, from, through int64, matchers []*labels.Matcher, shard *sharding.ShardSelector, stream client.Ingester_QueryStreamServer, ephemeral bool) (numSeries, numSamples int, _ error) {
var q storage.ChunkQuerier
var err error
if i.limits.OutOfOrderTimeWindow(db.userID) > 0 {
q, err = db.UnorderedChunkQuerier(ctx, from, through)
q, err = db.UnorderedChunkQuerier(ctx, from, through, ephemeral)
} else {
q, err = db.ChunkQuerier(ctx, from, through)
q, err = db.ChunkQuerier(ctx, from, through, ephemeral)
}
if err != nil {
return 0, 0, err
Expand Down Expand Up @@ -2649,3 +2685,40 @@ func (i *Ingester) UserRegistryHandler(w http.ResponseWriter, r *http.Request) {
Timeout: 10 * time.Second,
}).ServeHTTP(w, r)
}

// findStorageLabelMatcher returns value of storage label matcher and its index, if it exists.
func findStorageLabelMatcher(matchers []*labels.Matcher) (string, int, error) {
resultVal, resultIdx := "", -1

for idx, matcher := range matchers {
if matcher.Name == StorageLabelName {
if resultIdx >= 0 {
return "", idx, errMultipleStorageMatchersFound
}
if matcher.Type != labels.MatchEqual {
return "", idx, errInvalidStorageMatcherType
}
resultVal = matcher.Value
resultIdx = idx
}
}

return resultVal, resultIdx, nil
}

// removeStorageMatcher returns the value of storage label (or empty string, if not found),
// and input matchers without the storage label matcher. When removing storage label matcher, original slice is reused.
func removeStorageMatcher(matchers []*labels.Matcher) (val string, filtered []*labels.Matcher, _ error) {
val, idx, err := findStorageLabelMatcher(matchers)
if err != nil {
return "", matchers, err
}
if idx < 0 {
return "", matchers, nil
}

// Prepare slice without storage matcher.
copy(matchers[idx:], matchers[idx+1:])
filtered = matchers[:len(matchers)-1]
return val, filtered, nil
}
Loading