From b4b99f0f22dda436771b38d742a37982e27da3d4 Mon Sep 17 00:00:00 2001 From: db <1301189887@qq.com> Date: Tue, 3 Dec 2024 17:36:36 +0800 Subject: [PATCH 1/8] add enum type --- src/CraneCtld/AccountManager.cpp | 59 ++++++++------ src/CraneCtld/AccountManager.h | 5 +- src/CraneCtld/CtldGrpcServer.cpp | 58 +++----------- src/CraneCtld/TaskScheduler.cpp | 19 +++-- .../PublicHeader/include/crane/PublicHeader.h | 78 +++++++++++++++++++ 5 files changed, 138 insertions(+), 81 deletions(-) diff --git a/src/CraneCtld/AccountManager.cpp b/src/CraneCtld/AccountManager.cpp index fd4e8a239..aa8ae9da5 100644 --- a/src/CraneCtld/AccountManager.cpp +++ b/src/CraneCtld/AccountManager.cpp @@ -920,14 +920,16 @@ std::expected AccountManager::CheckIfUserOfAccountIsEnabled( return {}; } -std::expected AccountManager::CheckAndApplyQosLimitOnTask( - const std::string& user, const std::string& account, TaskInCtld* task) { +AccountManager::CraneExpected AccountManager::CheckAndApplyQosLimitOnTask(const std::string& user, + const std::string& account, + TaskInCtld* task) { util::read_lock_guard user_guard(m_rw_user_mutex_); util::read_lock_guard qos_guard(m_rw_qos_mutex_); const User* user_share_ptr = GetExistedUserInfoNoLock_(user); if (!user_share_ptr) { - return std::unexpected(fmt::format("Unknown user '{}'", user)); + CRANE_ERROR("CheckAndApplyQosLimitOnTask error: Unknown user {}", user); + return std::unexpected(CraneErrCode::ERR_INVALID_OP_USER); } if (task->uid != 0) { @@ -935,46 +937,59 @@ std::expected AccountManager::CheckAndApplyQosLimitOnTask( .allowed_partition_qos_map.find(task->partition_id); if (partition_it == user_share_ptr->account_to_attrs_map.at(account) .allowed_partition_qos_map.end()) - return std::unexpected("Partition is not allowed for this user."); + CRANE_ERROR( + "CheckAndApplyQosLimitOnTask error: Partition is not allowed for " + "this user"); + return std::unexpected(CraneErrCode::ERR_ALLOWED_PARTITION); if (task->qos.empty()) { // Default qos task->qos = partition_it->second.first; - if (task->qos.empty()) - return std::unexpected( - fmt::format("The user '{}' has no QOS available for this partition " - "'{}' to be used", - task->Username(), task->partition_id)); + if (task->qos.empty()) { + CRANE_ERROR( + "CheckAndApplyQosLimitOnTask error: The user '{}' has no QOS " + "available for this partition '{}' to be used", + task->Username(), task->partition_id); + return std::unexpected(CraneErrCode::ERR_HAS_NO_QOS_IN_PARTITION); + } } else { // Check whether task.qos in the qos list - if (!ranges::contains(partition_it->second.second, task->qos)) - return std::unexpected(fmt::format( - "The qos '{}' you set is not in partition's allowed qos list", - task->qos)); + if (!ranges::contains(partition_it->second.second, task->qos)) { + CRANE_ERROR( + "CheckAndApplyQosLimitOnTask error: The qos '{}'" + " you set is not in partition's allowed qos list", + task->qos); + return std::unexpected(CraneErrCode::ERR_HAS_ALLOWED_QOS_IN_PARTITION); + } } } else { if (task->qos.empty()) { - task->qos = kUnlimitedQosName; - } + @@ -960,19 +969,25 @@ result::result AccountManager::CheckAndApplyQosLimitOnTask( } const Qos* qos_share_ptr = GetExistedQosInfoNoLock_(task->qos); - if (!qos_share_ptr) - return std::unexpected(fmt::format("Unknown QOS '{}'", task->qos)); + if (!qos_share_ptr) { + CRANE_ERROR("Unknown QOS '{}'", task->qos); + return std::unexpected(CraneErrCode::ERR_INVALID_QOS); + } task->qos_priority = qos_share_ptr->priority; if (task->time_limit >= absl::Seconds(kTaskMaxTimeLimitSec)) { task->time_limit = qos_share_ptr->max_time_limit_per_task; - } else if (task->time_limit > qos_share_ptr->max_time_limit_per_task) - return std::unexpected("time-limit reached the user's limit."); + } else if (task->time_limit > qos_share_ptr->max_time_limit_per_task) { + CRANE_ERROR("time-limit reached the user's limit"); + return std::unexpected(CraneErrCode::ERR_TIME_TIMIT_BEYOND); + } if (static_cast(task->cpus_per_task) > - qos_share_ptr->max_cpus_per_user) - return std::unexpected("cpus-per-task reached the user's limit."); + qos_share_ptr->max_cpus_per_user) { + CRANE_ERROR("cpus-per-task reached the user's limit"); + return std::unexpected(CraneErrCode::ERR_CPUS_PER_TASK_BEYOND); + } return {}; -} +}} std::expected AccountManager::CheckUidIsAdmin( uint32_t uid) { diff --git a/src/CraneCtld/AccountManager.h b/src/CraneCtld/AccountManager.h index 812acc5a5..9c71ba7a9 100644 --- a/src/CraneCtld/AccountManager.h +++ b/src/CraneCtld/AccountManager.h @@ -132,8 +132,9 @@ class AccountManager { std::expected CheckIfUserOfAccountIsEnabled( const std::string& user, const std::string& account); - std::expected CheckAndApplyQosLimitOnTask( - const std::string& user, const std::string& account, TaskInCtld* task); + CraneExpected CheckAndApplyQosLimitOnTask(const std::string& user, + const std::string& account, + TaskInCtld* task); std::expected CheckUidIsAdmin(uint32_t uid); diff --git a/src/CraneCtld/CtldGrpcServer.cpp b/src/CraneCtld/CtldGrpcServer.cpp index c14c087d8..511690743 100644 --- a/src/CraneCtld/CtldGrpcServer.cpp +++ b/src/CraneCtld/CtldGrpcServer.cpp @@ -936,13 +936,11 @@ CtldServer::CtldServer(const Config::CraneCtldListenConf &listen_conf) { signal(SIGINT, &CtldServer::signal_handler_func); } -std::expected, std::string> +CraneErrCodeExpected > CtldServer::SubmitTaskToScheduler(std::unique_ptr task) { - CraneErr err; if (!task->password_entry->Valid()) { - return std::unexpected( - fmt::format("Uid {} not found on the controller node", task->uid)); + return std::unexpected(crane::grpc::ErrCode::ERR_INVALID_UID); } task->SetUsername(task->password_entry->Username()); @@ -950,8 +948,7 @@ CtldServer::SubmitTaskToScheduler(std::unique_ptr task) { auto user_scoped_ptr = g_account_manager->GetExistedUserInfo(task->Username()); if (!user_scoped_ptr) { - return std::unexpected(fmt::format( - "User '{}' not found in the account database", task->Username())); + return std::unexpected(crane::grpc::ErrCode::ERR_INVALID_USER); } if (task->account.empty()) { @@ -959,67 +956,34 @@ CtldServer::SubmitTaskToScheduler(std::unique_ptr task) { task->MutableTaskToCtld()->set_account(user_scoped_ptr->default_account); } else { if (!user_scoped_ptr->account_to_attrs_map.contains(task->account)) { - return std::unexpected(fmt::format( - "Account '{}' is not in your account list", task->account)); + return std::unexpected(crane::grpc::ErrCode::ERR_USER_ACCOUNT_MISMATCH); } } } if (!g_account_manager->CheckUserPermissionToPartition( task->Username(), task->account, task->partition_id)) { - return std::unexpected( - fmt::format("User '{}' doesn't have permission to use partition '{}' " - "when using account '{}'", - task->Username(), task->partition_id, task->account)); + return std::unexpected(crane::grpc::ErrCode::ERR_ALLOWED_PARTITION); } - auto enable_res = - g_account_manager->CheckIfUserOfAccountIsEnabled( + auto enable_res = g_account_manager->CheckIfUserOfAccountIsEnabled( task->Username(), task->account); if (!enable_res) { return std::unexpected(enable_res.error()); } - err = g_task_scheduler->AcquireTaskAttributes(task.get()); + auto result = g_task_scheduler->AcquireTaskAttributes(task.get()); - if (err == CraneErr::kOk) - err = g_task_scheduler->CheckTaskValidity(task.get()); + if (result) + result = g_task_scheduler->CheckTaskValidity(task.get()); - if (err == CraneErr::kOk) { + if (result) { task->SetSubmitTime(absl::Now()); std::future future = g_task_scheduler->SubmitTaskAsync(std::move(task)); return {std::move(future)}; } - if (err == CraneErr::kNonExistent) { - CRANE_DEBUG("Task submission failed. Reason: Partition doesn't exist!"); - return std::unexpected("Partition doesn't exist!"); - } else if (err == CraneErr::kInvalidNodeNum) { - CRANE_DEBUG( - "Task submission failed. Reason: --node is either invalid or greater " - "than the number of nodes in its partition."); - return std::unexpected( - "--node is either invalid or greater than the number of nodes in its " - "partition."); - } else if (err == CraneErr::kNoResource) { - CRANE_DEBUG( - "Task submission failed. " - "Reason: The resources of the partition are insufficient."); - return std::unexpected("The resources of the partition are insufficient"); - } else if (err == CraneErr::kNoAvailNode) { - CRANE_DEBUG( - "Task submission failed. " - "Reason: Nodes satisfying the requirements of task are insufficient"); - return std::unexpected( - "Nodes satisfying the requirements of task are insufficient."); - } else if (err == CraneErr::kInvalidParam) { - CRANE_DEBUG( - "Task submission failed. " - "Reason: The param of task is invalid."); - return std::unexpected("The param of task is invalid."); - } - return std::unexpected(CraneErrStr(err)); -} + return std::unexpected(result.error());} } // namespace Ctld diff --git a/src/CraneCtld/TaskScheduler.cpp b/src/CraneCtld/TaskScheduler.cpp index ffa3025e7..9326f5046 100644 --- a/src/CraneCtld/TaskScheduler.cpp +++ b/src/CraneCtld/TaskScheduler.cpp @@ -2720,7 +2720,7 @@ void TaskScheduler::PersistAndTransferTasksToMongodb_( CraneErr TaskScheduler::AcquireTaskAttributes(TaskInCtld* task) { auto part_it = g_config.Partitions.find(task->partition_id); - if (part_it == g_config.Partitions.end()) return CraneErr::kInvalidParam; + if (part_it == g_config.Partitions.end()) return CraneErr::kNonExistent; task->partition_priority = part_it->second.priority; @@ -2748,16 +2748,12 @@ CraneErr TaskScheduler::AcquireTaskAttributes(TaskInCtld* task) { auto check_qos_result = g_account_manager->CheckAndApplyQosLimitOnTask( task->Username(), task->account, task); - if (!check_qos_result) { - CRANE_ERROR("Failed to call CheckAndApplyQosLimitOnTask: {}", - check_qos_result.error()); - return CraneErr::kInvalidParam; - } + if (!check_qos_result) return check_qos_result; if (!task->TaskToCtld().nodelist().empty() && task->included_nodes.empty()) { std::list nodes; bool ok = util::ParseHostList(task->TaskToCtld().nodelist(), &nodes); - if (!ok) return CraneErr::kInvalidParam; + if (!ok) return CraneErr::kInvaildNodeList; for (auto&& node : nodes) task->included_nodes.emplace(std::move(node)); } @@ -2765,7 +2761,7 @@ CraneErr TaskScheduler::AcquireTaskAttributes(TaskInCtld* task) { if (!task->TaskToCtld().excludes().empty() && task->excluded_nodes.empty()) { std::list nodes; bool ok = util::ParseHostList(task->TaskToCtld().excludes(), &nodes); - if (!ok) return CraneErr::kInvalidParam; + if (!ok) return CraneErr::kInvalidExNodeList; for (auto&& node : nodes) task->excluded_nodes.emplace(std::move(node)); } @@ -2774,8 +2770,11 @@ CraneErr TaskScheduler::AcquireTaskAttributes(TaskInCtld* task) { } CraneErr TaskScheduler::CheckTaskValidity(TaskInCtld* task) { - if (!CheckIfTimeLimitIsValid(task->time_limit)) - return CraneErr::kInvalidParam; + if (!CheckIfTimeLimitIsValid(task->time_limit)) { + CRANE_TRACE("Time-limit {} reached the user's limit", + absl::FormatDuration(task->time_limit)); + return CraneErr::kInvalidTimeLimit; + } // Check whether the selected partition is able to run this task. std::unordered_set avail_nodes; diff --git a/src/Utilities/PublicHeader/include/crane/PublicHeader.h b/src/Utilities/PublicHeader/include/crane/PublicHeader.h index 4cf91633e..5d9a5dd0d 100644 --- a/src/Utilities/PublicHeader/include/crane/PublicHeader.h +++ b/src/Utilities/PublicHeader/include/crane/PublicHeader.h @@ -26,6 +26,7 @@ #include #include "protos/Crane.pb.h" +#include "protos/PublicDefs.pb.h" #if !defined(CRANE_VERSION_STRING) # define CRANE_VERSION_STRING "Unknown" @@ -64,6 +65,9 @@ enum class CraneErr : uint16_t { template using CraneExpected = std::expected; +template +using CraneErrCodeExpected = std::expected; + inline const char* kCtldDefaultPort = "10011"; inline const char* kCranedDefaultPort = "10010"; inline const char* kCforedDefaultPort = "10012"; @@ -147,12 +151,86 @@ constexpr std::array "Not enough nodes which satisfy resource requirements", }; +const std::unordered_map ErrCodeStrMap = { + {crane::grpc::ErrCode::SUCCESS, "Success"}, + {crane::grpc::ErrCode::ERR_INVALID_UID, "Invalid UID"}, + {crane::grpc::ErrCode::ERR_INVALID_OP_USER, "You are not a user of Crane"}, + {crane::grpc::ErrCode::ERR_INVALID_USER, "The entered user is not a user of Crane"}, + {crane::grpc::ErrCode::ERR_PERMISSION_USER, "Your permission is insufficient"}, + {crane::grpc::ErrCode::ERR_USER_DUPLICATE_ACCOUNT, "The user already exists in this account"}, + {crane::grpc::ErrCode::ERR_USER_ALLOWED_ACCOUNT, "he user is not allowed to access account"}, + {crane::grpc::ErrCode::ERR_INVALID_ADMIN_LEVEL, "Unknown admin level"}, + {crane::grpc::ErrCode::ERR_USER_ACCOUNT_MISMATCH, "The user does not belong to this account"}, + {crane::grpc::ErrCode::ERR_NO_ACCOUNT_SPECIFIED, "No account is specified for the user"}, + {crane::grpc::ErrCode::ERR_INVALID_ACCOUNT, "The entered account does not exist"}, + {crane::grpc::ErrCode::ERR_DUPLICATE_ACCOUNT, "The account already exists in the crane"}, + {crane::grpc::ErrCode::ERR_INVALID_PARENTACCOUNT, "The parent account of the entered account does not exist"}, + {crane::grpc::ErrCode::ERR_DELETE_ACCOUNT, "The account has child account or users, unable to delete"}, + {crane::grpc::ErrCode::ERR_INVALID_PARTITION, "The entered partition does not exist"}, + {crane::grpc::ErrCode::ERR_ALLOWED_PARTITION, "The entered account or user does not include this partition"}, + {crane::grpc::ErrCode::ERR_DUPLICATE_PARTITION, "The partition already exists in the account or user"}, + {crane::grpc::ErrCode::ERR_PARENT_ALLOWED_PARTITION, "Parent account does not include the partition"}, + {crane::grpc::ErrCode::ERR_USER_EMPTY_PARTITION, "The user does not contain any partitions, operation cannot be performed"}, + {crane::grpc::ErrCode::ERR_CHILD_HAS_PARTITION, "Child has partiton error"}, + {crane::grpc::ErrCode::ERR_HAS_NO_QOS_IN_PARTITION, "The user has no QoS available for this partition to be used"}, + {crane::grpc::ErrCode::ERR_HAS_ALLOWED_QOS_IN_PARTITION, "The qos you set is not in partition's allowed qos list"}, + {crane::grpc::ErrCode::ERR_INVALID_QOS, "The entered qos does not exist"}, + {crane::grpc::ErrCode::ERR_DB_DUPLICATE_QOS, "Qos already exists in the crane"}, + {crane::grpc::ErrCode::ERR_DELETE_QOS, "QoS is still being used by accounts or users, unable to delete"}, + {crane::grpc::ErrCode::ERR_CONVERT_TO_INTERGER, "Failed to convert value to integer"}, + {crane::grpc::ErrCode::ERR_TIME_LIMIT, "Invalid time limit value"}, + {crane::grpc::ErrCode::ERR_ALLOWED_QOS, "The entered account or user does not include this qos"}, + {crane::grpc::ErrCode::ERR_DUPLICATE_QOS, "The Qos already exists in the account or user"}, + {crane::grpc::ErrCode::ERR_PARENT_ALLOWED_QOS, "Parent account does not include the qos"}, + {crane::grpc::ErrCode::ERR_SET_ALLOWED_QOS, "Set allowed qos error"}, + {crane::grpc::ErrCode::ERR_ALLOWED_DEFAULT_QOS, "The entered default_qos is not allowed"}, + {crane::grpc::ErrCode::ERR_DUPLICATE_DEFAULT_QOS, "The QoS is already the default QoS for the account or specified partition of the user"}, + {crane::grpc::ErrCode::ERR_CHILD_HAS_DEFAULT_QOS, "child accounts has default error"}, + {crane::grpc::ErrCode::ERR_SET_ACCOUNT_QOS, "set account qos error"}, + {crane::grpc::ErrCode::ERR_SET_DEFAULT_QOS, "The Qos not allowed or is already the default qos"}, + {crane::grpc::ErrCode::ERR_IS_DEFAULT_QOS, "Is default qos error"}, + {crane::grpc::ErrCode::ERR_UPDATE_DATABASE, "Fail to update data in database"}, + {crane::grpc::ErrCode::ERR_GENERIC_FAILURE, "Generic Failure"}, + {crane::grpc::ErrCode::ERR_NO_RESOURCE, "Resource not enough for task"}, + {crane::grpc::ErrCode::ERR_NON_EXISTENT, "Non-existent Error"}, + {crane::grpc::ErrCode::ERR_INVALID_NODE_NUM, "Nodes partition not enough for task"}, + {crane::grpc::ErrCode::ERR_INVAILD_NODE_LIST, "Invalid node list"}, + {crane::grpc::ErrCode::ERR_INVAILD_EX_NODE_LIST, "Invalid exclude node list"}, + {crane::grpc::ErrCode::ERR_TIME_TIMIT_BEYOND, "Time-limit reached the user's limit"}, + {crane::grpc::ErrCode::ERR_CPUS_PER_TASK_BEYOND, "cpus-per-task reached the user's limit"}, + {crane::grpc::ErrCode::ERR_NO_ENOUGH_NODE, "Nodes num not enough for task"}, + {crane::grpc::ErrCode::ERR_SYSTEM_ERR, "System Error"}, + {crane::grpc::ErrCode::ERR_EXISTING_TASK, "Existing Task"}, + {crane::grpc::ErrCode::ERR_BEYOND_TASK_ID, "System error occurred or the number of pending tasks exceeded maximum value"}, + {crane::grpc::ErrCode::ERR_INVALID_PARAM, "Invalid Parameter"}, + {crane::grpc::ErrCode::ERR_STOP, "Stop Error"}, + {crane::grpc::ErrCode::ERR_PERMISSION_DENIED, "Permission Denied"}, + {crane::grpc::ErrCode::ERR_CONNECTION_TIMEOUT, "Connection Timeout"}, + {crane::grpc::ErrCode::ERR_CONNECTION_ABORTED, "Connection Aborted"}, + {crane::grpc::ErrCode::ERR_RPC_FAILURE, "RPC Failure"}, + {crane::grpc::ErrCode::ERR_TOKEN_REQUEST_FAILURE, "Token Request Failure"}, + {crane::grpc::ErrCode::ERR_STREAM_BROKEN, "Stream Broken"}, + {crane::grpc::ErrCode::ERR_INVALID_STUB, "Invalid Stub"}, + {crane::grpc::ErrCode::ERR_CGROUP, "CGroup Error"}, + {crane::grpc::ErrCode::ERR_PROTOBUF, "Protobuf Error"}, + {crane::grpc::ErrCode::ERR_LIB_EVENT, "Lib Event Error"}, + {crane::grpc::ErrCode::ERR_NO_AVAIL_NODE, "No Available Node"} +}; + } inline std::string_view CraneErrStr(CraneErr err) { return Internal::CraneErrStrArr[uint16_t(err)]; } +inline std::string_view CraneErrCodeStr(crane::grpc::ErrCode err) { + auto it = Internal::ErrCodeStrMap.find(err); + if (it != Internal::ErrCodeStrMap.end()) { + return it->second; + } + return "Unknown Error"; +} + /* ----------- Public definitions for all components */ using PartitionId = std::string; From 5dabedefd12a80be202aeefda97c6704e5445b84 Mon Sep 17 00:00:00 2001 From: db <1301189887@qq.com> Date: Tue, 3 Dec 2024 17:39:30 +0800 Subject: [PATCH 2/8] add enum type --- src/CraneCtld/TaskScheduler.cpp | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/src/CraneCtld/TaskScheduler.cpp b/src/CraneCtld/TaskScheduler.cpp index 9326f5046..d2c7b7f30 100644 --- a/src/CraneCtld/TaskScheduler.cpp +++ b/src/CraneCtld/TaskScheduler.cpp @@ -2770,11 +2770,8 @@ CraneErr TaskScheduler::AcquireTaskAttributes(TaskInCtld* task) { } CraneErr TaskScheduler::CheckTaskValidity(TaskInCtld* task) { - if (!CheckIfTimeLimitIsValid(task->time_limit)) { - CRANE_TRACE("Time-limit {} reached the user's limit", - absl::FormatDuration(task->time_limit)); + if (!CheckIfTimeLimitIsValid(task->time_limit)) return CraneErr::kInvalidTimeLimit; - } // Check whether the selected partition is able to run this task. std::unordered_set avail_nodes; From bf52d55574fddf011f76480ade954a3be5eae031 Mon Sep 17 00:00:00 2001 From: db <1301189887@qq.com> Date: Tue, 10 Dec 2024 16:32:52 +0800 Subject: [PATCH 3/8] use codeErr type --- protos/Crane.proto | 4 +- protos/PublicDefs.proto | 123 +++++++++++++++++-------------- src/CraneCtld/AccountManager.cpp | 16 ++++ src/CraneCtld/AccountManager.h | 4 + src/CraneCtld/CtldGrpcServer.cpp | 17 ++++- src/CraneCtld/CtldGrpcServer.h | 4 + src/CraneCtld/TaskScheduler.cpp | 35 ++++----- src/CraneCtld/TaskScheduler.h | 4 +- 8 files changed, 127 insertions(+), 80 deletions(-) diff --git a/protos/Crane.proto b/protos/Crane.proto index be2f822f0..278f246f8 100644 --- a/protos/Crane.proto +++ b/protos/Crane.proto @@ -61,7 +61,7 @@ message SubmitBatchTaskReply { bool ok = 1; oneof payload{ uint32 task_id = 2; - string reason = 3; + ErrCode reason = 3; } } @@ -72,7 +72,7 @@ message SubmitBatchTasksRequest { message SubmitBatchTasksReply { repeated uint32 task_id_list = 1; - repeated string reason_list = 2; + repeated ErrCode reason_list = 2; } message ExecuteTasksRequest { diff --git a/protos/PublicDefs.proto b/protos/PublicDefs.proto index 8c328d860..4e3d41967 100644 --- a/protos/PublicDefs.proto +++ b/protos/PublicDefs.proto @@ -319,65 +319,76 @@ message TrimmedPartitionInfo { enum ErrCode { SUCCESS = 0; // Success - ERR_INVALID_UID = 10001; - ERR_INVALID_OP_USER = 10002; - ERR_INVALID_USER = 10003; - ERR_PERMISSION_USER = 10004; - ERR_USER_DUPLICATE_ACCOUNT = 10005; - ERR_USER_ALLOWED_ACCOUNT = 10006; - ERR_INVALID_ADMIN_LEVEL = 10007; - ERR_USER_ACCOUNT_MISMATCH = 10008; - ERR_NO_ACCOUNT_SPECIFIED = 10009; - - ERR_INVALID_ACCOUNT = 10010; - ERR_DUPLICATE_ACCOUNT = 10011; - ERR_INVALID_PARENTACCOUNT = 10012; - ERR_DELETE_ACCOUNT = 10013; - - ERR_INVALID_PARTITION = 10014; - ERR_ALLOWED_PARTITION = 10015; - ERR_DUPLICATE_PARTITION = 10016; - ERR_PARENT_ALLOWED_PARTITION = 10017; - ERR_USER_EMPTY_PARTITION = 10018; - ERR_CHILD_HAS_PARTITION = 10019; - - ERR_INVALID_QOS = 10020; - ERR_DB_DUPLICATE_QOS = 10021; - ERR_DELETE_QOS = 10022; - ERR_CONVERT_TO_INTERGER = 10023; - ERR_TIME_LIMIT = 10024; - ERR_ALLOWED_QOS = 10025; - ERR_DUPLICATE_QOS = 10026; - ERR_PARENT_ALLOWED_QOS = 10027; - ERR_SET_ALLOWED_QOS = 10028; - ERR_ALLOWED_DEFAULT_QOS = 10029; - ERR_DUPLICATE_DEFAULT_QOS = 10030; - ERR_CHILD_HAS_DEFAULT_QOS = 10031; - ERR_SET_ACCOUNT_QOS = 10032; - ERR_SET_DEFAULT_QOS = 10033; - ERR_IS_DEFAULT_QOS = 10034; - - ERR_UPDATE_DATABASE = 10035; - - ERR_GENERIC_FAILURE = 10100; + ERR_INVALID_UID = 10001; // Invalid UID passed + ERR_INVALID_OP_USER = 10002; // Invalid operation user + ERR_INVALID_USER = 10003; // Invalid user + ERR_PERMISSION_USER = 10004; // User permissions too low, no permission to operate + ERR_BLOCKED_USER = 10005; + ERR_USER_DUPLICATE_ACCOUNT= 10006; // User duplicate account insertion + ERR_USER_ALLOWED_ACCOUNT = 10007; // User does not have permission for the account + ERR_INVALID_ADMIN_LEVEL = 10008; // Invalid permission level + ERR_USER_ACCOUNT_MISMATCH = 10009; // User does not belong to the account + ERR_NO_ACCOUNT_SPECIFIED = 10010; + + ERR_INVALID_ACCOUNT = 10011; // Invalid account + ERR_DUPLICATE_ACCOUNT = 10012; // Duplicate account insertion + ERR_INVALID_PARENTACCOUNT = 10013; // Invalid parent account + ERR_DELETE_ACCOUNT = 10014; // Account has child nodes + ERR_BLOCKED_ACCOUNT = 10015; + + ERR_INVALID_PARTITION = 10016; // Invalid partition, partition does not exist + ERR_ALLOWED_PARTITION = 10017; // Account/user does not include this partition + ERR_DUPLICATE_PARTITION = 10018; // Account/user duplicate insertion + ERR_PARENT_ALLOWED_PARTITION = 10019; // Parent account does not include this partition + ERR_USER_EMPTY_PARTITION = 10020; // Cannot add QoS when user has no partition + ERR_CHILD_HAS_PARTITION = 10021; // Partition '{}' is used by some descendant node of the account '{}'. Ignoring this constraint with forced operation. + ERR_HAS_NO_QOS_IN_PARTITION = 10022; + ERR_HAS_ALLOWED_QOS_IN_PARTITION = 10023; + + ERR_INVALID_QOS = 10024; // Invalid QoS, QoS does not exist + ERR_DB_DUPLICATE_QOS = 10025; // Duplicate QoS insertion in the database. + ERR_DELETE_QOS = 10026; // QoS reference count is not zero. + ERR_CONVERT_TO_INTERGER = 10027; // String to integer conversion failed + ERR_TIME_LIMIT = 10028; // Invalid time value + ERR_ALLOWED_QOS = 10029; // Account/user does not include this QoS. + ERR_DUPLICATE_QOS = 10030; // Account/user duplicate insertion. + ERR_PARENT_ALLOWED_QOS = 10031; // Parent account does not include this QoS. + ERR_SET_ALLOWED_QOS = 10032; // QoS '{}' is the default QoS of partition '{}', but not found in the new QoS list. + ERR_ALLOWED_DEFAULT_QOS = 10033; // Default QoS is not in the allowed QoS list + ERR_DUPLICATE_DEFAULT_QOS = 10034; // Duplicate default QoS setting + ERR_CHILD_HAS_DEFAULT_QOS = 10035; // Someone is using QoS '{}' as default QoS. Ignoring this constraint with forced deletion, the deleted default QoS is randomly replaced with one of the remaining items in the QoS list. + ERR_SET_ACCOUNT_QOS = 10036; // QoS '{}' is used by some descendant node or itself of the account '{}'. Ignoring this constraint with forced operation. + ERR_SET_DEFAULT_QOS = 10037; // Qos '{}' not in allowed qos list or is already the default qos + ERR_IS_DEFAULT_QOS = 10038; + + ERR_UPDATE_DATABASE = 10039; // Database update failed + + ERR_GENERIC_FAILURE = 10100; ERR_NO_RESOURCE = 10101; ERR_NON_EXISTENT = 10102; ERR_INVALID_NODE_NUM = 10103; - ERR_SYSTEM_ERR = 10104; - ERR_EXISTING_TASK = 10105; - ERR_INVALID_PARAM = 10106; - ERR_STOP = 10107; - ERR_PERMISSION_DENIED = 10108; - ERR_CONNECTION_TIMEOUT = 10109; - ERR_CONNECTION_ABORTED = 10110; - ERR_RPC_FAILURE = 10111; - ERR_TOKEN_REQUEST_FAILURE = 10112; - ERR_STREAM_BROKEN = 10113; - ERR_INVALID_STUB = 10114; - ERR_CGROUP = 10115; - ERR_PROTOBUF = 10116; - ERR_LIB_EVENT = 10117; - ERR_NO_AVAIL_NODE = 10118; + ERR_INVAILD_NODE_LIST = 10104; + ERR_INVAILD_EX_NODE_LIST = 10105; + ERR_TIME_TIMIT_BEYOND = 10106; + ERR_CPUS_PER_TASK_BEYOND = 10107; + ERR_NO_ENOUGH_NODE = 10108; + + ERR_SYSTEM_ERR = 10109; + ERR_EXISTING_TASK = 10110; + ERR_BEYOND_TASK_ID = 10111; + ERR_INVALID_PARAM = 10112; + ERR_STOP = 10113; + ERR_PERMISSION_DENIED = 10114; + ERR_CONNECTION_TIMEOUT = 10115; + ERR_CONNECTION_ABORTED = 10116; + ERR_RPC_FAILURE = 10117; + ERR_TOKEN_REQUEST_FAILURE = 10118; + ERR_STREAM_BROKEN = 10119; + ERR_INVALID_STUB = 10120; + ERR_CGROUP = 10121; + ERR_PROTOBUF = 10122; + ERR_LIB_EVENT = 10123; + ERR_NO_AVAIL_NODE = 10124; } enum EntityType { diff --git a/src/CraneCtld/AccountManager.cpp b/src/CraneCtld/AccountManager.cpp index aa8ae9da5..fedaac46e 100644 --- a/src/CraneCtld/AccountManager.cpp +++ b/src/CraneCtld/AccountManager.cpp @@ -898,7 +898,11 @@ bool AccountManager::CheckUserPermissionToPartition( return false; } +<<<<<<< HEAD std::expected AccountManager::CheckIfUserOfAccountIsEnabled( +======= +CraneErrCodeExpected AccountManager::CheckIfUserOfAccountIsEnabled( +>>>>>>> 2d88fb3 (use codeErr type) const std::string& user, const std::string& account) { util::read_lock_guard user_guard(m_rw_user_mutex_); util::read_lock_guard account_guard(m_rw_account_mutex_); @@ -907,15 +911,23 @@ std::expected AccountManager::CheckIfUserOfAccountIsEnabled( do { const Account* account_ptr = GetExistedAccountInfoNoLock_(account_name); if (account_ptr->blocked) { +<<<<<<< HEAD return std::unexpected( fmt::format("Ancestor account '{}' is blocked", account_ptr->name)); +======= + return std::unexpected(crane::grpc::ErrCode::ERR_BLOCKED_ACCOUNT); +>>>>>>> 2d88fb3 (use codeErr type) } account_name = account_ptr->parent_account; } while (!account_name.empty()); const User* user_ptr = GetExistedUserInfoNoLock_(user); if (user_ptr->account_to_attrs_map.at(account).blocked) { +<<<<<<< HEAD return std::unexpected(fmt::format("User '{}' is blocked", user_ptr->name)); +======= + return std::unexpected(crane::grpc::ErrCode::ERR_BLOCKED_USER); +>>>>>>> 2d88fb3 (use codeErr type) } return {}; } @@ -989,7 +1001,11 @@ AccountManager::CraneExpected AccountManager::CheckAndApplyQosLimitOnTask( } return {}; +<<<<<<< HEAD }} +======= +} +>>>>>>> 2d88fb3 (use codeErr type) std::expected AccountManager::CheckUidIsAdmin( uint32_t uid) { diff --git a/src/CraneCtld/AccountManager.h b/src/CraneCtld/AccountManager.h index 9c71ba7a9..c95e5f67e 100644 --- a/src/CraneCtld/AccountManager.h +++ b/src/CraneCtld/AccountManager.h @@ -129,7 +129,11 @@ class AccountManager { const std::string& account, const std::string& partition); +<<<<<<< HEAD std::expected CheckIfUserOfAccountIsEnabled( +======= + CraneErrCodeExpected CheckIfUserOfAccountIsEnabled( +>>>>>>> 2d88fb3 (use codeErr type) const std::string& user, const std::string& account); CraneExpected CheckAndApplyQosLimitOnTask(const std::string& user, diff --git a/src/CraneCtld/CtldGrpcServer.cpp b/src/CraneCtld/CtldGrpcServer.cpp index 511690743..52fb69a6c 100644 --- a/src/CraneCtld/CtldGrpcServer.cpp +++ b/src/CraneCtld/CtldGrpcServer.cpp @@ -41,9 +41,7 @@ grpc::Status CraneCtldServiceImpl::SubmitBatchTask( response->set_task_id(id); } else { response->set_ok(false); - response->set_reason( - "System error occurred or " - "the number of pending tasks exceeded maximum value."); + response->set_reason(crane::grpc::ErrCode::ERR_BEYOND_TASK_ID); } } else { response->set_ok(false); @@ -57,7 +55,11 @@ grpc::Status CraneCtldServiceImpl::SubmitBatchTasks( grpc::ServerContext *context, const crane::grpc::SubmitBatchTasksRequest *request, crane::grpc::SubmitBatchTasksReply *response) { +<<<<<<< HEAD std::vector, std::string>> results; +======= + std::vector>> results; +>>>>>>> 2d88fb3 (use codeErr type) uint32_t task_count = request->count(); const auto &task_to_ctld = request->task(); @@ -815,7 +817,11 @@ grpc::Status CraneCtldServiceImpl::CforedStream( result = std::expected{ submit_result.value().get()}; } else { +<<<<<<< HEAD result = std::unexpected(submit_result.error()); +======= + result = result::fail(CraneErrCodeStr(submit_result.error())); +>>>>>>> 2d88fb3 (use codeErr type) } ok = stream_writer->WriteTaskIdReply(payload.pid(), result); @@ -984,6 +990,11 @@ CtldServer::SubmitTaskToScheduler(std::unique_ptr task) { return {std::move(future)}; } +<<<<<<< HEAD return std::unexpected(result.error());} +======= + return std::unexpected(result.error()); +} +>>>>>>> 2d88fb3 (use codeErr type) } // namespace Ctld diff --git a/src/CraneCtld/CtldGrpcServer.h b/src/CraneCtld/CtldGrpcServer.h index fdc24d0ac..7295569a2 100644 --- a/src/CraneCtld/CtldGrpcServer.h +++ b/src/CraneCtld/CtldGrpcServer.h @@ -308,7 +308,11 @@ class CtldServer { inline void Wait() { m_server_->Wait(); } +<<<<<<< HEAD std::expected, std::string> SubmitTaskToScheduler( +======= + CraneErrCodeExpected > SubmitTaskToScheduler( +>>>>>>> 2d88fb3 (use codeErr type) std::unique_ptr task); private: diff --git a/src/CraneCtld/TaskScheduler.cpp b/src/CraneCtld/TaskScheduler.cpp index d2c7b7f30..86e5752b1 100644 --- a/src/CraneCtld/TaskScheduler.cpp +++ b/src/CraneCtld/TaskScheduler.cpp @@ -81,8 +81,8 @@ bool TaskScheduler::Init() { CRANE_TRACE("Restore task #{} from embedded running queue.", task->TaskId()); - err = AcquireTaskAttributes(task.get()); - if (err != CraneErr::kOk || task->type == crane::grpc::Interactive) { + auto result = AcquireTaskAttributes(task.get()); + if (!result || task->type == crane::grpc::Interactive) { task->SetStatus(crane::grpc::Failed); ok = g_embedded_db_client->UpdateRuntimeAttrOfTask(0, task_db_id, task->RuntimeAttr()); @@ -92,13 +92,13 @@ bool TaskScheduler::Init() { "mark the task as FAILED.", task_id); } - if (err != CraneErr::kOk) + if (!result) CRANE_INFO( "Failed to acquire task attributes for restored running task " "#{}. " "Error Code: {}. " "Mark it as FAILED and move it to the ended queue.", - task_id, CraneErrStr(err)); + task_id, CraneErrCodeStr(result.error())); else { CRANE_INFO("Mark running interactive task {} as FAILED.", task_id); for (const CranedId& craned_id : task->CranedIds()) { @@ -318,13 +318,13 @@ bool TaskScheduler::Init() { } if (!mark_task_as_failed && - AcquireTaskAttributes(task.get()) != CraneErr::kOk) { + !(AcquireTaskAttributes(task.get()))) { CRANE_ERROR("AcquireTaskAttributes failed for task #{}", task_id); mark_task_as_failed = true; } if (!mark_task_as_failed && - CheckTaskValidity(task.get()) != CraneErr::kOk) { + !(CheckTaskValidity(task.get()))) { CRANE_ERROR("CheckTaskValidity failed for task #{}", task_id); mark_task_as_failed = true; } @@ -2718,9 +2718,10 @@ void TaskScheduler::PersistAndTransferTasksToMongodb_( } } -CraneErr TaskScheduler::AcquireTaskAttributes(TaskInCtld* task) { +CraneErrCodeExpected TaskScheduler::AcquireTaskAttributes(TaskInCtld* task) { auto part_it = g_config.Partitions.find(task->partition_id); - if (part_it == g_config.Partitions.end()) return CraneErr::kNonExistent; + if (part_it == g_config.Partitions.end()) + return std::unexpected(crane::grpc::ErrCode::ERR_INVALID_PARTITION); task->partition_priority = part_it->second.priority; @@ -2753,7 +2754,7 @@ CraneErr TaskScheduler::AcquireTaskAttributes(TaskInCtld* task) { if (!task->TaskToCtld().nodelist().empty() && task->included_nodes.empty()) { std::list nodes; bool ok = util::ParseHostList(task->TaskToCtld().nodelist(), &nodes); - if (!ok) return CraneErr::kInvaildNodeList; + if (!ok) return std::unexpected(crane::grpc::ErrCode::ERR_INVAILD_NODE_LIST); for (auto&& node : nodes) task->included_nodes.emplace(std::move(node)); } @@ -2761,17 +2762,17 @@ CraneErr TaskScheduler::AcquireTaskAttributes(TaskInCtld* task) { if (!task->TaskToCtld().excludes().empty() && task->excluded_nodes.empty()) { std::list nodes; bool ok = util::ParseHostList(task->TaskToCtld().excludes(), &nodes); - if (!ok) return CraneErr::kInvalidExNodeList; + if (!ok) return std::unexpected(crane::grpc::ErrCode::ERR_INVAILD_EX_NODE_LIST); for (auto&& node : nodes) task->excluded_nodes.emplace(std::move(node)); } - return CraneErr::kOk; + return {}; } -CraneErr TaskScheduler::CheckTaskValidity(TaskInCtld* task) { +CraneErrCodeExpected TaskScheduler::CheckTaskValidity(TaskInCtld* task) { if (!CheckIfTimeLimitIsValid(task->time_limit)) - return CraneErr::kInvalidTimeLimit; + return std::unexpected(crane::grpc::ErrCode::ERR_TIME_TIMIT_BEYOND) ; // Check whether the selected partition is able to run this task. std::unordered_set avail_nodes; @@ -2801,7 +2802,7 @@ CraneErr TaskScheduler::CheckTaskValidity(TaskInCtld* task) { .memory_sw_bytes), util::ReadableTypedDeviceMap( metas_ptr->partition_global_meta.res_total.GetDeviceMap())); - return CraneErr::kNoResource; + return std::unexpected(crane::grpc::ErrCode::ERR_NO_RESOURCE) ; } if (task->node_num > metas_ptr->craned_ids.size()) { @@ -2809,7 +2810,7 @@ CraneErr TaskScheduler::CheckTaskValidity(TaskInCtld* task) { "Nodes not enough for task #{}. " "Partition total Nodes: {}", task->TaskId(), metas_ptr->craned_ids.size()); - return CraneErr::kInvalidNodeNum; + return std::unexpected(crane::grpc::ErrCode::ERR_INVALID_NODE_NUM); } auto craned_meta_map = g_meta_container->GetCranedMetaMapConstPtr(); @@ -2831,10 +2832,10 @@ CraneErr TaskScheduler::CheckTaskValidity(TaskInCtld* task) { "Resource not enough. Task #{} needs {} nodes, while only {} " "nodes satisfy its requirement.", task->TaskId(), task->node_num, avail_nodes.size()); - return CraneErr::kNoAvailNode; + return std::unexpected(crane::grpc::ErrCode::ERR_NO_ENOUGH_NODE); } - return CraneErr::kOk; + return {}; } void TaskScheduler::TerminateTasksOnCraned(const CranedId& craned_id, diff --git a/src/CraneCtld/TaskScheduler.h b/src/CraneCtld/TaskScheduler.h index 32eab6508..269defd03 100644 --- a/src/CraneCtld/TaskScheduler.h +++ b/src/CraneCtld/TaskScheduler.h @@ -282,9 +282,9 @@ class TaskScheduler { return TerminateRunningTaskNoLock_(iter->second.get()); } - static CraneErr AcquireTaskAttributes(TaskInCtld* task); + static CraneErrCodeExpected AcquireTaskAttributes(TaskInCtld* task); - static CraneErr CheckTaskValidity(TaskInCtld* task); + static CraneErrCodeExpected CheckTaskValidity(TaskInCtld* task); private: template From 9ba39df8e679e14370661c133d256569d0d5229f Mon Sep 17 00:00:00 2001 From: db <1301189887@qq.com> Date: Wed, 11 Dec 2024 17:50:30 +0800 Subject: [PATCH 4/8] fix conflicts --- src/CraneCtld/AccountManager.cpp | 17 ----------------- src/CraneCtld/AccountManager.h | 4 ---- src/CraneCtld/CtldGrpcServer.cpp | 12 ------------ src/CraneCtld/CtldGrpcServer.h | 4 ---- 4 files changed, 37 deletions(-) diff --git a/src/CraneCtld/AccountManager.cpp b/src/CraneCtld/AccountManager.cpp index fedaac46e..2f394b2ea 100644 --- a/src/CraneCtld/AccountManager.cpp +++ b/src/CraneCtld/AccountManager.cpp @@ -898,11 +898,7 @@ bool AccountManager::CheckUserPermissionToPartition( return false; } -<<<<<<< HEAD -std::expected AccountManager::CheckIfUserOfAccountIsEnabled( -======= CraneErrCodeExpected AccountManager::CheckIfUserOfAccountIsEnabled( ->>>>>>> 2d88fb3 (use codeErr type) const std::string& user, const std::string& account) { util::read_lock_guard user_guard(m_rw_user_mutex_); util::read_lock_guard account_guard(m_rw_account_mutex_); @@ -911,23 +907,14 @@ CraneErrCodeExpected AccountManager::CheckIfUserOfAccountIsEnabled( do { const Account* account_ptr = GetExistedAccountInfoNoLock_(account_name); if (account_ptr->blocked) { -<<<<<<< HEAD - return std::unexpected( - fmt::format("Ancestor account '{}' is blocked", account_ptr->name)); -======= return std::unexpected(crane::grpc::ErrCode::ERR_BLOCKED_ACCOUNT); ->>>>>>> 2d88fb3 (use codeErr type) } account_name = account_ptr->parent_account; } while (!account_name.empty()); const User* user_ptr = GetExistedUserInfoNoLock_(user); if (user_ptr->account_to_attrs_map.at(account).blocked) { -<<<<<<< HEAD - return std::unexpected(fmt::format("User '{}' is blocked", user_ptr->name)); -======= return std::unexpected(crane::grpc::ErrCode::ERR_BLOCKED_USER); ->>>>>>> 2d88fb3 (use codeErr type) } return {}; } @@ -1001,11 +988,7 @@ AccountManager::CraneExpected AccountManager::CheckAndApplyQosLimitOnTask( } return {}; -<<<<<<< HEAD -}} -======= } ->>>>>>> 2d88fb3 (use codeErr type) std::expected AccountManager::CheckUidIsAdmin( uint32_t uid) { diff --git a/src/CraneCtld/AccountManager.h b/src/CraneCtld/AccountManager.h index c95e5f67e..ef5fbd497 100644 --- a/src/CraneCtld/AccountManager.h +++ b/src/CraneCtld/AccountManager.h @@ -129,11 +129,7 @@ class AccountManager { const std::string& account, const std::string& partition); -<<<<<<< HEAD - std::expected CheckIfUserOfAccountIsEnabled( -======= CraneErrCodeExpected CheckIfUserOfAccountIsEnabled( ->>>>>>> 2d88fb3 (use codeErr type) const std::string& user, const std::string& account); CraneExpected CheckAndApplyQosLimitOnTask(const std::string& user, diff --git a/src/CraneCtld/CtldGrpcServer.cpp b/src/CraneCtld/CtldGrpcServer.cpp index 52fb69a6c..caa7a7fa1 100644 --- a/src/CraneCtld/CtldGrpcServer.cpp +++ b/src/CraneCtld/CtldGrpcServer.cpp @@ -55,11 +55,7 @@ grpc::Status CraneCtldServiceImpl::SubmitBatchTasks( grpc::ServerContext *context, const crane::grpc::SubmitBatchTasksRequest *request, crane::grpc::SubmitBatchTasksReply *response) { -<<<<<<< HEAD - std::vector, std::string>> results; -======= std::vector>> results; ->>>>>>> 2d88fb3 (use codeErr type) uint32_t task_count = request->count(); const auto &task_to_ctld = request->task(); @@ -817,11 +813,7 @@ grpc::Status CraneCtldServiceImpl::CforedStream( result = std::expected{ submit_result.value().get()}; } else { -<<<<<<< HEAD - result = std::unexpected(submit_result.error()); -======= result = result::fail(CraneErrCodeStr(submit_result.error())); ->>>>>>> 2d88fb3 (use codeErr type) } ok = stream_writer->WriteTaskIdReply(payload.pid(), result); @@ -990,11 +982,7 @@ CtldServer::SubmitTaskToScheduler(std::unique_ptr task) { return {std::move(future)}; } -<<<<<<< HEAD - return std::unexpected(result.error());} -======= return std::unexpected(result.error()); } ->>>>>>> 2d88fb3 (use codeErr type) } // namespace Ctld diff --git a/src/CraneCtld/CtldGrpcServer.h b/src/CraneCtld/CtldGrpcServer.h index 7295569a2..8d3755d2d 100644 --- a/src/CraneCtld/CtldGrpcServer.h +++ b/src/CraneCtld/CtldGrpcServer.h @@ -308,11 +308,7 @@ class CtldServer { inline void Wait() { m_server_->Wait(); } -<<<<<<< HEAD - std::expected, std::string> SubmitTaskToScheduler( -======= CraneErrCodeExpected > SubmitTaskToScheduler( ->>>>>>> 2d88fb3 (use codeErr type) std::unique_ptr task); private: From 047b89597b73e91186b1da3696e10cbb47ed182a Mon Sep 17 00:00:00 2001 From: db <1301189887@qq.com> Date: Thu, 12 Dec 2024 10:26:08 +0800 Subject: [PATCH 5/8] fix conflict --- src/CraneCtld/AccountManager.cpp | 10 ++++++---- src/CraneCtld/CtldGrpcServer.cpp | 2 +- 2 files changed, 7 insertions(+), 5 deletions(-) diff --git a/src/CraneCtld/AccountManager.cpp b/src/CraneCtld/AccountManager.cpp index 2f394b2ea..820f7c761 100644 --- a/src/CraneCtld/AccountManager.cpp +++ b/src/CraneCtld/AccountManager.cpp @@ -935,12 +935,13 @@ AccountManager::CraneExpected AccountManager::CheckAndApplyQosLimitOnTask( auto partition_it = user_share_ptr->account_to_attrs_map.at(account) .allowed_partition_qos_map.find(task->partition_id); if (partition_it == user_share_ptr->account_to_attrs_map.at(account) - .allowed_partition_qos_map.end()) + .allowed_partition_qos_map.end()) { + CRANE_ERROR( "CheckAndApplyQosLimitOnTask error: Partition is not allowed for " "this user"); - return std::unexpected(CraneErrCode::ERR_ALLOWED_PARTITION); - + return std::unexpected(CraneErrCode::ERR_ALLOWED_PARTITION); + } if (task->qos.empty()) { // Default qos task->qos = partition_it->second.first; @@ -963,7 +964,8 @@ AccountManager::CraneExpected AccountManager::CheckAndApplyQosLimitOnTask( } } else { if (task->qos.empty()) { - @@ -960,19 +969,25 @@ result::result AccountManager::CheckAndApplyQosLimitOnTask( + task->qos = kUnlimitedQosName; + } } const Qos* qos_share_ptr = GetExistedQosInfoNoLock_(task->qos); diff --git a/src/CraneCtld/CtldGrpcServer.cpp b/src/CraneCtld/CtldGrpcServer.cpp index caa7a7fa1..a28816fce 100644 --- a/src/CraneCtld/CtldGrpcServer.cpp +++ b/src/CraneCtld/CtldGrpcServer.cpp @@ -813,7 +813,7 @@ grpc::Status CraneCtldServiceImpl::CforedStream( result = std::expected{ submit_result.value().get()}; } else { - result = result::fail(CraneErrCodeStr(submit_result.error())); + result = std::unexpected(CraneErrCodeStr(submit_result.error())); } ok = stream_writer->WriteTaskIdReply(payload.pid(), result); From 535868118e675a47e20189a3a600bbaa6afa638d Mon Sep 17 00:00:00 2001 From: db <1301189887@qq.com> Date: Mon, 16 Dec 2024 10:33:39 +0800 Subject: [PATCH 6/8] fix conflicts --- protos/PublicDefs.proto | 141 +++++++++--------- src/CraneCtld/AccountManager.cpp | 14 +- src/CraneCtld/CtldGrpcServer.cpp | 7 +- src/CraneCtld/TaskScheduler.cpp | 17 ++- .../PublicHeader/include/crane/PublicHeader.h | 136 +++++++++-------- 5 files changed, 163 insertions(+), 152 deletions(-) diff --git a/protos/PublicDefs.proto b/protos/PublicDefs.proto index 4e3d41967..0f1f9c238 100644 --- a/protos/PublicDefs.proto +++ b/protos/PublicDefs.proto @@ -319,76 +319,77 @@ message TrimmedPartitionInfo { enum ErrCode { SUCCESS = 0; // Success - ERR_INVALID_UID = 10001; // Invalid UID passed - ERR_INVALID_OP_USER = 10002; // Invalid operation user - ERR_INVALID_USER = 10003; // Invalid user - ERR_PERMISSION_USER = 10004; // User permissions too low, no permission to operate - ERR_BLOCKED_USER = 10005; - ERR_USER_DUPLICATE_ACCOUNT= 10006; // User duplicate account insertion - ERR_USER_ALLOWED_ACCOUNT = 10007; // User does not have permission for the account - ERR_INVALID_ADMIN_LEVEL = 10008; // Invalid permission level - ERR_USER_ACCOUNT_MISMATCH = 10009; // User does not belong to the account - ERR_NO_ACCOUNT_SPECIFIED = 10010; - - ERR_INVALID_ACCOUNT = 10011; // Invalid account - ERR_DUPLICATE_ACCOUNT = 10012; // Duplicate account insertion - ERR_INVALID_PARENTACCOUNT = 10013; // Invalid parent account - ERR_DELETE_ACCOUNT = 10014; // Account has child nodes - ERR_BLOCKED_ACCOUNT = 10015; - - ERR_INVALID_PARTITION = 10016; // Invalid partition, partition does not exist - ERR_ALLOWED_PARTITION = 10017; // Account/user does not include this partition - ERR_DUPLICATE_PARTITION = 10018; // Account/user duplicate insertion - ERR_PARENT_ALLOWED_PARTITION = 10019; // Parent account does not include this partition - ERR_USER_EMPTY_PARTITION = 10020; // Cannot add QoS when user has no partition - ERR_CHILD_HAS_PARTITION = 10021; // Partition '{}' is used by some descendant node of the account '{}'. Ignoring this constraint with forced operation. - ERR_HAS_NO_QOS_IN_PARTITION = 10022; - ERR_HAS_ALLOWED_QOS_IN_PARTITION = 10023; - - ERR_INVALID_QOS = 10024; // Invalid QoS, QoS does not exist - ERR_DB_DUPLICATE_QOS = 10025; // Duplicate QoS insertion in the database. - ERR_DELETE_QOS = 10026; // QoS reference count is not zero. - ERR_CONVERT_TO_INTERGER = 10027; // String to integer conversion failed - ERR_TIME_LIMIT = 10028; // Invalid time value - ERR_ALLOWED_QOS = 10029; // Account/user does not include this QoS. - ERR_DUPLICATE_QOS = 10030; // Account/user duplicate insertion. - ERR_PARENT_ALLOWED_QOS = 10031; // Parent account does not include this QoS. - ERR_SET_ALLOWED_QOS = 10032; // QoS '{}' is the default QoS of partition '{}', but not found in the new QoS list. - ERR_ALLOWED_DEFAULT_QOS = 10033; // Default QoS is not in the allowed QoS list - ERR_DUPLICATE_DEFAULT_QOS = 10034; // Duplicate default QoS setting - ERR_CHILD_HAS_DEFAULT_QOS = 10035; // Someone is using QoS '{}' as default QoS. Ignoring this constraint with forced deletion, the deleted default QoS is randomly replaced with one of the remaining items in the QoS list. - ERR_SET_ACCOUNT_QOS = 10036; // QoS '{}' is used by some descendant node or itself of the account '{}'. Ignoring this constraint with forced operation. - ERR_SET_DEFAULT_QOS = 10037; // Qos '{}' not in allowed qos list or is already the default qos - ERR_IS_DEFAULT_QOS = 10038; - - ERR_UPDATE_DATABASE = 10039; // Database update failed - - ERR_GENERIC_FAILURE = 10100; - ERR_NO_RESOURCE = 10101; - ERR_NON_EXISTENT = 10102; - ERR_INVALID_NODE_NUM = 10103; - ERR_INVAILD_NODE_LIST = 10104; - ERR_INVAILD_EX_NODE_LIST = 10105; - ERR_TIME_TIMIT_BEYOND = 10106; - ERR_CPUS_PER_TASK_BEYOND = 10107; - ERR_NO_ENOUGH_NODE = 10108; - - ERR_SYSTEM_ERR = 10109; - ERR_EXISTING_TASK = 10110; - ERR_BEYOND_TASK_ID = 10111; - ERR_INVALID_PARAM = 10112; - ERR_STOP = 10113; - ERR_PERMISSION_DENIED = 10114; - ERR_CONNECTION_TIMEOUT = 10115; - ERR_CONNECTION_ABORTED = 10116; - ERR_RPC_FAILURE = 10117; - ERR_TOKEN_REQUEST_FAILURE = 10118; - ERR_STREAM_BROKEN = 10119; - ERR_INVALID_STUB = 10120; - ERR_CGROUP = 10121; - ERR_PROTOBUF = 10122; - ERR_LIB_EVENT = 10123; - ERR_NO_AVAIL_NODE = 10124; + ERR_INVALID_UID = 1; // Invalid UID passed + ERR_INVALID_OP_USER = 2; // Invalid operation user + ERR_INVALID_USER = 3; // Invalid user + ERR_PERMISSION_USER = 4; // User permissions too low, no permission to operate + ERR_BLOCKED_USER = 5; + ERR_USER_DUPLICATE_ACCOUNT= 6; // User duplicate account insertion + ERR_USER_ALLOWED_ACCOUNT = 7; // User does not have permission for the account + ERR_INVALID_ADMIN_LEVEL = 8; // Invalid permission level + ERR_USER_ACCOUNT_MISMATCH = 9; // User does not belong to the account + ERR_NO_ACCOUNT_SPECIFIED = 10; + + ERR_INVALID_ACCOUNT = 11; // Invalid account + ERR_DUPLICATE_ACCOUNT = 12; // Duplicate account insertion + ERR_INVALID_PARENTACCOUNT = 13; // Invalid parent account + ERR_DELETE_ACCOUNT = 14; // Account has child nodes + ERR_BLOCKED_ACCOUNT = 15; + + ERR_INVALID_PARTITION = 16; // Invalid partition, partition does not exist + ERR_ALLOWED_PARTITION = 17; // Account/user does not include this partition + ERR_DUPLICATE_PARTITION = 18; // Account/user duplicate insertion + ERR_PARENT_ALLOWED_PARTITION = 19; // Parent account does not include this partition + ERR_USER_EMPTY_PARTITION = 20; // Cannot add QoS when user has no partition + ERR_CHILD_HAS_PARTITION = 21; // Partition '{}' is used by some descendant node of the account '{}'. Ignoring this constraint with forced operation. + ERR_HAS_NO_QOS_IN_PARTITION = 22; + ERR_HAS_ALLOWED_QOS_IN_PARTITION = 23; + + ERR_INVALID_QOS = 24; // Invalid QoS, QoS does not exist + ERR_DB_DUPLICATE_QOS = 25; // Duplicate QoS insertion in the database. + ERR_DELETE_QOS = 26; // QoS reference count is not zero. + ERR_CONVERT_TO_INTERGER = 27; // String to integer conversion failed + ERR_TIME_LIMIT = 28; // Invalid time value + ERR_ALLOWED_QOS = 29; // Account/user does not include this QoS. + ERR_DUPLICATE_QOS = 30; // Account/user duplicate insertion. + ERR_PARENT_ALLOWED_QOS = 31; // Parent account does not include this QoS. + ERR_SET_ALLOWED_QOS = 32; // QoS '{}' is the default QoS of partition '{}', but not found in the new QoS list. + ERR_ALLOWED_DEFAULT_QOS = 33; // Default QoS is not in the allowed QoS list + ERR_DUPLICATE_DEFAULT_QOS = 34; // Duplicate default QoS setting + ERR_CHILD_HAS_DEFAULT_QOS = 35; // Someone is using QoS '{}' as default QoS. Ignoring this constraint with forced deletion, the deleted default QoS is randomly replaced with one of the remaining items in the QoS list. + ERR_SET_ACCOUNT_QOS = 36; // QoS '{}' is used by some descendant node or itself of the account '{}'. Ignoring this constraint with forced operation. + ERR_SET_DEFAULT_QOS = 37; // Qos '{}' not in allowed qos list or is already the default qos + ERR_IS_DEFAULT_QOS = 38; + + ERR_UPDATE_DATABASE = 39; // Database update failed + + ERR_GENERIC_FAILURE = 40; + ERR_NO_RESOURCE = 41; + ERR_NON_EXISTENT = 42; + ERR_INVALID_NODE_NUM = 43; + ERR_INVAILD_NODE_LIST = 44; + ERR_INVAILD_EX_NODE_LIST = 45; + ERR_TIME_TIMIT_BEYOND = 46; + ERR_CPUS_PER_TASK_BEYOND = 47; + ERR_NO_ENOUGH_NODE = 48; + + ERR_SYSTEM_ERR = 49; + ERR_EXISTING_TASK = 50; + ERR_BEYOND_TASK_ID = 51; + ERR_INVALID_PARAM = 52; + ERR_STOP = 53; + ERR_PERMISSION_DENIED = 54; + ERR_CONNECTION_TIMEOUT = 55; + ERR_CONNECTION_ABORTED = 56; + ERR_RPC_FAILURE = 57; + ERR_TOKEN_REQUEST_FAILURE = 58; + ERR_STREAM_BROKEN = 59; + ERR_INVALID_STUB = 60; + ERR_CGROUP = 61; + ERR_PROTOBUF = 62; + ERR_LIB_EVENT = 63; + ERR_NO_AVAIL_NODE = 64; + ERR_CODE_COUNT = 65; } enum EntityType { diff --git a/src/CraneCtld/AccountManager.cpp b/src/CraneCtld/AccountManager.cpp index 820f7c761..091092b4c 100644 --- a/src/CraneCtld/AccountManager.cpp +++ b/src/CraneCtld/AccountManager.cpp @@ -907,6 +907,8 @@ CraneErrCodeExpected AccountManager::CheckIfUserOfAccountIsEnabled( do { const Account* account_ptr = GetExistedAccountInfoNoLock_(account_name); if (account_ptr->blocked) { + CRANE_ERROR("Ancestor account '{}' is blocked", + account_ptr->name); return std::unexpected(crane::grpc::ErrCode::ERR_BLOCKED_ACCOUNT); } account_name = account_ptr->parent_account; @@ -914,20 +916,20 @@ CraneErrCodeExpected AccountManager::CheckIfUserOfAccountIsEnabled( const User* user_ptr = GetExistedUserInfoNoLock_(user); if (user_ptr->account_to_attrs_map.at(account).blocked) { + CRANE_ERROR("User '{}' is blocked", user_ptr->name); return std::unexpected(crane::grpc::ErrCode::ERR_BLOCKED_USER); } return {}; } -AccountManager::CraneExpected AccountManager::CheckAndApplyQosLimitOnTask(const std::string& user, - const std::string& account, - TaskInCtld* task) { +AccountManager::CraneExpected AccountManager::CheckAndApplyQosLimitOnTask( + const std::string& user, const std::string& account, TaskInCtld* task) { util::read_lock_guard user_guard(m_rw_user_mutex_); util::read_lock_guard qos_guard(m_rw_qos_mutex_); const User* user_share_ptr = GetExistedUserInfoNoLock_(user); if (!user_share_ptr) { - CRANE_ERROR("CheckAndApplyQosLimitOnTask error: Unknown user {}", user); + CRANE_ERROR("Unknown user {}", user); return std::unexpected(CraneErrCode::ERR_INVALID_OP_USER); } @@ -937,9 +939,7 @@ AccountManager::CraneExpected AccountManager::CheckAndApplyQosLimitOnTask( if (partition_it == user_share_ptr->account_to_attrs_map.at(account) .allowed_partition_qos_map.end()) { - CRANE_ERROR( - "CheckAndApplyQosLimitOnTask error: Partition is not allowed for " - "this user"); + CRANE_ERROR("Partition is not allowed for this user"); return std::unexpected(CraneErrCode::ERR_ALLOWED_PARTITION); } if (task->qos.empty()) { diff --git a/src/CraneCtld/CtldGrpcServer.cpp b/src/CraneCtld/CtldGrpcServer.cpp index a28816fce..cd84f9f8e 100644 --- a/src/CraneCtld/CtldGrpcServer.cpp +++ b/src/CraneCtld/CtldGrpcServer.cpp @@ -938,6 +938,7 @@ CraneErrCodeExpected > CtldServer::SubmitTaskToScheduler(std::unique_ptr task) { if (!task->password_entry->Valid()) { + CRANE_ERROR("Uid {} not found on the controller node", task->uid); return std::unexpected(crane::grpc::ErrCode::ERR_INVALID_UID); } task->SetUsername(task->password_entry->Username()); @@ -946,6 +947,7 @@ CtldServer::SubmitTaskToScheduler(std::unique_ptr task) { auto user_scoped_ptr = g_account_manager->GetExistedUserInfo(task->Username()); if (!user_scoped_ptr) { + CRANE_ERROR("User '{}' not found in the account database", task->Username()); return std::unexpected(crane::grpc::ErrCode::ERR_INVALID_USER); } @@ -954,6 +956,7 @@ CtldServer::SubmitTaskToScheduler(std::unique_ptr task) { task->MutableTaskToCtld()->set_account(user_scoped_ptr->default_account); } else { if (!user_scoped_ptr->account_to_attrs_map.contains(task->account)) { + CRANE_ERROR("Account '{}' is not in your account list", task->account); return std::unexpected(crane::grpc::ErrCode::ERR_USER_ACCOUNT_MISMATCH); } } @@ -961,7 +964,9 @@ CtldServer::SubmitTaskToScheduler(std::unique_ptr task) { if (!g_account_manager->CheckUserPermissionToPartition( task->Username(), task->account, task->partition_id)) { - return std::unexpected(crane::grpc::ErrCode::ERR_ALLOWED_PARTITION); + CRANE_ERROR("User '{}' doesn't have permission to use partition '{}' when using account '{}'", + task->Username(), task->partition_id, task->account); + return std::unexpected(crane::grpc::ErrCode::ERR_ALLOWED_PARTITION); } auto enable_res = g_account_manager->CheckIfUserOfAccountIsEnabled( diff --git a/src/CraneCtld/TaskScheduler.cpp b/src/CraneCtld/TaskScheduler.cpp index 86e5752b1..8b7d53235 100644 --- a/src/CraneCtld/TaskScheduler.cpp +++ b/src/CraneCtld/TaskScheduler.cpp @@ -2720,8 +2720,11 @@ void TaskScheduler::PersistAndTransferTasksToMongodb_( CraneErrCodeExpected TaskScheduler::AcquireTaskAttributes(TaskInCtld* task) { auto part_it = g_config.Partitions.find(task->partition_id); - if (part_it == g_config.Partitions.end()) + if (part_it == g_config.Partitions.end()) { + CRANE_ERROR("Failed to call AcquireTaskAttributes: {}", + CraneErrCodeStr(crane::grpc::ErrCode::ERR_INVALID_PARTITION)); return std::unexpected(crane::grpc::ErrCode::ERR_INVALID_PARTITION); + } task->partition_priority = part_it->second.priority; @@ -2749,7 +2752,11 @@ CraneErrCodeExpected TaskScheduler::AcquireTaskAttributes(TaskInCtld* task auto check_qos_result = g_account_manager->CheckAndApplyQosLimitOnTask( task->Username(), task->account, task); - if (!check_qos_result) return check_qos_result; + if (!check_qos_result) { + CRANE_ERROR("Failed to call CheckAndApplyQosLimitOnTask: {}", + CraneErrCodeStr(check_qos_result.error())); + return check_qos_result; + } if (!task->TaskToCtld().nodelist().empty() && task->included_nodes.empty()) { std::list nodes; @@ -2802,7 +2809,7 @@ CraneErrCodeExpected TaskScheduler::CheckTaskValidity(TaskInCtld* task) { .memory_sw_bytes), util::ReadableTypedDeviceMap( metas_ptr->partition_global_meta.res_total.GetDeviceMap())); - return std::unexpected(crane::grpc::ErrCode::ERR_NO_RESOURCE) ; + return std::unexpected(crane::grpc::ErrCode::ERR_NO_RESOURCE); } if (task->node_num > metas_ptr->craned_ids.size()) { @@ -2810,7 +2817,7 @@ CraneErrCodeExpected TaskScheduler::CheckTaskValidity(TaskInCtld* task) { "Nodes not enough for task #{}. " "Partition total Nodes: {}", task->TaskId(), metas_ptr->craned_ids.size()); - return std::unexpected(crane::grpc::ErrCode::ERR_INVALID_NODE_NUM); + return std::unexpected(crane::grpc::ErrCode::ERR_INVALID_NODE_NUM); } auto craned_meta_map = g_meta_container->GetCranedMetaMapConstPtr(); @@ -2832,7 +2839,7 @@ CraneErrCodeExpected TaskScheduler::CheckTaskValidity(TaskInCtld* task) { "Resource not enough. Task #{} needs {} nodes, while only {} " "nodes satisfy its requirement.", task->TaskId(), task->node_num, avail_nodes.size()); - return std::unexpected(crane::grpc::ErrCode::ERR_NO_ENOUGH_NODE); + return std::unexpected(crane::grpc::ErrCode::ERR_NO_ENOUGH_NODE); } return {}; diff --git a/src/Utilities/PublicHeader/include/crane/PublicHeader.h b/src/Utilities/PublicHeader/include/crane/PublicHeader.h index 5d9a5dd0d..2771bb2c8 100644 --- a/src/Utilities/PublicHeader/include/crane/PublicHeader.h +++ b/src/Utilities/PublicHeader/include/crane/PublicHeader.h @@ -151,70 +151,72 @@ constexpr std::array "Not enough nodes which satisfy resource requirements", }; -const std::unordered_map ErrCodeStrMap = { - {crane::grpc::ErrCode::SUCCESS, "Success"}, - {crane::grpc::ErrCode::ERR_INVALID_UID, "Invalid UID"}, - {crane::grpc::ErrCode::ERR_INVALID_OP_USER, "You are not a user of Crane"}, - {crane::grpc::ErrCode::ERR_INVALID_USER, "The entered user is not a user of Crane"}, - {crane::grpc::ErrCode::ERR_PERMISSION_USER, "Your permission is insufficient"}, - {crane::grpc::ErrCode::ERR_USER_DUPLICATE_ACCOUNT, "The user already exists in this account"}, - {crane::grpc::ErrCode::ERR_USER_ALLOWED_ACCOUNT, "he user is not allowed to access account"}, - {crane::grpc::ErrCode::ERR_INVALID_ADMIN_LEVEL, "Unknown admin level"}, - {crane::grpc::ErrCode::ERR_USER_ACCOUNT_MISMATCH, "The user does not belong to this account"}, - {crane::grpc::ErrCode::ERR_NO_ACCOUNT_SPECIFIED, "No account is specified for the user"}, - {crane::grpc::ErrCode::ERR_INVALID_ACCOUNT, "The entered account does not exist"}, - {crane::grpc::ErrCode::ERR_DUPLICATE_ACCOUNT, "The account already exists in the crane"}, - {crane::grpc::ErrCode::ERR_INVALID_PARENTACCOUNT, "The parent account of the entered account does not exist"}, - {crane::grpc::ErrCode::ERR_DELETE_ACCOUNT, "The account has child account or users, unable to delete"}, - {crane::grpc::ErrCode::ERR_INVALID_PARTITION, "The entered partition does not exist"}, - {crane::grpc::ErrCode::ERR_ALLOWED_PARTITION, "The entered account or user does not include this partition"}, - {crane::grpc::ErrCode::ERR_DUPLICATE_PARTITION, "The partition already exists in the account or user"}, - {crane::grpc::ErrCode::ERR_PARENT_ALLOWED_PARTITION, "Parent account does not include the partition"}, - {crane::grpc::ErrCode::ERR_USER_EMPTY_PARTITION, "The user does not contain any partitions, operation cannot be performed"}, - {crane::grpc::ErrCode::ERR_CHILD_HAS_PARTITION, "Child has partiton error"}, - {crane::grpc::ErrCode::ERR_HAS_NO_QOS_IN_PARTITION, "The user has no QoS available for this partition to be used"}, - {crane::grpc::ErrCode::ERR_HAS_ALLOWED_QOS_IN_PARTITION, "The qos you set is not in partition's allowed qos list"}, - {crane::grpc::ErrCode::ERR_INVALID_QOS, "The entered qos does not exist"}, - {crane::grpc::ErrCode::ERR_DB_DUPLICATE_QOS, "Qos already exists in the crane"}, - {crane::grpc::ErrCode::ERR_DELETE_QOS, "QoS is still being used by accounts or users, unable to delete"}, - {crane::grpc::ErrCode::ERR_CONVERT_TO_INTERGER, "Failed to convert value to integer"}, - {crane::grpc::ErrCode::ERR_TIME_LIMIT, "Invalid time limit value"}, - {crane::grpc::ErrCode::ERR_ALLOWED_QOS, "The entered account or user does not include this qos"}, - {crane::grpc::ErrCode::ERR_DUPLICATE_QOS, "The Qos already exists in the account or user"}, - {crane::grpc::ErrCode::ERR_PARENT_ALLOWED_QOS, "Parent account does not include the qos"}, - {crane::grpc::ErrCode::ERR_SET_ALLOWED_QOS, "Set allowed qos error"}, - {crane::grpc::ErrCode::ERR_ALLOWED_DEFAULT_QOS, "The entered default_qos is not allowed"}, - {crane::grpc::ErrCode::ERR_DUPLICATE_DEFAULT_QOS, "The QoS is already the default QoS for the account or specified partition of the user"}, - {crane::grpc::ErrCode::ERR_CHILD_HAS_DEFAULT_QOS, "child accounts has default error"}, - {crane::grpc::ErrCode::ERR_SET_ACCOUNT_QOS, "set account qos error"}, - {crane::grpc::ErrCode::ERR_SET_DEFAULT_QOS, "The Qos not allowed or is already the default qos"}, - {crane::grpc::ErrCode::ERR_IS_DEFAULT_QOS, "Is default qos error"}, - {crane::grpc::ErrCode::ERR_UPDATE_DATABASE, "Fail to update data in database"}, - {crane::grpc::ErrCode::ERR_GENERIC_FAILURE, "Generic Failure"}, - {crane::grpc::ErrCode::ERR_NO_RESOURCE, "Resource not enough for task"}, - {crane::grpc::ErrCode::ERR_NON_EXISTENT, "Non-existent Error"}, - {crane::grpc::ErrCode::ERR_INVALID_NODE_NUM, "Nodes partition not enough for task"}, - {crane::grpc::ErrCode::ERR_INVAILD_NODE_LIST, "Invalid node list"}, - {crane::grpc::ErrCode::ERR_INVAILD_EX_NODE_LIST, "Invalid exclude node list"}, - {crane::grpc::ErrCode::ERR_TIME_TIMIT_BEYOND, "Time-limit reached the user's limit"}, - {crane::grpc::ErrCode::ERR_CPUS_PER_TASK_BEYOND, "cpus-per-task reached the user's limit"}, - {crane::grpc::ErrCode::ERR_NO_ENOUGH_NODE, "Nodes num not enough for task"}, - {crane::grpc::ErrCode::ERR_SYSTEM_ERR, "System Error"}, - {crane::grpc::ErrCode::ERR_EXISTING_TASK, "Existing Task"}, - {crane::grpc::ErrCode::ERR_BEYOND_TASK_ID, "System error occurred or the number of pending tasks exceeded maximum value"}, - {crane::grpc::ErrCode::ERR_INVALID_PARAM, "Invalid Parameter"}, - {crane::grpc::ErrCode::ERR_STOP, "Stop Error"}, - {crane::grpc::ErrCode::ERR_PERMISSION_DENIED, "Permission Denied"}, - {crane::grpc::ErrCode::ERR_CONNECTION_TIMEOUT, "Connection Timeout"}, - {crane::grpc::ErrCode::ERR_CONNECTION_ABORTED, "Connection Aborted"}, - {crane::grpc::ErrCode::ERR_RPC_FAILURE, "RPC Failure"}, - {crane::grpc::ErrCode::ERR_TOKEN_REQUEST_FAILURE, "Token Request Failure"}, - {crane::grpc::ErrCode::ERR_STREAM_BROKEN, "Stream Broken"}, - {crane::grpc::ErrCode::ERR_INVALID_STUB, "Invalid Stub"}, - {crane::grpc::ErrCode::ERR_CGROUP, "CGroup Error"}, - {crane::grpc::ErrCode::ERR_PROTOBUF, "Protobuf Error"}, - {crane::grpc::ErrCode::ERR_LIB_EVENT, "Lib Event Error"}, - {crane::grpc::ErrCode::ERR_NO_AVAIL_NODE, "No Available Node"} +constexpr std::array ErrCodeStrArray = { + "Success", + "Invalid UID", + "You are not a user of Crane", + "The entered user is not a user of Crane", + "Your permission is insufficient", + "The user has been blocked", + "The user already exists in this account", + "he user is not allowed to access account", + "Unknown admin level", + "The user does not belong to this account", + "No account is specified for the user", + "The entered account does not exist", + "The account already exists in the crane", + "The parent account of the entered account does not exist", + "The account has child account or users, unable to delete", + "The account has been blocked", + "The entered partition does not exist", + "The entered account or user does not include this partition", + "The partition already exists in the account or user", + "Parent account does not include the partition", + "The user does not contain any partitions, operation cannot be performed", + "Child has partiton error", + "The user has no QoS available for this partition to be used", + "The qos you set is not in partition's allowed qos list", + "The entered qos does not exist", + "Qos already exists in the crane", + "QoS is still being used by accounts or users, unable to delete", + "Failed to convert value to integer", + "Invalid time limit value", + "The entered account or user does not include this qos", + "The Qos already exists in the account or user", + "Parent account does not include the qos", + "Set allowed qos error", + "The entered default_qos is not allowed", + "The QoS is already the default QoS for the account or specified partition of the user", + "child accounts has default error", + "set account qos error", + "The Qos not allowed or is already the default qos", + "Is default qos error", + "Fail to update data in database", + "Generic Failure", + "Resource not enough for task", + "Non-existent Error", + "Nodes partition not enough for task", + "Invalid node list", + "Invalid exclude node list", + "Time-limit reached the user's limit", + "cpus-per-task reached the user's limit", + "Nodes num not enough for task", + "System Error", + "Existing Task", + "System error occurred or the number of pending tasks exceeded maximum value", + "Invalid Parameter", + "Stop Error", + "Permission Denied", + "Connection Timeout", + "Connection Aborted", + "RPC Failure", + "Token Request Failure", + "Stream Broken", + "Invalid Stub", + "CGroup Error", + "Protobuf Error", + "Lib Event Error", + "No Available Node" }; } @@ -224,11 +226,7 @@ inline std::string_view CraneErrStr(CraneErr err) { } inline std::string_view CraneErrCodeStr(crane::grpc::ErrCode err) { - auto it = Internal::ErrCodeStrMap.find(err); - if (it != Internal::ErrCodeStrMap.end()) { - return it->second; - } - return "Unknown Error"; + return Internal::CraneErrStrArr[uint16_t(err)]; } /* ----------- Public definitions for all components */ From df6ab4c270d1181f330869ed1c6b2f21396a407c Mon Sep 17 00:00:00 2001 From: db <1301189887@qq.com> Date: Mon, 16 Dec 2024 10:44:26 +0800 Subject: [PATCH 7/8] fix conflicts --- src/CraneCtld/AccountManager.cpp | 25 +++++++++++-------------- src/CraneCtld/AccountManager.h | 2 +- 2 files changed, 12 insertions(+), 15 deletions(-) diff --git a/src/CraneCtld/AccountManager.cpp b/src/CraneCtld/AccountManager.cpp index 091092b4c..5adea1216 100644 --- a/src/CraneCtld/AccountManager.cpp +++ b/src/CraneCtld/AccountManager.cpp @@ -907,8 +907,7 @@ CraneErrCodeExpected AccountManager::CheckIfUserOfAccountIsEnabled( do { const Account* account_ptr = GetExistedAccountInfoNoLock_(account_name); if (account_ptr->blocked) { - CRANE_ERROR("Ancestor account '{}' is blocked", - account_ptr->name); + CRANE_ERROR("Ancestor account '{}' is blocked", account_ptr->name); return std::unexpected(crane::grpc::ErrCode::ERR_BLOCKED_ACCOUNT); } account_name = account_ptr->parent_account; @@ -922,7 +921,7 @@ CraneErrCodeExpected AccountManager::CheckIfUserOfAccountIsEnabled( return {}; } -AccountManager::CraneExpected AccountManager::CheckAndApplyQosLimitOnTask( +CraneErrCodeExpected AccountManager::CheckAndApplyQosLimitOnTask( const std::string& user, const std::string& account, TaskInCtld* task) { util::read_lock_guard user_guard(m_rw_user_mutex_); util::read_lock_guard qos_guard(m_rw_qos_mutex_); @@ -930,7 +929,7 @@ AccountManager::CraneExpected AccountManager::CheckAndApplyQosLimitOnTask( const User* user_share_ptr = GetExistedUserInfoNoLock_(user); if (!user_share_ptr) { CRANE_ERROR("Unknown user {}", user); - return std::unexpected(CraneErrCode::ERR_INVALID_OP_USER); + return std::unexpected(crane::grpc::ErrCode::ERR_INVALID_OP_USER); } if (task->uid != 0) { @@ -940,26 +939,24 @@ AccountManager::CraneExpected AccountManager::CheckAndApplyQosLimitOnTask( .allowed_partition_qos_map.end()) { CRANE_ERROR("Partition is not allowed for this user"); - return std::unexpected(CraneErrCode::ERR_ALLOWED_PARTITION); + return std::unexpected(crane::grpc::ErrCode::ERR_ALLOWED_PARTITION); } if (task->qos.empty()) { // Default qos task->qos = partition_it->second.first; if (task->qos.empty()) { CRANE_ERROR( - "CheckAndApplyQosLimitOnTask error: The user '{}' has no QOS " - "available for this partition '{}' to be used", + "The user '{}' has no QOS available for this partition '{}' to be used", task->Username(), task->partition_id); - return std::unexpected(CraneErrCode::ERR_HAS_NO_QOS_IN_PARTITION); + return std::unexpected(crane::grpc::ErrCode::ERR_HAS_NO_QOS_IN_PARTITION); } } else { // Check whether task.qos in the qos list if (!ranges::contains(partition_it->second.second, task->qos)) { CRANE_ERROR( - "CheckAndApplyQosLimitOnTask error: The qos '{}'" - " you set is not in partition's allowed qos list", + "The qos '{}' you set is not in partition's allowed qos list", task->qos); - return std::unexpected(CraneErrCode::ERR_HAS_ALLOWED_QOS_IN_PARTITION); + return std::unexpected(crane::grpc::ErrCode::ERR_HAS_ALLOWED_QOS_IN_PARTITION); } } } else { @@ -971,7 +968,7 @@ AccountManager::CraneExpected AccountManager::CheckAndApplyQosLimitOnTask( const Qos* qos_share_ptr = GetExistedQosInfoNoLock_(task->qos); if (!qos_share_ptr) { CRANE_ERROR("Unknown QOS '{}'", task->qos); - return std::unexpected(CraneErrCode::ERR_INVALID_QOS); + return std::unexpected(crane::grpc::ErrCode::ERR_INVALID_QOS); } task->qos_priority = qos_share_ptr->priority; @@ -980,13 +977,13 @@ AccountManager::CraneExpected AccountManager::CheckAndApplyQosLimitOnTask( task->time_limit = qos_share_ptr->max_time_limit_per_task; } else if (task->time_limit > qos_share_ptr->max_time_limit_per_task) { CRANE_ERROR("time-limit reached the user's limit"); - return std::unexpected(CraneErrCode::ERR_TIME_TIMIT_BEYOND); + return std::unexpected(crane::grpc::ErrCode::ERR_TIME_TIMIT_BEYOND); } if (static_cast(task->cpus_per_task) > qos_share_ptr->max_cpus_per_user) { CRANE_ERROR("cpus-per-task reached the user's limit"); - return std::unexpected(CraneErrCode::ERR_CPUS_PER_TASK_BEYOND); + return std::unexpected(crane::grpc::ErrCode::ERR_CPUS_PER_TASK_BEYOND); } return {}; diff --git a/src/CraneCtld/AccountManager.h b/src/CraneCtld/AccountManager.h index ef5fbd497..d12ad6f2e 100644 --- a/src/CraneCtld/AccountManager.h +++ b/src/CraneCtld/AccountManager.h @@ -132,7 +132,7 @@ class AccountManager { CraneErrCodeExpected CheckIfUserOfAccountIsEnabled( const std::string& user, const std::string& account); - CraneExpected CheckAndApplyQosLimitOnTask(const std::string& user, + CraneErrCodeExpected CheckAndApplyQosLimitOnTask(const std::string& user, const std::string& account, TaskInCtld* task); From b0055fd3a5c960b472aecae6881e599230002b68 Mon Sep 17 00:00:00 2001 From: db <1301189887@qq.com> Date: Mon, 16 Dec 2024 14:55:04 +0800 Subject: [PATCH 8/8] fix account enum --- src/CraneCtld/AccountManager.cpp | 172 +++++++++--------- src/CraneCtld/AccountManager.h | 136 +++++++------- src/CraneCtld/CtldGrpcServer.cpp | 18 +- .../PublicHeader/include/crane/PublicHeader.h | 8 +- 4 files changed, 166 insertions(+), 168 deletions(-) diff --git a/src/CraneCtld/AccountManager.cpp b/src/CraneCtld/AccountManager.cpp index 5adea1216..62d3d177e 100644 --- a/src/CraneCtld/AccountManager.cpp +++ b/src/CraneCtld/AccountManager.cpp @@ -25,9 +25,9 @@ namespace Ctld { AccountManager::AccountManager() { InitDataMap_(); } -AccountManager::CraneExpected AccountManager::AddUser( +CraneErrCodeExpected AccountManager::AddUser( uint32_t uid, const User& new_user) { - CraneExpected result; + CraneErrCodeExpected result; util::write_lock_guard user_guard(m_rw_user_mutex_); util::write_lock_guard account_guard(m_rw_account_mutex_); @@ -69,12 +69,12 @@ AccountManager::CraneExpected AccountManager::AddUser( return AddUser_(new_user, account, stale_user); } -AccountManager::CraneExpected AccountManager::AddAccount( +CraneErrCodeExpected AccountManager::AddAccount( uint32_t uid, const Account& new_account) { { util::read_lock_guard user_guard(m_rw_user_mutex_); util::read_lock_guard account_guard(m_rw_account_mutex_); - CraneExpected result; + CraneErrCodeExpected result; auto user_result = GetUserInfoByUidNoLock_(uid); if (!user_result) return std::unexpected(user_result.error()); const User* op_user = user_result.value(); @@ -142,7 +142,7 @@ AccountManager::CraneExpected AccountManager::AddAccount( return AddAccount_(new_account, find_parent, stale_account); } -AccountManager::CraneExpected AccountManager::AddQos(uint32_t uid, +CraneErrCodeExpected AccountManager::AddQos(uint32_t uid, const Qos& new_qos) { { util::read_lock_guard user_guard(m_rw_user_mutex_); @@ -165,7 +165,7 @@ AccountManager::CraneExpected AccountManager::AddQos(uint32_t uid, return AddQos_(new_qos, find_qos); } -AccountManager::CraneExpected AccountManager::DeleteUser( +CraneErrCodeExpected AccountManager::DeleteUser( uint32_t uid, const std::string& name, const std::string& account) { util::write_lock_guard user_guard(m_rw_user_mutex_); util::write_lock_guard account_guard(m_rw_account_mutex_); @@ -187,7 +187,7 @@ AccountManager::CraneExpected AccountManager::DeleteUser( return DeleteUser_(*user, account); } -AccountManager::CraneExpected AccountManager::DeleteAccount( +CraneErrCodeExpected AccountManager::DeleteAccount( uint32_t uid, const std::string& name) { { util::read_lock_guard user_guard(m_rw_user_mutex_); @@ -214,7 +214,7 @@ AccountManager::CraneExpected AccountManager::DeleteAccount( return DeleteAccount_(*account); } -AccountManager::CraneExpected AccountManager::DeleteQos( +CraneErrCodeExpected AccountManager::DeleteQos( uint32_t uid, const std::string& name) { { util::read_lock_guard user_guard(m_rw_user_mutex_); @@ -310,11 +310,11 @@ AccountManager::QosMapMutexSharedPtr AccountManager::GetAllQosInfo() { return QosMapMutexSharedPtr{&m_qos_map_, &m_rw_qos_mutex_}; } -AccountManager::CraneExpected AccountManager::QueryUserInfo( +CraneErrCodeExpected AccountManager::QueryUserInfo( uint32_t uid, const std::string& name, std::unordered_map* res_user_map) { util::read_lock_guard user_guard(m_rw_user_mutex_); - CraneExpected result{}; + CraneErrCodeExpected result{}; auto user_result = GetUserInfoByUidNoLock_(uid); if (!user_result) return std::unexpected(user_result.error()); @@ -358,11 +358,11 @@ AccountManager::CraneExpected AccountManager::QueryUserInfo( return result; } -AccountManager::CraneExpected AccountManager::QueryAccountInfo( +CraneErrCodeExpected AccountManager::QueryAccountInfo( uint32_t uid, const std::string& name, std::unordered_map* res_account_map) { User res_user; - CraneExpected result{}; + CraneErrCodeExpected result{}; { util::read_lock_guard user_guard(m_rw_user_mutex_); @@ -422,11 +422,11 @@ AccountManager::CraneExpected AccountManager::QueryAccountInfo( return result; } -AccountManager::CraneExpected AccountManager::QueryQosInfo( +CraneErrCodeExpected AccountManager::QueryQosInfo( uint32_t uid, const std::string& name, std::unordered_map* res_qos_map) { User res_user; - CraneExpected result{}; + CraneErrCodeExpected result{}; { util::read_lock_guard user_guard(m_rw_user_mutex_); auto user_result = GetUserInfoByUidNoLock_(uid); @@ -474,7 +474,7 @@ AccountManager::CraneExpected AccountManager::QueryQosInfo( return result; } -AccountManager::CraneExpected AccountManager::ModifyAdminLevel( +CraneErrCodeExpected AccountManager::ModifyAdminLevel( uint32_t uid, const std::string& name, const std::string& value) { util::write_lock_guard user_guard(m_rw_user_mutex_); @@ -506,13 +506,13 @@ AccountManager::CraneExpected AccountManager::ModifyAdminLevel( return SetUserAdminLevel_(name, new_level); } -AccountManager::CraneExpected AccountManager::ModifyUserDefaultQos( +CraneErrCodeExpected AccountManager::ModifyUserDefaultQos( uint32_t uid, const std::string& name, const std::string& partition, const std::string& account, const std::string& value) { util::write_lock_guard user_guard(m_rw_user_mutex_); const User* p_target_user = GetExistedUserInfoNoLock_(name); - CraneExpected result{}; + CraneErrCodeExpected result{}; // Account might be empty. In that case, ues user->default_account. std::string actual_account = account; { @@ -532,7 +532,7 @@ AccountManager::CraneExpected AccountManager::ModifyUserDefaultQos( return SetUserDefaultQos_(*p_target_user, actual_account, partition, value); } -AccountManager::CraneExpected AccountManager::ModifyUserAllowedPartition( +CraneErrCodeExpected AccountManager::ModifyUserAllowedPartition( crane::grpc::OperationType operation_type, uint32_t uid, const std::string& name, const std::string& account, const std::string& value) { @@ -540,7 +540,7 @@ AccountManager::CraneExpected AccountManager::ModifyUserAllowedPartition( util::read_lock_guard account_guard(m_rw_account_mutex_); const User* p = GetExistedUserInfoNoLock_(name); - CraneExpected result{}; + CraneErrCodeExpected result{}; std::string actual_account = account; auto user_result = GetUserInfoByUidNoLock_(uid); if (!user_result) return std::unexpected(user_result.error()); @@ -565,7 +565,7 @@ AccountManager::CraneExpected AccountManager::ModifyUserAllowedPartition( return result; } -AccountManager::CraneExpected AccountManager::ModifyUserAllowedQos( +CraneErrCodeExpected AccountManager::ModifyUserAllowedQos( crane::grpc::OperationType operation_type, uint32_t uid, const std::string& name, const std::string& partition, const std::string& account, const std::string& value, bool force) { @@ -574,7 +574,7 @@ AccountManager::CraneExpected AccountManager::ModifyUserAllowedQos( util::read_lock_guard qos_guard(m_rw_qos_mutex_); const User* p = GetExistedUserInfoNoLock_(name); - CraneExpected result{}; + CraneErrCodeExpected result{}; std::string actual_account = account; auto user_result = GetUserInfoByUidNoLock_(uid); if (!user_result) return std::unexpected(user_result.error()); @@ -603,13 +603,13 @@ AccountManager::CraneExpected AccountManager::ModifyUserAllowedQos( return result; } -AccountManager::CraneExpected AccountManager::DeleteUserAllowedPartition( +CraneErrCodeExpected AccountManager::DeleteUserAllowedPartition( uint32_t uid, const std::string& name, const std::string& account, const std::string& value) { util::write_lock_guard user_guard(m_rw_user_mutex_); const User* p = GetExistedUserInfoNoLock_(name); - CraneExpected result; + CraneErrCodeExpected result; // Account might be empty. In that case, ues user->default_account. std::string actual_account = account; { @@ -628,13 +628,13 @@ AccountManager::CraneExpected AccountManager::DeleteUserAllowedPartition( return DeleteUserAllowedPartition_(*p, actual_account, value); } -AccountManager::CraneExpected AccountManager::DeleteUserAllowedQos( +CraneErrCodeExpected AccountManager::DeleteUserAllowedQos( uint32_t uid, const std::string& name, const std::string& partition, const std::string& account, const std::string& value, bool force) { util::write_lock_guard user_guard(m_rw_user_mutex_); const User* p = GetExistedUserInfoNoLock_(name); - CraneExpected result; + CraneErrCodeExpected result; std::string actual_account = account; { util::read_lock_guard account_guard(m_rw_account_mutex_); @@ -653,11 +653,11 @@ AccountManager::CraneExpected AccountManager::DeleteUserAllowedQos( return DeleteUserAllowedQos_(*p, value, actual_account, partition, force); } -AccountManager::CraneExpected AccountManager::ModifyAccount( +CraneErrCodeExpected AccountManager::ModifyAccount( crane::grpc::OperationType operation_type, uint32_t uid, const std::string& name, crane::grpc::ModifyField modify_field, const std::string& value, bool force) { - CraneExpected result{}; + CraneErrCodeExpected result{}; { util::read_lock_guard user_guard(m_rw_user_mutex_); @@ -757,7 +757,7 @@ AccountManager::CraneExpected AccountManager::ModifyAccount( return result; } -AccountManager::CraneExpected AccountManager::ModifyQos( +CraneErrCodeExpected AccountManager::ModifyQos( uint32_t uid, const std::string& name, crane::grpc::ModifyField modify_field, const std::string& value) { { @@ -837,7 +837,7 @@ AccountManager::CraneExpected AccountManager::ModifyQos( return {}; } -AccountManager::CraneExpected AccountManager::BlockAccount( +CraneErrCodeExpected AccountManager::BlockAccount( uint32_t uid, const std::string& name, bool block) { { util::read_lock_guard user_guard(m_rw_user_mutex_); @@ -861,7 +861,7 @@ AccountManager::CraneExpected AccountManager::BlockAccount( return BlockAccount_(name, block); } -AccountManager::CraneExpected AccountManager::BlockUser( +CraneErrCodeExpected AccountManager::BlockUser( uint32_t uid, const std::string& name, const std::string& account, bool block) { util::write_lock_guard user_guard(m_rw_user_mutex_); @@ -908,7 +908,7 @@ CraneErrCodeExpected AccountManager::CheckIfUserOfAccountIsEnabled( const Account* account_ptr = GetExistedAccountInfoNoLock_(account_name); if (account_ptr->blocked) { CRANE_ERROR("Ancestor account '{}' is blocked", account_ptr->name); - return std::unexpected(crane::grpc::ErrCode::ERR_BLOCKED_ACCOUNT); + return std::unexpected(CraneErrCode::ERR_BLOCKED_ACCOUNT); } account_name = account_ptr->parent_account; } while (!account_name.empty()); @@ -916,7 +916,7 @@ CraneErrCodeExpected AccountManager::CheckIfUserOfAccountIsEnabled( const User* user_ptr = GetExistedUserInfoNoLock_(user); if (user_ptr->account_to_attrs_map.at(account).blocked) { CRANE_ERROR("User '{}' is blocked", user_ptr->name); - return std::unexpected(crane::grpc::ErrCode::ERR_BLOCKED_USER); + return std::unexpected(CraneErrCode::ERR_BLOCKED_USER); } return {}; } @@ -929,7 +929,7 @@ CraneErrCodeExpected AccountManager::CheckAndApplyQosLimitOnTask( const User* user_share_ptr = GetExistedUserInfoNoLock_(user); if (!user_share_ptr) { CRANE_ERROR("Unknown user {}", user); - return std::unexpected(crane::grpc::ErrCode::ERR_INVALID_OP_USER); + return std::unexpected(CraneErrCode::ERR_INVALID_OP_USER); } if (task->uid != 0) { @@ -939,7 +939,7 @@ CraneErrCodeExpected AccountManager::CheckAndApplyQosLimitOnTask( .allowed_partition_qos_map.end()) { CRANE_ERROR("Partition is not allowed for this user"); - return std::unexpected(crane::grpc::ErrCode::ERR_ALLOWED_PARTITION); + return std::unexpected(CraneErrCode::ERR_ALLOWED_PARTITION); } if (task->qos.empty()) { // Default qos @@ -948,7 +948,7 @@ CraneErrCodeExpected AccountManager::CheckAndApplyQosLimitOnTask( CRANE_ERROR( "The user '{}' has no QOS available for this partition '{}' to be used", task->Username(), task->partition_id); - return std::unexpected(crane::grpc::ErrCode::ERR_HAS_NO_QOS_IN_PARTITION); + return std::unexpected(CraneErrCode::ERR_HAS_NO_QOS_IN_PARTITION); } } else { // Check whether task.qos in the qos list @@ -956,7 +956,7 @@ CraneErrCodeExpected AccountManager::CheckAndApplyQosLimitOnTask( CRANE_ERROR( "The qos '{}' you set is not in partition's allowed qos list", task->qos); - return std::unexpected(crane::grpc::ErrCode::ERR_HAS_ALLOWED_QOS_IN_PARTITION); + return std::unexpected(CraneErrCode::ERR_HAS_ALLOWED_QOS_IN_PARTITION); } } } else { @@ -968,7 +968,7 @@ CraneErrCodeExpected AccountManager::CheckAndApplyQosLimitOnTask( const Qos* qos_share_ptr = GetExistedQosInfoNoLock_(task->qos); if (!qos_share_ptr) { CRANE_ERROR("Unknown QOS '{}'", task->qos); - return std::unexpected(crane::grpc::ErrCode::ERR_INVALID_QOS); + return std::unexpected(CraneErrCode::ERR_INVALID_QOS); } task->qos_priority = qos_share_ptr->priority; @@ -977,13 +977,13 @@ CraneErrCodeExpected AccountManager::CheckAndApplyQosLimitOnTask( task->time_limit = qos_share_ptr->max_time_limit_per_task; } else if (task->time_limit > qos_share_ptr->max_time_limit_per_task) { CRANE_ERROR("time-limit reached the user's limit"); - return std::unexpected(crane::grpc::ErrCode::ERR_TIME_TIMIT_BEYOND); + return std::unexpected(CraneErrCode::ERR_TIME_TIMIT_BEYOND); } if (static_cast(task->cpus_per_task) > qos_share_ptr->max_cpus_per_user) { CRANE_ERROR("cpus-per-task reached the user's limit"); - return std::unexpected(crane::grpc::ErrCode::ERR_CPUS_PER_TASK_BEYOND); + return std::unexpected(CraneErrCode::ERR_CPUS_PER_TASK_BEYOND); } return {}; @@ -1003,7 +1003,7 @@ std::expected AccountManager::CheckUidIsAdmin( return std::unexpected("User has insufficient privilege."); } -AccountManager::CraneExpected AccountManager::CheckIfUidHasPermOnUser( +CraneErrCodeExpected AccountManager::CheckIfUidHasPermOnUser( uint32_t uid, const std::string& username, bool read_only_priv) { util::read_lock_guard user_guard(m_rw_user_mutex_); util::read_lock_guard account_guard(m_rw_account_mutex_); @@ -1017,7 +1017,7 @@ AccountManager::CraneExpected AccountManager::CheckIfUidHasPermOnUser( return CheckIfUserHasPermOnUserNoLock_(*op_user, user, read_only_priv); } -AccountManager::CraneExpected +CraneErrCodeExpected AccountManager::CheckAddUserAllowedPartitionNoLock_( const User* user, const Account* account, const std::string& partition) { auto result = CheckPartitionIsAllowedNoLock_(account, partition, false, true); @@ -1032,7 +1032,7 @@ AccountManager::CheckAddUserAllowedPartitionNoLock_( return {}; } -AccountManager::CraneExpected +CraneErrCodeExpected AccountManager::CheckSetUserAllowedPartitionNoLock_( const Account* account, const std::string& partition) { auto result = CheckPartitionIsAllowedNoLock_(account, partition, false, true); @@ -1041,7 +1041,7 @@ AccountManager::CheckSetUserAllowedPartitionNoLock_( return {}; } -AccountManager::CraneExpected +CraneErrCodeExpected AccountManager::CheckAddUserAllowedQosNoLock_(const User* user, const Account* account, const std::string& partition, @@ -1079,7 +1079,7 @@ AccountManager::CheckAddUserAllowedQosNoLock_(const User* user, return {}; } -AccountManager::CraneExpected +CraneErrCodeExpected AccountManager::CheckSetUserAllowedQosNoLock_(const User* user, const Account* account, const std::string& partition, @@ -1116,7 +1116,7 @@ AccountManager::CheckSetUserAllowedQosNoLock_(const User* user, return {}; } -AccountManager::CraneExpected +CraneErrCodeExpected AccountManager::CheckSetUserDefaultQosNoLock_(const User& user, const std::string& account, const std::string& partition, @@ -1148,7 +1148,7 @@ AccountManager::CheckSetUserDefaultQosNoLock_(const User& user, return {}; } -AccountManager::CraneExpected +CraneErrCodeExpected AccountManager::CheckDeleteUserAllowedPartitionNoLock_( const User& user, const std::string& account, const std::string& partition) { @@ -1159,7 +1159,7 @@ AccountManager::CheckDeleteUserAllowedPartitionNoLock_( return {}; } -AccountManager::CraneExpected +CraneErrCodeExpected AccountManager::CheckDeleteUserAllowedQosNoLock_(const User& user, const std::string& account, const std::string& partition, @@ -1193,7 +1193,7 @@ AccountManager::CheckDeleteUserAllowedQosNoLock_(const User& user, return {}; } -AccountManager::CraneExpected +CraneErrCodeExpected AccountManager::CheckAddAccountAllowedPartitionNoLock_( const Account* account, const std::string& partition) { auto result = CheckPartitionIsAllowedNoLock_(account, partition, true, false); @@ -1205,7 +1205,7 @@ AccountManager::CheckAddAccountAllowedPartitionNoLock_( return {}; } -AccountManager::CraneExpected +CraneErrCodeExpected AccountManager::CheckAddAccountAllowedQosNoLock_(const Account* account, const std::string& qos) { auto result = CheckQosIsAllowedNoLock_(account, qos, true, false); @@ -1217,14 +1217,14 @@ AccountManager::CheckAddAccountAllowedQosNoLock_(const Account* account, return {}; } -AccountManager::CraneExpected +CraneErrCodeExpected AccountManager::CheckSetAccountDescriptionNoLock_(const Account* account) { if (!account) return std::unexpected(CraneErrCode::ERR_INVALID_ACCOUNT); return {}; } -AccountManager::CraneExpected +CraneErrCodeExpected AccountManager::CheckSetAccountAllowedPartitionNoLock_( const Account* account, const std::string& partitions, bool force) { auto result = @@ -1244,7 +1244,7 @@ AccountManager::CheckSetAccountAllowedPartitionNoLock_( return {}; } -AccountManager::CraneExpected +CraneErrCodeExpected AccountManager::CheckSetAccountAllowedQosNoLock_(const Account* account, const std::string& qos_list, bool force) { @@ -1264,7 +1264,7 @@ AccountManager::CheckSetAccountAllowedQosNoLock_(const Account* account, return {}; } -AccountManager::CraneExpected +CraneErrCodeExpected AccountManager::CheckSetAccountDefaultQosNoLock_(const Account* account, const std::string& qos) { auto result = CheckQosIsAllowedNoLock_(account, qos, false, false); @@ -1276,7 +1276,7 @@ AccountManager::CheckSetAccountDefaultQosNoLock_(const Account* account, return {}; } -AccountManager::CraneExpected +CraneErrCodeExpected AccountManager::CheckDeleteAccountAllowedPartitionNoLock_( const Account* account, const std::string& partition, bool force) { auto result = @@ -1289,7 +1289,7 @@ AccountManager::CheckDeleteAccountAllowedPartitionNoLock_( return {}; } -AccountManager::CraneExpected +CraneErrCodeExpected AccountManager::CheckDeleteAccountAllowedQosNoLock_(const Account* account, const std::string& qos, bool force) { @@ -1305,7 +1305,7 @@ AccountManager::CheckDeleteAccountAllowedQosNoLock_(const Account* account, return {}; } -AccountManager::CraneExpected +CraneErrCodeExpected AccountManager::CheckIfUserHasHigherPrivThan_(const User& op_user, User::AdminLevel admin_level) { if (op_user.admin_level <= admin_level) @@ -1314,7 +1314,7 @@ AccountManager::CheckIfUserHasHigherPrivThan_(const User& op_user, return {}; } -AccountManager::CraneExpected +CraneErrCodeExpected AccountManager::CheckIfUserHasPermOnAccountNoLock_(const User& op_user, const std::string& account, bool read_only_priv) { @@ -1346,7 +1346,7 @@ AccountManager::CheckIfUserHasPermOnAccountNoLock_(const User& op_user, return {}; } -AccountManager::CraneExpected +CraneErrCodeExpected AccountManager::CheckIfUserHasPermOnUserOfAccountNoLock_(const User& op_user, const User* user, std::string* account, @@ -1364,7 +1364,7 @@ AccountManager::CheckIfUserHasPermOnUserOfAccountNoLock_(const User& op_user, return CheckIfUserHasPermOnAccountNoLock_(op_user, *account, read_only_priv); } -AccountManager::CraneExpected +CraneErrCodeExpected AccountManager::CheckIfUserHasPermOnUserNoLock_(const User& op_user, const User* user, bool read_only_priv) { @@ -1382,7 +1382,7 @@ AccountManager::CheckIfUserHasPermOnUserNoLock_(const User& op_user, return std::unexpected(CraneErrCode::ERR_PERMISSION_USER); } -AccountManager::CraneExpected +CraneErrCodeExpected AccountManager::CheckPartitionIsAllowedNoLock_(const Account* account, const std::string& partition, bool check_parent, @@ -1419,7 +1419,7 @@ AccountManager::CheckPartitionIsAllowedNoLock_(const Account* account, return {}; } -AccountManager::CraneExpected AccountManager::CheckQosIsAllowedNoLock_( +CraneErrCodeExpected AccountManager::CheckQosIsAllowedNoLock_( const Account* account, const std::string& qos_str, bool check_parent, bool is_user) { if (!account) return std::unexpected(CraneErrCode::ERR_INVALID_ACCOUNT); @@ -1479,7 +1479,7 @@ void AccountManager::InitDataMap_() { } } -AccountManager::CraneExpected +CraneErrCodeExpected AccountManager::GetUserInfoByUidNoLock_(uint32_t uid) { PasswordEntry entry(uid); @@ -1549,7 +1549,7 @@ bool AccountManager::IncQosReferenceCountInDb_(const std::string& name, name, "reference_count", num); } -AccountManager::CraneExpected AccountManager::AddUser_( +CraneErrCodeExpected AccountManager::AddUser_( const User& user, const Account* account, const User* stale_user) { const std::string& object_account = user.default_account; const std::string& name = user.name; @@ -1627,7 +1627,7 @@ AccountManager::CraneExpected AccountManager::AddUser_( return {}; } -AccountManager::CraneExpected AccountManager::AddAccount_( +CraneErrCodeExpected AccountManager::AddAccount_( const Account& account, const Account* parent, const Account* stale_account) { const std::string& name = account.name; @@ -1692,7 +1692,7 @@ AccountManager::CraneExpected AccountManager::AddAccount_( return {}; } -AccountManager::CraneExpected AccountManager::AddQos_( +CraneErrCodeExpected AccountManager::AddQos_( const Qos& qos, const Qos* stale_qos) { if (stale_qos) { // There is a same qos but was deleted,here will delete the original @@ -1719,7 +1719,7 @@ AccountManager::CraneExpected AccountManager::AddQos_( return {}; } -AccountManager::CraneExpected AccountManager::DeleteUser_( +CraneErrCodeExpected AccountManager::DeleteUser_( const User& user, const std::string& account) { const std::string& name = user.name; @@ -1785,7 +1785,7 @@ AccountManager::CraneExpected AccountManager::DeleteUser_( return {}; } -AccountManager::CraneExpected AccountManager::DeleteAccount_( +CraneErrCodeExpected AccountManager::DeleteAccount_( const Account& account) { const std::string& name = account.name; @@ -1821,7 +1821,7 @@ AccountManager::CraneExpected AccountManager::DeleteAccount_( return {}; } -AccountManager::CraneExpected AccountManager::DeleteQos_( +CraneErrCodeExpected AccountManager::DeleteQos_( const std::string& name) { if (!g_db_client->UpdateEntityOne(MongodbClient::EntityType::QOS, "$set", name, "deleted", true)) { @@ -1832,7 +1832,7 @@ AccountManager::CraneExpected AccountManager::DeleteQos_( return {}; } -AccountManager::CraneExpected AccountManager::AddUserAllowedPartition_( +CraneErrCodeExpected AccountManager::AddUserAllowedPartition_( const User& user, const Account& account, const std::string& partition) { const std::string& name = user.name; const std::string& account_name = account.name; @@ -1864,7 +1864,7 @@ AccountManager::CraneExpected AccountManager::AddUserAllowedPartition_( return {}; } -AccountManager::CraneExpected AccountManager::AddUserAllowedQos_( +CraneErrCodeExpected AccountManager::AddUserAllowedQos_( const User& user, const Account& account, const std::string& partition, const std::string& qos) { const std::string& name = user.name; @@ -1913,7 +1913,7 @@ AccountManager::CraneExpected AccountManager::AddUserAllowedQos_( return {}; } -AccountManager::CraneExpected AccountManager::SetUserAdminLevel_( +CraneErrCodeExpected AccountManager::SetUserAdminLevel_( const std::string& name, User::AdminLevel new_level) { // Update to database mongocxx::client_session::with_transaction_cb callback = @@ -1932,7 +1932,7 @@ AccountManager::CraneExpected AccountManager::SetUserAdminLevel_( return {}; } -AccountManager::CraneExpected AccountManager::SetUserDefaultQos_( +CraneErrCodeExpected AccountManager::SetUserDefaultQos_( const User& user, const std::string& account, const std::string& partition, const std::string& qos) { const std::string& name = user.name; @@ -1969,7 +1969,7 @@ AccountManager::CraneExpected AccountManager::SetUserDefaultQos_( return {}; } -AccountManager::CraneExpected AccountManager::SetUserAllowedPartition_( +CraneErrCodeExpected AccountManager::SetUserAllowedPartition_( const User& user, const Account& account, const std::string& partitions) { const std::string& name = user.name; const std::string& account_name = account.name; @@ -2006,7 +2006,7 @@ AccountManager::CraneExpected AccountManager::SetUserAllowedPartition_( return {}; } -AccountManager::CraneExpected AccountManager::SetUserAllowedQos_( +CraneErrCodeExpected AccountManager::SetUserAllowedQos_( const User& user, const Account& account, const std::string& partition, const std::string& qos_list_str, bool force) { const std::string& name = user.name; @@ -2053,7 +2053,7 @@ AccountManager::CraneExpected AccountManager::SetUserAllowedQos_( return {}; } -AccountManager::CraneExpected AccountManager::DeleteUserAllowedPartition_( +CraneErrCodeExpected AccountManager::DeleteUserAllowedPartition_( const User& user, const std::string& account, const std::string& partition) { const std::string& name = user.name; @@ -2079,7 +2079,7 @@ AccountManager::CraneExpected AccountManager::DeleteUserAllowedPartition_( return {}; } -AccountManager::CraneExpected AccountManager::DeleteUserAllowedQos_( +CraneErrCodeExpected AccountManager::DeleteUserAllowedQos_( const User& user, const std::string& qos, const std::string& account, const std::string& partition, bool force) { const std::string& name = user.name; @@ -2127,7 +2127,7 @@ AccountManager::CraneExpected AccountManager::DeleteUserAllowedQos_( return {}; } -AccountManager::CraneExpected AccountManager::AddAccountAllowedPartition_( +CraneErrCodeExpected AccountManager::AddAccountAllowedPartition_( const std::string& name, const std::string& partition) { // Update to database mongocxx::client_session::with_transaction_cb callback = @@ -2145,7 +2145,7 @@ AccountManager::CraneExpected AccountManager::AddAccountAllowedPartition_( return {}; } -AccountManager::CraneExpected AccountManager::AddAccountAllowedQos_( +CraneErrCodeExpected AccountManager::AddAccountAllowedQos_( const Account& account, const std::string& qos) { const std::string& name = account.name; @@ -2175,7 +2175,7 @@ AccountManager::CraneExpected AccountManager::AddAccountAllowedQos_( return {}; } -AccountManager::CraneExpected AccountManager::SetAccountDescription_( +CraneErrCodeExpected AccountManager::SetAccountDescription_( const std::string& name, const std::string& description) { mongocxx::client_session::with_transaction_cb callback = [&](mongocxx::client_session* session) { @@ -2193,7 +2193,7 @@ AccountManager::CraneExpected AccountManager::SetAccountDescription_( return {}; } -AccountManager::CraneExpected AccountManager::SetAccountDefaultQos_( +CraneErrCodeExpected AccountManager::SetAccountDefaultQos_( const Account& account, const std::string& qos) { const std::string& name = account.name; @@ -2212,7 +2212,7 @@ AccountManager::CraneExpected AccountManager::SetAccountDefaultQos_( return {}; } -AccountManager::CraneExpected AccountManager::SetAccountAllowedPartition_( +CraneErrCodeExpected AccountManager::SetAccountAllowedPartition_( const Account& account, const std::string& partitions) { const std::string& name = account.name; @@ -2256,7 +2256,7 @@ AccountManager::CraneExpected AccountManager::SetAccountAllowedPartition_( return {}; } -AccountManager::CraneExpected AccountManager::SetAccountAllowedQos_( +CraneErrCodeExpected AccountManager::SetAccountAllowedQos_( const Account& account, const std::string& qos_list_str) { const std::string& name = account.name; @@ -2325,7 +2325,7 @@ AccountManager::CraneExpected AccountManager::SetAccountAllowedQos_( return {}; } -AccountManager::CraneExpected +CraneErrCodeExpected AccountManager::DeleteAccountAllowedPartition_(const Account& account, const std::string& partition) { mongocxx::client_session::with_transaction_cb callback = @@ -2342,7 +2342,7 @@ AccountManager::DeleteAccountAllowedPartition_(const Account& account, return {}; } -AccountManager::CraneExpected AccountManager::DeleteAccountAllowedQos_( +CraneErrCodeExpected AccountManager::DeleteAccountAllowedQos_( const Account& account, const std::string& qos) { int change_num; mongocxx::client_session::with_transaction_cb callback = @@ -2361,7 +2361,7 @@ AccountManager::CraneExpected AccountManager::DeleteAccountAllowedQos_( return {}; } -AccountManager::CraneExpected AccountManager::BlockUser_( +CraneErrCodeExpected AccountManager::BlockUser_( const std::string& name, const std::string& account, bool block) { mongocxx::client_session::with_transaction_cb callback = [&](mongocxx::client_session* session) { @@ -2379,7 +2379,7 @@ AccountManager::CraneExpected AccountManager::BlockUser_( return {}; } -AccountManager::CraneExpected AccountManager::BlockAccount_( +CraneErrCodeExpected AccountManager::BlockAccount_( const std::string& name, bool block) { mongocxx::client_session::with_transaction_cb callback = [&](mongocxx::client_session* session) { diff --git a/src/CraneCtld/AccountManager.h b/src/CraneCtld/AccountManager.h index d12ad6f2e..0bbc40151 100644 --- a/src/CraneCtld/AccountManager.h +++ b/src/CraneCtld/AccountManager.h @@ -40,37 +40,33 @@ class AccountManager { using QosMapMutexSharedPtr = util::ScopeConstSharedPtr< std::unordered_map>, util::rw_mutex>; - using CraneErrCode = crane::grpc::ErrCode; - - template - using CraneExpected = std::expected; AccountManager(); ~AccountManager() = default; - CraneExpected AddUser(uint32_t uid, const User& new_user); + CraneErrCodeExpected AddUser(uint32_t uid, const User& new_user); - CraneExpected AddAccount(uint32_t uid, const Account& new_account); + CraneErrCodeExpected AddAccount(uint32_t uid, const Account& new_account); - CraneExpected AddQos(uint32_t uid, const Qos& new_qos); + CraneErrCodeExpected AddQos(uint32_t uid, const Qos& new_qos); - CraneExpected DeleteUser(uint32_t uid, const std::string& name, + CraneErrCodeExpected DeleteUser(uint32_t uid, const std::string& name, const std::string& account); - CraneExpected DeleteAccount(uint32_t uid, const std::string& name); + CraneErrCodeExpected DeleteAccount(uint32_t uid, const std::string& name); - CraneExpected DeleteQos(uint32_t uid, const std::string& name); + CraneErrCodeExpected DeleteQos(uint32_t uid, const std::string& name); - CraneExpected QueryUserInfo( + CraneErrCodeExpected QueryUserInfo( uint32_t uid, const std::string& name, std::unordered_map* res_user_map); - CraneExpected QueryAccountInfo( + CraneErrCodeExpected QueryAccountInfo( uint32_t uid, const std::string& name, std::unordered_map* res_account_map); - CraneExpected QueryQosInfo( + CraneErrCodeExpected QueryQosInfo( uint32_t uid, const std::string& name, std::unordered_map* res_qos_map); @@ -87,42 +83,42 @@ class AccountManager { * ModifyUser-related functions * --------------------------------------------------------------------------- */ - CraneExpected ModifyAdminLevel(uint32_t uid, const std::string& name, + CraneErrCodeExpected ModifyAdminLevel(uint32_t uid, const std::string& name, const std::string& value); - CraneExpected ModifyUserDefaultQos(uint32_t uid, + CraneErrCodeExpected ModifyUserDefaultQos(uint32_t uid, const std::string& name, const std::string& partition, const std::string& account, const std::string& value); - CraneExpected ModifyUserAllowedPartition( + CraneErrCodeExpected ModifyUserAllowedPartition( crane::grpc::OperationType operation_type, uint32_t uid, const std::string& name, const std::string& account, const std::string& value); - CraneExpected ModifyUserAllowedQos( + CraneErrCodeExpected ModifyUserAllowedQos( crane::grpc::OperationType operation_type, uint32_t uid, const std::string& name, const std::string& partition, const std::string& account, const std::string& value, bool force); - CraneExpected DeleteUserAllowedPartition(uint32_t uid, + CraneErrCodeExpected DeleteUserAllowedPartition(uint32_t uid, const std::string& name, const std::string& account, const std::string& value); - CraneExpected DeleteUserAllowedQos( + CraneErrCodeExpected DeleteUserAllowedQos( uint32_t uid, const std::string& name, const std::string& partition, const std::string& account, const std::string& value, bool force); - CraneExpected ModifyAccount(crane::grpc::OperationType operation_type, + CraneErrCodeExpected ModifyAccount(crane::grpc::OperationType operation_type, uint32_t uid, const std::string& name, crane::grpc::ModifyField modify_field, const std::string& value, bool force); - CraneExpected ModifyQos(uint32_t uid, const std::string& name, + CraneErrCodeExpected ModifyQos(uint32_t uid, const std::string& name, crane::grpc::ModifyField modify_field, const std::string& value); - CraneExpected BlockAccount(uint32_t uid, const std::string& name, + CraneErrCodeExpected BlockAccount(uint32_t uid, const std::string& name, bool block); - CraneExpected BlockUser(uint32_t uid, const std::string& name, + CraneErrCodeExpected BlockUser(uint32_t uid, const std::string& name, const std::string& account, bool block); bool CheckUserPermissionToPartition(const std::string& name, @@ -138,14 +134,14 @@ class AccountManager { std::expected CheckUidIsAdmin(uint32_t uid); - CraneExpected CheckIfUidHasPermOnUser(uint32_t uid, + CraneErrCodeExpected CheckIfUidHasPermOnUser(uint32_t uid, const std::string& username, bool read_only_priv); private: void InitDataMap_(); - CraneExpected GetUserInfoByUidNoLock_(uint32_t uid); + CraneErrCodeExpected GetUserInfoByUidNoLock_(uint32_t uid); const User* GetUserInfoNoLock_(const std::string& name); const User* GetExistedUserInfoNoLock_(const std::string& name); @@ -160,23 +156,23 @@ class AccountManager { * ModifyUser-related functions(no lock) * --------------------------------------------------------------------------- */ - CraneExpected CheckAddUserAllowedPartitionNoLock_( + CraneErrCodeExpected CheckAddUserAllowedPartitionNoLock_( const User* user, const Account* account, const std::string& partition); - CraneExpected CheckSetUserAllowedPartitionNoLock_( + CraneErrCodeExpected CheckSetUserAllowedPartitionNoLock_( const Account* account, const std::string& partition); - CraneExpected CheckAddUserAllowedQosNoLock_( + CraneErrCodeExpected CheckAddUserAllowedQosNoLock_( const User* user, const Account* account, const std::string& partition, const std::string& qos_str); - CraneExpected CheckSetUserAllowedQosNoLock_( + CraneErrCodeExpected CheckSetUserAllowedQosNoLock_( const User* user, const Account* account, const std::string& partition, const std::string& qos_str, bool force); - CraneExpected CheckSetUserDefaultQosNoLock_( + CraneErrCodeExpected CheckSetUserDefaultQosNoLock_( const User& user, const std::string& account, const std::string& partition, const std::string& qos); - CraneExpected CheckDeleteUserAllowedPartitionNoLock_( + CraneErrCodeExpected CheckDeleteUserAllowedPartitionNoLock_( const User& user, const std::string& account, const std::string& partition); - CraneExpected CheckDeleteUserAllowedQosNoLock_( + CraneErrCodeExpected CheckDeleteUserAllowedQosNoLock_( const User& user, const std::string& account, const std::string& partition, const std::string& qos, bool force); @@ -184,29 +180,29 @@ class AccountManager { * ModifyAccount-related functions(no lock) * --------------------------------------------------------------------------- */ - CraneExpected CheckAddAccountAllowedPartitionNoLock_( + CraneErrCodeExpected CheckAddAccountAllowedPartitionNoLock_( const Account* account, const std::string& partition); - CraneExpected CheckAddAccountAllowedQosNoLock_(const Account* account, + CraneErrCodeExpected CheckAddAccountAllowedQosNoLock_(const Account* account, const std::string& qos); - CraneExpected CheckSetAccountDescriptionNoLock_(const Account* account); - CraneExpected CheckSetAccountAllowedPartitionNoLock_( + CraneErrCodeExpected CheckSetAccountDescriptionNoLock_(const Account* account); + CraneErrCodeExpected CheckSetAccountAllowedPartitionNoLock_( const Account* account, const std::string& partitions, bool force); - CraneExpected CheckSetAccountAllowedQosNoLock_( + CraneErrCodeExpected CheckSetAccountAllowedQosNoLock_( const Account* account, const std::string& qos_list, bool force); - CraneExpected CheckSetAccountDefaultQosNoLock_(const Account* account, + CraneErrCodeExpected CheckSetAccountDefaultQosNoLock_(const Account* account, const std::string& qos); - CraneExpected CheckDeleteAccountAllowedPartitionNoLock_( + CraneErrCodeExpected CheckDeleteAccountAllowedPartitionNoLock_( const Account* account, const std::string& partition, bool force); - CraneExpected CheckDeleteAccountAllowedQosNoLock_( + CraneErrCodeExpected CheckDeleteAccountAllowedQosNoLock_( const Account* account, const std::string& qos, bool force); // Compare the user's permission levels for operations. - CraneExpected CheckIfUserHasHigherPrivThan_( + CraneErrCodeExpected CheckIfUserHasHigherPrivThan_( const User& op_user, User::AdminLevel admin_level); // Determine if the operating user has permissions for the account, // e.g. admin or coordinator - CraneExpected CheckIfUserHasPermOnAccountNoLock_( + CraneErrCodeExpected CheckIfUserHasPermOnAccountNoLock_( const User& op_user, const std::string& account, bool read_only_priv); /** @@ -218,7 +214,7 @@ class AccountManager { * If the read_only_priv is true, it means the operating user is the * coordinator of any target user's account. */ - CraneExpected CheckIfUserHasPermOnUserNoLock_(const User& op_user, + CraneErrCodeExpected CheckIfUserHasPermOnUserNoLock_(const User& op_user, const User* user, bool read_only_priv); @@ -226,89 +222,89 @@ class AccountManager { // particular user. // 1. The operating user's permissions are greater than the target user's. // 2. The operating user is the coordinator of the account. - CraneExpected CheckIfUserHasPermOnUserOfAccountNoLock_( + CraneErrCodeExpected CheckIfUserHasPermOnUserOfAccountNoLock_( const User& op_user, const User* user, std::string* account, bool read_only_priv); - CraneExpected CheckPartitionIsAllowedNoLock_( + CraneErrCodeExpected CheckPartitionIsAllowedNoLock_( const Account* account, const std::string& partition, bool check_parent, bool is_user); - CraneExpected CheckQosIsAllowedNoLock_(const Account* account, + CraneErrCodeExpected CheckQosIsAllowedNoLock_(const Account* account, const std::string& qos_str, bool check_parent, bool is_user); bool IncQosReferenceCountInDb_(const std::string& name, int num); - CraneExpected AddUser_(const User& user, const Account* account, + CraneErrCodeExpected AddUser_(const User& user, const Account* account, const User* stale_user); - CraneExpected AddAccount_(const Account& account, const Account* parent, + CraneErrCodeExpected AddAccount_(const Account& account, const Account* parent, const Account* stale_account); - CraneExpected AddQos_(const Qos& qos, const Qos* stale_qos); + CraneErrCodeExpected AddQos_(const Qos& qos, const Qos* stale_qos); - CraneExpected DeleteUser_(const User& user, const std::string& account); + CraneErrCodeExpected DeleteUser_(const User& user, const std::string& account); - CraneExpected DeleteAccount_(const Account& account); + CraneErrCodeExpected DeleteAccount_(const Account& account); - CraneExpected DeleteQos_(const std::string& name); + CraneErrCodeExpected DeleteQos_(const std::string& name); - CraneExpected AddUserAllowedPartition_(const User& user, + CraneErrCodeExpected AddUserAllowedPartition_(const User& user, const Account& account, const std::string& partition); - CraneExpected AddUserAllowedQos_(const User& user, + CraneErrCodeExpected AddUserAllowedQos_(const User& user, const Account& account, const std::string& partition, const std::string& qos); - CraneExpected SetUserAdminLevel_(const std::string& name, + CraneErrCodeExpected SetUserAdminLevel_(const std::string& name, User::AdminLevel new_level); - CraneExpected SetUserDefaultQos_(const User& user, + CraneErrCodeExpected SetUserDefaultQos_(const User& user, const std::string& account, const std::string& partition, const std::string& qos); - CraneExpected SetUserAllowedPartition_(const User& user, + CraneErrCodeExpected SetUserAllowedPartition_(const User& user, const Account& account, const std::string& partitions); - CraneExpected SetUserAllowedQos_(const User& user, + CraneErrCodeExpected SetUserAllowedQos_(const User& user, const Account& account, const std::string& partition, const std::string& qos_list_str, bool force); - CraneExpected DeleteUserAllowedPartition_(const User& user, + CraneErrCodeExpected DeleteUserAllowedPartition_(const User& user, const std::string& account, const std::string& partition); - CraneExpected DeleteUserAllowedQos_(const User& user, + CraneErrCodeExpected DeleteUserAllowedQos_(const User& user, const std::string& qos, const std::string& account, const std::string& partition, bool force); - CraneExpected AddAccountAllowedPartition_(const std::string& name, + CraneErrCodeExpected AddAccountAllowedPartition_(const std::string& name, const std::string& partition); - CraneExpected AddAccountAllowedQos_(const Account& account, + CraneErrCodeExpected AddAccountAllowedQos_(const Account& account, const std::string& qos); - CraneExpected SetAccountDescription_(const std::string& name, + CraneErrCodeExpected SetAccountDescription_(const std::string& name, const std::string& description); - CraneExpected SetAccountDefaultQos_(const Account& account, + CraneErrCodeExpected SetAccountDefaultQos_(const Account& account, const std::string& qos); - CraneExpected SetAccountAllowedPartition_( + CraneErrCodeExpected SetAccountAllowedPartition_( const Account& account, const std::string& partitions); - CraneExpected SetAccountAllowedQos_(const Account& account, + CraneErrCodeExpected SetAccountAllowedQos_(const Account& account, const std::string& qos_list_str); - CraneExpected DeleteAccountAllowedPartition_( + CraneErrCodeExpected DeleteAccountAllowedPartition_( const Account& account, const std::string& partition); - CraneExpected DeleteAccountAllowedQos_(const Account& account, + CraneErrCodeExpected DeleteAccountAllowedQos_(const Account& account, const std::string& qos); - CraneExpected BlockUser_(const std::string& name, + CraneErrCodeExpected BlockUser_(const std::string& name, const std::string& account, bool block); - CraneExpected BlockAccount_(const std::string& name, bool block); + CraneErrCodeExpected BlockAccount_(const std::string& name, bool block); bool IsAllowedPartitionOfAnyNodeNoLock_(const Account* account, const std::string& partition, diff --git a/src/CraneCtld/CtldGrpcServer.cpp b/src/CraneCtld/CtldGrpcServer.cpp index cd84f9f8e..be9d5db10 100644 --- a/src/CraneCtld/CtldGrpcServer.cpp +++ b/src/CraneCtld/CtldGrpcServer.cpp @@ -41,7 +41,7 @@ grpc::Status CraneCtldServiceImpl::SubmitBatchTask( response->set_task_id(id); } else { response->set_ok(false); - response->set_reason(crane::grpc::ErrCode::ERR_BEYOND_TASK_ID); + response->set_reason(CraneErrCode::ERR_BEYOND_TASK_ID); } } else { response->set_ok(false); @@ -349,7 +349,7 @@ grpc::Status CraneCtldServiceImpl::AddUser( .allowed_partition_qos_map[apq.partition_name()]; } - AccountManager::CraneExpected result = + CraneErrCodeExpected result = g_account_manager->AddUser(request->uid(), user); if (result) { response->set_ok(true); @@ -377,7 +377,7 @@ grpc::Status CraneCtldServiceImpl::AddQos( int64_t sec = qos_info->max_time_limit_per_task(); if (!CheckIfTimeLimitSecIsValid(sec)) { response->set_ok(false); - response->set_reason(AccountManager::CraneErrCode::ERR_TIME_LIMIT); + response->set_reason(CraneErrCode::ERR_TIME_LIMIT); return grpc::Status::OK; } qos.max_time_limit_per_task = absl::Seconds(sec); @@ -414,7 +414,7 @@ grpc::Status CraneCtldServiceImpl::ModifyAccount( grpc::Status CraneCtldServiceImpl::ModifyUser( grpc::ServerContext *context, const crane::grpc::ModifyUserRequest *request, crane::grpc::ModifyUserReply *response) { - AccountManager::CraneExpected modify_res; + CraneErrCodeExpected modify_res; if (request->type() == crane::grpc::OperationType::Delete) { switch (request->modify_field()) { @@ -670,7 +670,7 @@ grpc::Status CraneCtldServiceImpl::BlockAccountOrUser( grpc::ServerContext *context, const crane::grpc::BlockAccountOrUserRequest *request, crane::grpc::BlockAccountOrUserReply *response) { - AccountManager::CraneExpected res; + CraneErrCodeExpected res; switch (request->entity_type()) { case crane::grpc::Account: @@ -939,7 +939,7 @@ CtldServer::SubmitTaskToScheduler(std::unique_ptr task) { if (!task->password_entry->Valid()) { CRANE_ERROR("Uid {} not found on the controller node", task->uid); - return std::unexpected(crane::grpc::ErrCode::ERR_INVALID_UID); + return std::unexpected(CraneErrCode::ERR_INVALID_UID); } task->SetUsername(task->password_entry->Username()); @@ -948,7 +948,7 @@ CtldServer::SubmitTaskToScheduler(std::unique_ptr task) { g_account_manager->GetExistedUserInfo(task->Username()); if (!user_scoped_ptr) { CRANE_ERROR("User '{}' not found in the account database", task->Username()); - return std::unexpected(crane::grpc::ErrCode::ERR_INVALID_USER); + return std::unexpected(CraneErrCode::ERR_INVALID_USER); } if (task->account.empty()) { @@ -957,7 +957,7 @@ CtldServer::SubmitTaskToScheduler(std::unique_ptr task) { } else { if (!user_scoped_ptr->account_to_attrs_map.contains(task->account)) { CRANE_ERROR("Account '{}' is not in your account list", task->account); - return std::unexpected(crane::grpc::ErrCode::ERR_USER_ACCOUNT_MISMATCH); + return std::unexpected(CraneErrCode::ERR_USER_ACCOUNT_MISMATCH); } } } @@ -966,7 +966,7 @@ CtldServer::SubmitTaskToScheduler(std::unique_ptr task) { task->Username(), task->account, task->partition_id)) { CRANE_ERROR("User '{}' doesn't have permission to use partition '{}' when using account '{}'", task->Username(), task->partition_id, task->account); - return std::unexpected(crane::grpc::ErrCode::ERR_ALLOWED_PARTITION); + return std::unexpected(CraneErrCode::ERR_ALLOWED_PARTITION); } auto enable_res = g_account_manager->CheckIfUserOfAccountIsEnabled( diff --git a/src/Utilities/PublicHeader/include/crane/PublicHeader.h b/src/Utilities/PublicHeader/include/crane/PublicHeader.h index 2771bb2c8..b625d9376 100644 --- a/src/Utilities/PublicHeader/include/crane/PublicHeader.h +++ b/src/Utilities/PublicHeader/include/crane/PublicHeader.h @@ -65,8 +65,10 @@ enum class CraneErr : uint16_t { template using CraneExpected = std::expected; +using CraneErrCode = crane::grpc::ErrCode; + template -using CraneErrCodeExpected = std::expected; +using CraneErrCodeExpected = std::expected; inline const char* kCtldDefaultPort = "10011"; inline const char* kCranedDefaultPort = "10010"; @@ -151,7 +153,7 @@ constexpr std::array "Not enough nodes which satisfy resource requirements", }; -constexpr std::array ErrCodeStrArray = { +constexpr std::array ErrCodeStrArray = { "Success", "Invalid UID", "You are not a user of Crane", @@ -225,7 +227,7 @@ inline std::string_view CraneErrStr(CraneErr err) { return Internal::CraneErrStrArr[uint16_t(err)]; } -inline std::string_view CraneErrCodeStr(crane::grpc::ErrCode err) { +inline std::string_view CraneErrCodeStr(CraneErrCode err) { return Internal::CraneErrStrArr[uint16_t(err)]; }