Search in sources :

Example 31 with Releasable

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

the class TcpTransport method connectToNode.

@Override
public void connectToNode(DiscoveryNode node, ConnectionProfile connectionProfile, CheckedBiConsumer<Connection, ConnectionProfile, IOException> connectionValidator) throws ConnectTransportException {
    connectionProfile = resolveConnectionProfile(connectionProfile, defaultConnectionProfile);
    if (node == null) {
        throw new ConnectTransportException(null, "can't connect to a null node");
    }
    // ensure we don't open connections while we are closing
    globalLock.readLock().lock();
    try {
        ensureOpen();
        try (Releasable ignored = connectionLock.acquire(node.getId())) {
            NodeChannels nodeChannels = connectedNodes.get(node);
            if (nodeChannels != null) {
                return;
            }
            try {
                try {
                    nodeChannels = openConnection(node, connectionProfile);
                    connectionValidator.accept(nodeChannels, connectionProfile);
                } catch (Exception e) {
                    logger.trace((Supplier<?>) () -> new ParameterizedMessage("failed to connect to [{}], cleaning dangling connections", node), e);
                    IOUtils.closeWhileHandlingException(nodeChannels);
                    throw e;
                }
                // we acquire a connection lock, so no way there is an existing connection
                connectedNodes.put(node, nodeChannels);
                if (logger.isDebugEnabled()) {
                    logger.debug("connected to node [{}]", node);
                }
                transportServiceAdapter.onNodeConnected(node);
            } catch (ConnectTransportException e) {
                throw e;
            } catch (Exception e) {
                throw new ConnectTransportException(node, "general node connection failure", e);
            }
        }
    } finally {
        globalLock.readLock().unlock();
    }
}
Also used : Releasable(org.elasticsearch.common.lease.Releasable) Supplier(org.apache.logging.log4j.util.Supplier) ParameterizedMessage(org.apache.logging.log4j.message.ParameterizedMessage) ElasticsearchException(org.elasticsearch.ElasticsearchException) NotCompressedException(org.elasticsearch.common.compress.NotCompressedException) StreamCorruptedException(java.io.StreamCorruptedException) CancelledKeyException(java.nio.channels.CancelledKeyException) NetworkExceptionHelper.isCloseConnectionException(org.elasticsearch.common.transport.NetworkExceptionHelper.isCloseConnectionException) BindException(java.net.BindException) IOException(java.io.IOException) UnknownHostException(java.net.UnknownHostException) EsRejectedExecutionException(org.elasticsearch.common.util.concurrent.EsRejectedExecutionException) NetworkExceptionHelper.isConnectException(org.elasticsearch.common.transport.NetworkExceptionHelper.isConnectException)

Example 32 with Releasable

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

the class TransportReplicationActionTests method testSeqNoIsSetOnPrimary.

