Alex
08/01/2025, 5:29 AMSRTThreadPoolServer based on TThreadPoolServer instead of TThreadedSelectorServer ?
because it create thread per connection in the end we finished with hundreds (or even thousands) threads who just waiting on socket readAlex
08/01/2025, 5:30 AMAlex
08/01/2025, 5:33 AMKevin Cai
08/01/2025, 9:52 AMKevin Cai
08/01/2025, 9:52 AMAlex
08/01/2025, 9:55 AMAlex
08/01/2025, 9:59 AM// allow core thread time out so that the thread can be released
// if not used for ThreadPoolManager.KEEP_ALIVE_TIME
threadPoolExecutor.allowCoreThreadTimeOut(true);
if we want to have cached thread pool better to create few core thread and specify max threads
but we created pull where only core thread exist and try to emulate it with allowCoreThreadTimeOut
it's incorrect because
* Proceed in 3 steps:
*
* 1. If fewer than corePoolSize threads are running, try to
* start a new thread with the given command as its first
* task. The call to addWorker atomically checks runState and
* workerCount, and so prevents false alarms that would add
* threads when it shouldn't, by returning false.
int c = ctl.get();
if (workerCountOf(c) < corePoolSize) {
if (addWorker(command, true)) // we permanently do this, because thread poll created with core threads only
return;
c = ctl.get();
}
if (isRunning(c) && workQueue.offer(command)) {
int recheck = ctl.get();
if (! isRunning(recheck) && remove(command))
reject(command);
else if (workerCountOf(recheck) == 0)
addWorker(null, false);
}
else if (!addWorker(command, false))
reject(command);
}Alex
08/01/2025, 10:02 AMthrift_server_max_worker_threads - max thread (config exist now)
thrift_server_min_worker_threads - core thread size, poll will not decrease less than this amount (new config parameter)
and drop this hack
// allow core thread time out so that the thread can be released
// if not used for ThreadPoolManager.KEEP_ALIVE_TIME
threadPoolExecutor.allowCoreThreadTimeOut(true);Alex
08/01/2025, 10:02 AMAlex
08/01/2025, 10:04 AMKevin Cai
08/01/2025, 10:06 AMKevin Cai
08/01/2025, 10:07 AMAlex
08/01/2025, 10:08 AMKevin Cai
08/01/2025, 10:09 AMAlex
08/01/2025, 10:09 AM/*
* Proceed in 3 steps:
*
* 1. If fewer than corePoolSize threads are running, try to
* start a new thread with the given command as its first
* task. The call to addWorker atomically checks runState and
* workerCount, and so prevents false alarms that would add
* threads when it shouldn't, by returning false.
*
* 2. If a task can be successfully queued, then we still need
* to double-check whether we should have added a thread
* (because existing ones died since last checking) or that
* the pool shut down since entry into this method. So we
* recheck state and if necessary roll back the enqueuing if
* stopped, or start a new thread if there are none.
*
* 3. If we cannot queue task, then we try to add a new
* thread. If it fails, we know we are shut down or saturated
* and so reject the task.
*/Kevin Cai
08/01/2025, 10:10 AMKevin Cai
08/01/2025, 10:10 AMKevin Cai
08/01/2025, 10:11 AMAlex
08/01/2025, 10:15 AMAlex
08/01/2025, 10:18 AMSynchronousQueue hack
public static ExecutorService newCachedThreadPool() {
return new ThreadPoolExecutor(0, Integer.MAX_VALUE, 60L, TimeUnit.SECONDS,
new SynchronousQueue<Runnable>());
}