Unverified Commit c12dfdfb authored by Connor's avatar Connor Committed by GitHub

Use individual thread pool for background GC (#126)

* new thread pool for titan
Signed-off-by: 's avatarConnor1996 <zbk602423539@gmail.com>
parent 5ab6a37d
...@@ -5,10 +5,10 @@ ...@@ -5,10 +5,10 @@
#endif #endif
#include <inttypes.h> #include <inttypes.h>
#include "logging/log_buffer.h" #include "logging/log_buffer.h"
#include "port/port.h" #include "port/port.h"
#include "util/autovector.h" #include "util/autovector.h"
#include "util/threadpool_imp.h"
#include "base_db_listener.h" #include "base_db_listener.h"
#include "blob_file_builder.h" #include "blob_file_builder.h"
...@@ -259,8 +259,12 @@ Status TitanDBImpl::OpenImpl(const std::vector<TitanCFDescriptor>& descs, ...@@ -259,8 +259,12 @@ Status TitanDBImpl::OpenImpl(const std::vector<TitanCFDescriptor>& descs,
} }
// Initialize GC thread pool. // Initialize GC thread pool.
if (!db_options_.disable_background_gc && db_options_.max_background_gc > 0) { if (!db_options_.disable_background_gc && db_options_.max_background_gc > 0) {
env_->IncBackgroundThreadsIfNeeded(db_options_.max_background_gc, auto pool = NewThreadPool(0);
Env::Priority::BOTTOM); // Hack: set thread priority to change the thread name
(reinterpret_cast<ThreadPoolImpl*>(pool))
->SetThreadPriority(Env::Priority::USER);
pool->SetBackgroundThreads(db_options_.max_background_gc);
thread_pool_.reset(pool);
} }
// Open base DB. // Open base DB.
s = DB::Open(db_options_, dbname_, base_descs, handles, &db_); s = DB::Open(db_options_, dbname_, base_descs, handles, &db_);
...@@ -346,10 +350,13 @@ Status TitanDBImpl::CloseImpl() { ...@@ -346,10 +350,13 @@ Status TitanDBImpl::CloseImpl() {
shuting_down_.store(true, std::memory_order_release); shuting_down_.store(true, std::memory_order_release);
} }
int gc_unscheduled = env_->UnSchedule(this, Env::Priority::BOTTOM); if (thread_pool_ != nullptr) {
thread_pool_->JoinAllThreads();
}
{ {
MutexLock l(&mutex_); MutexLock l(&mutex_);
bg_gc_scheduled_ -= gc_unscheduled; // `bg_gc_scheduled_` should be 0 after `JoinAllThreads`, double check here.
while (bg_gc_scheduled_ > 0) { while (bg_gc_scheduled_ > 0) {
bg_cv_.Wait(); bg_cv_.Wait();
} }
......
...@@ -2,6 +2,7 @@ ...@@ -2,6 +2,7 @@
#include "db/db_impl/db_impl.h" #include "db/db_impl/db_impl.h"
#include "rocksdb/statistics.h" #include "rocksdb/statistics.h"
#include "rocksdb/threadpool.h"
#include "util/repeatable_thread.h" #include "util/repeatable_thread.h"
#include "blob_file_manager.h" #include "blob_file_manager.h"
...@@ -256,6 +257,9 @@ class TitanDBImpl : public TitanDB { ...@@ -256,6 +257,9 @@ class TitanDBImpl : public TitanDB {
Status bg_error_; Status bg_error_;
std::atomic_bool has_bg_error_{false}; std::atomic_bool has_bg_error_{false};
// Thread pool for running background GC.
std::unique_ptr<ThreadPool> thread_pool_;
// TitanStats is turned on only if statistics field of DBOptions // TitanStats is turned on only if statistics field of DBOptions
// is not null. // is not null.
std::unique_ptr<TitanStats> stats_; std::unique_ptr<TitanStats> stats_;
......
...@@ -20,7 +20,7 @@ void TitanDBImpl::MaybeScheduleGC() { ...@@ -20,7 +20,7 @@ void TitanDBImpl::MaybeScheduleGC() {
bg_gc_scheduled_ < db_options_.max_background_gc) { bg_gc_scheduled_ < db_options_.max_background_gc) {
unscheduled_gc_--; unscheduled_gc_--;
bg_gc_scheduled_++; bg_gc_scheduled_++;
env_->Schedule(&TitanDBImpl::BGWorkGC, this, Env::Priority::BOTTOM, this); thread_pool_->SubmitJob(std::bind(&TitanDBImpl::BGWorkGC, this));
} }
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment