-
Notifications
You must be signed in to change notification settings - Fork 607
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
236 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
#include "lockfree_bucket.h" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
#pragma once | ||
|
||
#include <atomic> | ||
#include <limits> | ||
|
||
#include <library/cpp/bucket_quoter/timers.h> | ||
#include <util/datetime/base.h> | ||
|
||
template<class TTimer> | ||
class TLockFreeBucket { | ||
public: | ||
TLockFreeBucket(std::atomic<i64>& maxTokens, std::atomic<i64>& minTokens, std::atomic<ui64>& inflowPerSecond) | ||
: MaxTokens(maxTokens) | ||
, MinTokens(minTokens) | ||
, InflowPerSecond(inflowPerSecond) | ||
, Tokens(maxTokens.load()) | ||
{ | ||
Y_DEBUG_ABORT_UNLESS(maxTokens > 0); | ||
Y_DEBUG_ABORT_UNLESS(minTokens < 0); | ||
} | ||
|
||
bool IsEmpty() { | ||
FillBucket(); | ||
return Tokens.load() <= 0; | ||
} | ||
|
||
void FillAndTake(i64 tokens) { | ||
FillBucket(); | ||
TakeTokens(tokens); | ||
} | ||
|
||
private: | ||
void FillBucket() { | ||
TTime prev; | ||
TTime now; | ||
for (prev = LastUpdate.load(), now = TTimer::Now(); !LastUpdate.compare_exchange_strong(prev, now); ) {} | ||
|
||
ui64 rawInflow = InflowPerSecond.load() * TTimer::Duration(prev, now); | ||
if (rawInflow >= TTimer::Resolution) { | ||
Tokens.fetch_add(rawInflow / TTimer::Resolution); | ||
for (i64 tokens = Tokens.load(), maxTokens = MaxTokens.load(); tokens > maxTokens; ) { | ||
if (Tokens.compare_exchange_strong(tokens, maxTokens)) { | ||
break; | ||
} | ||
} | ||
} | ||
} | ||
|
||
void TakeTokens(i64 tokens) { | ||
Tokens.fetch_sub(tokens); | ||
for (i64 tokens = Tokens.load(), minTokens = MinTokens.load(); tokens < minTokens; ) { | ||
if (Tokens.compare_exchange_strong(tokens, minTokens)) { | ||
break; | ||
} | ||
} | ||
} | ||
|
||
private: | ||
using TTime = typename TTimer::TTime; | ||
|
||
std::atomic<i64>& MaxTokens; | ||
std::atomic<i64>& MinTokens; | ||
std::atomic<ui64>& InflowPerSecond; | ||
|
||
std::atomic<i64> Tokens; | ||
std::atomic<TTime> LastUpdate; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,128 @@ | ||
#include <library/cpp/testing/unittest/registar.h> | ||
#include <library/cpp/lockfree_bucket/lockfree_bucket.h> | ||
#include <util/system/guard.h> | ||
#include <util/system/spinlock.h> | ||
#include <util/system/types.h> | ||
|
||
#include <thread> | ||
|
||
struct TTestTimerMs { | ||
using TTime = TInstant; | ||
static constexpr ui64 Resolution = 1000ull; // milliseconds | ||
|
||
static TTime Now() { | ||
return TInstant::Zero() + TDuration::MilliSeconds(Time.load()); | ||
} | ||
|
||
static ui64 Duration(TTime from, TTime to) { | ||
return (to - from).MilliSeconds(); | ||
} | ||
|
||
static std::atomic<ui64> Time; | ||
|
||
static void Reset() { | ||
Time.store(0); | ||
} | ||
|
||
static void Advance(TDuration delta) { | ||
Time.fetch_add(delta.MilliSeconds()); | ||
} | ||
}; | ||
|
||
std::atomic<ui64> TTestTimerMs::Time = {}; | ||
|
||
Y_UNIT_TEST_SUITE(TLockFreeBucket) { | ||
struct TTestContext { | ||
TTestContext() { | ||
MaxTokens.store(1'000'000); | ||
MinTokens.store(-1'000'000); | ||
Inflow.store(1'000'000); | ||
TTestTimerMs::Reset(); | ||
} | ||
|
||
template<class TCallback> | ||
void Initialize(TCallback callback, ui32 threadCount) { | ||
for (ui32 i = 0; i < threadCount; ++i) { | ||
Threads.emplace_back(callback); | ||
} | ||
} | ||
|
||
~TTestContext() { | ||
JoinAll(); | ||
} | ||
|
||
void JoinAll() { | ||
for (std::thread& t : Threads) { | ||
t.join(); | ||
} | ||
Threads.clear(); | ||
} | ||
|
||
std::atomic<i64> MaxTokens; | ||
std::atomic<i64> MinTokens; | ||
std::atomic<ui64> Inflow; | ||
std::vector<std::thread> Threads; | ||
}; | ||
|
||
void TestLowerLimit(ui32 threadCount) { | ||
TTestContext ctx; | ||
TLockFreeBucket<TTestTimerMs> bucket(ctx.MaxTokens, ctx.MinTokens, ctx.Inflow); | ||
|
||
auto worker = [&]() { | ||
for (ui32 i = 0; i < 100; ++i) { | ||
TTestTimerMs::Advance(TDuration::MilliSeconds(10)); | ||
bucket.FillAndTake(123'456); | ||
} | ||
}; | ||
|
||
ctx.Initialize(worker, threadCount); | ||
ctx.JoinAll(); | ||
|
||
TTestTimerMs::Advance(TDuration::Seconds(1)); | ||
TTestTimerMs::Advance(TDuration::MilliSeconds(1)); | ||
|
||
UNIT_ASSERT(!bucket.IsEmpty()); | ||
} | ||
|
||
void TestUpperLimit(ui32 tokensTaken, bool isEmpty, ui32 threadCount) { | ||
TTestContext ctx; | ||
TLockFreeBucket<TTestTimerMs> bucket(ctx.MaxTokens, ctx.MinTokens, ctx.Inflow); | ||
TTestTimerMs::Advance(TDuration::Seconds(100500)); | ||
|
||
auto worker = [&]() { | ||
for (ui32 i = 0; i < 100; ++i) { | ||
TTestTimerMs::Advance(TDuration::MilliSeconds(10)); | ||
bucket.FillAndTake(tokensTaken); | ||
} | ||
}; | ||
|
||
ctx.Initialize(worker, threadCount); | ||
ctx.JoinAll(); | ||
|
||
UNIT_ASSERT_VALUES_EQUAL(bucket.IsEmpty(), isEmpty); | ||
} | ||
|
||
Y_UNIT_TEST(LowerLimitSingleThreaded) { | ||
TestLowerLimit(1); | ||
} | ||
|
||
Y_UNIT_TEST(LowerLimitMultiThreaded) { | ||
TestLowerLimit(20); | ||
} | ||
|
||
Y_UNIT_TEST(UpperLimitSingleThreaded) { | ||
TestUpperLimit(123'456, true, 1); | ||
} | ||
|
||
Y_UNIT_TEST(UpperLimitMultiThreaded) { | ||
TestUpperLimit(123'456, true, 20); | ||
} | ||
|
||
Y_UNIT_TEST(LowIntakeSingleThreaded) { | ||
TestUpperLimit(1, false, 1); | ||
} | ||
|
||
Y_UNIT_TEST(LowIntakeMultiThreaded) { | ||
TestUpperLimit(1, false, 20); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
UNITTEST() | ||
|
||
FORK_SUBTESTS() | ||
SRCS( | ||
main.cpp | ||
) | ||
PEERDIR( | ||
library/cpp/lockfree_bucket | ||
) | ||
|
||
END() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
LIBRARY() | ||
|
||
|
||
SRCS( | ||
lockfree_bucket.cpp | ||
) | ||
|
||
END() | ||
|
||
RECURSE_FOR_TESTS( | ||
ut | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -14,6 +14,7 @@ RECURSE( | |
grpc | ||
http_proxy | ||
keys | ||
lockfree_bucket | ||
logger | ||
login | ||
mkql_proto | ||
|