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());
}
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));
}
}
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();
}
}
Aggregations