org.elasticsearch.common.util.concurrent.AbstractRunnable Java Examples

The following examples show how to use org.elasticsearch.common.util.concurrent.AbstractRunnable. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: test.java    From vscode-extension with MIT License 6 votes vote down vote up
@Override
protected void handleMergeException(final Directory dir, final Throwable exc) {
    engineConfig.getThreadPool().generic().execute(new AbstractRunnable() {
        @Override
        public void onFailure(Exception e) {
            logger.debug("merge failure action rejected", e);
        }

        @Override
        protected void doRun() throws Exception {
            /*
             * We do this on another thread rather than the merge thread that we are initially called on so that we have complete
             * confidence that the call stack does not contain catch statements that would cause the error that might be thrown
             * here from being caught and never reaching the uncaught exception handler.
             */
            failEngine("merge failed", new MergePolicy.MergeException(exc, dir));
        }
    });
}
 
Example #2
Source File: MockTcpTransport.java    From crate with Apache License 2.0 6 votes vote down vote up
void loopRead(Executor executor) {
    executor.execute(new AbstractRunnable() {
        @Override
        public void onFailure(Exception e) {
            if (isOpen.get()) {
                try {
                    onException(MockChannel.this, e);
                } catch (Exception ex) {
                    logger.warn("failed on handling exception", ex);
                    IOUtils.closeWhileHandlingException(MockChannel.this); // pure paranoia
                }
            }
        }

        @Override
        protected void doRun() throws Exception {
            StreamInput input = new InputStreamStreamInput(new BufferedInputStream(activeChannel.getInputStream()));
            // There is a (slim) chance that we get interrupted right after a loop iteration, so check explicitly
            while (isOpen.get() && !Thread.currentThread().isInterrupted()) {
                cancellableThreads.executeIO(() -> readMessage(MockChannel.this, input));
            }
        }
    });
}
 
Example #3
Source File: DelayedAllocationService.java    From crate with Apache License 2.0 6 votes vote down vote up
public void schedule() {
    cancellable = threadPool.schedule(new AbstractRunnable() {
        @Override
        protected void doRun() throws Exception {
            if (cancelScheduling.get()) {
                return;
            }
            clusterService.submitStateUpdateTask(CLUSTER_UPDATE_TASK_SOURCE, DelayedRerouteTask.this);
        }

        @Override
        public void onFailure(Exception e) {
            LOGGER.warn("failed to submit schedule/execute reroute post unassigned shard", e);
            removeIfSameTask(DelayedRerouteTask.this);
        }
    }, nextDelay, ThreadPool.Names.SAME);
}
 
Example #4
Source File: IndexShardOperationPermits.java    From crate with Apache License 2.0 6 votes vote down vote up
/**
 * Immediately delays operations and on another thread waits for in-flight operations to finish and then executes {@code onBlocked}
 * under the guarantee that no new operations are started. Delayed operations are run after {@code onBlocked} has executed. After
 * operations are delayed and the blocking is forked to another thread, returns to the caller. If a failure occurs while blocking
 * operations or executing {@code onBlocked} then the {@code onFailure} handler will be invoked.
 *
 * @param timeout   the maximum time to wait for the in-flight operations block
 * @param timeUnit  the time unit of the {@code timeout} argument
 * @param onBlocked the action to run once the block has been acquired
 * @param onFailure the action to run if a failure occurs while blocking operations
 * @param <E>       the type of checked exception thrown by {@code onBlocked} (not thrown on the calling thread)
 */
<E extends Exception> void asyncBlockOperations(
        final long timeout, final TimeUnit timeUnit, final CheckedRunnable<E> onBlocked, final Consumer<Exception> onFailure) {
    delayOperations();
    threadPool.executor(ThreadPool.Names.GENERIC).execute(new AbstractRunnable() {
        @Override
        public void onFailure(final Exception e) {
            onFailure.accept(e);
        }

        @Override
        protected void doRun() throws Exception {
            doBlockOperations(timeout, timeUnit, onBlocked);
        }

        @Override
        public void onAfter() {
            releaseDelayedOperations();
        }
    });
}
 
Example #5
Source File: InternalEngine.java    From crate with Apache License 2.0 6 votes vote down vote up
@Override
protected void handleMergeException(final Directory dir, final Throwable exc) {
    engineConfig.getThreadPool().generic().execute(new AbstractRunnable() {
        @Override
        public void onFailure(Exception e) {
            logger.debug("merge failure action rejected", e);
        }

        @Override
        protected void doRun() throws Exception {
            /*
             * We do this on another thread rather than the merge thread that we are initially called on so that we have complete
             * confidence that the call stack does not contain catch statements that would cause the error that might be thrown
             * here from being caught and never reaching the uncaught exception handler.
             */
            failEngine("merge failed", new MergePolicy.MergeException(exc, dir));
        }
    });
}
 
