forked from vesoft-inc/nebula
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathNebulaStore.h
882 lines (789 loc) · 27 KB
/
NebulaStore.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
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
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
/* Copyright (c) 2018 vesoft inc. All rights reserved.
*
* This source code is licensed under Apache 2.0 License.
*/
#ifndef KVSTORE_NEBULASTORE_H_
#define KVSTORE_NEBULASTORE_H_
#include <folly/RWSpinLock.h>
#include <folly/concurrency/ConcurrentHashMap.h>
#include <gtest/gtest_prod.h>
#include "common/base/Base.h"
#include "common/ssl/SSLConfig.h"
#include "common/utils/Utils.h"
#include "interface/gen-cpp2/RaftexServiceAsyncClient.h"
#include "kvstore/DiskManager.h"
#include "kvstore/KVEngine.h"
#include "kvstore/KVStore.h"
#include "kvstore/Part.h"
#include "kvstore/PartManager.h"
#include "kvstore/listener/Listener.h"
#include "kvstore/raftex/RaftexService.h"
#include "kvstore/raftex/SnapshotManager.h"
namespace nebula {
namespace kvstore {
using ListenerMap = std::unordered_map<meta::cpp2::ListenerType, std::shared_ptr<Listener>>;
struct SpacePartInfo {
~SpacePartInfo() {
parts_.clear();
engines_.clear();
}
std::unordered_map<PartitionID, std::shared_ptr<Part>> parts_;
std::vector<std::unique_ptr<KVEngine>> engines_;
};
struct SpaceListenerInfo {
std::unordered_map<PartitionID, ListenerMap> listeners_;
};
/**
* @brief A derived class of KVStore, interfaces to manipulate data
*/
class NebulaStore : public KVStore, public Handler {
FRIEND_TEST(NebulaStoreTest, SimpleTest);
FRIEND_TEST(NebulaStoreTest, MultiPathTest);
FRIEND_TEST(NebulaStoreTest, PartsTest);
FRIEND_TEST(NebulaStoreTest, PersistPeersTest);
FRIEND_TEST(NebulaStoreTest, ThreeCopiesTest);
FRIEND_TEST(NebulaStoreTest, TransLeaderTest);
FRIEND_TEST(NebulaStoreTest, CheckpointTest);
FRIEND_TEST(NebulaStoreTest, ThreeCopiesCheckpointTest);
FRIEND_TEST(NebulaStoreTest, RemoveInvalidSpaceTest);
friend class ListenerBasicTest;
public:
/**
* @brief Construct a new NebulaStore object
*
* @param options
* @param ioPool IOThreadPool
* @param serviceAddr Address of NebulaStore, used in raft
* @param workers Worker thread
*/
NebulaStore(KVOptions options,
std::shared_ptr<folly::IOThreadPoolExecutor> ioPool,
HostAddr serviceAddr,
std::shared_ptr<folly::Executor> workers)
: ioPool_(ioPool),
storeSvcAddr_(serviceAddr),
workers_(workers),
raftAddr_(getRaftAddr(serviceAddr)),
options_(std::move(options)) {
CHECK_NOTNULL(options_.partMan_);
clientMan_ =
std::make_shared<thrift::ThriftClientManager<raftex::cpp2::RaftexServiceAsyncClient>>(
FLAGS_enable_ssl);
}
~NebulaStore();
/**
* @brief Calculate the raft service address based on the storage service address
*
* @param srvcAddr Storage service address
* @return HostAddr Raft service address
*/
static HostAddr getRaftAddr(const HostAddr& srvcAddr) {
return Utils::getRaftAddrFromStoreAddr(srvcAddr);
}
/**
* @brief Calculate the storage service address based on the raft service address
*
* @param raftAddr Raft service address
* @return HostAddr Storage service address
*/
static HostAddr getStoreAddr(const HostAddr& raftAddr) {
return Utils::getStoreAddrFromRaftAddr(raftAddr);
}
/**
* @brief Pull meta information from the PartManager and initiate the current store instance
*
* @return True if succeed. False if failed.
*/
bool init();
/**
* @brief Stop the engine
*/
void stop() override;
/**
* @brief Return bit-wise capability, not used
*/
uint32_t capability() const override {
return 0;
}
/**
* @brief Return storage service address
*/
HostAddr address() const {
return storeSvcAddr_;
}
/**
* @brief Get the IOThreadPool
*/
std::shared_ptr<folly::IOThreadPoolExecutor> getIoPool() const {
return ioPool_;
}
/**
* @brief Get the Background workers
*/
std::shared_ptr<thread::GenericThreadPool> getBgWorkers() const {
return bgWorkers_;
}
/**
* @brief Get the worker executors
*/
std::shared_ptr<folly::Executor> getExecutors() const {
return workers_;
}
/**
* @brief Return the current leader
*
* @param spaceId
* @param partId
* @return ErrorOr<nebula::cpp2::ErrorCode, HostAddr> Get the leader address of given partition if
* succeed, else return ErrorCode
*/
ErrorOr<nebula::cpp2::ErrorCode, HostAddr> partLeader(GraphSpaceID spaceId,
PartitionID partId) override;
/**
* @brief Return pointer of part manager
*
* @return PartManager*
*/
PartManager* partManager() const override {
return options_.partMan_.get();
}
/**
* @brief Return the NebulaStore is started as listener
*/
bool isListener() const {
return !options_.listenerPath_.empty();
}
/**
* @brief Get the data paths passed from configuration
*
* @return std::vector<std::string> Data paths
*/
std::vector<std::string> getDataRoot() const override {
return options_.dataPaths_;
}
/**
* @brief Get the Snapshot from engine.
*
* @param spaceId
* @param partID
* @return const void* Snapshot pointer.
*/
const void* GetSnapshot(GraphSpaceID spaceId, PartitionID partID) override;
/**
* @brief Release snapshot from engine.
*
* @param spaceId
* @param partId
* @param snapshot
*/
void ReleaseSnapshot(GraphSpaceID spaceId, PartitionID partId, const void* snapshot) override;
/**
* @brief Read a single key
*
* @param spaceId
* @param partId
* @param key
* @param value
* @param canReadFromFollower Whether check if current kvstore is leader of given partition
* @return nebula::cpp2::ErrorCode
*/
nebula::cpp2::ErrorCode get(GraphSpaceID spaceId,
PartitionID partId,
const std::string& key,
std::string* value,
bool canReadFromFollower = false,
const void* snapshot = nullptr) override;
/**
* @brief Read a list of keys
*
* @param spaceId
* @param partId
* @param keys Keys to read
* @param values Pointers of value
* @param canReadFromFollower Whether check if current kvstore is leader of given partition
* @return Return std::vector<Status> when succeeded: Result status of each key, if key[i] does
* not exist, the i-th value in return value would be Status::KeyNotFound. Return ErrorCode when
* failed
*/
std::pair<nebula::cpp2::ErrorCode, std::vector<Status>> multiGet(
GraphSpaceID spaceId,
PartitionID partId,
const std::vector<std::string>& keys,
std::vector<std::string>* values,
bool canReadFromFollower = false) override;
/**
* @brief Get all results in range [start, end)
*
* @param spaceId
* @param partId
* @param start Start key, inclusive
* @param end End key, exclusive
* @param iter Iterator in range [start, end), returns by kv engine
* @param canReadFromFollower Whether check if current kvstore is leader of given partition
* @return nebula::cpp2::ErrorCode
*/
nebula::cpp2::ErrorCode range(GraphSpaceID spaceId,
PartitionID partId,
const std::string& start,
const std::string& end,
std::unique_ptr<KVIterator>* iter,
bool canReadFromFollower = false) override;
/**
* @brief To forbid to pass rvalue via the 'range' parameter.
*/
nebula::cpp2::ErrorCode range(GraphSpaceID spaceId,
PartitionID partId,
std::string&& start,
std::string&& end,
std::unique_ptr<KVIterator>* iter,
bool canReadFromFollower = false) override = delete;
/**
* @brief Get all results with 'prefix' str as prefix.
*
* @param spaceId
* @param partId
* @param prefix Key of prefix to seek
* @param iter Iterator of keys starts with 'prefix', returns by kv engine
* @param canReadFromFollower Whether check if current kvstore is leader of given partition
* @return nebula::cpp2::ErrorCode
*/
nebula::cpp2::ErrorCode prefix(GraphSpaceID spaceId,
PartitionID partId,
const std::string& prefix,
std::unique_ptr<KVIterator>* iter,
bool canReadFromFollower = false,
const void* snapshot = nullptr) override;
/**
* @brief To forbid to pass rvalue via the 'prefix' parameter.
*/
nebula::cpp2::ErrorCode prefix(GraphSpaceID spaceId,
PartitionID partId,
std::string&& prefix,
std::unique_ptr<KVIterator>* iter,
bool canReadFromFollower = false,
const void* snapshot = nullptr) override = delete;
/**
* @brief Get all results with 'prefix' str as prefix starting form 'start'
*
* @param spaceId
* @param partId
* @param start Start key, inclusive
* @param prefix The prefix of keys to iterate
* @param iter Iterator of keys starts with 'prefix' beginning from 'start', returns by kv engine
* @param canReadFromFollower Whether check if current kvstore is leader of given partition
* @return nebula::cpp2::ErrorCode
*/
nebula::cpp2::ErrorCode rangeWithPrefix(GraphSpaceID spaceId,
PartitionID partId,
const std::string& start,
const std::string& prefix,
std::unique_ptr<KVIterator>* iter,
bool canReadFromFollower = false) override;
/**
* @brief To forbid to pass rvalue via the 'rangeWithPrefix' parameter.
*/
nebula::cpp2::ErrorCode rangeWithPrefix(GraphSpaceID spaceId,
PartitionID partId,
std::string&& start,
std::string&& prefix,
std::unique_ptr<KVIterator>* iter,
bool canReadFromFollower = false) override = delete;
/**
* @brief Synchronize the kvstore across multiple replica by add a empty log
*
* @param spaceId
* @param partId
* @return nebula::cpp2::ErrorCode
*/
nebula::cpp2::ErrorCode sync(GraphSpaceID spaceId, PartitionID partId) override;
/**
* @brief Write multiple key/values to kvstore asynchronously
*
* @param spaceId
* @param partId
* @param keyValues Key/values to put
* @param cb Callback when has a result
*/
void asyncMultiPut(GraphSpaceID spaceId,
PartitionID partId,
std::vector<KV>&& keyValues,
KVCallback cb) override;
/**
* @brief Remove a key from kvstore asynchronously
*
* @param spaceId
* @param partId
* @param key Key to remove
* @param cb Callback when has a result
*/
void asyncRemove(GraphSpaceID spaceId,
PartitionID partId,
const std::string& key,
KVCallback cb) override;
/**
* @brief Remove multiple keys from kvstore asynchronously
*
* @param spaceId
* @param partId
* @param key Keys to remove
* @param cb Callback when has a result
*/
void asyncMultiRemove(GraphSpaceID spaceId,
PartitionID partId,
std::vector<std::string>&& keys,
KVCallback cb) override;
/**
* @brief Remove keys in range [start, end) asynchronously
*
* @param spaceId
* @param partId
* @param start Start key
* @param end End key
* @param cb Callback when has a result
*/
void asyncRemoveRange(GraphSpaceID spaceId,
PartitionID partId,
const std::string& start,
const std::string& end,
KVCallback cb) override;
/**
* @brief Async commit multi operation, difference between asyncMultiPut or asyncMultiRemove
* is this method allow contains both put and remove together, difference between asyncAtomicOp is
* that asyncAtomicOp may have CAS
*
* @param spaceId
* @param partId
* @param batch Encoded write batch
* @param cb Callback when has a result
*/
void asyncAppendBatch(GraphSpaceID spaceId,
PartitionID partId,
std::string&& batch,
KVCallback cb) override;
/**
* @brief Do some atomic operation on kvstore
*
* @param spaceId
* @param partId
* @param op Atomic operation
* @param cb Callback when has a result
*/
void asyncAtomicOp(GraphSpaceID spaceId,
PartitionID partId,
MergeableAtomicOp op,
KVCallback cb) override;
/**
* @brief Get the part object of given spaceId and partId
*
* @param spaceId
* @param partId
* @return ErrorOr<nebula::cpp2::ErrorCode, std::shared_ptr<Part>> Return the part if succeeded,
* else return ErrorCode
*/
ErrorOr<nebula::cpp2::ErrorCode, std::shared_ptr<Part>> part(GraphSpaceID spaceId,
PartitionID partId) override;
/**
* @brief Ingest the sst file under download directory
*
* @param spaceId
* @return nebula::cpp2::ErrorCode
*/
nebula::cpp2::ErrorCode ingest(GraphSpaceID spaceId) override;
/**
* @brief Set space related rocksdb option
*
* @param spaceId
* @param configKey Config name
* @param configValue Config value
* @return nebula::cpp2::ErrorCode
*/
nebula::cpp2::ErrorCode setOption(GraphSpaceID spaceId,
const std::string& configKey,
const std::string& configValue);
/**
* @brief Set space related rocksdb db option
*
* @param spaceId
* @param configKey DB Config name
* @param configValue Config value
* @return nebula::cpp2::ErrorCode
*/
nebula::cpp2::ErrorCode setDBOption(GraphSpaceID spaceId,
const std::string& configKey,
const std::string& configValue);
/**
* @brief Trigger comapction, only used in rocksdb
*
* @param spaceId
* @return nebula::cpp2::ErrorCode
*/
nebula::cpp2::ErrorCode compact(GraphSpaceID spaceId) override;
/**
* @brief Trigger flush, only used in rocksdb
*
* @param spaceId
* @return nebula::cpp2::ErrorCode
*/
nebula::cpp2::ErrorCode flush(GraphSpaceID spaceId) override;
/**
* @brief Create a Checkpoint, only used in rocksdb
*
* @param spaceId
* @param name Checkpoint name
* @return ErrorOr<nebula::cpp2::ErrorCode, std::vector<cpp2::CheckpointInfo>> Return the
* checkpoint info if succeed, else return ErrorCode
*/
ErrorOr<nebula::cpp2::ErrorCode, std::vector<cpp2::CheckpointInfo>> createCheckpoint(
GraphSpaceID spaceId, const std::string& name) override;
/**
* @brief Trigger kv engine's backup, mainly for rocksdb PlainTable mounted on tmpfs/ramfs
*
* @return nebula::cpp2::ErrorCode
*/
nebula::cpp2::ErrorCode backup();
/**
* @brief Drop a Checkpoint, only used in rocksdb
*
* @param spaceId
* @param name Checkpoint name
* @return nebula::cpp2::ErrorCode
*/
nebula::cpp2::ErrorCode dropCheckpoint(GraphSpaceID spaceId, const std::string& name) override;
/**
* @brief Set the write blocking flag, if blocked, only heartbeat can be replicated
*
* @param spaceId
* @param sign True to block. Falst to unblock
* @return nebula::cpp2::ErrorCode
*/
nebula::cpp2::ErrorCode setWriteBlocking(GraphSpaceID spaceId, bool sign) override;
/**
* @brief Whether is leader of given partition or not
*
* @param spaceId
* @param partId
*/
bool isLeader(GraphSpaceID spaceId, PartitionID partId);
/**
* @brief Try to retrieve the space part info of given spaceId
*
* @param spaceId
* @return ErrorOr<nebula::cpp2::ErrorCode, std::shared_ptr<SpacePartInfo>> Return space part info
* when succeed, return Errorcode when failed
*/
ErrorOr<nebula::cpp2::ErrorCode, std::shared_ptr<SpacePartInfo>> space(GraphSpaceID spaceId);
/**
* @brief Try to retrieve the space listener info of given spaceId
*
* @param spaceId
* @return ErrorOr<nebula::cpp2::ErrorCode, std::shared_ptr<SpaceListenerInfo>> Return space
* listener info when succeed, return Errorcode when failed
*/
ErrorOr<nebula::cpp2::ErrorCode, std::shared_ptr<SpaceListenerInfo>> spaceListener(
GraphSpaceID spaceId);
/**
* @brief Add a space, called from part manager
*
* @param spaceId
*/
void addSpace(GraphSpaceID spaceId) override;
/**
* @brief Add a partition, called from part manager
*
* @param spaceId
* @param partId
* @param asLearner Whether start partition as learner
* @param peers Storage peers, do not contain learner address
*/
void addPart(GraphSpaceID spaceId,
PartitionID partId,
bool asLearner,
const std::vector<HostAddr>& peers) override;
/**
* @brief Remove a space, called from part manager
*
* @param spaceId
* @param isListener Whether the space is listener
*/
void removeSpace(GraphSpaceID spaceId) override;
/**
* @brief clear space data, but not remove the data dirs.
*
* @param spaceId space which will be cleared.
* @return
*/
nebula::cpp2::ErrorCode clearSpace(GraphSpaceID spaceId) override;
/**
* @brief Remove a partition, called from part manager
*
* @param spaceId
* @param partId
* @param needLock if lock_ has already been locked, we need
* to set needLock as false, or we set it as true
*/
void removePart(GraphSpaceID spaceId, PartitionID partId, bool needLock = true) override;
/**
* @brief Retrieve the leader distribution
*
* @param leaderIds The leader address of all partitions
* @return int32_t The leader count of all spaces
*/
int32_t allLeader(
std::unordered_map<GraphSpaceID, std::vector<meta::cpp2::LeaderInfo>>& leaderIds) override;
/**
* @brief Backup the data of a table prefix, for meta backup
*
* @param spaceId
* @param name
* @param tablePrefix Table prefix
* @param filter Data filter when iterate the table
* @return ErrorOr<nebula::cpp2::ErrorCode, std::vector<std::string>> Return the sst file path if
* succeed, else return ErrorCode
*/
ErrorOr<nebula::cpp2::ErrorCode, std::vector<std::string>> backupTable(
GraphSpaceID spaceId,
const std::string& name,
const std::string& tablePrefix,
std::function<bool(const folly::StringPiece& key)> filter) override;
/**
* @brief Restore from sst files
*
* @param spaceId
* @param files SST file path
* @return nebula::cpp2::ErrorCode
*/
nebula::cpp2::ErrorCode restoreFromFiles(GraphSpaceID spaceId,
const std::vector<std::string>& files) override;
/**
* @brief Add a specified type listener to space. Perform extra initialization of given type
* listener if necessary. User should call addListenerSpace first then addListenerPart.
*
* @param spaceId
* @param type Listener type
*/
void addListenerSpace(GraphSpaceID spaceId, meta::cpp2::ListenerType type) override;
/**
* @brief Remove a specified type listener from space. Perform extra destruction of given type
* listener if necessary. User should call removeListenerPart first then removeListenerSpace.
*
* @param spaceId
* @param type Listener type
*/
void removeListenerSpace(GraphSpaceID spaceId, meta::cpp2::ListenerType type) override;
/**
* @brief Add a partition as listener
*
* @param spaceId
* @param partId
* @param type Listener type
* @param peers Raft peers of listener
*/
void addListenerPart(GraphSpaceID spaceId,
PartitionID partId,
meta::cpp2::ListenerType type,
const std::vector<HostAddr>& peers) override;
/**
* @brief Remove a listener partition
*
* @param spaceId
* @param partId
* @param type Listener type
*/
void removeListenerPart(GraphSpaceID spaceId,
PartitionID partId,
meta::cpp2::ListenerType type) override;
/**
* @brief Check if the partition's listener state has changed, add/remove if necessary
*
* @param spaceId
* @param partId
* @param remoteListeners The given partition's remote listener list
*/
void checkRemoteListeners(GraphSpaceID spaceId,
PartitionID partId,
const std::vector<HostAddr>& remoteListeners) override;
/**
* @brief Get all partitions grouped by data path and spaceId
*
* @param diskParts Get all space data path and all partition in the path
*/
void fetchDiskParts(SpaceDiskPartsMap& diskParts) override;
/**
* @brief return a WriteBatch object to do batch operation
*
* @return std::unique_ptr<WriteBatch>
*/
std::unique_ptr<WriteBatch> startBatchWrite() override;
/**
* @brief Write batch to local storage engine only
*
* @param spaceId
* @param batch
* @return nebula::cpp2::ErrorCode
*/
nebula::cpp2::ErrorCode batchWriteWithoutReplicator(GraphSpaceID spaceId,
std::unique_ptr<WriteBatch> batch) override;
/**
* @brief Get the kvstore property, only used in rocksdb
*
* @param spaceId
* @param property Property name
* @return ErrorOr<nebula::cpp2::ErrorCode, std::string> Return the property value in string if
* succeed, else return ErrorCode
*/
ErrorOr<nebula::cpp2::ErrorCode, std::string> getProperty(GraphSpaceID spaceId,
const std::string& property) override;
/**
* @brief Register callback when found new partition is added
*
* @param funcName Modulename
* @param func Callback
* @param existParts All existing partitions
*/
void registerOnNewPartAdded(const std::string& funcName,
std::function<void(std::shared_ptr<Part>&)> func,
std::vector<std::pair<GraphSpaceID, PartitionID>>& existParts);
/**
* @brief Unregister a module's callback
*
* @param funcName modulename
*/
void unregisterOnNewPartAdded(const std::string& funcName) {
onNewPartAdded_.erase(funcName);
}
/**
* @brief Register callback to cleanup before a space is removed
*
* @param func Callback to cleanup
*/
void registerBeforeRemoveSpace(std::function<void(GraphSpaceID)> func) {
beforeRemoveSpace_ = func;
}
/**
* @brief Unregister the callback to cleanup before a space is removed
*
*/
void unregisterBeforeRemoveSpace() {
beforeRemoveSpace_ = nullptr;
}
private:
/**
* @brief Load partitions by reading system part keys in kv engine
*/
void loadPartFromDataPath();
/**
* @brief Load partitions from meta
*/
void loadPartFromPartManager();
/**
* @brief Load and start listener of local address
*/
void loadLocalListenerFromPartManager();
/**
* @brief Load and add remote listener into current partition's raft group
*/
void loadRemoteListenerFromPartManager();
/**
* @brief Update space specific options
*
* @param spaceId
* @param options Options map
* @param isDbOption
*/
void updateSpaceOption(GraphSpaceID spaceId,
const std::unordered_map<std::string, std::string>& options,
bool isDbOption) override;
/**
* @brief Start a new kv engine on specified path
*
* @param spaceId
* @param dataPath
* @param walPath
* @return std::unique_ptr<KVEngine>
*/
std::unique_ptr<KVEngine> newEngine(GraphSpaceID spaceId,
const std::string& dataPath,
const std::string& walPath);
/**
* @brief Start a new part
*
* @param spaceId
* @param partId
* @param engine Partition's related kv engine
* @param asLearner Whether start as raft learner
* @param peers All partition raft peers
* @return std::shared_ptr<Part>
*/
std::shared_ptr<Part> newPart(GraphSpaceID spaceId,
PartitionID partId,
KVEngine* engine,
bool asLearner,
const std::vector<HostAddr>& raftPeers);
/**
* @brief Start a new listener part
*
* @param spaceId
* @param partId
* @param type Listener type
* @param peers The raft peer's address
* @return std::shared_ptr<Listener>
*/
std::shared_ptr<Listener> newListener(GraphSpaceID spaceId,
PartitionID partId,
meta::cpp2::ListenerType type,
const std::vector<HostAddr>& peers);
/**
* @brief Get given partition's kv engine
*
* @param spaceId
* @param partId
* @return ErrorOr<nebula::cpp2::ErrorCode, KVEngine*> Return kv engine if succeed, return
* ErrorCode if failed
*/
ErrorOr<nebula::cpp2::ErrorCode, KVEngine*> engine(GraphSpaceID spaceId, PartitionID partId);
/**
* @brief Check if the partition is leader of not
*
* @param part
* @param canReadFromFollower If set to true, will skip the check and return true
* @return True if we regard as leader
*/
bool checkLeader(std::shared_ptr<Part> part, bool canReadFromFollower = false) const;
/**
* @brief clean useless wal
*/
void cleanWAL();
/**
* @brief Get the vertex id length of given space
*
* @param spaceId
* @return int32_t Vertex id length
*/
int32_t getSpaceVidLen(GraphSpaceID spaceId);
/**
* @brief Remove a space's directory
*/
void removeSpaceDir(const std::string& dir);
private:
// The lock used to protect spaces_
folly::RWSpinLock lock_;
std::unordered_map<GraphSpaceID, std::shared_ptr<SpacePartInfo>> spaces_;
std::unordered_map<GraphSpaceID, std::shared_ptr<SpaceListenerInfo>> spaceListeners_;
std::shared_ptr<folly::IOThreadPoolExecutor> ioPool_;
std::shared_ptr<thread::GenericWorker> storeWorker_;
std::shared_ptr<thread::GenericThreadPool> bgWorkers_;
HostAddr storeSvcAddr_;
std::shared_ptr<folly::Executor> workers_;
HostAddr raftAddr_;
KVOptions options_;
std::shared_ptr<raftex::RaftexService> raftService_;
std::shared_ptr<raftex::SnapshotManager> snapshot_;
std::shared_ptr<thrift::ThriftClientManager<raftex::cpp2::RaftexServiceAsyncClient>> clientMan_;
std::shared_ptr<DiskManager> diskMan_;
folly::ConcurrentHashMap<std::string, std::function<void(std::shared_ptr<Part>&)>>
onNewPartAdded_;
std::function<void(GraphSpaceID)> beforeRemoveSpace_{nullptr};
};
} // namespace kvstore
} // namespace nebula
#endif // KVSTORE_NEBULASTORE_H_