Skip to content

Commit

Permalink
feat: Synchronize resources when AddUser, DeleteUser, ModifyUserAllow…
Browse files Browse the repository at this point in the history
…edQos, or ModifyQos are performed.
  • Loading branch information
huerni committed Nov 20, 2024
1 parent 5f7cf1b commit d744600
Showing 1 changed file with 46 additions and 0 deletions.
46 changes: 46 additions & 0 deletions src/CraneCtld/AccountManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@

#include "AccountManager.h"

#include "AccountMetaContainer.h"
#include "crane/Lock.h"
#include "crane/PasswordEntry.h"

namespace Ctld {
Expand All @@ -35,6 +37,7 @@ AccountManager::Result AccountManager::AddUser(User&& new_user) {

util::write_lock_guard user_guard(m_rw_user_mutex_);
util::write_lock_guard account_guard(m_rw_account_mutex_);
util::read_lock_guard qos_guard(m_rw_qos_mutex_);

if (new_user.default_account.empty()) {
// User must specify an account
Expand Down Expand Up @@ -107,6 +110,19 @@ AccountManager::Result AccountManager::AddUser(User&& new_user) {
}
res_user.account_to_attrs_map[object_account].blocked = false;

AccountMetaContainer::QosResourceList qos_resource_list;
for (const auto& [partition, qos] :
res_user.account_to_attrs_map[object_account]
.allowed_partition_qos_map) {
for (const auto& qos_name : qos.second) {
const Qos* qos_content = GetExistedQosInfoNoLock_(qos_name);
qos_resource_list.emplace_back(
qos_name, QosResource{qos_content->max_cpus_per_user,
qos_content->max_jobs_per_user});
}
}
g_account_meta_container->AddQosResourceToUser(name, qos_resource_list);

mongocxx::client_session::with_transaction_cb callback =
[&](mongocxx::client_session* session) {
// Update the user's account
Expand Down Expand Up @@ -327,6 +343,8 @@ AccountManager::Result AccountManager::DeleteUser(const std::string& name,
fmt::format("User '{}' doesn't exist in the database", name)};
}

g_account_meta_container->EraseUserResource(name);

mongocxx::client_session::with_transaction_cb callback =
[&](mongocxx::client_session* session) {
// delete form the parent accounts' users list
Expand Down Expand Up @@ -674,6 +692,12 @@ AccountManager::Result AccountManager::ModifyQos(const std::string& name,
// To avoid frequently judging item, obtain the modified qos of the Mongodb
Qos qos;
g_db_client->SelectQos("name", name, &qos);

// Modify QosResource when max_jobs_per_user or max_cpus_per_user is changed.
if (item == "max_jobs_per_user" || item == "max_cpus_per_user")
g_account_meta_container->ModifyQosResourceOnUser(
name, QosResource{qos.max_cpus_per_user, qos.max_jobs_per_user});

*m_qos_map_[name] = std::move(qos);

return Result{true};
Expand Down Expand Up @@ -1204,6 +1228,13 @@ AccountManager::Result AccountManager::AddUserAllowedQos_(
return Result{false, "Fail to update data in database"};
}

AccountMetaContainer::QosResourceList qos_resource_list;
const Qos* qos_content = GetExistedQosInfoNoLock_(qos);
qos_resource_list.emplace_back(qos,
QosResource{qos_content->max_cpus_per_user,
qos_content->max_jobs_per_user});
g_account_meta_container->AddQosResourceToUser(name, qos_resource_list);

m_user_map_[name]->account_to_attrs_map[account].allowed_partition_qos_map =
user.account_to_attrs_map[account].allowed_partition_qos_map;

Expand Down Expand Up @@ -1459,6 +1490,18 @@ AccountManager::Result AccountManager::SetUserAllowedQos_(
return Result{false, "Fail to update data in database"};
}

AccountMetaContainer::QosResourceList qos_resource_list;
for (const auto& [partition, qos] :
user.account_to_attrs_map[account].allowed_partition_qos_map) {
for (const auto& qos_name : qos.second) {
const Qos* qos_content = GetExistedQosInfoNoLock_(qos_name);
qos_resource_list.emplace_back(
qos_name, QosResource{qos_content->max_cpus_per_user,
qos_content->max_jobs_per_user});
}
}
g_account_meta_container->AddQosResourceToUser(name, qos_resource_list);

m_user_map_[name]->account_to_attrs_map[account].allowed_partition_qos_map =
user.account_to_attrs_map[account].allowed_partition_qos_map;

Expand Down Expand Up @@ -1592,6 +1635,8 @@ AccountManager::Result AccountManager::DeleteUserAllowedQos_(
return Result{false, "Fail to update data in database"};
}

g_account_meta_container->EraseQosResourceOnUser(name, qos);

m_user_map_[name]->account_to_attrs_map[account].allowed_partition_qos_map =
user.account_to_attrs_map[account].allowed_partition_qos_map;

Expand Down Expand Up @@ -2180,6 +2225,7 @@ bool AccountManager::DeleteAccountAllowedQosFromMapNoLock_(

for (const auto& child : account->child_accounts) {
DeleteAccountAllowedQosFromMapNoLock_(child, qos);
g_account_meta_container->EraseQosResourceOnUser(name, qos);
}

for (const auto& user : account->users) {
Expand Down

0 comments on commit d744600

Please sign in to comment.