forked from vesoft-inc/nebula
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRocksEngine.cpp
693 lines (623 loc) · 24.3 KB
/
RocksEngine.cpp
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
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
/* Copyright (c) 2018 vesoft inc. All rights reserved.
*
* This source code is licensed under Apache 2.0 License.
*/
#include "kvstore/RocksEngine.h"
#include <folly/String.h>
#include <rocksdb/convenience.h>
#include "common/base/Base.h"
#include "common/fs/FileUtils.h"
#include "common/utils/MetaKeyUtils.h"
#include "common/utils/NebulaKeyUtils.h"
#include "kvstore/KVStore.h"
DEFINE_bool(move_files, false, "Move the SST files instead of copy when ingest into dataset");
DEFINE_int64(balance_expired_sesc,
86400,
"The expired time of balancing part info persisted in the storaged");
namespace nebula {
namespace kvstore {
using fs::FileType;
using fs::FileUtils;
/***************************************
*
* Implementation of RocksEngine
*
**************************************/
RocksEngine::RocksEngine(GraphSpaceID spaceId,
int32_t vIdLen,
const std::string& dataPath,
const std::string& walPath,
std::shared_ptr<rocksdb::MergeOperator> mergeOp,
std::shared_ptr<rocksdb::CompactionFilterFactory> cfFactory,
bool readonly)
: KVEngine(spaceId),
spaceId_(spaceId),
dataPath_(folly::stringPrintf("%s/nebula/%d", dataPath.c_str(), spaceId)) {
// set wal path as dataPath by default
if (walPath.empty()) {
walPath_ = folly::stringPrintf("%s/nebula/%d", dataPath.c_str(), spaceId);
} else {
walPath_ = folly::stringPrintf("%s/nebula/%d", walPath.c_str(), spaceId);
}
auto path = folly::stringPrintf("%s/data", dataPath_.c_str());
if (FileUtils::fileType(path.c_str()) == FileType::NOTEXIST) {
if (readonly) {
LOG(FATAL) << "Path " << path << " not exist";
} else {
if (!FileUtils::makeDir(path)) {
LOG(FATAL) << "makeDir " << path << " failed";
}
}
}
if (FileUtils::fileType(path.c_str()) != FileType::DIRECTORY) {
LOG(FATAL) << path << " is not directory";
}
openBackupEngine(spaceId);
rocksdb::Options options;
rocksdb::DB* db = nullptr;
rocksdb::Status status = initRocksdbOptions(options, spaceId, vIdLen);
CHECK(status.ok()) << status.ToString();
if (mergeOp != nullptr) {
options.merge_operator = mergeOp;
}
if (cfFactory != nullptr) {
options.compaction_filter_factory = cfFactory;
}
if (readonly) {
status = rocksdb::DB::OpenForReadOnly(options, path, &db);
} else {
status = rocksdb::DB::Open(options, path, &db);
}
CHECK(status.ok()) << status.ToString();
if (!readonly && spaceId_ != kDefaultSpaceId /* only for storage*/) {
rocksdb::ReadOptions readOptions;
std::string dataVersionValue = "";
status = db->Get(readOptions, NebulaKeyUtils::dataVersionKey(), &dataVersionValue);
if (status.IsNotFound()) {
rocksdb::WriteOptions writeOptions;
status = db->Put(
writeOptions, NebulaKeyUtils::dataVersionKey(), NebulaKeyUtils::dataVersionValue());
}
CHECK(status.ok()) << status.ToString();
}
db_.reset(db);
std::string factoryName = options.table_factory->Name();
if (factoryName == rocksdb::TableFactory::kBlockBasedTableName()) {
extractorLen_ = sizeof(PartitionID) + vIdLen;
} else if (factoryName == rocksdb::TableFactory::kPlainTableName()) {
// PlainTable only support prefix-based seek, which means if the prefix is not inserted into
// rocksdb, we can't read them from "prefix" api anymore. For simplicity, we just set the length
// of prefix extractor to the minimum length we used in "prefix" api, which is 4 when we seek by
// tagPrefix(partId) or edgePrefix(partId).
isPlainTable_ = true;
extractorLen_ = sizeof(PartitionID);
}
partsNum_ = allParts().size();
LOG(INFO) << "open rocksdb on " << path;
backup();
}
void RocksEngine::stop() {
if (db_) {
// Because we trigger compaction in WebService, we need to stop all
// background work before we stop HttpServer.
rocksdb::CancelAllBackgroundWork(db_.get(), true);
}
}
std::unique_ptr<WriteBatch> RocksEngine::startBatchWrite() {
return std::make_unique<RocksWriteBatch>();
}
nebula::cpp2::ErrorCode RocksEngine::commitBatchWrite(std::unique_ptr<WriteBatch> batch,
bool disableWAL,
bool sync,
bool wait) {
rocksdb::WriteOptions options;
options.disableWAL = disableWAL;
options.sync = sync;
options.no_slowdown = !wait;
auto* b = static_cast<RocksWriteBatch*>(batch.get());
rocksdb::Status status = db_->Write(options, b->data());
if (status.ok()) {
return nebula::cpp2::ErrorCode::SUCCEEDED;
} else if (!wait && status.IsIncomplete()) {
return nebula::cpp2::ErrorCode::E_WRITE_STALLED;
}
VLOG(3) << "Write into rocksdb failed because of " << status.ToString();
return nebula::cpp2::ErrorCode::E_UNKNOWN;
}
nebula::cpp2::ErrorCode RocksEngine::get(const std::string& key,
std::string* value,
const void* snapshot) {
memory::MemoryCheckOffGuard guard;
rocksdb::ReadOptions options;
if (UNLIKELY(snapshot != nullptr)) {
options.snapshot = reinterpret_cast<const rocksdb::Snapshot*>(snapshot);
}
rocksdb::Status status = db_->Get(options, rocksdb::Slice(key), value);
if (status.ok()) {
return nebula::cpp2::ErrorCode::SUCCEEDED;
} else if (status.IsNotFound()) {
VLOG(4) << "Get: " << key << " Not Found";
return nebula::cpp2::ErrorCode::E_KEY_NOT_FOUND;
} else {
VLOG(4) << "Get Failed: " << key << " " << status.ToString();
return nebula::cpp2::ErrorCode::E_UNKNOWN;
}
}
std::vector<Status> RocksEngine::multiGet(const std::vector<std::string>& keys,
std::vector<std::string>* values) {
memory::MemoryCheckOffGuard guard;
rocksdb::ReadOptions options;
std::vector<rocksdb::Slice> slices;
for (size_t index = 0; index < keys.size(); index++) {
slices.emplace_back(keys[index]);
}
auto status = db_->MultiGet(options, slices, values);
std::vector<Status> ret;
std::transform(status.begin(), status.end(), std::back_inserter(ret), [](const auto& s) {
if (s.ok()) {
return Status::OK();
} else if (s.IsNotFound()) {
return Status::KeyNotFound();
} else {
return Status::Error();
}
});
return ret;
}
nebula::cpp2::ErrorCode RocksEngine::range(const std::string& start,
const std::string& end,
std::unique_ptr<KVIterator>* storageIter) {
memory::MemoryCheckOffGuard guard;
rocksdb::ReadOptions options;
if (!isPlainTable_) {
options.total_order_seek = FLAGS_enable_rocksdb_prefix_filtering;
} else {
options.prefix_same_as_start = true;
}
std::unique_ptr<rocksdb::Iterator> iter(db_->NewIterator(options));
if (iter) {
iter->Seek(rocksdb::Slice(start));
}
storageIter->reset(new RocksRangeIter(std::move(iter), start, end));
return nebula::cpp2::ErrorCode::SUCCEEDED;
}
nebula::cpp2::ErrorCode RocksEngine::prefix(const std::string& prefix,
std::unique_ptr<KVIterator>* storageIter,
const void* snapshot) {
memory::MemoryCheckOffGuard guard;
// In fact, we don't need to check prefix.size() >= extractorLen_, which is caller's duty to make
// sure the prefix bloom filter exists. But this is quite error-prone, so we do a check here.
if (FLAGS_enable_rocksdb_prefix_filtering && prefix.size() >= extractorLen_) {
return prefixWithExtractor(prefix, snapshot, storageIter);
} else {
return prefixWithoutExtractor(prefix, snapshot, storageIter);
}
}
nebula::cpp2::ErrorCode RocksEngine::prefixWithExtractor(const std::string& prefix,
const void* snapshot,
std::unique_ptr<KVIterator>* storageIter) {
memory::MemoryCheckOffGuard guard;
rocksdb::ReadOptions options;
if (UNLIKELY(snapshot != nullptr)) {
options.snapshot = reinterpret_cast<const rocksdb::Snapshot*>(snapshot);
}
options.prefix_same_as_start = true;
std::unique_ptr<rocksdb::Iterator> iter(db_->NewIterator(options));
if (iter) {
iter->Seek(rocksdb::Slice(prefix));
}
storageIter->reset(new RocksPrefixIter(std::move(iter), prefix));
return nebula::cpp2::ErrorCode::SUCCEEDED;
}
nebula::cpp2::ErrorCode RocksEngine::prefixWithoutExtractor(
const std::string& prefix, const void* snapshot, std::unique_ptr<KVIterator>* storageIter) {
memory::MemoryCheckOffGuard guard;
rocksdb::ReadOptions options;
if (snapshot != nullptr) {
options.snapshot = reinterpret_cast<const rocksdb::Snapshot*>(snapshot);
}
// prefix_same_as_start is false by default
options.total_order_seek = FLAGS_enable_rocksdb_prefix_filtering;
std::unique_ptr<rocksdb::Iterator> iter(db_->NewIterator(options));
if (iter) {
iter->Seek(rocksdb::Slice(prefix));
}
storageIter->reset(new RocksPrefixIter(std::move(iter), prefix));
return nebula::cpp2::ErrorCode::SUCCEEDED;
}
nebula::cpp2::ErrorCode RocksEngine::rangeWithPrefix(const std::string& start,
const std::string& prefix,
std::unique_ptr<KVIterator>* storageIter) {
memory::MemoryCheckOffGuard guard;
rocksdb::ReadOptions options;
if (!isPlainTable_) {
options.total_order_seek = FLAGS_enable_rocksdb_prefix_filtering;
} else {
options.prefix_same_as_start = true;
}
std::unique_ptr<rocksdb::Iterator> iter(db_->NewIterator(options));
if (iter) {
iter->Seek(rocksdb::Slice(start));
}
storageIter->reset(new RocksPrefixIter(std::move(iter), prefix));
return nebula::cpp2::ErrorCode::SUCCEEDED;
}
nebula::cpp2::ErrorCode RocksEngine::scan(std::unique_ptr<KVIterator>* storageIter) {
memory::MemoryCheckOffGuard guard;
rocksdb::ReadOptions options;
options.total_order_seek = true;
std::unique_ptr<rocksdb::Iterator> iter(db_->NewIterator(options));
iter->SeekToFirst();
storageIter->reset(new RocksCommonIter(std::move(iter)));
return nebula::cpp2::ErrorCode::SUCCEEDED;
}
nebula::cpp2::ErrorCode RocksEngine::put(std::string key, std::string value) {
rocksdb::WriteOptions options;
options.disableWAL = FLAGS_rocksdb_disable_wal;
rocksdb::Status status = db_->Put(options, key, value);
if (status.ok()) {
return nebula::cpp2::ErrorCode::SUCCEEDED;
} else {
VLOG(4) << "Put Failed: " << key << status.ToString();
return nebula::cpp2::ErrorCode::E_UNKNOWN;
}
}
nebula::cpp2::ErrorCode RocksEngine::multiPut(std::vector<KV> keyValues) {
rocksdb::WriteBatch updates(FLAGS_rocksdb_batch_size);
for (size_t i = 0; i < keyValues.size(); i++) {
updates.Put(keyValues[i].first, keyValues[i].second);
}
rocksdb::WriteOptions options;
options.disableWAL = FLAGS_rocksdb_disable_wal;
rocksdb::Status status = db_->Write(options, &updates);
if (status.ok()) {
return nebula::cpp2::ErrorCode::SUCCEEDED;
} else {
VLOG(4) << "MultiPut Failed: " << status.ToString();
return nebula::cpp2::ErrorCode::E_UNKNOWN;
}
}
nebula::cpp2::ErrorCode RocksEngine::remove(const std::string& key) {
rocksdb::WriteOptions options;
options.disableWAL = FLAGS_rocksdb_disable_wal;
auto status = db_->Delete(options, key);
if (status.ok()) {
return nebula::cpp2::ErrorCode::SUCCEEDED;
} else {
VLOG(4) << "Remove Failed: " << key << status.ToString();
return nebula::cpp2::ErrorCode::E_UNKNOWN;
}
}
nebula::cpp2::ErrorCode RocksEngine::multiRemove(std::vector<std::string> keys) {
rocksdb::WriteBatch deletes(FLAGS_rocksdb_batch_size);
for (size_t i = 0; i < keys.size(); i++) {
deletes.Delete(keys[i]);
}
rocksdb::WriteOptions options;
options.disableWAL = FLAGS_rocksdb_disable_wal;
rocksdb::Status status = db_->Write(options, &deletes);
if (status.ok()) {
return nebula::cpp2::ErrorCode::SUCCEEDED;
} else {
VLOG(4) << "MultiRemove Failed: " << status.ToString();
return nebula::cpp2::ErrorCode::E_UNKNOWN;
}
}
nebula::cpp2::ErrorCode RocksEngine::removeRange(const std::string& start, const std::string& end) {
rocksdb::WriteOptions options;
options.disableWAL = FLAGS_rocksdb_disable_wal;
auto status = db_->DeleteRange(options, db_->DefaultColumnFamily(), start, end);
if (status.ok()) {
return nebula::cpp2::ErrorCode::SUCCEEDED;
} else {
VLOG(4) << "RemoveRange Failed: " << status.ToString();
return nebula::cpp2::ErrorCode::E_UNKNOWN;
}
}
std::string RocksEngine::partKey(PartitionID partId) {
return NebulaKeyUtils::systemPartKey(partId);
}
std::string RocksEngine::balanceKey(PartitionID partId) {
return NebulaKeyUtils::systemBalanceKey(partId);
}
void RocksEngine::addPart(PartitionID partId, const Peers& raftPeers) {
auto ret = put(partKey(partId), "");
if (ret == nebula::cpp2::ErrorCode::SUCCEEDED) {
partsNum_++;
CHECK_GE(partsNum_, 0);
}
if (!raftPeers.allNormalPeers()) {
put(balanceKey(partId), raftPeers.toString());
}
}
nebula::cpp2::ErrorCode RocksEngine::updatePart(PartitionID partId, const Peer& raftPeer) {
std::string val;
auto ret = get(balanceKey(partId), &val);
Peers peers;
if (ret == nebula::cpp2::ErrorCode::SUCCEEDED) {
peers = Peers::fromString(val);
} else if (ret == nebula::cpp2::ErrorCode::E_KEY_NOT_FOUND) {
// do nothing
} else {
LOG(INFO) << "Update part failed when get, partId=" << partId;
return ret;
}
peers.addOrUpdate(raftPeer);
if (peers.allNormalPeers()) {
// When all replica become normal peers, delete this temp key. (when learner is promoted to
// normal replica)
ret = remove(balanceKey(partId));
} else {
ret = put(balanceKey(partId), peers.toString());
}
if (ret != nebula::cpp2::ErrorCode::SUCCEEDED) {
LOG(INFO) << "Update part failed when put back, partId=" << partId;
}
return ret;
}
void RocksEngine::removePart(PartitionID partId) {
rocksdb::WriteOptions options;
options.disableWAL = FLAGS_rocksdb_disable_wal;
std::vector<std::string> sysKeysToDelete;
sysKeysToDelete.emplace_back(partKey(partId));
sysKeysToDelete.emplace_back(balanceKey(partId));
sysKeysToDelete.emplace_back(NebulaKeyUtils::systemCommitKey(partId));
auto code = multiRemove(sysKeysToDelete);
if (code == nebula::cpp2::ErrorCode::SUCCEEDED) {
partsNum_--;
CHECK_GE(partsNum_, 0);
}
}
std::vector<PartitionID> RocksEngine::allParts() {
std::unique_ptr<KVIterator> iter;
std::vector<PartitionID> parts;
static const std::string prefixStr = NebulaKeyUtils::systemPrefix();
auto retCode = this->prefix(prefixStr, &iter);
if (nebula::cpp2::ErrorCode::SUCCEEDED != retCode) {
return parts;
}
while (iter->valid()) {
auto key = iter->key();
if (!NebulaKeyUtils::isSystemPart(key)) {
iter->next();
continue;
}
PartitionID partId = *reinterpret_cast<const PartitionID*>(key.data());
partId = partId >> 8;
parts.emplace_back(partId);
iter->next();
}
return parts;
}
std::map<PartitionID, Peers> RocksEngine::balancePartPeers() {
std::unique_ptr<KVIterator> iter;
std::map<PartitionID, Peers> partRaftPeers;
static const std::string prefixStr = NebulaKeyUtils::systemPrefix();
auto retCode = this->prefix(prefixStr, &iter);
if (nebula::cpp2::ErrorCode::SUCCEEDED != retCode) {
return partRaftPeers;
}
while (iter->valid()) {
auto key = iter->key();
if (!NebulaKeyUtils::isSystemBalance(key)) {
iter->next();
continue;
}
PartitionID partId = *reinterpret_cast<const PartitionID*>(key.data());
auto raftPeers = Peers::fromString(iter->val().toString());
partId = partId >> 8;
partRaftPeers.emplace(partId, raftPeers);
iter->next();
}
return partRaftPeers;
}
int32_t RocksEngine::totalPartsNum() {
return partsNum_;
}
nebula::cpp2::ErrorCode RocksEngine::ingest(const std::vector<std::string>& files,
bool verifyFileChecksum) {
if (files.empty()) {
return nebula::cpp2::ErrorCode::SUCCEEDED;
}
rocksdb::IngestExternalFileOptions options;
options.move_files = FLAGS_move_files;
options.verify_file_checksum = verifyFileChecksum;
rocksdb::Status status = db_->IngestExternalFile(files, options);
if (status.ok()) {
return nebula::cpp2::ErrorCode::SUCCEEDED;
} else {
LOG(WARNING) << "Ingest Failed: " << status.ToString();
return nebula::cpp2::ErrorCode::E_UNKNOWN;
}
}
nebula::cpp2::ErrorCode RocksEngine::setOption(const std::string& configKey,
const std::string& configValue) {
std::unordered_map<std::string, std::string> configOptions = {{configKey, configValue}};
rocksdb::Status status = db_->SetOptions(configOptions);
if (status.ok()) {
LOG(INFO) << "SetOption Succeeded: " << configKey << ":" << configValue;
return nebula::cpp2::ErrorCode::SUCCEEDED;
} else {
LOG(WARNING) << "SetOption Failed: " << configKey << ":" << configValue;
return nebula::cpp2::ErrorCode::E_INVALID_PARM;
}
}
nebula::cpp2::ErrorCode RocksEngine::setDBOption(const std::string& configKey,
const std::string& configValue) {
std::unordered_map<std::string, std::string> configOptions = {{configKey, configValue}};
rocksdb::Status status = db_->SetDBOptions(configOptions);
if (status.ok()) {
LOG(INFO) << "SetDBOption Succeeded: " << configKey << ":" << configValue;
return nebula::cpp2::ErrorCode::SUCCEEDED;
} else {
LOG(WARNING) << "SetDBOption Failed: " << configKey << ":" << configValue;
return nebula::cpp2::ErrorCode::E_INVALID_PARM;
}
}
ErrorOr<nebula::cpp2::ErrorCode, std::string> RocksEngine::getProperty(
const std::string& property) {
std::string value;
if (!db_->GetProperty(property, &value)) {
return nebula::cpp2::ErrorCode::E_INVALID_PARM;
} else {
return value;
}
}
nebula::cpp2::ErrorCode RocksEngine::compact() {
rocksdb::CompactRangeOptions options;
options.change_level = FLAGS_rocksdb_compact_change_level;
options.target_level = FLAGS_rocksdb_compact_target_level;
rocksdb::Status status = db_->CompactRange(options, nullptr, nullptr);
if (status.ok()) {
return nebula::cpp2::ErrorCode::SUCCEEDED;
} else {
LOG(WARNING) << "CompactAll Failed: " << status.ToString();
return nebula::cpp2::ErrorCode::E_UNKNOWN;
}
}
nebula::cpp2::ErrorCode RocksEngine::flush() {
rocksdb::FlushOptions options;
rocksdb::Status status = db_->Flush(options);
if (status.ok()) {
return nebula::cpp2::ErrorCode::SUCCEEDED;
} else {
LOG(WARNING) << "Flush Failed: " << status.ToString();
return nebula::cpp2::ErrorCode::E_UNKNOWN;
}
}
nebula::cpp2::ErrorCode RocksEngine::backup() {
if (!backupDb_) {
return nebula::cpp2::ErrorCode::SUCCEEDED;
}
LOG(INFO) << "begin to backup space " << spaceId_ << " on path " << backupPath_;
bool flushBeforeBackup = true;
auto status = backupDb_->CreateNewBackup(db_.get(), flushBeforeBackup);
if (status.ok()) {
return nebula::cpp2::ErrorCode::SUCCEEDED;
} else {
LOG(WARNING) << "backup failed: " << status.ToString();
return nebula::cpp2::ErrorCode::E_BACKUP_FAILED;
}
}
void RocksEngine::openBackupEngine(GraphSpaceID spaceId) {
// If backup dir is not empty, set backup related options
if (FLAGS_rocksdb_table_format == "PlainTable" && !FLAGS_rocksdb_backup_dir.empty()) {
backupPath_ =
folly::stringPrintf("%s/rocksdb_backup/%d", FLAGS_rocksdb_backup_dir.c_str(), spaceId);
if (FileUtils::fileType(backupPath_.c_str()) == FileType::NOTEXIST) {
if (!FileUtils::makeDir(backupPath_)) {
LOG(FATAL) << "makeDir " << backupPath_ << " failed";
}
}
rocksdb::BackupEngine* backupDb;
rocksdb::BackupEngineOptions backupOptions(backupPath_);
backupOptions.backup_log_files = false;
auto status = rocksdb::BackupEngine::Open(rocksdb::Env::Default(), backupOptions, &backupDb);
CHECK(status.ok()) << status.ToString();
backupDb_.reset(backupDb);
LOG(INFO) << "open plain table backup engine on " << backupPath_;
std::string dataPath = folly::stringPrintf("%s/data", dataPath_.c_str());
auto walDir = dataPath;
if (!FLAGS_rocksdb_wal_dir.empty()) {
walDir = folly::stringPrintf("%s/rocksdb_wal/%d", FLAGS_rocksdb_wal_dir.c_str(), spaceId);
} else {
LOG(WARNING) << "rocksdb wal is stored with data. If data_path is on tmpfs, the wal is "
"volatile as well";
}
rocksdb::RestoreOptions restoreOptions;
restoreOptions.keep_log_files = true;
status = backupDb_->RestoreDBFromLatestBackup(dataPath, walDir, restoreOptions);
LOG(INFO) << "try to restore from backup path " << backupPath_;
if (status.IsNotFound()) {
LOG(WARNING) << "no valid backup found";
return;
} else if (!status.ok()) {
LOG(FATAL) << status.ToString();
}
LOG(INFO) << "restore from latest backup successfully"
<< ", backup path " << backupPath_ << ", wal path " << walDir << ", data path "
<< dataPath;
}
}
nebula::cpp2::ErrorCode RocksEngine::createCheckpoint(const std::string& checkpointPath) {
LOG(INFO) << "Target checkpoint data path : " << checkpointPath;
if (fs::FileUtils::exist(checkpointPath) && !fs::FileUtils::remove(checkpointPath.data(), true)) {
LOG(WARNING) << "Remove exist checkpoint data dir failed: " << checkpointPath;
return nebula::cpp2::ErrorCode::E_STORE_FAILURE;
}
rocksdb::Checkpoint* checkpoint;
rocksdb::Status status = rocksdb::Checkpoint::Create(db_.get(), &checkpoint);
if (!status.ok()) {
LOG(WARNING) << "Init checkpoint Failed: " << status.ToString();
return nebula::cpp2::ErrorCode::E_FAILED_TO_CHECKPOINT;
}
std::unique_ptr<rocksdb::Checkpoint> cp(checkpoint);
status = cp->CreateCheckpoint(checkpointPath, 0);
if (!status.ok()) {
LOG(WARNING) << "Create checkpoint Failed: " << status.ToString();
return nebula::cpp2::ErrorCode::E_FAILED_TO_CHECKPOINT;
}
return nebula::cpp2::ErrorCode::SUCCEEDED;
}
ErrorOr<nebula::cpp2::ErrorCode, std::string> RocksEngine::backupTable(
const std::string& name,
const std::string& tablePrefix,
std::function<bool(const folly::StringPiece& key)> filter) {
auto backupPath = folly::stringPrintf(
"%s/checkpoints/%s/%s.sst", dataPath_.c_str(), name.c_str(), tablePrefix.c_str());
VLOG(3) << "Start writing the sst file with table (" << tablePrefix
<< ") to file: " << backupPath;
auto parent = backupPath.substr(0, backupPath.rfind('/'));
if (!FileUtils::exist(parent)) {
if (!FileUtils::makeDir(parent)) {
LOG(WARNING) << "Make dir " << parent << " failed";
return nebula::cpp2::ErrorCode::E_BACKUP_FAILED;
}
}
std::unique_ptr<KVIterator> iter;
auto ret = prefix(tablePrefix, &iter);
if (ret != nebula::cpp2::ErrorCode::SUCCEEDED) {
return nebula::cpp2::ErrorCode::E_BACKUP_EMPTY_TABLE;
}
if (!iter->valid()) {
return nebula::cpp2::ErrorCode::E_BACKUP_EMPTY_TABLE;
}
rocksdb::Options options;
options.file_checksum_gen_factory = rocksdb::GetFileChecksumGenCrc32cFactory();
rocksdb::SstFileWriter sstFileWriter(rocksdb::EnvOptions(), options);
auto s = sstFileWriter.Open(backupPath);
if (!s.ok()) {
LOG(WARNING) << "BackupTable failed, path: " << backupPath << ", error: " << s.ToString();
return nebula::cpp2::ErrorCode::E_BACKUP_TABLE_FAILED;
}
for (; iter->valid(); iter->next()) {
if (filter && filter(iter->key())) {
continue;
}
s = sstFileWriter.Put(iter->key().toString(), iter->val().toString());
if (!s.ok()) {
LOG(WARNING) << "BackupTable failed, path: " << backupPath << ", error: " << s.ToString();
sstFileWriter.Finish();
return nebula::cpp2::ErrorCode::E_BACKUP_TABLE_FAILED;
}
}
s = sstFileWriter.Finish();
if (!s.ok()) {
LOG(WARNING) << "Failed to insert data when backupTable, " << backupPath
<< ", error: " << s.ToString();
return nebula::cpp2::ErrorCode::E_BACKUP_EMPTY_TABLE;
}
if (sstFileWriter.FileSize() == 0) {
return nebula::cpp2::ErrorCode::E_BACKUP_EMPTY_TABLE;
}
if (backupPath[0] == '/') {
return backupPath;
}
auto result = FileUtils::realPath(backupPath.c_str());
if (!result.ok()) {
return nebula::cpp2::ErrorCode::E_BACKUP_TABLE_FAILED;
}
return result.value();
}
} // namespace kvstore
} // namespace nebula