|
81 | 81 | import static org.elasticsearch.repositories.blobstore.ChecksumBlobStoreFormat.SNAPSHOT_ONLY_FORMAT_PARAMS;
|
82 | 82 | import static org.elasticsearch.xcontent.XContentFactory.jsonBuilder;
|
83 | 83 | import static org.hamcrest.Matchers.equalTo;
|
| 84 | +import static org.hamcrest.Matchers.greaterThan; |
84 | 85 | import static org.hamcrest.Matchers.hasSize;
|
| 86 | +import static org.hamcrest.Matchers.lessThan; |
85 | 87 | import static org.hamcrest.Matchers.notNullValue;
|
86 | 88 |
|
87 | 89 | @LuceneTestCase.SuppressFileSystems(value = "HandleLimitFS") // we sometimes have >2048 open files
|
@@ -468,17 +470,20 @@ private void restoreSnapshot(SnapshotInfo snapshotInfo, Releasable releasePrevio
|
468 | 470 | restoreSpecificIndicesTmp = true;
|
469 | 471 | continue;
|
470 | 472 | }
|
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) { |
472 | 475 |
|
473 | 476 | indicesToRestoreList.add(indexName);
|
474 | 477 |
|
475 | 478 | 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) { |
478 | 483 | indicesToCloseList.add(indexName);
|
479 | 484 | } else {
|
480 | 485 | indicesToDeleteList.add(indexName);
|
481 |
| - indices.get(indexName).shardCount = snapshotShardCount; |
| 486 | + trackedIndex.shardCount = snapshotShardCount; |
482 | 487 | }
|
483 | 488 | } else {
|
484 | 489 | restoreSpecificIndicesTmp = true;
|
@@ -994,7 +999,9 @@ private void startPartialSnapshotter() {
|
994 | 999 | boolean snapshotSpecificIndicesTmp = randomBoolean();
|
995 | 1000 | final List<String> targetIndexNames = new ArrayList<>(indices.size());
|
996 | 1001 | 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) { |
998 | 1005 | targetIndexNames.add(trackedIndex.indexName);
|
999 | 1006 | } else {
|
1000 | 1007 | snapshotSpecificIndicesTmp = true;
|
@@ -1550,6 +1557,29 @@ private void scheduleIndexingAndPossibleDelete() {
|
1550 | 1557 | });
|
1551 | 1558 | }
|
1552 | 1559 |
|
| 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 | + } |
1553 | 1583 | }
|
1554 | 1584 |
|
1555 | 1585 | }
|
|
0 commit comments