Example #6
Source File: TransportRenderSearchTemplateAction.java    From Elasticsearch with Apache License 2.0 6 votes vote down vote up
@Override
protected void doExecute(final RenderSearchTemplateRequest request, final ActionListener<RenderSearchTemplateResponse> listener) {
    threadPool.generic().execute(new AbstractRunnable() {

        @Override
        public void onFailure(Throwable t) {
            listener.onFailure(t);
        }

        @Override
        protected void doRun() throws Exception {
            ExecutableScript executable = scriptService.executable(request.template(), ScriptContext.Standard.SEARCH, request, Collections.<String, String>emptyMap());
            BytesReference processedTemplate = (BytesReference) executable.run();
            RenderSearchTemplateResponse response = new RenderSearchTemplateResponse();
            response.source(processedTemplate);
            listener.onResponse(response);
        }
    });
}
 
Example #7
Source File: NodeIndexDeletedAction.java    From Elasticsearch with Apache License 2.0 6 votes vote down vote up
public void nodeIndexDeleted(final ClusterState clusterState, final String index, final Settings indexSettings, final String nodeId) {
    final DiscoveryNodes nodes = clusterState.nodes();
    transportService.sendRequest(clusterState.nodes().masterNode(),
            INDEX_DELETED_ACTION_NAME, new NodeIndexDeletedMessage(index, nodeId), EmptyTransportResponseHandler.INSTANCE_SAME);
    if (nodes.localNode().isDataNode() == false) {
        logger.trace("[{}] not acking store deletion (not a data node)", index);
        return;
    }
    threadPool.generic().execute(new AbstractRunnable() {
        @Override
        public void onFailure(Throwable t) {
            logger.warn("[{}] failed to ack index store deleted for index", t, index);
        }

        @Override
        protected void doRun() throws Exception {
            lockIndexAndAck(index, nodes, nodeId, clusterState, indexSettings);
        }
    });
}
 
Example #8
Source File: InternalEngine.java    From Elasticsearch with Apache License 2.0 6 votes vote down vote up
@Override
protected void handleMergeException(final Directory dir, final Throwable exc) {
    logger.error("failed to merge", exc);
    if (config().getMergeSchedulerConfig().isNotifyOnMergeFailure()) {
        engineConfig.getThreadPool().generic().execute(new AbstractRunnable() {
            @Override
            public void onFailure(Throwable t) {
                logger.debug("merge failure action rejected", t);
            }

            @Override
            protected void doRun() throws Exception {
                MergePolicy.MergeException e = new MergePolicy.MergeException(exc, dir);
                failEngine("merge failed", e);
            }
        });
    }
}
 
Example #9
Source File: DLBasedEngine.java    From Elasticsearch with Apache License 2.0 6 votes vote down vote up
@Override
protected void handleMergeException(final Directory dir, final Throwable exc) {
    logger.error("failed to merge", exc);
    if (config().getMergeSchedulerConfig().isNotifyOnMergeFailure()) {
        engineConfig.getThreadPool().generic().execute(new AbstractRunnable() {
            @Override
            public void onFailure(Throwable t) {
                logger.debug("merge failure action rejected", t);
            }

            @Override
            protected void doRun() throws Exception {
                MergePolicy.MergeException e = new MergePolicy.MergeException(exc, dir);
                failEngine("merge failed", e);
            }
        });
    }
}
 
Example #10
Source File: ThreadedActionListener.java    From Elasticsearch with Apache License 2.0 5 votes vote down vote up
@Override
public void onResponse(final Response response) {
    threadPool.executor(executor).execute(new AbstractRunnable() {
        @Override
        protected void doRun() throws Exception {
            listener.onResponse(response);
        }

        @Override
        public void onFailure(Throwable t) {
            listener.onFailure(t);
        }
    });
}
 
Example #11
Source File: ThreadedActionListener.java    From Elasticsearch with Apache License 2.0 5 votes vote down vote up
@Override
public void onFailure(final Throwable e) {
    threadPool.executor(executor).execute(new AbstractRunnable() {
        @Override
        protected void doRun() throws Exception {
            listener.onFailure(e);
        }

        @Override
        public void onFailure(Throwable t) {
            logger.warn("failed to execute failure callback on [{}], failure [{}]", t, listener, e);
        }
    });
}
 
