|
9 | 9 |
|
10 | 10 | #include "common/base/Base.h"
|
11 | 11 | #include "common/fs/TempDir.h"
|
| 12 | +#include "common/utils/IndexKeyUtils.h" |
| 13 | +#include "common/utils/NebulaKeyUtils.h" |
| 14 | +#include "common/utils/OperationKeyUtils.h" |
12 | 15 | #include "kvstore/Part.h"
|
| 16 | +#include "kvstore/RocksEngine.h" |
13 | 17 |
|
14 | 18 | namespace nebula {
|
15 | 19 | namespace kvstore {
|
16 | 20 |
|
| 21 | +const int32_t kDefaultVIdLen = 8; |
| 22 | + |
| 23 | +void checkVertexData(RocksEngine* engine, |
| 24 | + PartitionID partId, |
| 25 | + int expectNum, |
| 26 | + bool checkVal = false) { |
| 27 | + std::string vertexPrefix = NebulaKeyUtils::vertexPrefix(partId); |
| 28 | + std::unique_ptr<KVIterator> iter; |
| 29 | + auto code = engine->prefix(vertexPrefix, &iter); |
| 30 | + ASSERT_EQ(nebula::cpp2::ErrorCode::SUCCEEDED, code); |
| 31 | + int32_t num = 0; |
| 32 | + while (iter->valid()) { |
| 33 | + num++; |
| 34 | + if (checkVal) { |
| 35 | + ASSERT_EQ(iter->val().str(), folly::stringPrintf("val%d", num)); |
| 36 | + } |
| 37 | + iter->next(); |
| 38 | + } |
| 39 | + ASSERT_EQ(num, expectNum); |
| 40 | +} |
| 41 | + |
| 42 | +void checkEdgeData(RocksEngine* engine, PartitionID partId, int expectNum) { |
| 43 | + std::string edgePrefix = NebulaKeyUtils::edgePrefix(partId); |
| 44 | + std::unique_ptr<KVIterator> iter; |
| 45 | + auto code = engine->prefix(edgePrefix, &iter); |
| 46 | + ASSERT_EQ(nebula::cpp2::ErrorCode::SUCCEEDED, code); |
| 47 | + int32_t num = 0; |
| 48 | + while (iter->valid()) { |
| 49 | + num++; |
| 50 | + iter->next(); |
| 51 | + } |
| 52 | + ASSERT_EQ(num, expectNum); |
| 53 | +} |
| 54 | + |
| 55 | +void checkIndexData(RocksEngine* engine, PartitionID partId, int expectNum) { |
| 56 | + std::string indexPrefix = IndexKeyUtils::indexPrefix(partId); |
| 57 | + std::unique_ptr<KVIterator> iter; |
| 58 | + auto code = engine->prefix(indexPrefix, &iter); |
| 59 | + ASSERT_EQ(nebula::cpp2::ErrorCode::SUCCEEDED, code); |
| 60 | + int32_t num = 0; |
| 61 | + while (iter->valid()) { |
| 62 | + num++; |
| 63 | + iter->next(); |
| 64 | + } |
| 65 | + ASSERT_EQ(num, expectNum); |
| 66 | +} |
| 67 | + |
17 | 68 | TEST(PartTest, RocksTest) {
|
18 | 69 | fs::TempDir dataPath("/tmp/rocksdb_test.XXXXXX");
|
19 | 70 | rocksdb::Options options;
|
@@ -46,6 +97,158 @@ TEST(PartTest, RocksTest) {
|
46 | 97 | delete db;
|
47 | 98 | }
|
48 | 99 |
|
| 100 | +TEST(PartTest, KeyOrderTest) { |
| 101 | + fs::TempDir dataPath("/tmp/KeyOrderTest.XXXXXX"); |
| 102 | + auto engine = std::make_unique<RocksEngine>(0, kDefaultVIdLen, dataPath.path()); |
| 103 | + |
| 104 | + std::vector<KV> data; |
| 105 | + PartitionID partId = 1; |
| 106 | + |
| 107 | + // build vertex data in part 1, 2 |
| 108 | + while (partId < 3) { |
| 109 | + auto key1 = NebulaKeyUtils::vertexKey(kDefaultVIdLen, partId, "", 0); |
| 110 | + data.emplace_back(key1, folly::stringPrintf("val%d", 1)); |
| 111 | + |
| 112 | + auto key2 = NebulaKeyUtils::vertexKey(kDefaultVIdLen, partId, "", INT_MAX); |
| 113 | + data.emplace_back(key2, folly::stringPrintf("val%d", 2)); |
| 114 | + |
| 115 | + auto key3 = NebulaKeyUtils::vertexKey(kDefaultVIdLen, partId, "ffffff", INT_MAX, '\377'); |
| 116 | + data.emplace_back(key3, folly::stringPrintf("val%d", 3)); |
| 117 | + |
| 118 | + auto key4 = NebulaKeyUtils::vertexKey(kDefaultVIdLen, partId, "", INT_MAX, '\377'); |
| 119 | + data.emplace_back(key4, folly::stringPrintf("val%d", 4)); |
| 120 | + |
| 121 | + ASSERT_EQ(nebula::cpp2::ErrorCode::SUCCEEDED, engine->multiPut(data)); |
| 122 | + data.clear(); |
| 123 | + partId++; |
| 124 | + } |
| 125 | + { |
| 126 | + partId = 1; |
| 127 | + while (partId < 3) { |
| 128 | + checkVertexData(engine.get(), partId, 4, true); |
| 129 | + partId++; |
| 130 | + } |
| 131 | + } |
| 132 | +} |
| 133 | + |
| 134 | +TEST(PartTest, PartCleanTest) { |
| 135 | + fs::TempDir dataPath("/tmp/PartCleanTest.XXXXXX"); |
| 136 | + auto engine = std::make_unique<RocksEngine>(0, kDefaultVIdLen, dataPath.path()); |
| 137 | + |
| 138 | + std::vector<KV> data; |
| 139 | + PartitionID partId = 1; |
| 140 | + |
| 141 | + // build vertex data in part 1, 2 |
| 142 | + while (partId < 3) { |
| 143 | + TagID tagId = 1; |
| 144 | + for (int i = 0; i < 10; i++) { |
| 145 | + auto key = NebulaKeyUtils::vertexKey(kDefaultVIdLen, partId, std::to_string(i), tagId); |
| 146 | + data.emplace_back(key, folly::stringPrintf("val%d", i)); |
| 147 | + } |
| 148 | + tagId = 2; |
| 149 | + for (int i = 0; i < 10; i++) { |
| 150 | + auto key = NebulaKeyUtils::vertexKey(kDefaultVIdLen, partId, std::to_string(i), tagId); |
| 151 | + data.emplace_back(key, folly::stringPrintf("val%d", i)); |
| 152 | + } |
| 153 | + |
| 154 | + EdgeType edgetype = 3; |
| 155 | + for (int i = 0; i < 10; i++) { |
| 156 | + auto key = NebulaKeyUtils::edgeKey( |
| 157 | + kDefaultVIdLen, partId, std::to_string(i), edgetype, 0, std::to_string(i)); |
| 158 | + data.emplace_back(key, folly::stringPrintf("val%d", i)); |
| 159 | + } |
| 160 | + IndexID indexId = 5; |
| 161 | + for (int i = 0; i < 10; i++) { |
| 162 | + auto key = |
| 163 | + IndexKeyUtils::vertexIndexKey(kDefaultVIdLen, partId, indexId, std::to_string(i), "123"); |
| 164 | + data.emplace_back(key, folly::stringPrintf("val%d", i)); |
| 165 | + } |
| 166 | + |
| 167 | + data.emplace_back(NebulaKeyUtils::systemCommitKey(partId), "123"); |
| 168 | + data.emplace_back(NebulaKeyUtils::systemPartKey(partId), ""); |
| 169 | + ASSERT_EQ(nebula::cpp2::ErrorCode::SUCCEEDED, engine->multiPut(data)); |
| 170 | + data.clear(); |
| 171 | + partId++; |
| 172 | + } |
| 173 | + |
| 174 | + { |
| 175 | + partId = 1; |
| 176 | + while (partId < 2) { |
| 177 | + checkVertexData(engine.get(), partId, 20); |
| 178 | + checkEdgeData(engine.get(), partId, 10); |
| 179 | + checkIndexData(engine.get(), partId, 10); |
| 180 | + { |
| 181 | + std::string val1; |
| 182 | + auto code1 = engine->get(NebulaKeyUtils::systemCommitKey(partId), &val1); |
| 183 | + ASSERT_EQ(nebula::cpp2::ErrorCode::SUCCEEDED, code1); |
| 184 | + ASSERT_EQ("123", val1); |
| 185 | + |
| 186 | + std::string val2; |
| 187 | + auto code2 = engine->get(NebulaKeyUtils::systemPartKey(partId), &val2); |
| 188 | + ASSERT_EQ(nebula::cpp2::ErrorCode::SUCCEEDED, code2); |
| 189 | + } |
| 190 | + |
| 191 | + partId++; |
| 192 | + } |
| 193 | + |
| 194 | + { |
| 195 | + // remove range part::clean data |
| 196 | + partId = 1; |
| 197 | + |
| 198 | + const auto& vertexPre = NebulaKeyUtils::vertexPrefix(partId); |
| 199 | + auto ret = engine->removeRange(NebulaKeyUtils::firstKey(vertexPre, kDefaultVIdLen), |
| 200 | + NebulaKeyUtils::lastKey(vertexPre, kDefaultVIdLen)); |
| 201 | + ASSERT_EQ(nebula::cpp2::ErrorCode::SUCCEEDED, ret); |
| 202 | + |
| 203 | + const auto& edgePre = NebulaKeyUtils::edgePrefix(partId); |
| 204 | + ret = engine->removeRange(NebulaKeyUtils::firstKey(edgePre, kDefaultVIdLen), |
| 205 | + NebulaKeyUtils::lastKey(edgePre, kDefaultVIdLen)); |
| 206 | + ASSERT_EQ(nebula::cpp2::ErrorCode::SUCCEEDED, ret); |
| 207 | + |
| 208 | + const auto& indexPre = IndexKeyUtils::indexPrefix(partId); |
| 209 | + ret = engine->removeRange(NebulaKeyUtils::firstKey(indexPre, sizeof(IndexID)), |
| 210 | + NebulaKeyUtils::lastKey(indexPre, sizeof(IndexID))); |
| 211 | + ASSERT_EQ(nebula::cpp2::ErrorCode::SUCCEEDED, ret); |
| 212 | + |
| 213 | + const auto& operationPre = OperationKeyUtils::operationPrefix(partId); |
| 214 | + ret = engine->removeRange(NebulaKeyUtils::firstKey(operationPre, sizeof(int64_t)), |
| 215 | + NebulaKeyUtils::lastKey(operationPre, sizeof(int64_t))); |
| 216 | + ASSERT_EQ(nebula::cpp2::ErrorCode::SUCCEEDED, ret); |
| 217 | + |
| 218 | + ret = engine->remove(NebulaKeyUtils::systemCommitKey(partId)); |
| 219 | + ASSERT_EQ(nebula::cpp2::ErrorCode::SUCCEEDED, ret); |
| 220 | + } |
| 221 | + |
| 222 | + { |
| 223 | + // check data again |
| 224 | + partId = 1; |
| 225 | + checkVertexData(engine.get(), partId, 0); |
| 226 | + checkEdgeData(engine.get(), partId, 0); |
| 227 | + checkIndexData(engine.get(), partId, 0); |
| 228 | + std::string val1; |
| 229 | + auto code1 = engine->get(NebulaKeyUtils::systemCommitKey(partId), &val1); |
| 230 | + ASSERT_NE(nebula::cpp2::ErrorCode::SUCCEEDED, code1); |
| 231 | + |
| 232 | + std::string val2; |
| 233 | + auto code2 = engine->get(NebulaKeyUtils::systemPartKey(partId), &val2); |
| 234 | + ASSERT_EQ(nebula::cpp2::ErrorCode::SUCCEEDED, code2); |
| 235 | + } |
| 236 | + { |
| 237 | + partId = 2; |
| 238 | + checkVertexData(engine.get(), partId, 20); |
| 239 | + checkEdgeData(engine.get(), partId, 10); |
| 240 | + checkIndexData(engine.get(), partId, 10); |
| 241 | + std::string val1; |
| 242 | + auto code1 = engine->get(NebulaKeyUtils::systemCommitKey(partId), &val1); |
| 243 | + ASSERT_EQ(nebula::cpp2::ErrorCode::SUCCEEDED, code1); |
| 244 | + |
| 245 | + std::string val2; |
| 246 | + auto code2 = engine->get(NebulaKeyUtils::systemPartKey(partId), &val2); |
| 247 | + ASSERT_EQ(nebula::cpp2::ErrorCode::SUCCEEDED, code2); |
| 248 | + } |
| 249 | + } |
| 250 | +} |
| 251 | + |
49 | 252 | } // namespace kvstore
|
50 | 253 | } // namespace nebula
|
51 | 254 |
|
|
0 commit comments