-
Notifications
You must be signed in to change notification settings - Fork 846
discard ooo samples in some special cases #7227
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
Open
Shvejan
wants to merge
2
commits into
cortexproject:master
Choose a base branch
from
Shvejan:master
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+464
−96
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains hidden or 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 hidden or 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 hidden or 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,205 @@ | ||
| package ingester | ||
|
|
||
| import ( | ||
| "context" | ||
| "testing" | ||
| "time" | ||
|
|
||
| "github.com/prometheus/client_golang/prometheus" | ||
| "github.com/prometheus/common/model" | ||
| "github.com/prometheus/prometheus/model/labels" | ||
| "github.com/prometheus/prometheus/storage" | ||
| "github.com/prometheus/prometheus/tsdb/chunkenc" | ||
| "github.com/stretchr/testify/assert" | ||
| "github.com/stretchr/testify/require" | ||
| "github.com/weaveworks/common/user" | ||
|
|
||
| "github.com/cortexproject/cortex/pkg/cortexpb" | ||
| "github.com/cortexproject/cortex/pkg/ingester/client" | ||
| "github.com/cortexproject/cortex/pkg/ring" | ||
| "github.com/cortexproject/cortex/pkg/util/services" | ||
| "github.com/cortexproject/cortex/pkg/util/test" | ||
| ) | ||
|
|
||
| // mockAppender implements the extendedAppender interface for testing | ||
| type mockAppender struct { | ||
| storage.Appender | ||
| lastOptions *storage.AppendOptions | ||
| } | ||
|
|
||
| func (m *mockAppender) SetOptions(opts *storage.AppendOptions) { | ||
| m.lastOptions = opts | ||
| } | ||
|
|
||
| func TestIngester_Push_DiscardOutOfOrder_True(t *testing.T) { | ||
| req := &cortexpb.WriteRequest{ | ||
| Source: cortexpb.RULE, | ||
| DiscardOutOfOrder: true, | ||
| Timeseries: []cortexpb.PreallocTimeseries{}, | ||
| } | ||
|
|
||
| assert.True(t, req.DiscardOutOfOrder, "DiscardOutOfOrder should be true") | ||
| assert.True(t, req.GetDiscardOutOfOrder(), "GetDiscardOutOfOrder should return true") | ||
| } | ||
|
|
||
| func TestIngester_Push_DiscardOutOfOrder_Default(t *testing.T) { | ||
| // Create a WriteRequest without setting DiscardOutOfOrder | ||
| req := &cortexpb.WriteRequest{ | ||
| Source: cortexpb.API, | ||
| Timeseries: []cortexpb.PreallocTimeseries{}, | ||
| } | ||
|
|
||
| // Verify the default value is false | ||
| assert.False(t, req.DiscardOutOfOrder, "DiscardOutOfOrder should default to false") | ||
| assert.False(t, req.GetDiscardOutOfOrder(), "GetDiscardOutOfOrder should return false by default") | ||
| } | ||
|
|
||
| func TestIngester_WriteRequest_MultipleScenarios(t *testing.T) { | ||
| scenarios := []struct { | ||
| name string | ||
| setupReq func() *cortexpb.WriteRequest | ||
| expectOpts bool | ||
| description string | ||
| }{ | ||
| { | ||
| name: "Stale marker during rule migration", | ||
| setupReq: func() *cortexpb.WriteRequest { | ||
| return &cortexpb.WriteRequest{ | ||
| Source: cortexpb.RULE, | ||
| DiscardOutOfOrder: true, | ||
| } | ||
| }, | ||
| expectOpts: true, | ||
| description: "Should set appender options to discard OOO", | ||
| }, | ||
| { | ||
| name: "Normal rule evaluation", | ||
| setupReq: func() *cortexpb.WriteRequest { | ||
| return &cortexpb.WriteRequest{ | ||
| Source: cortexpb.RULE, | ||
| DiscardOutOfOrder: false, | ||
| } | ||
| }, | ||
| expectOpts: false, | ||
| description: "Should not set appender options", | ||
| }, | ||
| { | ||
| name: "API write request", | ||
| setupReq: func() *cortexpb.WriteRequest { | ||
| return &cortexpb.WriteRequest{ | ||
| Source: cortexpb.API, | ||
| DiscardOutOfOrder: false, | ||
| } | ||
| }, | ||
| expectOpts: false, | ||
| description: "API requests should never trigger OOO discard", | ||
| }, | ||
| { | ||
| name: "Default values", | ||
| setupReq: func() *cortexpb.WriteRequest { | ||
| return &cortexpb.WriteRequest{} | ||
| }, | ||
| expectOpts: false, | ||
| description: "Default values should not trigger OOO discard", | ||
| }, | ||
| } | ||
|
|
||
| for _, scenario := range scenarios { | ||
| t.Run(scenario.name, func(t *testing.T) { | ||
| req := scenario.setupReq() | ||
| mock := &mockAppender{} | ||
|
|
||
| // Simulate the ingester logic | ||
| if req.DiscardOutOfOrder { | ||
| mock.SetOptions(&storage.AppendOptions{DiscardOutOfOrder: true}) | ||
| } | ||
|
|
||
| // Verify expectations | ||
| if scenario.expectOpts { | ||
| require.NotNil(t, mock.lastOptions) | ||
| assert.True(t, mock.lastOptions.DiscardOutOfOrder) | ||
| } | ||
| }) | ||
| } | ||
| } | ||
|
|
||
| func TestIngester_DiscardOutOfOrderFlagIngegrationTest(t *testing.T) { | ||
| registry := prometheus.NewRegistry() | ||
| cfg := defaultIngesterTestConfig(t) | ||
| cfg.LifecyclerConfig.JoinAfter = 0 | ||
|
|
||
| limits := defaultLimitsTestConfig() | ||
| limits.EnableNativeHistograms = true | ||
| limits.OutOfOrderTimeWindow = model.Duration(60 * time.Minute) | ||
|
|
||
| i, err := prepareIngesterWithBlocksStorageAndLimits(t, cfg, limits, nil, "", registry) | ||
| require.NoError(t, err) | ||
| require.NoError(t, services.StartAndAwaitRunning(context.Background(), i)) | ||
| defer services.StopAndAwaitTerminated(context.Background(), i) //nolint:errcheck | ||
|
|
||
| // Wait until it's ACTIVE | ||
| test.Poll(t, 100*time.Millisecond, ring.ACTIVE, func() any { | ||
| return i.lifecycler.GetState() | ||
| }) | ||
|
|
||
| ctx := user.InjectOrgID(context.Background(), "test-user") | ||
|
|
||
| // Create labels for our test metric | ||
| metricLabels := labels.FromStrings("__name__", "test_metric", "job", "test") | ||
|
|
||
| currentTime := time.Now().UnixMilli() | ||
| olderTime := currentTime - 60000 // 1 minute earlier (within OOO window) | ||
|
|
||
| // First, push a sample with current timestamp with discardOutOfOrder=true | ||
| req1 := cortexpb.ToWriteRequest( | ||
| []labels.Labels{metricLabels}, | ||
| []cortexpb.Sample{{Value: 100, TimestampMs: currentTime}}, | ||
| nil, nil, cortexpb.RULE) | ||
| req1.DiscardOutOfOrder = true | ||
|
|
||
| _, err = i.Push(ctx, req1) | ||
| require.NoError(t, err, "First sample push should succeed") | ||
|
|
||
| // Now try to push a sample with older timestamp with discardOutOfOrder=true | ||
| // This should be discarded because DiscardOutOfOrder is true | ||
| req2 := cortexpb.ToWriteRequest( | ||
| []labels.Labels{metricLabels}, | ||
| []cortexpb.Sample{{Value: 50, TimestampMs: olderTime}}, | ||
| nil, nil, cortexpb.RULE) | ||
| req2.DiscardOutOfOrder = true | ||
|
|
||
| _, _ = i.Push(ctx, req2) | ||
|
|
||
| // Query back the data to ensure only the first (current time) sample was stored | ||
| s := &mockQueryStreamServer{ctx: ctx} | ||
| err = i.QueryStream(&client.QueryRequest{ | ||
| StartTimestampMs: olderTime - 1000, | ||
| EndTimestampMs: currentTime + 1000, | ||
| Matchers: []*client.LabelMatcher{ | ||
| {Type: client.EQUAL, Name: "__name__", Value: "test_metric"}, | ||
| }, | ||
| }, s) | ||
| require.NoError(t, err) | ||
|
|
||
| // Verify we only have one series with one sample (the current time sample) | ||
| require.Len(t, s.series, 1, "Should have exactly one series") | ||
|
|
||
| // Convert chunks to samples to verify content | ||
| series := s.series[0] | ||
| require.Len(t, series.Chunks, 1, "Should have exactly one chunk") | ||
|
|
||
| chunk := series.Chunks[0] | ||
| chunkData, err := chunkenc.FromData(chunkenc.EncXOR, chunk.Data) | ||
| require.NoError(t, err) | ||
|
|
||
| iter := chunkData.Iterator(nil) | ||
| sampleCount := 0 | ||
| for iter.Next() != chunkenc.ValNone { | ||
| ts, val := iter.At() | ||
| require.Equal(t, currentTime, ts, "Sample timestamp should match current time") | ||
| require.Equal(t, 100.0, val, "Sample value should match first push") | ||
| sampleCount++ | ||
| } | ||
| require.NoError(t, iter.Err()) | ||
| require.Equal(t, 1, sampleCount, "Should have exactly one sample stored") | ||
| } | ||
This file contains hidden or 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 hidden or 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,93 @@ | ||
| package ruler | ||
|
|
||
| import ( | ||
| "context" | ||
| "testing" | ||
|
|
||
| "github.com/prometheus/client_golang/prometheus" | ||
| "github.com/prometheus/prometheus/model/labels" | ||
| "github.com/prometheus/prometheus/storage" | ||
| "github.com/stretchr/testify/assert" | ||
| "github.com/stretchr/testify/require" | ||
|
|
||
| "github.com/cortexproject/cortex/pkg/cortexpb" | ||
| ) | ||
|
|
||
| type mockPusher struct { | ||
| lastRequest *cortexpb.WriteRequest | ||
| pushError error | ||
| } | ||
|
|
||
| func (m *mockPusher) Push(ctx context.Context, req *cortexpb.WriteRequest) (*cortexpb.WriteResponse, error) { | ||
| m.lastRequest = req | ||
| return &cortexpb.WriteResponse{}, m.pushError | ||
| } | ||
|
|
||
| func TestPusherAppender_Commit_WithDiscardOutOfOrder(t *testing.T) { | ||
| mock := &mockPusher{} | ||
| counter := prometheus.NewCounter(prometheus.CounterOpts{Name: "test"}) | ||
|
|
||
| appender := &PusherAppender{ | ||
| ctx: context.Background(), | ||
| pusher: mock, | ||
| userID: "test-user", | ||
| totalWrites: counter, | ||
| failedWrites: counter, | ||
| labels: []labels.Labels{labels.FromStrings("__name__", "test_metric")}, | ||
| samples: []cortexpb.Sample{{TimestampMs: 1000, Value: 1.0}}, | ||
| } | ||
|
|
||
| appender.SetOptions(&storage.AppendOptions{DiscardOutOfOrder: true}) | ||
|
|
||
| err := appender.Commit() | ||
| require.NoError(t, err) | ||
|
|
||
| // Verify that DiscardOutOfOrder was set in the WriteRequest | ||
| require.NotNil(t, mock.lastRequest, "WriteRequest should have been sent") | ||
| assert.True(t, mock.lastRequest.DiscardOutOfOrder, "DiscardOutOfOrder should be true in WriteRequest") | ||
| } | ||
|
|
||
| func TestPusherAppender_Commit_WithoutDiscardOutOfOrder(t *testing.T) { | ||
| mock := &mockPusher{} | ||
| counter := prometheus.NewCounter(prometheus.CounterOpts{Name: "test"}) | ||
|
|
||
| appender := &PusherAppender{ | ||
| ctx: context.Background(), | ||
| pusher: mock, | ||
| userID: "test-user", | ||
| totalWrites: counter, | ||
| failedWrites: counter, | ||
| labels: []labels.Labels{labels.FromStrings("__name__", "test_metric")}, | ||
| samples: []cortexpb.Sample{{TimestampMs: 1000, Value: 1.0}}, | ||
| } | ||
|
|
||
| appender.SetOptions(&storage.AppendOptions{DiscardOutOfOrder: false}) | ||
|
|
||
| err := appender.Commit() | ||
| require.NoError(t, err) | ||
|
|
||
| require.NotNil(t, mock.lastRequest, "WriteRequest should have been sent") | ||
| assert.False(t, mock.lastRequest.DiscardOutOfOrder, "DiscardOutOfOrder should be false in WriteRequest") | ||
| } | ||
|
|
||
| func TestPusherAppender_Commit_WithNilOptions(t *testing.T) { | ||
| mock := &mockPusher{} | ||
| counter := prometheus.NewCounter(prometheus.CounterOpts{Name: "test"}) | ||
|
|
||
| appender := &PusherAppender{ | ||
| ctx: context.Background(), | ||
| pusher: mock, | ||
| userID: "test-user", | ||
| totalWrites: counter, | ||
| failedWrites: counter, | ||
| labels: []labels.Labels{labels.FromStrings("__name__", "test_metric")}, | ||
| samples: []cortexpb.Sample{{TimestampMs: 1000, Value: 1.0}}, | ||
| opts: nil, // Explicitly nil | ||
| } | ||
|
|
||
| err := appender.Commit() | ||
| require.NoError(t, err) | ||
|
|
||
| require.NotNil(t, mock.lastRequest, "WriteRequest should have been sent") | ||
| assert.False(t, mock.lastRequest.DiscardOutOfOrder, "DiscardOutOfOrder should be false when opts is nil") | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.