forked from membase/ep-engine
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcheckpoint.cc
More file actions
1288 lines (1153 loc) · 52.7 KB
/
checkpoint.cc
File metadata and controls
1288 lines (1153 loc) · 52.7 KB
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: 4; c-basic-offset: 4; indent-tabs-mode: nil -*- */
#include "config.h"
#include "vbucket.hh"
#include "checkpoint.hh"
#include "ep_engine.h"
/**
* A listener class to update checkpoint related configs at runtime.
*/
class CheckpointConfigChangeListener : public ValueChangedListener {
public:
CheckpointConfigChangeListener(CheckpointConfig &c) : config(c) { }
virtual ~CheckpointConfigChangeListener() { }
virtual void sizeValueChanged(const std::string &key, size_t value) {
if (key.compare("chk_period") == 0) {
config.setCheckpointPeriod(value);
} else if (key.compare("chk_max_items") == 0) {
config.setCheckpointMaxItems(value);
} else if (key.compare("max_checkpoints") == 0) {
config.setMaxCheckpoints(value);
}
}
virtual void booleanValueChanged(const std::string &key, bool value) {
if (key.compare("inconsistent_slave_chk") == 0) {
config.allowInconsistentSlaveCheckpoint(value);
} else if (key.compare("item_num_based_new_chk") == 0) {
config.allowItemNumBasedNewCheckpoint(value);
} else if (key.compare("keep_closed_chks") == 0) {
config.allowKeepClosedCheckpoints(value);
}
}
private:
CheckpointConfig &config;
};
#define STATWRITER_NAMESPACE checkpoint
#include "statwriter.hh"
#undef STATWRITER_NAMESPACE
Checkpoint::~Checkpoint() {
getLogger()->log(EXTENSION_LOG_INFO, NULL,
"Checkpoint %llu for vbucket %d is purged from memory.\n",
checkpointId, vbucketId);
stats.memOverhead.decr(memorySize());
assert(stats.memOverhead.get() < GIGANTOR);
}
void Checkpoint::setState(checkpoint_state state) {
checkpointState = state;
}
void Checkpoint::popBackCheckpointEndItem() {
if (toWrite.size() > 0 && toWrite.back()->getOperation() == queue_op_checkpoint_end) {
toWrite.pop_back();
}
}
bool Checkpoint::keyExists(const std::string &key) {
return keyIndex.find(key) != keyIndex.end();
}
queue_dirty_t Checkpoint::queueDirty(const queued_item &qi, CheckpointManager *checkpointManager) {
assert (checkpointState == opened);
uint64_t newMutationId = checkpointManager->nextMutationId();
queue_dirty_t rv;
checkpoint_index::iterator it = keyIndex.find(qi->getKey());
// Check if this checkpoint already had an item for the same key.
if (it != keyIndex.end()) {
std::list<queued_item>::iterator currPos = it->second.position;
uint64_t currMutationId = it->second.mutation_id;
CheckpointCursor &pcursor = checkpointManager->persistenceCursor;
if (*(pcursor.currentCheckpoint) == this) {
// If the existing item is in the left-hand side of the item pointed by the
// persistence cursor, decrease the persistence cursor's offset by 1.
const std::string &key = (*(pcursor.currentPos))->getKey();
checkpoint_index::iterator ita = keyIndex.find(key);
if (ita != keyIndex.end()) {
uint64_t mutationId = ita->second.mutation_id;
if (currMutationId <= mutationId) {
checkpointManager->decrCursorOffset_UNLOCKED(pcursor, 1);
}
}
// If the persistence cursor points to the existing item for the same key,
// shift the cursor left by 1.
if (pcursor.currentPos == currPos) {
checkpointManager->decrCursorPos_UNLOCKED(pcursor);
}
}
std::map<const std::string, CheckpointCursor>::iterator map_it;
for (map_it = checkpointManager->tapCursors.begin();
map_it != checkpointManager->tapCursors.end(); map_it++) {
if (*(map_it->second.currentCheckpoint) == this) {
const std::string &key = (*(map_it->second.currentPos))->getKey();
checkpoint_index::iterator ita = keyIndex.find(key);
if (ita != keyIndex.end()) {
uint64_t mutationId = ita->second.mutation_id;
if (currMutationId <= mutationId) {
checkpointManager->decrCursorOffset_UNLOCKED(map_it->second, 1);
}
}
// If an TAP cursor points to the existing item for the same key, shift it left by 1
if (map_it->second.currentPos == currPos) {
checkpointManager->decrCursorPos_UNLOCKED(map_it->second);
}
}
}
queued_item &existing_itm = *currPos;
existing_itm->setOperation(qi->getOperation());
existing_itm->setQueuedTime(qi->getQueuedTime());
toWrite.push_back(existing_itm);
// Remove the existing item for the same key from the list.
toWrite.erase(currPos);
rv = EXISTING_ITEM;
} else {
if (qi->getOperation() == queue_op_set || qi->getOperation() == queue_op_del) {
++numItems;
}
rv = NEW_ITEM;
// Push the new item into the list
toWrite.push_back(qi);
}
if (qi->getKey().size() > 0) {
std::list<queued_item>::iterator last = toWrite.end();
// --last is okay as the list is not empty now.
index_entry entry = {--last, newMutationId};
// Set the index of the key to the new item that is pushed back into the list.
keyIndex[qi->getKey()] = entry;
if (rv == NEW_ITEM) {
size_t newEntrySize = qi->getKey().size() + sizeof(index_entry) + sizeof(queued_item);
memOverhead += newEntrySize;
stats.memOverhead.incr(newEntrySize);
assert(stats.memOverhead.get() < GIGANTOR);
}
}
return rv;
}
size_t Checkpoint::mergePrevCheckpoint(Checkpoint *pPrevCheckpoint) {
size_t numNewItems = 0;
size_t newEntryMemOverhead = 0;
std::list<queued_item>::reverse_iterator rit = pPrevCheckpoint->rbegin();
getLogger()->log(EXTENSION_LOG_INFO, NULL,
"Collapse the checkpoint %llu into the checkpoint %llu for vbucket %d.\n",
pPrevCheckpoint->getId(), checkpointId, vbucketId);
for (; rit != pPrevCheckpoint->rend(); ++rit) {
const std::string &key = (*rit)->getKey();
if (key.size() == 0) {
continue;
}
checkpoint_index::iterator it = keyIndex.find(key);
if (it == keyIndex.end()) {
std::list<queued_item>::iterator pos = toWrite.begin();
// Skip the first two meta items
++pos; ++pos;
toWrite.insert(pos, *rit);
index_entry entry = {--pos, pPrevCheckpoint->getMutationIdForKey(key)};
keyIndex[key] = entry;
newEntryMemOverhead += key.size() + sizeof(index_entry);
++numItems;
++numNewItems;
}
}
memOverhead += newEntryMemOverhead;
stats.memOverhead.incr(newEntryMemOverhead);
assert(stats.memOverhead.get() < GIGANTOR);
return numNewItems;
}
uint64_t Checkpoint::getMutationIdForKey(const std::string &key) {
uint64_t mid = 0;
checkpoint_index::iterator it = keyIndex.find(key);
if (it != keyIndex.end()) {
mid = it->second.mutation_id;
}
return mid;
}
CheckpointManager::~CheckpointManager() {
LockHolder lh(queueLock);
std::list<Checkpoint*>::iterator it = checkpointList.begin();
while(it != checkpointList.end()) {
delete *it;
++it;
}
}
uint64_t CheckpointManager::getOpenCheckpointId_UNLOCKED() {
if (checkpointList.size() == 0) {
return 0;
}
uint64_t id = checkpointList.back()->getId();
return checkpointList.back()->getState() == opened ? id : id + 1;
}
uint64_t CheckpointManager::getOpenCheckpointId() {
LockHolder lh(queueLock);
return getOpenCheckpointId_UNLOCKED();
}
uint64_t CheckpointManager::getLastClosedCheckpointId_UNLOCKED() {
if (!isCollapsedCheckpoint) {
uint64_t id = getOpenCheckpointId_UNLOCKED();
lastClosedCheckpointId = id > 0 ? (id - 1) : 0;
}
return lastClosedCheckpointId;
}
uint64_t CheckpointManager::getLastClosedCheckpointId() {
LockHolder lh(queueLock);
return getLastClosedCheckpointId_UNLOCKED();
}
void CheckpointManager::setOpenCheckpointId_UNLOCKED(uint64_t id) {
if (checkpointList.size() > 0) {
getLogger()->log(EXTENSION_LOG_INFO, NULL,
"Set the current open checkpoint id to %llu for vbucket %d.\n",
id, vbucketId);
checkpointList.back()->setId(id);
// Update the checkpoint_start item with the new Id.
queued_item qi = createCheckpointItem(id, vbucketId, queue_op_checkpoint_start);
std::list<queued_item>::iterator it = ++(checkpointList.back()->begin());
*it = qi;
}
}
bool CheckpointManager::addNewCheckpoint_UNLOCKED(uint64_t id) {
// This is just for making sure that the current checkpoint should be closed.
if (checkpointList.size() > 0 && checkpointList.back()->getState() == opened) {
closeOpenCheckpoint_UNLOCKED(checkpointList.back()->getId());
}
getLogger()->log(EXTENSION_LOG_INFO, NULL,
"Create a new open checkpoint %llu for vbucket %d.\n",
id, vbucketId);
bool empty = checkpointList.empty() ? true : false;
Checkpoint *checkpoint = new Checkpoint(stats, id, vbucketId, opened);
// Add a dummy item into the new checkpoint, so that any cursor referring to the actual first
// item in this new checkpoint can be safely shifted left by 1 if the first item is removed
// and pushed into the tail.
queued_item dummyItem(new QueuedItem("dummy_key", 0xffff, queue_op_empty));
checkpoint->queueDirty(dummyItem, this);
// This item represents the start of the new checkpoint and is also sent to the slave node.
queued_item qi = createCheckpointItem(id, vbucketId, queue_op_checkpoint_start);
checkpoint->queueDirty(qi, this);
++numItems;
checkpointList.push_back(checkpoint);
if (empty) {
return true;
}
// Move the persistence cursor to the next checkpoint if it already reached to
// the end of its current checkpoint.
++(persistenceCursor.currentPos);
if (persistenceCursor.currentPos != (*(persistenceCursor.currentCheckpoint))->end()) {
if ((*(persistenceCursor.currentPos))->getOperation() == queue_op_checkpoint_end) {
// Skip checkpoint_end meta item that is only used by TAP replication cursors.
++(persistenceCursor.offset);
++(persistenceCursor.currentPos); // cursor now reaches to the checkpoint end.
}
}
if (persistenceCursor.currentPos == (*(persistenceCursor.currentCheckpoint))->end()) {
if ((*(persistenceCursor.currentCheckpoint))->getState() == closed) {
uint64_t chkid = (*(persistenceCursor.currentCheckpoint))->getId();
if (moveCursorToNextCheckpoint(persistenceCursor)) {
pCursorPreCheckpointId = chkid;
} else {
--(persistenceCursor.currentPos);
}
} else {
// The persistence cursor is already reached to the end of the open checkpoint.
--(persistenceCursor.currentPos);
}
} else {
--(persistenceCursor.currentPos);
}
return true;
}
bool CheckpointManager::addNewCheckpoint(uint64_t id) {
LockHolder lh(queueLock);
return addNewCheckpoint_UNLOCKED(id);
}
bool CheckpointManager::closeOpenCheckpoint_UNLOCKED(uint64_t id) {
if (checkpointList.size() == 0) {
return false;
}
if (id != checkpointList.back()->getId() || checkpointList.back()->getState() == closed) {
return true;
}
getLogger()->log(EXTENSION_LOG_INFO, NULL,
"Close the open checkpoint %llu for vbucket %d\n", id, vbucketId);
// This item represents the end of the current open checkpoint and is sent to the slave node.
queued_item qi = createCheckpointItem(id, vbucketId, queue_op_checkpoint_end);
checkpointList.back()->queueDirty(qi, this);
++numItems;
checkpointList.back()->setState(closed);
return true;
}
bool CheckpointManager::closeOpenCheckpoint(uint64_t id) {
LockHolder lh(queueLock);
return closeOpenCheckpoint_UNLOCKED(id);
}
void CheckpointManager::registerPersistenceCursor() {
LockHolder lh(queueLock);
assert(checkpointList.size() > 0);
persistenceCursor.currentCheckpoint = checkpointList.begin();
persistenceCursor.currentPos = checkpointList.front()->begin();
checkpointList.front()->registerCursorName(persistenceCursor.name);
}
bool CheckpointManager::registerTAPCursor(const std::string &name, uint64_t checkpointId,
bool closedCheckpointOnly, bool alwaysFromBeginning) {
LockHolder lh(queueLock);
return registerTAPCursor_UNLOCKED(name,
checkpointId,
closedCheckpointOnly,
alwaysFromBeginning);
}
bool CheckpointManager::registerTAPCursor_UNLOCKED(const std::string &name,
uint64_t checkpointId,
bool closedCheckpointOnly,
bool alwaysFromBeginning) {
assert(checkpointList.size() > 0);
bool found = false;
std::list<Checkpoint*>::iterator it = checkpointList.begin();
for (; it != checkpointList.end(); it++) {
if (checkpointId == (*it)->getId()) {
found = true;
break;
}
}
getLogger()->log(EXTENSION_LOG_INFO, NULL,
"Register the tap cursor with the name \"%s\" for vbucket %d.\n",
name.c_str(), vbucketId);
// Get the current open_checkpoint_id. The cursor that grabs items from closed checkpoints
// only walks the checkpoint datastructure until it reaches to the beginning of the
// checkpoint with open_checkpoint_id. One of the typical use cases is the cursor for the
// incremental backup client.
uint64_t open_chk_id = getOpenCheckpointId_UNLOCKED();
// If the tap cursor exists, remove its name from the checkpoint that is
// currently referenced by the tap cursor.
std::map<const std::string, CheckpointCursor>::iterator map_it = tapCursors.find(name);
if (map_it != tapCursors.end()) {
(*(map_it->second.currentCheckpoint))->removeCursorName(name);
}
if (!found) {
getLogger()->log(EXTENSION_LOG_DEBUG, NULL,
"Checkpoint %llu for vbucket %d doesn't exist in memory. "
"Set the cursor with the name \"%s\" to the open checkpoint.\n",
checkpointId, vbucketId, name.c_str());
it = --(checkpointList.end());
CheckpointCursor cursor(name, it, (*it)->begin(),
numItems - ((*it)->getNumItems() + 1), // 1 is for checkpoint start item
closedCheckpointOnly, open_chk_id);
tapCursors[name] = cursor;
(*it)->registerCursorName(name);
} else {
size_t offset = 0;
std::list<queued_item>::iterator curr;
getLogger()->log(EXTENSION_LOG_DEBUG, NULL,
"Checkpoint %llu for vbucket %d exists in memory. "
"Set the cursor with the name \"%s\" to the checkpoint %llu\n",
checkpointId, vbucketId, name.c_str(), checkpointId);
if (!alwaysFromBeginning &&
map_it != tapCursors.end() &&
(*(map_it->second.currentCheckpoint))->getId() == (*it)->getId()) {
// If the cursor is currently in the checkpoint to start with, simply start from
// its current position.
curr = map_it->second.currentPos;
offset = map_it->second.offset;
} else {
// Set the cursor's position to the begining of the checkpoint to start with
curr = (*it)->begin();
std::list<Checkpoint*>::iterator pos = checkpointList.begin();
for (; pos != it; ++pos) {
offset += (*pos)->getNumItems() + 2; // 2 is for checkpoint start and end items.
}
}
CheckpointCursor cursor(name, it, curr, offset, closedCheckpointOnly, open_chk_id);
tapCursors[name] = cursor;
// Register the tap cursor's name to the checkpoint.
(*it)->registerCursorName(name);
}
return found;
}
bool CheckpointManager::removeTAPCursor(const std::string &name) {
LockHolder lh(queueLock);
getLogger()->log(EXTENSION_LOG_INFO, NULL,
"Remove the checkpoint cursor with the name \"%s\" from vbucket %d.\n",
name.c_str(), vbucketId);
std::map<const std::string, CheckpointCursor>::iterator it = tapCursors.find(name);
if (it == tapCursors.end()) {
return false;
}
// We can simply remove the cursor's name from the checkpoint to which it currently belongs,
// by calling
// (*(it->second.currentCheckpoint))->removeCursorName(name);
// However, we just want to do more sanity checks by looking at each checkpoint. This won't
// cause much overhead because the max number of checkpoints allowed per vbucket is small.
std::list<Checkpoint*>::iterator cit = checkpointList.begin();
for (; cit != checkpointList.end(); cit++) {
(*cit)->removeCursorName(name);
}
tapCursors.erase(it);
return true;
}
uint64_t CheckpointManager::getCheckpointIdForTAPCursor(const std::string &name) {
LockHolder lh(queueLock);
std::map<const std::string, CheckpointCursor>::iterator it = tapCursors.find(name);
if (it == tapCursors.end()) {
return 0;
}
return (*(it->second.currentCheckpoint))->getId();
}
size_t CheckpointManager::getNumOfTAPCursors() {
LockHolder lh(queueLock);
return tapCursors.size();
}
size_t CheckpointManager::getNumCheckpoints() {
LockHolder lh(queueLock);
return checkpointList.size();
}
std::list<std::string> CheckpointManager::getTAPCursorNames() {
LockHolder lh(queueLock);
std::list<std::string> cursor_names;
std::map<const std::string, CheckpointCursor>::iterator tap_it = tapCursors.begin();
for (; tap_it != tapCursors.end(); ++tap_it) {
cursor_names.push_back((tap_it->first));
}
return cursor_names;
}
bool CheckpointManager::tapCursorExists(const std::string &name) {
return tapCursors.find(name) != tapCursors.end();
}
bool CheckpointManager::isCheckpointCreationForHighMemUsage(const RCPtr<VBucket> &vbucket) {
bool forceCreation = false;
double memoryUsed = static_cast<double>(stats.getTotalMemoryUsed());
// pesistence and tap cursors are all currently in the open checkpoint?
bool allCursorsInOpenCheckpoint =
(tapCursors.size() + 1) == checkpointList.back()->getNumberOfCursors();
if (memoryUsed > stats.mem_high_wat &&
allCursorsInOpenCheckpoint &&
(checkpointList.back()->getNumItems() >= MIN_CHECKPOINT_ITEMS ||
checkpointList.back()->getNumItems() == vbucket->ht.getNumItems())) {
forceCreation = true;
}
return forceCreation;
}
size_t CheckpointManager::removeClosedUnrefCheckpoints(const RCPtr<VBucket> &vbucket,
bool &newOpenCheckpointCreated) {
// This function is executed periodically by the non-IO dispatcher.
LockHolder lh(queueLock);
assert(vbucket);
uint64_t oldCheckpointId = 0;
bool canCreateNewCheckpoint = false;
if (checkpointList.size() < checkpointConfig.getMaxCheckpoints() ||
(checkpointList.size() == checkpointConfig.getMaxCheckpoints() &&
checkpointList.front()->getNumberOfCursors() == 0)) {
canCreateNewCheckpoint = true;
}
if (vbucket->getState() == vbucket_state_active &&
!checkpointConfig.isInconsistentSlaveCheckpoint() &&
canCreateNewCheckpoint) {
bool forceCreation = isCheckpointCreationForHighMemUsage(vbucket);
// Check if this master active vbucket needs to create a new open checkpoint.
oldCheckpointId = checkOpenCheckpoint_UNLOCKED(forceCreation, true);
}
newOpenCheckpointCreated = oldCheckpointId > 0;
if (oldCheckpointId > 0) {
// If the persistence cursor reached to the end of the old open checkpoint, move it to
// the new open checkpoint.
if ((*(persistenceCursor.currentCheckpoint))->getId() == oldCheckpointId) {
if (++(persistenceCursor.currentPos) ==
(*(persistenceCursor.currentCheckpoint))->end()) {
moveCursorToNextCheckpoint(persistenceCursor);
} else {
decrCursorPos_UNLOCKED(persistenceCursor);
}
}
// If any of TAP cursors reached to the end of the old open checkpoint, move them to
// the new open checkpoint.
std::map<const std::string, CheckpointCursor>::iterator tap_it = tapCursors.begin();
for (; tap_it != tapCursors.end(); ++tap_it) {
CheckpointCursor &cursor = tap_it->second;
if ((*(cursor.currentCheckpoint))->getId() == oldCheckpointId) {
if (++(cursor.currentPos) == (*(cursor.currentCheckpoint))->end()) {
moveCursorToNextCheckpoint(cursor);
} else {
decrCursorPos_UNLOCKED(cursor);
}
}
}
}
if (checkpointConfig.canKeepClosedCheckpoints()) {
double memoryUsed = static_cast<double>(stats.getTotalMemoryUsed());
if (memoryUsed < stats.mem_high_wat &&
checkpointList.size() <= checkpointConfig.getMaxCheckpoints()) {
return 0;
}
}
size_t numUnrefItems = 0;
size_t numCheckpointsRemoved = 0;
std::list<Checkpoint*> unrefCheckpointList;
std::list<Checkpoint*>::iterator it = checkpointList.begin();
for (; it != checkpointList.end(); it++) {
removeInvalidCursorsOnCheckpoint(*it);
if ((*it)->getNumberOfCursors() > 0) {
break;
} else {
numUnrefItems += (*it)->getNumItems() + 2; // 2 is for checkpoint start and end items.
++numCheckpointsRemoved;
if (checkpointConfig.canKeepClosedCheckpoints() &&
(checkpointList.size() - numCheckpointsRemoved) <=
checkpointConfig.getMaxCheckpoints()) {
// Collect unreferenced closed checkpoints until the number of checkpoints is
// equal to the number of max checkpoints allowed.
++it;
break;
}
}
}
if (numUnrefItems > 0) {
numItems -= numUnrefItems;
decrCursorOffset_UNLOCKED(persistenceCursor, numUnrefItems);
std::map<const std::string, CheckpointCursor>::iterator map_it = tapCursors.begin();
for (; map_it != tapCursors.end(); ++map_it) {
decrCursorOffset_UNLOCKED(map_it->second, numUnrefItems);
}
}
unrefCheckpointList.splice(unrefCheckpointList.begin(), checkpointList,
checkpointList.begin(), it);
// If any cursor on a replica vbucket or downstream active vbucket receiving checkpoints from
// the upstream master is very slow and causes more closed checkpoints in memory,
// collapse those closed checkpoints into a single one to reduce the memory overhead.
if (!checkpointConfig.canKeepClosedCheckpoints() &&
(vbucket->getState() == vbucket_state_replica ||
(vbucket->getState() == vbucket_state_active &&
checkpointConfig.isInconsistentSlaveCheckpoint()))) {
collapseClosedCheckpoints(unrefCheckpointList);
}
lh.unlock();
std::list<Checkpoint*>::iterator chkpoint_it = unrefCheckpointList.begin();
for (; chkpoint_it != unrefCheckpointList.end(); chkpoint_it++) {
delete *chkpoint_it;
}
return numUnrefItems;
}
void CheckpointManager::removeInvalidCursorsOnCheckpoint(Checkpoint *pCheckpoint) {
std::list<std::string> invalidCursorNames;
const std::set<std::string> &cursors = pCheckpoint->getCursorNameList();
std::set<std::string>::const_iterator cit = cursors.begin();
for (; cit != cursors.end(); ++cit) {
// Check it with persistence cursor
if ((*cit).compare(persistenceCursor.name) == 0) {
if (pCheckpoint != *(persistenceCursor.currentCheckpoint)) {
invalidCursorNames.push_back(*cit);
}
} else { // Check it with tap cursors
std::map<const std::string, CheckpointCursor>::iterator mit = tapCursors.find(*cit);
if (mit == tapCursors.end() || pCheckpoint != *(mit->second.currentCheckpoint)) {
invalidCursorNames.push_back(*cit);
}
}
}
std::list<std::string>::iterator it = invalidCursorNames.begin();
for (; it != invalidCursorNames.end(); ++it) {
pCheckpoint->removeCursorName(*it);
}
}
void CheckpointManager::collapseClosedCheckpoints(std::list<Checkpoint*> &collapsedChks) {
// If there are one open checkpoint and more than one closed checkpoint, collapse those
// closed checkpoints into one checkpoint to reduce the memory overhead.
if (checkpointList.size() > 2) {
std::set<std::string> slowCursors;
std::set<std::string> fastCursors;
std::list<Checkpoint*>::iterator lastClosedChk = checkpointList.end();
--lastClosedChk; --lastClosedChk; // Move to the lastest closed checkpoint.
fastCursors.insert((*lastClosedChk)->getCursorNameList().begin(),
(*lastClosedChk)->getCursorNameList().end());
std::list<Checkpoint*>::reverse_iterator rit = checkpointList.rbegin();
++rit; ++rit;// Move to the second lastest closed checkpoint.
size_t numDuplicatedItems = 0, numMetaItems = 0;
for (; rit != checkpointList.rend(); ++rit) {
size_t numAddedItems = (*lastClosedChk)->mergePrevCheckpoint(*rit);
numDuplicatedItems += ((*rit)->getNumItems() - numAddedItems);
numMetaItems += 2; // checkpoint start and end meta items
slowCursors.insert((*rit)->getCursorNameList().begin(),
(*rit)->getCursorNameList().end());
}
// Reposition the slow cursors to the beginning of the last closed checkpoint.
std::set<std::string>::iterator sit = slowCursors.begin();
for (; sit != slowCursors.end(); ++sit) {
CheckpointCursor *cursor = NULL;
if ((*sit).compare(persistenceCursor.name) == 0) { // Reposition persistence cursor
cursor = &persistenceCursor;
} else { // Reposition tap cursors
std::map<const std::string, CheckpointCursor>::iterator mit = tapCursors.find(*sit);
if (mit != tapCursors.end()) {
cursor = &(mit->second);
}
}
if (cursor) {
cursor->currentCheckpoint = lastClosedChk;
cursor->currentPos = (*lastClosedChk)->begin();
cursor->offset = 0;
(*lastClosedChk)->registerCursorName(cursor->name);
}
}
numItems -= (numDuplicatedItems + numMetaItems);
Checkpoint *pOpenCheckpoint = checkpointList.back();
const std::set<std::string> &openCheckpointCursors = pOpenCheckpoint->getCursorNameList();
fastCursors.insert(openCheckpointCursors.begin(), openCheckpointCursors.end());
std::set<std::string>::const_iterator cit = fastCursors.begin();
// Update the offset of each fast cursor.
for (; cit != fastCursors.end(); ++cit) {
if ((*cit).compare(persistenceCursor.name) == 0) {
decrCursorOffset_UNLOCKED(persistenceCursor, numDuplicatedItems + numMetaItems);
} else {
std::map<const std::string, CheckpointCursor>::iterator mit = tapCursors.find(*cit);
if (mit != tapCursors.end()) {
decrCursorOffset_UNLOCKED(mit->second, numDuplicatedItems + numMetaItems);
}
}
}
collapsedChks.splice(collapsedChks.end(), checkpointList,
checkpointList.begin(), lastClosedChk);
}
}
bool CheckpointManager::queueDirty(const queued_item &qi, const RCPtr<VBucket> &vbucket) {
LockHolder lh(queueLock);
if (vbucket->getState() != vbucket_state_active &&
checkpointList.back()->getState() == closed) {
// Replica vbucket might receive items from the master even if the current open checkpoint
// has been already closed, because some items from the backfill with an invalid token
// are on the wire even after that backfill thread is closed. Simply ignore those items.
return false;
}
assert(vbucket);
bool canCreateNewCheckpoint = false;
if (checkpointList.size() < checkpointConfig.getMaxCheckpoints() ||
(checkpointList.size() == checkpointConfig.getMaxCheckpoints() &&
checkpointList.front()->getNumberOfCursors() == 0)) {
canCreateNewCheckpoint = true;
}
if (vbucket->getState() == vbucket_state_active &&
!checkpointConfig.isInconsistentSlaveCheckpoint() &&
canCreateNewCheckpoint) {
// Only the master active vbucket can create a next open checkpoint.
checkOpenCheckpoint_UNLOCKED(false, true);
}
// Note that the creation of a new checkpoint on the replica vbucket will be controlled by TAP
// mutation messages from the active vbucket, which contain the checkpoint Ids.
assert(checkpointList.back()->getState() == opened);
size_t numItemsBefore = getNumItemsForPersistence_UNLOCKED();
if (checkpointList.back()->queueDirty(qi, this) == NEW_ITEM) {
++numItems;
}
size_t numItemsAfter = getNumItemsForPersistence_UNLOCKED();
return (numItemsAfter - numItemsBefore) > 0;
}
void CheckpointManager::getAllItemsFromCurrentPosition(CheckpointCursor &cursor,
uint64_t barrier,
std::vector<queued_item> &items) {
while (true) {
if ( barrier > 0 ) {
if ((*(cursor.currentCheckpoint))->getId() >= barrier) {
break;
}
}
while (++(cursor.currentPos) != (*(cursor.currentCheckpoint))->end()) {
items.push_back(*(cursor.currentPos));
}
if ((*(cursor.currentCheckpoint))->getState() == closed) {
if (!moveCursorToNextCheckpoint(cursor)) {
--(cursor.currentPos);
break;
}
} else { // The cursor is currently in the open checkpoint and reached to
// the end() of the open checkpoint.
--(cursor.currentPos);
break;
}
}
}
void CheckpointManager::getAllItemsForPersistence(std::vector<queued_item> &items) {
LockHolder lh(queueLock);
// Get all the items up to the end of the current open checkpoint.
getAllItemsFromCurrentPosition(persistenceCursor, 0, items);
persistenceCursor.offset = numItems;
pCursorPreCheckpointId = getLastClosedCheckpointId_UNLOCKED();
getLogger()->log(EXTENSION_LOG_DEBUG, NULL,
"Grab %ld items through the persistence cursor from vbucket %d.\n",
items.size(), vbucketId);
}
void CheckpointManager::getAllItemsForTAPConnection(const std::string &name,
std::vector<queued_item> &items) {
LockHolder lh(queueLock);
std::map<const std::string, CheckpointCursor>::iterator it = tapCursors.find(name);
if (it == tapCursors.end()) {
getLogger()->log(EXTENSION_LOG_DEBUG, NULL,
"The cursor for TAP connection \"%s\" is not found in the checkpoint.\n",
name.c_str());
return;
}
getAllItemsFromCurrentPosition(it->second, 0, items);
it->second.offset = numItems;
getLogger()->log(EXTENSION_LOG_DEBUG, NULL,
"Grab %ld items through the tap cursor with name \"%s\" from vbucket %d.\n",
items.size(), name.c_str(), vbucketId);
}
queued_item CheckpointManager::nextItem(const std::string &name, bool &isLastMutationItem) {
LockHolder lh(queueLock);
isLastMutationItem = false;
std::map<const std::string, CheckpointCursor>::iterator it = tapCursors.find(name);
if (it == tapCursors.end()) {
getLogger()->log(EXTENSION_LOG_WARNING, NULL,
"The cursor with name \"%s\" is not found in "
"the checkpoint of vbucket %d.\n",
name.c_str(), vbucketId);
queued_item qi(new QueuedItem("", 0xffff, queue_op_empty));
return qi;
}
if (checkpointList.back()->getId() == 0) {
getLogger()->log(EXTENSION_LOG_INFO, NULL,
"VBucket %d is still in backfill phase that doesn't allow "
" the tap cursor to fetch an item from it's current checkpoint.\n",
vbucketId);
queued_item qi(new QueuedItem("", 0xffff, queue_op_empty));
return qi;
}
CheckpointCursor &cursor = it->second;
if ((*(it->second.currentCheckpoint))->getState() == closed) {
return nextItemFromClosedCheckpoint(cursor, isLastMutationItem);
} else {
return nextItemFromOpenedCheckpoint(cursor, isLastMutationItem);
}
}
queued_item CheckpointManager::nextItemFromClosedCheckpoint(CheckpointCursor &cursor,
bool &isLastMutationItem) {
// The cursor already reached to the beginning of the checkpoint that had "open" state
// when registered. Simply return an empty item so that the corresponding TAP client
// can close the connection.
if (cursor.closedCheckpointOnly &&
cursor.openChkIdAtRegistration <= (*(cursor.currentCheckpoint))->getId()) {
queued_item qi(new QueuedItem("", vbucketId, queue_op_empty));
return qi;
}
++(cursor.currentPos);
if (cursor.currentPos != (*(cursor.currentCheckpoint))->end()) {
++(cursor.offset);
isLastMutationItem = isLastMutationItemInCheckpoint(cursor);
return *(cursor.currentPos);
} else {
if (!moveCursorToNextCheckpoint(cursor)) {
--(cursor.currentPos);
queued_item qi(new QueuedItem("", 0xffff, queue_op_empty));
return qi;
}
if ((*(cursor.currentCheckpoint))->getState() == closed) { // the close checkpoint.
++(cursor.currentPos); // Move the cursor to point to the actual first item.
++(cursor.offset);
isLastMutationItem = isLastMutationItemInCheckpoint(cursor);
return *(cursor.currentPos);
} else { // the open checkpoint.
return nextItemFromOpenedCheckpoint(cursor, isLastMutationItem);
}
}
}
queued_item CheckpointManager::nextItemFromOpenedCheckpoint(CheckpointCursor &cursor,
bool &isLastMutationItem) {
if (cursor.closedCheckpointOnly) {
queued_item qi(new QueuedItem("", vbucketId, queue_op_empty));
return qi;
}
++(cursor.currentPos);
if (cursor.currentPos != (*(cursor.currentCheckpoint))->end()) {
++(cursor.offset);
isLastMutationItem = isLastMutationItemInCheckpoint(cursor);
return *(cursor.currentPos);
} else {
--(cursor.currentPos);
queued_item qi(new QueuedItem("", 0xffff, queue_op_empty));
return qi;
}
}
void CheckpointManager::clear(vbucket_state_t vbState) {
LockHolder lh(queueLock);
std::list<Checkpoint*>::iterator it = checkpointList.begin();
// Remove all the checkpoints.
while(it != checkpointList.end()) {
delete *it;
++it;
}
checkpointList.clear();
numItems = 0;
mutationCounter = 0;
uint64_t checkpointId = vbState == vbucket_state_active ? 1 : 0;
// Add a new open checkpoint.
addNewCheckpoint_UNLOCKED(checkpointId);
resetCursors();
}
void CheckpointManager::resetCursors() {
// Reset the persistence cursor.
persistenceCursor.currentCheckpoint = checkpointList.begin();
persistenceCursor.currentPos = checkpointList.front()->begin();
persistenceCursor.offset = 0;
checkpointList.front()->registerCursorName(persistenceCursor.name);
// Reset all the TAP cursors.
std::map<const std::string, CheckpointCursor>::iterator cit = tapCursors.begin();
for (; cit != tapCursors.end(); ++cit) {
cit->second.currentCheckpoint = checkpointList.begin();
cit->second.currentPos = checkpointList.front()->begin();
cit->second.offset = 0;
checkpointList.front()->registerCursorName(cit->second.name);
}
}
void CheckpointManager::resetTAPCursors(const std::list<std::string> &cursors) {
LockHolder lh(queueLock);
std::list<std::string>::const_iterator it = cursors.begin();
for (; it != cursors.end(); it++) {
registerTAPCursor_UNLOCKED(*it, getOpenCheckpointId_UNLOCKED(), false, true);
}
}
bool CheckpointManager::moveCursorToNextCheckpoint(CheckpointCursor &cursor) {
if ((*(cursor.currentCheckpoint))->getState() == opened) {
return false;
} else if ((*(cursor.currentCheckpoint))->getState() == closed) {
std::list<Checkpoint*>::iterator currCheckpoint = cursor.currentCheckpoint;
if (++currCheckpoint == checkpointList.end()) {
return false;
}
}
// Remove the cursor's name from its current checkpoint.
(*(cursor.currentCheckpoint))->removeCursorName(cursor.name);
// Move the cursor to the next checkpoint.
++(cursor.currentCheckpoint);
cursor.currentPos = (*(cursor.currentCheckpoint))->begin();
// Register the cursor's name to its new current checkpoint.
(*(cursor.currentCheckpoint))->registerCursorName(cursor.name);
return true;
}
uint64_t CheckpointManager::checkOpenCheckpoint_UNLOCKED(bool forceCreation, bool timeBound) {
int checkpoint_id = 0;
if (checkpointExtension) {
return checkpoint_id;
}
timeBound = timeBound &&
(ep_real_time() - checkpointList.back()->getCreationTime()) >=
checkpointConfig.getCheckpointPeriod();
// Create the new open checkpoint if any of the following conditions is satisfied:
// (1) force creation due to online update or high memory usage
// (2) current checkpoint is reached to the max number of items allowed.
// (3) time elapsed since the creation of the current checkpoint is greater than the threshold
if (forceCreation ||
(checkpointConfig.isItemNumBasedNewCheckpoint() &&
checkpointList.back()->getNumItems() >= checkpointConfig.getCheckpointMaxItems()) ||
(checkpointList.back()->getNumItems() > 0 && timeBound)) {
checkpoint_id = checkpointList.back()->getId();
closeOpenCheckpoint_UNLOCKED(checkpoint_id);
addNewCheckpoint_UNLOCKED(checkpoint_id + 1);
}
return checkpoint_id;
}
bool CheckpointManager::eligibleForEviction(const std::string &key) {
LockHolder lh(queueLock);
uint64_t smallest_mid = 0;
// Get the mutation id of the item pointed by the slowest cursor.
// This won't cause much overhead as the number of cursors per vbucket is
// usually bounded to 3 (persistence cursor + 2 replicas).
const std::string &pkey = (*(persistenceCursor.currentPos))->getKey();
smallest_mid = (*(persistenceCursor.currentCheckpoint))->getMutationIdForKey(pkey);
std::map<const std::string, CheckpointCursor>::iterator mit = tapCursors.begin();
for (; mit != tapCursors.end(); ++mit) {
const std::string &tkey = (*(mit->second.currentPos))->getKey();
uint64_t mid = (*(mit->second.currentCheckpoint))->getMutationIdForKey(tkey);
if (mid < smallest_mid) {
smallest_mid = mid;
}
}
bool can_evict = true;
std::list<Checkpoint*>::reverse_iterator it = checkpointList.rbegin();
for (; it != checkpointList.rend(); ++it) {
uint64_t mid = (*it)->getMutationIdForKey(key);
if (mid == 0) { // key doesn't exist in a checkpoint.
continue;
}
if (smallest_mid < mid) { // The slowest cursor is still sitting behind a given key.
can_evict = false;
break;
}
}
return can_evict;
}
size_t CheckpointManager::getNumItemsForTAPConnection(const std::string &name) {
LockHolder lh(queueLock);
size_t remains = 0;
std::map<const std::string, CheckpointCursor>::iterator it = tapCursors.find(name);
if (it != tapCursors.end()) {
remains = (numItems >= it->second.offset) ? numItems - it->second.offset : 0;
}
return remains;
}
void CheckpointManager::decrTapCursorFromCheckpointEnd(const std::string &name) {
LockHolder lh(queueLock);
std::map<const std::string, CheckpointCursor>::iterator it = tapCursors.find(name);
if (it != tapCursors.end() &&
(*(it->second.currentPos))->getOperation() == queue_op_checkpoint_end) {
decrCursorOffset_UNLOCKED(it->second, 1);
decrCursorPos_UNLOCKED(it->second);
}
}
bool CheckpointManager::isLastMutationItemInCheckpoint(CheckpointCursor &cursor) {