@Override public Unlocker writeLock(String key) { return delegate.writeLock(key); } }
@Override public Unlocker readLock(String key) { return delegate.readLock(key); }
public DefaultWorkspaceLockService() { this.delegate = new StripedLocks(16); }
private static ThreadFactory getFactory() { return new ThreadFactoryBuilder() .setUncaughtExceptionHandler(LoggingUncaughtExceptionHandler.getInstance()) .setNameFormat(LanguageServerInitializer.class.getSimpleName()) .setDaemon(true) .build(); }
/** * Stops some of the system services preparing system to full shutdown. System status is changed * from {@link SystemStatus#RUNNING} to {@link SystemStatus#PREPARING_TO_SHUTDOWN}. * * @throws ConflictException when system status is different from running */ public void stopServices() throws ConflictException { if (!statusRef.compareAndSet(RUNNING, PREPARING_TO_SHUTDOWN)) { throw new ConflictException( "System shutdown has been already called, system status: " + statusRef.get()); } ExecutorService exec = Executors.newSingleThreadExecutor( new ThreadFactoryBuilder() .setDaemon(false) .setNameFormat("ShutdownSystemServicesPool") .setUncaughtExceptionHandler(LoggingUncaughtExceptionHandler.getInstance()) .build()); exec.execute(ThreadLocalPropagateContext.wrap(this::doStopServices)); exec.shutdown(); }
/** * Delegates call to {@link ExecutorService#submit(Callable)} and propagates thread locals to it * like defined by {@link ThreadLocalPropagateContext}. */ public <T> Future<T> submit(Callable<T> callable) { return executor.submit(ThreadLocalPropagateContext.wrap(callable)); }
@Inject public MailSender(MailSessionProvider mailSessionProvider) { this.mailSessionProvider = mailSessionProvider; this.executor = newFixedThreadPool( 2 * Runtime.getRuntime().availableProcessors(), new ThreadFactoryBuilder() .setNameFormat("MailNotificationsPool-%d") .setDaemon(false) .setUncaughtExceptionHandler(LoggingUncaughtExceptionHandler.getInstance()) .build()); }
/** * Suspends some of the system services preparing system to lighter shutdown. System status is * changed from {@link SystemStatus#RUNNING} to {@link SystemStatus#PREPARING_TO_SHUTDOWN}. * * @throws ConflictException when system status is different from running */ public void suspendServices() throws ConflictException { if (!statusRef.compareAndSet(RUNNING, PREPARING_TO_SHUTDOWN)) { throw new ConflictException( "System shutdown has been already called, system status: " + statusRef.get()); } ExecutorService exec = Executors.newSingleThreadExecutor( new ThreadFactoryBuilder() .setDaemon(false) .setNameFormat("SuspendSystemServicesPool") .setUncaughtExceptionHandler(LoggingUncaughtExceptionHandler.getInstance()) .build()); exec.execute(ThreadLocalPropagateContext.wrap(this::doSuspendServices)); exec.shutdown(); }
/** * Asynchronously runs the given task wrapping it with {@link * ThreadLocalPropagateContext#wrap(Runnable)} * * @param runnable task to run * @return completable future bounded to the task */ public CompletableFuture<Void> runAsync(Runnable runnable) { return CompletableFuture.runAsync(ThreadLocalPropagateContext.wrap(runnable), executor); }
/** * Acquire resources lock for specified account. * * @param accountId account id to lock resources * @return lock for unlocking resources when resources operation finishes * @throws NotFoundException when account with specified {@code account id} was not found * @throws ServerException when any other error occurs */ public Unlocker lock(String accountId) throws NotFoundException, ServerException { final Account account = accountManager.getById(accountId); final ResourceLockKeyProvider resourceLockKeyProvider = accountTypeToLockProvider.get(account.getType()); String lockKey; if (resourceLockKeyProvider == null) { // this account type doesn't have custom lock provider. // Lock resources by current account lockKey = accountId; } else { lockKey = resourceLockKeyProvider.getLockKey(accountId); } return stripedLocks.writeLock(lockKey); }
@Inject public ResourcesLocks( Set<ResourceLockKeyProvider> resourceLockKeyProviders, AccountManager accountManager) { this.accountManager = accountManager; this.stripedLocks = new StripedLocks(16); this.accountTypeToLockProvider = resourceLockKeyProviders .stream() .collect( Collectors.toMap(ResourceLockKeyProvider::getAccountType, Function.identity())); }
/** * @param corePoolSize the number of threads to keep in the pool, even if they are idle, unless * {@code allowCoreThreadTimeOut} is set */ @Inject public ThreadPullLauncher(@Named("schedule.core_pool_size") Integer corePoolSize) { this.service = new CronThreadPoolExecutor( corePoolSize, new ThreadFactoryBuilder() .setNameFormat("Annotated-scheduler-%d") .setUncaughtExceptionHandler(LoggingUncaughtExceptionHandler.getInstance()) .setDaemon(false) .build()); }
@Override public void process(Runnable runnable) { executorService.execute(ThreadLocalPropagateContext.wrap(runnable)); } }
@PostConstruct private void postConstruct() { ThreadFactory factory = new ThreadFactoryBuilder() .setUncaughtExceptionHandler(LoggingUncaughtExceptionHandler.getInstance()) .setNameFormat(ServerSideRequestProcessor.class.getSimpleName() + "-%d") .setDaemon(true) .build(); executorService = new ThreadPoolExecutor( 0, maxPoolSize, 60L, TimeUnit.SECONDS, new SynchronousQueue<>(), factory); ((ThreadPoolExecutor) executorService) .setRejectedExecutionHandler( (r, executor) -> LOG.warn("Message {} rejected for execution", r)); }
/** * Delegates call to {@link ExecutorService#execute(Runnable)} and propagates thread locals to it * like defined by {@link ThreadLocalPropagateContext}. */ public void execute(Runnable runnable) { executor.execute(ThreadLocalPropagateContext.wrap(runnable)); }
new ThreadFactoryBuilder() .setNameFormat("WorkspaceSharedPool-%d") .setUncaughtExceptionHandler(LoggingUncaughtExceptionHandler.getInstance()) .setDaemon(false) .build();
@Override protected Callable<Object> newCallable(Object resource, Method method, Object[] params) { return ThreadLocalPropagateContext.wrap((super.newCallable(resource, method, params))); } }
.setNameFormat("WorkspaceInitializer-%d") .setDaemon(true) .setUncaughtExceptionHandler(LoggingUncaughtExceptionHandler.getInstance()) .build());
publishWorkspaceStatusEvent(workspaceId, STOPPING, status, options.get(WORKSPACE_STOP_REASON)); return CompletableFuture.runAsync( ThreadLocalPropagateContext.wrap(new StopRuntimeTask(workspace, options, stoppedBy)), sharedPool.getExecutor());
ThreadLocalPropagateContext.wrap(new StartRuntimeTask(workspace, options, runtime)), sharedPool.getExecutor()); } catch (ValidationException e) {