public void testSeqNoIsSetOnPrimary() throws Exception {
    final String index = "test";
    final ShardId shardId = new ShardId(index, "_na_", 0);
    // we use one replica to check the primary term was set on the operation and sent to the replica
    setState(clusterService, state(index, true, ShardRoutingState.STARTED, randomFrom(ShardRoutingState.INITIALIZING, ShardRoutingState.STARTED)));
    logger.debug("--> using initial state:\n{}", clusterService.state());
    final ShardRouting routingEntry = clusterService.state().getRoutingTable().index("test").shard(0).primaryShard();
    Request request = new Request(shardId);
    TransportReplicationAction.ConcreteShardRequest<Request> concreteShardRequest = new TransportReplicationAction.ConcreteShardRequest<>(request, routingEntry.allocationId().getId());
    PlainActionFuture<TestResponse> listener = new PlainActionFuture<>();
    final IndexShard shard = mock(IndexShard.class);
    long primaryTerm = clusterService.state().getMetaData().index(index).primaryTerm(0);
    when(shard.getPrimaryTerm()).thenReturn(primaryTerm);
    when(shard.routingEntry()).thenReturn(routingEntry);
    AtomicBoolean closed = new AtomicBoolean();
    Releasable releasable = () -> {
        if (closed.compareAndSet(false, true) == false) {
            fail("releasable is closed twice");
        }
    };
    TestAction action = new TestAction(Settings.EMPTY, "testSeqNoIsSetOnPrimary", transportService, clusterService, shardStateAction, threadPool);
    TransportReplicationAction<Request, Request, TestResponse>.PrimaryOperationTransportHandler<Request, Request, TestResponse> primaryPhase = action.new PrimaryOperationTransportHandler();
    primaryPhase.messageReceived(concreteShardRequest, createTransportChannel(listener), null);
    CapturingTransport.CapturedRequest[] requestsToReplicas = transport.capturedRequests();
    assertThat(requestsToReplicas, arrayWithSize(1));
    assertThat(((TransportReplicationAction.ConcreteShardRequest<Request>) requestsToReplicas[0].request).getRequest().primaryTerm(), equalTo(primaryTerm));
}
Also used : IndexShard(org.elasticsearch.index.shard.IndexShard) TransportRequest(org.elasticsearch.transport.TransportRequest) CloseIndexRequest(org.elasticsearch.action.admin.indices.close.CloseIndexRequest) CoreMatchers.containsString(org.hamcrest.CoreMatchers.containsString) Matchers.anyString(org.mockito.Matchers.anyString) ShardId(org.elasticsearch.index.shard.ShardId) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) PlainActionFuture(org.elasticsearch.action.support.PlainActionFuture) Releasable(org.elasticsearch.common.lease.Releasable) TestShardRouting(org.elasticsearch.cluster.routing.TestShardRouting) ShardRouting(org.elasticsearch.cluster.routing.ShardRouting)

Example 33 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)

Example 34 with Releasable

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

the class InternalEngine method innerDelete.

private DeleteResult innerDelete(Delete delete) throws IOException {
    assert assertSequenceNumber(delete.origin(), delete.seqNo());
    final Translog.Location location;
    final long updatedVersion;
    final boolean found;
    long seqNo = delete.seqNo();
    try (Releasable ignored = acquireLock(delete.uid())) {
        lastWriteNanos = delete.startTime();
        final long currentVersion;
        final boolean deleted;
        final VersionValue versionValue = versionMap.getUnderLock(delete.uid());
        assert incrementVersionLookup();
        if (versionValue == null) {
            currentVersion = loadCurrentVersionFromIndex(delete.uid());
            deleted = currentVersion == Versions.NOT_FOUND;
        } else {
            currentVersion = checkDeletedAndGCed(versionValue);
            deleted = versionValue.delete();
        }
        final long expectedVersion = delete.version();
        Optional<DeleteResult> resultOnVersionConflict;
        try {
            final boolean isVersionConflict = checkVersionConflict(delete, currentVersion, expectedVersion, deleted);
            resultOnVersionConflict = isVersionConflict ? Optional.of(new DeleteResult(expectedVersion, delete.seqNo(), true)) : Optional.empty();
        } catch (IllegalArgumentException | VersionConflictEngineException ex) {
            resultOnVersionConflict = Optional.of(new DeleteResult(ex, expectedVersion, delete.seqNo()));
        }
        final DeleteResult deleteResult;
        if (resultOnVersionConflict.isPresent()) {
            deleteResult = resultOnVersionConflict.get();
        } else {
            if (delete.origin() == Operation.Origin.PRIMARY) {
                seqNo = seqNoService().generateSeqNo();
            }
            updatedVersion = delete.versionType().updateVersion(currentVersion, expectedVersion);
            found = deleteIfFound(delete.uid(), currentVersion, deleted, versionValue);
            deleteResult = new DeleteResult(updatedVersion, seqNo, found);
            versionMap.putUnderLock(delete.uid().bytes(), new DeleteVersionValue(updatedVersion, engineConfig.getThreadPool().relativeTimeInMillis()));
        }
        if (!deleteResult.hasFailure()) {
            location = delete.origin() != Operation.Origin.LOCAL_TRANSLOG_RECOVERY ? translog.add(new Translog.Delete(delete, deleteResult)) : null;
            deleteResult.setTranslogLocation(location);
        }
        deleteResult.setTook(System.nanoTime() - delete.startTime());
        deleteResult.freeze();
        return deleteResult;
    } finally {
        if (seqNo != SequenceNumbersService.UNASSIGNED_SEQ_NO) {
            seqNoService().markSeqNoAsCompleted(seqNo);
        }
    }
}
Also used : Translog(org.elasticsearch.index.translog.Translog) Releasable(org.elasticsearch.common.lease.Releasable)

