use of java.util.concurrent.Future in project elasticsearch by elastic.
the class NodeJoinControllerTests method testSimpleJoinAccumulation.
public void testSimpleJoinAccumulation() throws InterruptedException, ExecutionException {
List<DiscoveryNode> nodes = new ArrayList<>();
nodes.add(clusterService.localNode());
int nodeId = 0;
for (int i = randomInt(5); i > 0; i--) {
DiscoveryNode node = newNode(nodeId++);
nodes.add(node);
joinNode(node);
}
nodeJoinController.startElectionContext();
ArrayList<Future<Void>> pendingJoins = new ArrayList<>();
for (int i = randomInt(5); i > 0; i--) {
DiscoveryNode node = newNode(nodeId++);
nodes.add(node);
pendingJoins.add(joinNodeAsync(node));
}
nodeJoinController.stopElectionContext("test");
boolean hadSyncJoin = false;
for (int i = randomInt(5); i > 0; i--) {
DiscoveryNode node = newNode(nodeId++);
nodes.add(node);
joinNode(node);
hadSyncJoin = true;
}
if (hadSyncJoin) {
for (Future<Void> joinFuture : pendingJoins) {
assertThat(joinFuture.isDone(), equalTo(true));
}
}
for (Future<Void> joinFuture : pendingJoins) {
joinFuture.get();
}
}
use of java.util.concurrent.Future in project elasticsearch by elastic.
the class NodeJoinControllerTests method testFailingJoinsWhenNotMaster.
public void testFailingJoinsWhenNotMaster() throws ExecutionException, InterruptedException {
// remove current master flag
DiscoveryNodes.Builder nodes = DiscoveryNodes.builder(clusterService.state().nodes()).masterNodeId(null);
setState(clusterService, ClusterState.builder(clusterService.state()).nodes(nodes));
int nodeId = 0;
try {
joinNode(newNode(nodeId++));
fail("failed to fail node join when not a master");
} catch (ExecutionException e) {
assertThat(e.getCause(), instanceOf(NotMasterException.class));
}
logger.debug("--> testing joins fail post accumulation");
ArrayList<Future<Void>> pendingJoins = new ArrayList<>();
nodeJoinController.startElectionContext();
for (int i = 1 + randomInt(5); i > 0; i--) {
DiscoveryNode node = newNode(nodeId++);
final Future<Void> future = joinNodeAsync(node);
pendingJoins.add(future);
assertThat(future.isDone(), equalTo(false));
}
nodeJoinController.stopElectionContext("test");
for (Future<Void> future : pendingJoins) {
try {
future.get();
fail("failed to fail accumulated node join when not a master");
} catch (ExecutionException e) {
assertThat(e.getCause(), instanceOf(NotMasterException.class));
}
}
}
use of java.util.concurrent.Future in project elasticsearch by elastic.
the class IndexLevelReplicationTests method testAppendWhileRecovering.
public void testAppendWhileRecovering() throws Exception {
try (ReplicationGroup shards = createGroup(0)) {
shards.startAll();
IndexShard replica = shards.addReplica();
CountDownLatch latch = new CountDownLatch(2);
int numDocs = randomIntBetween(100, 200);
// just append one to the translog so we can assert below
shards.appendDocs(1);
Thread thread = new Thread() {
@Override
public void run() {
try {
latch.countDown();
latch.await();
shards.appendDocs(numDocs - 1);
} catch (Exception e) {
throw new AssertionError(e);
}
}
};
thread.start();
Future<Void> future = shards.asyncRecoverReplica(replica, (indexShard, node) -> new RecoveryTarget(indexShard, node, recoveryListener, version -> {
}) {
@Override
public void cleanFiles(int totalTranslogOps, Store.MetadataSnapshot sourceMetaData) throws IOException {
super.cleanFiles(totalTranslogOps, sourceMetaData);
latch.countDown();
try {
latch.await();
} catch (InterruptedException e) {
throw new AssertionError(e);
}
}
});
future.get();
thread.join();
shards.assertAllEqual(numDocs);
Engine engine = IndexShardTests.getEngineFromShard(replica);
assertEquals("expected at no version lookups ", InternalEngineTests.getNumVersionLookups((InternalEngine) engine), 0);
for (IndexShard shard : shards) {
engine = IndexShardTests.getEngineFromShard(shard);
assertEquals(0, InternalEngineTests.getNumIndexVersionsLookups((InternalEngine) engine));
assertEquals(0, InternalEngineTests.getNumVersionLookups((InternalEngine) engine));
}
}
}
use of java.util.concurrent.Future in project elasticsearch by elastic.
the class RestClientMultipleHostsTests method createRestClient.
@Before
@SuppressWarnings("unchecked")
public void createRestClient() throws IOException {
CloseableHttpAsyncClient httpClient = mock(CloseableHttpAsyncClient.class);
when(httpClient.<HttpResponse>execute(any(HttpAsyncRequestProducer.class), any(HttpAsyncResponseConsumer.class), any(HttpClientContext.class), any(FutureCallback.class))).thenAnswer(new Answer<Future<HttpResponse>>() {
@Override
public Future<HttpResponse> answer(InvocationOnMock invocationOnMock) throws Throwable {
HttpAsyncRequestProducer requestProducer = (HttpAsyncRequestProducer) invocationOnMock.getArguments()[0];
HttpUriRequest request = (HttpUriRequest) requestProducer.generateRequest();
HttpHost httpHost = requestProducer.getTarget();
HttpClientContext context = (HttpClientContext) invocationOnMock.getArguments()[2];
assertThat(context.getAuthCache().get(httpHost), instanceOf(BasicScheme.class));
FutureCallback<HttpResponse> futureCallback = (FutureCallback<HttpResponse>) invocationOnMock.getArguments()[3];
//return the desired status code or exception depending on the path
if (request.getURI().getPath().equals("/soe")) {
futureCallback.failed(new SocketTimeoutException(httpHost.toString()));
} else if (request.getURI().getPath().equals("/coe")) {
futureCallback.failed(new ConnectTimeoutException(httpHost.toString()));
} else if (request.getURI().getPath().equals("/ioe")) {
futureCallback.failed(new IOException(httpHost.toString()));
} else {
int statusCode = Integer.parseInt(request.getURI().getPath().substring(1));
StatusLine statusLine = new BasicStatusLine(new ProtocolVersion("http", 1, 1), statusCode, "");
futureCallback.completed(new BasicHttpResponse(statusLine));
}
return null;
}
});
int numHosts = RandomNumbers.randomIntBetween(getRandom(), 2, 5);
httpHosts = new HttpHost[numHosts];
for (int i = 0; i < numHosts; i++) {
httpHosts[i] = new HttpHost("localhost", 9200 + i);
}
failureListener = new HostsTrackingFailureListener();
restClient = new RestClient(httpClient, 10000, new Header[0], httpHosts, null, failureListener);
}
use of java.util.concurrent.Future in project elasticsearch by elastic.
the class RestClientSingleHostTests method createRestClient.
@Before
@SuppressWarnings("unchecked")
public void createRestClient() throws IOException {
httpClient = mock(CloseableHttpAsyncClient.class);
when(httpClient.<HttpResponse>execute(any(HttpAsyncRequestProducer.class), any(HttpAsyncResponseConsumer.class), any(HttpClientContext.class), any(FutureCallback.class))).thenAnswer(new Answer<Future<HttpResponse>>() {
@Override
public Future<HttpResponse> answer(InvocationOnMock invocationOnMock) throws Throwable {
HttpAsyncRequestProducer requestProducer = (HttpAsyncRequestProducer) invocationOnMock.getArguments()[0];
HttpClientContext context = (HttpClientContext) invocationOnMock.getArguments()[2];
assertThat(context.getAuthCache().get(httpHost), instanceOf(BasicScheme.class));
FutureCallback<HttpResponse> futureCallback = (FutureCallback<HttpResponse>) invocationOnMock.getArguments()[3];
HttpUriRequest request = (HttpUriRequest) requestProducer.generateRequest();
//return the desired status code or exception depending on the path
if (request.getURI().getPath().equals("/soe")) {
futureCallback.failed(new SocketTimeoutException());
} else if (request.getURI().getPath().equals("/coe")) {
futureCallback.failed(new ConnectTimeoutException());
} else {
int statusCode = Integer.parseInt(request.getURI().getPath().substring(1));
StatusLine statusLine = new BasicStatusLine(new ProtocolVersion("http", 1, 1), statusCode, "");
HttpResponse httpResponse = new BasicHttpResponse(statusLine);
//return the same body that was sent
if (request instanceof HttpEntityEnclosingRequest) {
HttpEntity entity = ((HttpEntityEnclosingRequest) request).getEntity();
if (entity != null) {
assertTrue("the entity is not repeatable, cannot set it to the response directly", entity.isRepeatable());
httpResponse.setEntity(entity);
}
}
//return the same headers that were sent
httpResponse.setHeaders(request.getAllHeaders());
futureCallback.completed(httpResponse);
}
return null;
}
});
defaultHeaders = RestClientTestUtil.randomHeaders(getRandom(), "Header-default");
httpHost = new HttpHost("localhost", 9200);
failureListener = new HostsTrackingFailureListener();
restClient = new RestClient(httpClient, 10000, defaultHeaders, new HttpHost[] { httpHost }, null, failureListener);
}
Aggregations