Search in sources :

Example 36 with Releasable

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

the class ReleasablesTests method testReleaseOnce.

public void testReleaseOnce() {
    AtomicInteger count = new AtomicInteger(0);
    Releasable releasable = Releasables.releaseOnce(count::incrementAndGet, count::incrementAndGet);
    assertEquals(0, count.get());
    releasable.close();
    assertEquals(2, count.get());
    releasable.close();
    assertEquals(2, count.get());
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Releasable(org.elasticsearch.common.lease.Releasable)

Example 37 with Releasable

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

the class TribeIT method testOnConflictPrefer.

public void testOnConflictPrefer() throws Exception {
    final String preference = randomFrom(cluster1, cluster2).getClusterName();
    Settings additionalSettings = Settings.builder().put("tribe.on_conflict", "prefer_" + preference).build();
    try (Releasable tribeNode = startTribeNode(ALL, additionalSettings)) {
        assertAcked(cluster1.client().admin().indices().prepareCreate("test1"));
        assertAcked(cluster1.client().admin().indices().prepareCreate("shared"));
        ensureGreen(cluster1.client());
        assertAcked(cluster2.client().admin().indices().prepareCreate("test2"));
        assertAcked(cluster2.client().admin().indices().prepareCreate("shared"));
        ensureGreen(cluster2.client());
        // Wait for the tribe node to connect to the two remote clusters
        assertNodes(ALL);
        // Wait for the tribe node to retrieve the indices into its cluster state
        assertIndicesExist(client(), "test1", "test2", "shared");
        ClusterState clusterState = client().admin().cluster().prepareState().get().getState();
        assertThat(clusterState.getMetaData().hasIndex("test1"), is(true));
        assertThat(clusterState.getMetaData().index("test1").getSettings().get("tribe.name"), equalTo(cluster1.getClusterName()));
        assertThat(clusterState.getMetaData().hasIndex("test2"), is(true));
        assertThat(clusterState.getMetaData().index("test2").getSettings().get("tribe.name"), equalTo(cluster2.getClusterName()));
        assertThat(clusterState.getMetaData().hasIndex("shared"), is(true));
        assertThat(clusterState.getMetaData().index("shared").getSettings().get("tribe.name"), equalTo(preference));
    }
}
Also used : ClusterState(org.elasticsearch.cluster.ClusterState) Releasable(org.elasticsearch.common.lease.Releasable) Matchers.containsString(org.hamcrest.Matchers.containsString) Settings(org.elasticsearch.common.settings.Settings) DiscoverySettings(org.elasticsearch.discovery.DiscoverySettings)

Example 38 with Releasable

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

the class NodeConnectionsService method connectToNodes.

public void connectToNodes(DiscoveryNodes discoveryNodes) {
    CountDownLatch latch = new CountDownLatch(discoveryNodes.getSize());
    for (final DiscoveryNode node : discoveryNodes) {
        final boolean connected;
        try (Releasable ignored = nodeLocks.acquire(node)) {
            nodes.putIfAbsent(node, 0);
            connected = transportService.nodeConnected(node);
        }
        if (connected) {
            latch.countDown();
        } else {
            // spawn to another thread to do in parallel
            threadPool.executor(ThreadPool.Names.MANAGEMENT).execute(new AbstractRunnable() {

                @Override
                public void onFailure(Exception e) {
                    // both errors and rejections are logged here. the service
                    // will try again after `cluster.nodes.reconnect_interval` on all nodes but the current master.
                    // On the master, node fault detection will remove these nodes from the cluster as their are not
                    // connected. Note that it is very rare that we end up here on the master.
                    logger.warn((Supplier<?>) () -> new ParameterizedMessage("failed to connect to {}", node), e);
                }

                @Override
                protected void doRun() throws Exception {
                    try (Releasable ignored = nodeLocks.acquire(node)) {
                        validateAndConnectIfNeeded(node);
                    }
                }

                @Override
                public void onAfter() {
                    latch.countDown();
                }
            });
        }
    }
    try {
        latch.await();
    } catch (InterruptedException e) {
        Thread.currentThread().interrupt();
    }
}
Also used : AbstractRunnable(org.elasticsearch.common.util.concurrent.AbstractRunnable) DiscoveryNode(org.elasticsearch.cluster.node.DiscoveryNode) Releasable(org.elasticsearch.common.lease.Releasable) Supplier(org.apache.logging.log4j.util.Supplier) ParameterizedMessage(org.apache.logging.log4j.message.ParameterizedMessage) CountDownLatch(java.util.concurrent.CountDownLatch)

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