-
Notifications
You must be signed in to change notification settings - Fork 420
LAC: smoothing tokens request and keep in high level (#10997) #11034
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
JaySon-Huang
merged 2 commits into
pingcap:release-8.5-20260811-v8.5.7
from
yongman:release-8.5-20260811-v8.5.7-hotfix
Aug 11, 2026
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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
115 changes: 115 additions & 0 deletions
115
dbms/src/Flash/ResourceControl/tests/gtest_local_admission_controller.cpp
This file contains hidden or 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,115 @@ | ||
| // Copyright 2026 PingCAP, Inc. | ||
| // | ||
| // Licensed under the Apache License, Version 2.0 (the "License"); | ||
| // you may not use this file except in compliance with the License. | ||
| // You may obtain a copy of the License at | ||
| // | ||
| // http://www.apache.org/licenses/LICENSE-2.0 | ||
| // | ||
| // Unless required by applicable law or agreed to in writing, software | ||
| // distributed under the License is distributed on an "AS IS" BASIS, | ||
| // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| // See the License for the specific language governing permissions and | ||
| // limitations under the License. | ||
|
|
||
| #include <Flash/ResourceControl/LocalAdmissionController.h> | ||
| #include <gtest/gtest.h> | ||
|
|
||
| namespace DB::tests | ||
| { | ||
| TEST(LocalAdmissionControllerTest, StartupRefillDoesNotUseGlobalBurstLimitAsHighWatermark) | ||
| { | ||
| constexpr double fill_rate = 1000; | ||
| constexpr int64_t global_burst_limit = 10000; | ||
| constexpr double consumed_tokens = 500; | ||
| const auto start_time = SteadyClock::now() - 2 * ResourceGroup::REFILL_TOKEN_INTERVAL; | ||
|
|
||
| resource_manager::ResourceGroup group_pb; | ||
| group_pb.set_name("startup"); | ||
| group_pb.set_mode(resource_manager::GroupMode::RUMode); | ||
| group_pb.set_priority(ResourceGroup::UserMediumPriority); | ||
| auto * settings = group_pb.mutable_r_u_settings()->mutable_r_u()->mutable_settings(); | ||
| settings->set_fill_rate(fill_rate); | ||
| settings->set_burst_limit(global_burst_limit); | ||
|
|
||
| ResourceGroup group(group_pb, start_time); | ||
| group.smooth_ru_consumption_speed = 0; | ||
| group.consumeResource(consumed_tokens, 0); | ||
|
|
||
| EXPECT_FALSE(group.shouldRefillToken(start_time + ResourceGroup::REFILL_TOKEN_INTERVAL / 2)); | ||
| EXPECT_TRUE(group.shouldRefillToken(start_time + ResourceGroup::REFILL_TOKEN_INTERVAL)); | ||
|
|
||
| const auto request_info = group.buildRequestInfoIfNecessary(start_time + ResourceGroup::REFILL_TOKEN_INTERVAL); | ||
| ASSERT_TRUE(request_info.has_value()); | ||
| EXPECT_DOUBLE_EQ(request_info->acquire_tokens, fill_rate * (1 - ResourceGroup::REFILL_TOKEN_THRESHOLD_RATE)); | ||
| EXPECT_DOUBLE_EQ(request_info->ru_consumption_delta, consumed_tokens); | ||
|
|
||
| const auto response_time = start_time + ResourceGroup::REFILL_TOKEN_INTERVAL; | ||
| group.updateNormalMode(request_info->acquire_tokens, global_burst_limit, response_time); | ||
| EXPECT_FALSE(group.lowToken()); | ||
| EXPECT_TRUE(group.shouldRefillToken(response_time + ResourceGroup::REFILL_TOKEN_INTERVAL)); | ||
|
|
||
| const auto next_request_info | ||
| = group.buildRequestInfoIfNecessary(response_time + ResourceGroup::REFILL_TOKEN_INTERVAL); | ||
| ASSERT_TRUE(next_request_info.has_value()); | ||
| EXPECT_DOUBLE_EQ( | ||
| next_request_info->acquire_tokens, | ||
| global_burst_limit * (1 - ResourceGroup::REFILL_TOKEN_THRESHOLD_RATE)); | ||
| } | ||
|
|
||
| TEST(LocalAdmissionControllerTest, RefillTokensIncrementallyAboveLowWatermark) | ||
| { | ||
| constexpr double capacity = 10000; | ||
| constexpr double consumed_tokens = 3000; | ||
|
|
||
| ResourceGroup group( | ||
| "normal_refill", | ||
| ResourceGroup::UserMediumPriority, | ||
| capacity, | ||
| /*burstable_=*/false); | ||
| group.smooth_ru_consumption_speed = 0; | ||
| group.consumeResource(consumed_tokens, 0); | ||
|
|
||
| ASSERT_TRUE(group.shouldRefillToken(SteadyClock::now())); | ||
| auto request_info = group.buildRequestInfoIfNecessary(SteadyClock::now()); | ||
| ASSERT_TRUE(request_info.has_value()); | ||
| EXPECT_DOUBLE_EQ(request_info->acquire_tokens, capacity * (1 - ResourceGroup::REFILL_TOKEN_THRESHOLD_RATE)); | ||
| EXPECT_DOUBLE_EQ(request_info->ru_consumption_delta, consumed_tokens); | ||
| } | ||
|
|
||
| TEST(LocalAdmissionControllerTest, RefillTokensUsesPredictedConsumptionWhenHigher) | ||
| { | ||
| constexpr double capacity = 10000; | ||
| constexpr double consumed_tokens = 3000; | ||
|
|
||
| ResourceGroup group( | ||
| "high_speed", | ||
| ResourceGroup::UserMediumPriority, | ||
| capacity, | ||
| /*burstable_=*/false); | ||
| group.smooth_ru_consumption_speed = 4000; | ||
| group.consumeResource(consumed_tokens, 0); | ||
|
|
||
| auto request_info = group.buildRequestInfoIfNecessary(SteadyClock::now()); | ||
| ASSERT_TRUE(request_info.has_value()); | ||
| EXPECT_DOUBLE_EQ(request_info->acquire_tokens, consumed_tokens); | ||
| } | ||
|
|
||
| TEST(LocalAdmissionControllerTest, LowTokenRefillBypassesIncrementalLimit) | ||
| { | ||
| constexpr double capacity = 10000; | ||
| constexpr double consumed_tokens = 7500; | ||
|
|
||
| ResourceGroup group( | ||
| "emergency_refill", | ||
| ResourceGroup::UserMediumPriority, | ||
| capacity, | ||
| /*burstable_=*/false); | ||
| group.smooth_ru_consumption_speed = 0; | ||
| group.consumeResource(consumed_tokens, 0); | ||
|
|
||
| auto request_info = group.buildRequestInfoIfNecessary(SteadyClock::now()); | ||
| ASSERT_TRUE(request_info.has_value()); | ||
| EXPECT_DOUBLE_EQ(request_info->acquire_tokens, consumed_tokens); | ||
| } | ||
| } // namespace DB::tests | ||
This file contains hidden or 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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🎯 Functional Correctness | 🟡 Minor | ⚡ Quick win
🧩 Analysis chain
🌐 Web query:
According to the C++ standard library, what value doesstd::chrono::seconds{1} / 2produce?💡 Result:
In the C++ standard library, the expression
std::chrono::seconds{1} / 2produces astd::chrono::secondsobject with a count of 0 [1][2][3]. When astd::chrono::duration(such asstd::chrono::seconds) is divided by a scalar value, the division is performed on the internal tick count of the duration [1][4]. The result of this operation is a newstd::chrono::durationobject of the same type [1][2][3]. In this case: 1.std::chrono::seconds{1}has an internal tick count of 1. 2. The division operator performs integer division:1 / 2, which equals 0. 3. The resulting object isstd::chrono::seconds{0}.Citations:
🏁 Script executed:
Repository: pingcap/tiflash
Length of output: 10265
Use a non-zero sub-second duration.
ResourceGroup::REFILL_TOKEN_INTERVAL / 2evaluates to zero seconds. The assertion therefore checksstart_time, not a time before the refill interval. Usestd::chrono::milliseconds(500)instead.🤖 Prompt for AI Agents