forked from membase/ep-engine
/
ep.cc
406 lines (343 loc) · 11.6 KB
/
ep.cc
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
/* -*- Mode: C++; tab-width: 4; c-basic-offset: 4; indent-tabs-mode: nil -*- */
#include "ep.hh"
#include "flusher.hh"
#include "locks.hh"
#include <vector>
#include <time.h>
#include <string.h>
extern "C" {
static void* launch_flusher_thread(void* arg) {
Flusher *flusher = (Flusher*) arg;
try {
flusher->run();
} catch (std::exception& e) {
std::cerr << "flusher exception caught: " << e.what() << std::endl;
} catch(...) {
std::cerr << "Caught a fatal exception in the flusher thread" << std::endl;
}
return NULL;
}
static rel_time_t uninitialized_current_time(void) {
abort();
return 0;
}
rel_time_t (*ep_current_time)() = uninitialized_current_time;
}
EventuallyPersistentStore::EventuallyPersistentStore(KVStore *t,
size_t est) :
loadStorageKVPairCallback(storage, stats)
{
est_size = est;
stats.min_data_age.set(DEFAULT_MIN_DATA_AGE);
stats.queue_age_cap.set(DEFAULT_MIN_DATA_AGE_CAP);
doPersistence = getenv("EP_NO_PERSISTENCE") == NULL;
flusher = new Flusher(this);
txnSize = DEFAULT_TXN_SIZE;
underlying = t;
startFlusher();
assert(underlying);
}
class VerifyStoredVisitor : public HashTableVisitor {
public:
std::vector<std::string> dirty;
virtual void visit(StoredValue *v) {
if (v->isDirty()) {
dirty.push_back(v->getKey());
}
}
};
EventuallyPersistentStore::~EventuallyPersistentStore() {
stopFlusher();
if (flusher != NULL) {
pthread_join(thread, NULL);
}
// Verify that we don't have any dirty objects!
if (getenv("EP_VERIFY_SHUTDOWN_FLUSH") != NULL) {
VerifyStoredVisitor walker;
storage.visit(walker);
if (!walker.dirty.empty()) {
std::vector<std::string>::const_iterator iter;
for (iter = walker.dirty.begin();
iter != walker.dirty.end();
++iter) {
std::cerr << "ERROR: Object dirty after flushing: "
<< iter->c_str() << std::endl;
}
throw std::runtime_error("Internal error, objects dirty objects exists");
}
}
delete flusher;
}
const Flusher* EventuallyPersistentStore::getFlusher() {
return flusher;
}
void EventuallyPersistentStore::startFlusher() {
LockHolder lh(mutex);
// Run in a thread...
if(pthread_create(&thread, NULL, launch_flusher_thread, flusher)
!= 0) {
throw std::runtime_error("Error initializing queue thread");
}
mutex.notify();
}
void EventuallyPersistentStore::stopFlusher() {
LockHolder lh(mutex);
flusher->stop();
mutex.notify();
}
bool EventuallyPersistentStore::pauseFlusher() {
LockHolder lh(mutex);
flusher->pause();
mutex.notify();
return true;
}
bool EventuallyPersistentStore::resumeFlusher() {
LockHolder lh(mutex);
flusher->resume();
mutex.notify();
return true;
}
void EventuallyPersistentStore::set(const Item &item, Callback<bool> &cb) {
mutation_type_t mtype = storage.set(item);
bool rv = true;
if (mtype == INVALID_CAS || mtype == IS_LOCKED) {
rv = false;
} else if (mtype == WAS_CLEAN || mtype == NOT_FOUND) {
queueDirty(item.getKey());
if (mtype == NOT_FOUND) {
stats.curr_items.incr();
}
}
cb.setStatus((int)mtype);
cb.callback(rv);
}
void EventuallyPersistentStore::get(const std::string &key,
Callback<GetValue> &cb) {
int bucket_num = storage.bucket(key);
LockHolder lh(storage.getMutex(bucket_num));
StoredValue *v = storage.unlocked_find(key, bucket_num);
if (v) {
// return an invalid cas value if the item is locked
GetValue rv(new Item(v->getKey(), v->getFlags(), v->getExptime(),
v->getValue(), v->isLocked(ep_current_time()) ? -1 : v->getCas()));
cb.callback(rv);
} else {
GetValue rv(false);
cb.callback(rv);
}
lh.unlock();
}
bool EventuallyPersistentStore::getLocked(const std::string &key,
Callback<GetValue> &cb,
rel_time_t currentTime,
uint32_t lockTimeout) {
int bucket_num = storage.bucket(key);
LockHolder lh(storage.getMutex(bucket_num));
StoredValue *v = storage.unlocked_find(key, bucket_num);
if (v) {
if (v->isLocked(currentTime)) {
GetValue rv(false);
cb.callback(rv);
lh.unlock();
return false;
}
// acquire lock and increment cas value
v->lock(currentTime + lockTimeout);
Item *it = new Item(v->getKey(), v->getFlags(), v->getExptime(),
v->getValue(), v->getCas());
it->setCas();
v->setCas(it->getCas());
GetValue rv(it);
cb.callback(rv);
} else {
GetValue rv(false);
cb.callback(rv);
}
lh.unlock();
return true;
}
bool EventuallyPersistentStore::getKeyStats(const std::string &key,
struct key_stats &kstats)
{
bool found = false;
int bucket_num = storage.bucket(key);
LockHolder lh(storage.getMutex(bucket_num));
StoredValue *v = storage.unlocked_find(key, bucket_num);
found = (v != NULL);
if (found) {
kstats.dirty = v->isDirty();
kstats.exptime = v->getExptime();
kstats.flags = v->getFlags();
kstats.cas = v->getCas();
kstats.dirtied = v->getDirtied();
kstats.data_age = v->getDataAge();
}
return found;
}
void EventuallyPersistentStore::setMinDataAge(int to) {
stats.min_data_age.set(to);
}
void EventuallyPersistentStore::setQueueAgeCap(int to) {
stats.queue_age_cap.set(to);
}
void EventuallyPersistentStore::resetStats(void) {
stats.tooYoung.set(0);
stats.tooOld.set(0);
stats.dirtyAge.set(0);
stats.dirtyAgeHighWat.set(0);
stats.flushDuration.set(0);
stats.flushDurationHighWat.set(0);
stats.commit_time.set(0);
}
void EventuallyPersistentStore::del(const std::string &key, Callback<bool> &cb) {
bool existed = storage.del(key);
if (existed) {
queueDirty(key);
stats.curr_items.decr();
}
cb.callback(existed);
}
std::queue<std::string> *EventuallyPersistentStore::beginFlush(bool shouldWait) {
if (towrite.empty() && writing.empty()) {
stats.dirtyAge.set(0);
if (shouldWait) {
mutex.wait();
} else {
getLogger()->log(EXTENSION_LOG_DEBUG, NULL, "Nothing to flush\n");
return &writing;
}
}
assert(underlying);
towrite.getAll(writing);
stats.flusher_todo.set(writing.size());
stats.queue_size.set(towrite.size() + writing.size());
getLogger()->log(EXTENSION_LOG_DEBUG, NULL, "Flushing %d items with %d still in queue\n",
writing.size(), towrite.size());
return &writing;
}
void EventuallyPersistentStore::completeFlush(std::queue<std::string> *rej,
rel_time_t flush_start) {
// Requeue the rejects.
getLogger()->log(EXTENSION_LOG_DEBUG, NULL, "Rejected %d items\n", rej->size());
stats.queue_size.incr(rej->size());
while (!rej->empty()) {
writing.push(rej->front());
rej->pop();
}
stats.queue_size.set(towrite.size() + writing.size());
rel_time_t complete_time = ep_current_time();
stats.flushDuration.set(complete_time - flush_start);
stats.flushDurationHighWat.set(std::max(stats.flushDuration.get(),
stats.flushDurationHighWat.get()));
}
int EventuallyPersistentStore::flushSome(std::queue<std::string> *q,
std::queue<std::string> *rejectQueue) {
int tsz = getTxnSize();
underlying->begin();
int oldest = stats.min_data_age.get();
for (int i = 0; i < tsz && !q->empty(); i++) {
int n = flushOne(q, rejectQueue);
if (n != 0 && n < oldest) {
oldest = n;
}
}
rel_time_t cstart = ep_current_time();
while (!underlying->commit()) {
sleep(1);
stats.commitFailed.incr();
}
rel_time_t complete_time = ep_current_time();
stats.commit_time.set(complete_time - cstart);
return oldest;
}
// This class exists to create a closure around a few variables within
// EventuallyPersistentStore::flushOne so that an object can be
// requeued in case of failure to store in the underlying layer.
class Requeuer : public Callback<bool> {
public:
Requeuer(const std::string k, std::queue<std::string> *q,
StoredValue *v, rel_time_t qd, rel_time_t d, struct EPStats *s) :
key(k), rq(q), sval(v), queued(qd), dirtied(d), stats(s) {
assert(rq);
assert(s);
}
void callback(bool &value) {
if (!value) {
stats->flushFailed.incr();
if (sval != NULL) {
sval->reDirty(queued, dirtied);
}
rq->push(key);
}
}
private:
const std::string key;
std::queue<std::string> *rq;
StoredValue *sval;
rel_time_t queued;
rel_time_t dirtied;
struct EPStats *stats;
DISALLOW_COPY_AND_ASSIGN(Requeuer);
};
int EventuallyPersistentStore::flushOne(std::queue<std::string> *q,
std::queue<std::string> *rejectQueue) {
std::string key = q->front();
q->pop();
int bucket_num = storage.bucket(key);
LockHolder lh(storage.getMutex(bucket_num));
StoredValue *v = storage.unlocked_find(key, bucket_num);
bool found = v != NULL;
bool isDirty = (found && v->isDirty());
Item *val = NULL;
rel_time_t queued(0), dirtied(0);
int ret = 0;
if (isDirty) {
v->markClean(&queued, &dirtied);
assert(dirtied > 0);
// Calculate stats if this had a positive time.
rel_time_t now = ep_current_time();
int dataAge = now - dirtied;
int dirtyAge = now - queued;
bool eligible = true;
if (dirtyAge > stats.queue_age_cap.get()) {
stats.tooOld.incr();
} else if (dataAge < stats.min_data_age.get()) {
eligible = false;
// Skip this one. It's too young.
ret = stats.min_data_age.get() - dataAge;
isDirty = false;
stats.tooYoung.incr();
v->reDirty(queued, dirtied);
rejectQueue->push(key);
}
if (eligible) {
assert(dirtyAge < (86400 * 30));
assert(dataAge <= dirtyAge);
stats.dirtyAge.set(dirtyAge);
stats.dataAge.set(dataAge);
stats.dirtyAgeHighWat.set(std::max(stats.dirtyAge.get(),
stats.dirtyAgeHighWat.get()));
stats.dataAgeHighWat.set(std::max(stats.dataAge.get(),
stats.dataAgeHighWat.get()));
// Copy it for the duration.
val = new Item(key, v->getFlags(), v->getExptime(), v->getValue(),
v->getCas());
// Consider this persisted as it is our intention, though
// it may fail and be requeued later.
stats.totalPersisted.incr();
}
}
stats.flusher_todo.decr();
lh.unlock();
if (found && isDirty) {
Requeuer cb(key, rejectQueue, v, queued, dirtied, &stats);
underlying->set(*val, cb);
} else if (!found) {
Requeuer cb(key, rejectQueue, v, queued, dirtied, &stats);
underlying->del(key, cb);
}
if (val != NULL) {
delete val;
}
return ret;
}