/
ObjectCacher.cc
2169 lines (1877 loc) · 60.9 KB
/
ObjectCacher.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
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*-
// vim: ts=8 sw=2 smarttab
#include <limits.h>
#include "msg/Messenger.h"
#include "ObjectCacher.h"
#include "WritebackHandler.h"
#include "common/errno.h"
#include "common/perf_counters.h"
#include "include/assert.h"
#define MAX_FLUSH_UNDER_LOCK 20 ///< max bh's we start writeback on while holding the lock
/*** ObjectCacher::BufferHead ***/
/*** ObjectCacher::Object ***/
#define dout_subsys ceph_subsys_objectcacher
#undef dout_prefix
#define dout_prefix *_dout << "objectcacher.object(" << oid << ") "
ObjectCacher::BufferHead *ObjectCacher::Object::split(BufferHead *left, loff_t off)
{
assert(oc->lock.is_locked());
ldout(oc->cct, 20) << "split " << *left << " at " << off << dendl;
// split off right
ObjectCacher::BufferHead *right = new BufferHead(this);
//inherit and if later access, this auto clean.
right->set_dontneed(left->get_dontneed());
right->last_write_tid = left->last_write_tid;
right->last_read_tid = left->last_read_tid;
right->set_state(left->get_state());
right->snapc = left->snapc;
loff_t newleftlen = off - left->start();
right->set_start(off);
right->set_length(left->length() - newleftlen);
// shorten left
oc->bh_stat_sub(left);
left->set_length(newleftlen);
oc->bh_stat_add(left);
// add right
oc->bh_add(this, right);
// split buffers too
bufferlist bl;
bl.claim(left->bl);
if (bl.length()) {
assert(bl.length() == (left->length() + right->length()));
right->bl.substr_of(bl, left->length(), right->length());
left->bl.substr_of(bl, 0, left->length());
}
// move read waiters
if (!left->waitfor_read.empty()) {
map<loff_t, list<Context*> >::iterator start_remove = left->waitfor_read.begin();
while (start_remove != left->waitfor_read.end() &&
start_remove->first < right->start())
++start_remove;
for (map<loff_t, list<Context*> >::iterator p = start_remove;
p != left->waitfor_read.end(); ++p) {
ldout(oc->cct, 20) << "split moving waiters at byte " << p->first << " to right bh" << dendl;
right->waitfor_read[p->first].swap( p->second );
assert(p->second.empty());
}
left->waitfor_read.erase(start_remove, left->waitfor_read.end());
}
ldout(oc->cct, 20) << "split left is " << *left << dendl;
ldout(oc->cct, 20) << "split right is " << *right << dendl;
return right;
}
void ObjectCacher::Object::merge_left(BufferHead *left, BufferHead *right)
{
assert(oc->lock.is_locked());
assert(left->end() == right->start());
assert(left->get_state() == right->get_state());
ldout(oc->cct, 10) << "merge_left " << *left << " + " << *right << dendl;
oc->bh_remove(this, right);
oc->bh_stat_sub(left);
left->set_length(left->length() + right->length());
oc->bh_stat_add(left);
// data
left->bl.claim_append(right->bl);
// version
// note: this is sorta busted, but should only be used for dirty buffers
left->last_write_tid = MAX( left->last_write_tid, right->last_write_tid );
left->last_write = MAX( left->last_write, right->last_write );
left->set_dontneed(right->get_dontneed() ? left->get_dontneed() : false);
// waiters
for (map<loff_t, list<Context*> >::iterator p = right->waitfor_read.begin();
p != right->waitfor_read.end();
++p)
left->waitfor_read[p->first].splice( left->waitfor_read[p->first].begin(),
p->second );
// hose right
delete right;
ldout(oc->cct, 10) << "merge_left result " << *left << dendl;
}
void ObjectCacher::Object::try_merge_bh(BufferHead *bh)
{
assert(oc->lock.is_locked());
ldout(oc->cct, 10) << "try_merge_bh " << *bh << dendl;
// do not merge rx buffers; last_read_tid may not match
if (bh->is_rx())
return;
// to the left?
map<loff_t,BufferHead*>::iterator p = data.find(bh->start());
assert(p->second == bh);
if (p != data.begin()) {
--p;
if (p->second->end() == bh->start() &&
p->second->get_state() == bh->get_state()) {
merge_left(p->second, bh);
bh = p->second;
} else {
++p;
}
}
// to the right?
assert(p->second == bh);
++p;
if (p != data.end() &&
p->second->start() == bh->end() &&
p->second->get_state() == bh->get_state())
merge_left(bh, p->second);
}
/*
* count bytes we have cached in given range
*/
bool ObjectCacher::Object::is_cached(loff_t cur, loff_t left)
{
assert(oc->lock.is_locked());
map<loff_t, BufferHead*>::iterator p = data_lower_bound(cur);
while (left > 0) {
if (p == data.end())
return false;
if (p->first <= cur) {
// have part of it
loff_t lenfromcur = MIN(p->second->end() - cur, left);
cur += lenfromcur;
left -= lenfromcur;
++p;
continue;
} else if (p->first > cur) {
// gap
return false;
} else
assert(0);
}
return true;
}
/*
* all cached data in this range[off, off+len]
*/
bool ObjectCacher::Object::include_all_cached_data(loff_t off, loff_t len)
{
assert(oc->lock.is_locked());
if (data.empty())
return true;
map<loff_t, BufferHead*>::iterator first = data.begin();
map<loff_t, BufferHead*>::reverse_iterator last = data.rbegin();
if (first->second->start() >= off && last->second->end() <= (off + len))
return true;
else
return false;
}
/*
* map a range of bytes into buffer_heads.
* - create missing buffer_heads as necessary.
*/
int ObjectCacher::Object::map_read(OSDRead *rd,
map<loff_t, BufferHead*>& hits,
map<loff_t, BufferHead*>& missing,
map<loff_t, BufferHead*>& rx,
map<loff_t, BufferHead*>& errors)
{
assert(oc->lock.is_locked());
for (vector<ObjectExtent>::iterator ex_it = rd->extents.begin();
ex_it != rd->extents.end();
++ex_it) {
if (ex_it->oid != oid.oid)
continue;
ldout(oc->cct, 10) << "map_read " << ex_it->oid
<< " " << ex_it->offset << "~" << ex_it->length
<< dendl;
loff_t cur = ex_it->offset;
loff_t left = ex_it->length;
map<loff_t, BufferHead*>::iterator p = data_lower_bound(ex_it->offset);
while (left > 0) {
// at end?
if (p == data.end()) {
// rest is a miss.
BufferHead *n = new BufferHead(this);
n->set_start(cur);
n->set_length(left);
oc->bh_add(this, n);
if (complete) {
oc->mark_zero(n);
hits[cur] = n;
ldout(oc->cct, 20) << "map_read miss+complete+zero " << left << " left, " << *n << dendl;
} else {
missing[cur] = n;
ldout(oc->cct, 20) << "map_read miss " << left << " left, " << *n << dendl;
}
cur += left;
assert(cur == (loff_t)ex_it->offset + (loff_t)ex_it->length);
break; // no more.
}
if (p->first <= cur) {
// have it (or part of it)
BufferHead *e = p->second;
if (e->is_clean() ||
e->is_dirty() ||
e->is_tx() ||
e->is_zero()) {
hits[cur] = e; // readable!
ldout(oc->cct, 20) << "map_read hit " << *e << dendl;
} else if (e->is_rx()) {
rx[cur] = e; // missing, not readable.
ldout(oc->cct, 20) << "map_read rx " << *e << dendl;
} else if (e->is_error()) {
errors[cur] = e;
ldout(oc->cct, 20) << "map_read error " << *e << dendl;
} else {
assert(0);
}
loff_t lenfromcur = MIN(e->end() - cur, left);
cur += lenfromcur;
left -= lenfromcur;
++p;
continue; // more?
} else if (p->first > cur) {
// gap.. miss
loff_t next = p->first;
BufferHead *n = new BufferHead(this);
loff_t len = MIN(next - cur, left);
n->set_start(cur);
n->set_length(len);
oc->bh_add(this,n);
if (complete) {
oc->mark_zero(n);
hits[cur] = n;
ldout(oc->cct, 20) << "map_read gap+complete+zero " << *n << dendl;
} else {
missing[cur] = n;
ldout(oc->cct, 20) << "map_read gap " << *n << dendl;
}
cur += MIN(left, n->length());
left -= MIN(left, n->length());
continue; // more?
} else {
assert(0);
}
}
}
return 0;
}
void ObjectCacher::Object::audit_buffers()
{
loff_t offset = 0;
for (map<loff_t, BufferHead*>::const_iterator it = data.begin();
it != data.end(); ++it) {
if (it->first != it->second->start()) {
lderr(oc->cct) << "AUDIT FAILURE: map position " << it->first
<< " does not match bh start position: "
<< *it->second << dendl;
assert(it->first == it->second->start());
}
if (it->first < offset) {
lderr(oc->cct) << "AUDIT FAILURE: " << it->first << " " << *it->second
<< " overlaps with previous bh " << *((--it)->second)
<< dendl;
assert(it->first >= offset);
}
BufferHead *bh = it->second;
map<loff_t, list<Context*> >::const_iterator w_it;
for (w_it = bh->waitfor_read.begin();
w_it != bh->waitfor_read.end(); ++w_it) {
if (w_it->first < bh->start() ||
w_it->first >= bh->start() + bh->length()) {
lderr(oc->cct) << "AUDIT FAILURE: waiter at " << w_it->first
<< " is not within bh " << *bh << dendl;
assert(w_it->first >= bh->start());
assert(w_it->first < bh->start() + bh->length());
}
}
offset = it->first + it->second->length();
}
}
/*
* map a range of extents on an object's buffer cache.
* - combine any bh's we're writing into one
* - break up bufferheads that don't fall completely within the range
* //no! - return a bh that includes the write. may also include other dirty data to left and/or right.
*/
ObjectCacher::BufferHead *ObjectCacher::Object::map_write(OSDWrite *wr)
{
assert(oc->lock.is_locked());
BufferHead *final = 0;
for (vector<ObjectExtent>::iterator ex_it = wr->extents.begin();
ex_it != wr->extents.end();
++ex_it) {
if (ex_it->oid != oid.oid) continue;
ldout(oc->cct, 10) << "map_write oex " << ex_it->oid
<< " " << ex_it->offset << "~" << ex_it->length << dendl;
loff_t cur = ex_it->offset;
loff_t left = ex_it->length;
map<loff_t, BufferHead*>::iterator p = data_lower_bound(ex_it->offset);
while (left > 0) {
loff_t max = left;
// at end ?
if (p == data.end()) {
if (final == NULL) {
final = new BufferHead(this);
final->set_start( cur );
final->set_length( max );
oc->bh_add(this, final);
ldout(oc->cct, 10) << "map_write adding trailing bh " << *final << dendl;
} else {
oc->bh_stat_sub(final);
final->set_length(final->length() + max);
oc->bh_stat_add(final);
}
left -= max;
cur += max;
continue;
}
ldout(oc->cct, 10) << "cur is " << cur << ", p is " << *p->second << dendl;
//oc->verify_stats();
if (p->first <= cur) {
BufferHead *bh = p->second;
ldout(oc->cct, 10) << "map_write bh " << *bh << " intersected" << dendl;
if (p->first < cur) {
assert(final == 0);
if (cur + max >= p->first + p->second->length()) {
// we want right bit (one splice)
final = split(bh, cur); // just split it, take right half.
++p;
assert(p->second == final);
} else {
// we want middle bit (two splices)
final = split(bh, cur);
++p;
assert(p->second == final);
split(final, cur+max);
}
} else {
assert(p->first == cur);
if (p->second->length() <= max) {
// whole bufferhead, piece of cake.
} else {
// we want left bit (one splice)
split(bh, cur + max); // just split
}
if (final) {
oc->mark_dirty(bh);
oc->mark_dirty(final);
--p; // move iterator back to final
assert(p->second == final);
merge_left(final, bh);
} else {
final = bh;
}
}
// keep going.
loff_t lenfromcur = final->end() - cur;
cur += lenfromcur;
left -= lenfromcur;
++p;
continue;
} else {
// gap!
loff_t next = p->first;
loff_t glen = MIN(next - cur, max);
ldout(oc->cct, 10) << "map_write gap " << cur << "~" << glen << dendl;
if (final) {
oc->bh_stat_sub(final);
final->set_length(final->length() + glen);
oc->bh_stat_add(final);
} else {
final = new BufferHead(this);
final->set_start( cur );
final->set_length( glen );
oc->bh_add(this, final);
}
cur += glen;
left -= glen;
continue; // more?
}
}
}
// set versoin
assert(final);
ldout(oc->cct, 10) << "map_write final is " << *final << dendl;
return final;
}
void ObjectCacher::Object::truncate(loff_t s)
{
assert(oc->lock.is_locked());
ldout(oc->cct, 10) << "truncate " << *this << " to " << s << dendl;
while (!data.empty()) {
BufferHead *bh = data.rbegin()->second;
if (bh->end() <= s)
break;
// split bh at truncation point?
if (bh->start() < s) {
split(bh, s);
continue;
}
// remove bh entirely
assert(bh->start() >= s);
assert(bh->waitfor_read.empty());
oc->bh_remove(this, bh);
delete bh;
}
}
void ObjectCacher::Object::discard(loff_t off, loff_t len)
{
assert(oc->lock.is_locked());
ldout(oc->cct, 10) << "discard " << *this << " " << off << "~" << len << dendl;
if (!exists) {
ldout(oc->cct, 10) << " setting exists on " << *this << dendl;
exists = true;
}
if (complete) {
ldout(oc->cct, 10) << " clearing complete on " << *this << dendl;
complete = false;
}
map<loff_t, BufferHead*>::iterator p = data_lower_bound(off);
while (p != data.end()) {
BufferHead *bh = p->second;
if (bh->start() >= off + len)
break;
// split bh at truncation point?
if (bh->start() < off) {
split(bh, off);
++p;
continue;
}
assert(bh->start() >= off);
if (bh->end() > off + len) {
split(bh, off + len);
}
++p;
ldout(oc->cct, 10) << "discard " << *this << " bh " << *bh << dendl;
assert(bh->waitfor_read.empty());
oc->bh_remove(this, bh);
delete bh;
}
}
/*** ObjectCacher ***/
#undef dout_prefix
#define dout_prefix *_dout << "objectcacher "
ObjectCacher::ObjectCacher(CephContext *cct_, string name, WritebackHandler& wb, Mutex& l,
flush_set_callback_t flush_callback,
void *flush_callback_arg,
uint64_t max_bytes, uint64_t max_objects,
uint64_t max_dirty, uint64_t target_dirty,
double max_dirty_age, bool block_writes_upfront)
: perfcounter(NULL),
cct(cct_), writeback_handler(wb), name(name), lock(l),
max_dirty(max_dirty), target_dirty(target_dirty),
max_size(max_bytes), max_objects(max_objects),
block_writes_upfront(block_writes_upfront),
flush_set_callback(flush_callback), flush_set_callback_arg(flush_callback_arg),
last_read_tid(0),
flusher_stop(false), flusher_thread(this), finisher(cct),
stat_clean(0), stat_zero(0), stat_dirty(0), stat_rx(0), stat_tx(0), stat_missing(0),
stat_error(0), stat_dirty_waiting(0), reads_outstanding(0)
{
this->max_dirty_age.set_from_double(max_dirty_age);
perf_start();
finisher.start();
}
ObjectCacher::~ObjectCacher()
{
finisher.stop();
perf_stop();
// we should be empty.
for (vector<ceph::unordered_map<sobject_t, Object *> >::iterator i = objects.begin();
i != objects.end();
++i)
assert(i->empty());
assert(bh_lru_rest.lru_get_size() == 0);
assert(bh_lru_dirty.lru_get_size() == 0);
assert(ob_lru.lru_get_size() == 0);
assert(dirty_or_tx_bh.empty());
}
void ObjectCacher::perf_start()
{
string n = "objectcacher-" + name;
PerfCountersBuilder plb(cct, n, l_objectcacher_first, l_objectcacher_last);
plb.add_u64_counter(l_objectcacher_cache_ops_hit, "cache_ops_hit");
plb.add_u64_counter(l_objectcacher_cache_ops_miss, "cache_ops_miss");
plb.add_u64_counter(l_objectcacher_cache_bytes_hit, "cache_bytes_hit");
plb.add_u64_counter(l_objectcacher_cache_bytes_miss, "cache_bytes_miss");
plb.add_u64_counter(l_objectcacher_data_read, "data_read");
plb.add_u64_counter(l_objectcacher_data_written, "data_written");
plb.add_u64_counter(l_objectcacher_data_flushed, "data_flushed");
plb.add_u64_counter(l_objectcacher_overwritten_in_flush,
"data_overwritten_while_flushing");
plb.add_u64_counter(l_objectcacher_write_ops_blocked, "write_ops_blocked");
plb.add_u64_counter(l_objectcacher_write_bytes_blocked, "write_bytes_blocked");
plb.add_time(l_objectcacher_write_time_blocked, "write_time_blocked");
perfcounter = plb.create_perf_counters();
cct->get_perfcounters_collection()->add(perfcounter);
}
void ObjectCacher::perf_stop()
{
assert(perfcounter);
cct->get_perfcounters_collection()->remove(perfcounter);
delete perfcounter;
}
/* private */
ObjectCacher::Object *ObjectCacher::get_object(sobject_t oid,
uint64_t object_no,
ObjectSet *oset,
object_locator_t &l,
uint64_t truncate_size,
uint64_t truncate_seq)
{
// XXX: Add handling of nspace in object_locator_t in cache
assert(lock.is_locked());
// have it?
if ((uint32_t)l.pool < objects.size()) {
if (objects[l.pool].count(oid)) {
Object *o = objects[l.pool][oid];
o->object_no = object_no;
o->truncate_size = truncate_size;
o->truncate_seq = truncate_seq;
return o;
}
} else {
objects.resize(l.pool+1);
}
// create it.
Object *o = new Object(this, oid, object_no, oset, l, truncate_size,
truncate_seq);
objects[l.pool][oid] = o;
ob_lru.lru_insert_top(o);
return o;
}
void ObjectCacher::close_object(Object *ob)
{
assert(lock.is_locked());
ldout(cct, 10) << "close_object " << *ob << dendl;
assert(ob->can_close());
// ok!
ob_lru.lru_remove(ob);
objects[ob->oloc.pool].erase(ob->get_soid());
ob->set_item.remove_myself();
delete ob;
}
void ObjectCacher::bh_read(BufferHead *bh, int op_flags)
{
assert(lock.is_locked());
ldout(cct, 7) << "bh_read on " << *bh << " outstanding reads "
<< reads_outstanding << dendl;
mark_rx(bh);
bh->last_read_tid = ++last_read_tid;
// finisher
C_ReadFinish *onfinish = new C_ReadFinish(this, bh->ob, bh->last_read_tid,
bh->start(), bh->length());
// go
writeback_handler.read(bh->ob->get_oid(), bh->ob->get_object_number(),
bh->ob->get_oloc(), bh->start(), bh->length(),
bh->ob->get_snap(), &onfinish->bl,
bh->ob->truncate_size, bh->ob->truncate_seq,
op_flags, onfinish);
++reads_outstanding;
}
void ObjectCacher::bh_read_finish(int64_t poolid, sobject_t oid, ceph_tid_t tid,
loff_t start, uint64_t length,
bufferlist &bl, int r,
bool trust_enoent)
{
assert(lock.is_locked());
ldout(cct, 7) << "bh_read_finish "
<< oid
<< " tid " << tid
<< " " << start << "~" << length
<< " (bl is " << bl.length() << ")"
<< " returned " << r
<< " outstanding reads " << reads_outstanding
<< dendl;
if (r >= 0 && bl.length() < length) {
bufferptr bp(length - bl.length());
bp.zero();
ldout(cct, 7) << "bh_read_finish " << oid << " padding " << start << "~" << length
<< " with " << bp.length() << " bytes of zeroes" << dendl;
bl.push_back(bp);
}
list<Context*> ls;
int err = 0;
if (objects[poolid].count(oid) == 0) {
ldout(cct, 7) << "bh_read_finish no object cache" << dendl;
} else {
Object *ob = objects[poolid][oid];
if (r == -ENOENT && !ob->complete) {
// wake up *all* rx waiters, or else we risk reordering identical reads. e.g.
// read 1~1
// reply to unrelated 3~1 -> !exists
// read 1~1 -> immediate ENOENT
// reply to first 1~1 -> ooo ENOENT
bool allzero = true;
for (map<loff_t, BufferHead*>::iterator p = ob->data.begin(); p != ob->data.end(); ++p) {
BufferHead *bh = p->second;
for (map<loff_t, list<Context*> >::iterator p = bh->waitfor_read.begin();
p != bh->waitfor_read.end();
++p)
ls.splice(ls.end(), p->second);
bh->waitfor_read.clear();
if (!bh->is_zero() && !bh->is_rx())
allzero = false;
}
// just pass through and retry all waiters if we don't trust
// -ENOENT for this read
if (trust_enoent) {
ldout(cct, 7) << "bh_read_finish ENOENT, marking complete and !exists on " << *ob << dendl;
ob->complete = true;
ob->exists = false;
/* If all the bhs are effectively zero, get rid of them. All
* the waiters will be retried and get -ENOENT immediately, so
* it's safe to clean up the unneeded bh's now. Since we know
* it's safe to remove them now, do so, so they aren't hanging
*around waiting for more -ENOENTs from rados while the cache
* is being shut down.
*
* Only do this when all the bhs are rx or clean, to match the
* condition in _readx(). If there are any non-rx or non-clean
* bhs, _readx() will wait for the final result instead of
* returning -ENOENT immediately.
*/
if (allzero) {
ldout(cct, 10) << "bh_read_finish ENOENT and allzero, getting rid of "
<< "bhs for " << *ob << dendl;
map<loff_t, BufferHead*>::iterator p = ob->data.begin();
while (p != ob->data.end()) {
BufferHead *bh = p->second;
// current iterator will be invalidated by bh_remove()
++p;
bh_remove(ob, bh);
delete bh;
}
}
}
}
// apply to bh's!
loff_t opos = start;
while (true) {
map<loff_t, BufferHead*>::iterator p = ob->data_lower_bound(opos);
if (p == ob->data.end())
break;
if (opos >= start+(loff_t)length) {
ldout(cct, 20) << "break due to opos " << opos << " >= start+length "
<< start << "+" << length << "=" << start+(loff_t)length
<< dendl;
break;
}
BufferHead *bh = p->second;
ldout(cct, 20) << "checking bh " << *bh << dendl;
// finishers?
for (map<loff_t, list<Context*> >::iterator it = bh->waitfor_read.begin();
it != bh->waitfor_read.end();
++it)
ls.splice(ls.end(), it->second);
bh->waitfor_read.clear();
if (bh->start() > opos) {
ldout(cct, 1) << "bh_read_finish skipping gap "
<< opos << "~" << bh->start() - opos
<< dendl;
opos = bh->start();
continue;
}
if (!bh->is_rx()) {
ldout(cct, 10) << "bh_read_finish skipping non-rx " << *bh << dendl;
opos = bh->end();
continue;
}
if (bh->last_read_tid != tid) {
ldout(cct, 10) << "bh_read_finish bh->last_read_tid " << bh->last_read_tid
<< " != tid " << tid << ", skipping" << dendl;
opos = bh->end();
continue;
}
assert(opos >= bh->start());
assert(bh->start() == opos); // we don't merge rx bh's... yet!
assert(bh->length() <= start+(loff_t)length-opos);
if (bh->error < 0)
err = bh->error;
loff_t oldpos = opos;
opos = bh->end();
if (r == -ENOENT) {
if (trust_enoent) {
ldout(cct, 10) << "bh_read_finish removing " << *bh << dendl;
bh_remove(ob, bh);
delete bh;
} else {
ldout(cct, 10) << "skipping unstrusted -ENOENT and will retry for "
<< *bh << dendl;
}
continue;
}
if (r < 0) {
bh->error = r;
mark_error(bh);
} else {
bh->bl.substr_of(bl,
oldpos-bh->start(),
bh->length());
mark_clean(bh);
}
ldout(cct, 10) << "bh_read_finish read " << *bh << dendl;
ob->try_merge_bh(bh);
}
}
// called with lock held.
ldout(cct, 20) << "finishing waiters " << ls << dendl;
finish_contexts(cct, ls, err);
retry_waiting_reads();
--reads_outstanding;
read_cond.Signal();
}
void ObjectCacher::bh_write(BufferHead *bh)
{
assert(lock.is_locked());
ldout(cct, 7) << "bh_write " << *bh << dendl;
bh->ob->get();
// finishers
C_WriteCommit *oncommit = new C_WriteCommit(this, bh->ob->oloc.pool,
bh->ob->get_soid(), bh->start(), bh->length());
// go
ceph_tid_t tid = writeback_handler.write(bh->ob->get_oid(), bh->ob->get_oloc(),
bh->start(), bh->length(),
bh->snapc, bh->bl, bh->last_write,
bh->ob->truncate_size, bh->ob->truncate_seq,
oncommit);
ldout(cct, 20) << " tid " << tid << " on " << bh->ob->get_oid() << dendl;
// set bh last_write_tid
oncommit->tid = tid;
bh->ob->last_write_tid = tid;
bh->last_write_tid = tid;
if (perfcounter) {
perfcounter->inc(l_objectcacher_data_flushed, bh->length());
}
mark_tx(bh);
}
void ObjectCacher::bh_write_commit(int64_t poolid, sobject_t oid, loff_t start,
uint64_t length, ceph_tid_t tid, int r)
{
assert(lock.is_locked());
ldout(cct, 7) << "bh_write_commit "
<< oid
<< " tid " << tid
<< " " << start << "~" << length
<< " returned " << r
<< dendl;
if (objects[poolid].count(oid) == 0) {
ldout(cct, 7) << "bh_write_commit no object cache" << dendl;
} else {
Object *ob = objects[poolid][oid];
int was_dirty_or_tx = ob->oset->dirty_or_tx;
if (!ob->exists) {
ldout(cct, 10) << "bh_write_commit marking exists on " << *ob << dendl;
ob->exists = true;
if (writeback_handler.may_copy_on_write(ob->get_oid(), start, length, ob->get_snap())) {
ldout(cct, 10) << "bh_write_commit may copy on write, clearing complete on " << *ob << dendl;
ob->complete = false;
}
}
// apply to bh's!
for (map<loff_t, BufferHead*>::iterator p = ob->data_lower_bound(start);
p != ob->data.end();
++p) {
BufferHead *bh = p->second;
if (bh->start() > start+(loff_t)length)
break;
if (bh->start() < start &&
bh->end() > start+(loff_t)length) {
ldout(cct, 20) << "bh_write_commit skipping " << *bh << dendl;
continue;
}
// make sure bh is tx
if (!bh->is_tx()) {
ldout(cct, 10) << "bh_write_commit skipping non-tx " << *bh << dendl;
continue;
}
// make sure bh tid matches
if (bh->last_write_tid != tid) {
assert(bh->last_write_tid > tid);
ldout(cct, 10) << "bh_write_commit newer tid on " << *bh << dendl;
continue;
}
if (r >= 0) {
// ok! mark bh clean and error-free
mark_clean(bh);
ldout(cct, 10) << "bh_write_commit clean " << *bh << dendl;
} else {
mark_dirty(bh);
ldout(cct, 10) << "bh_write_commit marking dirty again due to error "
<< *bh << " r = " << r << " " << cpp_strerror(-r)
<< dendl;
}
}
// update last_commit.
assert(ob->last_commit_tid < tid);
ob->last_commit_tid = tid;
// waiters?
list<Context*> ls;
if (ob->waitfor_commit.count(tid)) {
ls.splice(ls.begin(), ob->waitfor_commit[tid]);
ob->waitfor_commit.erase(tid);
}
// is the entire object set now clean and fully committed?
ObjectSet *oset = ob->oset;
ob->put();
if (flush_set_callback &&
was_dirty_or_tx > 0 &&
oset->dirty_or_tx == 0) { // nothing dirty/tx
flush_set_callback(flush_set_callback_arg, oset);
}
if (!ls.empty())
finish_contexts(cct, ls, r);
}
}
void ObjectCacher::flush(loff_t amount)
{
assert(lock.is_locked());
utime_t cutoff = ceph_clock_now(cct);
ldout(cct, 10) << "flush " << amount << dendl;
/*
* NOTE: we aren't actually pulling things off the LRU here, just looking at the
* tail item. Then we call bh_write, which moves it to the other LRU, so that we
* can call lru_dirty.lru_get_next_expire() again.
*/
loff_t did = 0;
while (amount == 0 || did < amount) {
BufferHead *bh = static_cast<BufferHead*>(bh_lru_dirty.lru_get_next_expire());
if (!bh) break;
if (bh->last_write > cutoff) break;
did += bh->length();
bh_write(bh);
}
}
void ObjectCacher::trim()
{
assert(lock.is_locked());
ldout(cct, 10) << "trim start: bytes: max " << max_size << " clean " << get_stat_clean()
<< ", objects: max " << max_objects << " current " << ob_lru.lru_get_size()
<< dendl;
while (get_stat_clean() > 0 && (uint64_t) get_stat_clean() > max_size) {
BufferHead *bh = static_cast<BufferHead*>(bh_lru_rest.lru_expire());
if (!bh)
break;
ldout(cct, 10) << "trim trimming " << *bh << dendl;
assert(bh->is_clean() || bh->is_zero());
Object *ob = bh->ob;
bh_remove(ob, bh);
delete bh;
if (ob->complete) {
ldout(cct, 10) << "trim clearing complete on " << *ob << dendl;
ob->complete = false;