Skip to content

Commit

Permalink
Move to utils
Browse files Browse the repository at this point in the history
  • Loading branch information
yixinglu committed Nov 17, 2022
1 parent 141135a commit ed9937a
Show file tree
Hide file tree
Showing 11 changed files with 70 additions and 69 deletions.
9 changes: 0 additions & 9 deletions src/graph/executor/StorageAccessExecutor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -148,14 +148,5 @@ StatusOr<std::vector<Value>> StorageAccessExecutor::buildRequestListByVidType(It
return internal::buildRequestList<std::string>(space, exprCtx, iter, expr, dedup, isCypher);
}

folly::dynamic StorageAccessExecutor::getStorageDetail(
const std::map<std::string, int32_t> &profileDetail) const {
folly::dynamic profileData = folly::dynamic::object();
for (auto &p : profileDetail) {
profileData.insert(p.first, folly::sformat("{}(us)", p.second));
}
return profileData;
}

} // namespace graph
} // namespace nebula
25 changes: 2 additions & 23 deletions src/graph/executor/StorageAccessExecutor.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

#include "clients/storage/StorageClientBase.h"
#include "graph/executor/Executor.h"
#include "graph/util/Utils.h"

namespace nebula {

Expand Down Expand Up @@ -139,38 +140,16 @@ class StorageAccessExecutor : public Executor {
return Status::OK();
}

folly::dynamic collectRespProfileData(const storage::cpp2::ResponseCommon &resp,
const std::tuple<HostAddr, int32_t, int32_t> &info,
size_t numVertices = 0UL,
size_t totalRpcTime = 0UL) const {
folly::dynamic stat = folly::dynamic::object();
stat.insert("address", std::get<0>(info).toString());
stat.insert("exec", folly::sformat("{}(us)", std::get<1>(info)));
stat.insert("total", folly::sformat("{}(us)", std::get<2>(info)));
if (numVertices > 0) {
stat.insert("vertices", numVertices);
}
if (totalRpcTime > 0) {
stat.insert("total_rpc_time", folly::sformat("{}(us)", totalRpcTime));
}
if (resp.latency_detail_us_ref().has_value()) {
stat.insert("storage_detail", getStorageDetail(*resp.get_latency_detail_us()));
}
return stat;
}

template <typename RESP>
void addStats(storage::StorageRpcResponse<RESP> &resp,
std::unordered_map<std::string, std::string> &stats) const {
auto &hostLatency = resp.hostLatency();
for (size_t i = 0; i < hostLatency.size(); ++i) {
auto info = collectRespProfileData(resp.responses()[i].get_result(), hostLatency[i]);
auto info = util::collectRespProfileData(resp.responses()[i].get_result(), hostLatency[i]);
stats.emplace(folly::sformat("resp[{}]", i), folly::toPrettyJson(info));
}
}

folly::dynamic getStorageDetail(const std::map<std::string, int32_t> &profileDetail) const;

bool isIntVidType(const SpaceInfo &space) const;

StatusOr<DataSet> buildRequestDataSetByVidType(Iterator *iter,
Expand Down
35 changes: 5 additions & 30 deletions src/graph/executor/algo/ShortestPathBase.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
#include "graph/util/Utils.h"

using apache::thrift::optional_field_ref;
using nebula::graph::util::collectRespProfileData;
using nebula::storage::StorageClient;

namespace nebula {
Expand Down Expand Up @@ -70,15 +71,6 @@ std::vector<Value> ShortestPathBase::handlePropResp(PropRpcResponse&& resps) {
return vertices;
}

folly::dynamic ShortestPathBase::getStorageDetail(
const std::map<std::string, int32_t>& profileDetail) const {
folly::dynamic info = folly::dynamic::object();
for (auto& p : profileDetail) {
info.insert(p.first, folly::sformat("{}(us)", p.second));
}
return info;
}

Status ShortestPathBase::handleErrorCode(nebula::cpp2::ErrorCode code, PartitionID partId) const {
switch (code) {
case nebula::cpp2::ErrorCode::E_KEY_NOT_FOUND:
Expand Down Expand Up @@ -177,17 +169,8 @@ void ShortestPathBase::addStats(RpcResponse& resp,
if (result.vertices_ref().has_value()) {
size = (*result.vertices_ref()).size();
}
auto& info = hostLatency[i];
folly::dynamic stat = folly::dynamic::object();
stat.insert("address", std::get<0>(info).toString());
stat.insert("exec", folly::sformat("{}(us)", std::get<1>(info)));
stat.insert("total", folly::sformat("{}(us)", std::get<2>(info)));
stat.insert("vertices", size);
stat.insert("total_rpc_time", folly::sformat("{}(us)", timeInUSec));
if (result.result.latency_detail_us_ref().has_value()) {
stat.insert("storage_detail", getStorageDetail(*result.result.get_latency_detail_us()));
}
stats.push_back(folly::dynamic::object(folly::sformat("resp[{}]", i), stat));
auto info = util::collectRespProfileData(result.result, hostLatency[i], size, timeInUSec);
stats.push_back(std::move(info));
}

auto key = folly::sformat("{}step[{}]", reverse ? "reverse " : "", stepNum);
Expand All @@ -200,17 +183,9 @@ void ShortestPathBase::addStats(PropRpcResponse& resp, int64_t timeInUSec) const
folly::dynamic stats = folly::dynamic::array();
auto& hostLatency = resp.hostLatency();
for (size_t i = 0; i < hostLatency.size(); ++i) {
auto& info = hostLatency[i];
folly::dynamic stat = folly::dynamic::object();
stat.insert("address", std::get<0>(info).toString());
stat.insert("exec", folly::sformat("{}(us)", std::get<1>(info)));
stat.insert("total", folly::sformat("{}(us)", std::get<2>(info)));
stat.insert("total_rpc_time", folly::sformat("{}(us)", timeInUSec));
const auto& result = resp.responses()[i].get_result();
if (result.latency_detail_us_ref().has_value()) {
stat.insert("storage_detail", getStorageDetail(*result.get_latency_detail_us()));
}
stats.push_back(std::move(stat));
auto info = util::collectRespProfileData(result, hostLatency[i], 0, timeInUSec);
stats.push_back(std::move(info));
}

statsLock_.lock();
Expand Down
2 changes: 0 additions & 2 deletions src/graph/executor/algo/ShortestPathBase.h
Original file line number Diff line number Diff line change
Expand Up @@ -74,8 +74,6 @@ class ShortestPathBase {
return Result::State::kSuccess;
}

folly::dynamic getStorageDetail(const std::map<std::string, int32_t>& profileDetail) const;

protected:
const ShortestPath* pathNode_{nullptr};
QueryContext* qctx_{nullptr};
Expand Down
3 changes: 2 additions & 1 deletion src/graph/executor/algo/SubgraphExecutor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#include "graph/executor/algo/SubgraphExecutor.h"

#include "graph/service/GraphFlags.h"
#include "graph/util/Utils.h"

using nebula::storage::StorageClient;
namespace nebula {
Expand Down Expand Up @@ -59,7 +60,7 @@ folly::Future<Status> SubgraphExecutor::getNeighbors() {
if (result.vertices_ref().has_value()) {
size = (*result.vertices_ref()).size();
}
auto info = collectRespProfileData(result.result, hostLatency[i], size);
auto info = util::collectRespProfileData(result.result, hostLatency[i], size);
otherStats_.emplace(folly::sformat("resp[{}]", i), folly::toPrettyJson(info));
}
vids_.clear();
Expand Down
3 changes: 2 additions & 1 deletion src/graph/executor/query/GetDstBySrcExecutor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#include "graph/executor/query/GetDstBySrcExecutor.h"

#include "graph/service/GraphFlags.h"
#include "graph/util/Utils.h"

using nebula::storage::StorageClient;
using nebula::storage::StorageRpcResponse;
Expand Down Expand Up @@ -55,7 +56,7 @@ folly::Future<Status> GetDstBySrcExecutor::execute() {
if (result.dsts_ref().has_value()) {
size = (*result.dsts_ref()).size();
}
auto info = collectRespProfileData(result.result, hostLatency[i], size);
auto info = util::collectRespProfileData(result.result, hostLatency[i], size);
otherStats_.emplace(folly::sformat("resp[{}]", i), folly::toPrettyJson(info));
}
return handleResponse(resp, this->gd_->colNames());
Expand Down
3 changes: 2 additions & 1 deletion src/graph/executor/query/GetNeighborsExecutor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#include "graph/executor/query/GetNeighborsExecutor.h"

#include "graph/service/GraphFlags.h"
#include "graph/util/Utils.h"

using nebula::storage::StorageClient;
using nebula::storage::StorageRpcResponse;
Expand Down Expand Up @@ -69,7 +70,7 @@ folly::Future<Status> GetNeighborsExecutor::execute() {
if (result.vertices_ref().has_value()) {
size = (*result.vertices_ref()).size();
}
auto info = collectRespProfileData(result.result, hostLatency[i], size);
auto info = util::collectRespProfileData(result.result, hostLatency[i], size);
otherStats_.emplace(folly::sformat("resp[{}]", i), folly::toPrettyJson(info));
}
return handleResponse(resp);
Expand Down
5 changes: 3 additions & 2 deletions src/graph/executor/query/TraverseExecutor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
#include "clients/storage/StorageClient.h"
#include "graph/service/GraphFlags.h"
#include "graph/util/SchemaUtil.h"
#include "graph/util/Utils.h"

using nebula::storage::StorageClient;
using nebula::storage::StorageRpcResponse;
Expand Down Expand Up @@ -128,8 +129,8 @@ void TraverseExecutor::addStats(RpcResponse& resp, int64_t getNbrTimeInUSec) {
if (result.vertices_ref().has_value()) {
size = (*result.vertices_ref()).size();
}
auto info = collectRespProfileData(result.result, hostLatency[i], size, getNbrTimeInUSec);
stepInfo.push_back(folly::dynamic::object(folly::sformat("resp[{}]", i), info));
auto info = util::collectRespProfileData(result.result, hostLatency[i], size, getNbrTimeInUSec);
stepInfo.push_back(std::move(info));
}
otherStats_.emplace(folly::sformat("step[{}]", currentStep_), folly::toPrettyJson(stepInfo));
}
Expand Down
1 change: 1 addition & 0 deletions src/graph/util/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ nebula_add_library(
ParserUtil.cpp
PlannerUtil.cpp
ValidateUtil.cpp
Utils.cpp
)

nebula_add_library(
Expand Down
39 changes: 39 additions & 0 deletions src/graph/util/Utils.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
// Copyright (c) 2022 vesoft inc. All rights reserved.
//
// This source code is licensed under Apache 2.0 License.

#include "graph/util/Utils.h"

#include "interface/gen-cpp2/storage_types.h"

namespace nebula::graph::util {

folly::dynamic getStorageDetail(const std::map<std::string, int32_t>& profileDetail) {
folly::dynamic profileData = folly::dynamic::object();
for (auto& p : profileDetail) {
profileData.insert(p.first, folly::sformat("{}(us)", p.second));
}
return profileData;
}

folly::dynamic collectRespProfileData(const storage::cpp2::ResponseCommon& resp,
const std::tuple<HostAddr, int32_t, int32_t>& info,
size_t numVertices,
size_t totalRpcTime) {
folly::dynamic stat = folly::dynamic::object();
stat.insert("address", std::get<0>(info).toString());
stat.insert("exec", folly::sformat("{}(us)", std::get<1>(info)));
stat.insert("total", folly::sformat("{}(us)", std::get<2>(info)));
if (numVertices > 0) {
stat.insert("vertices", numVertices);
}
if (totalRpcTime > 0) {
stat.insert("total_rpc_time", folly::sformat("{}(us)", totalRpcTime));
}
if (resp.latency_detail_us_ref().has_value()) {
stat.insert("storage_detail", getStorageDetail(*resp.get_latency_detail_us()));
}
return stat;
}

} // namespace nebula::graph::util
14 changes: 14 additions & 0 deletions src/graph/util/Utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,18 @@
#define GRAPH_UTIL_UTILS_H_

#include <folly/String.h>
#include <folly/dynamic.h>

#include <iterator>
#include <string>
#include <vector>

#include "common/datatypes/HostAddr.h"

namespace nebula::storage::cpp2 {
class ResponseCommon;
}

namespace nebula::graph::util {

// Iterates the container and for each element, apply the function fn(). Joins the results of the
Expand All @@ -24,6 +31,13 @@ std::string join(const Container& container, Fn fn, const std::string& delimiter
return folly::join(delimiter, strs);
}

folly::dynamic getStorageDetail(const std::map<std::string, int32_t>& profileDetail);

folly::dynamic collectRespProfileData(const storage::cpp2::ResponseCommon& resp,
const std::tuple<HostAddr, int32_t, int32_t>& info,
size_t numVertices = 0UL,
size_t totalRpcTime = 0UL);

} // namespace nebula::graph::util

#endif // GRAPH_UTIL_UTILS_H_

0 comments on commit ed9937a

Please sign in to comment.