Search in sources :

Example 21 with AllocationId

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")));
}
Also used : ShardId(org.elasticsearch.index.shard.ShardId) AllocationId(org.elasticsearch.cluster.routing.AllocationId) Matchers.hasToString(org.hamcrest.Matchers.hasToString) Matchers.containsString(org.hamcrest.Matchers.containsString) Test(org.junit.Test)

Example 22 with AllocationId

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);
    }
}
Also used : ShardId(org.elasticsearch.index.shard.ShardId) AtomicLong(java.util.concurrent.atomic.AtomicLong) AllocationId(org.elasticsearch.cluster.routing.AllocationId) Settings(org.elasticsearch.common.settings.Settings) IndexSettings(org.elasticsearch.index.IndexSettings)

Example 23 with AllocationId

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();
}
Also used : ShardId(org.elasticsearch.index.shard.ShardId) IndexShardRoutingTable(org.elasticsearch.cluster.routing.IndexShardRoutingTable) AllocationId(org.elasticsearch.cluster.routing.AllocationId)

Example 24 with AllocationId

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));
}
Also used : TestGatewayAllocator(org.elasticsearch.test.gateway.TestGatewayAllocator) ClusterState(org.elasticsearch.cluster.ClusterState) DiscoveryNode(org.elasticsearch.cluster.node.DiscoveryNode) BalancedShardsAllocator(org.elasticsearch.cluster.routing.allocation.allocator.BalancedShardsAllocator) AllocationId(org.elasticsearch.cluster.routing.AllocationId) IndexMetadata(org.elasticsearch.cluster.metadata.IndexMetadata) Metadata(org.elasticsearch.cluster.metadata.Metadata) AllocationDeciders(org.elasticsearch.cluster.routing.allocation.decider.AllocationDeciders) ShardId(org.elasticsearch.index.shard.ShardId) IndexShardRoutingTable(org.elasticsearch.cluster.routing.IndexShardRoutingTable) IndexRoutingTable(org.elasticsearch.cluster.routing.IndexRoutingTable) RoutingTable(org.elasticsearch.cluster.routing.RoutingTable) NodeVersionAllocationDecider(org.elasticsearch.cluster.routing.allocation.decider.NodeVersionAllocationDecider) Test(org.junit.Test)

Aggregations

AllocationId (org.elasticsearch.cluster.routing.AllocationId)24 ShardId (org.elasticsearch.index.shard.ShardId)22 Test (org.junit.Test)16 IndexShardRoutingTable (org.elasticsearch.cluster.routing.IndexShardRoutingTable)7 Matchers.containsString (org.hamcrest.Matchers.containsString)7 Matchers.hasToString (org.hamcrest.Matchers.hasToString)7 AtomicLong (java.util.concurrent.atomic.AtomicLong)6 ClusterState (org.elasticsearch.cluster.ClusterState)5 DiscoveryNode (org.elasticsearch.cluster.node.DiscoveryNode)4 IndexRoutingTable (org.elasticsearch.cluster.routing.IndexRoutingTable)4 RoutingTable (org.elasticsearch.cluster.routing.RoutingTable)4 Path (java.nio.file.Path)3 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)3 AtomicReference (java.util.concurrent.atomic.AtomicReference)3 IndexMetadata (org.elasticsearch.cluster.metadata.IndexMetadata)3 BalancedShardsAllocator (org.elasticsearch.cluster.routing.allocation.allocator.BalancedShardsAllocator)3 AllocationDeciders (org.elasticsearch.cluster.routing.allocation.decider.AllocationDeciders)3 NodeVersionAllocationDecider (org.elasticsearch.cluster.routing.allocation.decider.NodeVersionAllocationDecider)3 TestGatewayAllocator (org.elasticsearch.test.gateway.TestGatewayAllocator)3 HashMap (java.util.HashMap)2