/
PGLog.cc
688 lines (615 loc) · 18.9 KB
/
PGLog.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
// -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*-
// vim: ts=8 sw=2 smarttab
/*
* Ceph - scalable distributed file system
*
* Copyright (C) 2004-2006 Sage Weil <sage@newdream.net>
* Copyright (C) 2013 Cloudwatt <libre.licensing@cloudwatt.com>
*
* Author: Loic Dachary <loic@dachary.org>
*
* This is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License version 2.1, as published by the Free Software
* Foundation. See file COPYING.
*
*/
#include "PGLog.h"
#include "include/unordered_map.h"
#include "common/ceph_context.h"
#define dout_context cct
#define dout_subsys ceph_subsys_osd
#undef dout_prefix
#define dout_prefix _prefix(_dout, this)
static ostream& _prefix(std::ostream *_dout, const PGLog *pglog)
{
return *_dout << pglog->gen_prefix();
}
//////////////////// PGLog::IndexedLog ////////////////////
void PGLog::IndexedLog::split_out_child(
pg_t child_pgid,
unsigned split_bits,
PGLog::IndexedLog *target)
{
unindex();
*target = pg_log_t::split_out_child(child_pgid, split_bits);
index();
target->index();
reset_rollback_info_trimmed_to_riter();
}
void PGLog::IndexedLog::trim(
CephContext* cct,
eversion_t s,
set<eversion_t> *trimmed)
{
if (complete_to != log.end() &&
complete_to->version <= s) {
generic_dout(0) << " bad trim to " << s << " when complete_to is "
<< complete_to->version
<< " on " << *this << dendl;
}
assert(s <= can_rollback_to);
while (!log.empty()) {
pg_log_entry_t &e = *log.begin();
if (e.version > s)
break;
generic_dout(20) << "trim " << e << dendl;
if (trimmed)
trimmed->insert(e.version);
unindex(e); // remove from index,
if (rollback_info_trimmed_to_riter == log.rend() ||
e.version == rollback_info_trimmed_to_riter->version) {
log.pop_front();
rollback_info_trimmed_to_riter = log.rend();
} else {
log.pop_front();
}
}
// raise tail?
if (tail < s)
tail = s;
}
ostream& PGLog::IndexedLog::print(ostream& out) const
{
out << *this << std::endl;
for (list<pg_log_entry_t>::const_iterator p = log.begin();
p != log.end();
++p) {
out << *p << " " << (logged_object(p->soid) ? "indexed":"NOT INDEXED") << std::endl;
assert(!p->reqid_is_indexed() || logged_req(p->reqid));
}
return out;
}
//////////////////// PGLog ////////////////////
void PGLog::reset_backfill()
{
missing.clear();
}
void PGLog::clear() {
missing.clear();
log.clear();
log_keys_debug.clear();
undirty();
}
void PGLog::clear_info_log(
spg_t pgid,
ObjectStore::Transaction *t) {
coll_t coll(pgid);
t->remove(coll, pgid.make_pgmeta_oid());
}
void PGLog::trim(
eversion_t trim_to,
pg_info_t &info)
{
// trim?
if (trim_to > log.tail) {
// We shouldn't be trimming the log past last_complete
assert(trim_to <= info.last_complete);
dout(10) << "trim " << log << " to " << trim_to << dendl;
log.trim(cct, trim_to, &trimmed);
info.log_tail = log.tail;
}
}
void PGLog::proc_replica_log(
pg_info_t &oinfo,
const pg_log_t &olog,
pg_missing_t& omissing,
pg_shard_t from) const
{
dout(10) << "proc_replica_log for osd." << from << ": "
<< oinfo << " " << olog << " " << omissing << dendl;
if (olog.head < log.tail) {
dout(10) << __func__ << ": osd." << from << " does not overlap, not looking "
<< "for divergent objects" << dendl;
return;
}
if (olog.head == log.head) {
dout(10) << __func__ << ": osd." << from << " same log head, not looking "
<< "for divergent objects" << dendl;
return;
}
assert(olog.head >= log.tail);
/*
basically what we're doing here is rewinding the remote log,
dropping divergent entries, until we find something that matches
our master log. we then reset last_update to reflect the new
point up to which missing is accurate.
later, in activate(), missing will get wound forward again and
we will send the peer enough log to arrive at the same state.
*/
for (map<hobject_t, pg_missing_item>::const_iterator i = omissing.get_items().begin();
i != omissing.get_items().end();
++i) {
dout(20) << " before missing " << i->first << " need " << i->second.need
<< " have " << i->second.have << dendl;
}
list<pg_log_entry_t>::const_reverse_iterator first_non_divergent =
log.log.rbegin();
while (1) {
if (first_non_divergent == log.log.rend())
break;
if (first_non_divergent->version <= olog.head) {
dout(20) << "merge_log point (usually last shared) is "
<< *first_non_divergent << dendl;
break;
}
++first_non_divergent;
}
/* Because olog.head >= log.tail, we know that both pgs must at least have
* the event represented by log.tail. Similarly, because log.head >= olog.tail,
* we know that the even represented by olog.tail must be common to both logs.
* Furthermore, the event represented by a log tail was necessarily trimmed,
* thus neither olog.tail nor log.tail can be divergent. It's
* possible that olog/log contain no actual events between olog.head and
* MAX(log.tail, olog.tail), however, since they might have been split out.
* Thus, if we cannot find an event e such that
* log.tail <= e.version <= log.head, the last_update must actually be
* MAX(log.tail, olog.tail).
*/
eversion_t limit = MAX(olog.tail, log.tail);
eversion_t lu =
(first_non_divergent == log.log.rend() ||
first_non_divergent->version < limit) ?
limit :
first_non_divergent->version;
IndexedLog folog(olog);
auto divergent = folog.rewind_from_head(lu);
_merge_divergent_entries(
folog,
divergent,
oinfo,
olog.get_can_rollback_to(),
omissing,
0,
this);
if (lu < oinfo.last_update) {
dout(10) << " peer osd." << from << " last_update now " << lu << dendl;
oinfo.last_update = lu;
}
if (omissing.have_missing()) {
eversion_t first_missing =
omissing.get_items().at(omissing.get_rmissing().begin()->second).need;
oinfo.last_complete = eversion_t();
list<pg_log_entry_t>::const_iterator i = olog.log.begin();
for (;
i != olog.log.end();
++i) {
if (i->version < first_missing)
oinfo.last_complete = i->version;
else
break;
}
} else {
oinfo.last_complete = oinfo.last_update;
}
}
/**
* rewind divergent entries at the head of the log
*
* This rewinds entries off the head of our log that are divergent.
* This is used by replicas during activation.
*
* @param newhead new head to rewind to
*/
void PGLog::rewind_divergent_log(eversion_t newhead,
pg_info_t &info, LogEntryHandler *rollbacker,
bool &dirty_info, bool &dirty_big_info)
{
dout(10) << "rewind_divergent_log truncate divergent future " << newhead << dendl;
if (info.last_complete > newhead)
info.last_complete = newhead;
auto divergent = log.rewind_from_head(newhead);
if (!divergent.empty()) {
mark_dirty_from(divergent.front().version);
}
for (auto &&entry: divergent) {
dout(10) << "rewind_divergent_log future divergent " << entry << dendl;
}
info.last_update = newhead;
_merge_divergent_entries(
log,
divergent,
info,
log.get_can_rollback_to(),
missing,
rollbacker,
this);
dirty_info = true;
dirty_big_info = true;
}
void PGLog::merge_log(pg_info_t &oinfo, pg_log_t &olog, pg_shard_t fromosd,
pg_info_t &info, LogEntryHandler *rollbacker,
bool &dirty_info, bool &dirty_big_info)
{
dout(10) << "merge_log " << olog << " from osd." << fromosd
<< " into " << log << dendl;
// Check preconditions
// If our log is empty, the incoming log needs to have not been trimmed.
assert(!log.null() || olog.tail == eversion_t());
// The logs must overlap.
assert(log.head >= olog.tail && olog.head >= log.tail);
for (map<hobject_t, pg_missing_item>::const_iterator i = missing.get_items().begin();
i != missing.get_items().end();
++i) {
dout(20) << "pg_missing_t sobject: " << i->first << dendl;
}
bool changed = false;
// extend on tail?
// this is just filling in history. it does not affect our
// missing set, as that should already be consistent with our
// current log.
eversion_t orig_tail = log.tail;
if (olog.tail < log.tail) {
dout(10) << "merge_log extending tail to " << olog.tail << dendl;
list<pg_log_entry_t>::iterator from = olog.log.begin();
list<pg_log_entry_t>::iterator to;
eversion_t last;
for (to = from;
to != olog.log.end();
++to) {
if (to->version > log.tail)
break;
log.index(*to);
dout(15) << *to << dendl;
last = to->version;
}
mark_dirty_to(last);
// splice into our log.
log.log.splice(log.log.begin(),
olog.log, from, to);
info.log_tail = log.tail = olog.tail;
changed = true;
}
if (oinfo.stats.reported_seq < info.stats.reported_seq || // make sure reported always increases
oinfo.stats.reported_epoch < info.stats.reported_epoch) {
oinfo.stats.reported_seq = info.stats.reported_seq;
oinfo.stats.reported_epoch = info.stats.reported_epoch;
}
if (info.last_backfill.is_max())
info.stats = oinfo.stats;
info.hit_set = oinfo.hit_set;
// do we have divergent entries to throw out?
if (olog.head < log.head) {
rewind_divergent_log(olog.head, info, rollbacker, dirty_info, dirty_big_info);
changed = true;
}
// extend on head?
if (olog.head > log.head) {
dout(10) << "merge_log extending head to " << olog.head << dendl;
// find start point in olog
list<pg_log_entry_t>::iterator to = olog.log.end();
list<pg_log_entry_t>::iterator from = olog.log.end();
eversion_t lower_bound = MAX(olog.tail, orig_tail);
while (1) {
if (from == olog.log.begin())
break;
--from;
dout(20) << " ? " << *from << dendl;
if (from->version <= log.head) {
lower_bound = MAX(lower_bound, from->version);
++from;
break;
}
}
dout(20) << "merge_log cut point (usually last shared) is "
<< lower_bound << dendl;
mark_dirty_from(lower_bound);
auto divergent = log.rewind_from_head(lower_bound);
// move aside divergent items
for (auto &&oe: divergent) {
dout(10) << "merge_log divergent " << oe << dendl;
}
log.roll_forward_to(log.head, rollbacker);
mempool::osd_pglog::list<pg_log_entry_t> new_entries;
new_entries.splice(new_entries.end(), olog.log, from, to);
append_log_entries_update_missing(
info.last_backfill,
info.last_backfill_bitwise,
new_entries,
false,
&log,
missing,
rollbacker,
this);
_merge_divergent_entries(
log,
divergent,
info,
log.get_can_rollback_to(),
missing,
rollbacker,
this);
info.last_update = log.head = olog.head;
// We cannot rollback into the new log entries
log.skip_can_rollback_to_to_head();
info.last_user_version = oinfo.last_user_version;
info.purged_snaps = oinfo.purged_snaps;
changed = true;
}
dout(10) << "merge_log result " << log << " " << missing << " changed=" << changed << dendl;
if (changed) {
dirty_info = true;
dirty_big_info = true;
}
}
void PGLog::check() {
if (!pg_log_debug)
return;
if (log.log.size() != log_keys_debug.size()) {
derr << "log.log.size() != log_keys_debug.size()" << dendl;
derr << "actual log:" << dendl;
for (list<pg_log_entry_t>::iterator i = log.log.begin();
i != log.log.end();
++i) {
derr << " " << *i << dendl;
}
derr << "log_keys_debug:" << dendl;
for (set<string>::const_iterator i = log_keys_debug.begin();
i != log_keys_debug.end();
++i) {
derr << " " << *i << dendl;
}
}
assert(log.log.size() == log_keys_debug.size());
for (list<pg_log_entry_t>::iterator i = log.log.begin();
i != log.log.end();
++i) {
assert(log_keys_debug.count(i->get_key_name()));
}
}
void PGLog::write_log_and_missing(
ObjectStore::Transaction& t,
map<string,bufferlist> *km,
const coll_t& coll, const ghobject_t &log_oid,
bool require_rollback)
{
if (is_dirty()) {
dout(5) << "write_log_and_missing with: "
<< "dirty_to: " << dirty_to
<< ", dirty_from: " << dirty_from
<< ", writeout_from: " << writeout_from
<< ", trimmed: " << trimmed
<< ", clear_divergent_priors: " << clear_divergent_priors
<< dendl;
_write_log_and_missing(
t, km, log, coll, log_oid,
dirty_to,
dirty_from,
writeout_from,
trimmed,
missing,
!touched_log,
require_rollback,
clear_divergent_priors,
(pg_log_debug ? &log_keys_debug : 0));
undirty();
} else {
dout(10) << "log is not dirty" << dendl;
}
}
void PGLog::write_log_and_missing_wo_missing(
ObjectStore::Transaction& t,
map<string,bufferlist> *km,
pg_log_t &log,
const coll_t& coll, const ghobject_t &log_oid,
map<eversion_t, hobject_t> &divergent_priors,
bool require_rollback)
{
_write_log_and_missing_wo_missing(
t, km, log, coll, log_oid,
divergent_priors, eversion_t::max(), eversion_t(), eversion_t(),
set<eversion_t>(),
true, true, require_rollback, 0);
}
void PGLog::write_log_and_missing(
ObjectStore::Transaction& t,
map<string,bufferlist> *km,
pg_log_t &log,
const coll_t& coll,
const ghobject_t &log_oid,
const pg_missing_tracker_t &missing,
bool require_rollback)
{
_write_log_and_missing(
t, km, log, coll, log_oid,
eversion_t::max(),
eversion_t(),
eversion_t(),
set<eversion_t>(),
missing,
true, require_rollback, false, 0);
}
void PGLog::_write_log_and_missing_wo_missing(
ObjectStore::Transaction& t,
map<string,bufferlist> *km,
pg_log_t &log,
const coll_t& coll, const ghobject_t &log_oid,
map<eversion_t, hobject_t> &divergent_priors,
eversion_t dirty_to,
eversion_t dirty_from,
eversion_t writeout_from,
const set<eversion_t> &trimmed,
bool dirty_divergent_priors,
bool touch_log,
bool require_rollback,
set<string> *log_keys_debug
)
{
set<string> to_remove;
for (set<eversion_t>::const_iterator i = trimmed.begin();
i != trimmed.end();
++i) {
to_remove.insert(i->get_key_name());
if (log_keys_debug) {
assert(log_keys_debug->count(i->get_key_name()));
log_keys_debug->erase(i->get_key_name());
}
}
//dout(10) << "write_log_and_missing, clearing up to " << dirty_to << dendl;
if (touch_log)
t.touch(coll, log_oid);
if (dirty_to != eversion_t()) {
t.omap_rmkeyrange(
coll, log_oid,
eversion_t().get_key_name(), dirty_to.get_key_name());
clear_up_to(log_keys_debug, dirty_to.get_key_name());
}
if (dirty_to != eversion_t::max() && dirty_from != eversion_t::max()) {
// dout(10) << "write_log_and_missing, clearing from " << dirty_from << dendl;
t.omap_rmkeyrange(
coll, log_oid,
dirty_from.get_key_name(), eversion_t::max().get_key_name());
clear_after(log_keys_debug, dirty_from.get_key_name());
}
for (list<pg_log_entry_t>::iterator p = log.log.begin();
p != log.log.end() && p->version <= dirty_to;
++p) {
bufferlist bl(sizeof(*p) * 2);
p->encode_with_checksum(bl);
(*km)[p->get_key_name()].claim(bl);
}
for (list<pg_log_entry_t>::reverse_iterator p = log.log.rbegin();
p != log.log.rend() &&
(p->version >= dirty_from || p->version >= writeout_from) &&
p->version >= dirty_to;
++p) {
bufferlist bl(sizeof(*p) * 2);
p->encode_with_checksum(bl);
(*km)[p->get_key_name()].claim(bl);
}
if (log_keys_debug) {
for (map<string, bufferlist>::iterator i = (*km).begin();
i != (*km).end();
++i) {
if (i->first[0] == '_')
continue;
assert(!log_keys_debug->count(i->first));
log_keys_debug->insert(i->first);
}
}
if (dirty_divergent_priors) {
//dout(10) << "write_log_and_missing: writing divergent_priors" << dendl;
::encode(divergent_priors, (*km)["divergent_priors"]);
}
if (require_rollback) {
::encode(
log.get_can_rollback_to(),
(*km)["can_rollback_to"]);
::encode(
log.get_rollback_info_trimmed_to(),
(*km)["rollback_info_trimmed_to"]);
}
if (!to_remove.empty())
t.omap_rmkeys(coll, log_oid, to_remove);
}
void PGLog::_write_log_and_missing(
ObjectStore::Transaction& t,
map<string,bufferlist>* km,
pg_log_t &log,
const coll_t& coll, const ghobject_t &log_oid,
eversion_t dirty_to,
eversion_t dirty_from,
eversion_t writeout_from,
const set<eversion_t> &trimmed,
const pg_missing_tracker_t &missing,
bool touch_log,
bool require_rollback,
bool clear_divergent_priors,
set<string> *log_keys_debug
) {
set<string> to_remove;
for (set<eversion_t>::const_iterator i = trimmed.begin();
i != trimmed.end();
++i) {
to_remove.insert(i->get_key_name());
if (log_keys_debug) {
assert(log_keys_debug->count(i->get_key_name()));
log_keys_debug->erase(i->get_key_name());
}
}
if (touch_log)
t.touch(coll, log_oid);
if (dirty_to != eversion_t()) {
t.omap_rmkeyrange(
coll, log_oid,
eversion_t().get_key_name(), dirty_to.get_key_name());
clear_up_to(log_keys_debug, dirty_to.get_key_name());
}
if (dirty_to != eversion_t::max() && dirty_from != eversion_t::max()) {
// dout(10) << "write_log_and_missing, clearing from " << dirty_from << dendl;
t.omap_rmkeyrange(
coll, log_oid,
dirty_from.get_key_name(), eversion_t::max().get_key_name());
clear_after(log_keys_debug, dirty_from.get_key_name());
}
for (list<pg_log_entry_t>::iterator p = log.log.begin();
p != log.log.end() && p->version <= dirty_to;
++p) {
bufferlist bl(sizeof(*p) * 2);
p->encode_with_checksum(bl);
(*km)[p->get_key_name()].claim(bl);
}
for (list<pg_log_entry_t>::reverse_iterator p = log.log.rbegin();
p != log.log.rend() &&
(p->version >= dirty_from || p->version >= writeout_from) &&
p->version >= dirty_to;
++p) {
bufferlist bl(sizeof(*p) * 2);
p->encode_with_checksum(bl);
(*km)[p->get_key_name()].claim(bl);
}
if (log_keys_debug) {
for (map<string, bufferlist>::iterator i = (*km).begin();
i != (*km).end();
++i) {
if (i->first[0] == '_')
continue;
assert(!log_keys_debug->count(i->first));
log_keys_debug->insert(i->first);
}
}
if (clear_divergent_priors) {
//dout(10) << "write_log_and_missing: writing divergent_priors" << dendl;
to_remove.insert("divergent_priors");
}
missing.get_changed(
[&](const hobject_t &obj) {
string key = string("missing/") + obj.to_str();
pg_missing_item item;
if (!missing.is_missing(obj, &item)) {
to_remove.insert(key);
} else {
::encode(make_pair(obj, item), (*km)[key]);
}
});
if (require_rollback) {
::encode(
log.get_can_rollback_to(),
(*km)["can_rollback_to"]);
::encode(
log.get_rollback_info_trimmed_to(),
(*km)["rollback_info_trimmed_to"]);
}
if (!to_remove.empty())
t.omap_rmkeys(coll, log_oid, to_remove);
}