|
|
@@ -24,6 +24,7 @@ import org.elasticsearch.cluster.routing.AllocationId;
|
|
|
import org.elasticsearch.common.collect.Tuple;
|
|
|
import org.elasticsearch.common.settings.Settings;
|
|
|
import org.elasticsearch.common.unit.TimeValue;
|
|
|
+import org.elasticsearch.common.xcontent.NamedXContentRegistry;
|
|
|
import org.elasticsearch.gateway.WriteStateException;
|
|
|
import org.elasticsearch.index.IndexSettings;
|
|
|
import org.elasticsearch.index.shard.ShardId;
|
|
|
@@ -489,6 +490,48 @@ public class ReplicationTrackerRetentionLeaseTests extends ReplicationTrackerTes
|
|
|
assertThat(replicationTracker.loadRetentionLeases(path), equalTo(replicationTracker.getRetentionLeases()));
|
|
|
}
|
|
|
|
|
|
+ public void testUnnecessaryPersistenceOfRetentionLeases() throws IOException {
|
|
|
+ final AllocationId allocationId = AllocationId.newInitializing();
|
|
|
+ long primaryTerm = randomLongBetween(1, Long.MAX_VALUE);
|
|
|
+ final ReplicationTracker replicationTracker = new ReplicationTracker(
|
|
|
+ new ShardId("test", "_na", 0),
|
|
|
+ allocationId.getId(),
|
|
|
+ IndexSettingsModule.newIndexSettings("test", Settings.EMPTY),
|
|
|
+ primaryTerm,
|
|
|
+ UNASSIGNED_SEQ_NO,
|
|
|
+ value -> {},
|
|
|
+ () -> 0L,
|
|
|
+ (leases, listener) -> {});
|
|
|
+ replicationTracker.updateFromMaster(
|
|
|
+ randomNonNegativeLong(),
|
|
|
+ Collections.singleton(allocationId.getId()),
|
|
|
+ routingTable(Collections.emptySet(), allocationId));
|
|
|
+ replicationTracker.activatePrimaryMode(SequenceNumbers.NO_OPS_PERFORMED);
|
|
|
+ final int length = randomIntBetween(0, 8);
|
|
|
+ for (int i = 0; i < length; i++) {
|
|
|
+ if (rarely() && primaryTerm < Long.MAX_VALUE) {
|
|
|
+ primaryTerm = randomLongBetween(primaryTerm + 1, Long.MAX_VALUE);
|
|
|
+ replicationTracker.setOperationPrimaryTerm(primaryTerm);
|
|
|
+ }
|
|
|
+ final long retainingSequenceNumber = randomLongBetween(SequenceNumbers.NO_OPS_PERFORMED, Long.MAX_VALUE);
|
|
|
+ replicationTracker.addRetentionLease(
|
|
|
+ Integer.toString(i), retainingSequenceNumber, "test-" + i, ActionListener.wrap(() -> {}));
|
|
|
+ }
|
|
|
+
|
|
|
+ final Path path = createTempDir();
|
|
|
+ replicationTracker.persistRetentionLeases(path);
|
|
|
+
|
|
|
+ final Tuple<RetentionLeases, Long> retentionLeasesWithGeneration =
|
|
|
+ RetentionLeases.FORMAT.loadLatestStateWithGeneration(logger, NamedXContentRegistry.EMPTY, path);
|
|
|
+
|
|
|
+ replicationTracker.persistRetentionLeases(path);
|
|
|
+ final Tuple<RetentionLeases, Long> retentionLeasesWithGenerationAfterUnnecessaryPersistence =
|
|
|
+ RetentionLeases.FORMAT.loadLatestStateWithGeneration(logger, NamedXContentRegistry.EMPTY, path);
|
|
|
+
|
|
|
+ assertThat(retentionLeasesWithGenerationAfterUnnecessaryPersistence.v1(), equalTo(retentionLeasesWithGeneration.v1()));
|
|
|
+ assertThat(retentionLeasesWithGenerationAfterUnnecessaryPersistence.v2(), equalTo(retentionLeasesWithGeneration.v2()));
|
|
|
+ }
|
|
|
+
|
|
|
/**
|
|
|
* Test that we correctly synchronize writing the retention lease state file in {@link ReplicationTracker#persistRetentionLeases(Path)}.
|
|
|
* This test can fail without the synchronization block in that method.
|