]> git.proxmox.com Git - ceph.git/blob - ceph/src/tools/cephfs_mirror/Mirror.h
2e50a2a8bdbdcf28077f10bdfe7f4962efbaaedd
[ceph.git] / ceph / src / tools / cephfs_mirror / Mirror.h
1 // -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*-
2 // vim: ts=8 sw=2 smarttab
3
4 #ifndef CEPHFS_MIRROR_H
5 #define CEPHFS_MIRROR_H
6
7 #include <map>
8 #include <set>
9 #include <vector>
10
11 #include "common/ceph_mutex.h"
12 #include "common/WorkQueue.h"
13 #include "mds/FSMap.h"
14 #include "ClusterWatcher.h"
15 #include "FSMirror.h"
16 #include "ServiceDaemon.h"
17 #include "Types.h"
18
19 class Messenger;
20 class MonClient;
21 class ContextWQ;
22
23 namespace cephfs {
24 namespace mirror {
25
26 // this wraps up ClusterWatcher and FSMirrors to implement mirroring
27 // for ceph filesystems.
28
29 class Mirror {
30 public:
31 Mirror(CephContext *cct, const std::vector<const char*> &args,
32 MonClient *monc, Messenger *msgr);
33 ~Mirror();
34
35 int init(std::string &reason);
36 void shutdown();
37 void run();
38
39 void handle_signal(int signum);
40
41 private:
42 static constexpr std::string_view MIRRORING_MODULE = "mirroring";
43
44 struct C_EnableMirroring;
45 struct C_DisableMirroring;
46 struct C_PeerUpdate;
47 struct C_RestartMirroring;
48
49 struct ClusterListener : ClusterWatcher::Listener {
50 Mirror *mirror;
51
52 ClusterListener(Mirror *mirror)
53 : mirror(mirror) {
54 }
55
56 void handle_mirroring_enabled(const FilesystemSpec &spec) override {
57 mirror->mirroring_enabled(spec.filesystem, spec.pool_id);
58 }
59
60 void handle_mirroring_disabled(const Filesystem &filesystem) override {
61 mirror->mirroring_disabled(filesystem);
62 }
63
64 void handle_peers_added(const Filesystem &filesystem, const Peer &peer) override {
65 mirror->peer_added(filesystem, peer);
66 }
67
68 void handle_peers_removed(const Filesystem &filesystem, const Peer &peer) override {
69 mirror->peer_removed(filesystem, peer);
70 }
71 };
72
73 struct MirrorAction {
74 MirrorAction(uint64_t pool_id) :
75 pool_id(pool_id) {
76 }
77
78 uint64_t pool_id; // for restarting blocklisted mirror instance
79 bool action_in_progress = false;
80 std::list<Context *> action_ctxs;
81 std::unique_ptr<FSMirror> fs_mirror;
82 };
83
84 ceph::mutex m_lock = ceph::make_mutex("cephfs::mirror::Mirror");
85 ceph::condition_variable m_cond;
86
87 CephContext *m_cct;
88 std::vector<const char *> m_args;
89 MonClient *m_monc;
90 Messenger *m_msgr;
91 ClusterListener m_listener;
92
93 ThreadPool *m_thread_pool = nullptr;
94 ContextWQ *m_work_queue = nullptr;
95 SafeTimer *m_timer = nullptr;
96 ceph::mutex *m_timer_lock = nullptr;
97 Context *m_timer_task = nullptr;
98
99 bool m_stopping = false;
100 std::unique_ptr<ClusterWatcher> m_cluster_watcher;
101 std::map<Filesystem, MirrorAction> m_mirror_actions;
102
103 utime_t m_last_blocklist_check;
104 utime_t m_last_failure_check;
105
106 RadosRef m_local;
107 std::unique_ptr<ServiceDaemon> m_service_daemon;
108
109 int init_mon_client();
110
111 // called via listener
112 void mirroring_enabled(const Filesystem &filesystem, uint64_t local_pool_id);
113 void mirroring_disabled(const Filesystem &filesystem);
114 void peer_added(const Filesystem &filesystem, const Peer &peer);
115 void peer_removed(const Filesystem &filesystem, const Peer &peer);
116
117 // mirror enable callback
118 void enable_mirroring(const Filesystem &filesystem, uint64_t local_pool_id,
119 Context *on_finish, bool is_restart=false);
120 void handle_enable_mirroring(const Filesystem &filesystem, int r);
121 void handle_enable_mirroring(const Filesystem &filesystem, const Peers &peers, int r);
122
123 // mirror disable callback
124 void disable_mirroring(const Filesystem &filesystem, Context *on_finish);
125 void handle_disable_mirroring(const Filesystem &filesystem, int r);
126
127 // peer update callback
128 void add_peer(const Filesystem &filesystem, const Peer &peer);
129 void remove_peer(const Filesystem &filesystem, const Peer &peer);
130
131 void schedule_mirror_update_task();
132 void update_fs_mirrors();
133 };
134
135 } // namespace mirror
136 } // namespace cephfs
137
138 #endif // CEPHFS_MIRROR_H