-
Notifications
You must be signed in to change notification settings - Fork 606
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
21 changed files
with
434 additions
and
6 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
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
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,59 @@ | ||
#include "service.h" | ||
|
||
namespace NKikimr::NLimiter { | ||
|
||
TLimiterActor::TLimiterActor(const TConfig& config, const TString& limiterName, TIntrusivePtr<::NMonitoring::TDynamicCounters> baseCounters) | ||
: LimiterName(limiterName) | ||
, Config(config) | ||
, Counters(LimiterName, baseCounters) | ||
{ | ||
Counters.InProgressLimit->Set(Config.GetLimit()); | ||
} | ||
|
||
void TLimiterActor::HandleMain(TEvExternal::TEvAskResource::TPtr& ev) { | ||
const auto now = TMonotonic::Now(); | ||
if (VolumeInFlight + ev->Get()->GetRequest()->GetVolume() <= Config.GetLimit()) { | ||
VolumeInFlight += ev->Get()->GetRequest()->GetVolume(); | ||
RequestsInFlight.emplace_back(now, ev->Get()->GetRequest()->GetVolume()); | ||
if (RequestsInFlight.size() == 1) { | ||
Schedule(now + Config.GetPeriod(), new NActors::TEvents::TEvWakeup()); | ||
} | ||
ev->Get()->GetRequest()->OnResourceAllocated(); | ||
Counters.InProgressStart->Inc(); | ||
} else { | ||
RequestsQueue.emplace_back(now, ev->Get()->GetRequest()); | ||
VolumeInWaiting += ev->Get()->GetRequest()->GetVolume(); | ||
} | ||
Counters.InProgressCount->Set(RequestsInFlight.size()); | ||
Counters.InProgressVolume->Set(VolumeInFlight); | ||
Counters.WaitingQueueCount->Set(RequestsQueue.size()); | ||
Counters.WaitingQueueVolume->Set(VolumeInWaiting); | ||
} | ||
|
||
void TLimiterActor::HandleMain(NActors::TEvents::TEvWakeup::TPtr& /*ev*/) { | ||
const auto now = TMonotonic::Now(); | ||
AFL_VERIFY(RequestsInFlight.size()); | ||
while (RequestsInFlight.size() && now + Config.GetPeriod() <= RequestsInFlight.front().GetInstant()) { | ||
AFL_VERIFY(RequestsInFlight.front().GetVolume() <= VolumeInFlight); | ||
VolumeInFlight = VolumeInFlight - RequestsInFlight.front().GetVolume(); | ||
RequestsInFlight.pop_front(); | ||
} | ||
if (RequestsInFlight.size()) { | ||
Schedule(RequestsInFlight.front().GetInstant() + Config.GetPeriod(), new NActors::TEvents::TEvWakeup()); | ||
} | ||
while (RequestsQueue.size() && (!VolumeInFlight || VolumeInFlight + RequestsQueue.front().GetRequest()->GetVolume() <= Config.GetLimit())) { | ||
Counters.WaitingHistogram->Collect((i64)(now - RequestsQueue.front().GetInstant()).MilliSeconds(), 1); | ||
VolumeInFlight += RequestsQueue.front().GetRequest()->GetVolume(); | ||
RequestsInFlight.emplace_back(now, RequestsQueue.front().GetRequest()->GetVolume()); | ||
RequestsQueue.front().GetRequest()->OnResourceAllocated(); | ||
VolumeInWaiting -= RequestsQueue.front().GetRequest()->GetVolume(); | ||
RequestsQueue.pop_front(); | ||
Counters.InProgressStart->Inc(); | ||
} | ||
Counters.InProgressCount->Set(RequestsInFlight.size()); | ||
Counters.InProgressVolume->Set(VolumeInFlight); | ||
Counters.WaitingQueueCount->Set(RequestsQueue.size()); | ||
Counters.WaitingQueueVolume->Set(VolumeInWaiting); | ||
} | ||
|
||
} |
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,101 @@ | ||
#pragma once | ||
#include <ydb/core/tx/columnshard/counters/common/owner.h> | ||
#include <ydb/core/tx/limiter/usage/abstract.h> | ||
#include <ydb/core/tx/limiter/usage/config.h> | ||
#include <ydb/core/tx/limiter/usage/events.h> | ||
|
||
#include <ydb/library/actors/core/actor_bootstrapped.h> | ||
#include <ydb/library/actors/core/log.h> | ||
#include <ydb/library/accessor/accessor.h> | ||
|
||
#include <library/cpp/monlib/dynamic_counters/counters.h> | ||
|
||
#include <queue> | ||
|
||
namespace NKikimr::NLimiter { | ||
|
||
class TCounters: public NColumnShard::TCommonCountersOwner { | ||
private: | ||
using TBase = NColumnShard::TCommonCountersOwner; | ||
public: | ||
const ::NMonitoring::TDynamicCounters::TCounterPtr WaitingQueueCount; | ||
const ::NMonitoring::TDynamicCounters::TCounterPtr WaitingQueueVolume; | ||
const ::NMonitoring::TDynamicCounters::TCounterPtr InProgressLimit; | ||
|
||
const ::NMonitoring::TDynamicCounters::TCounterPtr InProgressCount; | ||
const ::NMonitoring::TDynamicCounters::TCounterPtr InProgressVolume; | ||
|
||
const ::NMonitoring::TDynamicCounters::TCounterPtr InProgressStart; | ||
|
||
const ::NMonitoring::THistogramPtr WaitingHistogram; | ||
|
||
TCounters(const TString& limiterName, TIntrusivePtr<::NMonitoring::TDynamicCounters> baseSignals) | ||
: TBase("Limiter/" + limiterName, baseSignals) | ||
, WaitingQueueCount(TBase::GetValue("WaitingQueue/Count")) | ||
, WaitingQueueVolume(TBase::GetValue("WaitingQueue/Volume")) | ||
, InProgressLimit(TBase::GetValue("InProgress/Limit/Volume")) | ||
, InProgressCount(TBase::GetValue("InProgress/Count")) | ||
, InProgressVolume(TBase::GetValue("InProgress/Volume")) | ||
, InProgressStart(TBase::GetDeriviative("InProgress")) | ||
, WaitingHistogram(TBase::GetHistogram("Waiting", NMonitoring::ExponentialHistogram(20, 2))) { | ||
} | ||
}; | ||
|
||
class TLimiterActor: public NActors::TActorBootstrapped<TLimiterActor> { | ||
private: | ||
const TString LimiterName; | ||
const TConfig Config; | ||
TCounters Counters; | ||
class TResourceRequest { | ||
private: | ||
YDB_READONLY(TMonotonic, Instant, TMonotonic::Zero()); | ||
YDB_READONLY_DEF(std::shared_ptr<IResourceRequest>, Request); | ||
public: | ||
TResourceRequest(const TMonotonic instant, const std::shared_ptr<IResourceRequest>& req) | ||
: Instant(instant) | ||
, Request(req) { | ||
|
||
} | ||
}; | ||
|
||
class TResourceRequestInFlight { | ||
private: | ||
YDB_READONLY(TMonotonic, Instant, TMonotonic::Zero()); | ||
YDB_READONLY(ui64, Volume, 0); | ||
public: | ||
TResourceRequestInFlight(const TMonotonic instant, const ui64 volume) | ||
: Instant(instant) | ||
, Volume(volume) { | ||
|
||
} | ||
}; | ||
|
||
ui64 VolumeInFlight = 0; | ||
ui64 VolumeInWaiting = 0; | ||
std::deque<TResourceRequest> RequestsQueue; | ||
std::deque<TResourceRequestInFlight> RequestsInFlight; | ||
|
||
void HandleMain(TEvExternal::TEvAskResource::TPtr& ev); | ||
void HandleMain(NActors::TEvents::TEvWakeup::TPtr& ev); | ||
|
||
public: | ||
|
||
STATEFN(StateMain) { | ||
switch (ev->GetTypeRewrite()) { | ||
hFunc(TEvExternal::TEvAskResource, HandleMain); | ||
hFunc(NActors::TEvents::TEvWakeup, HandleMain); | ||
default: | ||
AFL_ERROR(NKikimrServices::TX_LIMITER)("limiter", LimiterName)("problem", "unexpected event")("type", ev->GetTypeRewrite()); | ||
AFL_VERIFY_DEBUG(false)("type", ev->GetTypeRewrite()); | ||
break; | ||
} | ||
} | ||
|
||
TLimiterActor(const TConfig& config, const TString& limiterName, TIntrusivePtr<::NMonitoring::TDynamicCounters> baseCounters); | ||
|
||
void Bootstrap() { | ||
Become(&TLimiterActor::StateMain); | ||
} | ||
}; | ||
|
||
} |
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( | ||
service.cpp | ||
) | ||
|
||
PEERDIR( | ||
ydb/core/tx/limiter/usage | ||
ydb/core/protos | ||
) | ||
|
||
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,5 @@ | ||
#include "abstract.h" | ||
|
||
namespace NKikimr::NLimiter { | ||
|
||
} |
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,24 @@ | ||
#pragma once | ||
#include <memory> | ||
#include <ydb/core/tx/columnshard/counters/common/owner.h> | ||
|
||
namespace NKikimr::NLimiter { | ||
class IResourceRequest { | ||
private: | ||
YDB_READONLY(ui64, Volume, 0); | ||
virtual void DoOnResourceAllocated() = 0; | ||
public: | ||
void OnResourceAllocated() { | ||
return DoOnResourceAllocated(); | ||
} | ||
|
||
virtual ~IResourceRequest() = default; | ||
|
||
IResourceRequest(const ui64 volume) | ||
: Volume(volume) | ||
{ | ||
|
||
} | ||
}; | ||
|
||
} |
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 @@ | ||
#include "config.h" | ||
#include <util/string/builder.h> | ||
|
||
namespace NKikimr::NLimiter { | ||
|
||
TString TConfig::DebugString() const { | ||
TStringBuilder sb; | ||
sb << "Period=" << Period << ";Limit=" << Limit << ";Enabled=" << EnabledFlag << ";"; | ||
return sb; | ||
} | ||
|
||
} |
Oops, something went wrong.