Search in sources :

Example 6 with Releasable

use of org.elasticsearch.common.lease.Releasable in project elasticsearch by elastic.

the class IndexShardOperationsLockTests method testOperationsDelayedIfBlock.

public void testOperationsDelayedIfBlock() throws ExecutionException, InterruptedException, TimeoutException {
    PlainActionFuture<Releasable> future = new PlainActionFuture<>();
    try (Releasable releasable = blockAndWait()) {
        block.acquire(future, ThreadPool.Names.GENERIC, true);
        assertFalse(future.isDone());
    }
    future.get(1, TimeUnit.HOURS).close();
}
Also used : PlainActionFuture(org.elasticsearch.action.support.PlainActionFuture) Releasable(org.elasticsearch.common.lease.Releasable)

Example 7 with Releasable

use of org.elasticsearch.common.lease.Releasable in project elasticsearch by elastic.

the class IndexShardOperationsLockTests method testOperationsIfClosed.

public void testOperationsIfClosed() throws ExecutionException, InterruptedException {
    PlainActionFuture<Releasable> future = new PlainActionFuture<>();
    block.close();
    block.acquire(future, ThreadPool.Names.GENERIC, true);
    ExecutionException exception = expectThrows(ExecutionException.class, future::get);
    assertThat(exception.getCause(), instanceOf(IndexShardClosedException.class));
}
Also used : PlainActionFuture(org.elasticsearch.action.support.PlainActionFuture) Releasable(org.elasticsearch.common.lease.Releasable) ExecutionException(java.util.concurrent.ExecutionException)

Example 8 with Releasable

use of org.elasticsearch.common.lease.Releasable in project elasticsearch by elastic.

the class IndexShardOperationsLockTests method testActiveOperationsCount.

public void testActiveOperationsCount() throws ExecutionException, InterruptedException {
    PlainActionFuture<Releasable> future1 = new PlainActionFuture<>();
    block.acquire(future1, ThreadPool.Names.GENERIC, true);
    assertTrue(future1.isDone());
    assertThat(block.getActiveOperationsCount(), equalTo(1));
    PlainActionFuture<Releasable> future2 = new PlainActionFuture<>();
    block.acquire(future2, ThreadPool.Names.GENERIC, true);
    assertTrue(future2.isDone());
    assertThat(block.getActiveOperationsCount(), equalTo(2));
    future1.get().close();
    assertThat(block.getActiveOperationsCount(), equalTo(1));
    // check idempotence
    future1.get().close();
    assertThat(block.getActiveOperationsCount(), equalTo(1));
    future2.get().close();
    assertThat(block.getActiveOperationsCount(), equalTo(0));
    try (Releasable releasable = blockAndWait()) {
        assertThat(block.getActiveOperationsCount(), equalTo(0));
    }
    PlainActionFuture<Releasable> future3 = new PlainActionFuture<>();
    block.acquire(future3, ThreadPool.Names.GENERIC, true);
    assertTrue(future3.isDone());
    assertThat(block.getActiveOperationsCount(), equalTo(1));
    future3.get().close();
    assertThat(block.getActiveOperationsCount(), equalTo(0));
}
Also used : PlainActionFuture(org.elasticsearch.action.support.PlainActionFuture) Releasable(org.elasticsearch.common.lease.Releasable)

Example 9 with Releasable

use of org.elasticsearch.common.lease.Releasable in project elasticsearch by elastic.

the class IndexShardOperationsLockTests method testThreadContextPreservedIfBlock.

/**
     * Tests that the ThreadContext is restored when a operation is executed after it has been delayed due to a block
     */
public void testThreadContextPreservedIfBlock() throws ExecutionException, InterruptedException, TimeoutException {
    final ThreadContext context = threadPool.getThreadContext();
    final Function<ActionListener<Releasable>, Boolean> contextChecker = (listener) -> {
        if ("bar".equals(context.getHeader("foo")) == false) {
            listener.onFailure(new IllegalStateException("context did not have value [bar] for header [foo]. Actual value [" + context.getHeader("foo") + "]"));
        } else if ("baz".equals(context.getTransient("bar")) == false) {
            listener.onFailure(new IllegalStateException("context did not have value [baz] for transient [bar]. Actual value [" + context.getTransient("bar") + "]"));
        } else {
            return true;
        }
        return false;
    };
    PlainActionFuture<Releasable> future = new PlainActionFuture<Releasable>() {

        @Override
        public void onResponse(Releasable releasable) {
            if (contextChecker.apply(this)) {
                super.onResponse(releasable);
            }
        }
    };
    PlainActionFuture<Releasable> future2 = new PlainActionFuture<Releasable>() {

        @Override
        public void onResponse(Releasable releasable) {
            if (contextChecker.apply(this)) {
                super.onResponse(releasable);
            }
        }
    };
    try (Releasable releasable = blockAndWait()) {
        // when the releasable is closed
        try (ThreadContext.StoredContext ignore = context.newStoredContext(false)) {
            context.putHeader("foo", "bar");
            context.putTransient("bar", "baz");
            // test both with and without a executor name
            block.acquire(future, ThreadPool.Names.GENERIC, true);
            block.acquire(future2, null, true);
        }
        assertFalse(future.isDone());
    }
    future.get(1, TimeUnit.HOURS).close();
    future2.get(1, TimeUnit.HOURS).close();
}
Also used : TestThreadPool(org.elasticsearch.threadpool.TestThreadPool) AfterClass(org.junit.AfterClass) BeforeClass(org.junit.BeforeClass) PlainActionFuture(org.elasticsearch.action.support.PlainActionFuture) ThreadContext(org.elasticsearch.common.util.concurrent.ThreadContext) TimeoutException(java.util.concurrent.TimeoutException) Function(java.util.function.Function) Supplier(java.util.function.Supplier) ArrayList(java.util.ArrayList) ExecutionException(java.util.concurrent.ExecutionException) TimeUnit(java.util.concurrent.TimeUnit) Matchers.instanceOf(org.hamcrest.Matchers.instanceOf) CountDownLatch(java.util.concurrent.CountDownLatch) List(java.util.List) After(org.junit.After) Matchers.equalTo(org.hamcrest.Matchers.equalTo) ThreadPool(org.elasticsearch.threadpool.ThreadPool) ESTestCase(org.elasticsearch.test.ESTestCase) ActionListener(org.elasticsearch.action.ActionListener) Releasable(org.elasticsearch.common.lease.Releasable) Before(org.junit.Before) ActionListener(org.elasticsearch.action.ActionListener) PlainActionFuture(org.elasticsearch.action.support.PlainActionFuture) ThreadContext(org.elasticsearch.common.util.concurrent.ThreadContext) Releasable(org.elasticsearch.common.lease.Releasable)

