]> git.proxmox.com Git - ceph.git/blob - ceph/src/tools/rbd_mirror/main.cc
update ceph source to reef 18.1.2
[ceph.git] / ceph / src / tools / rbd_mirror / main.cc
1 // -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*-
2 // vim: ts=8 sw=2 smarttab
3
4 #include "common/ceph_argparse.h"
5 #include "common/config.h"
6 #include "common/debug.h"
7 #include "common/errno.h"
8 #include "common/perf_counters.h"
9 #include "global/global_init.h"
10 #include "global/signal_handler.h"
11 #include "Mirror.h"
12 #include "Types.h"
13
14 #include <vector>
15
16 rbd::mirror::Mirror *mirror = nullptr;
17 PerfCounters *g_journal_perf_counters = nullptr;
18 PerfCounters *g_snapshot_perf_counters = nullptr;
19
20 void usage() {
21 std::cout << "usage: rbd-mirror [options...]" << std::endl;
22 std::cout << "options:\n";
23 std::cout << " -m monaddress[:port] connect to specified monitor\n";
24 std::cout << " --keyring=<path> path to keyring for local cluster\n";
25 std::cout << " --log-file=<logfile> file to log debug output\n";
26 std::cout << " --debug-rbd-mirror=<log-level>/<memory-level> set rbd-mirror debug level\n";
27 generic_server_usage();
28 }
29
30 static void handle_signal(int signum)
31 {
32 if (mirror)
33 mirror->handle_signal(signum);
34 }
35
36 int main(int argc, const char **argv)
37 {
38 auto args = argv_to_vec(argc, argv);
39 if (args.empty()) {
40 std::cerr << argv[0] << ": -h or --help for usage" << std::endl;
41 exit(1);
42 }
43 if (ceph_argparse_need_usage(args)) {
44 usage();
45 exit(0);
46 }
47
48 auto cct = global_init(nullptr, args, CEPH_ENTITY_TYPE_CLIENT,
49 CODE_ENVIRONMENT_DAEMON,
50 CINIT_FLAG_UNPRIVILEGED_DAEMON_DEFAULTS);
51
52 if (g_conf()->daemonize) {
53 global_init_daemonize(g_ceph_context);
54 }
55
56 common_init_finish(g_ceph_context);
57
58 init_async_signal_handler();
59 register_async_signal_handler(SIGHUP, handle_signal);
60 register_async_signal_handler_oneshot(SIGINT, handle_signal);
61 register_async_signal_handler_oneshot(SIGTERM, handle_signal);
62
63 auto cmd_args = argv_to_vec(argc, argv);
64
65 // disable unnecessary librbd cache
66 g_ceph_context->_conf.set_val_or_die("rbd_cache", "false");
67
68 auto prio =
69 g_ceph_context->_conf.get_val<int64_t>("rbd_mirror_perf_stats_prio");
70 {
71 PerfCountersBuilder plb(g_ceph_context, "rbd_mirror_journal",
72 rbd::mirror::l_rbd_mirror_journal_first,
73 rbd::mirror::l_rbd_mirror_journal_last);
74 plb.add_u64_counter(rbd::mirror::l_rbd_mirror_journal_entries, "entries",
75 "Number of entries replayed", nullptr, prio);
76 plb.add_u64_counter(rbd::mirror::l_rbd_mirror_journal_replay_bytes,
77 "replay_bytes", "Total bytes replayed", nullptr, prio,
78 unit_t(UNIT_BYTES));
79 plb.add_time_avg(rbd::mirror::l_rbd_mirror_journal_replay_latency,
80 "replay_latency", "Replay latency", nullptr, prio);
81 g_journal_perf_counters = plb.create_perf_counters();
82 }
83 {
84 PerfCountersBuilder plb(
85 g_ceph_context, "rbd_mirror_snapshot",
86 rbd::mirror::l_rbd_mirror_snapshot_first,
87 rbd::mirror::l_rbd_mirror_snapshot_remote_timestamp);
88 plb.add_u64_counter(rbd::mirror::l_rbd_mirror_snapshot_snapshots,
89 "snapshots", "Number of snapshots synced", nullptr,
90 prio);
91 plb.add_time_avg(rbd::mirror::l_rbd_mirror_snapshot_sync_time, "sync_time",
92 "Average sync time", nullptr, prio);
93 plb.add_u64_counter(rbd::mirror::l_rbd_mirror_snapshot_sync_bytes,
94 "sync_bytes", "Total bytes synced", nullptr, prio,
95 unit_t(UNIT_BYTES));
96 g_snapshot_perf_counters = plb.create_perf_counters();
97 }
98 g_ceph_context->get_perfcounters_collection()->add(g_journal_perf_counters);
99 g_ceph_context->get_perfcounters_collection()->add(g_snapshot_perf_counters);
100
101 mirror = new rbd::mirror::Mirror(g_ceph_context, cmd_args);
102 int r = mirror->init();
103 if (r < 0) {
104 std::cerr << "failed to initialize: " << cpp_strerror(r) << std::endl;
105 goto cleanup;
106 }
107
108 mirror->run();
109
110 cleanup:
111 unregister_async_signal_handler(SIGHUP, handle_signal);
112 unregister_async_signal_handler(SIGINT, handle_signal);
113 unregister_async_signal_handler(SIGTERM, handle_signal);
114 shutdown_async_signal_handler();
115
116 g_ceph_context->get_perfcounters_collection()->remove(g_journal_perf_counters);
117 g_ceph_context->get_perfcounters_collection()->remove(g_snapshot_perf_counters);
118
119 delete mirror;
120 delete g_journal_perf_counters;
121 delete g_snapshot_perf_counters;
122
123 return r < 0 ? EXIT_FAILURE : EXIT_SUCCESS;
124 }