Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

not hold mutex when destruct big object #178

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 12 additions & 7 deletions src/db_impl_gc.cc
Original file line number Diff line number Diff line change
Expand Up @@ -201,21 +201,26 @@ Status TitanDBImpl::BackgroundGC(LogBuffer* log_buffer,
TITAN_LOG_BUFFER(log_buffer, "Titan GC nothing to do");
} else {
StopWatch gc_sw(env_, statistics(stats_.get()), TITAN_GC_MICROS);
BlobGCJob blob_gc_job(blob_gc.get(), db_, &mutex_, db_options_,
gc_merge_rewrite, env_, env_options_,
blob_manager_.get(), blob_file_set_.get(), log_buffer,
&shuting_down_, stats_.get());
s = blob_gc_job.Prepare();
BlobGCJob *blob_gc_job = new BlobGCJob(blob_gc.get(), db_, &mutex_, db_options_,
gc_merge_rewrite, env_, env_options_,
blob_manager_.get(), blob_file_set_.get(), log_buffer,
&shuting_down_, stats_.get());
s = blob_gc_job->Prepare();
if (s.ok()) {
mutex_.Unlock();
TEST_SYNC_POINT("TitanDBImpl::BackgroundGC::BeforeRunGCJob");
s = blob_gc_job.Run();
s = blob_gc_job->Run();
TEST_SYNC_POINT("TitanDBImpl::BackgroundGC::AfterRunGCJob");
mutex_.Lock();
}
if (s.ok()) {
s = blob_gc_job.Finish();
s = blob_gc_job->Finish();
}

mutex_.Unlock();
delete blob_gc_job;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

not sure if unlocking would cause any race condition, and it is just hard to check. A better pattern is we can pass a struct GCContext { std::unique_ptr<BlobGCJob> } from outside of the mutex (in BackgroundCallGC()) into BackgroundGC and use the context struct to hold the pointer, and make sure the context struct is cleanup outside of mutex.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We checked the BlobGCJob destruct function and sure it will not cause race condition, but it is a good idea to use GCContext to manage the BlobGCJob pointer.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not just BlobGCJob destructor. I didn't check whether it is safe if some other job slip in between blob_gc_job->Finish() and blob_gc->ReleaseGcFiles().

mutex_.Lock();

blob_gc->ReleaseGcFiles();

if (blob_gc->trigger_next() &&
Expand Down