Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions game-server/libsidecar-cpp/include/libsidecar/tc9_events.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ typedef struct {
typedef struct {
uint32_t groupGuid;
uint64_t memberGuid;
/* Set when the removed member was the leader; 0 otherwise */
uint64_t newLeaderGuid;
} TC9EventGroupMemberRemoved;
Comment on lines 36 to 40

typedef struct {
Expand Down
4 changes: 2 additions & 2 deletions game-server/libsidecar-cpp/src/api.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ TC9_API void TC9InitLib(
realmID,
isCrossRealm,
availableMaps ? availableMaps : "",
"", // preferred hostname (empty = auto)
config.preferred_hostname(),
server_id,
assigned_maps_vec
);
Expand Down Expand Up @@ -754,7 +754,7 @@ TC9_API void TC9SetOnGroupMemberRemovedHook(OnGroupMemberRemovedHook hook) {

tc9::EventHooks::Instance().RegisterGroupMemberRemoved([](TC9EventGroupMemberRemoved event) {
if (stored_hook) {
stored_hook(event.groupGuid, event.memberGuid, 0); // newLeaderGuid not in TC9
stored_hook(event.groupGuid, event.memberGuid, event.newLeaderGuid);
}
});
}
Expand Down
33 changes: 30 additions & 3 deletions game-server/libsidecar-cpp/src/guids/guid_manager.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
#include "guid_manager.h"
#include "../core/config.h"
#include "../grpc/clients.h"
#include <spdlog/spdlog.h>
#include <algorithm>
#include <thread>

namespace tc9 {
Expand Down Expand Up @@ -77,7 +79,14 @@ void GuidIterator::AddRanges(const std::vector<std::pair<uint64_t, uint64_t>>& n

bool GuidIterator::NeedsRefill() const {
std::lock_guard<std::mutex> lock(ranges_mutex_);
return AvailableCount() < REFILL_THRESHOLD;
return AvailableCount() < refill_threshold_;
}

void GuidIterator::SetRefillPolicy(uint64_t pool_size) {
std::lock_guard<std::mutex> lock(ranges_mutex_);
// Mirror the Go sidecar: request more once 65% of the pool is used,
// i.e. when less than 35% remains available.
refill_threshold_ = static_cast<size_t>(std::max<uint64_t>(1, pool_size * 35 / 100));
}

size_t GuidIterator::AvailableCount() const {
Expand Down Expand Up @@ -165,7 +174,20 @@ void GuidManager::Initialize(GrpcClients* clients, uint32_t realm_id) {
grpc_clients_ = clients;
default_realm_id_ = realm_id;

spdlog::info("GuidManager initialized for realm {}", realm_id);
// Pool sizes come from the *_GUIDS_BUFFER_SIZE settings
auto& config = Config::Instance();
auto pool_size = [](int configured, uint64_t fallback) {
return configured > 0 ? static_cast<uint64_t>(configured) : fallback;
};
pool_sizes_[0] = pool_size(config.character_guids_buffer_size(), pool_sizes_[0]);
pool_sizes_[1] = pool_size(config.item_guids_buffer_size(), pool_sizes_[1]);
pool_sizes_[2] = pool_size(config.instance_guids_buffer_size(), pool_sizes_[2]);
character_guids_->SetRefillPolicy(pool_sizes_[0]);
item_guids_->SetRefillPolicy(pool_sizes_[1]);
Comment on lines +182 to +186
instance_guids_->SetRefillPolicy(pool_sizes_[2]);

spdlog::info("GuidManager initialized for realm {} (pool sizes: {}/{}/{})",
realm_id, pool_sizes_[0], pool_sizes_[1], pool_sizes_[2]);

// Pre-fetch initial GUID pools for all types
RefillGuidPool(0, realm_id); // Characters
Expand Down Expand Up @@ -238,13 +260,18 @@ void GuidManager::RefillGuidPool(int guid_type, uint32_t realm_id) {
return;
}

if (guid_type < 0 || guid_type > 2) {
spdlog::error("Invalid GUID type: {}", guid_type);
return;
}

spdlog::info("Requesting GUID pool for type {} (realm {})", guid_type, realm_id);

std::vector<std::pair<uint64_t, uint64_t>> ranges;
bool success = grpc_clients_->RequestGUIDPool(
realm_id,
guid_type,
POOL_SIZE,
pool_sizes_[guid_type],
ranges
);

Expand Down
10 changes: 7 additions & 3 deletions game-server/libsidecar-cpp/src/guids/guid_manager.h
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,9 @@ class GuidIterator {
// Get total available GUIDs
size_t AvailableCount() const;

// Derive the refill trigger from the configured pool size
void SetRefillPolicy(uint64_t pool_size);

private:
void MoveToNextRange();

Expand All @@ -62,7 +65,8 @@ class GuidIterator {
// Mutex only for range management (rare operations)
mutable std::mutex ranges_mutex_;

static constexpr size_t REFILL_THRESHOLD = 1000;
// Refill once available GUIDs drop below this (see SetRefillPolicy)
size_t refill_threshold_ = 1000;
};

// Manages GUID generation for all types
Expand Down Expand Up @@ -103,8 +107,8 @@ class GuidManager {
std::unique_ptr<GuidIterator> item_guids_; // Thread-safe (atomics)
std::unique_ptr<GuidIterator> instance_guids_; // Thread-unsafe (fast)

// Pool size to request from service
static constexpr uint64_t POOL_SIZE = 10000;
// Pool size to request from service, per GUID type (from Config)
uint64_t pool_sizes_[3] = {50, 200, 10};
};

} // namespace tc9
Expand Down
1 change: 1 addition & 0 deletions game-server/libsidecar-cpp/src/nats/handlers.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,7 @@ std::unique_ptr<Handler> CreateGroupMemberRemovedHandler(const std::string& data
TC9EventGroupMemberRemoved event{};
event.groupGuid = j.value("GroupID", 0u);
event.memberGuid = j.value("MemberGUID", 0ull);
event.newLeaderGuid = j.value("NewLeaderID", 0ull);

EventHooks::Instance().DispatchGroupMemberRemoved(event);
} catch (const std::exception& e) {
Expand Down
36 changes: 36 additions & 0 deletions game-server/libsidecar-cpp/tests/guid_manager_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -306,3 +306,39 @@ TEST(GuidIteratorTest, IncrementalRangeAddition) {
EXPECT_EQ(i, iter.Next(1));
}
}

// The refill trigger mirrors the Go sidecar: request more once 65% of the
// configured pool is used, i.e. below 35% remaining.
TEST(GuidIteratorTest, SetRefillPolicy_ThresholdFromPoolSize) {
GuidIterator iter(0, false);
iter.SetRefillPolicy(50); // threshold = 50 * 35 / 100 = 17

std::vector<std::pair<uint64_t, uint64_t>> ranges = {{1, 51}}; // 50 GUIDs
iter.AddRanges(ranges);
EXPECT_FALSE(iter.NeedsRefill());

// Consume down to exactly 17 available: not yet below the threshold.
for (int i = 0; i < 33; ++i) {
iter.Next(1);
}
EXPECT_FALSE(iter.NeedsRefill());

// One more leaves 16 available -> below 35% remaining.
iter.Next(1);
EXPECT_TRUE(iter.NeedsRefill());
}

TEST(GuidIteratorTest, SetRefillPolicy_TinyPoolFloorsAtOne) {
GuidIterator iter(2, false);
iter.SetRefillPolicy(2); // 2 * 35 / 100 = 0 -> floored to 1

std::vector<std::pair<uint64_t, uint64_t>> ranges = {{1, 3}}; // 2 GUIDs
iter.AddRanges(ranges);
EXPECT_FALSE(iter.NeedsRefill());

iter.Next(1); // 1 available: not below the floor of 1
EXPECT_FALSE(iter.NeedsRefill());

iter.Next(1); // exhausted -> refill
EXPECT_TRUE(iter.NeedsRefill());
}
Loading