-
Notifications
You must be signed in to change notification settings - Fork 0
/
concurrent-expire-cache.h
460 lines (388 loc) · 13.2 KB
/
concurrent-expire-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
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
#ifndef CONCURRENT_EXPIRE_CACHE_H
#define CONCURRENT_EXPIRE_CACHE_H
#include <tbb/concurrent_hash_map.h>
#include <atomic>
#include <iostream>
#include <memory>
#include <mutex>
#include <new>
#include <random>
#include <thread>
#include <unordered_map>
#include <vector>
namespace ww {
template <class TKey, class TValue, class THash = tbb::tbb_hash_compare<TKey>>
struct ConcurrentExpireCache {
private:
struct ListNode {
ListNode() : m_timestamp(0), m_prev(nullptr), m_next(nullptr) {}
explicit ListNode(const TKey &key, int64_t timestamp)
: m_key(key),
m_timestamp(timestamp),
m_prev(nullptr),
m_next(nullptr) {}
TKey m_key;
int64_t m_timestamp;
std::shared_ptr<ListNode> m_prev;
std::shared_ptr<ListNode> m_next;
bool isInList() const { return m_prev != OutOfListMarker; }
};
static std::shared_ptr<ListNode> const OutOfListMarker;
/**
* The value that we store in the hashtable. The list node is allocated from
* an internal object_pool. The ListNode* is owned by the list.
*/
struct HashMapValue {
HashMapValue() : m_listNode(nullptr) {}
HashMapValue(const TValue &value, std::shared_ptr<ListNode> node)
: m_value(value), m_listNode(node) {}
TValue m_value;
std::shared_ptr<ListNode> m_listNode;
};
typedef tbb::concurrent_hash_map<TKey, HashMapValue, THash> HashMap;
typedef typename HashMap::const_accessor HashMapConstAccessor;
typedef typename HashMap::accessor HashMapAccessor;
typedef typename HashMap::value_type HashMapValuePair;
public:
/**
* The proxy object for TBB::CHM::const_accessor. Provides direct access to
* the user's value by dereferencing, thus hiding our implementation
* details.
*/
struct ConstAccessor {
ConstAccessor() {}
const TValue &operator*() const { return *get(); }
const TValue *operator->() const { return get(); }
const TValue *get() const { return &m_hashAccessor->second.m_value; }
bool empty() const { return m_hashAccessor.empty(); }
private:
friend struct ConcurrentExpireCache;
HashMapConstAccessor m_hashAccessor;
};
struct Accessor {
Accessor() {}
TValue &operator*() const { return *get(); }
TValue *operator->() const { return get(); }
TValue *get() const { return &m_hashAccessor->second.m_value; }
bool empty() const { return m_hashAccessor.empty(); }
private:
friend struct ConcurrentExpireCache;
HashMapAccessor m_hashAccessor;
};
explicit ConcurrentExpireCache(size_t maxSize, int64_t ttlSecond);
ConcurrentExpireCache(const ConcurrentExpireCache &other) = delete;
ConcurrentExpireCache &operator=(const ConcurrentExpireCache &) = delete;
~ConcurrentExpireCache() {
evict_thread_stop_ = true;
evict_thread_->join();
clear();
}
bool remove(const TKey &key);
bool remove(const std::vector<TKey> &keys);
/**
* Find a value by key, and return it by filling the ConstAccessor, which
* can be default-constructed. Returns true if the element was found, false
* otherwise.
*/
bool find(ConstAccessor &ac, const TKey &key);
bool find(Accessor &ac, const TKey &key);
/**
* Insert a value into the container. Both the key and value will be copied.
* The new element will put into the eviction list as the most-recently
* used.
*
* If there was already an element in the container with the same key, it
* will not be updated, and false will be returned. Otherwise, true will be
* returned.
*/
bool insert(const TKey &key, const TValue &value);
bool warmup(const TKey &key, const TValue &value, uint64_t extra_sec);
/**
* Clear the container. NOT THREAD SAFE -- do not use while other threads
* are accessing the container.
*/
void clear();
/**
* Get the approximate size of the container. May be slightly too low when
* insertion is in progress.
*/
size_t size() const { return m_size.load(); }
/**
* need to clone map, be careful.
* @param dest_map
*/
void getSnapshot(std::unordered_map<TKey, TValue> &dest_map) {
HashMap clone(m_map);
for (auto itr = clone.begin(); itr != clone.end(); ++itr) {
dest_map.insert(std::make_pair(itr->first, itr->second.m_value));
}
}
std::vector<TKey> keys();
private:
/**
* Unlink a node from the list. The caller must lock the list mutex while
* this is called.
*/
void delink(std::shared_ptr<ListNode> node);
/**
* Add a new node to the list in the most-recently used position. The caller
* must lock the list mutex while this is called.
*/
void pushFront(std::shared_ptr<ListNode> node);
/**
* Evict the least-recently used item from the container. This function does
* its own locking.
*/
void evict();
void evictTask();
/**
* The maximum number of elements in the container.
*/
size_t m_maxSize;
/**
* This atomic variable is used to signal to all threads whether or not
* eviction should be done on insert. It is approximately equal to the
* number of elements in the container.
*/
std::atomic<size_t> m_size;
/**
* The underlying TBB hash map.
*/
HashMap m_map;
/**
* The linked list. The "head" is the most-recently used node, and the
* "tail" is the least-recently used node. The list mutex must be held
* during both read and write.
*/
// first element of list does not store data, and it is just a head that
// always in list
std::shared_ptr<ListNode> m_head;
// last element of list does not store data, and it is just a tail that always
// in list
std::shared_ptr<ListNode> m_tail;
typedef std::mutex ListMutex;
ListMutex m_listMutex;
int64_t m_ttlSecond;
std::shared_ptr<std::thread> evict_thread_;
int32_t evict_thread_duration_second_;
std::atomic<bool> evict_thread_stop_;
};
template <class TKey, class TValue, class THash>
std::shared_ptr<
typename ConcurrentExpireCache<TKey, TValue, THash>::ListNode> const
ConcurrentExpireCache<TKey, TValue, THash>::OutOfListMarker =
std::make_shared<ListNode>();
template <class TKey, class TValue, class THash>
ConcurrentExpireCache<TKey, TValue, THash>::ConcurrentExpireCache(
size_t maxSize, int64_t ttlSecond)
: m_maxSize(maxSize),
m_size(0),
m_ttlSecond(ttlSecond),
evict_thread_duration_second_(1),
evict_thread_stop_(false),
m_map(std::thread::hardware_concurrency() *
4) // it will automatically grow
{
m_head = std::make_shared<ListNode>();
m_tail = std::make_shared<ListNode>();
m_head->m_next = m_tail;
m_tail->m_prev = m_head;
evict_thread_.reset(new std::thread(
&ConcurrentExpireCache<TKey, TValue, THash>::evictTask, this));
}
template <class TKey, class TValue, class THash>
bool ConcurrentExpireCache<TKey, TValue, THash>::remove(const TKey &key) {
bool res = false;
HashMapAccessor hashAccessor;
if (m_map.find(hashAccessor, key)) {
res = m_map.erase(hashAccessor);
// Acquire the lock, block if it is already held
std::unique_lock<ListMutex> lock(m_listMutex);
if (lock) {
std::shared_ptr<ListNode> node = hashAccessor->second.m_listNode;
if (node->isInList()) {
delink(node);
--m_size;
} else {
std::cerr << "local cache removing key, node does not in list: "
<< node->m_key << std::endl;
}
lock.unlock();
}
}
return res;
}
template <class TKey, class TValue, class THash>
bool ConcurrentExpireCache<TKey, TValue, THash>::remove(
const std::vector<TKey> &keys) {
bool res = true;
if (!keys.empty()) {
std::vector<std::shared_ptr<ListNode>> nodeList;
size_t removedCnt = 0;
for (const TKey &key : keys) {
HashMapAccessor hashAccessor;
if (m_map.find(hashAccessor, key)) {
nodeList.emplace_back(hashAccessor->second.m_listNode);
res &= m_map.erase(hashAccessor);
++removedCnt;
}
}
if (!nodeList.empty()) {
// Acquire the lock, block if it is already held
std::unique_lock<ListMutex> lock(m_listMutex);
for (auto &node : nodeList) {
if (node->isInList()) {
delink(node);
} else {
std::cerr << "local cache removing keys, node does not in list: "
<< node->m_key << std::endl;
}
}
lock.unlock();
m_size -= removedCnt;
}
}
return res;
}
template <class TKey, class TValue, class THash>
bool ConcurrentExpireCache<TKey, TValue, THash>::find(ConstAccessor &ac,
const TKey &key) {
HashMapConstAccessor &hashAccessor = ac.m_hashAccessor;
return m_map.find(hashAccessor, key);
}
template <class TKey, class TValue, class THash>
bool ConcurrentExpireCache<TKey, TValue, THash>::find(Accessor &ac,
const TKey &key) {
HashMapAccessor &hashAccessor = ac.m_hashAccessor;
return m_map.find(hashAccessor, key);
}
template <class TKey, class TValue, class THash>
bool ConcurrentExpireCache<TKey, TValue, THash>::insert(const TKey &key,
const TValue &value) {
if (m_size >= m_maxSize) {
return false;
}
std::time_t timestamp = std::time(NULL);
std::shared_ptr<ListNode> node = std::make_shared<ListNode>(key, timestamp);
HashMapAccessor hashAccessor;
HashMapValuePair hashMapValue(key, HashMapValue(value, node));
if (!m_map.insert(hashAccessor, hashMapValue)) {
// delete node;
// node = nullptr;
return false;
}
// increase before insert
++m_size;
std::unique_lock<ListMutex> lock(m_listMutex);
pushFront(node);
lock.unlock();
return true;
}
template <class TKey, class TValue, class THash>
bool ConcurrentExpireCache<TKey, TValue, THash>::warmup(const TKey &key,
const TValue &value,
uint64_t extra_sec) {
static uint64_t extra_seconds =
extra_sec <= m_ttlSecond / 2 ? extra_sec : m_ttlSecond / 2;
static std::mt19937 engine(std::time(NULL));
static std::uniform_int_distribution<int> distribution(-extra_seconds,
extra_seconds);
if (m_size >= m_maxSize) {
return false;
}
std::time_t timestamp = std::time(NULL);
std::shared_ptr<ListNode> node = std::make_shared<ListNode>(key, timestamp);
HashMapAccessor hashAccessor;
HashMapValuePair hashMapValue(key, HashMapValue(value, node));
if (!m_map.insert(hashAccessor, hashMapValue)) {
// delete node;
// node = nullptr;
return false;
}
// increase before insert
++m_size;
std::unique_lock<ListMutex> lock(m_listMutex);
pushFront(node);
lock.unlock();
return true;
}
template <class TKey, class TValue, class THash>
void ConcurrentExpireCache<TKey, TValue, THash>::clear() {
m_map.clear();
m_head->m_next = m_tail;
m_tail->m_prev = m_head;
m_size = 0;
}
template <class TKey, class TValue, class THash>
std::vector<TKey> ConcurrentExpireCache<TKey, TValue, THash>::keys() {
std::vector<TKey> keys;
{
std::unique_lock<ListMutex> lock(m_listMutex);
for (auto curNode = m_head->m_next; curNode != m_tail;
curNode = curNode->m_next) {
keys.push_back(curNode->m_key);
}
}
return keys;
}
template <class TKey, class TValue, class THash>
inline void ConcurrentExpireCache<TKey, TValue, THash>::delink(
std::shared_ptr<ListNode> node) {
std::shared_ptr<ListNode> prev = node->m_prev;
std::shared_ptr<ListNode> next = node->m_next;
prev->m_next = next;
next->m_prev = prev;
node->m_prev = OutOfListMarker;
}
template <class TKey, class TValue, class THash>
inline void ConcurrentExpireCache<TKey, TValue, THash>::pushFront(
std::shared_ptr<ListNode> node) {
std::shared_ptr<ListNode> oldRealHead = m_head->m_next;
node->m_prev = m_head;
node->m_next = oldRealHead;
oldRealHead->m_prev = node;
m_head->m_next = node;
}
template <class TKey, class TValue, class THash>
void ConcurrentExpireCache<TKey, TValue, THash>::evict() {
std::vector<std::shared_ptr<ListNode>> expiredNodes;
std::time_t timestamp = std::time(NULL);
timestamp -= m_ttlSecond;
std::unique_lock<ListMutex> lock(m_listMutex);
std::shared_ptr<ListNode> moribund = m_tail->m_prev;
while (moribund != m_head && moribund->m_timestamp < timestamp) {
if (moribund->isInList()) {
delink(moribund);
} else {
std::cerr << "local cache evict, node does not in list: "
<< moribund->m_key << std::endl;
}
expiredNodes.push_back(moribund);
moribund = m_tail->m_prev;
}
lock.unlock();
if (expiredNodes.empty()) {
return;
}
size_t evictedCnt = 0;
for (auto &expiredNode : expiredNodes) {
HashMapAccessor hashAccessor;
if (!m_map.find(hashAccessor, expiredNode->m_key)) {
// Presumably unreachable
continue;
}
m_map.erase(hashAccessor);
++evictedCnt;
}
m_size -= evictedCnt;
}
template <class TKey, class TValue, class THash>
void ConcurrentExpireCache<TKey, TValue, THash>::evictTask() {
while (!evict_thread_stop_) {
evict();
std::this_thread::sleep_for(
std::chrono::seconds(evict_thread_duration_second_));
}
}
} // namespace ww
#endif // CONCURRENT_EXPIRE_CACHE_H