]> git.proxmox.com Git - ceph.git/blob - ceph/src/os/filestore/JournalingObjectStore.cc
7679d802969ffb940b59d4a03ec3b03eb95bac06
[ceph.git] / ceph / src / os / filestore / JournalingObjectStore.cc
1 // -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*-
2
3 #include "JournalingObjectStore.h"
4
5 #include "common/errno.h"
6 #include "common/debug.h"
7
8 #define dout_context cct
9 #define dout_subsys ceph_subsys_journal
10 #undef dout_prefix
11 #define dout_prefix *_dout << "journal "
12
13
14
15 void JournalingObjectStore::journal_start()
16 {
17 dout(10) << "journal_start" << dendl;
18 finisher.start();
19 }
20
21 void JournalingObjectStore::journal_stop()
22 {
23 dout(10) << "journal_stop" << dendl;
24 finisher.wait_for_empty();
25 finisher.stop();
26 }
27
28 // A journal_replay() makes journal writeable, this closes that out.
29 void JournalingObjectStore::journal_write_close()
30 {
31 if (journal) {
32 journal->close();
33 delete journal;
34 journal = 0;
35 }
36 apply_manager.reset();
37 }
38
39 int JournalingObjectStore::journal_replay(uint64_t fs_op_seq)
40 {
41 dout(10) << "journal_replay fs op_seq " << fs_op_seq << dendl;
42
43 if (cct->_conf->journal_replay_from) {
44 dout(0) << "journal_replay forcing replay from "
45 << cct->_conf->journal_replay_from
46 << " instead of " << fs_op_seq << dendl;
47 // the previous op is the last one committed
48 fs_op_seq = cct->_conf->journal_replay_from - 1;
49 }
50
51 uint64_t op_seq = fs_op_seq;
52 apply_manager.init_seq(fs_op_seq);
53
54 if (!journal) {
55 submit_manager.set_op_seq(op_seq);
56 return 0;
57 }
58
59 int err = journal->open(op_seq);
60 if (err < 0) {
61 dout(3) << "journal_replay open failed with "
62 << cpp_strerror(err) << dendl;
63 delete journal;
64 journal = 0;
65 return err;
66 }
67
68 replaying = true;
69
70 int count = 0;
71 while (1) {
72 bufferlist bl;
73 uint64_t seq = op_seq + 1;
74 if (!journal->read_entry(bl, seq)) {
75 dout(3) << "journal_replay: end of journal, done." << dendl;
76 break;
77 }
78
79 if (seq <= op_seq) {
80 dout(3) << "journal_replay: skipping old op seq " << seq << " <= " << op_seq << dendl;
81 continue;
82 }
83 assert(op_seq == seq-1);
84
85 dout(3) << "journal_replay: applying op seq " << seq << dendl;
86 bufferlist::iterator p = bl.begin();
87 vector<ObjectStore::Transaction> tls;
88 while (!p.end()) {
89 tls.emplace_back(Transaction(p));
90 }
91
92 apply_manager.op_apply_start(seq);
93 int r = do_transactions(tls, seq);
94 apply_manager.op_apply_finish(seq);
95
96 op_seq = seq;
97 count++;
98
99 dout(3) << "journal_replay: r = " << r << ", op_seq now " << op_seq << dendl;
100 }
101
102 if (count)
103 dout(3) << "journal_replay: total = " << count << dendl;
104
105 replaying = false;
106
107 submit_manager.set_op_seq(op_seq);
108
109 // done reading, make writeable.
110 err = journal->make_writeable();
111 if (err < 0)
112 return err;
113
114 return count;
115 }
116
117
118 // ------------------------------------
119
120 uint64_t JournalingObjectStore::ApplyManager::op_apply_start(uint64_t op)
121 {
122 Mutex::Locker l(apply_lock);
123 while (blocked) {
124 dout(10) << "op_apply_start blocked, waiting" << dendl;
125 blocked_cond.Wait(apply_lock);
126 }
127 dout(10) << "op_apply_start " << op << " open_ops " << open_ops << " -> "
128 << (open_ops+1) << dendl;
129 assert(!blocked);
130 assert(op > committed_seq);
131 open_ops++;
132 return op;
133 }
134
135 void JournalingObjectStore::ApplyManager::op_apply_finish(uint64_t op)
136 {
137 Mutex::Locker l(apply_lock);
138 dout(10) << "op_apply_finish " << op << " open_ops " << open_ops << " -> "
139 << (open_ops-1) << ", max_applied_seq " << max_applied_seq << " -> "
140 << MAX(op, max_applied_seq) << dendl;
141 --open_ops;
142 assert(open_ops >= 0);
143
144 // signal a blocked commit_start
145 if (blocked) {
146 blocked_cond.Signal();
147 }
148
149 // there can be multiple applies in flight; track the max value we
150 // note. note that we can't _read_ this value and learn anything
151 // meaningful unless/until we've quiesced all in-flight applies.
152 if (op > max_applied_seq)
153 max_applied_seq = op;
154 }
155
156 uint64_t JournalingObjectStore::SubmitManager::op_submit_start()
157 {
158 lock.Lock();
159 uint64_t op = ++op_seq;
160 dout(10) << "op_submit_start " << op << dendl;
161 return op;
162 }
163
164 void JournalingObjectStore::SubmitManager::op_submit_finish(uint64_t op)
165 {
166 dout(10) << "op_submit_finish " << op << dendl;
167 if (op != op_submitted + 1) {
168 dout(0) << "op_submit_finish " << op << " expected " << (op_submitted + 1)
169 << ", OUT OF ORDER" << dendl;
170 assert(0 == "out of order op_submit_finish");
171 }
172 op_submitted = op;
173 lock.Unlock();
174 }
175
176
177 // ------------------------------------------
178
179 void JournalingObjectStore::ApplyManager::add_waiter(uint64_t op, Context *c)
180 {
181 Mutex::Locker l(com_lock);
182 assert(c);
183 commit_waiters[op].push_back(c);
184 }
185
186 bool JournalingObjectStore::ApplyManager::commit_start()
187 {
188 bool ret = false;
189
190 {
191 Mutex::Locker l(apply_lock);
192 dout(10) << "commit_start max_applied_seq " << max_applied_seq
193 << ", open_ops " << open_ops << dendl;
194 blocked = true;
195 while (open_ops > 0) {
196 dout(10) << "commit_start waiting for " << open_ops
197 << " open ops to drain" << dendl;
198 blocked_cond.Wait(apply_lock);
199 }
200 assert(open_ops == 0);
201 dout(10) << "commit_start blocked, all open_ops have completed" << dendl;
202 {
203 Mutex::Locker l(com_lock);
204 if (max_applied_seq == committed_seq) {
205 dout(10) << "commit_start nothing to do" << dendl;
206 blocked = false;
207 assert(commit_waiters.empty());
208 goto out;
209 }
210
211 committing_seq = max_applied_seq;
212
213 dout(10) << "commit_start committing " << committing_seq
214 << ", still blocked" << dendl;
215 }
216 }
217 ret = true;
218
219 if (journal)
220 journal->commit_start(committing_seq); // tell the journal too
221 out:
222 return ret;
223 }
224
225 void JournalingObjectStore::ApplyManager::commit_started()
226 {
227 Mutex::Locker l(apply_lock);
228 // allow new ops. (underlying fs should now be committing all prior ops)
229 dout(10) << "commit_started committing " << committing_seq << ", unblocking"
230 << dendl;
231 blocked = false;
232 blocked_cond.Signal();
233 }
234
235 void JournalingObjectStore::ApplyManager::commit_finish()
236 {
237 Mutex::Locker l(com_lock);
238 dout(10) << "commit_finish thru " << committing_seq << dendl;
239
240 if (journal)
241 journal->committed_thru(committing_seq);
242
243 committed_seq = committing_seq;
244
245 map<version_t, vector<Context*> >::iterator p = commit_waiters.begin();
246 while (p != commit_waiters.end() &&
247 p->first <= committing_seq) {
248 finisher.queue(p->second);
249 commit_waiters.erase(p++);
250 }
251 }
252
253 void JournalingObjectStore::_op_journal_transactions(
254 bufferlist& tbl, uint32_t orig_len, uint64_t op,
255 Context *onjournal, TrackedOpRef osd_op)
256 {
257 if (osd_op.get())
258 dout(10) << "op_journal_transactions " << op << " reqid_t "
259 << (static_cast<OpRequest *>(osd_op.get()))->get_reqid() << dendl;
260 else
261 dout(10) << "op_journal_transactions " << op << dendl;
262
263 if (journal && journal->is_writeable()) {
264 journal->submit_entry(op, tbl, orig_len, onjournal, osd_op);
265 } else if (onjournal) {
266 apply_manager.add_waiter(op, onjournal);
267 }
268 }