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

*: Fix bug that DynamicThreadPool may not destruct tasks in time. (#4210) #4231

Merged
Merged
Show file tree
Hide file tree
Changes from 3 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
13 changes: 9 additions & 4 deletions dbms/src/Common/DynamicThreadPool.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,12 @@ void DynamicThreadPool::scheduledToNewDynamicThread(TaskPtr & task)
t.detach();
}

void DynamicThreadPool::executeTask(TaskPtr & task)
{
task->execute();
task.reset();
}

void DynamicThreadPool::fixedWork(size_t index)
{
Queue * queue = fixed_queues[index].get();
Expand All @@ -87,13 +93,13 @@ void DynamicThreadPool::fixedWork(size_t index)
queue->pop(task);
if (!task)
break;
task->execute();
executeTask(task);
}
}

void DynamicThreadPool::dynamicWork(TaskPtr initial_task)
{
initial_task->execute();
executeTask(initial_task);

DynamicNode node;
while (true)
Expand All @@ -110,8 +116,7 @@ void DynamicThreadPool::dynamicWork(TaskPtr initial_task)

if (!node.task) // may be timeout or cancelled
break;
node.task->execute();
node.task.reset();
executeTask(node.task);
}
alive_dynamic_threads.fetch_sub(1);
}
Expand Down
2 changes: 2 additions & 0 deletions dbms/src/Common/DynamicThreadPool.h
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,8 @@ class DynamicThreadPool
void fixedWork(size_t index);
void dynamicWork(TaskPtr initial_task);

static void executeTask(TaskPtr & task);

const std::chrono::nanoseconds dynamic_auto_shrink_cooldown;

std::vector<std::thread> fixed_threads;
Expand Down
43 changes: 43 additions & 0 deletions dbms/src/Common/tests/gtest_dynamic_thread_pool.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@

namespace DB::tests
{
namespace
{
class DynamicThreadPoolTest : public ::testing::Test
{
};
Expand Down Expand Up @@ -158,4 +160,45 @@ try
}
CATCH

struct X
{
std::mutex * mu;
std::condition_variable * cv;
bool * destructed;

X(std::mutex * mu_, std::condition_variable * cv_, bool * destructed_)
: mu(mu_)
, cv(cv_)
, destructed(destructed_)
{}

~X()
{
std::unique_lock lock(*mu);
*destructed = true;
cv->notify_all();
}
};

TEST_F(DynamicThreadPoolTest, testTaskDestruct)
try
{
std::mutex mu;
std::condition_variable cv;
bool destructed = false;

DynamicThreadPool pool(0, std::chrono::minutes(1));
auto tmp = std::make_shared<X>(&mu, &cv, &destructed);
pool.schedule(true, [x = tmp] {});
tmp.reset();

{
std::unique_lock lock(mu);
auto ret = cv.wait_for(lock, std::chrono::seconds(1), [&] { return destructed; });
ASSERT_TRUE(ret);
}
}
CATCH

} // namespace
} // namespace DB::tests