Example 10 with Releasable

use of org.elasticsearch.common.lease.Releasable in project elasticsearch by elastic.

the class IndexShardTests method testOperationLocksOnReplicaShards.

public void testOperationLocksOnReplicaShards() throws InterruptedException, ExecutionException, IOException {
    final ShardId shardId = new ShardId("test", "_na_", 0);
    final IndexShard indexShard;
    switch(randomInt(2)) {
        case 0:
            // started replica
            indexShard = newStartedShard(false);
            break;
        case 1:
            {
                // initializing replica / primary
                final boolean relocating = randomBoolean();
                ShardRouting routing = TestShardRouting.newShardRouting(shardId, "local_node", relocating ? "sourceNode" : null, relocating ? randomBoolean() : false, ShardRoutingState.INITIALIZING, relocating ? AllocationId.newRelocation(AllocationId.newInitializing()) : AllocationId.newInitializing());
                indexShard = newShard(routing);
                break;
            }
        case 2:
            {
                // relocation source
                indexShard = newStartedShard(true);
                ShardRouting routing = indexShard.routingEntry();
                routing = TestShardRouting.newShardRouting(routing.shardId(), routing.currentNodeId(), "otherNode", true, ShardRoutingState.RELOCATING, AllocationId.newRelocation(routing.allocationId()));
                indexShard.updateRoutingEntry(routing);
                indexShard.relocated("test");
                break;
            }
        default:
            throw new UnsupportedOperationException("get your numbers straight");
    }
    final ShardRouting shardRouting = indexShard.routingEntry();
    logger.info("shard routing to {}", shardRouting);
    assertEquals(0, indexShard.getActiveOperationsCount());
    if (shardRouting.primary() == false) {
        try {
            indexShard.acquirePrimaryOperationLock(null, ThreadPool.Names.INDEX);
            fail("shard shouldn't accept primary ops");
        } catch (IllegalStateException ignored) {
        }
    }
    final long primaryTerm = indexShard.getPrimaryTerm();
    Releasable operation1 = acquireReplicaOperationLockBlockingly(indexShard, primaryTerm);
    assertEquals(1, indexShard.getActiveOperationsCount());
    Releasable operation2 = acquireReplicaOperationLockBlockingly(indexShard, primaryTerm);
    assertEquals(2, indexShard.getActiveOperationsCount());
    try {
        indexShard.acquireReplicaOperationLock(primaryTerm - 1, null, ThreadPool.Names.INDEX);
        fail("you can not increment the operation counter with an older primary term");
    } catch (IllegalArgumentException e) {
        assertThat(e.getMessage(), containsString("operation term"));
        assertThat(e.getMessage(), containsString("too old"));
    }
    // but you can increment with a newer one..
    acquireReplicaOperationLockBlockingly(indexShard, primaryTerm + 1 + randomInt(20)).close();
    Releasables.close(operation1, operation2);
    assertEquals(0, indexShard.getActiveOperationsCount());
    closeShards(indexShard);
}
Also used : Releasable(org.elasticsearch.common.lease.Releasable) TestShardRouting(org.elasticsearch.cluster.routing.TestShardRouting) ShardRouting(org.elasticsearch.cluster.routing.ShardRouting)

Aggregations

Releasable (org.elasticsearch.common.lease.Releasable)38 PlainActionFuture (org.elasticsearch.action.support.PlainActionFuture)7 IndexShard (org.elasticsearch.index.shard.IndexShard)7 ClusterState (org.elasticsearch.cluster.ClusterState)6 IOException (java.io.IOException)5 CountDownLatch (java.util.concurrent.CountDownLatch)5 ElasticsearchException (org.elasticsearch.ElasticsearchException)5 ShardRouting (org.elasticsearch.cluster.routing.ShardRouting)5 TestShardRouting (org.elasticsearch.cluster.routing.TestShardRouting)5 Settings (org.elasticsearch.common.settings.Settings)5 ExecutionException (java.util.concurrent.ExecutionException)4 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)4 BytesReference (org.elasticsearch.common.bytes.BytesReference)4 ArrayList (java.util.ArrayList)3 ActionListener (org.elasticsearch.action.ActionListener)3 ClusterBlockException (org.elasticsearch.cluster.block.ClusterBlockException)3 AbstractRunnable (org.elasticsearch.common.util.concurrent.AbstractRunnable)3 DiscoverySettings (org.elasticsearch.discovery.DiscoverySettings)3 ShardId (org.elasticsearch.index.shard.ShardId)3 List (java.util.List)2