forked from couchbase/ep-engine
/
restore_impl.cc
457 lines (399 loc) · 13.5 KB
/
restore_impl.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
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
/* -*- Mode: C++; tab-width: 4; c-basic-offset: 4; indent-tabs-mode: nil -*- */
/*
* Copyright 2011 Couchbase, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include "config.h"
#include "ep_engine.h"
#include "restore.hh"
#ifdef USE_SYSTEM_LIBSQLITE3
#include <sqlite3.h>
#else
#include "embedded/sqlite3.h"
#endif
static const char *checks_enabled_query =
"select cpoint_op.vbucket_id,op,key,flg,exp,cas,val,cpoint_op.cpoint_id "
"from cpoint_state "
" join cpoint_op on (cpoint_op.vbucket_id = cpoint_state.vbucket_id and"
" cpoint_op.cpoint_id = cpoint_state.cpoint_id) "
"where cpoint_state.state = \"closed\" "
"order by cpoint_op.cpoint_id desc";
static const char *checks_disabled_query =
"select cpoint_op.vbucket_id,op,key,flg,exp,cas,val,cpoint_id "
"from cpoint_op ";
static const int vbucket_id_idx = 0;
static const int op_idx = 1;
static const int key_idx = 2;
static const int flag_idx = 3;
static const int exp_idx = 4;
static const int cas_idx = 5;
static const int val_idx = 6;
static const int cpoint_idx = 7;
extern "C" {
static void *restoreThreadMain(void *arg);
}
/**
* A helper class to track the state of the restore manager
*/
class State {
public:
State(const char *s) : name(s) {}
const std::string &toString() const {
return name;
}
static const State Uninitialized;
static const State Initialized;
static const State Starting;
static const State Running;
static const State Zombie;
private:
const std::string name;
};
const State State::Uninitialized("uninitialized");
const State State::Initialized("initialized");
const State State::Starting("starting");
const State State::Running("running");
const State State::Zombie("zombie");
/**
* Hehe.. Since we're applying all of the incremental backups in the
* let's let the name reflect that ;-)
*
* The DecrementalRestorer is responsible for processing a single
* incremental restore file and add all of it's content to epengine.
*
*/
class DecrementalRestorer {
public:
/**
* Create a new instance of DecrementalRestorer and initialize
* its member variable.
* @param theEngine where to restore the data
* @param dbname the name of the incremental database to restore
*/
DecrementalRestorer(EventuallyPersistentEngine &theEngine,
const std::string &dbname, bool restore_file_checks) :
db(NULL), statement(NULL), engine(theEngine),
store(*engine.getEpStore()), file(dbname),
expired(0), wrongVBucket(0), restored(0), skipped(0), busy(0), restore_cpoint(0)
{
query = restore_file_checks == true ? checks_enabled_query : checks_disabled_query;
}
/**
* Release all allocated resources. We might have the database in an
* open state if an exception is thrown during restore.
*/
~DecrementalRestorer() {
if (db != NULL) {
(void)sqlite3_finalize(statement);
(void)sqlite3_close(db);
}
}
const std::string &getDbFile() const
{
return file;
}
uint32_t getNumBusy() const {
return busy;
}
uint32_t getRestoreCheckpoint() const {
return restore_cpoint;
}
uint32_t getNumRestored() const {
return restored;
}
uint32_t getNumSkipped() const {
return skipped;
}
uint32_t getNumWrongVBucket() const {
return wrongVBucket;
}
uint32_t getNumExpired() const {
return expired;
}
/**
* Process this database file
* @throw a string describing why an error occured
*/
void process() throw (std::string) {
if (sqlite3_open(file.c_str(), &db) != SQLITE_OK) {
db = NULL;
throw std::string("Failed to open database");
}
if (sqlite3_prepare_v2(db, query,
strlen(query),
&statement, NULL) != SQLITE_OK) {
(void)sqlite3_finalize(statement);
(void)sqlite3_close(db);
db = NULL;
throw std::string("Failed to prepare statement");
}
int rc;
while ((rc = sqlite3_step(statement)) != SQLITE_DONE) {
if (rc == SQLITE_ROW) {
processEntry();
} else if (rc == SQLITE_BUSY) {
++busy;
} else {
std::stringstream ss;
ss << "sqlite error: " << sqlite3_errmsg(db);
throw std::string(ss.str());
}
}
(void)sqlite3_finalize(statement);
(void)sqlite3_close(db);
db = NULL;
}
private:
/**
* callback to process the current item
*/
void processEntry() throw(std::string) {
uint32_t exptime = sqlite3_column_int(statement, exp_idx);
if (exptime != 0 && exptime < static_cast<int64_t>(ep_real_time())) {
++expired;
return ;
}
std::string key((const char*)sqlite3_column_text(statement, key_idx),
sqlite3_column_bytes(statement, key_idx));
enum queue_operation op = queue_op_set;
if ((sqlite3_column_bytes(statement, op_idx) > 0) &&
sqlite3_column_text(statement, op_idx)[0] == 'd') {
op = queue_op_del;
}
value_t value(Blob::New((const char*)sqlite3_column_text(statement,
val_idx),
sqlite3_column_bytes(statement, val_idx)));
uint16_t vbid = (uint16_t)sqlite3_column_int(statement,
vbucket_id_idx);
if (!restore_cpoint) {
restore_cpoint = (uint32_t)sqlite3_column_int(statement,
cpoint_idx);
}
uint32_t flags = sqlite3_column_int(statement, flag_idx);
time_t expiration = sqlite3_column_int(statement, exp_idx);
uint64_t cas = sqlite3_column_int64(statement, cas_idx);
Item itm(key, flags, expiration, value, cas, -1, vbid);
int r = store.restoreItem(itm, op);
if (r == 0) {
++restored;
} else if (r == 1) {
++skipped;
} else {
++wrongVBucket;
}
}
sqlite3 *db;
sqlite3_stmt *statement;
EventuallyPersistentEngine &engine;
EventuallyPersistentStore &store;
const std::string file;
uint32_t expired;
uint32_t wrongVBucket;
uint32_t restored;
uint32_t skipped;
uint32_t busy;
uint32_t restore_cpoint;
const char *query;
};
class RestoreManagerImpl : public RestoreManager {
public:
RestoreManagerImpl(EventuallyPersistentEngine &theEngine) :
RestoreManager(theEngine),
instance(NULL),
expired(0),
wrongVBucket(0),
restored(0),
skipped(0),
busy(0),
restore_cpoint(0),
restore_file_checks(true),
state(&State::Uninitialized)
{
// None needed
}
virtual void initialize(const std::string &config) throw(std::string)
{
if (access(config.c_str(), F_OK) != 0) {
throw std::string("File not found");
}
LockHolder lh(mutex);
errorMsg.resize(0);
if (state != &State::Uninitialized) {
// Only allow the zombie state, because then we can just reap it..
if (state != &State::Zombie) {
throw std::string("restorer isn't idle!");
}
// reap the zombie!
reap_UNLOCKED();
}
assert(instance == NULL);
instance = new DecrementalRestorer(engine, config, restore_file_checks);
setState_UNLOCKED(State::Initialized);
}
virtual void start() throw (std::string)
{
LockHolder lh(mutex);
if (instance == NULL) {
lh.unlock();
throw std::string("you need to call initialize before start");
}
if (state != &State::Initialized) {
lh.unlock();
throw std::string("Restore already running");
}
state = &State::Starting;
int ret = pthread_create(&thread, NULL, restoreThreadMain, this);
if (ret != 0) {
state = &State::Uninitialized;
collectResults();
delete instance;
instance = NULL;
lh.unlock();
std::stringstream ss;
ss << "Failed to create restore thread: " << strerror(ret);
throw ss.str();
}
}
virtual void abort() throw (std::string)
{
LockHolder lh(mutex);
terminate = true;
}
virtual void wait() throw (std::string)
{
LockHolder lh(mutex);
if (state != &State::Initialized && state != &State::Uninitialized) {
reap_UNLOCKED();
}
}
virtual void stats(const void* cookie, ADD_STAT add_stat)
{
RestoreManager::stats(cookie, add_stat);
addStat(cookie, "engine", "RestoreManagerImpl", add_stat);
LockHolder lh(mutex);
addStat(cookie, "state", state->toString(), add_stat);
if (errorMsg.length() > 0) {
addStat(cookie, "last_error", errorMsg, add_stat);
}
if (instance == NULL) {
addStat(cookie, "restore_checkpoint", restore_cpoint, add_stat);
addStat(cookie, "number_busy", busy, add_stat);
addStat(cookie, "number_skipped", skipped, add_stat);
addStat(cookie, "number_restored", restored, add_stat);
addStat(cookie, "number_expired", expired, add_stat);
addStat(cookie, "number_wrong_vbucket", wrongVBucket, add_stat);
} else {
addStat(cookie, "restore_checkpoint", restore_cpoint ? restore_cpoint :
instance->getRestoreCheckpoint(), add_stat);
addStat(cookie, "file", instance->getDbFile(), add_stat);
addStat(cookie, "number_busy", instance->getNumBusy() + busy,
add_stat);
addStat(cookie, "number_skipped", instance->getNumSkipped() + skipped,
add_stat);
addStat(cookie, "number_restored", instance->getNumRestored() + restored,
add_stat);
addStat(cookie, "number_expired",
instance->getNumExpired() + expired, add_stat);
addStat(cookie, "number_wrong_vbucket",
instance->getNumWrongVBucket() + wrongVBucket, add_stat);
addStat(cookie, "terminate", terminate, add_stat);
}
}
virtual bool isRunning() {
LockHolder lh(mutex);
return (state == &State::Starting ||
state == &State::Running);
}
virtual void enableRestoreFileChecks(bool chk) {
restore_file_checks = chk;
}
virtual ~RestoreManagerImpl() {
wait();
}
void *run(void) {
ObjectRegistry::onSwitchThread(&engine);
setState(State::Running);
try {
instance->process();
} catch (std::string message) {
LockHolder lh(mutex);
errorMsg.assign(message);
}
setState(State::Zombie);
return NULL;
}
private:
void collectResults() {
skipped += instance->getNumSkipped();
busy += instance->getNumBusy();
restored += instance->getNumRestored();
if (!restore_cpoint) {
restore_cpoint = instance->getRestoreCheckpoint();
}
}
void reap_UNLOCKED() throw (std::string) {
if (instance != NULL) {
void *rcode;
int ret = pthread_join(thread, &rcode);
if (ret != 0 && ret != ESRCH) {
std::stringstream ss;
ss << "Failed to join restore thread: " << strerror(ret);
throw ss.str();
}
collectResults();
delete instance;
instance = NULL;
setState_UNLOCKED(State::Uninitialized);
}
}
void setState_UNLOCKED(const State &s) {
state = &s;
}
void setState(const State &s) {
LockHolder lh(mutex);
setState_UNLOCKED(s);
}
// Access to the variables in here are all protected by a single mutex.
// I know this doesn't scale much, but if you're having performance
// problems you should stop calling stats all of the times ;-)
Mutex mutex;
DecrementalRestorer *instance;
std::string errorMsg;
uint32_t expired;
uint32_t wrongVBucket;
uint32_t restored;
uint32_t skipped;
uint32_t busy;
uint32_t restore_cpoint;
bool restore_file_checks;
// should we abort or not?
Atomic<bool> terminate;
const State *state;
// The thread running the backup
pthread_t thread;
};
RestoreManager* create_restore_manager(EventuallyPersistentEngine &engine)
{
return new RestoreManagerImpl(engine);
}
void destroy_restore_manager(RestoreManager *manager)
{
delete manager;
}
static void *restoreThreadMain(void *arg)
{
RestoreManagerImpl *instance;
instance = reinterpret_cast<RestoreManagerImpl*>(arg);
return instance->run();
}