]> git.proxmox.com Git - ceph.git/blob - ceph/src/librbd/journal/Replay.h
update sources to v12.1.0
[ceph.git] / ceph / src / librbd / journal / Replay.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_REPLAY_H
5 #define CEPH_LIBRBD_JOURNAL_REPLAY_H
6
7 #include "include/int_types.h"
8 #include "include/buffer_fwd.h"
9 #include "include/Context.h"
10 #include "common/Mutex.h"
11 #include "librbd/io/Types.h"
12 #include "librbd/journal/Types.h"
13 #include <boost/variant.hpp>
14 #include <list>
15 #include <unordered_set>
16 #include <unordered_map>
17
18 namespace librbd {
19
20 class ImageCtx;
21 namespace io { struct AioCompletion; }
22
23 namespace journal {
24
25 template <typename ImageCtxT = ImageCtx>
26 class Replay {
27 public:
28 static Replay *create(ImageCtxT &image_ctx) {
29 return new Replay(image_ctx);
30 }
31
32 Replay(ImageCtxT &image_ctx);
33 ~Replay();
34
35 int decode(bufferlist::iterator *it, EventEntry *event_entry);
36 void process(const EventEntry &event_entry,
37 Context *on_ready, Context *on_safe);
38
39 void shut_down(bool cancel_ops, Context *on_finish);
40 void flush(Context *on_finish);
41
42 void replay_op_ready(uint64_t op_tid, Context *on_resume);
43
44 private:
45 typedef std::unordered_set<int> ReturnValues;
46
47 struct OpEvent {
48 bool op_in_progress = false;
49 bool finish_on_ready = false;
50 Context *on_op_finish_event = nullptr;
51 Context *on_start_ready = nullptr;
52 Context *on_start_safe = nullptr;
53 Context *on_finish_ready = nullptr;
54 Context *on_finish_safe = nullptr;
55 Context *on_op_complete = nullptr;
56 ReturnValues op_finish_error_codes;
57 ReturnValues ignore_error_codes;
58 };
59
60 typedef std::list<uint64_t> OpTids;
61 typedef std::list<Context *> Contexts;
62 typedef std::unordered_set<Context *> ContextSet;
63 typedef std::unordered_map<uint64_t, OpEvent> OpEvents;
64
65 struct C_OpOnComplete : public Context {
66 Replay *replay;
67 uint64_t op_tid;
68 C_OpOnComplete(Replay *replay, uint64_t op_tid)
69 : replay(replay), op_tid(op_tid) {
70 }
71 void finish(int r) override {
72 replay->handle_op_complete(op_tid, r);
73 }
74 };
75
76 struct C_AioModifyComplete : public Context {
77 Replay *replay;
78 Context *on_ready;
79 Context *on_safe;
80 C_AioModifyComplete(Replay *replay, Context *on_ready, Context *on_safe)
81 : replay(replay), on_ready(on_ready), on_safe(on_safe) {
82 }
83 void finish(int r) override {
84 replay->handle_aio_modify_complete(on_ready, on_safe, r);
85 }
86 };
87
88 struct C_AioFlushComplete : public Context {
89 Replay *replay;
90 Context *on_flush_safe;
91 Contexts on_safe_ctxs;
92 C_AioFlushComplete(Replay *replay, Context *on_flush_safe,
93 Contexts &&on_safe_ctxs)
94 : replay(replay), on_flush_safe(on_flush_safe),
95 on_safe_ctxs(on_safe_ctxs) {
96 }
97 void finish(int r) override {
98 replay->handle_aio_flush_complete(on_flush_safe, on_safe_ctxs, r);
99 }
100 };
101
102 struct EventVisitor : public boost::static_visitor<void> {
103 Replay *replay;
104 Context *on_ready;
105 Context *on_safe;
106
107 EventVisitor(Replay *_replay, Context *_on_ready, Context *_on_safe)
108 : replay(_replay), on_ready(_on_ready), on_safe(_on_safe) {
109 }
110
111 template <typename Event>
112 inline void operator()(const Event &event) const {
113 replay->handle_event(event, on_ready, on_safe);
114 }
115 };
116
117 ImageCtxT &m_image_ctx;
118
119 Mutex m_lock;
120
121 uint64_t m_in_flight_aio_flush = 0;
122 uint64_t m_in_flight_aio_modify = 0;
123 Contexts m_aio_modify_unsafe_contexts;
124 ContextSet m_aio_modify_safe_contexts;
125
126 OpEvents m_op_events;
127 uint64_t m_in_flight_op_events = 0;
128
129 bool m_shut_down = false;
130 Context *m_flush_ctx = nullptr;
131 Context *m_on_aio_ready = nullptr;
132
133 void handle_event(const AioDiscardEvent &event, Context *on_ready,
134 Context *on_safe);
135 void handle_event(const AioWriteEvent &event, Context *on_ready,
136 Context *on_safe);
137 void handle_event(const AioWriteSameEvent &event, Context *on_ready,
138 Context *on_safe);
139 void handle_event(const AioFlushEvent &event, Context *on_ready,
140 Context *on_safe);
141 void handle_event(const OpFinishEvent &event, Context *on_ready,
142 Context *on_safe);
143 void handle_event(const SnapCreateEvent &event, Context *on_ready,
144 Context *on_safe);
145 void handle_event(const SnapRemoveEvent &event, Context *on_ready,
146 Context *on_safe);
147 void handle_event(const SnapRenameEvent &event, Context *on_ready,
148 Context *on_safe);
149 void handle_event(const SnapProtectEvent &event, Context *on_ready,
150 Context *on_safe);
151 void handle_event(const SnapUnprotectEvent &event, Context *on_ready,
152 Context *on_safe);
153 void handle_event(const SnapRollbackEvent &event, Context *on_ready,
154 Context *on_safe);
155 void handle_event(const RenameEvent &event, Context *on_ready,
156 Context *on_safe);
157 void handle_event(const ResizeEvent &event, Context *on_ready,
158 Context *on_safe);
159 void handle_event(const FlattenEvent &event, Context *on_ready,
160 Context *on_safe);
161 void handle_event(const DemotePromoteEvent &event, Context *on_ready,
162 Context *on_safe);
163 void handle_event(const SnapLimitEvent &event, Context *on_ready,
164 Context *on_safe);
165 void handle_event(const UpdateFeaturesEvent &event, Context *on_ready,
166 Context *on_safe);
167 void handle_event(const MetadataSetEvent &event, Context *on_ready,
168 Context *on_safe);
169 void handle_event(const MetadataRemoveEvent &event, Context *on_ready,
170 Context *on_safe);
171 void handle_event(const UnknownEvent &event, Context *on_ready,
172 Context *on_safe);
173
174 void handle_aio_modify_complete(Context *on_ready, Context *on_safe, int r);
175 void handle_aio_flush_complete(Context *on_flush_safe, Contexts &on_safe_ctxs,
176 int r);
177
178 Context *create_op_context_callback(uint64_t op_tid, Context *on_ready,
179 Context *on_safe, OpEvent **op_event);
180 void handle_op_complete(uint64_t op_tid, int r);
181
182 io::AioCompletion *create_aio_modify_completion(Context *on_ready,
183 Context *on_safe,
184 io::aio_type_t aio_type,
185 bool *flush_required);
186 io::AioCompletion *create_aio_flush_completion(Context *on_safe);
187 void handle_aio_completion(io::AioCompletion *aio_comp);
188
189 };
190
191 } // namespace journal
192 } // namespace librbd
193
194 extern template class librbd::journal::Replay<librbd::ImageCtx>;
195
196 #endif // CEPH_LIBRBD_JOURNAL_REPLAY_H