Search in sources :

Example 11 with AllocationId

use of org.elasticsearch.cluster.routing.AllocationId in project crate by crate.

the class ReplicationTrackerRetentionLeaseTests method testUnnecessaryPersistenceOfRetentionLeases.

@Test
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) -> {
    }, 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);
    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()));
}
Also used : ShardId(org.elasticsearch.index.shard.ShardId) Path(java.nio.file.Path) AllocationId(org.elasticsearch.cluster.routing.AllocationId) AtomicLong(java.util.concurrent.atomic.AtomicLong) Test(org.junit.Test)

Example 12 with AllocationId

use of org.elasticsearch.cluster.routing.AllocationId in project crate by crate.

the class ReplicationTrackerRetentionLeaseTests method testCloneDuplicateRetentionLease.

@Test
public void testCloneDuplicateRetentionLease() {
    final AllocationId allocationId = AllocationId.newInitializing();
    final ReplicationTracker replicationTracker = new ReplicationTracker(new ShardId("test", "_na", 0), allocationId.getId(), IndexSettingsModule.newIndexSettings("test", Settings.EMPTY), randomLongBetween(1, Long.MAX_VALUE), 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);
    replicationTracker.addRetentionLease("source", randomLongBetween(0L, Long.MAX_VALUE), "test-source", ActionListener.wrap(() -> {
    }));
    replicationTracker.addRetentionLease("exists", randomLongBetween(0L, Long.MAX_VALUE), "test-source", ActionListener.wrap(() -> {
    }));
    assertThat(expectThrows(RetentionLeaseAlreadyExistsException.class, () -> replicationTracker.cloneRetentionLease("source", "exists", ActionListener.wrap(() -> {
    }))).getMessage(), equalTo("retention lease with ID [exists] already exists"));
}
Also used : ShardId(org.elasticsearch.index.shard.ShardId) AllocationId(org.elasticsearch.cluster.routing.AllocationId) Test(org.junit.Test)

Example 13 with AllocationId

use of org.elasticsearch.cluster.routing.AllocationId in project crate by crate.

the class ReplicationTrackerRetentionLeaseTests method testCloneRetentionLease.

@Test
public void testCloneRetentionLease() {
    final AllocationId allocationId = AllocationId.newInitializing();
    final AtomicReference<ReplicationTracker> replicationTrackerRef = new AtomicReference<>();
    final AtomicLong timeReference = new AtomicLong();
    final AtomicBoolean synced = new AtomicBoolean();
    final ReplicationTracker replicationTracker = new ReplicationTracker(new ShardId("test", "_na", 0), allocationId.getId(), IndexSettingsModule.newIndexSettings("test", Settings.EMPTY), randomLongBetween(1, Long.MAX_VALUE), UNASSIGNED_SEQ_NO, value -> {
    }, timeReference::get, (leases, listener) -> {
        assertFalse(Thread.holdsLock(replicationTrackerRef.get()));
        assertTrue(synced.compareAndSet(false, true));
        listener.onResponse(new ReplicationResponse());
    }, OPS_BASED_RECOVERY_ALWAYS_REASONABLE);
    replicationTrackerRef.set(replicationTracker);
    replicationTracker.updateFromMaster(randomNonNegativeLong(), Collections.singleton(allocationId.getId()), routingTable(Collections.emptySet(), allocationId));
    replicationTracker.activatePrimaryMode(SequenceNumbers.NO_OPS_PERFORMED);
    final long addTime = randomLongBetween(timeReference.get(), Long.MAX_VALUE);
    timeReference.set(addTime);
    final long minimumRetainingSequenceNumber = randomLongBetween(SequenceNumbers.NO_OPS_PERFORMED, Long.MAX_VALUE);
    final PlainActionFuture<ReplicationResponse> addFuture = new PlainActionFuture<>();
    replicationTracker.addRetentionLease("source", minimumRetainingSequenceNumber, "test-source", addFuture);
    addFuture.actionGet();
    assertTrue(synced.get());
    synced.set(false);
    final long cloneTime = randomLongBetween(timeReference.get(), Long.MAX_VALUE);
    timeReference.set(cloneTime);
    final PlainActionFuture<ReplicationResponse> cloneFuture = new PlainActionFuture<>();
    final RetentionLease clonedLease = replicationTracker.cloneRetentionLease("source", "target", cloneFuture);
    cloneFuture.actionGet();
    assertTrue(synced.get());
    synced.set(false);
    assertThat(clonedLease.id(), equalTo("target"));
    assertThat(clonedLease.retainingSequenceNumber(), equalTo(minimumRetainingSequenceNumber));
    assertThat(clonedLease.timestamp(), equalTo(cloneTime));
    assertThat(clonedLease.source(), equalTo("test-source"));
    assertThat(replicationTracker.getRetentionLeases().get("target"), equalTo(clonedLease));
}
Also used : AllocationId(org.elasticsearch.cluster.routing.AllocationId) AtomicReference(java.util.concurrent.atomic.AtomicReference) ReplicationResponse(org.elasticsearch.action.support.replication.ReplicationResponse) ShardId(org.elasticsearch.index.shard.ShardId) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) AtomicLong(java.util.concurrent.atomic.AtomicLong) PlainActionFuture(org.elasticsearch.action.support.PlainActionFuture) Test(org.junit.Test)

Example 14 with AllocationId

use of org.elasticsearch.cluster.routing.AllocationId in project crate by crate.

the class PeerRecoveryRetentionLeaseExpiryTests method setUpReplicationTracker.

