1
0
mirror of https://github.com/mariadb-corporation/mariadb-columnstore-engine.git synced 2025-07-29 08:21:15 +03:00

MCOL-5044 This patch replaces PriorityThreadPool with FairThreadPool that uses a simple

operations + morsel size weight model to equally allocate CPU b/w parallel query morsels.
This patch delivers better parallel query timings distribution(timings graph resembles normal
distribution with a bigger left side thus more queries runs faster comparing with PrioThreadPool-based
single-node installation).
See changes in batchprimitiveprocessor-jl.h and comments in fair_threadpool.h for
important implementation details
This commit is contained in:
Roman Nozdrin
2022-05-24 17:57:40 +00:00
committed by Roman Nozdrin
parent 0f0b3a2bed
commit fd8ba33f21
12 changed files with 173 additions and 254 deletions

View File

@ -34,7 +34,7 @@ namespace threadpool
{
FairThreadPool::FairThreadPool(uint targetWeightPerRun, uint highThreads, uint midThreads, uint lowThreads,
uint ID)
: _stop(false), weightPerRun(targetWeightPerRun), id(ID), blockedThreads(0), extraThreads(0), stopExtra(true)
: _stop(false), weightPerRun(targetWeightPerRun), id(ID)
{
boost::thread* newThread;
size_t numberOfThreads = highThreads + midThreads + lowThreads;
@ -73,32 +73,20 @@ void FairThreadPool::addJob_(const Job& job, bool useLock)
++threadCounts;
}
// If some threads have blocked (because of output queue full)
// Temporarily add some extra worker threads to make up for the blocked threads.
if (blockedThreads > extraThreads)
{
stopExtra = false;
newThread = threads.create_thread(ThreadHelper(this, PriorityThreadPool::Priority::EXTRA));
newThread->detach();
extraThreads++;
}
else if (blockedThreads == 0)
{
// Release the temporary threads -- some threads have become unblocked.
stopExtra = true;
}
auto jobsListMapIter = txn2JobsListMap_.find(job.txnIdx_);
if (jobsListMapIter == txn2JobsListMap_.end())
if (jobsListMapIter == txn2JobsListMap_.end()) // there is no txn in the map
{
ThreadPoolJobsList* jobsList = new ThreadPoolJobsList;
jobsList->push_back(job);
txn2JobsListMap_[job.txnIdx_] = jobsList;
WeightT currentTopWeight = weightedTxnsQueue_.empty() ? 0 : weightedTxnsQueue_.top().first;
weightedTxnsQueue_.push({currentTopWeight, job.txnIdx_});
weightedTxnsQueue_.push({job.weight_, job.txnIdx_});
}
else
else // txn is in the map
{
if (jobsListMapIter->second->empty()) // there are no jobs for the txn
{
weightedTxnsQueue_.push({job.weight_, job.txnIdx_});
}
jobsListMapIter->second->push_back(job);
}
@ -135,11 +123,8 @@ void FairThreadPool::removeJobs(uint32_t id)
void FairThreadPool::threadFcn(const PriorityThreadPool::Priority preferredQueue)
{
if (preferredQueue == PriorityThreadPool::Priority::EXTRA)
utils::setThreadName("Extra");
else
utils::setThreadName("Idle");
RunListT runList; // This is a vector to allow to grab multiple jobs
utils::setThreadName("Idle");
RunListT runList(1); // This is a vector to allow to grab multiple jobs
RescheduleVecType reschedule;
bool running = false;
bool rescheduleJob = false;
@ -151,12 +136,6 @@ void FairThreadPool::threadFcn(const PriorityThreadPool::Priority preferredQueue
runList.clear(); // remove the job
std::unique_lock<std::mutex> lk(mutex);
if (preferredQueue == PriorityThreadPool::Priority::EXTRA && stopExtra)
{
--extraThreads;
return;
}
if (weightedTxnsQueue_.empty())
{
newJob.wait(lk);
@ -166,7 +145,7 @@ void FairThreadPool::threadFcn(const PriorityThreadPool::Priority preferredQueue
WeightedTxnT weightedTxn = weightedTxnsQueue_.top();
auto txnAndJobListPair = txn2JobsListMap_.find(weightedTxn.second);
// Looking for non-empty jobsList in a loop
// Waiting on cond_var if PQ is empty(no jobs in this thread pool)
// The loop waits on newJob cond_var if PQ is empty(no jobs in this thread pool)
while (txnAndJobListPair == txn2JobsListMap_.end() || txnAndJobListPair->second->empty())
{
// JobList is empty. This can happen when this method pops the last Job.
@ -196,7 +175,6 @@ void FairThreadPool::threadFcn(const PriorityThreadPool::Priority preferredQueue
weightedTxnsQueue_.pop();
TransactionIdxT txnIdx = txnAndJobListPair->first;
ThreadPoolJobsList* jobsList = txnAndJobListPair->second;
// Job& job = jobsList->front();
runList.push_back(jobsList->front());
jobsList->pop_front();
@ -210,13 +188,15 @@ void FairThreadPool::threadFcn(const PriorityThreadPool::Priority preferredQueue
lk.unlock();
running = true;
rescheduleJob = (*(runList[0].functor_))();
rescheduleJob = (*(runList[0].functor_))(); // run the functor
running = false;
utils::setThreadName("Idle");
if (rescheduleJob)
{
// to avoid excessive CPU usage waiting for data from storage
usleep(500);
lk.lock();
addJob_(runList[0], false);
newJob.notify_one();

View File

@ -39,6 +39,12 @@
namespace threadpool
{
// The idea of this thread pool is to run morsel jobs(primitive job) is to equaly distribute CPU time
// b/w multiple parallel queries(thread maps morsel to query using txnId). Query(txnId) has its weight
// stored in PriorityQueue that thread increases before run another morsel for the query. When query is
// done(ThreadPoolJobsList is empty) it is removed from PQ and the Map(txn to ThreadPoolJobsList).
// I tested multiple morsels per one loop iteration in ::threadFcn. This approach reduces CPU consumption
// and increases query timings.
class FairThreadPool
{
public:
@ -92,23 +98,6 @@ class FairThreadPool
*/
void dump();
// If a job is blocked, we want to temporarily increase the number of threads managed by the pool
// A problem can occur if all threads are running long or blocked for a single query. Other
// queries won't get serviced, even though there are cpu cycles available.
// These calls are currently protected by respondLock in sendThread(). If you call from other
// places, you need to consider atomicity.
void incBlockedThreads()
{
blockedThreads++;
}
void decBlockedThreads()
{
blockedThreads--;
}
uint32_t blockedThreadCount() const
{
return blockedThreads;
}
size_t queueSize() const
{
return weightedTxnsQueue_.size();
@ -165,9 +154,6 @@ class FairThreadPool
using Txn2ThreadPoolJobsListMap = std::unordered_map<TransactionIdxT, ThreadPoolJobsList*>;
Txn2ThreadPoolJobsListMap txn2JobsListMap_;
WeightedTxnPrioQueue weightedTxnsQueue_;
std::atomic<uint32_t> blockedThreads;
std::atomic<uint32_t> extraThreads;
bool stopExtra;
};
} // namespace threadpool