Search in sources :

Example 86 with ThreadPool

use of org.elasticsearch.threadpool.ThreadPool in project crate by crate.

the class BulkRetryCoordinatorTest method testScheduleRetryAfterRejectedExecution.

@Test
public void testScheduleRetryAfterRejectedExecution() throws Exception {
    ThreadPool threadPool = mock(ThreadPool.class);
    BulkRetryCoordinator coordinator = new BulkRetryCoordinator(threadPool);
    BulkRequestExecutor<ShardUpsertRequest> executor = (request, listener) -> {
        listener.onFailure(new EsRejectedExecutionException("Dummy execution rejected"));
    };
    coordinator.retry(shardRequest(), executor, new ActionListener<ShardResponse>() {

        @Override
        public void onResponse(ShardResponse shardResponse) {
        }

        @Override
        public void onFailure(Throwable e) {
        }
    });
    verify(threadPool).schedule(eq(TimeValue.timeValueMillis(0)), eq(ThreadPool.Names.SAME), any(Runnable.class));
}
Also used : ShardId(org.elasticsearch.index.shard.ShardId) Reference(io.crate.metadata.Reference) Test(org.junit.Test) TableIdent(io.crate.metadata.TableIdent) UUID(java.util.UUID) SettableFuture(com.google.common.util.concurrent.SettableFuture) CrateUnitTest(io.crate.test.integration.CrateUnitTest) Executors(java.util.concurrent.Executors) TimeUnit(java.util.concurrent.TimeUnit) Matchers.any(org.mockito.Matchers.any) CountDownLatch(java.util.concurrent.CountDownLatch) Mockito(org.mockito.Mockito) ShardUpsertRequest(io.crate.executor.transport.ShardUpsertRequest) RowGranularity(io.crate.metadata.RowGranularity) EsRejectedExecutionException(org.elasticsearch.common.util.concurrent.EsRejectedExecutionException) DataTypes(io.crate.types.DataTypes) TimeValue(org.elasticsearch.common.unit.TimeValue) ShardResponse(io.crate.executor.transport.ShardResponse) ThreadPool(org.elasticsearch.threadpool.ThreadPool) ReferenceIdent(io.crate.metadata.ReferenceIdent) EsExecutors.daemonThreadFactory(org.elasticsearch.common.util.concurrent.EsExecutors.daemonThreadFactory) ActionListener(org.elasticsearch.action.ActionListener) ExecutorService(java.util.concurrent.ExecutorService) Before(org.junit.Before) ShardResponse(io.crate.executor.transport.ShardResponse) ShardUpsertRequest(io.crate.executor.transport.ShardUpsertRequest) ThreadPool(org.elasticsearch.threadpool.ThreadPool) EsRejectedExecutionException(org.elasticsearch.common.util.concurrent.EsRejectedExecutionException) Test(org.junit.Test) CrateUnitTest(io.crate.test.integration.CrateUnitTest)

Example 87 with ThreadPool

use of org.elasticsearch.threadpool.ThreadPool in project crate by crate.

the class BulkRetryCoordinatorTest method testNoPendingOperationsOnFailedExecution.

@Test
public void testNoPendingOperationsOnFailedExecution() throws Exception {
    ThreadPool threadPool = mock(ThreadPool.class);
    BulkRetryCoordinator coordinator = new BulkRetryCoordinator(threadPool);
    BulkRequestExecutor<ShardUpsertRequest> executor = (request, listener) -> {
        listener.onFailure(new InterruptedException("Dummy execution failed"));
    };
    final SettableFuture<ShardResponse> future = SettableFuture.create();
    coordinator.retry(shardRequest(), executor, new ActionListener<ShardResponse>() {

        @Override
        public void onResponse(ShardResponse shardResponse) {
        }

        @Override
        public void onFailure(Throwable e) {
            future.set(null);
        }
    });
    ShardResponse response = future.get();
    assertNull(response);
    assertEquals(0, coordinator.numPendingOperations());
}
Also used : ShardId(org.elasticsearch.index.shard.ShardId) Reference(io.crate.metadata.Reference) Test(org.junit.Test) TableIdent(io.crate.metadata.TableIdent) UUID(java.util.UUID) SettableFuture(com.google.common.util.concurrent.SettableFuture) CrateUnitTest(io.crate.test.integration.CrateUnitTest) Executors(java.util.concurrent.Executors) TimeUnit(java.util.concurrent.TimeUnit) Matchers.any(org.mockito.Matchers.any) CountDownLatch(java.util.concurrent.CountDownLatch) Mockito(org.mockito.Mockito) ShardUpsertRequest(io.crate.executor.transport.ShardUpsertRequest) RowGranularity(io.crate.metadata.RowGranularity) EsRejectedExecutionException(org.elasticsearch.common.util.concurrent.EsRejectedExecutionException) DataTypes(io.crate.types.DataTypes) TimeValue(org.elasticsearch.common.unit.TimeValue) ShardResponse(io.crate.executor.transport.ShardResponse) ThreadPool(org.elasticsearch.threadpool.ThreadPool) ReferenceIdent(io.crate.metadata.ReferenceIdent) EsExecutors.daemonThreadFactory(org.elasticsearch.common.util.concurrent.EsExecutors.daemonThreadFactory) ActionListener(org.elasticsearch.action.ActionListener) ExecutorService(java.util.concurrent.ExecutorService) Before(org.junit.Before) ShardResponse(io.crate.executor.transport.ShardResponse) ShardUpsertRequest(io.crate.executor.transport.ShardUpsertRequest) ThreadPool(org.elasticsearch.threadpool.ThreadPool) Test(org.junit.Test) CrateUnitTest(io.crate.test.integration.CrateUnitTest)

