Search in sources :

Example 26 with PlainActionFuture

use of org.elasticsearch.action.support.PlainActionFuture 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 27 with PlainActionFuture

use of org.elasticsearch.action.support.PlainActionFuture 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 28 with PlainActionFuture

use of org.elasticsearch.action.support.PlainActionFuture 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 29 with PlainActionFuture

use of org.elasticsearch.action.support.PlainActionFuture in project crate by crate.

the class BlobTransferTarget method restoreTransferStatus.

private BlobTransferStatus restoreTransferStatus(PutChunkReplicaRequest request) {
    LOGGER.trace("Restoring transferContext for PutChunkReplicaRequest with transferId {}", request.transferId);
    DiscoveryNodes nodes = clusterService.state().getNodes();
    DiscoveryNode recipientNodeId = nodes.get(request.sourceNodeId);
    String senderNodeId = nodes.getLocalNodeId();
    var listener = new PlainActionFuture<BlobTransferInfoResponse>();
    transportService.sendRequest(recipientNodeId, BlobHeadRequestHandler.Actions.GET_TRANSFER_INFO, new BlobInfoRequest(senderNodeId, request.transferId), TransportRequestOptions.EMPTY, new ActionListenerResponseHandler<>(listener, BlobTransferInfoResponse::new));
    BlobTransferInfoResponse transferInfoResponse = listener.actionGet();
    BlobShard blobShard = blobIndicesService.blobShardSafe(request.shardId());
    DigestBlob digestBlob = DigestBlob.resumeTransfer(blobShard.blobContainer(), transferInfoResponse.digest, request.transferId, request.currentPos);
    assert digestBlob != null : "DigestBlob couldn't be restored";
    BlobTransferStatus status;
    status = new BlobTransferStatus(request.shardId(), request.transferId, digestBlob);
    activeTransfers.put(request.transferId, status);
    LOGGER.trace("Restored transferStatus for digest {} transferId: {}", transferInfoResponse.digest, request.transferId);
    var getBlobHeadListener = new PlainActionFuture<>();
    transportService.sendRequest(recipientNodeId, BlobHeadRequestHandler.Actions.GET_BLOB_HEAD, new GetBlobHeadRequest(senderNodeId, request.transferId(), request.currentPos), TransportRequestOptions.EMPTY, new ActionListenerResponseHandler<>(getBlobHeadListener, in -> TransportResponse.Empty.INSTANCE));
    getBlobHeadListener.actionGet();
    return status;
}
Also used : GetBlobHeadRequest(io.crate.blob.transfer.GetBlobHeadRequest) ConcurrentCollections(org.elasticsearch.common.util.concurrent.ConcurrentCollections) BlobShard(io.crate.blob.v2.BlobShard) ClusterService(org.elasticsearch.cluster.service.ClusterService) CompletableFuture(java.util.concurrent.CompletableFuture) ArrayList(java.util.ArrayList) ConcurrentMap(java.util.concurrent.ConcurrentMap) Inject(org.elasticsearch.common.inject.Inject) DiscoveryNode(org.elasticsearch.cluster.node.DiscoveryNode) ActionListenerResponseHandler(org.elasticsearch.action.ActionListenerResponseHandler) ThreadPool(org.elasticsearch.threadpool.ThreadPool) TransportResponse(org.elasticsearch.transport.TransportResponse) TransportService(org.elasticsearch.transport.TransportService) DigestMismatchException(io.crate.blob.exceptions.DigestMismatchException) BlobHeadRequestHandler(io.crate.blob.transfer.BlobHeadRequestHandler) DiscoveryNodes(org.elasticsearch.cluster.node.DiscoveryNodes) PlainActionFuture(org.elasticsearch.action.support.PlainActionFuture) IOUtils(org.apache.lucene.util.IOUtils) UUID(java.util.UUID) BlobAlreadyExistsException(io.crate.blob.exceptions.BlobAlreadyExistsException) BlobInfoRequest(io.crate.blob.transfer.BlobInfoRequest) BlobIndicesService(io.crate.blob.v2.BlobIndicesService) File(java.io.File) ExecutionException(java.util.concurrent.ExecutionException) TimeUnit(java.util.concurrent.TimeUnit) CountDownLatch(java.util.concurrent.CountDownLatch) List(java.util.List) Logger(org.apache.logging.log4j.Logger) TimeValue(io.crate.common.unit.TimeValue) TransportRequestOptions(org.elasticsearch.transport.TransportRequestOptions) BlobTransferInfoResponse(io.crate.blob.transfer.BlobTransferInfoResponse) LogManager(org.apache.logging.log4j.LogManager) ConcurrentLinkedQueue(java.util.concurrent.ConcurrentLinkedQueue) GetBlobHeadRequest(io.crate.blob.transfer.GetBlobHeadRequest) DiscoveryNode(org.elasticsearch.cluster.node.DiscoveryNode) BlobTransferInfoResponse(io.crate.blob.transfer.BlobTransferInfoResponse) BlobInfoRequest(io.crate.blob.transfer.BlobInfoRequest) PlainActionFuture(org.elasticsearch.action.support.PlainActionFuture) BlobShard(io.crate.blob.v2.BlobShard) DiscoveryNodes(org.elasticsearch.cluster.node.DiscoveryNodes)

