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