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

Fail ingestion when a flush is going on #3422

Merged
merged 7 commits into from
Nov 3, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
- `cortex_ingester_tsdb_head_truncations_total`
- `cortex_ingester_tsdb_head_gc_duration_seconds`
* [ENHANCEMENT] Added `cortex_alertmanager_config_hash` metric to expose hash of Alertmanager Config loaded per user. #3388
* [BUGFIX] Blocks storage: Fix the race between ingestion and `/flush` call resulting in overlapping blocks. #3422

## 1.5.0 in progress

Expand Down
15 changes: 15 additions & 0 deletions pkg/ingester/ingester_v2.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,9 @@ type userTSDB struct {
seriesInMetric *metricCounter
limiter *Limiter

flushInProgress atomic.Bool
pushesInFlight sync.WaitGroup

// Used to detect idle TSDBs.
lastUpdate *atomic.Int64

Expand Down Expand Up @@ -501,6 +504,12 @@ func (i *Ingester) v2Push(ctx context.Context, req *client.WriteRequest) (*clien
}
i.userStatesMtx.RUnlock()

if db.flushInProgress.Load() {
return &client.WriteResponse{}, httpgrpc.Errorf(http.StatusServiceUnavailable, wrapWithUser(errors.New("flush in progress"), userID).Error())
}
db.pushesInFlight.Add(1)
defer db.pushesInFlight.Done()

// Given metadata is a best-effort approach, and we don't halt on errors
// process it before samples. Otherwise, we risk returning an error before ingestion.
i.pushMetadata(ctx, userID, req.GetMetadata())
Expand Down Expand Up @@ -1361,6 +1370,12 @@ func (i *Ingester) compactBlocks(ctx context.Context, force bool) {
switch {
case force:
reason = "forced"

userDB.flushInProgress.Store(true)
defer userDB.flushInProgress.Store(false)
// Ingestion of samples in parallel with forced compaction can lead to overlapping blocks.
// So we wait for existing in-flight requests to finish. Future push requests would fail until compaction is over.
userDB.pushesInFlight.Wait()
err = userDB.compactHead(i.cfg.BlocksStorageConfig.TSDB.BlockRanges[0].Milliseconds())

case i.cfg.BlocksStorageConfig.TSDB.HeadCompactionIdleTimeout > 0 && userDB.isIdle(time.Now(), i.cfg.BlocksStorageConfig.TSDB.HeadCompactionIdleTimeout):
Expand Down
36 changes: 36 additions & 0 deletions pkg/ingester/ingester_v2_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2422,3 +2422,39 @@ func TestIngesterNotDeleteUnshippedBlocks(t *testing.T) {
require.NotEqual(t, b.Meta().ULID, newBlocks2[1].Meta().ULID)
}
}

func TestIngesterPushErrorDuringFlush(t *testing.T) {
i, cleanup, err := newIngesterMockWithTSDBStorage(defaultIngesterTestConfig(), nil)
require.NoError(t, err)
t.Cleanup(cleanup)
codesome marked this conversation as resolved.
Show resolved Hide resolved

require.NoError(t, services.StartAndAwaitRunning(context.Background(), i))
t.Cleanup(func() {
_ = services.StopAndAwaitTerminated(context.Background(), i)
})

// Wait until it's ACTIVE
test.Poll(t, 10*time.Millisecond, ring.ACTIVE, func() interface{} {
return i.lifecycler.GetState()
})

// Push a sample, it should succeed.
pushSingleSample(t, i)

// We mock a flushing by setting the boolean.
db := i.getTSDB(userID)
require.NotNil(t, db)
require.False(t, db.flushInProgress.Load())
db.flushInProgress.Store(true)

// Ingestion should fail with a 503.
req, _, _ := mockWriteRequest(labels.Labels{{Name: labels.MetricName, Value: "test"}}, 0, util.TimeToMillis(time.Now()))
ctx := user.InjectOrgID(context.Background(), userID)
_, err = i.v2Push(ctx, req)
require.Equal(t, httpgrpc.Errorf(http.StatusServiceUnavailable, wrapWithUser(errors.New("flush in progress"), userID).Error()), err)

// Ingestion is successful after a flush.
require.True(t, db.flushInProgress.Load())
db.flushInProgress.Store(false)
pushSingleSample(t, i)
}