Skip to content

Commit 4372856

Browse files
committed
refactor atomicMap
1 parent 73bf1d6 commit 4372856

File tree

2 files changed

+43
-36
lines changed

2 files changed

+43
-36
lines changed

src/CraneCtld/LicensesManager.cpp

Lines changed: 35 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,14 @@ LicensesManager::LicensesManager() {}
2424

2525
int LicensesManager::Init(
2626
const std::unordered_map<LicenseId, uint32_t>& lic_id_to_count_map) {
27-
util::read_lock_guard readLock(rw_mutex_);
27+
HashMap<LicenseId, License> licenses_map;
28+
2829
for (auto& [lic_id, count] : lic_id_to_count_map) {
29-
lic_id_to_lic_map_.insert({lic_id, License(lic_id, count, 0, count)});
30+
licenses_map.insert({lic_id, License(lic_id, count, 0, count)});
3031
}
32+
33+
licenses_map_.InitFromMap(std::move(licenses_map));
34+
3135
return 0;
3236
}
3337

@@ -36,73 +40,72 @@ void LicensesManager::GetLicensesInfo(
3640
crane::grpc::QueryLicensesInfoReply* response) {
3741
auto* list = response->mutable_license_info_list();
3842

43+
auto licenses_map = licenses_map_.GetMapConstSharedPtr();
44+
3945
if (request->license_name_list().empty()) {
40-
util::read_lock_guard readLock(rw_mutex_);
41-
for (auto& [lic_index, lic] : lic_id_to_lic_map_) {
46+
for (const auto& [lic_index, lic_ptr] : *licenses_map) {
4247
auto* lic_info = list->Add();
43-
lic_info->set_name(lic.license_id);
44-
lic_info->set_total(lic.total);
45-
lic_info->set_used(lic.used);
46-
lic_info->set_free(lic.free);
48+
auto lic = lic_ptr.GetExclusivePtr();
49+
lic_info->set_name(lic->license_id);
50+
lic_info->set_total(lic->total);
51+
lic_info->set_used(lic->used);
52+
lic_info->set_free(lic->free);
4753
}
4854
} else {
49-
util::read_lock_guard readLock(rw_mutex_);
5055
for (auto& license_name : request->license_name_list()) {
51-
auto it = lic_id_to_lic_map_.find(license_name);
52-
if (it != lic_id_to_lic_map_.end()) {
56+
if (licenses_map->contains(license_name)) {
5357
auto* lic_info = list->Add();
54-
lic_info->set_name(it->second.license_id);
55-
lic_info->set_total(it->second.total);
56-
lic_info->set_used(it->second.used);
57-
lic_info->set_free(it->second.free);
58+
auto lic = licenses_map->at(license_name).GetExclusivePtr();
59+
lic_info->set_name(lic->license_id);
60+
lic_info->set_total(lic->total);
61+
lic_info->set_used(lic->used);
62+
lic_info->set_free(lic->free);
5863
}
5964
}
6065
}
6166
}
6267

6368
bool LicensesManager::CheckLicenseCountSufficient(
6469
const std::unordered_map<LicenseId, uint32_t>& lic_id_to_count_map) {
65-
util::read_lock_guard readLock(rw_mutex_);
70+
auto licenses_map = licenses_map_.GetMapConstSharedPtr();
6671
for (auto& [lic_id, count] : lic_id_to_count_map) {
67-
auto it = lic_id_to_lic_map_.find(lic_id);
68-
if (it == lic_id_to_lic_map_.end() || it->second.free < count) {
69-
return false;
70-
}
72+
if (!licenses_map->contains(lic_id)) return false;
73+
auto lic = licenses_map->at(lic_id).GetExclusivePtr();
74+
if (lic->free < count) return false;
7175
}
7276

7377
return true;
7478
}
7579

7680
std::expected<void, std::string> LicensesManager::CheckLicensesLegal(
7781
const ::google::protobuf::Map<std::string, uint32_t>& lic_id_to_count_map) {
78-
util::read_lock_guard readLock(rw_mutex_);
82+
auto licenses_map = licenses_map_.GetMapConstSharedPtr();
7983
for (auto& [lic_id, count] : lic_id_to_count_map) {
80-
auto it = lic_id_to_lic_map_.find(lic_id);
81-
if (it == lic_id_to_lic_map_.end() || count > it->second.total) {
84+
if (!licenses_map->contains(lic_id))
85+
return std::unexpected("Invalid license specification");
86+
auto lic = licenses_map->at(lic_id).GetExclusivePtr();
87+
if (count > lic->total)
8288
return std::unexpected("Invalid license specification");
83-
}
8489
}
8590

8691
return {};
8792
}
8893

8994
void LicensesManager::MallocLicenseResource(
9095
const std::unordered_map<LicenseId, uint32_t>& lic_id_to_count_map) {
91-
util::write_lock_guard writeLock(rw_mutex_);
9296
for (auto& [lic_id, count] : lic_id_to_count_map) {
93-
auto it = lic_id_to_lic_map_.find(lic_id);
94-
it->second.used += count;
95-
it->second.free -= count;
97+
auto lic = licenses_map_[lic_id];
98+
lic->used += count;
99+
lic->free -= count;
96100
}
97101
}
98102

99103
void LicensesManager::FreeLicenseResource(
100104
const std::unordered_map<LicenseId, uint32_t>& lic_id_to_count_map) {
101-
util::write_lock_guard writeLock(rw_mutex_);
102105
for (auto& [lic_id, count] : lic_id_to_count_map) {
103-
auto it = lic_id_to_lic_map_.find(lic_id);
104-
it->second.used -= count;
105-
it->second.free += count;
106+
auto lic = licenses_map_[lic_id];
107+
lic->used -= count;
108+
lic->free += count;
106109
}
107110
}
108111

src/CraneCtld/LicensesManager.h

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,17 @@
2121
#include "CtldPublicDefs.h"
2222
// Precompiled header comes first!
2323

24+
#include "crane/AtomicHashMap.h"
2425
#include "crane/Lock.h"
2526

2627
namespace Ctld {
2728

28-
using unorderedLicensesMap = absl::flat_hash_map<LicenseId, License>;
29+
template <typename K, typename V,
30+
typename Hash = absl::container_internal::hash_default_hash<K>>
31+
using HashMap = absl::flat_hash_map<K, V, Hash>;
32+
33+
using LicensesAtomicMap = util::AtomicHashMap<HashMap, LicenseId, License>;
34+
using LicensesMetaRawMap = LicensesAtomicMap::RawMap;
2935

3036
class LicensesManager {
3137
public:
@@ -52,9 +58,7 @@ class LicensesManager {
5258
const std::unordered_map<LicenseId, uint32_t> &lic_id_to_count_map);
5359

5460
private:
55-
unorderedLicensesMap lic_id_to_lic_map_ ABSL_GUARDED_BY(rw_mutex_);
56-
57-
util::rw_mutex rw_mutex_;
61+
LicensesAtomicMap licenses_map_;
5862
};
5963

6064
} // namespace Ctld

0 commit comments

Comments
 (0)