do we have any reason why `SRTThreadPoolServer` ba...
# questions-and-troubleshooting
a
do we have any reason why
SRTThreadPoolServer
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 read
@stdpain @Kevin Cai sorry for ping you, but worry you can miss the question
second question related thrift client on be side: any reasons why we don't use keepalive on thrift clients?
k
not quite sure, assume it is legacy code from starrocks day one.
do you have any thought on this thrift server?
a
mostly not, just found it when investigated why we create new threads for every connection even if have idle threads
๐Ÿ˜€ 1
issue is here https://github.com/StarRocks/starrocks/blob/main/fe/fe-core/src/main/java/com/starrocks/common/ThriftServer.java#L66-L68
Copy code
// 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
Copy code
* 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);
    }
i will prepare patch a bit later
thrift_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
Copy code
// allow core thread time out so that the thread can be released
        // if not used for ThreadPoolManager.KEEP_ALIVE_TIME
        threadPoolExecutor.allowCoreThreadTimeOut(true);
@Kevin Cai what do you think?
issue come from https://github.com/StarRocks/starrocks/pull/14571 idea is good but implementation have some issue ๐Ÿ˜ƒ
k
the threadpool issue is, if the thread number exceeds the coreSize, new thread will not be created until the request queue is full, which is not good actually.
i think this is why we want the core threads timeout
a
no, new thread will be created immediately without waiting until queue will be full
k
I need to double check the threadpool behavior
๐Ÿ‘Œ 1
a
full descritption
Copy code
/*
         * 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.
         */
k
When a new task is submitted in method execute(Runnable), and fewer than corePoolSize threads are running, a new thread is created to handle the request, even if other worker threads are idle. If there are more than corePoolSize but less than maximumPoolSize threads running, a new thread will be created only if the queue is full.
this is from the oracle javase doc, need double check if the behavior changed in later jdk
a
no, the same
it's why cached thread poll implemented with
SynchronousQueue
hack
Copy code
public static ExecutorService newCachedThreadPool() {
    return new ThreadPoolExecutor(0, Integer.MAX_VALUE, 60L, TimeUnit.SECONDS, 
      new SynchronousQueue<Runnable>());
}
๐Ÿ‘Œ 1