Search in sources :

Example 6 with AllocationId

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])));
    }
}
Also used : ShardId(org.elasticsearch.index.shard.ShardId) AllocationId(org.elasticsearch.cluster.routing.AllocationId) ArrayList(java.util.ArrayList) Test(org.junit.Test)

Example 7 with AllocationId

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());
    }
}
Also used : HashMap(java.util.HashMap) AllocationId(org.elasticsearch.cluster.routing.AllocationId) AtomicReference(java.util.concurrent.atomic.AtomicReference) Matchers.hasToString(org.hamcrest.Matchers.hasToString) Matchers.containsString(org.hamcrest.Matchers.containsString) ShardId(org.elasticsearch.index.shard.ShardId) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) AtomicLong(java.util.concurrent.atomic.AtomicLong) Test(org.junit.Test)

Example 8 with AllocationId

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")));
}
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 9 with AllocationId

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")));
}
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 10 with AllocationId

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