Skip to content

Commit

Permalink
refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
huerni committed Oct 31, 2024
1 parent a3d3158 commit 0a521d0
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 69 deletions.
134 changes: 68 additions & 66 deletions src/CraneCtld/LicensesManager.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/**
/**
* Copyright (c) 2023 Peking University and Peking University
* Changsha Institute for Computing and Digital Economy
*
Expand All @@ -14,90 +14,92 @@
* See the Mulan PSL v2 for more details.
*/

#include "LicensesManager.h"

#include "LicensesManager.h"

namespace Ctld {

LicensesManager::LicensesManager() {}

int LicensesManager::Init(const std::unordered_map<LicenseId, uint32_t> &lic_id_to_count_map) {
util::read_lock_guard readLock(rw_mutex_);
for (auto& [lic_id, count] : lic_id_to_count_map) {
lic_id_to_lic_map_.insert({lic_id, std::make_unique<License>(License(lic_id, count, 0, count))});
}
return 0;
int LicensesManager::Init(
const std::unordered_map<LicenseId, uint32_t>& lic_id_to_count_map) {
util::read_lock_guard readLock(rw_mutex_);
for (auto& [lic_id, count] : lic_id_to_count_map) {
lic_id_to_lic_map_.insert({lic_id, License(lic_id, count, 0, count)});
}
return 0;
}

void LicensesManager::GetLicensesInfo(const crane::grpc::QueryLicensesInfoRequest* request, crane::grpc::QueryLicensesInfoReply* response) {
auto* list = response->mutable_license_info_list();
void LicensesManager::GetLicensesInfo(
const crane::grpc::QueryLicensesInfoRequest* request,
crane::grpc::QueryLicensesInfoReply* response) {
auto* list = response->mutable_license_info_list();

if (request->license_name().empty()) {
util::read_lock_guard readLock(rw_mutex_);
for(auto& [lic_index, lic_ptr] : lic_id_to_lic_map_) {
auto *lic_info = list->Add();
lic_info->set_name(lic_ptr->license_id);
lic_info->set_total(lic_ptr->total);
lic_info->set_used(lic_ptr->used);
lic_info->set_free(lic_ptr->free);
}
} else {
util::read_lock_guard readLock(rw_mutex_);
auto it = lic_id_to_lic_map_.find(request->license_name());
if (it != lic_id_to_lic_map_.end()) {
auto *lic_info = list->Add();
lic_info->set_name(it->second->license_id);
lic_info->set_total(it->second->total);
lic_info->set_used(it->second->used);
lic_info->set_free(it->second->free);
}
if (request->license_name().empty()) {
util::read_lock_guard readLock(rw_mutex_);
for (auto& [lic_index, lic] : lic_id_to_lic_map_) {
auto* lic_info = list->Add();
lic_info->set_name(lic.license_id);
lic_info->set_total(lic.total);
lic_info->set_used(lic.used);
lic_info->set_free(lic.free);
}
} else {
util::read_lock_guard readLock(rw_mutex_);
auto it = lic_id_to_lic_map_.find(request->license_name());
if (it != lic_id_to_lic_map_.end()) {
auto* lic_info = list->Add();
lic_info->set_name(it->second.license_id);
lic_info->set_total(it->second.total);
lic_info->set_used(it->second.used);
lic_info->set_free(it->second.free);
}
}
}

bool LicensesManager::CheckLicenseCountSufficient(const std::unordered_map<LicenseId, uint32_t> &lic_id_to_count_map) {

util::read_lock_guard readLock(rw_mutex_);
for(auto& [lic_id, count] : lic_id_to_count_map) {
auto it = lic_id_to_lic_map_.find(lic_id);
if (it == lic_id_to_lic_map_.end() || it->second->free < count) {
return false;
}
bool LicensesManager::CheckLicenseCountSufficient(
const std::unordered_map<LicenseId, uint32_t>& lic_id_to_count_map) {
util::read_lock_guard readLock(rw_mutex_);
for (auto& [lic_id, count] : lic_id_to_count_map) {
auto it = lic_id_to_lic_map_.find(lic_id);
if (it == lic_id_to_lic_map_.end() || it->second.free < count) {
return false;
}
}

return true;
return true;
}

result::result<void, std::string> LicensesManager::CheckLicensesLegal(const ::google::protobuf::Map<std::string, uint32_t> &lic_id_to_count_map) {

util::read_lock_guard readLock(rw_mutex_);
for(auto& [lic_id, count] : lic_id_to_count_map) {
auto it = lic_id_to_lic_map_.find(lic_id);
if (it == lic_id_to_lic_map_.end() || count > it->second->total) {
return result::fail("Invalid license specification");
}
result::result<void, std::string> LicensesManager::CheckLicensesLegal(
const ::google::protobuf::Map<std::string, uint32_t>& lic_id_to_count_map) {
util::read_lock_guard readLock(rw_mutex_);
for (auto& [lic_id, count] : lic_id_to_count_map) {
auto it = lic_id_to_lic_map_.find(lic_id);
if (it == lic_id_to_lic_map_.end() || count > it->second.total) {
return result::fail("Invalid license specification");
}
}

return {};
return {};
}

void LicensesManager::MallocLicenseResource(const std::unordered_map<LicenseId, uint32_t> &lic_id_to_count_map) {

util::write_lock_guard writeLock(rw_mutex_);
for(auto& [lic_id, count] : lic_id_to_count_map) {
auto it = lic_id_to_lic_map_.find(lic_id);
it->second->used += count;
it->second->free -= count;
}
void LicensesManager::MallocLicenseResource(
const std::unordered_map<LicenseId, uint32_t>& lic_id_to_count_map) {
util::write_lock_guard writeLock(rw_mutex_);
for (auto& [lic_id, count] : lic_id_to_count_map) {
auto it = lic_id_to_lic_map_.find(lic_id);
it->second.used += count;
it->second.free -= count;
}
}

void LicensesManager::FreeLicenseResource(const std::unordered_map<LicenseId, uint32_t> &lic_id_to_count_map) {

util::write_lock_guard writeLock(rw_mutex_);
for(auto& [lic_id, count] : lic_id_to_count_map) {
auto it = lic_id_to_lic_map_.find(lic_id);
it->second->used -= count;
it->second->free += count;
}
}
void LicensesManager::FreeLicenseResource(
const std::unordered_map<LicenseId, uint32_t>& lic_id_to_count_map) {
util::write_lock_guard writeLock(rw_mutex_);
for (auto& [lic_id, count] : lic_id_to_count_map) {
auto it = lic_id_to_lic_map_.find(lic_id);
it->second.used -= count;
it->second.free += count;
}
}

} // namespace Craned
} // namespace Ctld
3 changes: 1 addition & 2 deletions src/CraneCtld/LicensesManager.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,7 @@

namespace Ctld {

using unorderedLicensesMap =
absl::flat_hash_map<LicenseId, std::unique_ptr<License>>;
using unorderedLicensesMap = absl::flat_hash_map<LicenseId, License>;

class LicensesManager {
public:
Expand Down
3 changes: 2 additions & 1 deletion src/Utilities/PublicHeader/include/crane/String.h
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,8 @@ std::string ReadableMemory(uint64_t memory_bytes);
bool ParseHostList(const std::string &host_str,
std::list<std::string> *host_list);

bool ParseLicensesList(const std::string& licenses_str, std::unordered_map<std::string, uint32_t> *licenses_map);
bool ParseLicensesList(const std::string &licenses_str,
std::unordered_map<LicenseId, uint32_t> *licenses_map);

bool FoundFirstNumberWithoutBrackets(const std::string &input, int *start,
int *end);
Expand Down

0 comments on commit 0a521d0

Please sign in to comment.