Skip to content

Commit 81382ac

Browse files
committed
tool/ceph_dedup: move signal_handler into SampleDedupGlobal to remove a gobal variable
Signed-off-by: Myoungwon Oh <[email protected]> Signed-off-by: Sungmin Lee <[email protected]>
1 parent 54516b7 commit 81382ac

File tree

1 file changed

+28
-30
lines changed

1 file changed

+28
-30
lines changed

src/tools/ceph_dedup/ceph_dedup_daemon.cc

Lines changed: 28 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -244,16 +244,29 @@ class SampleDedupWorkerThread : public Thread
244244
std::shared_lock l{glock};
245245
return all_stop;
246246
}
247-
void set_all_stop() {
247+
static void set_all_stop() {
248248
std::unique_lock l{glock};
249249
all_stop = true;
250250
}
251+
static void handle_signal(int signum)
252+
{
253+
switch (signum) {
254+
case SIGINT:
255+
case SIGTERM:
256+
set_all_stop();
257+
dout(0) << "got a signal(" << signum << "), daemon wil be terminiated" << dendl;
258+
break;
259+
260+
default:
261+
ceph_abort_msgf("unexpected signal %d", signum);
262+
}
263+
}
251264
friend class SampleDedupWorkerThread;
252265
private:
253266
FpStore fp_store;
254267
const double sampling_ratio = -1;
255-
ceph::shared_mutex glock = ceph::make_shared_mutex("glock");
256-
bool all_stop = false; // Accessed in the main thread and in other worker threads under glock
268+
inline static ceph::shared_mutex glock = ceph::make_shared_mutex("glock");
269+
inline static bool all_stop = false; // Accessed in the main thread and in other worker threads under glock
257270
};
258271

259272
SampleDedupWorkerThread(
@@ -580,8 +593,6 @@ int SampleDedupWorkerThread::do_chunk_dedup(chunk_t &chunk, snap_t snap)
580593
return ret;
581594
}
582595

583-
unique_ptr<SampleDedupWorkerThread::SampleDedupGlobal> state;
584-
585596
int run_crawling_daemon(const po::variables_map &opts)
586597
{
587598
string base_pool_name = get_opts_pool_name(opts);
@@ -668,11 +679,11 @@ int run_crawling_daemon(const po::variables_map &opts)
668679
<< ")"
669680
<< dendl;
670681

671-
state = std::make_unique<SampleDedupWorkerThread::SampleDedupGlobal>(
682+
SampleDedupWorkerThread::SampleDedupGlobal state(
672683
chunk_dedup_threshold, sampling_ratio, report_period, fp_threshold);
673684
ret = 0;
674685

675-
while (!state->is_all_stop()) {
686+
while (!state.is_all_stop()) {
676687
ObjectCursor begin = io_ctx.object_list_begin();
677688
ObjectCursor end = io_ctx.object_list_end();
678689

@@ -699,7 +710,7 @@ int run_crawling_daemon(const po::variables_map &opts)
699710
chunk_size,
700711
fp_algo,
701712
chunk_algo,
702-
*state,
713+
state,
703714
snap);
704715
threads.back().create("sample_dedup");
705716
}
@@ -729,8 +740,7 @@ int run_crawling_daemon(const po::variables_map &opts)
729740
}
730741

731742
if (run_once) {
732-
assert(state);
733-
state->set_all_stop();
743+
state.set_all_stop();
734744
break;
735745
}
736746
}
@@ -739,22 +749,6 @@ int run_crawling_daemon(const po::variables_map &opts)
739749
return ret;
740750
}
741751

742-
static void handle_signal(int signum)
743-
{
744-
switch (signum) {
745-
case SIGINT:
746-
case SIGTERM:
747-
if (state) {
748-
state->set_all_stop();
749-
}
750-
dout(0) << "got a signal(" << signum << "), daemon wil be terminiated" << dendl;
751-
break;
752-
753-
default:
754-
ceph_abort_msgf("unexpected signal %d", signum);
755-
}
756-
}
757-
758752
int main(int argc, const char **argv)
759753
{
760754
auto args = argv_to_vec(argc, argv);
@@ -809,13 +803,17 @@ int main(int argc, const char **argv)
809803
}
810804

811805
init_async_signal_handler();
812-
register_async_signal_handler_oneshot(SIGINT, handle_signal);
813-
register_async_signal_handler_oneshot(SIGTERM, handle_signal);
806+
register_async_signal_handler_oneshot(SIGINT,
807+
SampleDedupWorkerThread::SampleDedupGlobal::handle_signal);
808+
register_async_signal_handler_oneshot(SIGTERM,
809+
SampleDedupWorkerThread::SampleDedupGlobal::handle_signal);
814810

815811
int ret = run_crawling_daemon(opts);
816812

817-
unregister_async_signal_handler(SIGINT, handle_signal);
818-
unregister_async_signal_handler(SIGTERM, handle_signal);
813+
unregister_async_signal_handler(SIGINT,
814+
SampleDedupWorkerThread::SampleDedupGlobal::handle_signal);
815+
unregister_async_signal_handler(SIGTERM,
816+
SampleDedupWorkerThread::SampleDedupGlobal::handle_signal);
819817
shutdown_async_signal_handler();
820818

821819
return forker.signal_exit(ret);

0 commit comments

Comments
 (0)