-
Notifications
You must be signed in to change notification settings - Fork 1.2k
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
Add kvstore rate limiter #2557
Merged
Merged
Add kvstore rate limiter #2557
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
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 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,70 @@ | ||
/* Copyright (c) 2021 vesoft inc. All rights reserved. | ||
* | ||
* This source code is licensed under Apache 2.0 License, | ||
* attached with Common Clause Condition 1.0, found in the LICENSES directory. | ||
*/ | ||
|
||
#pragma once | ||
|
||
#include <folly/TokenBucket.h> | ||
|
||
#include "common/base/Base.h" | ||
#include "common/thrift/ThriftTypes.h" | ||
#include "common/time/WallClock.h" | ||
|
||
namespace nebula { | ||
namespace kvstore { | ||
|
||
// A simple wrapper for foly::TokenBucket, it would limit the speed to rate_ * buckets_.size(). | ||
// For now, there are two major cases: snapshot (both for balance or catch up) and rebuild index. | ||
class RateLimiter { | ||
public: | ||
RateLimiter(int32_t rate, int32_t burstSize) | ||
: rate_(static_cast<double>(rate)), burstSize_(static_cast<double>(burstSize)) {} | ||
|
||
void add(GraphSpaceID spaceId, PartitionID partId) { | ||
std::lock_guard<std::mutex> guard(lock_); | ||
DCHECK(buckets_.find({spaceId, partId}) == buckets_.end()); | ||
// token will be available after 1 second, to prevent speed spike at the beginning | ||
auto now = time::WallClock::fastNowInSec(); | ||
folly::TokenBucket bucket(rate_, burstSize_, static_cast<double>(now + 1)); | ||
buckets_.emplace(std::make_pair(spaceId, partId), std::move(bucket)); | ||
} | ||
|
||
void remove(GraphSpaceID spaceId, PartitionID partId) { | ||
std::lock_guard<std::mutex> guard(lock_); | ||
DCHECK(buckets_.find({spaceId, partId}) != buckets_.end()); | ||
buckets_.erase({spaceId, partId}); | ||
} | ||
|
||
// Caller must make sure the **the parition has been add, and won't be removed during consume.** | ||
// Snaphot and rebuild index follow this principle by design. | ||
void consume(GraphSpaceID spaceId, PartitionID partId, size_t toConsume) { | ||
DCHECK(buckets_.find({spaceId, partId}) != buckets_.end()); | ||
auto iter = buckets_.find({spaceId, partId}); | ||
if (iter != buckets_.end()) { | ||
if (toConsume > burstSize_) { | ||
// consumeWithBorrowAndWait do nothing when toConsume > burstSize_, we sleep 1s instead | ||
std::this_thread::sleep_for(std::chrono::seconds(1)); | ||
} else { | ||
// If there are enouth tokens, consume and return immediately. | ||
// If not, cosume anyway, but sleep enough time before return. | ||
auto now = time::WallClock::fastNowInSec(); | ||
iter->second.consumeWithBorrowAndWait(static_cast<double>(toConsume), | ||
static_cast<double>(now)); | ||
} | ||
} else { | ||
LOG_EVERY_N(WARNING, 100) << folly::format( | ||
"Rate limiter is not available for [{},{}]", spaceId, partId); | ||
} | ||
} | ||
|
||
private: | ||
std::unordered_map<std::pair<GraphSpaceID, PartitionID>, folly::TokenBucket> buckets_; | ||
std::mutex lock_; | ||
double rate_; | ||
double burstSize_; | ||
}; | ||
|
||
} // namespace kvstore | ||
} // namespace nebula |
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
Oops, something went wrong.
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.
Is it redundant?
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.
Yeah, just to prevent misuse.
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.
Doesn't it need to be locked here?
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.
There won't be concurrent add/remove/consume of a {spaceId, partId}. Caller must make sure part has been added, and won't remove during consume. I'll add some comments.