Search in sources :

Example 71 with Future

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();
    }
}
Also used : DiscoveryNode(org.elasticsearch.cluster.node.DiscoveryNode) CopyOnWriteArrayList(java.util.concurrent.CopyOnWriteArrayList) ArrayList(java.util.ArrayList) Future(java.util.concurrent.Future) BaseFuture(org.elasticsearch.common.util.concurrent.BaseFuture)

Example 72 with Future

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));
        }
    }
}
Also used : DiscoveryNode(org.elasticsearch.cluster.node.DiscoveryNode) CopyOnWriteArrayList(java.util.concurrent.CopyOnWriteArrayList) ArrayList(java.util.ArrayList) Future(java.util.concurrent.Future) BaseFuture(org.elasticsearch.common.util.concurrent.BaseFuture) ExecutionException(java.util.concurrent.ExecutionException) DiscoveryNodes(org.elasticsearch.cluster.node.DiscoveryNodes)

Example 73 with Future

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));
        }
    }
}
Also used : ShardRouting(org.elasticsearch.cluster.routing.ShardRouting) InternalEngineTests(org.elasticsearch.index.engine.InternalEngineTests) XContentType(org.elasticsearch.common.xcontent.XContentType) IndexShard(org.elasticsearch.index.shard.IndexShard) RecoveryTarget(org.elasticsearch.indices.recovery.RecoveryTarget) ToXContent(org.elasticsearch.common.xcontent.ToXContent) IOException(java.io.IOException) IndexShardTests(org.elasticsearch.index.shard.IndexShardTests) DocWriteResponse(org.elasticsearch.action.DocWriteResponse) SegmentsStats(org.elasticsearch.index.engine.SegmentsStats) Engine(org.elasticsearch.index.engine.Engine) XContentHelper(org.elasticsearch.common.xcontent.XContentHelper) SeqNoStats(org.elasticsearch.index.seqno.SeqNoStats) CountDownLatch(java.util.concurrent.CountDownLatch) IndexRequest(org.elasticsearch.action.index.IndexRequest) Future(java.util.concurrent.Future) InternalEngine(org.elasticsearch.index.engine.InternalEngine) Store(org.elasticsearch.index.store.Store) Matchers.equalTo(org.hamcrest.Matchers.equalTo) IndexResponse(org.elasticsearch.action.index.IndexResponse) Collections(java.util.Collections) IndexShard(org.elasticsearch.index.shard.IndexShard) Store(org.elasticsearch.index.store.Store) RecoveryTarget(org.elasticsearch.indices.recovery.RecoveryTarget) IOException(java.io.IOException) CountDownLatch(java.util.concurrent.CountDownLatch) IOException(java.io.IOException) InternalEngine(org.elasticsearch.index.engine.InternalEngine) Engine(org.elasticsearch.index.engine.Engine) InternalEngine(org.elasticsearch.index.engine.InternalEngine)

Example 74 with Future

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);
}
Also used : HttpUriRequest(org.apache.http.client.methods.HttpUriRequest) BasicHttpResponse(org.apache.http.message.BasicHttpResponse) HttpResponse(org.apache.http.HttpResponse) HttpAsyncResponseConsumer(org.apache.http.nio.protocol.HttpAsyncResponseConsumer) HttpClientContext(org.apache.http.client.protocol.HttpClientContext) IOException(java.io.IOException) ProtocolVersion(org.apache.http.ProtocolVersion) BasicStatusLine(org.apache.http.message.BasicStatusLine) BasicStatusLine(org.apache.http.message.BasicStatusLine) StatusLine(org.apache.http.StatusLine) BasicHttpResponse(org.apache.http.message.BasicHttpResponse) SocketTimeoutException(java.net.SocketTimeoutException) Header(org.apache.http.Header) HttpAsyncRequestProducer(org.apache.http.nio.protocol.HttpAsyncRequestProducer) InvocationOnMock(org.mockito.invocation.InvocationOnMock) HttpHost(org.apache.http.HttpHost) CloseableHttpAsyncClient(org.apache.http.impl.nio.client.CloseableHttpAsyncClient) Future(java.util.concurrent.Future) FutureCallback(org.apache.http.concurrent.FutureCallback) ConnectTimeoutException(org.apache.http.conn.ConnectTimeoutException) Before(org.junit.Before)

Example 75 with Future

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);
}
Also used : HttpUriRequest(org.apache.http.client.methods.HttpUriRequest) HttpEntity(org.apache.http.HttpEntity) BasicHttpResponse(org.apache.http.message.BasicHttpResponse) HttpResponse(org.apache.http.HttpResponse) HttpAsyncResponseConsumer(org.apache.http.nio.protocol.HttpAsyncResponseConsumer) HttpClientContext(org.apache.http.client.protocol.HttpClientContext) ProtocolVersion(org.apache.http.ProtocolVersion) BasicStatusLine(org.apache.http.message.BasicStatusLine) StatusLine(org.apache.http.StatusLine) BasicStatusLine(org.apache.http.message.BasicStatusLine) BasicHttpResponse(org.apache.http.message.BasicHttpResponse) SocketTimeoutException(java.net.SocketTimeoutException) HttpAsyncRequestProducer(org.apache.http.nio.protocol.HttpAsyncRequestProducer) InvocationOnMock(org.mockito.invocation.InvocationOnMock) HttpHost(org.apache.http.HttpHost) HttpEntityEnclosingRequest(org.apache.http.HttpEntityEnclosingRequest) CloseableHttpAsyncClient(org.apache.http.impl.nio.client.CloseableHttpAsyncClient) Future(java.util.concurrent.Future) FutureCallback(org.apache.http.concurrent.FutureCallback) ConnectTimeoutException(org.apache.http.conn.ConnectTimeoutException) Before(org.junit.Before)

Aggregations

Future (java.util.concurrent.Future)1138 ArrayList (java.util.ArrayList)479 ExecutorService (java.util.concurrent.ExecutorService)445 Test (org.junit.Test)413 ExecutionException (java.util.concurrent.ExecutionException)264 Callable (java.util.concurrent.Callable)206 IOException (java.io.IOException)177 ParallelTest (com.hazelcast.test.annotation.ParallelTest)148 QuickTest (com.hazelcast.test.annotation.QuickTest)148 HashMap (java.util.HashMap)92 List (java.util.List)84 CountDownLatch (java.util.concurrent.CountDownLatch)71 LinkedList (java.util.LinkedList)67 TimeoutException (java.util.concurrent.TimeoutException)63 HashSet (java.util.HashSet)62 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)59 Map (java.util.Map)58 ICompletableFuture (com.hazelcast.core.ICompletableFuture)57 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)53 File (java.io.File)46