use of org.elasticsearch.cluster.routing.AllocationId in project crate by crate.
the class ReplicationTrackerRetentionLeaseTests method testRenewNotFoundRetentionLease.
@Test
public void testRenewNotFoundRetentionLease() {
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.renewRetentionLease(id, randomNonNegativeLong(), randomAlphaOfLength(8)));
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 runExpirationTest.
private void runExpirationTest(final boolean primaryMode) {
final AllocationId allocationId = AllocationId.newInitializing();
final AtomicLong currentTimeMillis = new AtomicLong(randomLongBetween(0, 1024));
final long retentionLeaseMillis = randomLongBetween(1, TimeValue.timeValueHours(12).millis());
final Settings settings = Settings.builder().put(IndexSettings.INDEX_SOFT_DELETES_SETTING.getKey(), true).put(IndexSettings.INDEX_SOFT_DELETES_RETENTION_LEASE_PERIOD_SETTING.getKey(), TimeValue.timeValueMillis(retentionLeaseMillis).getStringRep()).build();
final long primaryTerm = randomLongBetween(1, Long.MAX_VALUE);
final ReplicationTracker replicationTracker = new ReplicationTracker(new ShardId("test", "_na", 0), allocationId.getId(), IndexSettingsModule.newIndexSettings("test", settings), primaryTerm, UNASSIGNED_SEQ_NO, value -> {
}, currentTimeMillis::get, (leases, listener) -> {
}, OPS_BASED_RECOVERY_ALWAYS_REASONABLE);
replicationTracker.updateFromMaster(randomNonNegativeLong(), Collections.singleton(allocationId.getId()), routingTable(Collections.emptySet(), allocationId));
if (primaryMode) {
replicationTracker.activatePrimaryMode(SequenceNumbers.NO_OPS_PERFORMED);
}
final long[] retainingSequenceNumbers = new long[1];
retainingSequenceNumbers[0] = randomLongBetween(0, Long.MAX_VALUE);
if (primaryMode) {
replicationTracker.addRetentionLease("0", retainingSequenceNumbers[0], "test-0", ActionListener.wrap(() -> {
}));
} else {
final RetentionLeases retentionLeases = new RetentionLeases(primaryTerm, 1, Collections.singleton(new RetentionLease("0", retainingSequenceNumbers[0], currentTimeMillis.get(), "test-0")));
replicationTracker.updateRetentionLeasesOnReplica(retentionLeases);
}
{
final RetentionLeases retentionLeases = replicationTracker.getRetentionLeases();
final long expectedVersion = primaryMode ? 2L : 1L;
assertThat(retentionLeases.version(), equalTo(expectedVersion));
assertThat(retentionLeases.leases(), hasSize(primaryMode ? 2 : 1));
final RetentionLease retentionLease = retentionLeases.get("0");
assertThat(retentionLease.timestamp(), equalTo(currentTimeMillis.get()));
assertRetentionLeases(replicationTracker, 1, retainingSequenceNumbers, primaryTerm, expectedVersion, primaryMode, false);
}
// renew the lease
currentTimeMillis.set(currentTimeMillis.get() + randomLongBetween(0, 1024));
retainingSequenceNumbers[0] = randomLongBetween(retainingSequenceNumbers[0], Long.MAX_VALUE);
if (primaryMode) {
replicationTracker.renewRetentionLease("0", retainingSequenceNumbers[0], "test-0");
} else {
final RetentionLeases retentionLeases = new RetentionLeases(primaryTerm, 2, Collections.singleton(new RetentionLease("0", retainingSequenceNumbers[0], currentTimeMillis.get(), "test-0")));
replicationTracker.updateRetentionLeasesOnReplica(retentionLeases);
}
{
final RetentionLeases retentionLeases = replicationTracker.getRetentionLeases();
final long expectedVersion = primaryMode ? 3L : 2L;
assertThat(retentionLeases.version(), equalTo(expectedVersion));
assertThat(retentionLeases.leases(), hasSize(primaryMode ? 2 : 1));
final RetentionLease retentionLease = retentionLeases.get("0");
assertThat(retentionLease.timestamp(), equalTo(currentTimeMillis.get()));
assertRetentionLeases(replicationTracker, 1, retainingSequenceNumbers, primaryTerm, expectedVersion, primaryMode, false);
}
// now force the lease to expire
currentTimeMillis.set(currentTimeMillis.get() + randomLongBetween(retentionLeaseMillis, Long.MAX_VALUE - currentTimeMillis.get()));
if (primaryMode) {
assertRetentionLeases(replicationTracker, 1, retainingSequenceNumbers, primaryTerm, 3, true, false);
assertRetentionLeases(replicationTracker, 0, new long[0], primaryTerm, 4, true, true);
} else {
// leases do not expire on replicas until synced from the primary
assertRetentionLeases(replicationTracker, 1, retainingSequenceNumbers, primaryTerm, 2, false, false);
}
}
use of org.elasticsearch.cluster.routing.AllocationId in project crate by crate.
the class ReplicationTrackerTestCase method routingTable.
static IndexShardRoutingTable routingTable(final Set<AllocationId> initializingIds, final ShardRouting primaryShard) {
assert !initializingIds.contains(primaryShard.allocationId());
final ShardId shardId = new ShardId("test", "_na_", 0);
final IndexShardRoutingTable.Builder builder = new IndexShardRoutingTable.Builder(shardId);
for (final AllocationId initializingId : initializingIds) {
builder.addShard(TestShardRouting.newShardRouting(shardId, nodeIdFromAllocationId(initializingId), null, false, ShardRoutingState.INITIALIZING, initializingId));
}
builder.addShard(primaryShard);
return builder.build();
}
use of org.elasticsearch.cluster.routing.AllocationId in project crate by crate.
the class NodeVersionAllocationDeciderTests method test_primary_and_replica_allocation_on_lower_hotfix_version_is_allowed.
@Test
public void test_primary_and_replica_allocation_on_lower_hotfix_version_is_allowed() {
ShardId shard1 = new ShardId("test1", UUIDs.randomBase64UUID(), 0);
final DiscoveryNode newNode1 = new DiscoveryNode("newNode1", buildNewFakeTransportAddress(), emptyMap(), MASTER_DATA_ROLES, Version.V_4_4_2);
final DiscoveryNode newNode2 = new DiscoveryNode("newNode2", buildNewFakeTransportAddress(), emptyMap(), MASTER_DATA_ROLES, Version.V_4_4_2);
final DiscoveryNode oldNode1 = new DiscoveryNode("oldNode1", buildNewFakeTransportAddress(), emptyMap(), MASTER_DATA_ROLES, Version.V_4_4_1);
final DiscoveryNode oldNode2 = new DiscoveryNode("oldNode2", buildNewFakeTransportAddress(), emptyMap(), MASTER_DATA_ROLES, Version.V_4_4_1);
AllocationId allocationId1P = AllocationId.newInitializing();
AllocationId allocationId1R = AllocationId.newInitializing();
Metadata metadata = Metadata.builder().put(IndexMetadata.builder(shard1.getIndexName()).settings(settings(Version.CURRENT).put(AutoExpandReplicas.SETTING.getKey(), "false").put(IndexMetadata.SETTING_INDEX_UUID, shard1.getIndex().getUUID()).put(INDEX_DELAYED_NODE_LEFT_TIMEOUT_SETTING.getKey(), 0)).numberOfShards(1).numberOfReplicas(1).putInSyncAllocationIds(0, Set.of(allocationId1P.getId(), allocationId1R.getId()))).build();
RoutingTable routingTable = RoutingTable.builder().add(IndexRoutingTable.builder(shard1.getIndex()).addIndexShard(new IndexShardRoutingTable.Builder(shard1).addShard(TestShardRouting.newShardRouting(shard1.getIndex(), shard1.getId(), newNode1.getId(), null, true, ShardRoutingState.STARTED, allocationId1P)).addShard(TestShardRouting.newShardRouting(shard1.getIndex(), shard1.getId(), newNode2.getId(), null, false, ShardRoutingState.STARTED, allocationId1R)).build())).build();
AllocationDeciders allocationDeciders = new AllocationDeciders(Collections.singleton(new NodeVersionAllocationDecider()));
AllocationService strategy = new MockAllocationService(allocationDeciders, new TestGatewayAllocator(), new BalancedShardsAllocator(Settings.EMPTY), EmptyClusterInfoService.INSTANCE);
// Test downgrade to same MAJOR.MINOR but lower HOTFIX version
logger.trace("Downgrade 1st node");
ClusterState state = ClusterState.builder(org.elasticsearch.cluster.ClusterName.CLUSTER_NAME_SETTING.getDefault(Settings.EMPTY)).metadata(metadata).routingTable(routingTable).nodes(DiscoveryNodes.builder().add(oldNode1).add(newNode2)).build();
state = stabilize(state, strategy);
assertThat(state.routingTable().index(shard1.getIndex()).shardsWithState(STARTED).size(), equalTo(2));
logger.trace("Downgrade 2nd node");
state = ClusterState.builder(state).nodes(DiscoveryNodes.builder().add(oldNode1).add(oldNode2)).build();
state = stabilize(state, strategy);
assertThat(state.routingTable().index(shard1.getIndex()).shardsWithState(STARTED).size(), equalTo(2));
}
Aggregations