Search in sources :

Example 16 with Releasable

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

the class TcpTransport method disconnectFromNode.

/**
     * Disconnects from a node, only if the relevant channel is found to be part of the node channels.
     */
protected boolean disconnectFromNode(DiscoveryNode node, Channel channel, String reason) {
    // this might be called multiple times from all the node channels, so do a lightweight
    // check outside of the lock
    NodeChannels nodeChannels = connectedNodes.get(node);
    if (nodeChannels != null && nodeChannels.hasChannel(channel)) {
        try (Releasable ignored = connectionLock.acquire(node.getId())) {
            nodeChannels = connectedNodes.get(node);
            // check again within the connection lock, if its still applicable to remove it
            if (nodeChannels != null && nodeChannels.hasChannel(channel)) {
                connectedNodes.remove(node);
                closeAndNotify(node, nodeChannels, reason);
                return true;
            }
        }
    }
    return false;
}
Also used : Releasable(org.elasticsearch.common.lease.Releasable)

Example 17 with Releasable

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

the class RecoverySourceHandler method finalizeRecovery.

/*
     * finalizes the recovery process
     */
public void finalizeRecovery() {
    if (shard.state() == IndexShardState.CLOSED) {
        throw new IndexShardClosedException(request.shardId());
    }
    cancellableThreads.checkForCancel();
    StopWatch stopWatch = new StopWatch().start();
    logger.trace("finalizing recovery");
    cancellableThreads.execute(() -> {
        shard.markAllocationIdAsInSync(recoveryTarget.getTargetAllocationId());
        recoveryTarget.finalizeRecovery(shard.getGlobalCheckpoint());
    });
    if (request.isPrimaryRelocation()) {
        // state. This means that no new recovery can be completed based on information of a newer cluster state than the current one.
        try (Releasable ignored = delayNewRecoveries.apply("primary relocation hand-off in progress or completed for " + shardId)) {
            final long currentClusterStateVersion = currentClusterStateVersionSupplier.get();
            logger.trace("waiting on remote node to have cluster state with version [{}]", currentClusterStateVersion);
            cancellableThreads.execute(() -> recoveryTarget.ensureClusterStateVersion(currentClusterStateVersion));
            logger.trace("performing relocation hand-off");
            cancellableThreads.execute(() -> shard.relocated("to " + request.targetNode()));
        }
    /*
             * if the recovery process fails after setting the shard state to RELOCATED, both relocation source and
             * target are failed (see {@link IndexShard#updateRoutingEntry}).
             */
    }
    stopWatch.stop();
    logger.trace("finalizing recovery took [{}]", stopWatch.totalTime());
}
Also used : IndexShardClosedException(org.elasticsearch.index.shard.IndexShardClosedException) Releasable(org.elasticsearch.common.lease.Releasable) StopWatch(org.elasticsearch.common.StopWatch)

Example 18 with Releasable

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

the class TransportReplicationAction method acquirePrimaryShardReference.

/**
     * Tries to acquire reference to {@link IndexShard} to perform a primary operation. Released after performing primary operation locally
     * and replication of the operation to all replica shards is completed / failed (see {@link ReplicationOperation}).
     */
private void acquirePrimaryShardReference(ShardId shardId, String allocationId, ActionListener<PrimaryShardReference> onReferenceAcquired) {
    IndexShard indexShard = getIndexShard(shardId);
    // the replica will take over and a replica will be assigned to the first node.
    if (indexShard.routingEntry().primary() == false) {
        throw new ReplicationOperation.RetryOnPrimaryException(indexShard.shardId(), "actual shard is not a primary " + indexShard.routingEntry());
    }
    final String actualAllocationId = indexShard.routingEntry().allocationId().getId();
    if (actualAllocationId.equals(allocationId) == false) {
        throw new ShardNotFoundException(shardId, "expected aID [{}] but found [{}]", allocationId, actualAllocationId);
    }
    ActionListener<Releasable> onAcquired = new ActionListener<Releasable>() {

        @Override
        public void onResponse(Releasable releasable) {
            onReferenceAcquired.onResponse(new PrimaryShardReference(indexShard, releasable));
        }

        @Override
        public void onFailure(Exception e) {
            onReferenceAcquired.onFailure(e);
        }
    };
    indexShard.acquirePrimaryOperationLock(onAcquired, executor);
}
Also used : ShardNotFoundException(org.elasticsearch.index.shard.ShardNotFoundException) ActionListener(org.elasticsearch.action.ActionListener) IndexShard(org.elasticsearch.index.shard.IndexShard) Releasable(org.elasticsearch.common.lease.Releasable) ElasticsearchException(org.elasticsearch.ElasticsearchException) IndexClosedException(org.elasticsearch.indices.IndexClosedException) NodeClosedException(org.elasticsearch.node.NodeClosedException) ShardNotFoundException(org.elasticsearch.index.shard.ShardNotFoundException) ConnectTransportException(org.elasticsearch.transport.ConnectTransportException) IndexNotFoundException(org.elasticsearch.index.IndexNotFoundException) NoNodeAvailableException(org.elasticsearch.client.transport.NoNodeAvailableException) TransportException(org.elasticsearch.transport.TransportException) ClusterBlockException(org.elasticsearch.cluster.block.ClusterBlockException) IOException(java.io.IOException) UnavailableShardsException(org.elasticsearch.action.UnavailableShardsException)

