|
24 | 24 | import org.elasticsearch.common.collect.Tuple;
|
25 | 25 | import org.elasticsearch.common.settings.Settings;
|
26 | 26 | import org.elasticsearch.common.unit.TimeValue;
|
| 27 | +import org.elasticsearch.common.xcontent.NamedXContentRegistry; |
27 | 28 | import org.elasticsearch.index.IndexSettings;
|
28 | 29 | import org.elasticsearch.index.shard.ShardId;
|
29 | 30 | import org.elasticsearch.test.IndexSettingsModule;
|
@@ -498,6 +499,49 @@ public void testLoadAndPersistRetentionLeases() throws IOException {
|
498 | 499 | assertThat(replicationTracker.loadRetentionLeases(path), equalTo(replicationTracker.getRetentionLeases()));
|
499 | 500 | }
|
500 | 501 |
|
| 502 | + public void testUnnecessaryPersistenceOfRetentionLeases() throws IOException { |
| 503 | + final AllocationId allocationId = AllocationId.newInitializing(); |
| 504 | + long primaryTerm = randomLongBetween(1, Long.MAX_VALUE); |
| 505 | + final ReplicationTracker replicationTracker = new ReplicationTracker( |
| 506 | + new ShardId("test", "_na", 0), |
| 507 | + allocationId.getId(), |
| 508 | + IndexSettingsModule.newIndexSettings("test", Settings.EMPTY), |
| 509 | + primaryTerm, |
| 510 | + UNASSIGNED_SEQ_NO, |
| 511 | + value -> {}, |
| 512 | + () -> 0L, |
| 513 | + (leases, listener) -> {}); |
| 514 | + replicationTracker.updateFromMaster( |
| 515 | + randomNonNegativeLong(), |
| 516 | + Collections.singleton(allocationId.getId()), |
| 517 | + routingTable(Collections.emptySet(), allocationId), |
| 518 | + Collections.emptySet()); |
| 519 | + replicationTracker.activatePrimaryMode(SequenceNumbers.NO_OPS_PERFORMED); |
| 520 | + final int length = randomIntBetween(0, 8); |
| 521 | + for (int i = 0; i < length; i++) { |
| 522 | + if (rarely() && primaryTerm < Long.MAX_VALUE) { |
| 523 | + primaryTerm = randomLongBetween(primaryTerm + 1, Long.MAX_VALUE); |
| 524 | + replicationTracker.setOperationPrimaryTerm(primaryTerm); |
| 525 | + } |
| 526 | + final long retainingSequenceNumber = randomLongBetween(SequenceNumbers.NO_OPS_PERFORMED, Long.MAX_VALUE); |
| 527 | + replicationTracker.addRetentionLease( |
| 528 | + Integer.toString(i), retainingSequenceNumber, "test-" + i, ActionListener.wrap(() -> {})); |
| 529 | + } |
| 530 | + |
| 531 | + final Path path = createTempDir(); |
| 532 | + replicationTracker.persistRetentionLeases(path); |
| 533 | + |
| 534 | + final Tuple<RetentionLeases, Long> retentionLeasesWithGeneration = |
| 535 | + RetentionLeases.FORMAT.loadLatestStateWithGeneration(logger, NamedXContentRegistry.EMPTY, path); |
| 536 | + |
| 537 | + replicationTracker.persistRetentionLeases(path); |
| 538 | + final Tuple<RetentionLeases, Long> retentionLeasesWithGenerationAfterUnnecessaryPersistence = |
| 539 | + RetentionLeases.FORMAT.loadLatestStateWithGeneration(logger, NamedXContentRegistry.EMPTY, path); |
| 540 | + |
| 541 | + assertThat(retentionLeasesWithGenerationAfterUnnecessaryPersistence.v1(), equalTo(retentionLeasesWithGeneration.v1())); |
| 542 | + assertThat(retentionLeasesWithGenerationAfterUnnecessaryPersistence.v2(), equalTo(retentionLeasesWithGeneration.v2())); |
| 543 | + } |
| 544 | + |
501 | 545 | /**
|
502 | 546 | * Test that we correctly synchronize writing the retention lease state file in {@link ReplicationTracker#persistRetentionLeases(Path)}.
|
503 | 547 | * This test can fail without the synchronization block in that method.
|
|
0 commit comments