forked from membase/ep-engine
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathep.hh
More file actions
933 lines (792 loc) · 28.8 KB
/
ep.hh
File metadata and controls
933 lines (792 loc) · 28.8 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
/* -*- 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.
*/
#ifndef EP_HH
#define EP_HH 1
#include <pthread.h>
#include <assert.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <stdexcept>
#include <iostream>
#include <queue>
#include <limits>
#include <unistd.h>
#include <set>
#include <list>
#include <queue>
#include <algorithm>
#include <memcached/engine.h>
#include "queueditem.hh"
#include "stats.hh"
#include "locks.hh"
#include "kvstore.hh"
#include "stored-value.hh"
#include "atomic.hh"
#include "dispatcher.hh"
#include "vbucket.hh"
#include "vbucketmap.hh"
#include "item_pager.hh"
#include "mutation_log.hh"
#include "mutation_log_compactor.hh"
#include "bgfetcher.hh"
#define MAX_BG_FETCH_DELAY 900
/**
* vbucket-aware hashtable visitor.
*/
class VBucketVisitor : public HashTableVisitor {
public:
VBucketVisitor() : HashTableVisitor() { }
VBucketVisitor(const VBucketFilter &filter) :
HashTableVisitor(), vBucketFilter(filter) { }
/**
* Begin visiting a bucket.
*
* @param vb the vbucket we are beginning to visit
*
* @return true iff we want to walk the hashtable in this vbucket
*/
virtual bool visitBucket(RCPtr<VBucket> &vb) {
if (vBucketFilter(vb->getId())) {
currentBucket = vb;
return true;
}
return false;
}
// This is unused in all implementations so far.
void visit(StoredValue* v) {
(void)v;
abort();
}
const VBucketFilter &getVBucketFilter() {
return vBucketFilter;
}
/**
* Called after all vbuckets have been visited.
*/
virtual void complete() { }
/**
* Return true if visiting vbuckets should be paused temporarily.
*/
virtual bool pauseVisitor() {
return false;
}
protected:
VBucketFilter vBucketFilter;
RCPtr<VBucket> currentBucket;
};
typedef std::pair<int64_t, int64_t> chunk_range_t;
typedef std::list<chunk_range_t>::iterator chunk_range_iterator_t;
// Forward declaration
class Flusher;
class Warmup;
class TapBGFetchCallback;
class EventuallyPersistentStore;
class PersistenceCallback;
/**
* Maintains scope of a underlying storage transaction, being useful
* and what not.
*/
class TransactionContext {
public:
TransactionContext(EPStats &st, KVStore *ks, MutationLog &log)
: stats(st), underlying(ks), mutationLog(log),
tranStartTime(0),intxn(false) {}
/**
* Call this whenever entering a transaction.
*
* @return true if we're in a transaction
*/
bool enter();
/**
* Explicitly commit a transaction.
*/
void commit();
/**
* Get the current number of updates permitted per transaction.
*/
int getTxnSize() {
return txnSize.get();
}
/**
* Set the current number of updates permitted per transaction.
*/
void setTxnSize(int to) {
txnSize.set(to);
underlying->processTxnSizeChange(to);
}
void addUncommittedItem(const queued_item &item);
size_t getNumUncommittedItems() {
return numUncommittedItems;
}
/**
* Return the last transaction time per item in millisecond.
*/
double getTransactionTimePerItem() {
return lastTranTimePerItem;
}
void addCallback(PersistenceCallback *cb) {
transactionCallbacks.push_back(cb);
}
private:
EPStats &stats;
KVStore *underlying;
MutationLog &mutationLog;
Atomic<int> txnSize;
Atomic<size_t> numUncommittedItems;
Atomic<double> lastTranTimePerItem;
hrtime_t tranStartTime;
bool intxn;
std::list<queued_item> uncommittedItems;
std::list<PersistenceCallback*> transactionCallbacks;
};
/**
* VBucket visitor callback adaptor.
*/
class VBCBAdaptor : public DispatcherCallback {
public:
VBCBAdaptor(EventuallyPersistentStore *s,
shared_ptr<VBucketVisitor> v, const char *l, double sleep=0);
std::string description() {
std::stringstream rv;
rv << label << " on vb " << currentvb;
return rv.str();
}
bool callback(Dispatcher &d, TaskId t);
private:
std::queue<uint16_t> vbList;
EventuallyPersistentStore *store;
shared_ptr<VBucketVisitor> visitor;
const char *label;
double sleepTime;
uint16_t currentvb;
DISALLOW_COPY_AND_ASSIGN(VBCBAdaptor);
};
class EventuallyPersistentEngine;
typedef enum {
BG_FETCH_VALUE,
BG_FETCH_METADATA
} bg_fetch_type_t;
/**
* Manager of all interaction with the persistence.
*/
class EventuallyPersistentStore {
public:
EventuallyPersistentStore(EventuallyPersistentEngine &theEngine,
KVStore *t, bool startVb0,
bool concurrentDB);
~EventuallyPersistentStore();
void initialize();
/**
* Set an item in the store.
* @param item the item to set
* @param cookie the cookie representing the client to store the item
* @param force override access to the vbucket even if the state of the
* vbucket would deny mutations.
* @param trackReference true if we want to set the nru bit for the item
* @return the result of the store operation
*/
ENGINE_ERROR_CODE set(const Item &item,
const void *cookie,
bool force = false,
bool trackReference = true);
ENGINE_ERROR_CODE add(const Item &item, const void *cookie);
/**
* Add an TAP backfill item into its corresponding vbucket
* @param item the item to be added
* @param meta contains meta info or not
* @param trackReference true if we want to set the nru bit for the item
* @return the result of the operation
*/
ENGINE_ERROR_CODE addTAPBackfillItem(const Item &item, bool meta,
bool trackReference=false);
/**
* Retrieve a value.
*
* @param key the key to fetch
* @param vbucket the vbucket from which to retrieve the key
* @param cookie the connection cookie
* @param queueBG if true, automatically queue a background fetch if necessary
* @param honorStates if false, fetch a result regardless of state
* @param trackReference true if we want to set the reference bit for the item
*
* @return a GetValue representing the result of the request
*/
GetValue get(const std::string &key, uint16_t vbucket,
const void *cookie, bool queueBG=true,
bool honorStates=true, bool trackReference=true) {
return getInternal(key, vbucket, cookie, queueBG, honorStates,
vbucket_state_active, trackReference);
}
/**
* Retrieve a value from a vbucket in replica state.
*
* @param key the key to fetch
* @param vbucket the vbucket from which to retrieve the key
* @param cookie the connection cookie
* @param queueBG if true, automatically queue a background fetch if necessary
*
* @return a GetValue representing the result of the request
*/
GetValue getReplica(const std::string &key, uint16_t vbucket,
const void *cookie, bool queueBG=true) {
return getInternal(key, vbucket, cookie, queueBG, true,
vbucket_state_replica);
}
/**
* Retrieve the meta data for an item
*
* @parapm key the key to get the meta data for
* @param vbucket the vbucket from which to retrieve the key
* @param cookie the connection cookie
* @param meta where to store the meta informaion
* @param cas where to store the cas information
* @param flags where to store the flags information
*/
ENGINE_ERROR_CODE getMetaData(const std::string &key,
uint16_t vbucket,
const void *cookie,
std::string &meta,
uint64_t &cas,
uint32_t &flags);
/**
* Set an item in the store.
* @param item the item to set
* @param cas value to match
* @param cookie the cookie representing the client to store the item
* @param force override vbucket states
* @param allowExisting set to false if you want set to fail if the
* item exists already
* @param trackReference true if we want to set the nru bit for the item
* @return the result of the store operation
*/
ENGINE_ERROR_CODE setWithMeta(const Item &item,
uint64_t cas,
const void *cookie,
bool force,
bool allowReplace,
bool trackReference = true);
/**
* Retrieve a value, but update its TTL first
*
* @param key the key to fetch
* @param vbucket the vbucket from which to retrieve the key
* @param cookie the connection cookie
* @param queueBG if true, automatically queue a background fetch if necessary
* @param exptime the new expiry time for the object
*
* @return a GetValue representing the result of the request
*/
GetValue getAndUpdateTtl(const std::string &key, uint16_t vbucket,
const void *cookie, bool queueBG, time_t exptime);
/**
* Retrieve an item from the disk for vkey stats
*
* @param key the key to fetch
* @param vbucket the vbucket from which to retrieve the key
* @param cookie the connection cookie
* @param cb callback to return an item fetched from the disk
*
* @return a status resulting form executing the method
*/
ENGINE_ERROR_CODE getFromUnderlying(const std::string &key,
uint16_t vbucket,
const void *cookie,
shared_ptr<Callback<GetValue> > cb);
protocol_binary_response_status evictKey(const std::string &key,
uint16_t vbucket,
const char **msg,
size_t *msg_size,
bool force=false);
/**
* delete an item in the store.
* @param key the key of the item
* @param newSeqno the new seq no of the item
* @param cas the CAS ID for a CASed delete (0 to override)
* @param vbucket the vbucket for the key
* @param cookie the cookie representing the client
* @param force override access to the vbucket even if the state of the
* vbucket would deny mutations.
* @param use_meta delete an item using its meta data
* @param newItem pointer to metadata of new item
* @return the result of the delete operation
*/
ENGINE_ERROR_CODE deleteItem(const std::string &key,
uint64_t cas,
uint16_t vbucket,
const void *cookie,
bool force,
bool use_meta,
ItemMetaData *newItemMeta);
void reset();
/**
* Set the background fetch delay.
*
* This exists for debugging and testing purposes. It
* artificially injects delays into background fetches that are
* performed when the user requests an item whose value is not
* currently resident.
*
* @param to how long to delay before performing a bg fetch
*/
void setBGFetchDelay(uint32_t to) {
bgFetchDelay = to;
}
double getBGFetchDelay(void) { return (double)bgFetchDelay; }
void startDispatcher(void);
void startNonIODispatcher(void);
/**
* Get the current dispatcher.
*
* You can use this to queue io related jobs. Don't do stupid things with
* it.
*/
Dispatcher* getDispatcher(void) {
assert(dispatcher);
return dispatcher;
}
/**
* Get the current read-only IO dispatcher.
*/
Dispatcher* getRODispatcher(void) {
assert(roDispatcher);
return roDispatcher;
}
/**
* True if the RW dispatcher and RO dispatcher are distinct.
*/
bool hasSeparateRODispatcher() {
return dispatcher != roDispatcher;
}
/**
* Get the current tap-replication IO dispatcher.
*/
Dispatcher* getTapDispatcher(void) {
assert(tapDispatcher);
return tapDispatcher;
}
/**
* True if the RO dispatcher and tap-replication dispatcher are distinct.
*/
bool hasSeparateTapDispatcher() {
return roDispatcher != tapDispatcher;
}
/**
* Get the current non-io dispatcher.
*
* Use this dispatcher to queue non-io jobs.
*/
Dispatcher* getNonIODispatcher(void) {
assert(nonIODispatcher);
return nonIODispatcher;
}
void stopFlusher(void);
void startFlusher(void);
bool pauseFlusher(void);
bool resumeFlusher(void);
void wakeUpFlusher(void);
void startBgFetcher(void);
void stopBgFetcher(void);
/**
* Enqueue a background fetch for a key.
*
* @param key the key to be bg fetched
* @param vbucket the vbucket in which the key lives
* @param rowid the rowid of the record within its shard
* @param cookie the cookie of the requestor
* @param type whether the fetch is for a non-resident value or metadata of
* a (possibly) deleted item
*/
void bgFetch(const std::string &key,
uint16_t vbucket,
uint64_t rowid,
const void *cookie,
bg_fetch_type_t type = BG_FETCH_VALUE);
/**
* Complete a background fetch of a non resident value or metadata.
*
* @param key the key that was fetched
* @param vbucket the vbucket in which the key lived
* @param rowid the rowid of the record within its shard
* @param cookie the cookie of the requestor
* @param init the timestamp of when the request came in
* @param type whether the fetch is for a non-resident value or metadata of
* a (possibly) deleted item
*/
void completeBGFetch(const std::string &key,
uint16_t vbucket,
uint64_t rowid,
const void *cookie,
hrtime_t init,
bg_fetch_type_t type);
/**
* Complete a batch of background fetch of a non resident value or metadata.
*
* @param vbId the vbucket in which the requested key lived
* @param fetchedItems vector of completed background feches containing key,
* value, client cookies
* @param start the time when the background fetch was started
*
*/
void completeBGFetchMulti(uint16_t vbId,
std::vector<VBucketBGFetchItem *> &fetchedItems,
hrtime_t start);
/**
* Helper function to update stats after completion of a background fetch
* for either the value of metadata of a key.
*
* @param init the time of epstore's initialization
* @param start the time when the background fetch was started
* @param stop the time when the background fetch completed
*/
void updateBGStats(const hrtime_t init,
const hrtime_t start,
const hrtime_t stop);
RCPtr<VBucket> getVBucket(uint16_t vbid) {
return vbuckets.getBucket(vbid);
}
uint64_t getLastPersistedCheckpointId(uint16_t vb) {
return vbuckets.getPersistenceCheckpointId(vb);
}
void snapshotVBuckets(const Priority &priority);
void setVBucketState(uint16_t vbid,
vbucket_state_t state);
/**
* Perform a fast vbucket deletion.
*/
vbucket_del_result completeVBucketDeletion(uint16_t vbid);
/**
* Deletes a vbucket
*
* @param vbid The vbucket to delete.
* @param c The cookie for this connection. Used in synchronous bucket deletes
* to notify the connection of operation completion.
*/
ENGINE_ERROR_CODE deleteVBucket(uint16_t vbid, const void* c = NULL);
void firePendingVBucketOps();
/**
* Reset a given vbucket from memory and disk. This differs from vbucket deletion in that
* it does not delete the vbucket instance from memory hash table.
*/
bool resetVBucket(uint16_t vbid);
void visit(VBucketVisitor &visitor);
/**
* Run a vbucket visitor with separate jobs per vbucket.
*
* Note that this is asynchronous.
*/
void visit(shared_ptr<VBucketVisitor> visitor, const char *lbl,
Dispatcher *d, const Priority &prio, bool isDaemon=true, double sleepTime=0) {
d->schedule(shared_ptr<DispatcherCallback>(new VBCBAdaptor(this, visitor, lbl, sleepTime)),
NULL, prio, 0, isDaemon);
}
int getTxnSize() {
return tctx.getTxnSize();
}
void setTxnSize(int to) {
tctx.setTxnSize(to);
}
size_t getNumUncommittedItems() {
return tctx.getNumUncommittedItems();
}
double getTransactionTimePerItem() {
return tctx.getTransactionTimePerItem();
}
const Flusher* getFlusher();
Warmup* getWarmup(void) const;
ENGINE_ERROR_CODE getKeyStats(const std::string &key, uint16_t vbucket,
key_stats &kstats, bool wantsDeleted=false);
bool getLocked(const std::string &key, uint16_t vbucket,
Callback<GetValue> &cb,
rel_time_t currentTime, uint32_t lockTimeout,
const void *cookie);
/**
* Retrieve the StoredValue associated with a key/vbucket pair.
*
* @param key the key
* @param vbucket the vbucket's ID
* @param honorStates if false, fetch a result regardless of state
*
* @return a pointer to the StoredValue associated with the key/vbucket,
* if any, NULL otherwise
*/
StoredValue* getStoredValue(const std::string &key,
uint16_t vbucket,
bool honorStates = true);
ENGINE_ERROR_CODE unlockKey(const std::string &key,
uint16_t vbucket,
uint64_t cas,
rel_time_t currentTime);
KVStore* getRWUnderlying() {
// This method might also be called leakAbstraction()
return rwUnderlying;
}
KVStore* getROUnderlying() {
// This method might also be called leakAbstraction()
return roUnderlying;
}
KVStore* getTapUnderlying() {
// This method might also be called leakAbstraction()
return tapUnderlying;
}
void deleteExpiredItems(std::list<std::pair<uint16_t, std::string> > &);
/**
* Get the memoized storage properties from the DB.kv
*/
const StorageProperties getStorageProperties() const {
return storageProperties;
}
void scheduleVBSnapshot(const Priority &priority);
const VBucketMap &getVBuckets() {
return vbuckets;
}
EventuallyPersistentEngine& getEPEngine() {
return engine;
}
size_t getExpiryPagerSleeptime(void) {
LockHolder lh(expiryPager.mutex);
return expiryPager.sleeptime;
}
/**
* During restore from backup we read the most recent values first
* and works our way back until epoch.. We should therefore only
* add values to the backup if they're not there;
*
* @return 0 success, 1 skipped, -1 invalid vbucket
*/
int restoreItem(const Item &itm, enum queue_operation op);
bool isFlushAllScheduled() {
return diskFlushAll.get();
}
void setItemExpiryWindow(size_t value) {
itemExpiryWindow = value;
}
void setVbDelChunkSize(size_t value) {
vbDelChunkSize = value;
}
void setVbChunkDelThresholdTime(size_t value) {
vbChunkDelThresholdTime = value;
}
void setExpiryPagerSleeptime(size_t val);
void setAccessScannerSleeptime(size_t val);
void resetAccessScannerStartTime();
void resetAccessScannerTasktime() {
accessScanner.lastTaskRuntime = gethrtime();
// notify item pager to check access scanner task time
pager.biased = false;
}
/**
* Complete the degraded mode phase by clearing the list of deleted items that
* are received from the upstream master via TAP or from the normal clients
*/
void completeDegradedMode();
/**
* Get access to the mutation log.
*/
const MutationLog *getMutationLog() const { return &mutationLog; }
/**
* Get the config of the mutation log compactor.
*/
MutationLogCompactorConfig &getMutationLogCompactorConfig() {
return mlogCompactorConfig;
}
void incExpirationStat(RCPtr<VBucket> &vb, bool byPager = true) {
if (byPager) {
++stats.expired_pager;
} else {
++stats.expired_access;
}
++vb->numExpiredItems;
}
bool multiBGFetchEnabled() {
return hasSeparateRODispatcher() && storageProperties.hasEfficientGet();
}
void updateCachedResidentRatio(size_t activePerc, size_t replicaPerc) {
cachedResidentRatio.activeRatio.set(activePerc);
cachedResidentRatio.replicaRatio.set(replicaPerc);
}
protected:
// During the warmup phase we might want to enable external traffic
// at a given point in time.. The LoadStorageKvPairCallback will be
// triggered whenever we want to check if we could enable traffic..
friend class LoadStorageKVPairCallback;
void maybeEnableTraffic(void);
// Methods called during warmup
std::map<uint16_t, vbucket_state> loadVBucketState();
void loadSessionStats();
bool warmupFromLog(const std::map<uint16_t, vbucket_state> &state,
shared_ptr<Callback<GetValue> >cb);
void warmupCompleted();
private:
void scheduleVBDeletion(RCPtr<VBucket> &vb,
const void* cookie, double delay);
RCPtr<VBucket> getVBucket(uint16_t vbid, vbucket_state_t wanted_state);
/* Queue an item to be written to persistent layer. */
void queueDirty(RCPtr<VBucket> &vb,
const std::string &key,
uint16_t vbid,
enum queue_operation op,
uint64_t seqno,
int64_t rowid,
bool tapBackfill = false);
/**
* Retrieve a StoredValue and invoke a method on it.
*
* Note that because of complications with void/non-void methods
* and potentially missing StoredValues along with the way I
* actually intend to use this, I don't return any values from
* this.
*
* @param key the item's key to retrieve
* @param vbid the vbucket containing the item
* @param f the method to invoke on the item
* @param arg the argument to supply to the method f
*
* @return true if the object was found and method was invoked
*/
template<typename A>
bool invokeOnLockedStoredValue(const std::string &key, uint16_t vbid,
void (StoredValue::* f)(A),
A &arg) {
RCPtr<VBucket> vb = getVBucket(vbid);
if (!vb) {
return false;
}
int bucket_num(0);
LockHolder lh = vb->ht.getLockedBucket(key, &bucket_num);
StoredValue *v = vb->ht.unlocked_find(key, bucket_num, true);
if (v) {
std::mem_fun(f)(v, arg);
}
return v != NULL;
}
bool diskQueueEmpty();
std::queue<queued_item> *beginFlush();
void pushToOutgoingQueue(std::vector<queued_item> &items);
void requeueRejectedItems(std::queue<queued_item> *rejects);
void completeFlush(rel_time_t flush_start);
int flushSome(std::queue<queued_item> *q,
std::queue<queued_item> *rejectQueue);
int flushOne(std::queue<queued_item> *q,
std::queue<queued_item> *rejectQueue);
int flushOneDeleteAll(void);
int flushOneDelOrSet(const queued_item &qi, std::queue<queued_item> *rejectQueue);
StoredValue *fetchValidValue(RCPtr<VBucket> &vb, const std::string &key,
int bucket_num, bool wantsDeleted=false, bool trackReference=true);
size_t getWriteQueueSize(void);
bool hasItemsForPersistence(void);
GetValue getInternal(const std::string &key, uint16_t vbucket,
const void *cookie, bool queueBG,
bool honorStates,
vbucket_state_t allowedState,
bool trackReference=true);
friend class Warmup;
friend class Flusher;
friend class BGFetchCallback;
friend class VKeyStatBGFetchCallback;
friend class TapBGFetchCallback;
friend class TapConnection;
friend class PersistenceCallback;
friend class Deleter;
friend class VBCBAdaptor;
friend class ItemPager;
friend class PagingVisitor;
EventuallyPersistentEngine &engine;
EPStats &stats;
bool doPersistence;
KVStore *rwUnderlying;
KVStore *roUnderlying;
KVStore *tapUnderlying;
StorageProperties storageProperties;
Dispatcher *dispatcher;
Dispatcher *roDispatcher;
Dispatcher *tapDispatcher;
Dispatcher *nonIODispatcher;
Flusher *flusher;
BgFetcher *bgFetcher;
Warmup *warmupTask;
VBucketMap vbuckets;
SyncObject mutex;
MutationLog mutationLog;
MutationLogCompactorConfig mlogCompactorConfig;
MutationLog accessLog;
// The writing queue is used by the flusher thread to keep
// track of the objects it works on. It should _not_ be used
// by any other threads (because the flusher use it without
// locking...
std::queue<queued_item> writing;
Atomic<size_t> bgFetchQueue;
Atomic<bool> diskFlushAll;
TransactionContext tctx;
Mutex vbsetMutex;
uint32_t bgFetchDelay;
// During restore we're bypassing the checkpoint lists with the
// objects we're restoring, but we need them to be persisted.
// This is solved by using a separate list for those objects.
struct {
Mutex mutex;
std::map<uint16_t, std::vector<queued_item> > items;
// Maintain the list of deleted keys that are received from the upstream
// master via TAP or from the normal clients during online restore.
// As an alternative to std::set, we can consider boost::unordered_set later.
std::set<std::string> itemsDeleted;
} restore;
struct ExpiryPagerDelta {
ExpiryPagerDelta() : sleeptime(0) {}
Mutex mutex;
size_t sleeptime;
TaskId task;
} expiryPager;
struct ALogTask {
ALogTask() : sleeptime(0), lastTaskRuntime(gethrtime()) {}
Mutex mutex;
size_t sleeptime;
TaskId task;
hrtime_t lastTaskRuntime;
} accessScanner;
struct ResidentRatio {
Atomic<size_t> activeRatio;
Atomic<size_t> replicaRatio;
} cachedResidentRatio;
struct ItemPagerInfo {
ItemPagerInfo() : biased(true) {}
bool biased;
} pager;
size_t itemExpiryWindow;
size_t vbDelChunkSize;
size_t vbChunkDelThresholdTime;
DISALLOW_COPY_AND_ASSIGN(EventuallyPersistentStore);
};
/**
* Object whose existence maintains a counter incremented.
*
* When the object is constructed, it increments the given counter,
* when destructed, it decrements the counter.
*/
class BGFetchCounter {
public:
BGFetchCounter(Atomic<size_t> &c) : counter(c) {
++counter;
}
~BGFetchCounter() {
--counter;
assert(counter.get() < GIGANTOR);
}
private:
Atomic<size_t> &counter;
};
#endif /* EP_HH */