1 // -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*-
2 // vim: ts=8 sw=2 smarttab
4 * Ceph - scalable distributed file system
6 * Copyright (C) 2004-2006 Sage Weil <sage@newdream.net>
8 * This is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License version 2.1, as published by the Free Software
11 * Foundation. See file COPYING.
15 #include "common/perf_counters.h"
16 #include "common/dout.h"
17 #include "include/Context.h"
18 #include "msg/Messenger.h"
19 #include "osdc/Journaler.h"
20 #include "common/errno.h"
21 #include "include/assert.h"
22 #include "common/Finisher.h"
24 #define dout_subsys ceph_subsys_journaler
26 #define dout_prefix *_dout << objecter->messenger->get_myname() \
27 << ".journaler." << name << (readonly ? "(ro) ":"(rw) ")
29 using std::chrono::seconds
;
32 class Journaler::C_DelayFlush
: public Context
{
35 C_DelayFlush(Journaler
*j
) : journaler(j
) {}
36 void finish(int r
) override
{
37 journaler
->_do_delayed_flush();
41 void Journaler::set_readonly()
45 ldout(cct
, 1) << "set_readonly" << dendl
;
49 void Journaler::set_writeable()
53 ldout(cct
, 1) << "set_writeable" << dendl
;
57 void Journaler::create(file_layout_t
*l
, stream_format_t
const sf
)
65 journal_stream
.set_format(sf
);
68 prezeroing_pos
= prezero_pos
= write_pos
= flush_pos
=
69 safe_pos
= read_pos
= requested_pos
= received_pos
=
70 expire_pos
= trimming_pos
= trimmed_pos
=
71 next_safe_pos
= layout
.get_period();
73 ldout(cct
, 1) << "created blank journal at inode 0x" << std::hex
<< ino
74 << std::dec
<< ", format=" << stream_format
<< dendl
;
77 void Journaler::set_layout(file_layout_t
const *l
)
83 void Journaler::_set_layout(file_layout_t
const *l
)
87 if (layout
.pool_id
!= pg_pool
) {
88 // user can reset pool id through cephfs-journal-tool
89 lderr(cct
) << "may got older pool id from header layout" << dendl
;
92 last_written
.layout
= layout
;
93 last_committed
.layout
= layout
;
95 // prefetch intelligently.
96 // (watch out, this is big if you use big objects or weird striping)
97 uint64_t periods
= cct
->_conf
->journaler_prefetch_periods
;
99 periods
= 2; // we need at least 2 periods to make progress.
100 fetch_len
= layout
.get_period() * periods
;
104 /***************** HEADER *******************/
106 ostream
& operator<<(ostream
&out
, const Journaler::Header
&h
)
108 return out
<< "loghead(trim " << h
.trimmed_pos
109 << ", expire " << h
.expire_pos
110 << ", write " << h
.write_pos
111 << ", stream_format " << (int)(h
.stream_format
)
115 class Journaler::C_ReadHead
: public Context
{
119 explicit C_ReadHead(Journaler
*l
) : ls(l
) {}
120 void finish(int r
) override
{
121 ls
->_finish_read_head(r
, bl
);
125 class Journaler::C_RereadHead
: public Context
{
130 C_RereadHead(Journaler
*l
, Context
*onfinish_
) : ls (l
),
131 onfinish(onfinish_
) {}
132 void finish(int r
) override
{
133 ls
->_finish_reread_head(r
, bl
, onfinish
);
137 class Journaler::C_ProbeEnd
: public Context
{
141 explicit C_ProbeEnd(Journaler
*l
) : ls(l
), end(-1) {}
142 void finish(int r
) override
{
143 ls
->_finish_probe_end(r
, end
);
147 class Journaler::C_ReProbe
: public Context
{
149 C_OnFinisher
*onfinish
;
152 C_ReProbe(Journaler
*l
, C_OnFinisher
*onfinish_
) :
153 ls(l
), onfinish(onfinish_
), end(0) {}
154 void finish(int r
) override
{
155 ls
->_finish_reprobe(r
, end
, onfinish
);
159 void Journaler::recover(Context
*onread
)
163 onread
->complete(-EAGAIN
);
167 ldout(cct
, 1) << "recover start" << dendl
;
168 assert(state
!= STATE_ACTIVE
);
172 waitfor_recover
.push_back(wrap_finisher(onread
));
174 if (state
!= STATE_UNDEF
) {
175 ldout(cct
, 1) << "recover - already recovering" << dendl
;
179 ldout(cct
, 1) << "read_head" << dendl
;
180 state
= STATE_READHEAD
;
181 C_ReadHead
*fin
= new C_ReadHead(this);
182 _read_head(fin
, &fin
->bl
);
185 void Journaler::_read_head(Context
*on_finish
, bufferlist
*bl
)
188 assert(state
== STATE_READHEAD
|| state
== STATE_REREADHEAD
);
190 object_t oid
= file_object_t(ino
, 0);
191 object_locator_t
oloc(pg_pool
);
192 objecter
->read_full(oid
, oloc
, CEPH_NOSNAP
, bl
, 0, wrap_finisher(on_finish
));
195 void Journaler::reread_head(Context
*onfinish
)
198 _reread_head(wrap_finisher(onfinish
));
202 * Re-read the head from disk, and set the write_pos, expire_pos, trimmed_pos
203 * from the on-disk header. This switches the state to STATE_REREADHEAD for
204 * the duration, and you shouldn't start a re-read while other operations are
205 * in-flight, nor start other operations while a re-read is in progress.
206 * Also, don't call this until the Journaler has finished its recovery and has
209 void Journaler::_reread_head(Context
*onfinish
)
211 ldout(cct
, 10) << "reread_head" << dendl
;
212 assert(state
== STATE_ACTIVE
);
214 state
= STATE_REREADHEAD
;
215 C_RereadHead
*fin
= new C_RereadHead(this, onfinish
);
216 _read_head(fin
, &fin
->bl
);
219 void Journaler::_finish_reread_head(int r
, bufferlist
& bl
, Context
*finish
)
223 finish
->complete(-EAGAIN
);
227 //read on-disk header into
228 assert(bl
.length() || r
< 0 );
233 bufferlist::iterator p
= bl
.begin();
236 } catch (const buffer::error
&e
) {
237 finish
->complete(-EINVAL
);
240 prezeroing_pos
= prezero_pos
= write_pos
= flush_pos
= safe_pos
= next_safe_pos
242 expire_pos
= h
.expire_pos
;
243 trimmed_pos
= trimming_pos
= h
.trimmed_pos
;
245 state
= STATE_ACTIVE
;
251 void Journaler::_finish_read_head(int r
, bufferlist
& bl
)
257 assert(state
== STATE_READHEAD
);
260 ldout(cct
, 0) << "error getting journal off disk" << dendl
;
262 ls
.swap(waitfor_recover
);
263 finish_contexts(cct
, ls
, r
);
267 if (bl
.length() == 0) {
268 ldout(cct
, 1) << "_finish_read_head r=" << r
269 << " read 0 bytes, assuming empty log" << dendl
;
270 state
= STATE_ACTIVE
;
272 ls
.swap(waitfor_recover
);
273 finish_contexts(cct
, ls
, 0);
278 bool corrupt
= false;
280 bufferlist::iterator p
= bl
.begin();
284 if (h
.magic
!= magic
) {
285 ldout(cct
, 0) << "on disk magic '" << h
.magic
<< "' != my magic '"
286 << magic
<< "'" << dendl
;
288 } else if (h
.write_pos
< h
.expire_pos
|| h
.expire_pos
< h
.trimmed_pos
) {
289 ldout(cct
, 0) << "Corrupt header (bad offsets): " << h
<< dendl
;
292 } catch (const buffer::error
&e
) {
298 ls
.swap(waitfor_recover
);
299 finish_contexts(cct
, ls
, -EINVAL
);
303 prezeroing_pos
= prezero_pos
= write_pos
= flush_pos
= safe_pos
= next_safe_pos
305 read_pos
= requested_pos
= received_pos
= expire_pos
= h
.expire_pos
;
306 trimmed_pos
= trimming_pos
= h
.trimmed_pos
;
309 _set_layout(&h
.layout
);
310 stream_format
= h
.stream_format
;
311 journal_stream
.set_format(h
.stream_format
);
313 ldout(cct
, 1) << "_finish_read_head " << h
314 << ". probing for end of log (from " << write_pos
<< ")..."
316 C_ProbeEnd
*fin
= new C_ProbeEnd(this);
317 state
= STATE_PROBING
;
318 _probe(fin
, &fin
->end
);
321 void Journaler::_probe(Context
*finish
, uint64_t *end
)
324 ldout(cct
, 1) << "probing for end of the log" << dendl
;
325 assert(state
== STATE_PROBING
|| state
== STATE_REPROBING
);
327 filer
.probe(ino
, &layout
, CEPH_NOSNAP
,
328 write_pos
, end
, true, 0, wrap_finisher(finish
));
331 void Journaler::_reprobe(C_OnFinisher
*finish
)
333 ldout(cct
, 10) << "reprobe" << dendl
;
334 assert(state
== STATE_ACTIVE
);
336 state
= STATE_REPROBING
;
337 C_ReProbe
*fin
= new C_ReProbe(this, finish
);
338 _probe(fin
, &fin
->end
);
342 void Journaler::_finish_reprobe(int r
, uint64_t new_end
,
343 C_OnFinisher
*onfinish
)
347 onfinish
->complete(-EAGAIN
);
351 assert(new_end
>= write_pos
|| r
< 0);
352 ldout(cct
, 1) << "_finish_reprobe new_end = " << new_end
353 << " (header had " << write_pos
<< ")."
355 prezeroing_pos
= prezero_pos
= write_pos
= flush_pos
= safe_pos
= next_safe_pos
= new_end
;
356 state
= STATE_ACTIVE
;
357 onfinish
->complete(r
);
360 void Journaler::_finish_probe_end(int r
, uint64_t end
)
366 assert(state
== STATE_PROBING
);
367 if (r
< 0) { // error in probing
370 if (((int64_t)end
) == -1) {
372 ldout(cct
, 1) << "_finish_probe_end write_pos = " << end
<< " (header had "
373 << write_pos
<< "). log was empty. recovered." << dendl
;
374 ceph_abort(); // hrm.
376 assert(end
>= write_pos
);
377 ldout(cct
, 1) << "_finish_probe_end write_pos = " << end
378 << " (header had " << write_pos
<< "). recovered."
382 state
= STATE_ACTIVE
;
384 prezeroing_pos
= prezero_pos
= write_pos
= flush_pos
= safe_pos
= next_safe_pos
= end
;
389 ls
.swap(waitfor_recover
);
390 finish_contexts(cct
, ls
, r
);
393 class Journaler::C_RereadHeadProbe
: public Context
396 C_OnFinisher
*final_finish
;
398 C_RereadHeadProbe(Journaler
*l
, C_OnFinisher
*finish
) :
399 ls(l
), final_finish(finish
) {}
400 void finish(int r
) override
{
401 ls
->_finish_reread_head_and_probe(r
, final_finish
);
405 void Journaler::reread_head_and_probe(Context
*onfinish
)
409 assert(state
== STATE_ACTIVE
);
410 _reread_head(new C_RereadHeadProbe(this, wrap_finisher(onfinish
)));
413 void Journaler::_finish_reread_head_and_probe(int r
, C_OnFinisher
*onfinish
)
415 // Expect to be called back from finish_reread_head, which already takes lock
418 onfinish
->complete(-EAGAIN
);
422 assert(!r
); //if we get an error, we're boned
429 class Journaler::C_WriteHead
: public Context
{
433 C_OnFinisher
*oncommit
;
434 C_WriteHead(Journaler
*l
, Header
& h_
, C_OnFinisher
*c
) : ls(l
), h(h_
),
436 void finish(int r
) override
{
437 ls
->_finish_write_head(r
, h
, oncommit
);
441 void Journaler::write_head(Context
*oncommit
)
444 _write_head(oncommit
);
448 void Journaler::_write_head(Context
*oncommit
)
451 assert(state
== STATE_ACTIVE
);
452 last_written
.trimmed_pos
= trimmed_pos
;
453 last_written
.expire_pos
= expire_pos
;
454 last_written
.unused_field
= expire_pos
;
455 last_written
.write_pos
= safe_pos
;
456 last_written
.stream_format
= stream_format
;
457 ldout(cct
, 10) << "write_head " << last_written
<< dendl
;
459 // Avoid persisting bad pointers in case of bugs
460 assert(last_written
.write_pos
>= last_written
.expire_pos
);
461 assert(last_written
.expire_pos
>= last_written
.trimmed_pos
);
463 last_wrote_head
= ceph::real_clock::now();
466 ::encode(last_written
, bl
);
469 object_t oid
= file_object_t(ino
, 0);
470 object_locator_t
oloc(pg_pool
);
471 objecter
->write_full(oid
, oloc
, snapc
, bl
, ceph::real_clock::now(), 0,
472 wrap_finisher(new C_WriteHead(
474 wrap_finisher(oncommit
))),
478 void Journaler::_finish_write_head(int r
, Header
&wrote
,
479 C_OnFinisher
*oncommit
)
484 lderr(cct
) << "_finish_write_head got " << cpp_strerror(r
) << dendl
;
485 handle_write_error(r
);
489 ldout(cct
, 10) << "_finish_write_head " << wrote
<< dendl
;
490 last_committed
= wrote
;
492 oncommit
->complete(r
);
499 /***************** WRITING *******************/
501 class Journaler::C_Flush
: public Context
{
504 ceph::real_time stamp
;
506 C_Flush(Journaler
*l
, int64_t s
, ceph::real_time st
)
507 : ls(l
), start(s
), stamp(st
) {}
508 void finish(int r
) override
{
509 ls
->_finish_flush(r
, start
, stamp
);
513 void Journaler::_finish_flush(int r
, uint64_t start
, ceph::real_time stamp
)
519 lderr(cct
) << "_finish_flush got " << cpp_strerror(r
) << dendl
;
520 handle_write_error(r
);
524 assert(start
< flush_pos
);
528 ceph::timespan lat
= ceph::real_clock::now() - stamp
;
529 logger
->tinc(logger_key_lat
, lat
);
533 auto it
= pending_safe
.find(start
);
534 assert(it
!= pending_safe
.end());
535 pending_safe
.erase(it
);
536 if (pending_safe
.empty())
537 safe_pos
= next_safe_pos
;
539 safe_pos
= pending_safe
.begin()->second
;
541 ldout(cct
, 10) << "_finish_flush safe from " << start
542 << ", pending_safe " << pending_safe
543 << ", (prezeroing/prezero)/write/flush/safe positions now "
544 << "(" << prezeroing_pos
<< "/" << prezero_pos
<< ")/"
545 << write_pos
<< "/" << flush_pos
<< "/" << safe_pos
548 // kick waiters <= safe_pos
549 if (!waitfor_safe
.empty()) {
551 while (!waitfor_safe
.empty()) {
552 auto it
= waitfor_safe
.begin();
553 if (it
->first
> safe_pos
)
555 ls
.splice(ls
.end(), it
->second
);
556 waitfor_safe
.erase(it
);
558 finish_contexts(cct
, ls
);
564 uint64_t Journaler::append_entry(bufferlist
& bl
)
569 uint32_t s
= bl
.length();
572 size_t delta
= bl
.length() + journal_stream
.get_envelope_size();
573 // write_buf space is nearly full
574 if (!write_buf_throttle
.get_or_fail(delta
)) {
576 ldout(cct
, 10) << "write_buf_throttle wait, delta " << delta
<< dendl
;
577 write_buf_throttle
.get(delta
);
580 ldout(cct
, 20) << "write_buf_throttle get, delta " << delta
<< dendl
;
581 size_t wrote
= journal_stream
.write(bl
, &write_buf
, write_pos
);
582 ldout(cct
, 10) << "append_entry len " << s
<< " to " << write_pos
<< "~"
586 // flush previous object?
587 uint64_t su
= get_layout_period();
589 uint64_t write_off
= write_pos
% su
;
590 uint64_t write_obj
= write_pos
/ su
;
591 uint64_t flush_obj
= flush_pos
/ su
;
592 if (write_obj
!= flush_obj
) {
593 ldout(cct
, 10) << " flushing completed object(s) (su " << su
<< " wro "
594 << write_obj
<< " flo " << flush_obj
<< ")" << dendl
;
595 _do_flush(write_buf
.length() - write_off
);
597 // if _do_flush() skips flushing some data, it does do a best effort to
598 // update next_safe_pos.
599 if (write_buf
.length() > 0 &&
600 write_buf
.length() <= wrote
) { // the unflushed data are within this entry
601 // set next_safe_pos to end of previous entry
602 next_safe_pos
= write_pos
- wrote
;
610 void Journaler::_do_flush(unsigned amount
)
614 if (write_pos
== flush_pos
)
616 assert(write_pos
> flush_pos
);
620 uint64_t len
= write_pos
- flush_pos
;
621 assert(len
== write_buf
.length());
622 if (amount
&& amount
< len
)
625 // zero at least two full periods ahead. this ensures
626 // that the next object will not exist.
627 uint64_t period
= get_layout_period();
628 if (flush_pos
+ len
+ 2*period
> prezero_pos
) {
631 int64_t newlen
= prezero_pos
- flush_pos
- period
;
633 ldout(cct
, 10) << "_do_flush wanted to do " << flush_pos
<< "~" << len
634 << " already too close to prezero_pos " << prezero_pos
635 << ", zeroing first" << dendl
;
636 waiting_for_zero_pos
= flush_pos
+ len
;
639 if (static_cast<uint64_t>(newlen
) < len
) {
640 ldout(cct
, 10) << "_do_flush wanted to do " << flush_pos
<< "~" << len
641 << " but hit prezero_pos " << prezero_pos
642 << ", will do " << flush_pos
<< "~" << newlen
<< dendl
;
643 waiting_for_zero_pos
= flush_pos
+ len
;
647 ldout(cct
, 10) << "_do_flush flushing " << flush_pos
<< "~" << len
<< dendl
;
649 // submit write for anything pending
650 // flush _start_ pos to _finish_flush
651 ceph::real_time now
= ceph::real_clock::now();
654 Context
*onsafe
= new C_Flush(this, flush_pos
, now
); // on COMMIT
655 pending_safe
[flush_pos
] = next_safe_pos
;
660 if (len
== write_buf
.length()) {
661 write_bl
.swap(write_buf
);
662 next_safe_pos
= write_pos
;
664 write_buf
.splice(0, len
, &write_bl
);
665 // Keys of waitfor_safe map are journal entry boundaries.
666 // Try finding a journal entry that we are actually flushing
667 // and set next_safe_pos to end of it. This is best effort.
668 // The one we found may not be the lastest flushing entry.
669 auto p
= waitfor_safe
.lower_bound(flush_pos
+ len
);
670 if (p
!= waitfor_safe
.end()) {
671 if (p
->first
> flush_pos
+ len
&& p
!= waitfor_safe
.begin())
673 if (p
->first
<= flush_pos
+ len
&& p
->first
> next_safe_pos
)
674 next_safe_pos
= p
->first
;
678 filer
.write(ino
, &layout
, snapc
,
679 flush_pos
, len
, write_bl
, ceph::real_clock::now(),
681 wrap_finisher(onsafe
), write_iohint
);
684 assert(write_buf
.length() == write_pos
- flush_pos
);
685 write_buf_throttle
.put(len
);
686 ldout(cct
, 20) << "write_buf_throttle put, len " << len
<< dendl
;
689 << "_do_flush (prezeroing/prezero)/write/flush/safe pointers now at "
690 << "(" << prezeroing_pos
<< "/" << prezero_pos
<< ")/" << write_pos
691 << "/" << flush_pos
<< "/" << safe_pos
<< dendl
;
697 void Journaler::wait_for_flush(Context
*onsafe
)
701 onsafe
->complete(-EAGAIN
);
704 _wait_for_flush(onsafe
);
707 void Journaler::_wait_for_flush(Context
*onsafe
)
711 // all flushed and safe?
712 if (write_pos
== safe_pos
) {
713 assert(write_buf
.length() == 0);
715 << "flush nothing to flush, (prezeroing/prezero)/write/flush/safe "
716 "pointers at " << "(" << prezeroing_pos
<< "/" << prezero_pos
<< ")/"
717 << write_pos
<< "/" << flush_pos
<< "/" << safe_pos
<< dendl
;
719 finisher
->queue(onsafe
, 0);
726 waitfor_safe
[write_pos
].push_back(wrap_finisher(onsafe
));
730 void Journaler::flush(Context
*onsafe
)
734 onsafe
->complete(-EAGAIN
);
737 _flush(wrap_finisher(onsafe
));
740 void Journaler::_flush(C_OnFinisher
*onsafe
)
744 if (write_pos
== flush_pos
) {
745 assert(write_buf
.length() == 0);
746 ldout(cct
, 10) << "flush nothing to flush, (prezeroing/prezero)/write/"
747 "flush/safe pointers at " << "(" << prezeroing_pos
<< "/" << prezero_pos
748 << ")/" << write_pos
<< "/" << flush_pos
<< "/" << safe_pos
755 _wait_for_flush(onsafe
);
759 if (_write_head_needed()) {
764 bool Journaler::_write_head_needed()
766 return last_wrote_head
+ seconds(cct
->_conf
->journaler_write_head_interval
)
767 < ceph::real_clock::now();
771 /*************** prezeroing ******************/
773 struct C_Journaler_Prezero
: public Context
{
774 Journaler
*journaler
;
776 C_Journaler_Prezero(Journaler
*j
, uint64_t f
, uint64_t l
)
777 : journaler(j
), from(f
), len(l
) {}
778 void finish(int r
) override
{
779 journaler
->_finish_prezero(r
, from
, len
);
783 void Journaler::_issue_prezero()
785 assert(prezeroing_pos
>= flush_pos
);
787 // we need to zero at least two periods, minimum, to ensure that we
788 // have a full empty object/period in front of us.
789 uint64_t num_periods
= MAX(2, cct
->_conf
->journaler_prezero_periods
);
792 * issue zero requests based on write_pos, even though the invariant
793 * is that we zero ahead of flush_pos.
795 uint64_t period
= get_layout_period();
796 uint64_t to
= write_pos
+ period
* num_periods
+ period
- 1;
799 if (prezeroing_pos
>= to
) {
800 ldout(cct
, 20) << "_issue_prezero target " << to
<< " <= prezeroing_pos "
801 << prezeroing_pos
<< dendl
;
805 while (prezeroing_pos
< to
) {
807 if (prezeroing_pos
% period
== 0) {
809 ldout(cct
, 10) << "_issue_prezero removing " << prezeroing_pos
<< "~"
810 << period
<< " (full period)" << dendl
;
812 len
= period
- (prezeroing_pos
% period
);
813 ldout(cct
, 10) << "_issue_prezero zeroing " << prezeroing_pos
<< "~"
814 << len
<< " (partial period)" << dendl
;
817 Context
*c
= wrap_finisher(new C_Journaler_Prezero(this, prezeroing_pos
,
819 filer
.zero(ino
, &layout
, snapc
, prezeroing_pos
, len
,
820 ceph::real_clock::now(), 0, c
);
821 prezeroing_pos
+= len
;
825 // Lock cycle because we get called out of objecter callback (holding
826 // objecter read lock), but there are also cases where we take the journaler
827 // lock before calling into objecter to do I/O.
828 void Journaler::_finish_prezero(int r
, uint64_t start
, uint64_t len
)
832 ldout(cct
, 10) << "_prezeroed to " << start
<< "~" << len
833 << ", prezeroing/prezero was " << prezeroing_pos
<< "/"
834 << prezero_pos
<< ", pending " << pending_zero
836 if (r
< 0 && r
!= -ENOENT
) {
837 lderr(cct
) << "_prezeroed got " << cpp_strerror(r
) << dendl
;
838 handle_write_error(r
);
842 assert(r
== 0 || r
== -ENOENT
);
844 if (start
== prezero_pos
) {
846 while (!pending_zero
.empty() &&
847 pending_zero
.begin().get_start() == prezero_pos
) {
848 interval_set
<uint64_t>::iterator
b(pending_zero
.begin());
849 prezero_pos
+= b
.get_len();
850 pending_zero
.erase(b
);
853 if (waiting_for_zero_pos
> flush_pos
) {
854 _do_flush(waiting_for_zero_pos
- flush_pos
);
857 pending_zero
.insert(start
, len
);
859 ldout(cct
, 10) << "_prezeroed prezeroing/prezero now " << prezeroing_pos
860 << "/" << prezero_pos
861 << ", pending " << pending_zero
867 /***************** READING *******************/
870 class Journaler::C_Read
: public Context
{
876 C_Read(Journaler
*j
, uint64_t o
, uint64_t l
) : ls(j
), offset(o
), length(l
) {}
877 void finish(int r
) override
{
878 ls
->_finish_read(r
, offset
, length
, bl
);
882 class Journaler::C_RetryRead
: public Context
{
885 explicit C_RetryRead(Journaler
*l
) : ls(l
) {}
887 void finish(int r
) override
{
888 // Should only be called from waitfor_safe i.e. already inside lock
889 // (ls->lock is locked
894 void Journaler::_finish_read(int r
, uint64_t offset
, uint64_t length
,
900 ldout(cct
, 0) << "_finish_read got error " << r
<< dendl
;
903 ldout(cct
, 10) << "_finish_read got " << offset
<< "~" << bl
.length()
905 if (bl
.length() < length
) {
906 ldout(cct
, 0) << "_finish_read got less than expected (" << length
<< ")"
914 C_OnFinisher
*f
= on_readable
;
921 prefetch_buf
[offset
].swap(bl
);
924 _assimilate_prefetch();
925 } catch (const buffer::error
&err
) {
926 lderr(cct
) << "_decode error from assimilate_prefetch" << dendl
;
929 C_OnFinisher
*f
= on_readable
;
938 void Journaler::_assimilate_prefetch()
940 bool was_readable
= readable
;
942 bool got_any
= false;
943 while (!prefetch_buf
.empty()) {
944 map
<uint64_t,bufferlist
>::iterator p
= prefetch_buf
.begin();
945 if (p
->first
!= received_pos
) {
946 uint64_t gap
= p
->first
- received_pos
;
947 ldout(cct
, 10) << "_assimilate_prefetch gap of " << gap
948 << " from received_pos " << received_pos
949 << " to first prefetched buffer " << p
->first
<< dendl
;
953 ldout(cct
, 10) << "_assimilate_prefetch " << p
->first
<< "~"
954 << p
->second
.length() << dendl
;
955 received_pos
+= p
->second
.length();
956 read_buf
.claim_append(p
->second
);
957 assert(received_pos
<= requested_pos
);
958 prefetch_buf
.erase(p
);
963 ldout(cct
, 10) << "_assimilate_prefetch read_buf now " << read_pos
<< "~"
964 << read_buf
.length() << ", read pointers " << read_pos
965 << "/" << received_pos
<< "/" << requested_pos
968 // Update readability (this will also hit any decode errors resulting
970 readable
= _is_readable();
973 if ((got_any
&& !was_readable
&& readable
) || read_pos
== write_pos
) {
975 ldout(cct
, 10) << "_finish_read now readable (or at journal end) readable="
976 << readable
<< " read_pos=" << read_pos
<< " write_pos="
977 << write_pos
<< dendl
;
979 C_OnFinisher
*f
= on_readable
;
986 void Journaler::_issue_read(uint64_t len
)
988 // stuck at safe_pos? (this is needed if we are reading the tail of
989 // a journal we are also writing to)
990 assert(requested_pos
<= safe_pos
);
991 if (requested_pos
== safe_pos
) {
992 ldout(cct
, 10) << "_issue_read requested_pos = safe_pos = " << safe_pos
993 << ", waiting" << dendl
;
994 assert(write_pos
> requested_pos
);
995 if (pending_safe
.empty()) {
999 // Make sure keys of waitfor_safe map are journal entry boundaries.
1000 // The key we used here is either next_safe_pos or old value of
1001 // next_safe_pos. next_safe_pos is always set to journal entry
1003 auto p
= pending_safe
.rbegin();
1004 if (p
!= pending_safe
.rend())
1005 waitfor_safe
[p
->second
].push_back(new C_RetryRead(this));
1007 waitfor_safe
[next_safe_pos
].push_back(new C_RetryRead(this));
1011 // don't read too much
1012 if (requested_pos
+ len
> safe_pos
) {
1013 len
= safe_pos
- requested_pos
;
1014 ldout(cct
, 10) << "_issue_read reading only up to safe_pos " << safe_pos
1019 ldout(cct
, 10) << "_issue_read reading " << requested_pos
<< "~" << len
1020 << ", read pointers " << read_pos
<< "/" << received_pos
1021 << "/" << (requested_pos
+len
) << dendl
;
1023 // step by period (object). _don't_ do a single big filer.read()
1024 // here because it will wait for all object reads to complete before
1025 // giving us back any data. this way we can process whatever bits
1026 // come in that are contiguous.
1027 uint64_t period
= get_layout_period();
1029 uint64_t e
= requested_pos
+ period
;
1031 uint64_t l
= e
- requested_pos
;
1034 C_Read
*c
= new C_Read(this, requested_pos
, l
);
1035 filer
.read(ino
, &layout
, CEPH_NOSNAP
, requested_pos
, l
, &c
->bl
, 0,
1036 wrap_finisher(c
), CEPH_OSD_OP_FLAG_FADVISE_DONTNEED
);
1042 void Journaler::_prefetch()
1047 ldout(cct
, 10) << "_prefetch" << dendl
;
1050 if (temp_fetch_len
) {
1051 ldout(cct
, 10) << "_prefetch temp_fetch_len " << temp_fetch_len
<< dendl
;
1052 pf
= temp_fetch_len
;
1058 uint64_t raw_target
= read_pos
+ pf
;
1060 // read full log segments, so increase if necessary
1061 uint64_t period
= get_layout_period();
1062 uint64_t remainder
= raw_target
% period
;
1063 uint64_t adjustment
= remainder
? period
- remainder
: 0;
1064 uint64_t target
= raw_target
+ adjustment
;
1066 // don't read past the log tail
1067 if (target
> write_pos
)
1070 if (requested_pos
< target
) {
1071 uint64_t len
= target
- requested_pos
;
1072 ldout(cct
, 10) << "_prefetch " << pf
<< " requested_pos " << requested_pos
1073 << " < target " << target
<< " (" << raw_target
1074 << "), prefetching " << len
<< dendl
;
1076 if (pending_safe
.empty() && write_pos
> safe_pos
) {
1077 // If we are reading and writing the journal, then we may need
1078 // to issue a flush if one isn't already in progress.
1079 // Avoid doing a flush every time so that if we do write/read/write/read
1080 // we don't end up flushing after every write.
1081 ldout(cct
, 10) << "_prefetch: requested_pos=" << requested_pos
1082 << ", read_pos=" << read_pos
1083 << ", write_pos=" << write_pos
1084 << ", safe_pos=" << safe_pos
<< dendl
;
1094 * _is_readable() - return true if next entry is ready.
1096 bool Journaler::_is_readable()
1098 // anything to read?
1099 if (read_pos
== write_pos
)
1102 // Check if the retrieve bytestream has enough for an entry
1104 if (journal_stream
.readable(read_buf
, &need
)) {
1108 ldout (cct
, 10) << "_is_readable read_buf.length() == " << read_buf
.length()
1109 << ", but need " << need
<< " for next entry; fetch_len is "
1110 << fetch_len
<< dendl
;
1112 // partial fragment at the end?
1113 if (received_pos
== write_pos
) {
1114 ldout(cct
, 10) << "is_readable() detected partial entry at tail, "
1115 "adjusting write_pos to " << read_pos
<< dendl
;
1118 prezeroing_pos
= prezero_pos
= write_pos
= flush_pos
= safe_pos
= next_safe_pos
= read_pos
;
1119 assert(write_buf
.length() == 0);
1120 assert(waitfor_safe
.empty());
1123 requested_pos
= received_pos
= read_pos
;
1126 // FIXME: truncate on disk?
1131 if (need
> fetch_len
) {
1132 temp_fetch_len
= need
;
1133 ldout(cct
, 10) << "_is_readable noting temp_fetch_len " << temp_fetch_len
1137 ldout(cct
, 10) << "_is_readable: not readable, returning false" << dendl
;
1142 * is_readable() - kickstart prefetch, too
1144 bool Journaler::is_readable()
1157 class Journaler::C_EraseFinish
: public Context
{
1158 Journaler
*journaler
;
1159 C_OnFinisher
*completion
;
1161 C_EraseFinish(Journaler
*j
, C_OnFinisher
*c
) : journaler(j
), completion(c
) {}
1162 void finish(int r
) override
{
1163 journaler
->_finish_erase(r
, completion
);
1168 * Entirely erase the journal, including header. For use when you
1169 * have already made a copy of the journal somewhere else.
1171 void Journaler::erase(Context
*completion
)
1175 // Async delete the journal data
1176 uint64_t first
= trimmed_pos
/ get_layout_period();
1177 uint64_t num
= (write_pos
- trimmed_pos
) / get_layout_period() + 2;
1178 filer
.purge_range(ino
, &layout
, SnapContext(), first
, num
,
1179 ceph::real_clock::now(), 0,
1180 wrap_finisher(new C_EraseFinish(
1181 this, wrap_finisher(completion
))));
1183 // We will not start the operation to delete the header until
1184 // _finish_erase has seen the data deletion succeed: otherwise if
1185 // there was an error deleting data we might prematurely delete the
1186 // header thereby lose our reference to the data.
1189 void Journaler::_finish_erase(int data_result
, C_OnFinisher
*completion
)
1192 if (is_stopping()) {
1193 completion
->complete(-EAGAIN
);
1197 if (data_result
== 0) {
1198 // Async delete the journal header
1199 filer
.purge_range(ino
, &layout
, SnapContext(), 0, 1,
1200 ceph::real_clock::now(),
1201 0, wrap_finisher(completion
));
1203 lderr(cct
) << "Failed to delete journal " << ino
<< " data: "
1204 << cpp_strerror(data_result
) << dendl
;
1205 completion
->complete(data_result
);
1209 /* try_read_entry(bl)
1210 * read entry into bl if it's ready.
1211 * otherwise, do nothing.
1213 bool Journaler::try_read_entry(bufferlist
& bl
)
1218 ldout(cct
, 10) << "try_read_entry at " << read_pos
<< " not readable"
1226 consumed
= journal_stream
.read(read_buf
, &bl
, &start_ptr
);
1227 if (stream_format
>= JOURNAL_FORMAT_RESILIENT
) {
1228 assert(start_ptr
== read_pos
);
1230 } catch (const buffer::error
&e
) {
1231 lderr(cct
) << __func__
<< ": decode error from journal_stream" << dendl
;
1236 ldout(cct
, 10) << "try_read_entry at " << read_pos
<< " read "
1237 << read_pos
<< "~" << consumed
<< " (have "
1238 << read_buf
.length() << ")" << dendl
;
1240 read_pos
+= consumed
;
1242 // We were readable, we might not be any more
1243 readable
= _is_readable();
1244 } catch (const buffer::error
&e
) {
1245 lderr(cct
) << __func__
<< ": decode error from _is_readable" << dendl
;
1255 void Journaler::wait_for_readable(Context
*onreadable
)
1258 if (is_stopping()) {
1259 finisher
->queue(onreadable
, -EAGAIN
);
1263 assert(on_readable
== 0);
1265 ldout(cct
, 10) << "wait_for_readable at " << read_pos
<< " onreadable "
1266 << onreadable
<< dendl
;
1267 on_readable
= wrap_finisher(onreadable
);
1269 // race with OSD reply
1270 finisher
->queue(onreadable
, 0);
1274 bool Journaler::have_waiter() const
1276 return on_readable
!= nullptr;
1282 /***************** TRIMMING *******************/
1285 class Journaler::C_Trim
: public Context
{
1289 C_Trim(Journaler
*l
, int64_t t
) : ls(l
), to(t
) {}
1290 void finish(int r
) override
{
1291 ls
->_finish_trim(r
, to
);
1295 void Journaler::trim()
1301 void Journaler::_trim()
1307 uint64_t period
= get_layout_period();
1308 uint64_t trim_to
= last_committed
.expire_pos
;
1309 trim_to
-= trim_to
% period
;
1310 ldout(cct
, 10) << "trim last_commited head was " << last_committed
1311 << ", can trim to " << trim_to
1313 if (trim_to
== 0 || trim_to
== trimming_pos
) {
1314 ldout(cct
, 10) << "trim already trimmed/trimming to "
1315 << trimmed_pos
<< "/" << trimming_pos
<< dendl
;
1319 if (trimming_pos
> trimmed_pos
) {
1320 ldout(cct
, 10) << "trim already trimming atm, try again later. "
1321 "trimmed/trimming is " << trimmed_pos
<< "/" << trimming_pos
<< dendl
;
1326 assert(trim_to
<= write_pos
);
1327 assert(trim_to
<= expire_pos
);
1328 assert(trim_to
> trimming_pos
);
1329 ldout(cct
, 10) << "trim trimming to " << trim_to
1330 << ", trimmed/trimming/expire are "
1331 << trimmed_pos
<< "/" << trimming_pos
<< "/" << expire_pos
1334 // delete range of objects
1335 uint64_t first
= trimming_pos
/ period
;
1336 uint64_t num
= (trim_to
- trimming_pos
) / period
;
1338 filer
.purge_range(ino
, &layout
, snapc
, first
, num
,
1339 ceph::real_clock::now(), 0,
1340 wrap_finisher(new C_Trim(this, trim_to
)));
1341 trimming_pos
= trim_to
;
1344 void Journaler::_finish_trim(int r
, uint64_t to
)
1349 ldout(cct
, 10) << "_finish_trim trimmed_pos was " << trimmed_pos
1350 << ", trimmed/trimming/expire now "
1351 << to
<< "/" << trimming_pos
<< "/" << expire_pos
1353 if (r
< 0 && r
!= -ENOENT
) {
1354 lderr(cct
) << "_finish_trim got " << cpp_strerror(r
) << dendl
;
1355 handle_write_error(r
);
1359 assert(r
>= 0 || r
== -ENOENT
);
1361 assert(to
<= trimming_pos
);
1362 assert(to
> trimmed_pos
);
1366 void Journaler::handle_write_error(int r
)
1370 lderr(cct
) << "handle_write_error " << cpp_strerror(r
) << dendl
;
1371 if (on_write_error
) {
1372 on_write_error
->complete(r
);
1373 on_write_error
= NULL
;
1374 called_write_error
= true;
1375 } else if (called_write_error
) {
1376 /* We don't call error handler more than once, subsequent errors
1377 * are dropped -- this is okay as long as the error handler does
1378 * something dramatic like respawn */
1379 lderr(cct
) << __func__
<< ": multiple write errors, handler already called"
1382 assert(0 == "unhandled write error");
1388 * Test whether the 'read_buf' byte stream has enough data to read
1391 * sets 'next_envelope_size' to the number of bytes needed to advance (enough
1392 * to get the next header if header was unavailable, or enough to get the whole
1393 * next entry if the header was available but the body wasn't).
1395 bool JournalStream::readable(bufferlist
&read_buf
, uint64_t *need
) const
1397 assert(need
!= NULL
);
1399 uint32_t entry_size
= 0;
1400 uint64_t entry_sentinel
= 0;
1401 bufferlist::iterator p
= read_buf
.begin();
1403 // Do we have enough data to decode an entry prefix?
1404 if (format
>= JOURNAL_FORMAT_RESILIENT
) {
1405 *need
= sizeof(entry_size
) + sizeof(entry_sentinel
);
1407 *need
= sizeof(entry_size
);
1409 if (read_buf
.length() >= *need
) {
1410 if (format
>= JOURNAL_FORMAT_RESILIENT
) {
1411 ::decode(entry_sentinel
, p
);
1412 if (entry_sentinel
!= sentinel
) {
1413 throw buffer::malformed_input("Invalid sentinel");
1417 ::decode(entry_size
, p
);
1422 // Do we have enough data to decode an entry prefix, payload and suffix?
1423 if (format
>= JOURNAL_FORMAT_RESILIENT
) {
1424 *need
= JOURNAL_ENVELOPE_RESILIENT
+ entry_size
;
1426 *need
= JOURNAL_ENVELOPE_LEGACY
+ entry_size
;
1428 if (read_buf
.length() >= *need
) {
1429 return true; // No more bytes needed
1437 * Consume one entry from a journal byte stream 'from', splicing a
1438 * serialized LogEvent blob into 'entry'.
1440 * 'entry' must be non null and point to an empty bufferlist.
1442 * 'from' must contain sufficient valid data (i.e. readable is true).
1444 * 'start_ptr' will be set to the entry's start pointer, if the collection
1445 * format provides it. It may not be null.
1447 * @returns The number of bytes consumed from the `from` byte stream. Note
1448 * that this is not equal to the length of `entry`, which contains
1449 * the inner serialized LogEvent and not the envelope.
1451 size_t JournalStream::read(bufferlist
&from
, bufferlist
*entry
,
1452 uint64_t *start_ptr
)
1454 assert(start_ptr
!= NULL
);
1455 assert(entry
!= NULL
);
1456 assert(entry
->length() == 0);
1458 uint32_t entry_size
= 0;
1460 // Consume envelope prefix: entry_size and entry_sentinel
1461 bufferlist::iterator from_ptr
= from
.begin();
1462 if (format
>= JOURNAL_FORMAT_RESILIENT
) {
1463 uint64_t entry_sentinel
= 0;
1464 ::decode(entry_sentinel
, from_ptr
);
1465 // Assertion instead of clean check because of precondition of this
1466 // fn is that readable() already passed
1467 assert(entry_sentinel
== sentinel
);
1469 ::decode(entry_size
, from_ptr
);
1471 // Read out the payload
1472 from_ptr
.copy(entry_size
, *entry
);
1474 // Consume the envelope suffix (start_ptr)
1475 if (format
>= JOURNAL_FORMAT_RESILIENT
) {
1476 ::decode(*start_ptr
, from_ptr
);
1481 // Trim the input buffer to discard the bytes we have consumed
1482 from
.splice(0, from_ptr
.get_off());
1484 return from_ptr
.get_off();
1491 size_t JournalStream::write(bufferlist
&entry
, bufferlist
*to
,
1492 uint64_t const &start_ptr
)
1496 uint32_t const entry_size
= entry
.length();
1497 if (format
>= JOURNAL_FORMAT_RESILIENT
) {
1498 ::encode(sentinel
, *to
);
1500 ::encode(entry_size
, *to
);
1501 to
->claim_append(entry
);
1502 if (format
>= JOURNAL_FORMAT_RESILIENT
) {
1503 ::encode(start_ptr
, *to
);
1506 if (format
>= JOURNAL_FORMAT_RESILIENT
) {
1507 return JOURNAL_ENVELOPE_RESILIENT
+ entry_size
;
1509 return JOURNAL_ENVELOPE_LEGACY
+ entry_size
;
1514 * set write error callback
1516 * Set a callback/context to trigger if we get a write error from
1517 * the objecter. This may be from an explicit request (e.g., flush)
1518 * or something async the journaler did on its own (e.g., journal
1521 * It is only used once; if the caller continues to use the
1522 * Journaler and wants to hear about errors, it needs to reset the
1525 * @param c callback/context to trigger on error
1527 void Journaler::set_write_error_handler(Context
*c
) {
1529 assert(!on_write_error
);
1530 on_write_error
= wrap_finisher(c
);
1531 called_write_error
= false;
1536 * Wrap a context in a C_OnFinisher, if it is non-NULL
1538 * Utility function to avoid lots of error-prone and verbose
1539 * NULL checking on contexts passed in.
1541 C_OnFinisher
*Journaler::wrap_finisher(Context
*c
)
1544 return new C_OnFinisher(c
, finisher
);
1550 void Journaler::shutdown()
1554 ldout(cct
, 1) << __func__
<< dendl
;
1556 state
= STATE_STOPPING
;
1559 // Kick out anyone reading from journal
1562 C_OnFinisher
*f
= on_readable
;
1564 f
->complete(-EAGAIN
);
1568 ls
.swap(waitfor_recover
);
1569 finish_contexts(cct
, ls
, -ESHUTDOWN
);
1571 std::map
<uint64_t, std::list
<Context
*> >::iterator i
;
1572 for (i
= waitfor_safe
.begin(); i
!= waitfor_safe
.end(); ++i
) {
1573 finish_contexts(cct
, i
->second
, -EAGAIN
);
1575 waitfor_safe
.clear();