Skip to content

fix(opensearch): propagate batch context to bulk flushes - #1174

Open
pcbeingused333 wants to merge 3 commits into
warpstreamlabs:mainfrom
pcbeingused333:fix/opensearch-bulk-flush-ctx
Open

pcbeingused333 wants to merge 3 commits into
warpstreamlabs:mainfrom
pcbeingused333:fix/opensearch-bulk-flush-ctx

Conversation

@pcbeingused333

@pcbeingused333 pcbeingused333 commented Nov 19, 2026 •

Copy link
Copy Markdown
Contributor

Fixes the opensearch output ignoring the batch context for bulk flushes triggered while a batch is still being added.

Cause: WriteBatch built its BulkIndexer without a context, so flushes ran under context.Background() and a stalled cluster kept the request alive after the batch context was cancelled.

Fix: upgrade opensearch-go/v4 to v4.8.2 and pass a context tied to the batch through BulkIndexerConfig.Context. NewBulkIndexer panics if that context is cancelled while workers are starting (opensearch-project/opensearch-go#1392), so an already cancelled batch returns early and the cancellation is only linked once the indexer exists. integration_test.go is migrated to the generic Do API and gains an interrupted-write case.

The docker-based integration tests compile and vet clean but have not been run.

This change was prepared with the help of an AI coding assistant (Claude).

@gregfurman gregfurman left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

This appears to be fixed in v4.7.0 of the opensearch-go client, with the inclusion of a short-lived Context attribute.

See https://github.com/opensearch-project/opensearch-go/blob/443927db1f7b94d0eebce95ccb7c85ea631c6c7e/opensearchutil/bulk_indexer.go#L86-L88

In addition to the worker.run now allowing a context to be passed in:

https://github.com/opensearch-project/opensearch-go/blob/443927db1f7b94d0eebce95ccb7c85ea631c6c7e/opensearchutil/bulk_indexer.go#L406-L407

I'd prefer if this PR rather upgraded to the latest version of v4 on v4.8.2 (see https://github.com/opensearch-project/opensearch-go/blob/v4.8.2/CHANGELOG.md) and migrated over the tests in integration_test.go to account for the deprecations + changes.

…ch-go to v4.8.2

opensearch-go v4.7 added BulkIndexerConfig.Context for the worker
lifecycle. Use it instead of OnFlushStart, bump the client to v4.8.2 and
migrate integration_test.go to the generic Do API.
@pcbeingused333

Copy link
Copy Markdown
Contributor Author

Thanks, agreed. Upgraded to v4.8.2 and switched to BulkIndexerConfig.Context (the regression test still fails on v4.8.2 without it, so setting the context is still needed). Migrated integration_test.go to the generic Do; the docker integration tests compile but I could not run them here.

Comment thread CHANGELOG.md Outdated

### Fixed

- `opensearch` output now cancels in-flight bulk requests triggered mid-batch when the batch context is cancelled (upgrades `opensearch-go` to v4.8.2)

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

nit: Upgrading the dependency should probably be in the Changed section (which should be before Fixed):

### Changed

- upgrade `opensearch-go` from `v4.7.0` to `v4.8.2` @pcbeingused333 

Then we can remove the (upgrades ...) trailer

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Want to just confirm: have you run these locally?

@gregfurman gregfurman left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Appears that this new approach will panic if we pass in an already cancelled context.Context to the NewBulkIndexer. I've opened an issue upstream for this opensearch-project/opensearch-go#1392

Also, this needs to run make deps or go mod tidy since it seems the go.sum is stale.

Comment thread internal/impl/opensearch/output_test.go Outdated

require.NoError(t, o.Connect(t.Context()))

ctx, cancel := context.WithTimeout(t.Context(), 300*time.Millisecond)

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

FYI this panics when the timeout is 0 and workers are greater than 1 -- since the upstream bulk indexer appears to have a bug.

Comment thread internal/impl/opensearch/output_test.go Outdated

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

IMO this entire test can be replaced with a simple additional integration test:

func testOpenSearchInterruptWrite(urls []string, client *osapi.Client, t *testing.T) {
	ctx, done := context.WithTimeout(context.Background(), time.Second*30)
	defer done()

	m := outputFromConf(t, `
index: does_not_exist
id: 'foo-${!count("interruptWriteTest")}'
urls: %v
action: index
`, urls)

	require.NoError(t, m.Connect(ctx))
	defer func() {
		require.NoError(t, m.Close(ctx))
	}()

	cancelCtx, cancel := context.WithCancel(ctx)
	cancel()
	err := m.WriteBatch(cancelCtx, service.MessageBatch{
		service.NewMessage([]byte(`{"message":"hello world","user":"1"}`)),
	})
	require.ErrorIs(t, err, context.Canceled)
}

NewBulkIndexer panics if its context is cancelled while the workers are
starting (opensearch-project/opensearch-go#1392). Return early for an
already cancelled batch and only link the batch context to the workers
once the indexer has been created.

Also tidy go.sum, move the dependency bump to Changed in the CHANGELOG
and replace the httptest test with an integration test.
@pcbeingused333

Copy link
Copy Markdown
Contributor Author

Pushed a fix for the panic. An early ctx.Err() check alone would still leave a window if the context is cancelled while NewBulkIndexer is starting its workers, so the indexer now gets a context that is only linked to the batch context (context.AfterFunc) after the constructor has returned. I also ran go mod tidy, moved the bump to Changed (it is v4.5.0 to v4.8.2, since that is what main has) and replaced the unit test with your integration test.

On your question: no, the integration tests have not been run yet. They compile and vet clean, and the cancelled-context case was checked with a temporary unit test (panics before this commit, passes after). Note that the new integration test covers an already cancelled context, not cancellation while a request is in flight.

This comment was drafted with Claude Code.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants