MB-24094: Add Item to SequenceList on TTL update
[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,
149             bool justTouch = false) override;
150
151     std::pair<StoredValue*, VBNotifyCtx> addNewStoredValue(
152             const HashTable::HashBucketLock& hbl,
153             const Item& itm,
154             const VBQueueItemCtx* queueItmCtx) override;
155
156     std::tuple<StoredValue*, VBNotifyCtx> softDeleteStoredValue(
157             const HashTable::HashBucketLock& hbl,
158             StoredValue& v,
159             bool onlyMarkDeleted,
160             const VBQueueItemCtx& queueItmCtx,
161             uint64_t bySeqno) override;
162
163     void bgFetch(const DocKey& key,
164                  const void* cookie,
165                  EventuallyPersistentEngine& engine,
166                  int bgFetchDelay,
167                  bool isMeta = false) override;
168
169     ENGINE_ERROR_CODE
170     addTempItemAndBGFetch(HashTable::HashBucketLock& hbl,
171                           const DocKey& key,
172                           const void* cookie,
173                           EventuallyPersistentEngine& engine,
174                           int bgFetchDelay,
175                           bool metadataOnly,
176                           bool isReplication = false) override;
177
178     /**
179      * Helper function to update stats after completion of a background fetch
180      * for either the value of metadata of a key.
181      *
182      * @param init the time of epstore's initialization
183      * @param start the time when the background fetch was started
184      * @param stop the time when the background fetch completed
185      */
186     void updateBGStats(const ProcessClock::time_point init,
187                        const ProcessClock::time_point start,
188                        const ProcessClock::time_point stop);
189
190     GetValue getInternalNonResident(const DocKey& key,
191                                     const void* cookie,
192                                     EventuallyPersistentEngine& engine,
193                                     int bgFetchDelay,
194                                     get_options_t options,
195                                     const StoredValue& v) override;
196
197     /* Indicates if multiple bg fetches are handled in a single bg fetch task */
198     const bool multiBGFetchEnabled;
199
200     std::mutex pendingBGFetchesLock;
201     vb_bgfetch_queue_t pendingBGFetches;
202
203     /* Pointer to the shard to which this VBucket belongs to */
204     KVShard* shard;
205
206     /**
207      * When deferred deletion is enabled for this object we store the database
208      * file revision we will unlink from disk.
209      */
210     std::atomic<uint64_t> deferredDeletionFileRevision;
211
212     friend class EPVBucketTest;
213 };