diff options
author | robot-piglet <robot-piglet@yandex-team.com> | 2024-08-23 07:31:34 +0300 |
---|---|---|
committer | robot-piglet <robot-piglet@yandex-team.com> | 2024-08-23 07:41:11 +0300 |
commit | 8e9d4416205322c34eba63df877991ac43e5a612 (patch) | |
tree | f280e5dc82260aa6605f9e0e166bf3b503510e49 | |
parent | 239d5bd1d2eff4e371de7092d4f33b299b9666da (diff) | |
download | ydb-8e9d4416205322c34eba63df877991ac43e5a612.tar.gz |
Intermediate changes
-rw-r--r-- | yt/yt/core/misc/fair_scheduler-inl.h | 13 | ||||
-rw-r--r-- | yt/yt/core/misc/fair_scheduler.h | 4 | ||||
-rw-r--r-- | yt/yt/core/misc/unittests/fair_scheduler_ut.cpp | 30 |
3 files changed, 17 insertions, 30 deletions
diff --git a/yt/yt/core/misc/fair_scheduler-inl.h b/yt/yt/core/misc/fair_scheduler-inl.h index 7de98c82d1..f9c508e0be 100644 --- a/yt/yt/core/misc/fair_scheduler-inl.h +++ b/yt/yt/core/misc/fair_scheduler-inl.h @@ -34,12 +34,14 @@ public: bucket->Tasks.push(std::move(task)); } - TTask Dequeue() override + std::optional<TTask> TryDequeue() override { auto guard = Guard(Lock_); while (true) { - YT_VERIFY(!BucketHeap_.empty()); + if (BucketHeap_.empty()) { + return std::nullopt; + } auto* bucket = BucketHeap_.front(); YT_ASSERT(bucket->InHeap); @@ -77,13 +79,6 @@ public: YT_ABORT(); } - bool IsEmpty() const override - { - auto guard = Guard(Lock_); - - return BucketHeap_.empty(); - } - void ChargeUser(const std::string& user, TDuration time) override { auto guard = Guard(Lock_); diff --git a/yt/yt/core/misc/fair_scheduler.h b/yt/yt/core/misc/fair_scheduler.h index 3f32fabde3..1399dcd0cd 100644 --- a/yt/yt/core/misc/fair_scheduler.h +++ b/yt/yt/core/misc/fair_scheduler.h @@ -11,9 +11,7 @@ struct IFairScheduler : public TRefCounted { virtual void Enqueue(TTask task, const std::string& user) = 0; - virtual TTask Dequeue() = 0; - - virtual bool IsEmpty() const = 0; + virtual std::optional<TTask> TryDequeue() = 0; virtual void ChargeUser(const std::string& user, TDuration time) = 0; }; diff --git a/yt/yt/core/misc/unittests/fair_scheduler_ut.cpp b/yt/yt/core/misc/unittests/fair_scheduler_ut.cpp index 4e517cd22d..d249f87b40 100644 --- a/yt/yt/core/misc/unittests/fair_scheduler_ut.cpp +++ b/yt/yt/core/misc/unittests/fair_scheduler_ut.cpp @@ -13,7 +13,7 @@ class TFairSchedulerTest : public ::testing::Test { protected: - IFairSchedulerPtr<TString> Scheduler_ = CreateFairScheduler<TString>(); + const IFairSchedulerPtr<TString> Scheduler_ = CreateFairScheduler<TString>(); }; TEST_F(TFairSchedulerTest, Simple) @@ -21,11 +21,9 @@ TEST_F(TFairSchedulerTest, Simple) Scheduler_->Enqueue("T1", "John"); Scheduler_->Enqueue("T2", "John"); - EXPECT_FALSE(Scheduler_->IsEmpty()); - EXPECT_EQ(Scheduler_->Dequeue(), "T1"); - EXPECT_FALSE(Scheduler_->IsEmpty()); - EXPECT_EQ(Scheduler_->Dequeue(), "T2"); - EXPECT_TRUE(Scheduler_->IsEmpty()); + EXPECT_EQ(Scheduler_->TryDequeue(), "T1"); + EXPECT_EQ(Scheduler_->TryDequeue(), "T2"); + EXPECT_EQ(Scheduler_->TryDequeue(), std::nullopt); } TEST_F(TFairSchedulerTest, Fairness1) @@ -36,19 +34,15 @@ TEST_F(TFairSchedulerTest, Fairness1) Scheduler_->Enqueue("B1", "Bob"); Scheduler_->Enqueue("B2", "Bob"); - EXPECT_FALSE(Scheduler_->IsEmpty()); - EXPECT_EQ(Scheduler_->Dequeue(), "A1"); + EXPECT_EQ(Scheduler_->TryDequeue(), "A1"); Scheduler_->ChargeUser("Alice", TDuration::Seconds(2)); - EXPECT_FALSE(Scheduler_->IsEmpty()); - EXPECT_EQ(Scheduler_->Dequeue(), "B1"); + EXPECT_EQ(Scheduler_->TryDequeue(), "B1"); Scheduler_->ChargeUser("Bob", TDuration::Seconds(2)); - EXPECT_FALSE(Scheduler_->IsEmpty()); - EXPECT_EQ(Scheduler_->Dequeue(), "A2"); + EXPECT_EQ(Scheduler_->TryDequeue(), "A2"); Scheduler_->ChargeUser("Alice", TDuration::Seconds(2)); - EXPECT_FALSE(Scheduler_->IsEmpty()); - EXPECT_EQ(Scheduler_->Dequeue(), "B2"); + EXPECT_EQ(Scheduler_->TryDequeue(), "B2"); Scheduler_->ChargeUser("Bob", TDuration::Seconds(2)); - EXPECT_TRUE(Scheduler_->IsEmpty()); + EXPECT_EQ(Scheduler_->TryDequeue(), std::nullopt); } TEST_F(TFairSchedulerTest, Fairness2) @@ -59,16 +53,16 @@ TEST_F(TFairSchedulerTest, Fairness2) Scheduler_->Enqueue(Format("B%v", index), "Bob"); } - EXPECT_EQ(Scheduler_->Dequeue(), "A1"); + EXPECT_EQ(Scheduler_->TryDequeue(), "A1"); Scheduler_->ChargeUser("Alice", TDuration::Seconds(100500)); for (int index = 1; index <= 10; ++index) { - EXPECT_EQ(Scheduler_->Dequeue(), Format("B%v", index)); + EXPECT_EQ(Scheduler_->TryDequeue(), Format("B%v", index)); Scheduler_->ChargeUser("Bob", TDuration::Seconds(1)); } for (int index = 2; index <= 10; ++index) { - EXPECT_EQ(Scheduler_->Dequeue(), Format("A%v", index)); + EXPECT_EQ(Scheduler_->TryDequeue(), Format("A%v", index)); } } |