use of org.elasticsearch.cluster.routing.AllocationId in project crate by crate.
the class ReplicationTrackerRetentionLeaseTests method testReplicaIgnoresOlderRetentionLeasesVersion.
@Test
public void testReplicaIgnoresOlderRetentionLeasesVersion() {
final AllocationId allocationId = AllocationId.newInitializing();
final ReplicationTracker replicationTracker = new ReplicationTracker(new ShardId("test", "_na", 0), allocationId.getId(), IndexSettingsModule.newIndexSettings("test", Settings.EMPTY), randomNonNegativeLong(), UNASSIGNED_SEQ_NO, value -> {
}, () -> 0L, (leases, listener) -> {
}, OPS_BASED_RECOVERY_ALWAYS_REASONABLE);
replicationTracker.updateFromMaster(randomNonNegativeLong(), Collections.singleton(allocationId.getId()), routingTable(Collections.emptySet(), allocationId));
final int length = randomIntBetween(0, 8);
final List<RetentionLeases> retentionLeasesCollection = new ArrayList<>(length);
long primaryTerm = 1;
long version = 0;
for (int i = 0; i < length; i++) {
final int innerLength = randomIntBetween(0, 8);
final Collection<RetentionLease> leases = new ArrayList<>();
for (int j = 0; j < innerLength; j++) {
leases.add(new RetentionLease(i + "-" + j, randomNonNegativeLong(), randomNonNegativeLong(), randomAlphaOfLength(8)));
}
version++;
if (rarely()) {
primaryTerm++;
}
retentionLeasesCollection.add(new RetentionLeases(primaryTerm, version, leases));
}
final Collection<RetentionLease> expectedLeases;
if (length == 0 || retentionLeasesCollection.get(length - 1).leases().isEmpty()) {
expectedLeases = Collections.emptyList();
} else {
expectedLeases = retentionLeasesCollection.get(length - 1).leases();
}
Collections.shuffle(retentionLeasesCollection, random());
for (final RetentionLeases retentionLeases : retentionLeasesCollection) {
replicationTracker.updateRetentionLeasesOnReplica(retentionLeases);
}
assertThat(replicationTracker.getRetentionLeases().version(), equalTo(version));
if (expectedLeases.isEmpty()) {
assertThat(replicationTracker.getRetentionLeases().leases(), empty());
} else {
assertThat(replicationTracker.getRetentionLeases().leases(), Matchers.contains(expectedLeases.toArray(new RetentionLease[0])));
}
}
use of org.elasticsearch.cluster.routing.AllocationId in project crate by crate.
the class ReplicationTrackerRetentionLeaseTests method testAddRetentionLeaseCausesRetentionLeaseSync.
@Test
public void testAddRetentionLeaseCausesRetentionLeaseSync() {
final AllocationId allocationId = AllocationId.newInitializing();
final Map<String, Long> retainingSequenceNumbers = new HashMap<>();
final AtomicBoolean invoked = new AtomicBoolean();
final AtomicReference<ReplicationTracker> reference = new AtomicReference<>();
final ReplicationTracker replicationTracker = new ReplicationTracker(new ShardId("test", "_na", 0), allocationId.getId(), IndexSettingsModule.newIndexSettings("test", Settings.EMPTY), randomNonNegativeLong(), UNASSIGNED_SEQ_NO, value -> {
}, () -> 0L, (leases, listener) -> {
// we do not want to hold a lock on the replication tracker in the callback!
assertFalse(Thread.holdsLock(reference.get()));
invoked.set(true);
assertThat(leases.leases().stream().collect(Collectors.toMap(RetentionLease::id, RetentionLease::retainingSequenceNumber)), equalTo(retainingSequenceNumbers));
}, OPS_BASED_RECOVERY_ALWAYS_REASONABLE);
reference.set(replicationTracker);
replicationTracker.updateFromMaster(randomNonNegativeLong(), Collections.singleton(allocationId.getId()), routingTable(Collections.emptySet(), allocationId));
replicationTracker.activatePrimaryMode(SequenceNumbers.NO_OPS_PERFORMED);
retainingSequenceNumbers.put(ReplicationTracker.getPeerRecoveryRetentionLeaseId(nodeIdFromAllocationId(allocationId)), 0L);
final int length = randomIntBetween(0, 8);
for (int i = 0; i < length; i++) {
final String id = randomAlphaOfLength(8);
final long retainingSequenceNumber = randomLongBetween(SequenceNumbers.NO_OPS_PERFORMED, Long.MAX_VALUE);
retainingSequenceNumbers.put(id, retainingSequenceNumber);
replicationTracker.addRetentionLease(id, retainingSequenceNumber, "test", ActionListener.wrap(() -> {
}));
// assert that the new retention lease callback was invoked
assertTrue(invoked.get());
// reset the invocation marker so that we can assert the callback was not invoked when renewing the lease
invoked.set(false);
replicationTracker.renewRetentionLease(id, retainingSequenceNumber, "test");
assertFalse(invoked.get());
}
}
use of org.elasticsearch.cluster.routing.AllocationId in project crate by crate.
the class ReplicationTrackerRetentionLeaseTests method testRemoveNotFound.
@Test
public void testRemoveNotFound() {
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) -> {
}, OPS_BASED_RECOVERY_ALWAYS_REASONABLE);
replicationTracker.updateFromMaster(randomNonNegativeLong(), Collections.singleton(allocationId.getId()), routingTable(Collections.emptySet(), allocationId));
replicationTracker.activatePrimaryMode(SequenceNumbers.NO_OPS_PERFORMED);
final String id = randomAlphaOfLength(8);
final RetentionLeaseNotFoundException e = expectThrows(RetentionLeaseNotFoundException.class, () -> replicationTracker.removeRetentionLease(id, ActionListener.wrap(() -> {
})));
assertThat(e, hasToString(containsString("retention lease with ID [" + id + "] not found")));
}
use of org.elasticsearch.cluster.routing.AllocationId in project crate by crate.
the class ReplicationTrackerRetentionLeaseTests method testAddDuplicateRetentionLease.
@Test
public void testAddDuplicateRetentionLease() {
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) -> {
}, OPS_BASED_RECOVERY_ALWAYS_REASONABLE);
replicationTracker.updateFromMaster(randomNonNegativeLong(), Collections.singleton(allocationId.getId()), routingTable(Collections.emptySet(), allocationId));
replicationTracker.activatePrimaryMode(SequenceNumbers.NO_OPS_PERFORMED);
final String id = randomAlphaOfLength(8);
final long retainingSequenceNumber = randomNonNegativeLong();
final String source = randomAlphaOfLength(8);
replicationTracker.addRetentionLease(id, retainingSequenceNumber, source, ActionListener.wrap(() -> {
}));
final long nextRetaininSequenceNumber = randomLongBetween(retainingSequenceNumber, Long.MAX_VALUE);
final RetentionLeaseAlreadyExistsException e = expectThrows(RetentionLeaseAlreadyExistsException.class, () -> replicationTracker.addRetentionLease(id, nextRetaininSequenceNumber, source, ActionListener.wrap(() -> {
})));
assertThat(e, hasToString(containsString("retention lease with ID [" + id + "] already exists")));
}
use of org.elasticsearch.cluster.routing.AllocationId in project crate by crate.
the class ReplicationTrackerRetentionLeaseTests method testAddOrRenewRetentionLease.
@Test
public void testAddOrRenewRetentionLease() {
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.builder().put(IndexSettings.INDEX_SOFT_DELETES_SETTING.getKey(), true).build()), primaryTerm, UNASSIGNED_SEQ_NO, value -> {
}, () -> 0L, (leases, listener) -> {
}, OPS_BASED_RECOVERY_ALWAYS_REASONABLE);
replicationTracker.updateFromMaster(randomNonNegativeLong(), Collections.singleton(allocationId.getId()), routingTable(Collections.emptySet(), allocationId));
replicationTracker.activatePrimaryMode(SequenceNumbers.NO_OPS_PERFORMED);
final int length = randomIntBetween(0, 8);
final long[] minimumRetainingSequenceNumbers = new long[length];
for (int i = 0; i < length; i++) {
if (rarely() && primaryTerm < Long.MAX_VALUE) {
primaryTerm = randomLongBetween(primaryTerm + 1, Long.MAX_VALUE);
replicationTracker.setOperationPrimaryTerm(primaryTerm);
}
minimumRetainingSequenceNumbers[i] = randomLongBetween(SequenceNumbers.NO_OPS_PERFORMED, Long.MAX_VALUE);
replicationTracker.addRetentionLease(Integer.toString(i), minimumRetainingSequenceNumbers[i], "test-" + i, ActionListener.wrap(() -> {
}));
assertRetentionLeases(replicationTracker, i + 1, minimumRetainingSequenceNumbers, primaryTerm, 2 + i, true, false);
}
for (int i = 0; i < length; i++) {
if (rarely() && primaryTerm < Long.MAX_VALUE) {
primaryTerm = randomLongBetween(primaryTerm + 1, Long.MAX_VALUE);
replicationTracker.setOperationPrimaryTerm(primaryTerm);
}
minimumRetainingSequenceNumbers[i] = randomLongBetween(minimumRetainingSequenceNumbers[i], Long.MAX_VALUE);
replicationTracker.renewRetentionLease(Integer.toString(i), minimumRetainingSequenceNumbers[i], "test-" + i);
assertRetentionLeases(replicationTracker, length, minimumRetainingSequenceNumbers, primaryTerm, 2 + length + i, true, false);
}
}
Aggregations