Example 88 with ThreadPool

use of org.elasticsearch.threadpool.ThreadPool in project crate by crate.

the class BulkRetryCoordinatorTest method testParallelSuccessfulExecution.

@Test
public void testParallelSuccessfulExecution() throws Exception {
    ThreadPool threadPool = mock(ThreadPool.class);
    final BulkRetryCoordinator coordinator = new BulkRetryCoordinator(threadPool);
    final BulkRequestExecutor<ShardUpsertRequest> executor = (request, listener) -> {
        listener.onResponse(new ShardResponse());
    };
    final CountDownLatch latch = new CountDownLatch(1000);
    ExecutorService executorService = Executors.newFixedThreadPool(10, daemonThreadFactory("DummyThreadPool"));
    for (int i = 0; i < 1000; i++) {
        executorService.submit(new Runnable() {

            @Override
            public void run() {
                coordinator.retry(shardRequest(), executor, new ActionListener<ShardResponse>() {

                    @Override
                    public void onResponse(ShardResponse shardResponse) {
                        latch.countDown();
                    }

                    @Override
                    public void onFailure(Throwable e) {
                    }
                });
            }
        });
    }
    latch.await();
    assertEquals(0, coordinator.numPendingOperations());
    executorService.awaitTermination(5, TimeUnit.SECONDS);
    executorService.shutdown();
}
Also used : ShardId(org.elasticsearch.index.shard.ShardId) Reference(io.crate.metadata.Reference) Test(org.junit.Test) TableIdent(io.crate.metadata.TableIdent) UUID(java.util.UUID) SettableFuture(com.google.common.util.concurrent.SettableFuture) CrateUnitTest(io.crate.test.integration.CrateUnitTest) Executors(java.util.concurrent.Executors) TimeUnit(java.util.concurrent.TimeUnit) Matchers.any(org.mockito.Matchers.any) CountDownLatch(java.util.concurrent.CountDownLatch) Mockito(org.mockito.Mockito) ShardUpsertRequest(io.crate.executor.transport.ShardUpsertRequest) RowGranularity(io.crate.metadata.RowGranularity) EsRejectedExecutionException(org.elasticsearch.common.util.concurrent.EsRejectedExecutionException) DataTypes(io.crate.types.DataTypes) TimeValue(org.elasticsearch.common.unit.TimeValue) ShardResponse(io.crate.executor.transport.ShardResponse) ThreadPool(org.elasticsearch.threadpool.ThreadPool) ReferenceIdent(io.crate.metadata.ReferenceIdent) EsExecutors.daemonThreadFactory(org.elasticsearch.common.util.concurrent.EsExecutors.daemonThreadFactory) ActionListener(org.elasticsearch.action.ActionListener) ExecutorService(java.util.concurrent.ExecutorService) Before(org.junit.Before) ShardUpsertRequest(io.crate.executor.transport.ShardUpsertRequest) ThreadPool(org.elasticsearch.threadpool.ThreadPool) CountDownLatch(java.util.concurrent.CountDownLatch) ShardResponse(io.crate.executor.transport.ShardResponse) ActionListener(org.elasticsearch.action.ActionListener) ExecutorService(java.util.concurrent.ExecutorService) Test(org.junit.Test) CrateUnitTest(io.crate.test.integration.CrateUnitTest)

Example 89 with ThreadPool

use of org.elasticsearch.threadpool.ThreadPool in project elasticsearch-indexing-proxy by codelibs.

the class IndexingProxyService method launchRequestSender.

