Skip to content

Simplify check to split bulk request #124035

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

Merged
merged 15 commits into from
Mar 4, 2025
Merged
Show file tree
Hide file tree
Changes from all 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
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Optional;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.function.Supplier;

Expand Down Expand Up @@ -133,8 +134,9 @@ public void addItems(List<DocWriteRequest<?>> items, Releasable releasable, Runn
} else {
assert bulkRequest != null;
if (internalAddItems(items, releasable)) {
if (incrementalOperation.shouldSplit()) {
IndexingPressure.Coordinating coordinating = incrementalOperation.split();
Optional<Releasable> maybeSplit = incrementalOperation.maybeSplit();
if (maybeSplit.isPresent()) {
Releasable coordinating = maybeSplit.get();
final boolean isFirstRequest = incrementalRequestSubmitted == false;
incrementalRequestSubmitted = true;
final ArrayList<Releasable> toRelease = new ArrayList<>(releasables);
Expand All @@ -156,8 +158,8 @@ public void onFailure(Exception e) {
}
}, () -> {
bulkInProgress = false;
coordinating.close();
toRelease.forEach(Releasable::close);
coordinating.close();
nextItems.run();
}));
} else {
Expand All @@ -177,7 +179,7 @@ public void lastItems(List<DocWriteRequest<?>> items, Releasable releasable, Act
} else {
assert bulkRequest != null;
if (internalAddItems(items, releasable)) {
IndexingPressure.Coordinating coordinating = incrementalOperation.split();
Releasable coordinating = incrementalOperation.split();
final ArrayList<Releasable> toRelease = new ArrayList<>(releasables);
releasables.clear();
// We do not need to set this back to false as this will be the last request.
Expand All @@ -198,8 +200,8 @@ public void onFailure(Exception e) {
errorResponse(listener);
}
}, () -> {
coordinating.close();
toRelease.forEach(Releasable::close);
coordinating.close();
}));
} else {
errorResponse(listener);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import org.elasticsearch.core.Releasable;
import org.elasticsearch.index.stats.IndexingPressureStats;

import java.util.Optional;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.concurrent.atomic.AtomicLong;

Expand Down Expand Up @@ -189,7 +190,7 @@ public long currentOperationsSize() {
return coordinating.currentOperationsSize;
}

public boolean shouldSplit() {
public Optional<Releasable> maybeSplit() {
long currentUsage = (currentCombinedCoordinatingAndPrimaryBytes.get() + currentReplicaBytes.get());
long currentOperationsSize = coordinating.currentOperationsSize;
if (currentUsage >= highWatermark && currentOperationsSize >= highWatermarkSize) {
Expand All @@ -201,7 +202,7 @@ public boolean shouldSplit() {
currentOperationsSize
)
);
return true;
return Optional.of(split());
}
if (currentUsage >= lowWatermark && currentOperationsSize >= lowWatermarkSize) {
lowWaterMarkSplits.getAndIncrement();
Expand All @@ -212,12 +213,12 @@ public boolean shouldSplit() {
currentOperationsSize
)
);
return true;
return Optional.of(split());
}
return false;
return Optional.empty();
}

public Coordinating split() {
public Releasable split() {
Coordinating toReturn = coordinating;
coordinating = new Coordinating(forceExecution);
return toReturn;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
import org.elasticsearch.test.ESTestCase;
import org.hamcrest.Matchers;

import java.util.Optional;

public class IndexingPressureTests extends ESTestCase {

private final Settings settings = Settings.builder()
Expand Down Expand Up @@ -58,28 +60,31 @@ public void testHighAndLowWatermarkSplits() {
false
)
) {
assertFalse(coordinating1.shouldSplit());
assertFalse(coordinating2.shouldSplit());
assertFalse(coordinating1.maybeSplit().isPresent());
assertFalse(coordinating2.maybeSplit().isPresent());
assertEquals(indexingPressure.stats().getHighWaterMarkSplits(), 0L);
assertEquals(indexingPressure.stats().getLowWaterMarkSplits(), 0L);
assertTrue(coordinating3.shouldSplit());
assertEquals(indexingPressure.stats().getHighWaterMarkSplits(), 0L);
assertEquals(indexingPressure.stats().getLowWaterMarkSplits(), 1L);

try (
Releasable ignored2 = indexingPressure.markCoordinatingOperationStarted(
10,
1 + (9 * 1024) - indexingPressure.stats().getCurrentCoordinatingBytes(),
false
)
) {
assertFalse(coordinating1.shouldSplit());
assertTrue(coordinating2.shouldSplit());
assertEquals(indexingPressure.stats().getHighWaterMarkSplits(), 1L);
Optional<Releasable> split1 = coordinating3.maybeSplit();
assertTrue(split1.isPresent());
try (Releasable ignored2 = split1.get()) {
assertEquals(indexingPressure.stats().getHighWaterMarkSplits(), 0L);
assertEquals(indexingPressure.stats().getLowWaterMarkSplits(), 1L);
assertTrue(coordinating3.shouldSplit());
assertEquals(indexingPressure.stats().getLowWaterMarkSplits(), 1L);
assertEquals(indexingPressure.stats().getHighWaterMarkSplits(), 2L);

try (
Releasable ignored3 = indexingPressure.markCoordinatingOperationStarted(
10,
1 + (9 * 1024) - indexingPressure.stats().getCurrentCoordinatingBytes(),
false
)
) {
assertFalse(coordinating1.maybeSplit().isPresent());
Optional<Releasable> split2 = coordinating2.maybeSplit();
assertTrue(split2.isPresent());
try (Releasable ignored4 = split2.get()) {
assertEquals(indexingPressure.stats().getHighWaterMarkSplits(), 1L);
assertEquals(indexingPressure.stats().getLowWaterMarkSplits(), 1L);
}
}
}
}
}
Expand Down