1c0dcb9bc2b02212ecd0ffb172c7a6799b911676
[ep-engine.git] / src / ep_vb.h
1 /* -*- Mode: C++; tab-width: 4; c-basic-offset: 4; indent-tabs-mode: nil -*- */
2 /*
3  *     Copyright 2017 Couchbase, Inc.
4  *
5  *   Licensed under the Apache License, Version 2.0 (the "License");
6  *   you may not use this file except in compliance with the License.
7  *   You may obtain a copy of the License at
8  *
9  *       http://www.apache.org/licenses/LICENSE-2.0
10  *
11  *   Unless required by applicable law or agreed to in writing, software
12  *   distributed under the License is distributed on an "AS IS" BASIS,
13  *   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  *   See the License for the specific language governing permissions and
15  *   limitations under the License.
16  */
17 #pragma once
18
19 #include "config.h"
20 #include "dcp/backfill_disk.h"
21 #include "vbucket.h"
22
23 /**
24  * Eventually Peristent VBucket (EPVBucket) is a child class of VBucket.
25  * It implements the logic of VBucket that is related only to persistence.
26  */
27 class EPVBucket : public VBucket {
28 public:
29     EPVBucket(id_type i,
30               vbucket_state_t newState,
31               EPStats& st,
32               CheckpointConfig& chkConfig,
33               KVShard* kvshard,
34               int64_t lastSeqno,
35               uint64_t lastSnapStart,
36               uint64_t lastSnapEnd,
37               std::unique_ptr<FailoverTable> table,
38               std::shared_ptr<Callback<id_type>> flusherCb,
39               NewSeqnoCallback newSeqnoCb,
40               Configuration& config,
41               item_eviction_policy_t evictionPolicy,
42               vbucket_state_t initState = vbucket_state_dead,
43               uint64_t purgeSeqno = 0,
44               uint64_t maxCas = 0,
45               const std::string& collectionsManifest = "");
46
47     ~EPVBucket();
48
49     ENGINE_ERROR_CODE completeBGFetchForSingleItem(
50             const DocKey& key,
51             const VBucketBGFetchItem& fetched_item,
52             const ProcessClock::time_point startTime) override;
53
54     vb_bgfetch_queue_t getBGFetchItems() override;
55
56     bool hasPendingBGFetchItems() override;
57
58     HighPriorityVBReqStatus checkAddHighPriorityVBEntry(
59             uint64_t seqnoOrChkId,
60             const void* cookie,
61             HighPriorityVBNotify reqType) override;
62
63     void notifyHighPriorityRequests(EventuallyPersistentEngine& engine,
64                                     uint64_t id,
65                                     HighPriorityVBNotify notifyType) override;
66
67     void notifyAllPendingConnsFailed(EventuallyPersistentEngine& e) override;
68
69     size_t getNumItems() const override;
70
71     ENGINE_ERROR_CODE statsVKey(const DocKey& key,
72                                 const void* cookie,
73                                 EventuallyPersistentEngine& engine,
74                                 int bgFetchDelay) override;
75
76     void completeStatsVKey(const DocKey& key,
77                            const RememberingCallback<GetValue>& gcb) override;
78
79     protocol_binary_response_status evictKey(const DocKey& key,
80                                              const char** msg) override;
81
82     bool pageOut(const HashTable::HashBucketLock& lh, StoredValue*& v) override;
83
84     void addStats(bool details, ADD_STAT add_stat, const void* c) override;
85
86     KVShard* getShard() override {
87         return shard;
88     }
89
90     UniqueDCPBackfillPtr createDCPBackfill(EventuallyPersistentEngine& e,
91                                            const active_stream_t& stream,
92                                            uint64_t startSeqno,
93                                            uint64_t endSeqno) override {
94         /* create a disk backfill object */
95         return std::make_unique<DCPBackfillDisk>(
96                 e, stream, startSeqno, endSeqno);
97     }
98
99     uint64_t getPersistenceSeqno() const override {
100         return persistenceSeqno.load();
101     }
102
103     uint64_t getPublicPersistenceSeqno() const override {
104         // For EPVBuckets this is the same as the PersistenceSeqno.
105         return getPersistenceSeqno();
106     }
107
108     void queueBackfillItem(queued_item& qi,
109                            const GenerateBySeqno generateBySeqno) override;
110
111     /**
112      * Setup deferred deletion, this is where deletion of the vbucket is
113      * deferred and completed by an AUXIO task as it will hit disk for the data
114      * file unlink.
115      *
116      * @param cookie A cookie to notify when the deletion task completes.
117      */
118     void setupDeferredDeletion(const void* cookie) override;
119
120     /**
121      * @return the file revision to be unlinked by the deferred deletion task
122      */
123     uint64_t getDeferredDeletionFileRevision() const {
124         return deferredDeletionFileRevision;
125     }
126
127     /**
128      * Schedule a VBucketMemoryAndDiskDeletionTask to delete this object.
129      * @param engine owning engine (required for task construction)
130      */
131     void scheduleDeferredDeletion(EventuallyPersistentEngine& engine) override;
132
133 protected:
134     /**
135      * queue a background fetch of the specified item.
136      * Returns the number of pending background fetches after
137      * adding the specified item.
138      */
139     size_t queueBGFetchItem(const DocKey& key,
140                             std::unique_ptr<VBucketBGFetchItem> fetch,
141                             BgFetcher* bgFetcher);
142
143 private:
144     std::tuple<StoredValue*, MutationStatus, VBNotifyCtx> updateStoredValue(
145             const HashTable::HashBucketLock& hbl,
146             StoredValue& v,
147             const Item& itm,
148             const VBQueueItemCtx* queueItmCtx) override;
149
150     std::pair<StoredValue*, VBNotifyCtx> addNewStoredValue(
151             const HashTable::HashBucketLock& hbl,
152             const Item& itm,
153             const VBQueueItemCtx* queueItmCtx) override;
154
155     std::tuple<StoredValue*, VBNotifyCtx> softDeleteStoredValue(
156             const HashTable::HashBucketLock& hbl,
157             StoredValue& v,
158             bool onlyMarkDeleted,
159             const VBQueueItemCtx& queueItmCtx,
160             uint64_t bySeqno) override;
161
162     void bgFetch(const DocKey& key,
163                  const void* cookie,
164                  EventuallyPersistentEngine& engine,
165                  int bgFetchDelay,
166                  bool isMeta = false) override;
167
168     ENGINE_ERROR_CODE
169     addTempItemAndBGFetch(HashTable::HashBucketLock& hbl,
170                           const DocKey& key,
171                           const void* cookie,
172                           EventuallyPersistentEngine& engine,
173                           int bgFetchDelay,
174                           bool metadataOnly,
175                           bool isReplication = false) override;
176
177     /**
178      * Helper function to update stats after completion of a background fetch
179      * for either the value of metadata of a key.
180      *
181      * @param init the time of epstore's initialization
182      * @param start the time when the background fetch was started
183      * @param stop the time when the background fetch completed
184      */
185     void updateBGStats(const ProcessClock::time_point init,
186                        const ProcessClock::time_point start,
187                        const ProcessClock::time_point stop);
188
189     GetValue getInternalNonResident(const DocKey& key,
190                                     const void* cookie,
191                                     EventuallyPersistentEngine& engine,
192                                     int bgFetchDelay,
193                                     get_options_t options,
194                                     const StoredValue& v) override;
195
196     /* Indicates if multiple bg fetches are handled in a single bg fetch task */
197     const bool multiBGFetchEnabled;
198
199     std::mutex pendingBGFetchesLock;
200     vb_bgfetch_queue_t pendingBGFetches;
201
202     /* Pointer to the shard to which this VBucket belongs to */
203     KVShard* shard;
204
205     /**
206      * When deferred deletion is enabled for this object we store the database
207      * file revision we will unlink from disk.
208      */
209     std::atomic<uint64_t> deferredDeletionFileRevision;
210
211     friend class EPVBucketTest;
212 };