private void launchRequestSender(final String index, final long filePosition, final long version, final ActionListener<Map<String, Object>> listener) {
    if (logger.isDebugEnabled()) {
        logger.debug("Launching RequestSender(" + index + ")");
    }
    final Map<String, Object> source = new HashMap<>();
    source.put(IndexingProxyPlugin.NODE_NAME, nodeName());
    source.put(IndexingProxyPlugin.FILE_POSITION, filePosition);
    source.put(IndexingProxyPlugin.TIMESTAMP, new Date());
    source.put(DOC_TYPE, "index");
    final IndexRequestBuilder builder = client.prepareIndex(IndexingProxyPlugin.INDEX_NAME, IndexingProxyPlugin.TYPE_NAME, index).setSource(source).setRefreshPolicy(RefreshPolicy.WAIT_UNTIL);
    if (version > 0) {
        builder.setVersion(version);
    } else {
        builder.setCreate(true);
    }
    builder.execute(wrap(res -> {
        if (res.getResult() == Result.CREATED || res.getResult() == Result.UPDATED) {
            final RequestSender sender = new RequestSender(settings, client, threadPool, namedWriteableRegistry, nodeName(), dataPath, index, dataFileFormat, docSenderMap, logger);
            final RequestSender oldSender = docSenderMap.put(index, sender);
            if (oldSender != null) {
                oldSender.terminate();
            }
            threadPool.schedule(TimeValue.ZERO, Names.GENERIC, sender);
            listener.onResponse(source);
        } else {
            listener.onFailure(new ElasticsearchException("Failed to update .idxproxy index: " + res));
        }
    }, listener::onFailure));
}
Also used : IndexRequestBuilder(org.elasticsearch.action.index.IndexRequestBuilder) ElasticsearchException(org.elasticsearch.ElasticsearchException) IndexingProxyPlugin(org.codelibs.elasticsearch.idxproxy.IndexingProxyPlugin) Arrays(java.util.Arrays) Date(java.util.Date) WriteRequestHandler(org.codelibs.elasticsearch.idxproxy.action.WriteRequestHandler) Environment(org.elasticsearch.env.Environment) FileTime(java.nio.file.attribute.FileTime) XContentBuilder(org.elasticsearch.common.xcontent.XContentBuilder) JsonXContent(org.elasticsearch.common.xcontent.json.JsonXContent) QueryBuilders(org.elasticsearch.index.query.QueryBuilders) DeleteRequest(org.elasticsearch.action.delete.DeleteRequest) IndexRequest(org.elasticsearch.action.index.IndexRequest) Settings(org.elasticsearch.common.settings.Settings) UpdateResponse(org.elasticsearch.action.update.UpdateResponse) LifecycleListener(org.elasticsearch.common.component.LifecycleListener) IndexNotFoundException(org.elasticsearch.index.IndexNotFoundException) Map(java.util.Map) DeleteByQueryRequest(org.elasticsearch.index.reindex.DeleteByQueryRequest) Streams(org.elasticsearch.common.io.Streams) ThreadPool(org.elasticsearch.threadpool.ThreadPool) Path(java.nio.file.Path) RefreshPolicy(org.elasticsearch.action.support.WriteRequest.RefreshPolicy) CreateRequest(org.codelibs.elasticsearch.idxproxy.action.CreateRequest) WriteResponse(org.codelibs.elasticsearch.idxproxy.action.WriteResponse) SearchHit(org.elasticsearch.search.SearchHit) ActionRequest(org.elasticsearch.action.ActionRequest) ActionFilter(org.elasticsearch.action.support.ActionFilter) ActionFilters(org.elasticsearch.action.support.ActionFilters) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) Set(java.util.Set) ToXContent(org.elasticsearch.common.xcontent.ToXContent) Reader(java.io.Reader) PrivilegedAction(java.security.PrivilegedAction) Collectors(java.util.stream.Collectors) StandardCharsets(java.nio.charset.StandardCharsets) List(java.util.List) IndexingProxyStreamInput(org.codelibs.elasticsearch.idxproxy.stream.IndexingProxyStreamInput) IndexRequestBuilder(org.elasticsearch.action.index.IndexRequestBuilder) Strings(org.apache.logging.log4j.util.Strings) TransportResponseHandler(org.elasticsearch.transport.TransportResponseHandler) AccessController(java.security.AccessController) TransportException(org.elasticsearch.transport.TransportException) ProxyActionFilter(org.codelibs.elasticsearch.idxproxy.action.ProxyActionFilter) XContentFactory(org.elasticsearch.common.xcontent.XContentFactory) CreateRequestHandler(org.codelibs.elasticsearch.idxproxy.action.CreateRequestHandler) ClusterService(org.elasticsearch.cluster.service.ClusterService) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) UpdateRequestBuilder(org.elasticsearch.action.update.UpdateRequestBuilder) ActionListener.wrap(org.elasticsearch.action.ActionListener.wrap) HashMap(java.util.HashMap) IndexingProxyStreamOutput(org.codelibs.elasticsearch.idxproxy.stream.IndexingProxyStreamOutput) Names(org.elasticsearch.threadpool.ThreadPool.Names) StandardCopyOption(java.nio.file.StandardCopyOption) ArrayList(java.util.ArrayList) Inject(org.elasticsearch.common.inject.Inject) DiscoveryNode(org.elasticsearch.cluster.node.DiscoveryNode) NamedWriteableRegistry(org.elasticsearch.common.io.stream.NamedWriteableRegistry) RequestUtils(org.codelibs.elasticsearch.idxproxy.util.RequestUtils) ThreadLocalRandom(java.util.concurrent.ThreadLocalRandom) TimeValue(org.elasticsearch.common.unit.TimeValue) BiConsumer(java.util.function.BiConsumer) Result(org.elasticsearch.action.DocWriteResponse.Result) TransportService(org.elasticsearch.transport.TransportService) UpdateByQueryRequest(org.elasticsearch.index.reindex.UpdateByQueryRequest) CreateResponse(org.codelibs.elasticsearch.idxproxy.action.CreateResponse) RequestSender(org.codelibs.elasticsearch.idxproxy.sender.RequestSender) FileAccessUtils(org.codelibs.elasticsearch.idxproxy.util.FileAccessUtils) PluginComponent(org.codelibs.elasticsearch.idxproxy.IndexingProxyPlugin.PluginComponent) Iterator(java.util.Iterator) Files(java.nio.file.Files) ActionResponse(org.elasticsearch.action.ActionResponse) Client(org.elasticsearch.client.Client) IOException(java.io.IOException) PingResponse(org.codelibs.elasticsearch.idxproxy.action.PingResponse) ClusterHealthResponse(org.elasticsearch.action.admin.cluster.health.ClusterHealthResponse) UpdateRequest(org.elasticsearch.action.update.UpdateRequest) InputStreamReader(java.io.InputStreamReader) AbstractLifecycleComponent(org.elasticsearch.common.component.AbstractLifecycleComponent) PingRequest(org.codelibs.elasticsearch.idxproxy.action.PingRequest) Consumer(java.util.function.Consumer) LocalNodeMasterListener(org.elasticsearch.cluster.LocalNodeMasterListener) Paths(java.nio.file.Paths) WriteRequest(org.codelibs.elasticsearch.idxproxy.action.WriteRequest) PingRequestHandler(org.codelibs.elasticsearch.idxproxy.action.PingRequestHandler) Collections(java.util.Collections) BulkRequest(org.elasticsearch.action.bulk.BulkRequest) ActionListener(org.elasticsearch.action.ActionListener) RequestSender(org.codelibs.elasticsearch.idxproxy.sender.RequestSender) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) HashMap(java.util.HashMap) ElasticsearchException(org.elasticsearch.ElasticsearchException) Date(java.util.Date)

