-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbucket_cache.h
411 lines (354 loc) · 11.5 KB
/
bucket_cache.h
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
// -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*-
// vim: ts=8 sw=2 smarttab ft=cpp
#pragma once
#include <iostream>
#include <memory>
#include <tuple>
#include <vector>
#include <string>
#include <string_view>
#include <mutex>
#include <shared_mutex>
#include <condition_variable>
#include <filesystem>
#include <boost/intrusive/avl_set.hpp>
#include "function2.hpp"
#include "cohort_lru.h"
#include <lmdb-safe.hh>
#include "notify.h"
#include <stdint.h>
#include <xxhash.h>
#undef FMT_HEADER_ONLY
#define FMT_HEADER_ONLY 1
#include <fmt/format.h>
namespace file::listing {
namespace bi = boost::intrusive;
namespace sf = std::filesystem;
typedef bi::link_mode<bi::normal_link> link_mode; /* XXX normal */
typedef bi::avl_set_member_hook<link_mode> member_hook_t;
struct BucketCache;
struct Bucket : public cohort::lru::Object
{
using lock_guard = std::lock_guard<std::mutex>;
using unique_lock = std::unique_lock<std::mutex>;
static constexpr uint32_t FLAG_NONE = 0x0000;
static constexpr uint32_t FLAG_FILLED = 0x0001;
static constexpr uint32_t FLAG_DELETED = 0x0002;
static constexpr uint64_t seed = 8675309;
BucketCache* bc;
std::string name;
std::shared_ptr<MDBEnv> env;
MDBDbi dbi;
uint64_t hk;
member_hook_t name_hook;
// XXX clean this up
std::mutex mtx; // XXX Adam's preferred shared mtx?
std::condition_variable cv;
uint32_t flags;
public:
Bucket(BucketCache* bc, const std::string& name, uint64_t hk)
: bc(bc), name(name), hk(hk), flags(FLAG_NONE) {}
void set_env(std::shared_ptr<MDBEnv>& _env, MDBDbi& _dbi) {
env = _env;
dbi = _dbi;
}
inline bool deleted() const {
return flags & FLAG_DELETED;
}
class Factory : public cohort::lru::ObjectFactory
{
public:
BucketCache* bc;
const std::string& name;
uint64_t hk;
uint32_t flags;
Factory() = delete;
Factory(BucketCache *bc, const std::string& name)
: bc(bc), name(name), flags(FLAG_NONE) {
hk = XXH64(name.c_str(), name.length(), Bucket::seed);
}
void recycle (cohort::lru::Object* o) override {
/* re-use an existing object */
o->~Object(); // call lru::Object virtual dtor
// placement new!
new (o) Bucket(bc, name, hk);
}
cohort::lru::Object* alloc() override {
return new Bucket(bc, name, hk);
}
}; /* Factory */
struct BucketLT
{
// for internal ordering
bool operator()(const Bucket& lhs, const Bucket& rhs) const
{ return (lhs.name < rhs.name); }
// for external search by name
bool operator()(const std::string& k, const Bucket& rhs) const
{ return k < rhs.name; }
bool operator()(const Bucket& lhs, const std::string& k) const
{ return lhs.name < k; }
};
struct BucketEQ
{
bool operator()(const Bucket& lhs, const Bucket& rhs) const
{ return (lhs.name == rhs.name); }
bool operator()(const std::string& k, const Bucket& rhs) const
{ return k == rhs.name; }
bool operator()(const Bucket& lhs, const std::string& k) const
{ return lhs.name == k; }
};
typedef cohort::lru::LRU<std::mutex> bucket_lru;
typedef bi::member_hook<Bucket, member_hook_t, &Bucket::name_hook> name_hook_t;
typedef bi::avltree<Bucket, bi::compare<BucketLT>, name_hook_t> bucket_avl_t;
typedef cohort::lru::TreeX<Bucket, bucket_avl_t, BucketLT, BucketEQ, std::string,
std::mutex> bucket_avl_cache;
bool reclaim(const cohort::lru::ObjectFactory* newobj_fac);
}; /* Bucket */
struct BucketCache : public Notifiable
{
using lock_guard = std::lock_guard<std::mutex>;
using unique_lock = std::unique_lock<std::mutex>;
std::string bucket_root;
uint32_t max_buckets;
std::atomic<uint64_t> recycle_count;
std::unique_ptr<Notify> un;
std::mutex mtx;
/* the bucket lru cache keeps track of the buckets whose listings are
* being cached in lmdb databases and updated from notify */
Bucket::bucket_lru lru;
Bucket::bucket_avl_cache cache;
sf::path rp;
/* the lmdb handle cache maintains a vector of lmdb environments,
* each supports 1 rw and unlimited ro transactions; the materialized
* listing for each bucket is stored as a database in one of these
* environments, selected by a hash of the bucket name; a bucket's database
* is dropped/cleared whenever its entry is reclaimed from cache; the entire
* complex is cleared on restart to preserve consistency */
class Lmdbs
{
std::string database_root;
uint8_t lmdb_count;
std::vector<std::shared_ptr<MDBEnv>> envs;
sf::path dbp;
public:
Lmdbs(std::string& database_root, uint8_t lmdb_count)
: database_root(database_root), lmdb_count(lmdb_count),
dbp(database_root) {
/* purge cache completely */
for (const auto& dir_entry : sf::directory_iterator{dbp}) {
sf::remove_all(dir_entry);
}
/* repopulate cache basis */
for (int ix = 0; ix < lmdb_count; ++ix) {
sf::path env_path{dbp / fmt::format("part_{}", ix)};
sf::create_directory(env_path);
auto env = getMDBEnv(env_path.string().c_str(), 0 /* flags? */, 0600);
envs.push_back(env);
}
}
inline std::shared_ptr<MDBEnv>& get_sp_env(Bucket* bucket) {
return envs[(bucket->hk % lmdb_count)];
}
inline MDBEnv& get_env(Bucket* bucket) {
return *(get_sp_env(bucket));
}
const std::string& get_root() const { return database_root; }
} lmdbs;
public:
BucketCache(std::string& bucket_root, std::string& database_root,
uint32_t max_buckets=100, uint8_t max_lanes=3,
uint8_t max_partitions=3, uint8_t lmdb_count=3)
: bucket_root(bucket_root), max_buckets(max_buckets),
lmdbs(database_root, lmdb_count),
un(Notify::factory(this, bucket_root)),
lru(max_lanes, max_buckets/max_lanes),
cache(max_lanes, max_buckets/max_partitions),
rp(bucket_root)
{
if (! (sf::exists(rp) && sf::is_directory(rp))) {
std::cerr << fmt::format("{} bucket root {} invalid", __func__,
bucket_root) << std::endl;
exit(1);
}
sf::path dp{database_root};
if (! (sf::exists(dp) && sf::is_directory(dp))) {
std::cerr << fmt::format("{} database root {} invalid", __func__,
database_root) << std::endl;
exit(1);
}
}
static constexpr uint32_t FLAG_NONE = 0x0000;
static constexpr uint32_t FLAG_CREATE = 0x0001;
static constexpr uint32_t FLAG_LOCK = 0x0002;
typedef std::tuple<Bucket*, uint32_t> GetBucketResult;
GetBucketResult get_bucket(const std::string& name, uint32_t flags)
{
/* this fn returns a bucket locked appropriately, having atomically
* found or inserted the required Bucket in_avl*/
Bucket* b{nullptr};
Bucket::Factory fac(this, name);
Bucket::bucket_avl_cache::Latch lat;
uint32_t iflags{cohort::lru::FLAG_INITIAL};
GetBucketResult result{nullptr, 0};
retry:
b = cache.find_latch(fac.hk /* partition selector */,
name /* key */, lat /* serializer */, Bucket::bucket_avl_cache::FLAG_LOCK);
/* LATCHED */
if (b) {
b->mtx.lock();
if (b->deleted() ||
! lru.ref(b, cohort::lru::FLAG_INITIAL)) {
// lru ref failed
lat.lock->unlock();
b->mtx.unlock();
goto retry;
}
lat.lock->unlock();
/* LOCKED */
} else {
/* Bucket not in cache, we need to create it */
b = static_cast<Bucket*>(
lru.insert(&fac, cohort::lru::Edge::MRU, iflags));
if (b) [[likely]] {
b->mtx.lock();
/* attach bucket to an lmdb partition and prepare it for i/o */
auto& env = lmdbs.get_sp_env(b);
auto dbi = env->openDB(b->name, MDB_CREATE);
b->set_env(env, dbi);
if (! (iflags & cohort::lru::FLAG_RECYCLE)) [[likely]] {
/* inserts at cached insert iterator, releasing latch */
cache.insert_latched(b, lat, Bucket::bucket_avl_cache::FLAG_UNLOCK);
} else {
/* recycle step invalidates Latch */
lat.lock->unlock(); /* !LATCHED */
cache.insert(fac.hk, b, Bucket::bucket_avl_cache::FLAG_NONE);
}
get<1>(result) |= BucketCache::FLAG_CREATE;
} else {
/* XXX lru allocate failed? seems impossible--that would mean that
* fallback to the allocator also failed, and maybe we should abend */
lat.lock->unlock();
goto retry; /* !LATCHED */
}
} /* have Bucket */
if (! (flags & BucketCache::FLAG_LOCK)) {
b->mtx.unlock();
}
get<0>(result) = b;
return result;
} /* get_bucket */
void fill(Bucket* bucket, uint32_t flags) /* assert: LOCKED */
{
sf::path bp{rp / bucket->name};
if (! (sf::exists(rp) && sf::is_directory(rp))) {
std::cerr << fmt::format("{} bucket {} invalid", __func__, bucket->name)
<< std::endl;
exit(1);
}
auto txn = bucket->env->getRWTransaction();
for (const auto& dir_entry : sf::directory_iterator{bp}) {
auto fname = dir_entry.path().filename().string();
txn->put(bucket->dbi, fname, fname /* TODO: structure, stat, &c */);
//std::cout << fmt::format("{} {}", __func__, fname) << '\n';
}
txn->commit();
bucket->flags |= Bucket::FLAG_FILLED;
un->add_watch(bucket->name, bucket);
} /* fill */
void list_bucket(std::string& name, std::string& marker,
const fu2::unique_function<int(const std::string_view&) const>& func /* XXX for now */)
{
GetBucketResult gbr = get_bucket(name, BucketCache::FLAG_LOCK);
auto [b, flags] = gbr;
if (b /* XXX again, can this fail? */) {
if (! (b->flags & Bucket::FLAG_FILLED)) {
/* bulk load into lmdb cache */
fill(b, FLAG_NONE);
}
/* display them */
b->mtx.unlock();
/*! LOCKED */
auto txn = b->env->getROTransaction();
auto cursor=txn->getCursor(b->dbi);
MDBOutVal key, data;
uint64_t count{0};
const auto proc_result = [&]() {
std::string_view svk = key.get<string_view>();
(void) func(svk);
count++;
};
if (! marker.empty()) {
MDBInVal k(marker);
auto rc = cursor.lower_bound(k, key, data);
if (rc == MDB_NOTFOUND) {
/* no key sorts after k/marker, so there is nothing to do */
return;
}
proc_result();
} else {
/* position at start of index */
cursor.get(key, data, MDB_FIRST);
proc_result();
}
while(! cursor.get(key, data, MDB_NEXT)) {
proc_result();
}
lru.unref(b, cohort::lru::FLAG_NONE);
}
} /* list_bucket */
int notify(const std::string& bname, void* opaque,
const std::vector<Notifiable::Event>& evec) override {
GetBucketResult gbr = get_bucket(bname, BucketCache::FLAG_LOCK);
auto [b, flags] = gbr;
if (b) {
unique_lock ulk{b->mtx, std::adopt_lock};
if ((b->name != bname) ||
(b != opaque) ||
(! (b->flags & Bucket::FLAG_FILLED))) {
/* do nothing */
return 0;
}
ulk.unlock();
auto txn = b->env->getRWTransaction();
for (const auto& ev : evec) {
using EventType = Notifiable::EventType;
std::string_view nil{""};
/*std::cout << fmt::format("notify {} {}!",
ev.name ? *ev.name : nil,
uint32_t(ev.type))
<< std::endl; */
switch (ev.type)
{
case EventType::ADD:
{
auto& ev_name = *ev.name;
txn->put(b->dbi, ev_name, ev_name);
}
break;
case EventType::REMOVE:
{
auto& ev_name = *ev.name;
txn->del(b->dbi, ev_name);
}
break;
[[unlikely]] case EventType::INVALIDATE:
{
/* yikes, cache blown */
ulk.lock();
mdb_drop(*txn, b->dbi, 0); /* apparently, does not require
* commit */
b->flags &= ~Bucket::FLAG_FILLED;
return 0; /* don't process any more events in this batch */
}
break;
default:
/* unknown event */
break;
}
} /* all events */
txn->commit();
} /* b */
return 0;
} /* notify */
}; /* BucketCache */
} // namespace file::listing