Skip to content

Commit c1233a9

Browse files
committed
Avoid closing when partial snapshotting in SnapshotStressTestsIT
Closing an index while it's being partially-snapshotted is forbidden, but `SnapshotStressTestsIT#testRandomActivities` still sometimes attempts to do so which causes it to fail. This commit changes the behaviour to avoid doing these things to the same index at the same time. Closes elastic#109138
1 parent 3cd3507 commit c1233a9

File tree

1 file changed

+35
-5
lines changed

1 file changed

+35
-5
lines changed

server/src/internalClusterTest/java/org/elasticsearch/snapshots/SnapshotStressTestsIT.java

Lines changed: 35 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,9 @@
8181
import static org.elasticsearch.repositories.blobstore.ChecksumBlobStoreFormat.SNAPSHOT_ONLY_FORMAT_PARAMS;
8282
import static org.elasticsearch.xcontent.XContentFactory.jsonBuilder;
8383
import static org.hamcrest.Matchers.equalTo;
84+
import static org.hamcrest.Matchers.greaterThan;
8485
import static org.hamcrest.Matchers.hasSize;
86+
import static org.hamcrest.Matchers.lessThan;
8587
import static org.hamcrest.Matchers.notNullValue;
8688

8789
@LuceneTestCase.SuppressFileSystems(value = "HandleLimitFS") // we sometimes have >2048 open files
@@ -468,17 +470,20 @@ private void restoreSnapshot(SnapshotInfo snapshotInfo, Releasable releasePrevio
468470
restoreSpecificIndicesTmp = true;
469471
continue;
470472
}
471-
if (randomBoolean() && localReleasables.add(tryAcquireAllPermits(indices.get(indexName).permits)) != null) {
473+
final var trackedIndex = indices.get(indexName);
474+
if (randomBoolean() && localReleasables.add(tryAcquireAllPermits(trackedIndex.permits)) != null) {
472475

473476
indicesToRestoreList.add(indexName);
474477

475478
final int snapshotShardCount = snapshotInfo.indexSnapshotDetails().get(indexName).getShardCount();
476-
final int indexShardCount = indices.get(indexName).shardCount;
477-
if (snapshotShardCount == indexShardCount && randomBoolean()) {
479+
final int indexShardCount = trackedIndex.shardCount;
480+
if (snapshotShardCount == indexShardCount
481+
&& randomBoolean()
482+
&& localReleasables.add(trackedIndex.tryAcquireClosingPermit()) != null) {
478483
indicesToCloseList.add(indexName);
479484
} else {
480485
indicesToDeleteList.add(indexName);
481-
indices.get(indexName).shardCount = snapshotShardCount;
486+
trackedIndex.shardCount = snapshotShardCount;
482487
}
483488
} else {
484489
restoreSpecificIndicesTmp = true;
@@ -994,7 +999,9 @@ private void startPartialSnapshotter() {
994999
boolean snapshotSpecificIndicesTmp = randomBoolean();
9951000
final List<String> targetIndexNames = new ArrayList<>(indices.size());
9961001
for (TrackedIndex trackedIndex : indices.values()) {
997-
if (usually() && releasableAfterStart.add(tryAcquirePermit(trackedIndex.permits)) != null) {
1002+
if (usually()
1003+
&& releasableAfterStart.add(tryAcquirePermit(trackedIndex.permits)) != null
1004+
&& localReleasables.add(trackedIndex.tryAcquirePartialSnapshottingPermit()) != null) {
9981005
targetIndexNames.add(trackedIndex.indexName);
9991006
} else {
10001007
snapshotSpecificIndicesTmp = true;
@@ -1550,6 +1557,29 @@ private void scheduleIndexingAndPossibleDelete() {
15501557
});
15511558
}
15521559

1560+
/**
1561+
* We must not close an index while it's being partially snapshotted; this counter tracks the number of ongoing
1562+
* close operations (positive) or partial snapshot operations (negative) in order to avoid them happening concurrently.
1563+
*/
1564+
private final AtomicInteger closingOrPartialSnapshottingCount = new AtomicInteger();
1565+
1566+
Releasable tryAcquireClosingPermit() {
1567+
final var prevCount = closingOrPartialSnapshottingCount.getAndUpdate(c -> c >= 0 ? c + 1 : c);
1568+
if (prevCount >= 0) {
1569+
return () -> assertThat(closingOrPartialSnapshottingCount.getAndDecrement(), greaterThan(0));
1570+
} else {
1571+
return null;
1572+
}
1573+
}
1574+
1575+
Releasable tryAcquirePartialSnapshottingPermit() {
1576+
final var prevCount = closingOrPartialSnapshottingCount.getAndUpdate(c -> c <= 0 ? c - 1 : c);
1577+
if (prevCount <= 0) {
1578+
return () -> assertThat(closingOrPartialSnapshottingCount.getAndIncrement(), lessThan(0));
1579+
} else {
1580+
return null;
1581+
}
1582+
}
15531583
}
15541584

15551585
}

0 commit comments

Comments
 (0)