/** * Get the generic {@link ExecutorService}. This executor service * {@link Executor#execute(Runnable)} method will run the {@link Runnable} it is given in the * {@link ThreadContext} of the thread that queues it. * <p> * Warning: this {@linkplain ExecutorService} will not throw {@link RejectedExecutionException} * if you submit a task while it shutdown. It will instead silently queue it and not run it. */ public ExecutorService generic() { return executor(Names.GENERIC); }
BitSetProducerWarmer(ThreadPool threadPool) { this.executor = threadPool.executor(ThreadPool.Names.WARMER); }
@Override public void onResponse(final Response response) { threadPool.executor(executor).execute(new AbstractRunnable() { @Override public boolean isForceExecution() { return forceExecution; } @Override protected void doRun() throws Exception { listener.onResponse(response); } @Override public void onFailure(Exception e) { listener.onFailure(e); } }); }
@Override public void onFailure(final Exception e) { threadPool.executor(executor).execute(new AbstractRunnable() { @Override public boolean isForceExecution() { return forceExecution; } @Override protected void doRun() throws Exception { listener.onFailure(e); } @Override public void onFailure(Exception e) { logger.warn(() -> new ParameterizedMessage("failed to execute failure callback on [{}]", listener), e); } }); } }
public void execute(SimulatePipelineRequest.Parsed request, ActionListener<SimulatePipelineResponse> listener) { threadPool.executor(THREAD_POOL_NAME).execute(new ActionRunnable<SimulatePipelineResponse>(listener) { @Override protected void doRun() throws Exception { List<SimulateDocumentResult> responses = new ArrayList<>(); for (IngestDocument ingestDocument : request.getDocuments()) { SimulateDocumentResult response = executeDocument(request.getPipeline(), ingestDocument, request.isVerbose()); if (response != null) { responses.add(response); } } listener.onResponse(new SimulatePipelineResponse(request.getPipeline().getId(), request.isVerbose(), responses)); } }); } }
@Override public void onResponse(final Releasable releasable) { threadPool.executor(executor).execute(new AbstractRunnable() { @Override public boolean isForceExecution() { return forceExecution; } @Override protected void doRun() throws Exception { listener.onResponse(releasable); } @Override public void onRejection(Exception e) { IOUtils.closeWhileHandlingException(releasable); super.onRejection(e); } @Override public void onFailure(Exception e) { listener.onFailure(e); // will possibly execute on the caller thread } }); }
protected void asyncShardOperation(Request request, ShardId shardId, ActionListener<Response> listener) throws IOException { threadPool.executor(getExecutor(request, shardId)).execute(new AbstractRunnable() { @Override public void onFailure(Exception e) { listener.onFailure(e); } @Override protected void doRun() throws Exception { listener.onResponse(shardOperation(request, shardId)); } }); }
IndexWarmer(ThreadPool threadPool, IndexFieldDataService indexFieldDataService, Listener... listeners) { ArrayList<Listener> list = new ArrayList<>(); final Executor executor = threadPool.executor(ThreadPool.Names.WARMER); list.add(new FieldDataWarmer(executor, indexFieldDataService)); Collections.addAll(list, listeners); this.listeners = Collections.unmodifiableList(list); }
/** 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); } }); }
protected void asyncShardOperation(ShardRequest request, Task task, ActionListener<ShardResponse> listener) { transportService.getThreadPool().executor(getExecutor(request)).execute(new AbstractRunnable() { @Override public void onFailure(Exception e) { listener.onFailure(e); } @Override protected void doRun() throws Exception { listener.onResponse(shardOperation(request, task)); } }); }
private Executor getExecutor(IndexShard indexShard) { assert indexShard != null; return threadPool.executor(indexShard.indexSettings().isSearchThrottled() ? Names.SEARCH_THROTTLED : Names.SEARCH); }
private void handleException(final TransportResponseHandler handler, Throwable error) { if (!(error instanceof RemoteTransportException)) { error = new RemoteTransportException(error.getMessage(), error); } final RemoteTransportException rtx = (RemoteTransportException) error; threadPool.executor(handler.executor()).execute(() -> { try { handler.handleException(rtx); } catch (Exception e) { logger.error(() -> new ParameterizedMessage("failed to handle exception response [{}]", handler), e); } }); }
public <Params extends PersistentTaskParams> void executeTask(final Params params, final @Nullable PersistentTaskState state, final AllocatedPersistentTask task, final PersistentTasksExecutor<Params> executor) { threadPool.executor(executor.getExecutor()).execute(new AbstractRunnable() { @Override public void onFailure(Exception e) { task.markAsFailed(e); } @SuppressWarnings("unchecked") @Override protected void doRun() throws Exception { try { executor.nodeOperation(task, params, state); } catch (Exception ex) { task.markAsFailed(ex); } } }); } }
/** * Schedules a one-shot command to run after a given delay. The command is not run in the context of the calling thread. To preserve the * context of the calling thread you may call <code>threadPool.getThreadContext().preserveContext</code> on the runnable before passing * it to this method. * * @param delay delay before the task executes * @param executor the name of the thread pool on which to execute this task. SAME means "execute on the scheduler thread" which changes the * meaning of the ScheduledFuture returned by this method. In that case the ScheduledFuture will complete only when the command * completes. * @param command the command to run * @return a ScheduledFuture who's get will return when the task is has been added to its target thread pool and throw an exception if * the task is canceled before it was added to its target thread pool. Once the task has been added to its target thread pool * the ScheduledFuture will cannot interact with it. * @throws org.elasticsearch.common.util.concurrent.EsRejectedExecutionException if the task cannot be scheduled for execution */ public ScheduledFuture<?> schedule(TimeValue delay, String executor, Runnable command) { if (!Names.SAME.equals(executor)) { command = new ThreadedRunnable(command, executor(executor)); } return scheduler.schedule(new ThreadPool.LoggingRunnable(command), delay.millis(), TimeUnit.MILLISECONDS); }
/** * Build {@linkplain RefreshListeners} for this shard. */ private RefreshListeners buildRefreshListeners() { return new RefreshListeners( indexSettings::getMaxRefreshListeners, () -> refresh("too_many_listeners"), threadPool.executor(ThreadPool.Names.LISTENER)::execute, logger, threadPool.getThreadContext()); }
@Override public void clusterChanged(ClusterChangedEvent event) { if (!master && event.localNodeMaster()) { master = true; for (LocalNodeMasterListener listener : listeners) { java.util.concurrent.Executor executor = threadPool.executor(listener.executorName()); executor.execute(new OnMasterRunnable(listener)); } return; } if (master && !event.localNodeMaster()) { master = false; for (LocalNodeMasterListener listener : listeners) { java.util.concurrent.Executor executor = threadPool.executor(listener.executorName()); executor.execute(new OffMasterRunnable(listener)); } } }
@Override public void sendResponse(TransportResponse response) throws IOException { service.onResponseSent(requestId, action, response); final TransportResponseHandler handler = service.responseHandlers.onResponseReceived(requestId, service); // ignore if its null, the service logs it if (handler != null) { final String executor = handler.executor(); if (ThreadPool.Names.SAME.equals(executor)) { processResponse(handler, response); } else { threadPool.executor(executor).execute(new Runnable() { @Override public void run() { processResponse(handler, response); } @Override public String toString() { return "delivery of response to [" + requestId + "][" + action + "]: " + response; } }); } } }
@Override public void onMaster() { this.isMaster = true; if (logger.isTraceEnabled()) { logger.trace("I have been elected master, scheduling a ClusterInfoUpdateJob"); } try { // Submit a job that will start after DEFAULT_STARTING_INTERVAL, and reschedule itself after running threadPool.schedule(updateFrequency, executorName(), new SubmitReschedulingClusterInfoUpdatedJob()); if (clusterService.state().getNodes().getDataNodes().size() > 1) { // Submit an info update job to be run immediately threadPool.executor(executorName()).execute(() -> maybeRefresh()); } } catch (EsRejectedExecutionException ex) { if (logger.isDebugEnabled()) { logger.debug("Couldn't schedule cluster info update task - node might be shutting down", ex); } } }
/** * Deletes snapshot from repository * * @param snapshot snapshot * @param listener listener * @param repositoryStateId the unique id representing the state of the repository at the time the deletion began */ private void deleteSnapshotFromRepository(final Snapshot snapshot, @Nullable final DeleteSnapshotListener listener, long repositoryStateId) { threadPool.executor(ThreadPool.Names.SNAPSHOT).execute(() -> { try { Repository repository = repositoriesService.repository(snapshot.getRepository()); repository.deleteSnapshot(snapshot.getSnapshotId(), repositoryStateId); logger.info("snapshot [{}] deleted", snapshot); removeSnapshotDeletionFromClusterState(snapshot, null, listener); } catch (Exception ex) { removeSnapshotDeletionFromClusterState(snapshot, ex, listener); } }); }
private void handleUpdateFailureWithRetry(final ActionListener<UpdateResponse> listener, final UpdateRequest request, final Exception failure, int retryCount) { final Throwable cause = unwrapCause(failure); if (cause instanceof VersionConflictEngineException) { if (retryCount < request.retryOnConflict()) { logger.trace("Retry attempt [{}] of [{}] on version conflict on [{}][{}][{}]", retryCount + 1, request.retryOnConflict(), request.index(), request.getShardId(), request.id()); threadPool.executor(executor()).execute(new ActionRunnable<UpdateResponse>(listener) { @Override protected void doRun() { shardOperation(request, listener, retryCount + 1); } }); return; } } listener.onFailure(cause instanceof Exception ? (Exception) cause : new NotSerializableExceptionWrapper(cause)); } }