-
Notifications
You must be signed in to change notification settings - Fork 2.5k
Prevent request corruption by avoiding failing entire bulk request on single shard rejection from shard backpressure #20727
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
varunbharadwaj
wants to merge
2
commits into
opensearch-project:main
Choose a base branch
from
varunbharadwaj:vb/shardindexpressure
base: main
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.
+206
−58
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
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 |
|---|---|---|
|
|
@@ -120,6 +120,7 @@ | |
| import java.util.concurrent.TimeUnit; | ||
| import java.util.concurrent.atomic.AtomicInteger; | ||
| import java.util.concurrent.atomic.AtomicIntegerArray; | ||
| import java.util.function.Consumer; | ||
| import java.util.function.LongSupplier; | ||
| import java.util.stream.Collectors; | ||
| import java.util.stream.Stream; | ||
|
|
@@ -723,6 +724,8 @@ protected void doRun() { | |
| final DocStatusStats docStatusStats = new DocStatusStats(); | ||
| String nodeId = clusterService.localNode().getId(); | ||
|
|
||
| final boolean isOnlySystem = isOnlySystem(bulkRequest, clusterService.state().metadata().getIndicesLookup(), systemIndices); | ||
|
|
||
| for (Map.Entry<ShardId, List<BulkItemRequest>> entry : requestsByShard.entrySet()) { | ||
| final ShardId shardId = entry.getKey(); | ||
| final List<BulkItemRequest> requests = entry.getValue(); | ||
|
|
@@ -737,20 +740,50 @@ protected void doRun() { | |
| if (task != null) { | ||
| bulkShardRequest.setParentTask(nodeId, task.getId()); | ||
| } | ||
| final long startTimeNanos = relativeTime(); | ||
| final long shardStartTimeNanos = relativeTime(); | ||
| final ShardRouting primary = routingTable.shardRoutingTable(shardId).primaryShard(); | ||
| String targetNodeId = primary != null ? primary.currentNodeId() : null; | ||
| final String targetNodeId = primary != null ? primary.currentNodeId() : null; | ||
| IndexMetadata indexMetaData = clusterState.metadata().index(shardId.getIndexName()); | ||
| boolean bulkAdaptiveShardSelectionEnabled = indexMetaData.isAppendOnlyIndex() | ||
| final boolean bulkAdaptiveShardSelectionEnabled = indexMetaData.isAppendOnlyIndex() | ||
| && indexMetaData.bulkAdaptiveShardSelectionEnabled(); | ||
|
|
||
| // Add the shard level accounting for coordinating and supply the listener | ||
| final boolean isOnlySystem = isOnlySystem(bulkRequest, clusterService.state().metadata().getIndicesLookup(), systemIndices); | ||
| final Releasable releasable = indexingPressureService.markCoordinatingOperationStarted( | ||
| shardId, | ||
| bulkShardRequest::ramBytesUsed, | ||
| isOnlySystem | ||
| ); | ||
| // Define the failure handler that can be reused for both pressure check failures and execution failures | ||
| final Consumer<Exception> onShardFailure = (Exception e) -> { | ||
| // create failures for all relevant requests | ||
| for (BulkItemRequest request : requests) { | ||
| final String indexName = concreteIndices.getConcreteIndex(request.index()).getName(); | ||
| final DocWriteRequest<?> docWriteRequest = request.request(); | ||
| final BulkItemResponse bulkItemResponse = new BulkItemResponse( | ||
| request.id(), | ||
| docWriteRequest.opType(), | ||
| new BulkItemResponse.Failure(indexName, docWriteRequest.id(), e) | ||
| ); | ||
|
|
||
| docStatusStats.inc(bulkItemResponse.status()); | ||
| responses.set(request.id(), bulkItemResponse); | ||
| } | ||
|
|
||
| if (counter.decrementAndGet() == 0) { | ||
| indicesService.addDocStatusStats(docStatusStats); | ||
| listener.onResponse( | ||
| new BulkResponse(responses.toArray(new BulkItemResponse[responses.length()]), buildTookInMillis(startTimeNanos)) | ||
| ); | ||
| } | ||
| }; | ||
|
|
||
| // Try to acquire shard-level indexing pressure permit | ||
| final Releasable releasable; | ||
| try { | ||
| releasable = indexingPressureService.markCoordinatingOperationStarted( | ||
| shardId, | ||
| bulkShardRequest::ramBytesUsed, | ||
| isOnlySystem | ||
| ); | ||
| } catch (Exception e) { | ||
| // If pressure check fails, treat it as a shard-level failure. | ||
| onShardFailure.accept(e); | ||
| continue; | ||
| } | ||
|
|
||
| final Span span = tracer.startSpan(SpanBuilder.from("bulkShardAction", nodeId, bulkShardRequest)); | ||
| boolean incrementedConnections = false; | ||
|
|
@@ -777,7 +810,7 @@ public void onResponse(BulkShardResponse bulkShardResponse) { | |
| nodeMetricsCollector.addNodeStatistics( | ||
| targetNodeId, | ||
| bulkShardResponse.getNodeQueueSize(), | ||
| relativeTime() - startTimeNanos, | ||
| relativeTime() - shardStartTimeNanos, | ||
| bulkShardResponse.getServiceTimeEWMAInNanos() | ||
| ); | ||
| } | ||
|
|
@@ -798,23 +831,7 @@ public void onResponse(BulkShardResponse bulkShardResponse) { | |
|
|
||
| @Override | ||
| public void onFailure(Exception e) { | ||
| // create failures for all relevant requests | ||
| for (BulkItemRequest request : requests) { | ||
| final String indexName = concreteIndices.getConcreteIndex(request.index()).getName(); | ||
| final DocWriteRequest<?> docWriteRequest = request.request(); | ||
| final BulkItemResponse bulkItemResponse = new BulkItemResponse( | ||
| request.id(), | ||
| docWriteRequest.opType(), | ||
| new BulkItemResponse.Failure(indexName, docWriteRequest.id(), e) | ||
| ); | ||
|
|
||
| docStatusStats.inc(bulkItemResponse.status()); | ||
| responses.set(request.id(), bulkItemResponse); | ||
| } | ||
|
|
||
| if (counter.decrementAndGet() == 0) { | ||
| finishHim(); | ||
| } | ||
| onShardFailure.accept(e); | ||
| } | ||
|
|
||
| private void finishHim() { | ||
|
|
@@ -842,7 +859,8 @@ private void finishHim() { | |
| } | ||
| span.setError(e); | ||
| span.endSpan(); | ||
| throw e; | ||
| // Treat execution failures as shard-level failures rather than failing the entire bulk request | ||
| onShardFailure.accept(e); | ||
|
Comment on lines
-845
to
+863
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Looks like we need a test for this, unless an existing test already covers this? |
||
| } | ||
| } | ||
| bulkRequest = null; // allow memory for bulk request items to be reclaimed before all items have been completed | ||
|
|
||
Oops, something went wrong.
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.
Can we avoid duplicating finishHim()?