Example 30 with PlainActionFuture

use of org.elasticsearch.action.support.PlainActionFuture in project crate by crate.

the class BlobRecoveryHandler method getExistingDigestsFromTarget.

private Set<BytesArray> getExistingDigestsFromTarget(byte prefix) {
    var listener = new PlainActionFuture<BlobStartPrefixResponse>();
    transportService.sendRequest(request.targetNode(), BlobRecoveryTarget.Actions.START_PREFIX, new BlobStartPrefixSyncRequest(request.recoveryId(), request.shardId(), prefix), TransportRequestOptions.EMPTY, new ActionListenerResponseHandler<>(listener, BlobStartPrefixResponse::new));
    BlobStartPrefixResponse response = listener.actionGet();
    Set<BytesArray> result = new HashSet<>();
    for (byte[] digests : response.existingDigests) {
        result.add(new BytesArray(digests));
    }
    return result;
}
Also used : BytesArray(org.elasticsearch.common.bytes.BytesArray) PlainActionFuture(org.elasticsearch.action.support.PlainActionFuture) BlobStartPrefixSyncRequest(org.elasticsearch.indices.recovery.BlobStartPrefixSyncRequest) BlobStartPrefixResponse(org.elasticsearch.indices.recovery.BlobStartPrefixResponse) HashSet(java.util.HashSet)

Aggregations

PlainActionFuture (org.elasticsearch.action.support.PlainActionFuture)82 ShardId (org.elasticsearch.index.shard.ShardId)37 ClusterState (org.elasticsearch.cluster.ClusterState)28 ExecutionException (java.util.concurrent.ExecutionException)27 ShardRouting (org.elasticsearch.cluster.routing.ShardRouting)25 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)21 IOException (java.io.IOException)20 ArrayList (java.util.ArrayList)20 Test (org.junit.Test)20 IndexShardRoutingTable (org.elasticsearch.cluster.routing.IndexShardRoutingTable)18 ElasticsearchException (org.elasticsearch.ElasticsearchException)17 TransportRequest (org.elasticsearch.transport.TransportRequest)17 Matchers.anyString (org.mockito.Matchers.anyString)17 HashSet (java.util.HashSet)16 List (java.util.List)16 CloseIndexRequest (org.elasticsearch.action.admin.indices.close.CloseIndexRequest)16 IndexShard (org.elasticsearch.index.shard.IndexShard)16 CoreMatchers.containsString (org.hamcrest.CoreMatchers.containsString)16 DiscoveryNode (org.elasticsearch.cluster.node.DiscoveryNode)15 ClusterBlockException (org.elasticsearch.cluster.block.ClusterBlockException)13