Example 35 with Releasable

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

the class TribeIT method testMergingMultipleCustomMetaData.

public void testMergingMultipleCustomMetaData() throws Exception {
    removeCustomMetaData(cluster1, MergableCustomMetaData1.TYPE);
    removeCustomMetaData(cluster2, MergableCustomMetaData1.TYPE);
    MergableCustomMetaData1 firstCustomMetaDataType1 = new MergableCustomMetaData1(randomAsciiOfLength(10));
    MergableCustomMetaData1 secondCustomMetaDataType1 = new MergableCustomMetaData1(randomAsciiOfLength(10));
    MergableCustomMetaData2 firstCustomMetaDataType2 = new MergableCustomMetaData2(randomAsciiOfLength(10));
    MergableCustomMetaData2 secondCustomMetaDataType2 = new MergableCustomMetaData2(randomAsciiOfLength(10));
    List<MergableCustomMetaData1> mergedCustomMetaDataType1 = Arrays.asList(firstCustomMetaDataType1, secondCustomMetaDataType1);
    List<MergableCustomMetaData2> mergedCustomMetaDataType2 = Arrays.asList(firstCustomMetaDataType2, secondCustomMetaDataType2);
    Collections.sort(mergedCustomMetaDataType1, (cm1, cm2) -> cm2.getData().compareTo(cm1.getData()));
    Collections.sort(mergedCustomMetaDataType2, (cm1, cm2) -> cm2.getData().compareTo(cm1.getData()));
    try (Releasable tribeNode = startTribeNode()) {
        assertNodes(ALL);
        // test putting multiple custom md types propagates to tribe
        putCustomMetaData(cluster1, firstCustomMetaDataType1);
        putCustomMetaData(cluster1, firstCustomMetaDataType2);
        assertCustomMetaDataUpdated(internalCluster(), firstCustomMetaDataType1);
        assertCustomMetaDataUpdated(internalCluster(), firstCustomMetaDataType2);
        // test multiple same type custom md is merged and propagates to tribe
        putCustomMetaData(cluster2, secondCustomMetaDataType1);
        assertCustomMetaDataUpdated(internalCluster(), firstCustomMetaDataType2);
        assertCustomMetaDataUpdated(internalCluster(), mergedCustomMetaDataType1.get(0));
        // test multiple same type custom md is merged and propagates to tribe
        putCustomMetaData(cluster2, secondCustomMetaDataType2);
        assertCustomMetaDataUpdated(internalCluster(), mergedCustomMetaDataType1.get(0));
        assertCustomMetaDataUpdated(internalCluster(), mergedCustomMetaDataType2.get(0));
        // test removing custom md is propagates to tribe
        removeCustomMetaData(cluster2, secondCustomMetaDataType1.getWriteableName());
        assertCustomMetaDataUpdated(internalCluster(), firstCustomMetaDataType1);
        assertCustomMetaDataUpdated(internalCluster(), mergedCustomMetaDataType2.get(0));
        removeCustomMetaData(cluster2, secondCustomMetaDataType2.getWriteableName());
        assertCustomMetaDataUpdated(internalCluster(), firstCustomMetaDataType1);
        assertCustomMetaDataUpdated(internalCluster(), firstCustomMetaDataType2);
    }
}
Also used : MergableCustomMetaData1(org.elasticsearch.tribe.TribeServiceTests.MergableCustomMetaData1) MergableCustomMetaData2(org.elasticsearch.tribe.TribeServiceTests.MergableCustomMetaData2) Releasable(org.elasticsearch.common.lease.Releasable)

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