Skip to content

Commit

Permalink
use codeErr type
Browse files Browse the repository at this point in the history
  • Loading branch information
1daidai1 committed Dec 10, 2024
1 parent b72a2a9 commit 2d88fb3
Show file tree
Hide file tree
Showing 9 changed files with 199 additions and 175 deletions.
4 changes: 2 additions & 2 deletions protos/Crane.proto
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ message SubmitBatchTaskReply {
bool ok = 1;
oneof payload{
uint32 task_id = 2;
string reason = 3;
ErrCode reason = 3;
}
}

Expand All @@ -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 {
Expand Down
123 changes: 67 additions & 56 deletions protos/PublicDefs.proto
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
25 changes: 12 additions & 13 deletions src/CraneCtld/AccountManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -899,7 +899,7 @@ bool AccountManager::CheckUserPermissionToPartition(
return false;
}

result::result<void, std::string> AccountManager::CheckIfUserOfAccountIsEnabled(
CraneErrCodeExpected<void> AccountManager::CheckIfUserOfAccountIsEnabled(
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_);
Expand All @@ -908,20 +908,19 @@ result::result<void, std::string> AccountManager::CheckIfUserOfAccountIsEnabled(
do {
const Account* account_ptr = GetExistedAccountInfoNoLock_(account_name);
if (account_ptr->blocked) {
return result::fail(
fmt::format("Ancestor account '{}' is blocked", account_ptr->name));
return std::unexpected(crane::grpc::ErrCode::ERR_BLOCKED_ACCOUNT);
}
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) {
return result::fail(fmt::format("User '{}' is blocked", user_ptr->name));
return std::unexpected(crane::grpc::ErrCode::ERR_BLOCKED_USER);
}
return {};
}

CraneErr AccountManager::CheckAndApplyQosLimitOnTask(const std::string& user,
AccountManager::CraneExpected<void> AccountManager::CheckAndApplyQosLimitOnTask(const std::string& user,
const std::string& account,
TaskInCtld* task) {
util::read_lock_guard user_guard(m_rw_user_mutex_);
Expand All @@ -930,7 +929,7 @@ CraneErr AccountManager::CheckAndApplyQosLimitOnTask(const std::string& user,
const User* user_share_ptr = GetExistedUserInfoNoLock_(user);
if (!user_share_ptr) {
CRANE_ERROR("CheckAndApplyQosLimitOnTask error: Unknown user {}", user);
return CraneErr::kInvalidUser;
return std::unexpected(CraneErrCode::ERR_INVALID_OP_USER);
}

if (task->uid != 0) {
Expand All @@ -941,7 +940,7 @@ CraneErr AccountManager::CheckAndApplyQosLimitOnTask(const std::string& user,
CRANE_ERROR(
"CheckAndApplyQosLimitOnTask error: Partition is not allowed for "
"this user");
return CraneErr::kNonExistent;
return std::unexpected(CraneErrCode::ERR_ALLOWED_PARTITION);

if (task->qos.empty()) {
// Default qos
Expand All @@ -951,7 +950,7 @@ CraneErr AccountManager::CheckAndApplyQosLimitOnTask(const std::string& user,
"CheckAndApplyQosLimitOnTask error: The user '{}' has no QOS "
"available for this partition '{}' to be used",
task->Username(), task->partition_id);
return CraneErr::kInvalidQos;
return std::unexpected(CraneErrCode::ERR_HAS_NO_QOS_IN_PARTITION);
}
} else {
// Check whether task.qos in the qos list
Expand All @@ -960,7 +959,7 @@ CraneErr AccountManager::CheckAndApplyQosLimitOnTask(const std::string& user,
"CheckAndApplyQosLimitOnTask error: The qos '{}'"
" you set is not in partition's allowed qos list",
task->qos);
return CraneErr::kInvalidQos;
return std::unexpected(CraneErrCode::ERR_HAS_ALLOWED_QOS_IN_PARTITION);
}
}
} else {
Expand All @@ -972,7 +971,7 @@ CraneErr AccountManager::CheckAndApplyQosLimitOnTask(const std::string& user,
const Qos* qos_share_ptr = GetExistedQosInfoNoLock_(task->qos);
if (!qos_share_ptr) {
CRANE_ERROR("Unknown QOS '{}'", task->qos);
return CraneErr::kInvalidQos;
return std::unexpected(CraneErrCode::ERR_INVALID_QOS);
}

task->qos_priority = qos_share_ptr->priority;
Expand All @@ -981,16 +980,16 @@ CraneErr AccountManager::CheckAndApplyQosLimitOnTask(const std::string& user,
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 CraneErr::kInvalidTimeLimit;
return std::unexpected(CraneErrCode::ERR_TIME_TIMIT_BEYOND);
}

if (static_cast<double>(task->cpus_per_task) >
qos_share_ptr->max_cpus_per_user) {
CRANE_ERROR("cpus-per-task reached the user's limit");
return CraneErr::kInvaildCpusperTask;
return std::unexpected(CraneErrCode::ERR_CPUS_PER_TASK_BEYOND);
}

return CraneErr::kOk;
return {};
}

result::result<void, std::string> AccountManager::CheckUidIsAdmin(
Expand Down
4 changes: 2 additions & 2 deletions src/CraneCtld/AccountManager.h
Original file line number Diff line number Diff line change
Expand Up @@ -129,10 +129,10 @@ class AccountManager {
const std::string& account,
const std::string& partition);

result::result<void, std::string> CheckIfUserOfAccountIsEnabled(
CraneErrCodeExpected<void> CheckIfUserOfAccountIsEnabled(
const std::string& user, const std::string& account);

CraneErr CheckAndApplyQosLimitOnTask(const std::string& user,
CraneExpected<void> CheckAndApplyQosLimitOnTask(const std::string& user,
const std::string& account,
TaskInCtld* task);

Expand Down
Loading

0 comments on commit 2d88fb3

Please sign in to comment.