Example 19 with Releasable

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

the class IndexShardOperationsLock method acquire.

/**
     * Acquires a lock whenever lock acquisition is not blocked. If the lock is directly available, the provided
     * ActionListener will be called on the calling thread. During calls of {@link #blockOperations(long, TimeUnit, Runnable)}, lock
     * acquisition can be delayed. The provided ActionListener will then be called using the provided executor once blockOperations
     * terminates.
     *
     * @param onAcquired ActionListener that is invoked once acquisition is successful or failed
     * @param executorOnDelay executor to use for delayed call
     * @param forceExecution whether the runnable should force its execution in case it gets rejected
     */
public void acquire(ActionListener<Releasable> onAcquired, String executorOnDelay, boolean forceExecution) {
    if (closed) {
        onAcquired.onFailure(new IndexShardClosedException(shardId));
        return;
    }
    Releasable releasable;
    try {
        synchronized (this) {
            releasable = tryAcquire();
            if (releasable == null) {
                // blockOperations is executing, this operation will be retried by blockOperations once it finishes
                if (delayedOperations == null) {
                    delayedOperations = new ArrayList<>();
                }
                final Supplier<StoredContext> contextSupplier = threadPool.getThreadContext().newRestorableContext(false);
                if (executorOnDelay != null) {
                    delayedOperations.add(new ThreadedActionListener<>(logger, threadPool, executorOnDelay, new ContextPreservingActionListener<>(contextSupplier, onAcquired), forceExecution));
                } else {
                    delayedOperations.add(new ContextPreservingActionListener<>(contextSupplier, onAcquired));
                }
                return;
            }
        }
    } catch (InterruptedException e) {
        onAcquired.onFailure(e);
        return;
    }
    onAcquired.onResponse(releasable);
}
Also used : StoredContext(org.elasticsearch.common.util.concurrent.ThreadContext.StoredContext) ContextPreservingActionListener(org.elasticsearch.action.support.ContextPreservingActionListener) Releasable(org.elasticsearch.common.lease.Releasable)

Example 20 with Releasable

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

the class InternalEngine method pruneDeletedTombstones.

private void pruneDeletedTombstones() {
    long timeMSec = engineConfig.getThreadPool().relativeTimeInMillis();
    // we only need to prune the deletes map; the current/old version maps are cleared on refresh:
    for (Map.Entry<BytesRef, VersionValue> entry : versionMap.getAllTombstones()) {
        BytesRef uid = entry.getKey();
        try (Releasable ignored = acquireLock(uid)) {
            // can we do it without this lock on each value? maybe batch to a set and get the lock once per set?
            // Must re-get it here, vs using entry.getValue(), in case the uid was indexed/deleted since we pulled the iterator:
            VersionValue versionValue = versionMap.getTombstoneUnderLock(uid);
            if (versionValue != null) {
                if (timeMSec - versionValue.time() > getGcDeletesInMillis()) {
                    versionMap.removeTombstoneUnderLock(uid);
                }
            }
        }
    }
    lastDeleteVersionPruneTimeMSec = timeMSec;
}
Also used : Releasable(org.elasticsearch.common.lease.Releasable) Map(java.util.Map) HashMap(java.util.HashMap) BytesRef(org.apache.lucene.util.BytesRef)

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