forked from membase/ep-engine
/
ep.cc
1256 lines (1103 loc) · 40.6 KB
/
ep.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: 4; c-basic-offset: 4; indent-tabs-mode: nil -*- */
/*
* Copyright 2010 NorthScale, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include "config.h"
#include <vector>
#include <time.h>
#include <string.h>
#include <iostream>
#include <functional>
#include "ep.hh"
#include "flusher.hh"
#include "locks.hh"
#include "dispatcher.hh"
#include "sqlite-kvstore.hh"
#include "ep_engine.h"
#include "item_pager.hh"
extern "C" {
static rel_time_t uninitialized_current_time(void) {
abort();
return 0;
}
static time_t default_abs_time(rel_time_t offset) {
/* This is overridden at init time */
return time(NULL) - ep_current_time() + offset;
}
rel_time_t (*ep_current_time)() = uninitialized_current_time;
time_t (*ep_abs_time)(rel_time_t) = default_abs_time;
time_t ep_real_time() {
return ep_abs_time(ep_current_time());
}
}
class BGFetchCallback : public DispatcherCallback {
public:
BGFetchCallback(EventuallyPersistentStore *e,
const std::string &k, uint16_t vbid, uint64_t r,
const void *c) :
ep(e), key(k), vbucket(vbid), rowid(r), cookie(c),
init(gethrtime()), start(0) {
assert(ep);
assert(cookie);
}
bool callback(Dispatcher &d, TaskId t) {
(void)d; (void)t;
start = gethrtime();
ep->completeBGFetch(key, vbucket, rowid, cookie, init, start);
return false;
}
std::string description() {
std::stringstream ss;
ss << "Fetching item from disk: " << key;
return ss.str();
}
private:
EventuallyPersistentStore *ep;
std::string key;
uint16_t vbucket;
uint64_t rowid;
const void *cookie;
hrtime_t init;
hrtime_t start;
};
class VKeyStatBGFetchCallback : public DispatcherCallback {
public:
VKeyStatBGFetchCallback(EventuallyPersistentStore *e,
const std::string &k, uint16_t vbid, uint64_t r,
const void *c, shared_ptr<Callback<GetValue> > cb) :
ep(e), key(k), vbucket(vbid), rowid(r), cookie(c),
lookup_cb(cb) {
assert(ep);
assert(cookie);
assert(lookup_cb);
}
bool callback(Dispatcher &d, TaskId t) {
(void)d; (void)t;
RememberingCallback<GetValue> gcb;
--ep->bgFetchQueue;
ep->getUnderlying()->get(key, rowid, gcb);
gcb.waitForValue();
assert(gcb.fired);
lookup_cb->callback(gcb.val);
return false;
}
std::string description() {
std::stringstream ss;
ss << "Fetching item from disk for vkey stat: " << key;
return ss.str();
}
private:
EventuallyPersistentStore *ep;
std::string key;
uint16_t vbucket;
uint64_t rowid;
const void *cookie;
shared_ptr<Callback<GetValue> > lookup_cb;
};
class SetVBStateCallback : public DispatcherCallback {
public:
SetVBStateCallback(EventuallyPersistentStore *e, uint16_t vb,
const std::string &k)
: ep(e), vbid(vb), key(k) { }
bool callback(Dispatcher &d, TaskId t) {
(void)d; (void)t;
ep->completeSetVBState(vbid, key);
return false;
}
std::string description() {
std::stringstream ss;
ss << "Setting vbucket " << vbid << " state to " << key;
return ss.str();
}
private:
EventuallyPersistentStore *ep;
uint16_t vbid;
std::string key;
};
class NotifyVBStateChangeCallback : public DispatcherCallback {
public:
NotifyVBStateChangeCallback(RCPtr<VBucket> vb, SERVER_HANDLE_V1 *a)
: vbucket(vb), api(a) {
assert(api);
}
bool callback(Dispatcher &d, TaskId t) {
(void)d; (void)t;
vbucket->fireAllOps(api);
return false;
}
std::string description() {
std::stringstream ss;
ss << "Notifying state change of vbucket " << vbucket->getId();
return ss.str();
}
private:
RCPtr<VBucket> vbucket;
SERVER_HANDLE_V1 *api;
};
class VBucketDeletionCallback : public DispatcherCallback {
public:
VBucketDeletionCallback(EventuallyPersistentStore *e, uint16_t vbid)
: ep(e), vbucket(vbid) {
assert(ep);
}
bool callback(Dispatcher &d, TaskId t) {
(void)d; (void)t;
ep->completeVBucketDeletion(vbucket);
return false;
}
std::string description() {
std::stringstream ss;
ss << "Removing vbucket " << vbucket << " from disk.";
return ss.str();
}
private:
EventuallyPersistentStore *ep;
uint16_t vbucket;
};
EventuallyPersistentStore::EventuallyPersistentStore(EventuallyPersistentEngine &theEngine,
StrategicSqlite3 *t,
bool startVb0) :
engine(theEngine), stats(engine.getEpStats()), bgFetchDelay(0)
{
doPersistence = getenv("EP_NO_PERSISTENCE") == NULL;
dispatcher = new Dispatcher();
nonIODispatcher = new Dispatcher();
flusher = new Flusher(this, dispatcher);
stats.memOverhead = sizeof(EventuallyPersistentStore);
setTxnSize(DEFAULT_TXN_SIZE);
underlying = t;
if (startVb0) {
RCPtr<VBucket> vb(new VBucket(0, active, stats));
vbuckets.addBucket(vb);
}
startDispatcher();
startFlusher();
startNonIODispatcher();
assert(underlying);
}
class VerifyStoredVisitor : public HashTableVisitor {
public:
std::vector<std::string> dirty;
void visit(StoredValue *v) {
if (v->isDirty()) {
dirty.push_back(v->getKey());
}
}
};
EventuallyPersistentStore::~EventuallyPersistentStore() {
stopFlusher();
dispatcher->stop();
nonIODispatcher->stop();
delete flusher;
delete dispatcher;
delete nonIODispatcher;
}
void EventuallyPersistentStore::startDispatcher() {
dispatcher->start();
}
void EventuallyPersistentStore::startNonIODispatcher() {
nonIODispatcher->start();
}
const Flusher* EventuallyPersistentStore::getFlusher() {
return flusher;
}
void EventuallyPersistentStore::startFlusher() {
flusher->start();
}
void EventuallyPersistentStore::stopFlusher() {
bool rv = flusher->stop();
if (rv) {
flusher->wait();
}
}
bool EventuallyPersistentStore::pauseFlusher() {
flusher->pause();
return true;
}
bool EventuallyPersistentStore::resumeFlusher() {
flusher->resume();
return true;
}
RCPtr<VBucket> EventuallyPersistentStore::getVBucket(uint16_t vbucket) {
return vbuckets.getBucket(vbucket);
}
RCPtr<VBucket> EventuallyPersistentStore::getVBucket(uint16_t vbid,
vbucket_state_t wanted_state) {
RCPtr<VBucket> vb = vbuckets.getBucket(vbid);
vbucket_state_t found_state(vb ? vb->getState() : dead);
if (found_state == wanted_state) {
return vb;
} else {
RCPtr<VBucket> rv;
return rv;
}
}
class Deleter {
public:
Deleter(EventuallyPersistentStore *ep) : e(ep) {}
void operator() (std::pair<uint16_t, std::string> vk) {
RCPtr<VBucket> vb = e->getVBucket(vk.first);
if (vb) {
int bucket_num = vb->ht.bucket(vk.second);
LockHolder lh(vb->ht.getMutex(bucket_num));
StoredValue *v = vb->ht.unlocked_find(vk.second, bucket_num);
if (v) {
if (vb->ht.unlocked_softDelete(vk.second, bucket_num) == WAS_CLEAN) {
e->queueDirty(vk.second, vb->getId(), queue_op_del);
}
}
}
}
private:
EventuallyPersistentStore *e;
};
void EventuallyPersistentStore::deleteMany(std::list<std::pair<uint16_t, std::string> > &keys) {
// This can be made a lot more efficient, but I'd rather see it
// show up in a profiling report first.
std::for_each(keys.begin(), keys.end(), Deleter(this));
}
StoredValue *EventuallyPersistentStore::fetchValidValue(RCPtr<VBucket> vb,
const std::string &key,
int bucket_num,
bool wantDeleted) {
StoredValue *v = vb->ht.unlocked_find(key, bucket_num, wantDeleted);
if (v && v->isDeleted()) {
// In the deleted case, we ignore expiration time.
return v;
} else if (v && v->isExpired(ep_real_time())) {
++stats.expired;
if (vb->ht.unlocked_softDelete(key, bucket_num) == WAS_CLEAN) {
queueDirty(key, vb->getId(), queue_op_del);
}
return NULL;
}
return v;
}
protocol_binary_response_status EventuallyPersistentStore::evictKey(const std::string &key,
uint16_t vbucket,
const char **msg) {
RCPtr<VBucket> vb = getVBucket(vbucket);
if (!(vb && vb->getState() == active)) {
return PROTOCOL_BINARY_RESPONSE_NOT_MY_VBUCKET;
}
int bucket_num = vb->ht.bucket(key);
LockHolder lh(vb->ht.getMutex(bucket_num));
StoredValue *v = fetchValidValue(vb, key, bucket_num);
protocol_binary_response_status rv(PROTOCOL_BINARY_RESPONSE_SUCCESS);
if (v) {
if (v->isResident()) {
if (v->ejectValue(stats)) {
++stats.numValueEjects;
++stats.numNonResident;
*msg = "Ejected.";
} else {
*msg = "Can't eject: Dirty or a small object.";
}
} else {
*msg = "Already ejected.";
}
} else {
*msg = "Not found.";
rv = PROTOCOL_BINARY_RESPONSE_KEY_ENOENT;
}
return rv;
}
ENGINE_ERROR_CODE EventuallyPersistentStore::set(const Item &item,
const void *cookie,
bool force) {
RCPtr<VBucket> vb = getVBucket(item.getVBucketId());
if (!vb || vb->getState() == dead) {
++stats.numNotMyVBuckets;
return ENGINE_NOT_MY_VBUCKET;
} else if (vb->getState() == active) {
// OK
} else if(vb->getState() == replica && !force) {
++stats.numNotMyVBuckets;
return ENGINE_NOT_MY_VBUCKET;
} else if(vb->getState() == pending && !force) {
if (vb->addPendingOp(cookie)) {
return ENGINE_EWOULDBLOCK;
}
}
bool cas_op = (item.getCas() != 0);
mutation_type_t mtype = vb->ht.set(item, !force);
switch(mtype) {
case NOMEM:
assert(!force);
return ENGINE_ENOMEM;
break;
case INVALID_CAS:
case IS_LOCKED:
return ENGINE_KEY_EEXISTS;
break;
case WAS_DIRTY:
// Do normal stuff, but don't enqueue dirty flags.
break;
case NOT_FOUND:
if (cas_op) {
return ENGINE_KEY_ENOENT;
}
// FALLTHROUGH
case WAS_CLEAN:
queueDirty(item.getKey(), item.getVBucketId(), queue_op_set);
break;
case INVALID_VBUCKET:
return ENGINE_NOT_MY_VBUCKET;
break;
}
return ENGINE_SUCCESS;
}
ENGINE_ERROR_CODE EventuallyPersistentStore::add(const Item &item,
const void *cookie)
{
RCPtr<VBucket> vb = getVBucket(item.getVBucketId());
if (!vb || vb->getState() == dead || vb->getState() == replica) {
++stats.numNotMyVBuckets;
return ENGINE_NOT_MY_VBUCKET;
} else if (vb->getState() == active) {
// OK
} else if(vb->getState() == pending) {
if (vb->addPendingOp(cookie)) {
return ENGINE_EWOULDBLOCK;
}
}
if (item.getCas() != 0) {
// Adding with a cas value doesn't make sense..
return ENGINE_NOT_STORED;
}
switch (vb->ht.add(item)) {
case ADD_NOMEM:
return ENGINE_ENOMEM;
case ADD_EXISTS:
return ENGINE_NOT_STORED;
case ADD_SUCCESS:
case ADD_UNDEL:
queueDirty(item.getKey(), item.getVBucketId(), queue_op_set);
}
return ENGINE_SUCCESS;
}
void EventuallyPersistentStore::completeSetVBState(uint16_t vbid, const std::string &key) {
if (!underlying->setVBState(vbid, key)) {
getLogger()->log(EXTENSION_LOG_DEBUG, NULL,
"Rescheduling a task to set the state of vbucket %d in disc\n", vbid);
dispatcher->schedule(shared_ptr<DispatcherCallback>(new SetVBStateCallback(this, vbid, key)),
NULL, Priority::VBucketPersistPriority, 5, false);
}
}
void EventuallyPersistentStore::setVBucketState(uint16_t vbid,
vbucket_state_t to) {
// Lock to prevent a race condition between a failed update and add.
LockHolder lh(vbsetMutex);
RCPtr<VBucket> vb = vbuckets.getBucket(vbid);
if (vb) {
vb->setState(to, engine.getServerApi());
nonIODispatcher->schedule(shared_ptr<DispatcherCallback>
(new NotifyVBStateChangeCallback(vb,
engine.getServerApi())),
NULL, Priority::NotifyVBStateChangePriority, 0, false);
dispatcher->schedule(shared_ptr<DispatcherCallback>(new SetVBStateCallback(this, vb,
VBucket::toString(to))),
NULL, Priority::VBucketPersistPriority, 0, false);
} else {
RCPtr<VBucket> newvb(new VBucket(vbid, to, stats));
vbuckets.addBucket(newvb);
}
}
void EventuallyPersistentStore::completeVBucketDeletion(uint16_t vbid) {
LockHolder lh(vbsetMutex);
RCPtr<VBucket> vb = vbuckets.getBucket(vbid);
if (!vb || vb->getState() == dead || vbuckets.isBucketDeletion(vbid)) {
lh.unlock();
BlockTimer timer(&stats.diskVBDelHisto);
if (underlying->delVBucket(vbid)) {
vbuckets.setBucketDeletion(vbid, false);
++stats.vbucketDeletions;
} else {
++stats.vbucketDeletionFail;
getLogger()->log(EXTENSION_LOG_DEBUG, NULL,
"Rescheduling a task to delete vbucket %d from disk\n", vbid);
dispatcher->schedule(shared_ptr<DispatcherCallback>(new VBucketDeletionCallback(this, vbid)),
NULL, Priority::VBucketDeletionPriority, 10, false);
}
}
}
bool EventuallyPersistentStore::deleteVBucket(uint16_t vbid) {
// Lock to prevent a race condition between a failed update and add (and delete).
LockHolder lh(vbsetMutex);
bool rv(false);
RCPtr<VBucket> vb = vbuckets.getBucket(vbid);
if (vb && vb->getState() == dead) {
lh.unlock();
vbuckets.setBucketDeletion(vbid, true);
HashTableStatVisitor statvis(vbuckets.removeBucket(vbid));
stats.numNonResident.decr(statvis.numNonResident);
stats.currentSize.decr(statvis.memSize);
assert(stats.currentSize.get() < GIGANTOR);
stats.totalCacheSize.decr(statvis.memSize);
dispatcher->schedule(shared_ptr<DispatcherCallback>(new VBucketDeletionCallback(this, vbid)),
NULL, Priority::VBucketDeletionPriority, 0, false);
rv = true;
}
return rv;
}
void EventuallyPersistentStore::completeBGFetch(const std::string &key,
uint16_t vbucket,
uint64_t rowid,
const void *cookie,
hrtime_t init, hrtime_t start) {
--bgFetchQueue;
++stats.bg_fetched;
std::stringstream ss;
ss << "Completed a background fetch, now at " << bgFetchQueue.get()
<< std::endl;
getLogger()->log(EXTENSION_LOG_DEBUG, NULL, ss.str().c_str());
// Go find the data
RememberingCallback<GetValue> gcb;
underlying->get(key, rowid, gcb);
gcb.waitForValue();
assert(gcb.fired);
// Lock to prevent a race condition between a fetch for restore and delete
LockHolder lh(vbsetMutex);
RCPtr<VBucket> vb = getVBucket(vbucket);
if (vb && vb->getState() == active && gcb.val.getStatus() == ENGINE_SUCCESS) {
int bucket_num = vb->ht.bucket(key);
LockHolder vblh(vb->ht.getMutex(bucket_num));
StoredValue *v = fetchValidValue(vb, key, bucket_num);
if (v) {
if (v->restoreValue(gcb.val.getValue()->getValue(), stats)) {
--stats.numNonResident;
}
}
}
lh.unlock();
hrtime_t stop = gethrtime();
if (stop > start && start > init) {
// skip the measurement if the counter wrapped...
++stats.bgNumOperations;
hrtime_t w = (start - init) / 1000;
stats.bgWaitHisto.add(w);
stats.bgWait += w;
stats.bgMinWait.setIfLess(w);
stats.bgMaxWait.setIfBigger(w);
hrtime_t l = (stop - start) / 1000;
stats.bgLoadHisto.add(l);
stats.bgLoad += l;
stats.bgMinLoad.setIfLess(l);
stats.bgMaxLoad.setIfBigger(l);
}
engine.getServerApi()->cookie->notify_io_complete(cookie, gcb.val.getStatus());
delete gcb.val.getValue();
}
void EventuallyPersistentStore::bgFetch(const std::string &key,
uint16_t vbucket,
uint64_t rowid,
const void *cookie) {
shared_ptr<BGFetchCallback> dcb(new BGFetchCallback(this, key,
vbucket, rowid, cookie));
++bgFetchQueue;
assert(bgFetchQueue > 0);
std::stringstream ss;
ss << "Queued a background fetch, now at " << bgFetchQueue.get()
<< std::endl;
getLogger()->log(EXTENSION_LOG_DEBUG, NULL, ss.str().c_str());
dispatcher->schedule(dcb, NULL, Priority::BgFetcherPriority, bgFetchDelay);
}
GetValue EventuallyPersistentStore::get(const std::string &key,
uint16_t vbucket,
const void *cookie,
bool queueBG, bool honorStates) {
RCPtr<VBucket> vb = getVBucket(vbucket);
if (!vb) {
++stats.numNotMyVBuckets;
return GetValue(NULL, ENGINE_NOT_MY_VBUCKET);
} else if (honorStates && vb->getState() == dead) {
++stats.numNotMyVBuckets;
return GetValue(NULL, ENGINE_NOT_MY_VBUCKET);
} else if (vb->getState() == active) {
// OK
} else if(honorStates && vb->getState() == replica) {
++stats.numNotMyVBuckets;
return GetValue(NULL, ENGINE_NOT_MY_VBUCKET);
} else if(honorStates && vb->getState() == pending) {
if (vb->addPendingOp(cookie)) {
return GetValue(NULL, ENGINE_EWOULDBLOCK);
}
}
int bucket_num = vb->ht.bucket(key);
LockHolder lh(vb->ht.getMutex(bucket_num));
StoredValue *v = fetchValidValue(vb, key, bucket_num);
if (v) {
// If the value is not resident, wait for it...
if (!v->isResident()) {
if (queueBG) {
bgFetch(key, vbucket, v->getId(), cookie);
}
return GetValue(NULL, ENGINE_EWOULDBLOCK, v->getId());
}
// return an invalid cas value if the item is locked
GetValue rv(new Item(v->getKey(), v->getFlags(), v->getExptime(),
v->getValue(),
v->isLocked(ep_current_time()) ? -1 : v->getCas(),
v->getId(), vbucket),
ENGINE_SUCCESS, v->getId());
return rv;
} else {
GetValue rv;
return rv;
}
}
ENGINE_ERROR_CODE
EventuallyPersistentStore::getFromUnderlying(const std::string &key,
uint16_t vbucket,
const void *cookie,
shared_ptr<Callback<GetValue> > cb) {
RCPtr<VBucket> vb = getVBucket(vbucket);
if (!vb || vb->getState() == dead) {
++stats.numNotMyVBuckets;
return ENGINE_NOT_MY_VBUCKET;
} else if (vb->getState() == active) {
// OK
} else if (vb->getState() == replica) {
++stats.numNotMyVBuckets;
return ENGINE_NOT_MY_VBUCKET;
} else if (vb->getState() == pending) {
if (vb->addPendingOp(cookie)) {
return ENGINE_EWOULDBLOCK;
}
}
int bucket_num = vb->ht.bucket(key);
LockHolder lh(vb->ht.getMutex(bucket_num));
StoredValue *v = fetchValidValue(vb, key, bucket_num);
if (v) {
shared_ptr<VKeyStatBGFetchCallback> dcb(new VKeyStatBGFetchCallback(this, key,
vbucket,
v->getId(),
cookie, cb));
++bgFetchQueue;
assert(bgFetchQueue > 0);
dispatcher->schedule(dcb, NULL, Priority::VKeyStatBgFetcherPriority, bgFetchDelay);
return ENGINE_EWOULDBLOCK;
} else {
return ENGINE_KEY_ENOENT;
}
}
bool EventuallyPersistentStore::getLocked(const std::string &key,
uint16_t vbucket,
Callback<GetValue> &cb,
rel_time_t currentTime,
uint32_t lockTimeout) {
RCPtr<VBucket> vb = getVBucket(vbucket, active);
if (!vb) {
++stats.numNotMyVBuckets;
GetValue rv(NULL, ENGINE_NOT_MY_VBUCKET);
cb.callback(rv);
return false;
}
int bucket_num = vb->ht.bucket(key);
LockHolder lh(vb->ht.getMutex(bucket_num));
StoredValue *v = fetchValidValue(vb, key, bucket_num);
if (v) {
if (v->isLocked(currentTime)) {
GetValue rv;
cb.callback(rv);
return false;
}
// acquire lock and increment cas value
v->lock(currentTime + lockTimeout);
Item *it = new Item(v->getKey(), v->getFlags(), v->getExptime(),
v->getValue(), v->getCas());
it->setCas();
v->setCas(it->getCas());
GetValue rv(it);
cb.callback(rv);
} else {
GetValue rv;
cb.callback(rv);
}
return true;
}
bool EventuallyPersistentStore::getKeyStats(const std::string &key,
uint16_t vbucket,
struct key_stats &kstats)
{
RCPtr<VBucket> vb = getVBucket(vbucket, active);
if (!vb) {
return false;
}
bool found = false;
int bucket_num = vb->ht.bucket(key);
LockHolder lh(vb->ht.getMutex(bucket_num));
StoredValue *v = fetchValidValue(vb, key, bucket_num);
found = (v != NULL);
if (found) {
kstats.dirty = v->isDirty();
kstats.exptime = v->getExptime();
kstats.flags = v->getFlags();
kstats.cas = v->getCas();
// TODO: Know this somehow.
kstats.dirtied = 0; // v->getDirtied();
kstats.data_age = v->getDataAge();
kstats.last_modification_time = ep_abs_time(v->getDataAge());
}
return found;
}
void EventuallyPersistentStore::setMinDataAge(int to) {
stats.min_data_age.set(to);
}
void EventuallyPersistentStore::setQueueAgeCap(int to) {
stats.queue_age_cap.set(to);
}
ENGINE_ERROR_CODE EventuallyPersistentStore::del(const std::string &key,
uint16_t vbucket,
const void *cookie) {
RCPtr<VBucket> vb = getVBucket(vbucket);
if (!vb || vb->getState() == dead) {
++stats.numNotMyVBuckets;
return ENGINE_NOT_MY_VBUCKET;
} else if (vb->getState() == active) {
// OK
} else if(vb->getState() == replica) {
++stats.numNotMyVBuckets;
return ENGINE_NOT_MY_VBUCKET;
} else if(vb->getState() == pending) {
if (vb->addPendingOp(cookie)) {
return ENGINE_EWOULDBLOCK;
}
}
mutation_type_t delrv = vb->ht.softDelete(key);
ENGINE_ERROR_CODE rv = delrv == NOT_FOUND ? ENGINE_KEY_ENOENT : ENGINE_SUCCESS;
if (delrv == WAS_CLEAN) {
queueDirty(key, vbucket, queue_op_del);
}
return rv;
}
void EventuallyPersistentStore::reset() {
std::vector<int> buckets = vbuckets.getBuckets();
std::vector<int>::iterator it;
for (it = buckets.begin(); it != buckets.end(); ++it) {
RCPtr<VBucket> vb = getVBucket(*it, active);
if (vb) {
HashTableStatVisitor statvis;
vb->ht.visit(statvis);
vb->ht.clear();
stats.numNonResident.decr(statvis.numNonResident);
stats.currentSize.decr(statvis.memSize);
assert(stats.currentSize.get() < GIGANTOR);
stats.totalCacheSize.decr(statvis.memSize);
}
}
queueDirty("", 0, queue_op_flush);
}
std::queue<QueuedItem>* EventuallyPersistentStore::beginFlush() {
std::queue<QueuedItem> *rv(NULL);
if (towrite.empty() && writing.empty()) {
stats.dirtyAge = 0;
} else {
assert(underlying);
towrite.getAll(writing);
stats.flusher_todo.set(writing.size());
stats.queue_size.set(towrite.size());
getLogger()->log(EXTENSION_LOG_DEBUG, NULL,
"Flushing %d items with %d still in queue\n",
writing.size(), towrite.size());
rv = &writing;
}
return rv;
}
void EventuallyPersistentStore::completeFlush(std::queue<QueuedItem> *rej,
rel_time_t flush_start) {
// Requeue the rejects.
stats.queue_size.incr(rej->size());
while (!rej->empty()) {
writing.push(rej->front());
rej->pop();
}
stats.queue_size.set(towrite.size() + writing.size());
rel_time_t complete_time = ep_current_time();
stats.flushDuration.set(complete_time - flush_start);
stats.flushDurationHighWat.set(std::max(stats.flushDuration.get(),
stats.flushDurationHighWat.get()));
stats.cumulativeFlushTime.incr(complete_time - flush_start);
}
int EventuallyPersistentStore::flushSome(std::queue<QueuedItem> *q,
std::queue<QueuedItem> *rejectQueue) {
int tsz = getTxnSize();
underlying->begin();
int oldest = stats.min_data_age;
for (int i = 0; i < tsz && !q->empty() && bgFetchQueue == 0; i++) {
int n = flushOne(q, rejectQueue);
if (n != 0 && n < oldest) {
oldest = n;
}
}
if (bgFetchQueue > 0) {
++stats.flusherPreempts;
}
rel_time_t cstart = ep_current_time();
BlockTimer timer(&stats.diskCommitHisto);
while (!underlying->commit()) {
sleep(1);
++stats.commitFailed;
}
++stats.flusherCommits;
rel_time_t complete_time = ep_current_time();
stats.commit_time.set(complete_time - cstart);
stats.cumulativeCommitTime.incr(complete_time - cstart);
return oldest;
}
// This class exists to create a closure around a few variables within
// EventuallyPersistentStore::flushOne so that an object can be
// requeued in case of failure to store in the underlying layer.
class PersistenceCallback : public Callback<mutation_result>,
public Callback<int> {
public:
PersistenceCallback(const QueuedItem &qi, std::queue<QueuedItem> *q,
EventuallyPersistentStore *st,
rel_time_t qd, rel_time_t d, EPStats *s) :
queuedItem(qi), rq(q), store(st), queued(qd), dirtied(d), stats(s) {
assert(rq);
assert(s);
}
// This callback is invoked for set only.
void callback(mutation_result &value) {
if (value.first == 1) {
if (value.second > 0) {
++stats->newItems;
setId(value.second);
}
RCPtr<VBucket> vb = store->getVBucket(queuedItem.getVBucketId());
if (vb && vb->getState() != active) {
int bucket_num = vb->ht.bucket(queuedItem.getKey());
LockHolder lh(vb->ht.getMutex(bucket_num));
StoredValue *v = store->fetchValidValue(vb, queuedItem.getKey(),
bucket_num, true);
double current = static_cast<double>(StoredValue::getCurrentSize(*stats));
double lower = static_cast<double>(stats->mem_low_wat);
if (v && current > lower) {
v->ejectValue(*stats);
}
}
} else {
// If the return was 0 here, we're in a bad state because
// we do not know the rowid of this object.
if (value.first == 0) {
getLogger()->log(EXTENSION_LOG_WARNING, NULL,
"Persisting vb%d, returned 0 updates for ``%s''\n",
queuedItem.getVBucketId(), queuedItem.getKey().c_str());
} else {
redirty();
}
}
}
// This callback is invoked for deletions only.
//
// The boolean indicates whether the underlying storage
// successfully deleted the item.
void callback(int &value) {
// > 1 would be bad. We were only trying to delete one row.
assert(value < 2);
// -1 means fail
// 1 means we deleted one row
// 0 means we did not delete a row, but did not fail (did not exist)
if (value >= 0) {
if (value > 0) {
++stats->delItems;
}
// We have succesfully removed an item from the disk, we
// may now remove it from the hash table.
RCPtr<VBucket> vb = store->getVBucket(queuedItem.getVBucketId());
if (vb) {
int bucket_num = vb->ht.bucket(queuedItem.getKey());
LockHolder lh(vb->ht.getMutex(bucket_num));
StoredValue *v = store->fetchValidValue(vb, queuedItem.getKey(),
bucket_num, true);
if (v && v->isDeleted()) {
bool deleted = vb->ht.unlocked_del(queuedItem.getKey(),
bucket_num);
assert(deleted);
} else if (v) {
v->clearId();
}
}
} else {
redirty();
}
}
private:
void setId(int64_t id) {
bool did = store->invokeOnLockedStoredValue(queuedItem.getKey(),
queuedItem.getVBucketId(),
std::mem_fun(&StoredValue::setId),
id);
if (!did) {
getLogger()->log(EXTENSION_LOG_WARNING, NULL,
"Failed to set id on vb%d ``%s''\n",
queuedItem.getVBucketId(), queuedItem.getKey().c_str());
}
}
void redirty() {
stats->memOverhead.incr(queuedItem.size());
assert(stats->memOverhead.get() < GIGANTOR);
++stats->flushFailed;
store->invokeOnLockedStoredValue(queuedItem.getKey(),
queuedItem.getVBucketId(),
std::mem_fun(&StoredValue::reDirty),
dirtied);
rq->push(queuedItem);
}
const QueuedItem queuedItem;
std::queue<QueuedItem> *rq;
EventuallyPersistentStore *store;
rel_time_t queued;
rel_time_t dirtied;
EPStats *stats;
DISALLOW_COPY_AND_ASSIGN(PersistenceCallback);
};
int EventuallyPersistentStore::flushOneDeleteAll() {
underlying->reset();
return 1;
}
// While I actually know whether a delete or set was intended, I'm
// still a bit better off running the older code that figures it out
// based on what's in memory.
int EventuallyPersistentStore::flushOneDelOrSet(QueuedItem &qi,