Example #12
Source File: IndexingMemoryController.java    From crate with Apache License 2.0 5 votes vote down vote up
/** ask this shard to refresh, in the background, to free up heap */
protected void writeIndexingBufferAsync(IndexShard shard) {
    threadPool.executor(ThreadPool.Names.REFRESH).execute(new AbstractRunnable() {
        @Override
        public void doRun() {
            shard.writeIndexingBuffer();
        }

        @Override
        public void onFailure(Exception e) {
            LOGGER.warn(() -> new ParameterizedMessage("failed to write indexing buffer for shard [{}]; ignoring", shard.shardId()), e);
        }
    });
}
 
Example #13
Source File: SeedHostsResolver.java    From crate with Apache License 2.0 5 votes vote down vote up
@Override
public void resolveConfiguredHosts(Consumer<List<TransportAddress>> consumer) {
    if (lifecycle.started() == false) {
        LOGGER.debug("resolveConfiguredHosts: lifecycle is {}, not proceeding", lifecycle);
        return;
    }

    if (resolveInProgress.compareAndSet(false, true)) {
        transportService.getThreadPool().generic().execute(new AbstractRunnable() {
            @Override
            public void onFailure(Exception e) {
                LOGGER.debug("failure when resolving unicast hosts list", e);
            }

            @Override
            protected void doRun() {
                if (lifecycle.started() == false) {
                    LOGGER.debug("resolveConfiguredHosts.doRun: lifecycle is {}, not proceeding", lifecycle);
                    return;
                }

                List<TransportAddress> providedAddresses = hostsProvider.getSeedAddresses(SeedHostsResolver.this);

                consumer.accept(providedAddresses);
            }

            @Override
            public void onAfter() {
                resolveInProgress.set(false);
            }

            @Override
            public String toString() {
                return "SeedHostsResolver resolving unicast hosts list";
            }
        });
    }
}
 
Example #14
Source File: GatewayService.java    From Elasticsearch with Apache License 2.0 5 votes vote down vote up
private void performStateRecovery(boolean enforceRecoverAfterTime, String reason) {
    final Gateway.GatewayStateRecoveredListener recoveryListener = new GatewayRecoveryListener();

    if (enforceRecoverAfterTime && recoverAfterTime != null) {
        if (scheduledRecovery.compareAndSet(false, true)) {
            logger.info("delaying initial state recovery for [{}]. {}", recoverAfterTime, reason);
            threadPool.schedule(recoverAfterTime, ThreadPool.Names.GENERIC, new Runnable() {
                @Override
                public void run() {
                    if (recovered.compareAndSet(false, true)) {
                        logger.info("recover_after_time [{}] elapsed. performing state recovery...", recoverAfterTime);
                        gateway.performStateRecovery(recoveryListener);
                    }
                }
            });
        }
    } else {
        if (recovered.compareAndSet(false, true)) {
            threadPool.generic().execute(new AbstractRunnable() {
                @Override
                public void onFailure(Throwable t) {
                    logger.warn("Recovery failed", t);
                    // we reset `recovered` in the listener don't reset it here otherwise there might be a race
                    // that resets it to false while a new recover is already running?
                    recoveryListener.onFailure("state recovery failed: " + t.getMessage());
                }

                @Override
                protected void doRun() throws Exception {
                    gateway.performStateRecovery(recoveryListener);
                }
            });
        }
    }
}
 
Example #15
Source File: ClusterFormationFailureHelper.java    From crate with Apache License 2.0 5 votes vote down vote up
void scheduleNextWarning() {
    threadPool.scheduleUnlessShuttingDown(clusterFormationWarningTimeout, Names.GENERIC, new AbstractRunnable() {
        @Override
        public void onFailure(Exception e) {
            LOGGER.debug("unexpected exception scheduling cluster formation warning", e);
        }

        @Override
        protected void doRun() {
            if (isActive()) {
                logLastFailedJoinAttempt.run();
                LOGGER.warn(clusterFormationStateSupplier.get().getDescription());
            }
        }

        @Override
        public void onAfter() {
            if (isActive()) {
                scheduleNextWarning();
            }
        }

        @Override
        public String toString() {
            return "emit warning if cluster not formed";
        }
    });
}
 
