-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
BaseProcessor.h
277 lines (223 loc) · 8.84 KB
/
BaseProcessor.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
/* Copyright (c) 2018 vesoft inc. All rights reserved.
*
* This source code is licensed under Apache 2.0 License.
*/
#ifndef META_BASEPROCESSOR_H_
#define META_BASEPROCESSOR_H_
#include <folly/SharedMutex.h>
#include <folly/futures/Future.h>
#include <folly/futures/Promise.h>
#include <thrift/lib/cpp/util/EnumUtils.h>
#include "common/base/Base.h"
#include "common/charset/Charset.h"
#include "common/network/NetworkUtils.h"
#include "common/time/Duration.h"
#include "common/utils/MetaKeyUtils.h"
#include "interface/gen-cpp2/storage_types.h"
#include "kvstore/KVStore.h"
#include "meta/ActiveHostsMan.h"
#include "meta/MetaServiceUtils.h"
#include "meta/common/MetaCommon.h"
#include "meta/processors/Common.h"
namespace nebula {
namespace meta {
using nebula::network::NetworkUtils;
using FieldType = std::pair<std::string, nebula::cpp2::PropertyType>;
using SignType = storage::cpp2::EngineSignType;
#define CHECK_SPACE_ID_AND_RETURN(spaceID) \
auto retSpace = spaceExist(spaceID); \
if (retSpace != nebula::cpp2::ErrorCode::SUCCEEDED) { \
handleErrorCode(retSpace); \
onFinished(); \
return; \
}
/**
* Check segment is consist of numbers and letters and should not empty.
* */
#define CHECK_SEGMENT(segment) \
if (!MetaCommon::checkSegment(segment)) { \
handleErrorCode(nebula::cpp2::ErrorCode::E_STORE_SEGMENT_ILLEGAL); \
onFinished(); \
return; \
}
template <typename RESP>
class BaseProcessor {
public:
explicit BaseProcessor(kvstore::KVStore* kvstore) : kvstore_(kvstore) {}
virtual ~BaseProcessor() = default;
folly::Future<RESP> getFuture() { return promise_.getFuture(); }
protected:
/**
* Destroy current instance when finished.
* */
virtual void onFinished() {
promise_.setValue(std::move(resp_));
delete this;
}
void handleErrorCode(nebula::cpp2::ErrorCode code,
GraphSpaceID spaceId = kDefaultSpaceId,
PartitionID partId = kDefaultPartId) {
resp_.set_code(code);
if (code == nebula::cpp2::ErrorCode::E_LEADER_CHANGED) {
handleLeaderChanged(spaceId, partId);
}
}
void handleLeaderChanged(GraphSpaceID spaceId, PartitionID partId) {
auto leaderRet = kvstore_->partLeader(spaceId, partId);
if (ok(leaderRet)) {
resp_.set_leader(toThriftHost(nebula::value(leaderRet)));
} else {
resp_.set_code(nebula::error(leaderRet));
}
}
template <class T>
std::enable_if_t<std::is_integral<T>::value, cpp2::ID> to(T id, EntryType type) {
cpp2::ID thriftID;
switch (type) {
case EntryType::SPACE:
thriftID.set_space_id(static_cast<GraphSpaceID>(id));
break;
case EntryType::TAG:
thriftID.set_tag_id(static_cast<TagID>(id));
break;
case EntryType::EDGE:
thriftID.set_edge_type(static_cast<EdgeType>(id));
break;
case EntryType::INDEX:
thriftID.set_index_id(static_cast<IndexID>(id));
break;
case EntryType::CONFIG:
case EntryType::GROUP:
case EntryType::ZONE:
break;
}
return thriftID;
}
HostAddr toThriftHost(const HostAddr& host) { return host; }
/**
* General put function.
* */
void doPut(std::vector<kvstore::KV> data);
ErrorOr<nebula::cpp2::ErrorCode, std::unique_ptr<kvstore::KVIterator>> doPrefix(
const std::string& key);
/**
* General get function.
* */
ErrorOr<nebula::cpp2::ErrorCode, std::string> doGet(const std::string& key);
/**
* General multi get function.
* */
ErrorOr<nebula::cpp2::ErrorCode, std::vector<std::string>> doMultiGet(
const std::vector<std::string>& keys);
/**
* General remove function.
* */
void doRemove(const std::string& key);
/**
* Remove keys from start to end, doesn't contain end.
* */
void doRemoveRange(const std::string& start, const std::string& end);
/**
* Scan keys from start to end, doesn't contain end.
* */
ErrorOr<nebula::cpp2::ErrorCode, std::vector<std::string>> doScan(const std::string& start,
const std::string& end);
/**
* General multi remove function.
**/
void doMultiRemove(std::vector<std::string> keys);
/**
* Get all hosts
* */
ErrorOr<nebula::cpp2::ErrorCode, std::vector<HostAddr>> allHosts();
/**
* Get one auto-increment Id.
* */
ErrorOr<nebula::cpp2::ErrorCode, int32_t> autoIncrementId();
/**
* Get the current available global id
**/
ErrorOr<nebula::cpp2::ErrorCode, int32_t> getAvailableGlobalId();
/**
* Get one auto-increment Id in spaceId.
* */
ErrorOr<nebula::cpp2::ErrorCode, int32_t> autoIncrementIdInSpace(GraphSpaceID spaceId);
/**
* Check spaceId exist or not.
* */
nebula::cpp2::ErrorCode spaceExist(GraphSpaceID spaceId);
/**
* Check user exist or not.
**/
nebula::cpp2::ErrorCode userExist(const std::string& account);
/**
* Check machine has been registered or not.
* */
nebula::cpp2::ErrorCode machineExist(const std::string& machineKey);
/**
* Check hosts has been include by zone or not.
* */
nebula::cpp2::ErrorCode includeByZone(const std::vector<HostAddr>& hosts);
/**
* Return the spaceId for name.
* */
ErrorOr<nebula::cpp2::ErrorCode, GraphSpaceID> getSpaceId(const std::string& name);
/**
* Return the tagId for name.
*/
ErrorOr<nebula::cpp2::ErrorCode, TagID> getTagId(GraphSpaceID spaceId, const std::string& name);
/**
* Fetch the latest version tag's schema.
*/
ErrorOr<nebula::cpp2::ErrorCode, cpp2::Schema> getLatestTagSchema(GraphSpaceID spaceId,
const TagID tagId);
/**
* Return the edgeType for name.
*/
ErrorOr<nebula::cpp2::ErrorCode, EdgeType> getEdgeType(GraphSpaceID spaceId,
const std::string& name);
/**
* Fetch the latest version edge's schema.
*/
ErrorOr<nebula::cpp2::ErrorCode, cpp2::Schema> getLatestEdgeSchema(GraphSpaceID spaceId,
const EdgeType edgeType);
ErrorOr<nebula::cpp2::ErrorCode, IndexID> getIndexID(GraphSpaceID spaceId,
const std::string& indexName);
ErrorOr<nebula::cpp2::ErrorCode, bool> checkPassword(const std::string& account,
const std::string& password);
nebula::cpp2::ErrorCode doSyncPut(std::vector<kvstore::KV> data);
void doSyncPutAndUpdate(std::vector<kvstore::KV> data);
void doSyncMultiRemoveAndUpdate(std::vector<std::string> keys);
/**
* Check the edge or tag contains indexes when alter it.
**/
nebula::cpp2::ErrorCode indexCheck(const std::vector<cpp2::IndexItem>& items,
const std::vector<cpp2::AlterSchemaItem>& alterItems);
nebula::cpp2::ErrorCode ftIndexCheck(const std::vector<std::string>& cols,
const std::vector<cpp2::AlterSchemaItem>& alterItems);
ErrorOr<nebula::cpp2::ErrorCode, std::vector<cpp2::IndexItem>> getIndexes(GraphSpaceID spaceId,
int32_t tagOrEdge);
ErrorOr<nebula::cpp2::ErrorCode, cpp2::FTIndex> getFTIndex(GraphSpaceID spaceId,
int32_t tagOrEdge);
bool checkIndexExist(const std::vector<cpp2::IndexFieldDef>& fields, const cpp2::IndexItem& item);
ErrorOr<nebula::cpp2::ErrorCode, ZoneID> getZoneId(const std::string& zoneName);
nebula::cpp2::ErrorCode listenerExist(GraphSpaceID space, cpp2::ListenerType type);
// A direct value of true means that data will not be written to follow via
// the raft protocol, but will be written directly to local disk
ErrorOr<nebula::cpp2::ErrorCode, bool> replaceHostInPartition(const HostAddr& ipv4From,
const HostAddr& ipv4To,
bool direct = false);
ErrorOr<nebula::cpp2::ErrorCode, bool> replaceHostInZone(const HostAddr& ipv4From,
const HostAddr& ipv4To,
bool direct = false);
protected:
kvstore::KVStore* kvstore_ = nullptr;
RESP resp_;
folly::Promise<RESP> promise_;
time::Duration duration_;
static const int32_t maxIndexLimit = 16;
};
} // namespace meta
} // namespace nebula
#include "meta/processors/BaseProcessor-inl.h"
#endif // META_BASEPROCESSOR_H_