@Before
public void setUpReplicationTracker() throws InterruptedException {
    final AllocationId primaryAllocationId = AllocationId.newInitializing();
    currentTimeMillis = new AtomicLong(randomLongBetween(0, 1024));
    if (randomBoolean()) {
        settings = Settings.builder().put(IndexSettings.INDEX_SOFT_DELETES_SETTING.getKey(), true).put(IndexSettings.INDEX_SOFT_DELETES_RETENTION_LEASE_PERIOD_SETTING.getKey(), TimeValue.timeValueMillis(randomLongBetween(1, TimeValue.timeValueHours(12).millis())).getStringRep()).build();
    } else {
        settings = Settings.builder().put(IndexSettings.INDEX_SOFT_DELETES_SETTING.getKey(), true).build();
    }
    // must be set in each test
    safeCommitInfo = null;
    final long primaryTerm = randomLongBetween(1, Long.MAX_VALUE);
    replicationTracker = new ReplicationTracker(new ShardId("test", "_na", 0), primaryAllocationId.getId(), IndexSettingsModule.newIndexSettings("test", settings), primaryTerm, UNASSIGNED_SEQ_NO, value -> {
    }, currentTimeMillis::get, (leases, listener) -> {
    }, () -> safeCommitInfo);
    replicationTracker.updateFromMaster(1L, Collections.singleton(primaryAllocationId.getId()), routingTable(Collections.emptySet(), primaryAllocationId));
    replicationTracker.activatePrimaryMode(SequenceNumbers.NO_OPS_PERFORMED);
    final AllocationId replicaAllocationId = AllocationId.newInitializing();
    final IndexShardRoutingTable routingTableWithReplica = routingTable(Collections.singleton(replicaAllocationId), primaryAllocationId);
    replicationTracker.updateFromMaster(2L, Collections.singleton(primaryAllocationId.getId()), routingTableWithReplica);
    replicationTracker.addPeerRecoveryRetentionLease(routingTableWithReplica.getByAllocationId(replicaAllocationId.getId()).currentNodeId(), randomCheckpoint(), EMPTY_LISTENER);
    replicationTracker.initiateTracking(replicaAllocationId.getId());
    replicationTracker.markAllocationIdAsInSync(replicaAllocationId.getId(), randomCheckpoint());
}
Also used : ShardId(org.elasticsearch.index.shard.ShardId) ShardRouting(org.elasticsearch.cluster.routing.ShardRouting) ShardId(org.elasticsearch.index.shard.ShardId) SafeCommitInfo(org.elasticsearch.index.engine.SafeCommitInfo) Tuple(io.crate.common.collections.Tuple) IndexSettingsModule(org.elasticsearch.test.IndexSettingsModule) UNASSIGNED_SEQ_NO(org.elasticsearch.index.seqno.SequenceNumbers.UNASSIGNED_SEQ_NO) Set(java.util.Set) IndexShardRoutingTable(org.elasticsearch.cluster.routing.IndexShardRoutingTable) Test(org.junit.Test) Collectors(java.util.stream.Collectors) AtomicLong(java.util.concurrent.atomic.AtomicLong) ReplicationResponse(org.elasticsearch.action.support.replication.ReplicationResponse) Stream(java.util.stream.Stream) Settings(org.elasticsearch.common.settings.Settings) Matchers.equalTo(org.hamcrest.Matchers.equalTo) IndexSettings(org.elasticsearch.index.IndexSettings) TimeValue(io.crate.common.unit.TimeValue) Matchers.hasSize(org.hamcrest.Matchers.hasSize) AllocationId(org.elasticsearch.cluster.routing.AllocationId) Collections(java.util.Collections) ActionListener(org.elasticsearch.action.ActionListener) Before(org.junit.Before) IndexShardRoutingTable(org.elasticsearch.cluster.routing.IndexShardRoutingTable) AtomicLong(java.util.concurrent.atomic.AtomicLong) AllocationId(org.elasticsearch.cluster.routing.AllocationId) Before(org.junit.Before)

Example 15 with AllocationId

use of org.elasticsearch.cluster.routing.AllocationId in project elasticsearch by elastic.

the class IndexShardTests method testWriteShardState.

public void testWriteShardState() throws Exception {
    try (NodeEnvironment env = newNodeEnvironment()) {
        ShardId id = new ShardId("foo", "fooUUID", 1);
        boolean primary = randomBoolean();
        AllocationId allocationId = randomBoolean() ? null : randomAllocationId();
        ShardStateMetaData state1 = new ShardStateMetaData(primary, "fooUUID", allocationId);
        write(state1, env.availableShardPaths(id));
        ShardStateMetaData shardStateMetaData = load(logger, env.availableShardPaths(id));
        assertEquals(shardStateMetaData, state1);
        ShardStateMetaData state2 = new ShardStateMetaData(primary, "fooUUID", allocationId);
        write(state2, env.availableShardPaths(id));
        shardStateMetaData = load(logger, env.availableShardPaths(id));
        assertEquals(shardStateMetaData, state1);
        ShardStateMetaData state3 = new ShardStateMetaData(primary, "fooUUID", allocationId);
        write(state3, env.availableShardPaths(id));
        shardStateMetaData = load(logger, env.availableShardPaths(id));
        assertEquals(shardStateMetaData, state3);
        assertEquals("fooUUID", state3.indexUUID);
    }
}
Also used : NodeEnvironment(org.elasticsearch.env.NodeEnvironment) AllocationId(org.elasticsearch.cluster.routing.AllocationId)

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