Example #16
Source File: test.java    From vscode-extension with MIT License 5 votes vote down vote up
@Override
public synchronized void afterMerge(OnGoingMerge merge) {
    int maxNumMerges = mergeScheduler.getMaxMergeCount();
    if (numMergesInFlight.decrementAndGet() < maxNumMerges) {
        if (isThrottling.getAndSet(false)) {
            logger.info("stop throttling indexing: numMergesInFlight={}, maxNumMerges={}",
                numMergesInFlight, maxNumMerges);
            deactivateThrottling();
        }
    }
    if (indexWriter.hasPendingMerges() == false &&
            System.nanoTime() - lastWriteNanos >= engineConfig.getFlushMergesAfter().nanos()) {
        // NEVER do this on a merge thread since we acquire some locks blocking here and if we concurrently rollback the writer
        // we deadlock on engine#close for instance.
        engineConfig.getThreadPool().executor(ThreadPool.Names.FLUSH).execute(new AbstractRunnable() {
            @Override
            public void onFailure(Exception e) {
                if (isClosed.get() == false) {
                    logger.warn("failed to flush after merge has finished");
                }
            }

            @Override
            protected void doRun() throws Exception {
                // if we have no pending merges and we are supposed to flush once merges have finished
                // we try to renew a sync commit which is the case when we are having a big merge after we
                // are inactive. If that didn't work we go and do a real flush which is ok since it only doesn't work
                // if we either have records in the translog or if we don't have a sync ID at all...
                // maybe even more important, we flush after all merges finish and we are inactive indexing-wise to
                // free up transient disk usage of the (presumably biggish) segments that were just merged
                if (tryRenewSyncCommit() == false) {
                    flush();
                }
            }
        });

    }
}
 
Example #17
Source File: InternalEngine.java    From crate with Apache License 2.0 4 votes vote down vote up
@Override
public synchronized void afterMerge(OnGoingMerge merge) {
    int maxNumMerges = mergeScheduler.getMaxMergeCount();
    if (numMergesInFlight.decrementAndGet() < maxNumMerges) {
        if (isThrottling.getAndSet(false)) {
            logger.info("stop throttling indexing: numMergesInFlight={}, maxNumMerges={}",
                        numMergesInFlight, maxNumMerges);
            deactivateThrottling();
        }
    }
    if (indexWriter.hasPendingMerges() == false &&
        System.nanoTime() - lastWriteNanos >= engineConfig.getFlushMergesAfter().nanos()) {
        // NEVER do this on a merge thread since we acquire some locks blocking here and if we concurrently rollback the writer
        // we deadlock on engine#close for instance.
        engineConfig.getThreadPool().executor(ThreadPool.Names.FLUSH).execute(new AbstractRunnable() {
            @Override
            public void onFailure(Exception e) {
                if (isClosed.get() == false) {
                    logger.warn("failed to flush after merge has finished");
                }
            }

            @Override
            protected void doRun() {
                // if we have no pending merges and we are supposed to flush once merges have finished
                // we try to renew a sync commit which is the case when we are having a big merge after we
                // are inactive. If that didn't work we go and do a real flush which is ok since it only doesn't work
                // if we either have records in the translog or if we don't have a sync ID at all...
                // maybe even more important, we flush after all merges finish and we are inactive indexing-wise to
                // free up transient disk usage of the (presumably biggish) segments that were just merged
                if (tryRenewSyncCommit() == false) {
                    flush();
                }
            }
        });
    } else if (merge.getTotalBytesSize() >= engineConfig.getIndexSettings().getFlushAfterMergeThresholdSize().getBytes()) {
        // we hit a significant merge which would allow us to free up memory if we'd commit it hence on the next change
        // we should execute a flush on the next operation if that's a flush after inactive or indexing a document.
        // we could fork a thread and do it right away but we try to minimize forking and piggyback on outside events.
        shouldPeriodicallyFlushAfterBigMerge.set(true);
    }
}
 
