Skip to content

Commit f5419ac

Browse files
author
Andrei Nasonov
committed
Make IsCorrupted and SetCorrupted thread-safe
1 parent 0dd3b47 commit f5419ac

3 files changed

Lines changed: 18 additions & 5 deletions

File tree

cloud/storage/core/libs/file_backed_containers/file_ring_buffer.cpp

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@
1515
#include <util/system/compiler.h>
1616
#include <util/system/filemap.h>
1717

18+
#include <atomic>
19+
1820
namespace NCloud {
1921

2022
namespace {
@@ -137,7 +139,7 @@ class TFileRingBuffer::TImpl
137139
private:
138140
const TFileRingBufferArgs Args;
139141
TFileMapFileRingBufferAccessor Accessor;
140-
bool Corrupted = false;
142+
std::atomic<bool> Corrupted = false;
141143

142144
TEntryInfo CurrentAllocation = TEntryInfo::CreateInvalid();
143145
ui64 MaxObservedEntryByteCount = 0;
@@ -788,13 +790,13 @@ class TFileRingBuffer::TImpl
788790

789791
bool IsCorrupted() const
790792
{
791-
return Corrupted;
793+
return Corrupted.load(std::memory_order_relaxed);
792794
}
793795

794796
void SetCorrupted(const TString& message)
795797
{
796-
if (!Corrupted) {
797-
Corrupted = true;
798+
auto prevValue = Corrupted.exchange(true);
799+
if (!prevValue) {
798800
ReportFileRingBufferCorruptionDetectedError(
799801
"Corruption detected in FileRingBuffer, path: " +
800802
Args.FilePath + ", message: " + message);

cloud/storage/core/libs/file_backed_containers/file_ring_buffer.h

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ namespace NCloud {
1212

1313
////////////////////////////////////////////////////////////////////////////////
1414

15+
// Non-thread safe
1516
class TFileRingBuffer
1617
{
1718
public:
@@ -170,7 +171,17 @@ class TFileRingBuffer
170171
*/
171172
NProto::TError Visit(const TVisitor& visitor);
172173

174+
// This method is thread safe
173175
bool IsCorrupted() const;
176+
177+
/**
178+
* Sets Corrupted flag and fires a critical event if the flag has not
179+
* been previously set. Unsetting the flag is not possible.
180+
*
181+
* All further operations on the buffer will fail once the flag is set.
182+
*
183+
* This method is thread safe
184+
*/
174185
void SetCorrupted();
175186

176187
ui64 GetRawCapacity() const;

cloud/storage/core/libs/file_backed_containers/file_ring_buffer_ut.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1319,7 +1319,7 @@ Y_UNIT_TEST_SUITE(TFileRingBufferTest)
13191319

13201320
{
13211321
TFileRingBuffer rb(f.GetName(), len, 0, EVersion::V6);
1322-
UNIT_ASSERT(rb.PushBack("b"));
1322+
UNIT_ASSERT_VALUES_EQUAL(true, rb.PushBack("b"));
13231323
}
13241324
}
13251325
}

0 commit comments

Comments
 (0)