Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

copy local id when clone space (#3005) #5781

Merged
merged 2 commits into from
Dec 18, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 35 additions & 0 deletions src/meta/processors/parts/CreateSpaceAsProcessor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,15 @@ void CreateSpaceAsProcessor::process(const cpp2::CreateSpaceAsReq &req) {
return;
}

auto newLocalIdKey = makeLocalIDKey(nebula::value(oldSpaceId), nebula::value(newSpaceId));
if (nebula::ok(newLocalIdKey)) {
data.push_back(nebula::value(newLocalIdKey));
} else {
rc_ = nebula::error(newLocalIdKey);
LOG(INFO) << "Make new local id failed, " << apache::thrift::util::enumNameSafe(rc_);
return;
}

resp_.id_ref() = to(nebula::value(newSpaceId), EntryType::SPACE);
auto timeInMilliSec = time::WallClock::fastNowInMilliSec();
LastUpdateTimeMan::update(data, timeInMilliSec);
Expand Down Expand Up @@ -231,5 +240,31 @@ ErrorOr<nebula::cpp2::ErrorCode, std::vector<kvstore::KV>> CreateSpaceAsProcesso
return data;
}

ErrorOr<nebula::cpp2::ErrorCode, kvstore::KV> CreateSpaceAsProcessor::makeLocalIDKey(
GraphSpaceID oldSpaceId, GraphSpaceID newSpaceId) {
auto localIdkey = MetaKeyUtils::localIdKey(oldSpaceId);
int32_t id;
std::string val;
auto ret = kvstore_->get(kDefaultSpaceId, kDefaultPartId, localIdkey, &val);
if (ret != nebula::cpp2::ErrorCode::SUCCEEDED) {
if (ret != nebula::cpp2::ErrorCode::E_KEY_NOT_FOUND) {
return ret;
}

// In order to be compatible with the existing old schema, and simple to implement,
// when the local_id record does not exist in space, directly use the smallest
// id available globally.
auto globalIdRet = getAvailableGlobalId();
if (!nebula::ok(globalIdRet)) {
return nebula::error(globalIdRet);
}
id = nebula::value(globalIdRet);
} else {
id = *reinterpret_cast<const int32_t *>(val.c_str());
}
auto newLocalIdKey = MetaKeyUtils::localIdKey(newSpaceId);
return {newLocalIdKey, std::string(reinterpret_cast<const char *>(&id), sizeof(id))};
}

} // namespace meta
} // namespace nebula
3 changes: 3 additions & 0 deletions src/meta/processors/parts/CreateSpaceAsProcessor.h
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,9 @@ class CreateSpaceAsProcessor : public BaseProcessor<cpp2::ExecResp> {
ErrorOr<nebula::cpp2::ErrorCode, std::vector<kvstore::KV>> makeNewIndexes(
GraphSpaceID oldSpaceId, GraphSpaceID newSpaceId);

ErrorOr<nebula::cpp2::ErrorCode, kvstore::KV> makeLocalIDKey(GraphSpaceID oldSpaceId,
GraphSpaceID newSpaceId);

nebula::cpp2::ErrorCode rc_{nebula::cpp2::ErrorCode::SUCCEEDED};

private:
Expand Down
18 changes: 18 additions & 0 deletions tests/tck/features/schema/CreateSpaceAs.feature
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,24 @@ Feature: Create space as another space
Then the result should be, in any order:
| src | dst | rank |
| "1" | "2" | 0 |
When executing query:
"""
create tag t2 (col1 int);
"""
Then the execution should be successful
When executing query:
"""
create tag index i3 on t2(col1);
"""
Then the execution should be successful
When executing query:
"""
show tag indexes;
"""
Then the result should be, in any order:
| Index Name | By Tag | Columns |
| "i1" | "t1" | ["col1"] |
| "i3" | "t2" | ["col1"] |
Then drop the used space

Scenario: clone space if not exist
Expand Down
Loading