Skip to content
This repository has been archived by the owner on Dec 1, 2022. It is now read-only.

Improve clusterIdsToHosts. #468

Merged
merged 5 commits into from
Mar 26, 2021
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
36 changes: 12 additions & 24 deletions src/common/clients/storage/GraphStorageClient.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -318,15 +318,11 @@ GraphStorageClient::updateVertex(GraphSpaceID space,
}

auto part = status.value();
auto hStatus = getPartHosts(space, part);
if (!hStatus.ok()) {
return folly::makeFuture<StatusOr<storage::cpp2::UpdateResponse>>(hStatus.status());
auto host = this->getLeader(space, part);
if (!host.ok()) {
return folly::makeFuture<StatusOr<storage::cpp2::UpdateResponse>>(host.status());
}

auto partHosts = hStatus.value();
CHECK_GT(partHosts.hosts_.size(), 0U);
const auto& host = this->getLeader(partHosts);
request.first = std::move(host);
request.first = std::move(host).value();
cpp2::UpdateVertexRequest req;
req.set_space_id(space);
req.set_vertex_id(vertexId);
Expand Down Expand Up @@ -376,15 +372,11 @@ GraphStorageClient::updateEdge(GraphSpaceID space,
}

auto part = status.value();
auto hStatus = getPartHosts(space, part);
if (!hStatus.ok()) {
return folly::makeFuture<StatusOr<storage::cpp2::UpdateResponse>>(hStatus.status());
auto host = this->getLeader(space, part);
if (!host.ok()) {
return folly::makeFuture<StatusOr<storage::cpp2::UpdateResponse>>(host.status());
}
auto partHosts = hStatus.value();
CHECK_GT(partHosts.hosts_.size(), 0U);

const auto& host = this->getLeader(partHosts);
request.first = std::move(host);
request.first = std::move(host).value();
cpp2::UpdateEdgeRequest req;
req.set_space_id(space);
req.set_edge_key(edgeKey);
Expand Down Expand Up @@ -423,15 +415,11 @@ GraphStorageClient::getUUID(GraphSpaceID space,
}

auto part = status.value();
auto hStatus = getPartHosts(space, part);
if (!hStatus.ok()) {
return folly::makeFuture<StatusOr<cpp2::GetUUIDResp>>(hStatus.status());
auto host = this->getLeader(space, part);
if (!host.ok()) {
return folly::makeFuture<StatusOr<storage::cpp2::GetUUIDResp>>(host.status());
}
auto partHosts = hStatus.value();
CHECK_GT(partHosts.hosts_.size(), 0U);

const auto& leader = this->getLeader(partHosts);
request.first = leader;
request.first = std::move(host).value();
cpp2::GetUUIDReq req;
req.set_space_id(space);
req.set_part_id(part);
Expand Down
6 changes: 1 addition & 5 deletions src/common/clients/storage/InternalStorageClient.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,11 +38,7 @@ cpp2::ErrorCode extractErrorCode(T& tryResp) {

StatusOr<HostAddr> InternalStorageClient::getFuzzyLeader(GraphSpaceID spaceId,
PartitionID partId) const {
auto stPartHosts = getPartHosts(spaceId, partId);
if (!stPartHosts.ok()) {
NG_LOG_AND_RETURN_IF_ERROR(stPartHosts.status());
}
return getLeader(stPartHosts.value());
return getLeader(spaceId, partId);
}

folly::SemiFuture<cpp2::ErrorCode> InternalStorageClient::forwardTransaction(
Expand Down
2 changes: 1 addition & 1 deletion src/common/clients/storage/StorageClientBase.h
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ class StorageClientBase {
virtual ~StorageClientBase();

virtual void loadLeader() const;
const HostAddr getLeader(const meta::PartHosts& partHosts) const;
StatusOr<HostAddr> getLeader(GraphSpaceID spaceId, PartitionID partId) const;
void updateLeader(GraphSpaceID spaceId, PartitionID partId, const HostAddr& leader);
void invalidLeader(GraphSpaceID spaceId, PartitionID partId);
void invalidLeader(GraphSpaceID spaceId, std::vector<PartitionID> &partsId);
Expand Down
41 changes: 22 additions & 19 deletions src/common/clients/storage/StorageClientBase.inl
Original file line number Diff line number Diff line change
Expand Up @@ -112,10 +112,10 @@ void StorageClientBase<ClientType>::loadLeader() const {


template<typename ClientType>
const HostAddr
StorageClientBase<ClientType>::getLeader(const meta::PartHosts& partHosts) const {
StatusOr<HostAddr>
StorageClientBase<ClientType>::getLeader(GraphSpaceID spaceId, PartitionID partId) const {
loadLeader();
auto part = std::make_pair(partHosts.spaceId_, partHosts.partId_);
auto part = std::make_pair(spaceId, partId);
{
folly::RWSpinLock::ReadHolder rh(leadersLock_);
auto it = leaders_.find(part);
Expand All @@ -124,6 +124,12 @@ StorageClientBase<ClientType>::getLeader(const meta::PartHosts& partHosts) const
}
}
{
auto metaStatus = getPartHosts(spaceId, partId);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe just getPartHosts and call getLeader(const meta::PartHosts& partHosts)? Most of the logic is same as that method.

if (!metaStatus.ok()) {
return metaStatus.status();
}
auto partHosts = metaStatus.value();

folly::RWSpinLock::WriteHolder wh(leadersLock_);
VLOG(1) << "No leader exists. Choose one in round-robin.";
auto index = (leaderIndex_[part] + 1) % partHosts.hosts_.size();
Expand All @@ -134,7 +140,6 @@ StorageClientBase<ClientType>::getLeader(const meta::PartHosts& partHosts) const
}
}


template<typename ClientType>
void StorageClientBase<ClientType>::updateLeader(GraphSpaceID spaceId,
PartitionID partId,
Expand Down Expand Up @@ -365,6 +370,14 @@ StorageClientBase<ClientType>::clusterIdsToHosts(GraphSpaceID spaceId,
return Status::Error("Space not found, spaceid: %d", spaceId);
}
auto numParts = status.value();
std::unordered_map<PartitionID, HostAddr> leaders;
for (int32_t partId = 1; partId <= numParts; ++partId) {
auto leader = getLeader(spaceId, partId);
if (!leader.ok()) {
return leader.status();
}
leaders[partId] = std::move(leader).value();
}
for (auto& id : ids) {
CHECK(!!metaClient_);
status = metaClient_->partId(numParts, f(id));
Expand All @@ -373,14 +386,7 @@ StorageClientBase<ClientType>::clusterIdsToHosts(GraphSpaceID spaceId,
}

auto part = status.value();
auto metaStatus = getPartHosts(spaceId, part);
if (!metaStatus.ok()) {
return status.status();
}

auto partHosts = metaStatus.value();
CHECK_GT(partHosts.hosts_.size(), 0U);
const auto leader = this->getLeader(partHosts);
const auto& leader = leaders[part];
clusters[leader][part].emplace_back(std::move(id));
}
return clusters;
Expand All @@ -398,14 +404,11 @@ StorageClientBase<ClientType>::getHostParts(GraphSpaceID spaceId) const {

auto parts = status.value();
for (auto partId = 1; partId <= parts; partId++) {
auto metaStatus = getPartHosts(spaceId, partId);
if (!metaStatus.ok()) {
return metaStatus.status();
auto leader = getLeader(spaceId, partId);
if (!leader.ok()) {
return leader.status();
}
auto partHosts = std::move(metaStatus).value();
DCHECK_GT(partHosts.hosts_.size(), 0U);
const auto leader = getLeader(partHosts);
hostParts[leader].emplace_back(partId);
hostParts[leader.value()].emplace_back(partId);
}
return hostParts;
}
Expand Down