diff --git a/src/common/memory/MemoryUtils.cpp b/src/common/memory/MemoryUtils.cpp index 7e0bb7db7cc..bcd4b84ca02 100644 --- a/src/common/memory/MemoryUtils.cpp +++ b/src/common/memory/MemoryUtils.cpp @@ -8,11 +8,6 @@ #include #include -#include -#include -#include -#include - #include "common/fs/FileUtils.h" DEFINE_bool(containerized, false, "Whether run this process inside the docker container"); @@ -42,7 +37,7 @@ StatusOr MemoryUtils::hitsHighWatermark() { uint64_t cacheSize = 0; for (; iter.valid(); ++iter) { auto& sm = iter.matched(); - cacheSize += std::stoul(sm[2].str(), NULL); + cacheSize += std::stoul(sm[2].str(), nullptr); } std::string limitPath = @@ -64,7 +59,7 @@ StatusOr MemoryUtils::hitsHighWatermark() { std::vector memorySize; for (; iter.valid(); ++iter) { auto& sm = iter.matched(); - memorySize.emplace_back(std::stoul(sm[2].str(), NULL) << 10); + memorySize.emplace_back(std::stoul(sm[2].str(), nullptr) << 10); } std::sort(memorySize.begin(), memorySize.end()); if (memorySize.size() >= 2u) { diff --git a/src/graph/service/Authenticator.h b/src/graph/service/Authenticator.h index dd53b7ba177..ffddb8d17e4 100644 --- a/src/graph/service/Authenticator.h +++ b/src/graph/service/Authenticator.h @@ -12,9 +12,10 @@ namespace nebula { namespace graph { +// Interface for authentication class Authenticator { public: - virtual ~Authenticator() {} + virtual ~Authenticator() = default; virtual Status NG_MUST_USE_RESULT auth(const std::string &user, const std::string &password) = 0; }; diff --git a/src/graph/service/CloudAuthenticator.cpp b/src/graph/service/CloudAuthenticator.cpp index 09961b30ae4..e642520ec94 100644 --- a/src/graph/service/CloudAuthenticator.cpp +++ b/src/graph/service/CloudAuthenticator.cpp @@ -31,14 +31,10 @@ Status CloudAuthenticator::auth(const std::string& user, const std::string& pass std::string userAndPasswd = user + ":" + password; std::string base64Str = proxygen::base64Encode(folly::StringPiece(userAndPasswd)); - std::string header = "-H \"Content-Type: application/json\" -H \"Authorization:Nebula "; + std::string header = R"(-H "Content-Type: application/json" -H "Authorization:Nebula )"; header = header + base64Str + "\""; auto result = http::HttpClient::post(FLAGS_cloud_http_url, header); - - if (!result.ok()) { - LOG(ERROR) << result.status(); - return result.status(); - } + NG_LOG_AND_RETURN_IF_ERROR(result); try { auto json = folly::parseJson(result.value()); diff --git a/src/graph/service/GraphFlags.h b/src/graph/service/GraphFlags.h index fe3ecaec3ba..22d8e433ddd 100644 --- a/src/graph/service/GraphFlags.h +++ b/src/graph/service/GraphFlags.h @@ -45,7 +45,7 @@ DECLARE_uint32(failed_login_attempts); // The deault value is 0. A value of 0 disables the option. DECLARE_uint32(password_lock_time_in_secs); -// optimizer +// Optimizer DECLARE_bool(enable_optimizer); DECLARE_int64(max_allowed_connections); diff --git a/src/graph/service/GraphServer.cpp b/src/graph/service/GraphServer.cpp index c9254d35295..51aa1039e68 100644 --- a/src/graph/service/GraphServer.cpp +++ b/src/graph/service/GraphServer.cpp @@ -40,6 +40,7 @@ bool GraphServer::start() { return false; } + // Init worker id for snowflake generating unique id nebula::Snowflake::initWorkerId(interface->metaClient_.get()); graphThread_ = std::make_unique([&] { diff --git a/src/graph/service/GraphServer.h b/src/graph/service/GraphServer.h index 671c0720605..6c2b2696dde 100644 --- a/src/graph/service/GraphServer.h +++ b/src/graph/service/GraphServer.h @@ -24,7 +24,7 @@ class GraphServer { void stop(); - // used for signal handler to set an internal stop flag + // Used for signal handler to set an internal stop flag void notifyStop(); void waitUntilStop(); diff --git a/src/graph/service/GraphService.cpp b/src/graph/service/GraphService.cpp index 4deaafc372b..bd71953838b 100644 --- a/src/graph/service/GraphService.cpp +++ b/src/graph/service/GraphService.cpp @@ -8,6 +8,7 @@ #include #include +#include #include "clients/storage/StorageClient.h" #include "common/base/Base.h" @@ -41,7 +42,7 @@ Status GraphService::init(std::shared_ptr ioExecuto metaClient_ = std::make_unique(ioExecutor, std::move(addrs.value()), options); - // load data try 3 time + // Load data try 3 time bool loadDataOk = metaClient_->waitForMetadReady(3); if (!loadDataOk) { // Resort to retrying in the background @@ -69,7 +70,7 @@ folly::Future GraphService::future_authenticate(const std::string& auto ctx = std::make_unique>(); auto future = ctx->future(); - // check username and password failed + // Check username and password failed auto authResult = auth(username, password); if (!authResult.ok()) { ctx->resp().errorCode = ErrorCode::E_BAD_USERNAME_PASSWORD; diff --git a/src/graph/service/GraphService.h b/src/graph/service/GraphService.h index 85ed21a6ce4..a54c679dd6c 100644 --- a/src/graph/service/GraphService.h +++ b/src/graph/service/GraphService.h @@ -22,7 +22,7 @@ namespace graph { class GraphService final : public cpp2::GraphServiceSvIf { public: GraphService() = default; - ~GraphService() = default; + ~GraphService() override = default; Status NG_MUST_USE_RESULT init(std::shared_ptr ioExecutor, const HostAddr& hostAddr); diff --git a/src/graph/service/PermissionCheck.cpp b/src/graph/service/PermissionCheck.cpp index 3acacf1846b..f0abcc00b5e 100644 --- a/src/graph/service/PermissionCheck.cpp +++ b/src/graph/service/PermissionCheck.cpp @@ -28,11 +28,10 @@ namespace graph { * Special operation : kShow, kChangePassword */ -// static -Status PermissionCheck::permissionCheck(ClientSession *session, - Sentence *sentence, - ValidateContext *vctx, - GraphSpaceID targetSpace) { +/* static */ Status PermissionCheck::permissionCheck(ClientSession *session, + Sentence *sentence, + ValidateContext *vctx, + GraphSpaceID targetSpace) { if (!FLAGS_enable_authorize) { return Status::OK(); } @@ -165,7 +164,7 @@ Status PermissionCheck::permissionCheck(ClientSession *session, case Sentence::Kind::kShowMetaLeader: case Sentence::Kind::kShowHosts: { /** - * all roles can be show for above operations. + * All roles can be show for above operations. */ return Status::OK(); } @@ -206,7 +205,7 @@ Status PermissionCheck::permissionCheck(ClientSession *session, return Status::OK(); } case Sentence::Kind::kExplain: - // everyone could explain + // Everyone could explain return Status::OK(); case Sentence::Kind::kSequential: { // No permission checking for sequential sentence. diff --git a/src/graph/service/PermissionManager.cpp b/src/graph/service/PermissionManager.cpp index 80ce3153a53..328463c2e75 100644 --- a/src/graph/service/PermissionManager.cpp +++ b/src/graph/service/PermissionManager.cpp @@ -10,8 +10,7 @@ namespace nebula { namespace graph { -// static -Status PermissionManager::canReadSpace(ClientSession *session, GraphSpaceID spaceId) { +/* static */ Status PermissionManager::canReadSpace(ClientSession *session, GraphSpaceID spaceId) { if (!FLAGS_enable_authorize) { return Status::OK(); } @@ -35,8 +34,8 @@ Status PermissionManager::canReadSpace(ClientSession *session, GraphSpaceID spac return Status::PermissionError("No permission to read space."); } -// static -Status PermissionManager::canReadSchemaOrData(ClientSession *session, ValidateContext *vctx) { +/* static */ Status PermissionManager::canReadSchemaOrData(ClientSession *session, + ValidateContext *vctx) { if (!FLAGS_enable_authorize) { return Status::OK(); } @@ -60,8 +59,7 @@ Status PermissionManager::canReadSchemaOrData(ClientSession *session, ValidateCo return Status::PermissionError("No permission to read schema/data."); } -// static -Status PermissionManager::canWriteSpace(ClientSession *session) { +/* static */ Status PermissionManager::canWriteSpace(ClientSession *session) { if (!FLAGS_enable_authorize) { return Status::OK(); } @@ -71,8 +69,8 @@ Status PermissionManager::canWriteSpace(ClientSession *session) { return Status::PermissionError("No permission to write space."); } -// static -Status PermissionManager::canWriteSchema(ClientSession *session, ValidateContext *vctx) { +/* static */ Status PermissionManager::canWriteSchema(ClientSession *session, + ValidateContext *vctx) { if (!FLAGS_enable_authorize) { return Status::OK(); } @@ -97,8 +95,7 @@ Status PermissionManager::canWriteSchema(ClientSession *session, ValidateContext return Status::PermissionError("No permission to write schema."); } -// static -Status PermissionManager::canWriteUser(ClientSession *session) { +/* static */ Status PermissionManager::canWriteUser(ClientSession *session) { if (!FLAGS_enable_authorize) { return Status::OK(); } @@ -113,8 +110,8 @@ Status PermissionManager::canWriteUser(ClientSession *session) { } } -// static -Status PermissionManager::canReadUser(ClientSession *session, const std::string &targetUser) { +/* static */ Status PermissionManager::canReadUser(ClientSession *session, + const std::string &targetUser) { if (!FLAGS_enable_authorize) { return Status::OK(); } @@ -177,8 +174,7 @@ Status PermissionManager::canWriteRole(ClientSession *session, targetUser.c_str()); } -// static -Status PermissionManager::canWriteData(ClientSession *session, ValidateContext *vctx) { +/* static */ Status PermissionManager::canWriteData(ClientSession *session, ValidateContext *vctx) { if (!FLAGS_enable_authorize) { return Status::OK(); } diff --git a/src/graph/service/PermissionManager.h b/src/graph/service/PermissionManager.h index 655b8dd420f..06737c439e1 100644 --- a/src/graph/service/PermissionManager.h +++ b/src/graph/service/PermissionManager.h @@ -16,6 +16,7 @@ namespace nebula { namespace graph { +// This module is responsible for checking the permission of the user class PermissionManager final { public: PermissionManager() = delete; diff --git a/src/graph/service/QueryEngine.cpp b/src/graph/service/QueryEngine.cpp index d3f4f54e609..692d1dc4c72 100644 --- a/src/graph/service/QueryEngine.cpp +++ b/src/graph/service/QueryEngine.cpp @@ -24,6 +24,7 @@ DEFINE_int32(check_memory_interval_in_secs, 1, "Memory check interval in seconds namespace nebula { namespace graph { +// Register planners, init optimizer and set memory monitor Status QueryEngine::init(std::shared_ptr ioExecutor, meta::MetaClient* metaClient) { metaClient_ = metaClient; @@ -54,6 +55,7 @@ void QueryEngine::execute(RequestContextPtr rctx) { instance->execute(); } +// Setup memory monitor thread Status QueryEngine::setupMemoryMonitorThread() { memoryMonitorThread_ = std::make_unique(); if (!memoryMonitorThread_ || !memoryMonitorThread_->start("graph-memory-monitor")) { diff --git a/src/graph/service/QueryInstance.cpp b/src/graph/service/QueryInstance.cpp index aa56c9e58d5..01182f228b1 100644 --- a/src/graph/service/QueryInstance.cpp +++ b/src/graph/service/QueryInstance.cpp @@ -43,11 +43,14 @@ void QueryInstance::execute() { return; } - if (!explainOrContinue()) { + // Sentence is explain query, finish + if (explainAndFinish()) { onFinish(); return; } + // The execution engine converts the physical execution plan generated by the Planner into a + // series of Executors through the Scheduler to drive the execution of the Executors. scheduler_->schedule() .thenValue([this](Status s) { if (s.ok()) { @@ -62,10 +65,12 @@ void QueryInstance::execute() { [this](const std::exception &e) { onError(Status::Error("%s", e.what())); }); } +// Get query from GQLParser, validate and optimize the query Status QueryInstance::validateAndOptimize() { auto *rctx = qctx()->rctx(); auto &spaceName = rctx->session()->space().name; VLOG(1) << "Parsing query: " << rctx->query(); + // Result of parsing, get the parsing tree auto result = GQLParser(qctx()).parse(rctx->query()); NG_RETURN_IF_ERROR(result); sentence_ = std::move(result).value(); @@ -84,7 +89,9 @@ Status QueryInstance::validateAndOptimize() { } } + // Validate the query, if failed, return NG_RETURN_IF_ERROR(Validator::validate(sentence_.get(), qctx())); + // Optimize the query, and get the execution plan NG_RETURN_IF_ERROR(findBestPlan()); stats::StatsManager::addValue(kOptimizerLatencyUs, *(qctx_->plan()->optimizeTimeInUs())); if (FLAGS_enable_space_level_metrics && spaceName != "") { @@ -95,9 +102,10 @@ Status QueryInstance::validateAndOptimize() { return Status::OK(); } -bool QueryInstance::explainOrContinue() { +// Sentence is explain query, return the description of plan , and then finish +bool QueryInstance::explainAndFinish() { if (sentence_->kind() != Sentence::Kind::kExplain) { - return true; + return false; } auto &resp = qctx_->rctx()->resp(); resp.planDesc = std::make_unique(); @@ -209,6 +217,7 @@ void QueryInstance::addSlowQueryStats(uint64_t latency, const std::string &space } } +// Get result from query context and fill the response void QueryInstance::fillRespData(ExecutionResponse *resp) { auto ectx = DCHECK_NOTNULL(qctx_->ectx()); auto plan = DCHECK_NOTNULL(qctx_->plan()); @@ -218,7 +227,7 @@ void QueryInstance::fillRespData(ExecutionResponse *resp) { auto &&value = ectx->moveValue(name); if (!value.isDataSet()) return; - // fill dataset + // Fill dataset auto result = value.moveDataSet(); if (!result.colNames.empty()) { resp->data = std::make_unique(std::move(result)); @@ -229,6 +238,7 @@ void QueryInstance::fillRespData(ExecutionResponse *resp) { } } +// The entry point of the optimizer Status QueryInstance::findBestPlan() { auto plan = qctx_->plan(); SCOPED_TIMER(plan->optimizeTimeInUs()); diff --git a/src/graph/service/QueryInstance.h b/src/graph/service/QueryInstance.h index a30168d56cc..c146c3fd6b2 100644 --- a/src/graph/service/QueryInstance.h +++ b/src/graph/service/QueryInstance.h @@ -29,6 +29,7 @@ class QueryInstance final : public boost::noncopyable, public cpp::NonMovable { explicit QueryInstance(std::unique_ptr qctx, opt::Optimizer* optimizer); ~QueryInstance() = default; + // Entrance of the Validate, Optimize, Schedule, Execute process void execute(); QueryContext* qctx() const { @@ -51,8 +52,8 @@ class QueryInstance final : public boost::noncopyable, public cpp::NonMovable { void onError(Status); Status validateAndOptimize(); - // return true if continue to execute - bool explainOrContinue(); + // Return true if continue to execute + bool explainAndFinish(); void addSlowQueryStats(uint64_t latency, const std::string& spaceName) const; void fillRespData(ExecutionResponse* resp); Status findBestPlan(); diff --git a/src/graph/service/RequestContext.h b/src/graph/service/RequestContext.h index 6bf3cd4fc0d..80348480680 100644 --- a/src/graph/service/RequestContext.h +++ b/src/graph/service/RequestContext.h @@ -33,7 +33,7 @@ class RequestContext final : public boost::noncopyable, public cpp::NonMovable { RequestContext() = default; ~RequestContext() { if (session_ != nullptr) { - // keep the session active + // Keep the session active session_->charge(); } } @@ -57,7 +57,7 @@ class RequestContext final : public boost::noncopyable, public cpp::NonMovable { void setSession(std::shared_ptr session) { session_ = std::move(session); if (session_ != nullptr) { - // keep the session active + // Keep the session active session_->charge(); } }