use of org.opensearch.cluster.routing.AllocationId in project OpenSearch by opensearch-project.
the class IndexShardTests method testRelocatedShardCanNotBeRevivedConcurrently.
public void testRelocatedShardCanNotBeRevivedConcurrently() throws IOException, InterruptedException, BrokenBarrierException {
final IndexShard shard = newStartedShard(true);
final ShardRouting originalRouting = shard.routingEntry();
final ShardRouting relocationRouting = ShardRoutingHelper.relocate(originalRouting, "other_node");
IndexShardTestCase.updateRoutingEntry(shard, relocationRouting);
CyclicBarrier cyclicBarrier = new CyclicBarrier(3);
AtomicReference<Exception> relocationException = new AtomicReference<>();
Thread relocationThread = new Thread(new AbstractRunnable() {
@Override
public void onFailure(Exception e) {
relocationException.set(e);
}
@Override
protected void doRun() throws Exception {
cyclicBarrier.await();
shard.relocated(relocationRouting.getTargetRelocatingShard().allocationId().getId(), primaryContext -> {
});
}
});
relocationThread.start();
AtomicReference<Exception> cancellingException = new AtomicReference<>();
Thread cancellingThread = new Thread(new AbstractRunnable() {
@Override
public void onFailure(Exception e) {
cancellingException.set(e);
}
@Override
protected void doRun() throws Exception {
cyclicBarrier.await();
IndexShardTestCase.updateRoutingEntry(shard, originalRouting);
}
});
cancellingThread.start();
cyclicBarrier.await();
relocationThread.join();
cancellingThread.join();
if (shard.isRelocatedPrimary()) {
logger.debug("shard was relocated successfully");
assertThat(cancellingException.get(), instanceOf(IllegalIndexShardStateException.class));
assertThat("current routing:" + shard.routingEntry(), shard.routingEntry().relocating(), equalTo(true));
assertThat(relocationException.get(), nullValue());
} else {
logger.debug("shard relocation was cancelled");
assertThat(relocationException.get(), either(instanceOf(IllegalIndexShardStateException.class)).or(instanceOf(IllegalStateException.class)));
assertThat("current routing:" + shard.routingEntry(), shard.routingEntry().relocating(), equalTo(false));
assertThat(cancellingException.get(), nullValue());
}
closeShards(shard);
}
use of org.opensearch.cluster.routing.AllocationId in project OpenSearch by opensearch-project.
the class IndexShardTests method testShardStateMetaHashCodeEquals.
public void testShardStateMetaHashCodeEquals() {
AllocationId allocationId = randomBoolean() ? null : randomAllocationId();
ShardStateMetadata meta = new ShardStateMetadata(randomBoolean(), randomRealisticUnicodeOfCodepointLengthBetween(1, 10), allocationId);
assertEquals(meta, new ShardStateMetadata(meta.primary, meta.indexUUID, meta.allocationId));
assertEquals(meta.hashCode(), new ShardStateMetadata(meta.primary, meta.indexUUID, meta.allocationId).hashCode());
assertFalse(meta.equals(new ShardStateMetadata(!meta.primary, meta.indexUUID, meta.allocationId)));
assertFalse(meta.equals(new ShardStateMetadata(!meta.primary, meta.indexUUID + "foo", meta.allocationId)));
assertFalse(meta.equals(new ShardStateMetadata(!meta.primary, meta.indexUUID + "foo", randomAllocationId())));
Set<Integer> hashCodes = new HashSet<>();
for (int i = 0; i < 30; i++) {
// just a sanity check that we impl hashcode
allocationId = randomBoolean() ? null : randomAllocationId();
meta = new ShardStateMetadata(randomBoolean(), randomRealisticUnicodeOfCodepointLengthBetween(1, 10), allocationId);
hashCodes.add(meta.hashCode());
}
assertTrue("more than one unique hashcode expected but got: " + hashCodes.size(), hashCodes.size() > 1);
}
use of org.opensearch.cluster.routing.AllocationId in project OpenSearch by opensearch-project.
the class ReplicationTrackerRetentionLeaseTests method testReplicaIgnoresOlderRetentionLeasesVersion.
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(), contains(expectedLeases.toArray(new RetentionLease[0])));
}
}
use of org.opensearch.cluster.routing.AllocationId in project OpenSearch by opensearch-project.
the class ReplicationTrackerRetentionLeaseTests method testRemoveRetentionLease.
public void testRemoveRetentionLease() {
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 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(() -> {
}));
}
for (int i = 0; i < length; i++) {
if (rarely() && primaryTerm < Long.MAX_VALUE) {
primaryTerm = randomLongBetween(primaryTerm + 1, Long.MAX_VALUE);
replicationTracker.setOperationPrimaryTerm(primaryTerm);
}
/*
* Remove from the end since it will make the following assertion easier; we want to ensure that only the intended lease was
* removed.
*/
replicationTracker.removeRetentionLease(Integer.toString(length - i - 1), ActionListener.wrap(() -> {
}));
assertRetentionLeases(replicationTracker, length - i - 1, minimumRetainingSequenceNumbers, primaryTerm, 2 + length + i, true, false);
}
}
use of org.opensearch.cluster.routing.AllocationId in project OpenSearch by opensearch-project.
the class ReplicationTrackerRetentionLeaseTests method testLoadAndPersistRetentionLeases.
public void testLoadAndPersistRetentionLeases() 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) -> {
}, 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);
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);
assertThat(replicationTracker.loadRetentionLeases(path), equalTo(replicationTracker.getRetentionLeases()));
}
Aggregations