aboutsummaryrefslogtreecommitdiffstats
path: root/searchlib/src/vespa/searchlib/docstore/visitcache.cpp
blob: 2f9b75261f3a55db847161c683011778ed86e6d7 (plain) (blame)
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
// Copyright Vespa.ai. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.

#include "visitcache.h"
#include "ibucketizer.h"
#include <vespa/vespalib/stllike/hash_set.h>
#include <vespa/vespalib/stllike/hash_map.h>
#include <vespa/vespalib/stllike/cache.h>
#include <vespa/vespalib/data/databuffer.h>
#include <vespa/vespalib/util/compressor.h>
#include <vespa/vespalib/util/memory_allocator.h>
#include <vespa/vespalib/stllike/cache.hpp>
#include <vespa/vespalib/stllike/hash_map.hpp>

namespace search::docstore {

using vespalib::CacheStats;
using vespalib::ConstBufferRef;
using vespalib::DataBuffer;
using vespalib::alloc::Alloc;
using vespalib::alloc::MemoryAllocator;

KeySet::KeySet(uint32_t key)
    : _keys()
{
    _keys.push_back(key);
}

KeySet::KeySet(const IDocumentStore::LidVector &keys)
    : _keys(keys)
{
    std::sort(_keys.begin(), _keys.end());
}

bool
KeySet::contains(const KeySet &rhs) const {
    return std::includes(_keys.begin(), _keys.end(), rhs._keys.begin(), rhs._keys.end());
}

BlobSet::BlobSet()
    : _positions(),
      _buffer(Alloc::alloc(0, 16 * MemoryAllocator::HUGEPAGE_SIZE), 0)
{ }

BlobSet::~BlobSet() = default;

namespace {

size_t
getBufferSize(const BlobSet::Positions & p) {
    return p.empty() ? 0 : p.back().offset() + p.back().size();
}

}

BlobSet::BlobSet(Positions positions, Alloc && buffer) noexcept
    : _positions(std::move(positions)),
      _buffer(std::move(buffer), getBufferSize(_positions))
{ }

void
BlobSet::append(uint32_t lid, ConstBufferRef blob) {
    _positions.emplace_back(lid, getBufferSize(_positions), blob.size());
    _buffer.write(blob.c_str(), blob.size());
}

ConstBufferRef
BlobSet::get(uint32_t lid) const
{
    ConstBufferRef buf;
    for (LidPosition pos : _positions) {
        if (pos.lid() == lid) {
            buf = ConstBufferRef(_buffer.data() + pos.offset(), pos.size());
            break;
        }
    }
    return buf;
}

CompressedBlobSet::CompressedBlobSet() noexcept
    : _positions(),
      _buffer(),
      _used(0),
      _compression(CompressionConfig::Type::LZ4)
{ }

CompressedBlobSet::~CompressedBlobSet() = default;

CompressedBlobSet::CompressedBlobSet(CompressionConfig compression, BlobSet uncompressed)
    : _positions(uncompressed.stealPositions()),
      _buffer(),
      _used(0),
      _compression(compression.type)
{
    if ( ! _positions.empty() ) {
        DataBuffer compressed;
        ConstBufferRef org = uncompressed.getBuffer();
        _compression = vespalib::compression::compress(compression, org, compressed, false);
        _used = compressed.getDataLen();
        _buffer = std::make_shared<Alloc>(Alloc::alloc(_used));
        memcpy(_buffer->get(), compressed.getData(), _used);
    } else {
        _buffer = std::make_shared<Alloc>();
    }
}

BlobSet
CompressedBlobSet::getBlobSet() const
{
    using vespalib::compression::decompress;
    // These are frequent lage allocations that are to expensive to mmap.
    DataBuffer uncompressed(0, 1, Alloc::alloc(0, 16 * MemoryAllocator::HUGEPAGE_SIZE));
    if ( ! _positions.empty() ) {
        decompress(_compression, getBufferSize(_positions),
                   ConstBufferRef(_buffer->get(), _used), uncompressed, false);
    }
    return BlobSet(_positions, std::move(uncompressed).stealBuffer());
}

size_t
CompressedBlobSet::bytesAllocated() const {
    return _positions.capacity() * sizeof(BlobSet::Positions::value_type) + _buffer->size();
}

namespace {

class VisitCollector : public IBufferVisitor
{
public:
    VisitCollector(BlobSet & blobSet) : _blobSet(blobSet) { }
    void visit(uint32_t lid, ConstBufferRef buf) override;
private:
    BlobSet & _blobSet;
};

void
VisitCollector::visit(uint32_t lid, ConstBufferRef buf) {
    if (buf.size() > 0) {
        _blobSet.append(lid, buf);
    }
}

struct ByteSize {
    size_t operator() (const CompressedBlobSet & arg) const noexcept { return arg.bytesAllocated(); }
};

}

using CacheParams = vespalib::CacheParam<
        vespalib::LruParam<KeySet, CompressedBlobSet>,
        VisitCache::BackingStore,
        vespalib::zero<KeySet>,
        ByteSize
>;

/**
 * This extends the default thread safe cache implementation so that
 * it will correctly invalidate the cached sets when objects are removed/updated.
 * It will also detect the addition of new objects to any of the sets upon first
 * usage of the set and then invalidate and perform fresh visit of the backing store.
 */
class VisitCache::Cache : public vespalib::cache<CacheParams> {
public:
    Cache(BackingStore & b, size_t maxBytes);
    ~Cache() override;
    CompressedBlobSet readSet(const KeySet & keys);
    void removeKey(uint32_t key);
    vespalib::MemoryUsage getStaticMemoryUsage() const override;
    CacheStats get_stats() const override;
private:
    void locateAndInvalidateOtherSubsets(const UniqueLock & cacheGuard, const KeySet & keys);
    using IdSet = vespalib::hash_set<uint64_t>;
    using Parent = vespalib::cache<CacheParams>;
    using LidUniqueKeySetId = vespalib::hash_map<uint32_t, uint64_t>;
    using IdKeySetMap = vespalib::hash_map<uint64_t, KeySet>;
    IdSet findSetsContaining(const UniqueLock &, const KeySet & keys) const;
    void onInsert(const K & key) override;
    void onRemove(const K & key) override;
    LidUniqueKeySetId _lid2Id;
    IdKeySetMap       _id2KeySet;
};

bool
VisitCache::BackingStore::read(const KeySet &key, CompressedBlobSet &blobs) const {
    BlobSet blobSet;
    blobSet.reserve(key.getKeys().size());
    VisitCollector collector(blobSet);
    _backingStore.read(key.getKeys(), collector);
    blobs = CompressedBlobSet(_compression.load(std::memory_order_relaxed), std::move(blobSet));
    return ! blobs.empty();
}

void
VisitCache::BackingStore::reconfigure(CompressionConfig compression) {
    _compression.store(compression, std::memory_order_relaxed);
}


VisitCache::VisitCache(IDataStore &store, size_t cacheSize, CompressionConfig compression) :
    _store(store, compression),
    _cache(std::make_unique<Cache>(_store, cacheSize))
{ }

VisitCache::~VisitCache() = default;

void
VisitCache::reconfigure(size_t cacheSize, CompressionConfig compression) {
    _store.reconfigure(compression);
    _cache->setCapacityBytes(cacheSize);
}

vespalib::MemoryUsage
VisitCache::getStaticMemoryUsage() const {
    return _cache->getStaticMemoryUsage();
}

VisitCache::Cache::IdSet
VisitCache::Cache::findSetsContaining(const UniqueLock &, const KeySet & keys) const {
    IdSet found;
    for (uint32_t subKey : keys.getKeys()) {
        const auto foundLid = _lid2Id.find(subKey);
        if (foundLid != _lid2Id.end()) {
            found.insert(foundLid->second);
        }
    }
    return found;
}

CompressedBlobSet
VisitCache::Cache::readSet(const KeySet & key)
{
    if (!key.empty()) {
        {
            auto cacheGuard = getGuard();
            if (!hasKey(cacheGuard, key)) {
                locateAndInvalidateOtherSubsets(cacheGuard, key);
            }
        }
        return read(key);
    }
    return CompressedBlobSet();
}

void
VisitCache::Cache::locateAndInvalidateOtherSubsets(const UniqueLock & cacheGuard, const KeySet & keys)
{
    // Due to the implementation of insert where the global lock is released and the fact
    // that 2 overlapping keysets kan have different keys and use different ValueLock
    // We do have a theoretical issue.
    // The reason it is theoretical is that for all practical purpose this inconsitency
    // is prevented by the storage layer above alloing only one visit/mutating operation to a single bucket.
    // So for that reason we will just merge this one to get testing started. 
    // The final fix will come in 2 days.
    IdSet otherSubSets = findSetsContaining(cacheGuard, keys);
    for (uint64_t keyId : otherSubSets) {
        invalidate(cacheGuard, _id2KeySet[keyId]);
    }
}

CompressedBlobSet
VisitCache::read(const IDocumentStore::LidVector & lids) const {
    return _cache->readSet(KeySet(lids));
}

void
VisitCache::remove(uint32_t key) {
    _cache->removeKey(key);
}

CacheStats
VisitCache::getCacheStats() const {
    CacheStats stats = _cache->get_stats();
    return stats;
}

VisitCache::Cache::Cache(BackingStore & b, size_t maxBytes) :
    Parent(b, maxBytes)
{ }

VisitCache::Cache::~Cache() = default;

void
VisitCache::Cache::removeKey(uint32_t subKey) {
    // Need to take hashLock
    auto cacheGuard = getGuard();
    const auto foundLid = _lid2Id.find(subKey);
    if (foundLid != _lid2Id.end()) {
        invalidate(cacheGuard, _id2KeySet[foundLid->second]);
    }
}

void
VisitCache::Cache::onInsert(const K & key) {
    uint32_t first(key.getKeys().front());
    _id2KeySet[first] = key;
    for(uint32_t subKey : key.getKeys()) {
        _lid2Id[subKey] = first;
    }
}

void
VisitCache::Cache::onRemove(const K & key) {
    for (uint32_t subKey : key.getKeys()) {
        _lid2Id.erase(subKey);
    }
    _id2KeySet.erase(key.getKeys().front());
}

vespalib::MemoryUsage
VisitCache::Cache::getStaticMemoryUsage() const {
    vespalib::MemoryUsage usage = Parent::getStaticMemoryUsage();
    size_t baseSelf = sizeof(_lid2Id) + sizeof(_id2KeySet);
    usage.incAllocatedBytes(baseSelf);
    usage.incUsedBytes(baseSelf);
    return usage;
}

CacheStats
VisitCache::Cache::get_stats() const {
    CacheStats stats = Parent::get_stats();
    auto cacheGuard = getGuard();
    stats.memory_used += _lid2Id.capacity() * sizeof(LidUniqueKeySetId::value_type);
    stats.memory_used += _id2KeySet.capacity() * sizeof(IdKeySetMap::value_type);
    for (const auto & entry: _id2KeySet) {
        stats.memory_used = entry.second.getKeys().capacity() * sizeof(uint32_t);
    }
    return stats;
}

}