Example 90 with ThreadPool

use of org.elasticsearch.threadpool.ThreadPool in project crate by crate.

the class Node method awaitClose.

/**
 * Wait for this node to be effectively closed.
 */
// synchronized to prevent running concurrently with close()
public synchronized boolean awaitClose(long timeout, TimeUnit timeUnit) throws InterruptedException {
    if (lifecycle.closed() == false) {
        // closed yet.
        throw new IllegalStateException("Call close() first");
    }
    ThreadPool threadPool = injector.getInstance(ThreadPool.class);
    final boolean terminated = ThreadPool.terminate(threadPool, timeout, timeUnit);
    if (terminated) {
        // that run on shards run in the threadpool, indices should be effectively closed by now.
        if (nodeService.awaitClose(0, TimeUnit.MILLISECONDS) == false) {
            throw new IllegalStateException("Some shards are still open after the threadpool terminated. " + "Something is leaking index readers or store references.");
        }
    }
    return terminated;
}
Also used : ThreadPool(org.elasticsearch.threadpool.ThreadPool)

Aggregations

ThreadPool (org.elasticsearch.threadpool.ThreadPool)109 Settings (org.elasticsearch.common.settings.Settings)65 TestThreadPool (org.elasticsearch.threadpool.TestThreadPool)61 TimeUnit (java.util.concurrent.TimeUnit)39 IOException (java.io.IOException)36 DiscoveryNode (org.elasticsearch.cluster.node.DiscoveryNode)36 Before (org.junit.Before)35 ESTestCase (org.elasticsearch.test.ESTestCase)34 TransportService (org.elasticsearch.transport.TransportService)33 ActionListener (org.elasticsearch.action.ActionListener)32 Version (org.elasticsearch.Version)31 ClusterState (org.elasticsearch.cluster.ClusterState)31 Collections (java.util.Collections)29 List (java.util.List)28 CountDownLatch (java.util.concurrent.CountDownLatch)28 ClusterService (org.elasticsearch.cluster.service.ClusterService)28 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)26 Set (java.util.Set)25 ArrayList (java.util.ArrayList)24 ShardId (org.elasticsearch.index.shard.ShardId)24