fix(store): add mutex locks for thread-safe metrics retrieval (#804)

* fix(store): add mutex locks for thread-safe metrics retrieval and comparisons

* refactor(get_metrics_internal): require caller to hold m_mutex instead of internal locking
This commit is contained in:
JinYan Su 2025-09-04 16:54:11 +08:00 committed by GitHub
parent 0cc51a60ac
commit 03a26184ea
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 6 additions and 1 deletions

View File

@ -173,7 +173,7 @@ class OffsetAllocator : public std::enable_shared_from_this<OffsetAllocator> {
// Internal method to get metrics without locking (caller must hold m_mutex)
[[nodiscard]]
OffsetAllocatorMetrics get_metrics_internal() const;
OffsetAllocatorMetrics get_metrics_internal() const REQUIRES(m_mutex);
std::unique_ptr<__Allocator> m_allocator GUARDED_BY(m_mutex);
uint64_t m_base;

View File

@ -1,4 +1,5 @@
#include "offset_allocator/offset_allocator.hpp"
#include "mutex.h"
#include "serializer.h"
#include "types.h"
@ -297,6 +298,8 @@ class OffsetAllocatorTest : public ::testing::Test {
void assertAllocatorEQ(const std::shared_ptr<OffsetAllocator>& a,
const std::shared_ptr<OffsetAllocator>& b) {
MutexLocker lock_a(&a->m_mutex);
MutexLocker lock_b(&b->m_mutex);
// Compare basic member variables
ASSERT_EQ(a->m_base, b->m_base);
ASSERT_EQ(a->m_multiplier_bits, b->m_multiplier_bits);
@ -353,6 +356,8 @@ class OffsetAllocatorTest : public ::testing::Test {
// Compare two allocators bytes by bytes to detect one bit difference.
bool isAllocatorEqual(const std::shared_ptr<OffsetAllocator>& a,
const std::shared_ptr<OffsetAllocator>& b) {
MutexLocker lock_a(&a->m_mutex);
MutexLocker lock_b(&b->m_mutex);
// Compare basic member variables
if (memcmp(&a->m_base, &b->m_base, sizeof(a->m_base)) != 0)
return false;