Example #18
Source File: LocalCheckpointTrackerTests.java    From crate with Apache License 2.0 4 votes vote down vote up
public void testConcurrentPrimary() throws InterruptedException {
    Thread[] threads = new Thread[randomIntBetween(2, 5)];
    final int opsPerThread = randomIntBetween(10, 20);
    final int maxOps = opsPerThread * threads.length;
    final long unFinishedSeq = randomIntBetween(0, maxOps - 2); // make sure we always index the last seqNo to simplify maxSeq checks
    logger.info("--> will run [{}] threads, maxOps [{}], unfinished seq no [{}]", threads.length, maxOps, unFinishedSeq);
    final CyclicBarrier barrier = new CyclicBarrier(threads.length);
    for (int t = 0; t < threads.length; t++) {
        final int threadId = t;
        threads[t] = new Thread(new AbstractRunnable() {
            @Override
            public void onFailure(Exception e) {
                throw new ElasticsearchException("failure in background thread", e);
            }

            @Override
            protected void doRun() throws Exception {
                barrier.await();
                for (int i = 0; i < opsPerThread; i++) {
                    long seqNo = tracker.generateSeqNo();
                    logger.info("[t{}] started   [{}]", threadId, seqNo);
                    if (seqNo != unFinishedSeq) {
                        tracker.markSeqNoAsProcessed(seqNo);
                        logger.info("[t{}] completed [{}]", threadId, seqNo);
                    }
                }
            }
        }, "testConcurrentPrimary_" + threadId);
        threads[t].start();
    }
    for (Thread thread : threads) {
        thread.join();
    }
    assertThat(tracker.getMaxSeqNo(), equalTo(maxOps - 1L));
    assertThat(tracker.getProcessedCheckpoint(), equalTo(unFinishedSeq - 1L));
    tracker.markSeqNoAsProcessed(unFinishedSeq);
    assertThat(tracker.getProcessedCheckpoint(), equalTo(maxOps - 1L));
    assertThat(tracker.processedSeqNo.size(), isOneOf(0, 1));
    if (tracker.processedSeqNo.size() == 1) {
        assertThat(tracker.processedSeqNo.keys().iterator().next().value, equalTo(tracker.processedCheckpoint.get() / BIT_SET_SIZE));
    }
}
 
Example #19
Source File: LocalCheckpointTrackerTests.java    From crate with Apache License 2.0 4 votes vote down vote up
public void testConcurrentReplica() throws InterruptedException {
    Thread[] threads = new Thread[randomIntBetween(2, 5)];
    final int opsPerThread = randomIntBetween(10, 20);
    final int maxOps = opsPerThread * threads.length;
    final long unFinishedSeq = randomIntBetween(0, maxOps - 2); // make sure we always index the last seqNo to simplify maxSeq checks
    Set<Integer> seqNos = IntStream.range(0, maxOps).boxed().collect(Collectors.toSet());

    final Integer[][] seqNoPerThread = new Integer[threads.length][];
    for (int t = 0; t < threads.length - 1; t++) {
        int size = Math.min(seqNos.size(), randomIntBetween(opsPerThread - 4, opsPerThread + 4));
        seqNoPerThread[t] = randomSubsetOf(size, seqNos).toArray(new Integer[size]);
        seqNos.removeAll(Arrays.asList(seqNoPerThread[t]));
    }
    seqNoPerThread[threads.length - 1] = seqNos.toArray(new Integer[seqNos.size()]);
    logger.info("--> will run [{}] threads, maxOps [{}], unfinished seq no [{}]", threads.length, maxOps, unFinishedSeq);
    final CyclicBarrier barrier = new CyclicBarrier(threads.length);
    for (int t = 0; t < threads.length; t++) {
        final int threadId = t;
        threads[t] = new Thread(new AbstractRunnable() {
            @Override
            public void onFailure(Exception e) {
                throw new ElasticsearchException("failure in background thread", e);
            }

            @Override
            protected void doRun() throws Exception {
                barrier.await();
                Integer[] ops = seqNoPerThread[threadId];
                for (int seqNo : ops) {
                    if (seqNo != unFinishedSeq) {
                        tracker.markSeqNoAsProcessed(seqNo);
                        logger.info("[t{}] completed [{}]", threadId, seqNo);
                    }
                }
            }
        }, "testConcurrentReplica_" + threadId);
        threads[t].start();
    }
    for (Thread thread : threads) {
        thread.join();
    }
    assertThat(tracker.getMaxSeqNo(), equalTo(maxOps - 1L));
    assertThat(tracker.getProcessedCheckpoint(), equalTo(unFinishedSeq - 1L));
    assertThat(tracker.hasProcessed(unFinishedSeq), equalTo(false));
    tracker.markSeqNoAsProcessed(unFinishedSeq);
    assertThat(tracker.getProcessedCheckpoint(), equalTo(maxOps - 1L));
    assertThat(tracker.hasProcessed(unFinishedSeq), equalTo(true));
    assertThat(tracker.hasProcessed(randomLongBetween(maxOps, Long.MAX_VALUE)), equalTo(false));
    assertThat(tracker.processedSeqNo.size(), isOneOf(0, 1));
    if (tracker.processedSeqNo.size() == 1) {
        assertThat(tracker.processedSeqNo.keys().iterator().next().value, equalTo(tracker.processedCheckpoint.get() / BIT_SET_SIZE));
    }
}