]> git.proxmox.com Git - ceph.git/blob - ceph/src/librbd/Journal.h
update sources to ceph Nautilus 14.2.1
[ceph.git] / ceph / src / librbd / Journal.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 CEPH_LIBRBD_JOURNAL_H
5 #define CEPH_LIBRBD_JOURNAL_H
6
7 #include "include/int_types.h"
8 #include "include/Context.h"
9 #include "include/interval_set.h"
10 #include "include/rados/librados_fwd.hpp"
11 #include "common/Cond.h"
12 #include "common/Mutex.h"
13 #include "common/Cond.h"
14 #include "common/WorkQueue.h"
15 #include "journal/Future.h"
16 #include "journal/JournalMetadataListener.h"
17 #include "journal/ReplayEntry.h"
18 #include "journal/ReplayHandler.h"
19 #include "librbd/Utils.h"
20 #include "librbd/journal/Types.h"
21 #include "librbd/journal/TypeTraits.h"
22
23 #include <algorithm>
24 #include <list>
25 #include <string>
26 #include <atomic>
27 #include <unordered_map>
28
29 class SafeTimer;
30 namespace journal {
31 class Journaler;
32 }
33
34 namespace librbd {
35
36 class ImageCtx;
37
38 namespace journal { template <typename> class Replay; }
39
40 template <typename ImageCtxT = ImageCtx>
41 class Journal {
42 public:
43 /**
44 * @verbatim
45 *
46 * <start>
47 * |
48 * v
49 * UNINITIALIZED ---> INITIALIZING ---> REPLAYING ------> FLUSHING ---> READY
50 * | * . ^ * . * |
51 * | * . | * . * |
52 * | * . | (error) * . . . . . . . * |
53 * | * . | * . * |
54 * | * . | v . * |
55 * | * . | FLUSHING_RESTART . * |
56 * | * . | | . * |
57 * | * . | | . * |
58 * | * . | v . * v
59 * | * . | RESTARTING < * * * * * STOPPING
60 * | * . | | . |
61 * | * . | | . |
62 * | * * * * * * . \-------------/ . |
63 * | * (error) . . |
64 * | * . . . . . . . . . . . . . . . . |
65 * | * . . |
66 * | v v v |
67 * | CLOSED <----- CLOSING <---------------------------------------/
68 * | |
69 * | v
70 * \---> <finish>
71 *
72 * @endverbatim
73 */
74 enum State {
75 STATE_UNINITIALIZED,
76 STATE_INITIALIZING,
77 STATE_REPLAYING,
78 STATE_FLUSHING_RESTART,
79 STATE_RESTARTING_REPLAY,
80 STATE_FLUSHING_REPLAY,
81 STATE_READY,
82 STATE_STOPPING,
83 STATE_CLOSING,
84 STATE_CLOSED
85 };
86
87 static const std::string IMAGE_CLIENT_ID;
88 static const std::string LOCAL_MIRROR_UUID;
89 static const std::string ORPHAN_MIRROR_UUID;
90
91 Journal(ImageCtxT &image_ctx);
92 ~Journal();
93
94 static bool is_journal_supported(ImageCtxT &image_ctx);
95 static int create(librados::IoCtx &io_ctx, const std::string &image_id,
96 uint8_t order, uint8_t splay_width,
97 const std::string &object_pool);
98 static int remove(librados::IoCtx &io_ctx, const std::string &image_id);
99 static int reset(librados::IoCtx &io_ctx, const std::string &image_id);
100
101 static void is_tag_owner(ImageCtxT *image_ctx, bool *is_tag_owner,
102 Context *on_finish);
103 static void is_tag_owner(librados::IoCtx& io_ctx, std::string& image_id,
104 bool *is_tag_owner, ContextWQ *op_work_queue,
105 Context *on_finish);
106 static void get_tag_owner(librados::IoCtx& io_ctx, std::string& image_id,
107 std::string *mirror_uuid,
108 ContextWQ *op_work_queue, Context *on_finish);
109 static int request_resync(ImageCtxT *image_ctx);
110 static void promote(ImageCtxT *image_ctx, Context *on_finish);
111 static void demote(ImageCtxT *image_ctx, Context *on_finish);
112
113 bool is_journal_ready() const;
114 bool is_journal_replaying() const;
115 bool is_journal_appending() const;
116
117 void wait_for_journal_ready(Context *on_ready);
118
119 void open(Context *on_finish);
120 void close(Context *on_finish);
121
122 bool is_tag_owner() const;
123 uint64_t get_tag_tid() const;
124 journal::TagData get_tag_data() const;
125
126 void allocate_local_tag(Context *on_finish);
127 void allocate_tag(const std::string &mirror_uuid,
128 const journal::TagPredecessor &predecessor,
129 Context *on_finish);
130
131 void flush_commit_position(Context *on_finish);
132
133 uint64_t append_write_event(uint64_t offset, size_t length,
134 const bufferlist &bl,
135 bool flush_entry);
136 uint64_t append_io_event(journal::EventEntry &&event_entry,
137 uint64_t offset, size_t length,
138 bool flush_entry, int filter_ret_val);
139 void commit_io_event(uint64_t tid, int r);
140 void commit_io_event_extent(uint64_t tid, uint64_t offset, uint64_t length,
141 int r);
142
143 void append_op_event(uint64_t op_tid, journal::EventEntry &&event_entry,
144 Context *on_safe);
145 void commit_op_event(uint64_t tid, int r, Context *on_safe);
146 void replay_op_ready(uint64_t op_tid, Context *on_resume);
147
148 void flush_event(uint64_t tid, Context *on_safe);
149 void wait_event(uint64_t tid, Context *on_safe);
150
151 uint64_t allocate_op_tid() {
152 uint64_t op_tid = ++m_op_tid;
153 ceph_assert(op_tid != 0);
154 return op_tid;
155 }
156
157 void start_external_replay(journal::Replay<ImageCtxT> **journal_replay,
158 Context *on_start);
159 void stop_external_replay();
160
161 void add_listener(journal::Listener *listener);
162 void remove_listener(journal::Listener *listener);
163
164 int is_resync_requested(bool *do_resync);
165
166 inline ContextWQ *get_work_queue() {
167 return m_work_queue;
168 }
169
170 private:
171 ImageCtxT &m_image_ctx;
172
173 // mock unit testing support
174 typedef journal::TypeTraits<ImageCtxT> TypeTraits;
175 typedef typename TypeTraits::Journaler Journaler;
176 typedef typename TypeTraits::Future Future;
177 typedef typename TypeTraits::ReplayEntry ReplayEntry;
178
179 typedef std::list<bufferlist> Bufferlists;
180 typedef std::list<Context *> Contexts;
181 typedef std::list<Future> Futures;
182 typedef interval_set<uint64_t> ExtentInterval;
183
184 struct Event {
185 Futures futures;
186 Contexts on_safe_contexts;
187 ExtentInterval pending_extents;
188 int filter_ret_val = 0;
189 bool committed_io = false;
190 bool safe = false;
191 int ret_val = 0;
192
193 Event() {
194 }
195 Event(const Futures &_futures, uint64_t offset, size_t length,
196 int filter_ret_val)
197 : futures(_futures), filter_ret_val(filter_ret_val) {
198 if (length > 0) {
199 pending_extents.insert(offset, length);
200 }
201 }
202 };
203
204 typedef std::unordered_map<uint64_t, Event> Events;
205 typedef std::unordered_map<uint64_t, Future> TidToFutures;
206
207 struct C_IOEventSafe : public Context {
208 Journal *journal;
209 uint64_t tid;
210
211 C_IOEventSafe(Journal *_journal, uint64_t _tid)
212 : journal(_journal), tid(_tid) {
213 }
214
215 void finish(int r) override {
216 journal->handle_io_event_safe(r, tid);
217 }
218 };
219
220 struct C_OpEventSafe : public Context {
221 Journal *journal;
222 uint64_t tid;
223 Future op_start_future;
224 Future op_finish_future;
225 Context *on_safe;
226
227 C_OpEventSafe(Journal *journal, uint64_t tid, const Future &op_start_future,
228 const Future &op_finish_future, Context *on_safe)
229 : journal(journal), tid(tid), op_start_future(op_start_future),
230 op_finish_future(op_finish_future), on_safe(on_safe) {
231 }
232
233 void finish(int r) override {
234 journal->handle_op_event_safe(r, tid, op_start_future, op_finish_future,
235 on_safe);
236 }
237 };
238
239 struct C_ReplayProcessSafe : public Context {
240 Journal *journal;
241 ReplayEntry replay_entry;
242
243 C_ReplayProcessSafe(Journal *journal, ReplayEntry &&replay_entry) :
244 journal(journal), replay_entry(std::move(replay_entry)) {
245 }
246 void finish(int r) override {
247 journal->handle_replay_process_safe(replay_entry, r);
248 }
249 };
250
251 struct ReplayHandler : public ::journal::ReplayHandler {
252 Journal *journal;
253 ReplayHandler(Journal *_journal) : journal(_journal) {
254 }
255
256 void get() override {
257 // TODO
258 }
259 void put() override {
260 // TODO
261 }
262
263 void handle_entries_available() override {
264 journal->handle_replay_ready();
265 }
266 void handle_complete(int r) override {
267 journal->handle_replay_complete(r);
268 }
269 };
270
271 ContextWQ *m_work_queue = nullptr;
272 SafeTimer *m_timer = nullptr;
273 Mutex *m_timer_lock = nullptr;
274
275 Journaler *m_journaler;
276 mutable Mutex m_lock;
277 State m_state;
278 uint64_t m_max_append_size = 0;
279 uint64_t m_tag_class = 0;
280 uint64_t m_tag_tid = 0;
281 journal::ImageClientMeta m_client_meta;
282 journal::TagData m_tag_data;
283
284 int m_error_result;
285 Contexts m_wait_for_state_contexts;
286
287 ReplayHandler m_replay_handler;
288 bool m_close_pending;
289
290 Mutex m_event_lock;
291 uint64_t m_event_tid;
292 Events m_events;
293
294 std::atomic<uint64_t> m_op_tid = { 0 };
295 TidToFutures m_op_futures;
296
297 bool m_processing_entry = false;
298 bool m_blocking_writes;
299
300 journal::Replay<ImageCtxT> *m_journal_replay;
301
302 util::AsyncOpTracker m_async_journal_op_tracker;
303
304 struct MetadataListener : public ::journal::JournalMetadataListener {
305 Journal<ImageCtxT> *journal;
306
307 MetadataListener(Journal<ImageCtxT> *journal) : journal(journal) { }
308
309 void handle_update(::journal::JournalMetadata *) override {
310 FunctionContext *ctx = new FunctionContext([this](int r) {
311 journal->handle_metadata_updated();
312 });
313 journal->m_work_queue->queue(ctx, 0);
314 }
315 } m_metadata_listener;
316
317 typedef std::set<journal::Listener *> Listeners;
318 Listeners m_listeners;
319 Cond m_listener_cond;
320 bool m_listener_notify = false;
321
322 uint64_t m_refresh_sequence = 0;
323
324 bool is_journal_replaying(const Mutex &) const;
325 bool is_tag_owner(const Mutex &) const;
326
327 uint64_t append_io_events(journal::EventType event_type,
328 const Bufferlists &bufferlists,
329 uint64_t offset, size_t length, bool flush_entry,
330 int filter_ret_val);
331 Future wait_event(Mutex &lock, uint64_t tid, Context *on_safe);
332
333 void create_journaler();
334 void destroy_journaler(int r);
335 void recreate_journaler(int r);
336
337 void complete_event(typename Events::iterator it, int r);
338
339 void start_append();
340
341 void handle_open(int r);
342
343 void handle_replay_ready();
344 void handle_replay_complete(int r);
345 void handle_replay_process_ready(int r);
346 void handle_replay_process_safe(ReplayEntry replay_entry, int r);
347
348 void handle_start_external_replay(int r,
349 journal::Replay<ImageCtxT> **journal_replay,
350 Context *on_finish);
351
352 void handle_flushing_restart(int r);
353 void handle_flushing_replay();
354
355 void handle_recording_stopped(int r);
356
357 void handle_journal_destroyed(int r);
358
359 void handle_io_event_safe(int r, uint64_t tid);
360 void handle_op_event_safe(int r, uint64_t tid, const Future &op_start_future,
361 const Future &op_finish_future, Context *on_safe);
362
363 void stop_recording();
364
365 void transition_state(State state, int r);
366
367 bool is_steady_state() const;
368 void wait_for_steady_state(Context *on_state);
369
370 int check_resync_requested(bool *do_resync);
371
372 void handle_metadata_updated();
373 void handle_refresh_metadata(uint64_t refresh_sequence, uint64_t tag_tid,
374 journal::TagData tag_data, int r);
375
376 };
377
378 } // namespace librbd
379
380 extern template class librbd::Journal<librbd::ImageCtx>;
381
382 #endif // CEPH_LIBRBD_JOURNAL_H