1 /* -*- Mode: C++; tab-width: 4; c-basic-offset: 4; indent-tabs-mode: nil -*- */
2 /*
3  *     Copyright 2018 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 
18 #include "collections/flush.h"
19 #include "collections/collection_persisted_stats.h"
20 #include "collections/vbucket_manifest.h"
21 #include "ep_bucket.h"
22 #include "item.h"
23 
saveCollectionStats( std::function<void(CollectionID, PersistedStats)> cb) const24 void Collections::VB::Flush::saveCollectionStats(
25         std::function<void(CollectionID, PersistedStats)> cb) const {
26     for (const auto c : mutated) {
27         PersistedStats stats;
28         {
29             auto lock = manifest.lock(c);
30             if (!lock.valid()) {
31                 // Can be flushing for a dropped collection (no longer in the
32                 // manifest)
33                 continue;
34             }
35             stats = lock.getPersistedStats();
36         }
37         cb(c, stats);
38     }
39 }
40 
incrementDiskCount(const DocKey& key)41 void Collections::VB::Flush::incrementDiskCount(const DocKey& key) {
42     if (key.getCollectionID() != CollectionID::System) {
43         mutated.insert(key.getCollectionID());
44         manifest.lock(key).incrementDiskCount();
45     }
46 }
47 
decrementDiskCount(const DocKey& key)48 void Collections::VB::Flush::decrementDiskCount(const DocKey& key) {
49     if (key.getCollectionID() != CollectionID::System) {
50         mutated.insert(key.getCollectionID());
51         manifest.lock(key).decrementDiskCount();
52     }
53 }
54 
setPersistedHighSeqno(const DocKey& key, uint64_t value, bool deleted)55 void Collections::VB::Flush::setPersistedHighSeqno(const DocKey& key,
56                                                    uint64_t value,
57                                                    bool deleted) {
58     if (key.getCollectionID() == CollectionID::System) {
59         CollectionID cid;
60         try {
61             // Pass in the Collections specific SystemEvent prefix. We should
62             // throw an exception if this isn't a Collections SystemEvent as
63             // we shouldn't try to lookup and update a collection using say a
64             // ScopeID
65             cid = getCollectionIDFromKey(key,
66                     Collections::CollectionEventPrefixWithSeparator);
67         } catch (std::invalid_argument&) {
68             // Not a collection system event, do nothing
69             return;
70         }
71 
72         // If this system event is a deletion, then it may be the case that we
73         // are the replica and we know nothing about the previous state of this
74         // collection. In this case, we do not want to throw if we cannot find
75         // the collection, we should simply do nothing.
76         manifest.lock().setPersistedHighSeqno(cid, value, deleted);
77     }
78     else {
79         mutated.insert(key.getCollectionID());
80         manifest.lock(key).setPersistedHighSeqno(value);
81     }
82 }
83 
checkAndTriggerPurge(Vbid vbid, KVBucket& bucket) const84 void Collections::VB::Flush::checkAndTriggerPurge(Vbid vbid,
85                                                   KVBucket& bucket) const {
86     if (needsPurge) {
87         triggerPurge(vbid, bucket);
88     }
89 }
90 
triggerPurge(Vbid vbid, KVBucket& bucket)91 void Collections::VB::Flush::triggerPurge(Vbid vbid, KVBucket& bucket) {
92     CompactionConfig config;
93     config.db_file_id = vbid;
94     bucket.scheduleCompaction(vbid, config, nullptr);
95 }