2020-04-10 07:55:56 +08:00
|
|
|
/*
|
|
|
|
* TagThrottle.actor.cpp
|
|
|
|
*
|
|
|
|
* This source file is part of the FoundationDB open source project
|
|
|
|
*
|
|
|
|
* Copyright 2013-2020 Apple Inc. and the FoundationDB project authors
|
|
|
|
*
|
|
|
|
* 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 "fdbclient/TagThrottle.h"
|
|
|
|
#include "fdbclient/MasterProxyInterface.h"
|
|
|
|
#include "fdbclient/DatabaseContext.h"
|
|
|
|
|
|
|
|
#include "flow/actorcompiler.h" // has to be last include
|
|
|
|
|
2020-04-17 05:43:22 +08:00
|
|
|
void TagSet::addTag(TransactionTagRef tag) {
|
2020-04-10 07:55:56 +08:00
|
|
|
ASSERT(CLIENT_KNOBS->MAX_TRANSACTION_TAG_LENGTH < 256); // Tag length is encoded with a single byte
|
2020-05-05 01:11:36 +08:00
|
|
|
ASSERT(CLIENT_KNOBS->MAX_TAGS_PER_TRANSACTION < 256); // Number of tags is encoded with a single byte
|
2020-04-10 07:55:56 +08:00
|
|
|
|
|
|
|
if(tag.size() > CLIENT_KNOBS->MAX_TRANSACTION_TAG_LENGTH) {
|
|
|
|
throw tag_too_long();
|
|
|
|
}
|
|
|
|
if(tags.size() >= CLIENT_KNOBS->MAX_TAGS_PER_TRANSACTION) {
|
|
|
|
throw too_many_tags();
|
|
|
|
}
|
|
|
|
|
2020-05-05 06:49:55 +08:00
|
|
|
auto result = tags.insert(TransactionTagRef(arena, tag));
|
|
|
|
if(result.second) {
|
|
|
|
bytes += tag.size();
|
|
|
|
}
|
2020-04-10 07:55:56 +08:00
|
|
|
}
|
|
|
|
|
2020-04-24 11:50:40 +08:00
|
|
|
size_t TagSet::size() const {
|
2020-04-10 07:55:56 +08:00
|
|
|
return tags.size();
|
|
|
|
}
|
|
|
|
|
2020-04-24 11:50:40 +08:00
|
|
|
// Format for throttle key:
|
|
|
|
//
|
|
|
|
// tagThrottleKeysPrefix + [auto-throttled (1-byte 0/1)] + [priority (1-byte)] + [tag list]
|
|
|
|
// tag list consists of 1 or more consecutive tags, each encoded as:
|
|
|
|
// tag.size() (1 byte) + tag's bytes. For example, tag 'foo' is: \x03foo
|
|
|
|
// The tags are listed in sorted order
|
|
|
|
//
|
|
|
|
// Currently, the throttle API supports only 1 tag per throttle
|
|
|
|
Key TagThrottleKey::toKey() const {
|
|
|
|
ASSERT(CLIENT_KNOBS->MAX_TRANSACTION_TAG_LENGTH < 256);
|
|
|
|
ASSERT(tags.size() > 0);
|
|
|
|
|
|
|
|
ASSERT(tags.size() == 1); // SOMEDAY: support multiple tags per throttle
|
|
|
|
|
|
|
|
int size = tagThrottleKeysPrefix.size() + tags.size() + 2;
|
|
|
|
for(auto tag : tags) {
|
|
|
|
ASSERT(tag.size() <= CLIENT_KNOBS->MAX_TRANSACTION_TAG_LENGTH);
|
|
|
|
size += tag.size();
|
|
|
|
}
|
|
|
|
|
|
|
|
Key result;
|
|
|
|
|
|
|
|
uint8_t* str = new (result.arena()) uint8_t[size];
|
|
|
|
result.contents() = StringRef(str, size);
|
|
|
|
|
|
|
|
memcpy(str, tagThrottleKeysPrefix.begin(), tagThrottleKeysPrefix.size());
|
|
|
|
str += tagThrottleKeysPrefix.size();
|
|
|
|
|
|
|
|
*(str++) = autoThrottled ? 1 : 0;
|
|
|
|
*(str++) = (uint8_t)priority;
|
|
|
|
|
|
|
|
for(auto tag : tags) {
|
|
|
|
*(str++) = (uint8_t)tag.size();
|
|
|
|
if(tag.size() > 0) {
|
|
|
|
memcpy(str, tag.begin(), tag.size());
|
|
|
|
str += tag.size();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
TagThrottleKey TagThrottleKey::fromKey(const KeyRef& key) {
|
|
|
|
const uint8_t *str = key.substr(tagThrottleKeysPrefix.size()).begin();
|
|
|
|
bool autoThrottled = *(str++) != 0;
|
2020-04-25 02:31:16 +08:00
|
|
|
TransactionPriority priority = TransactionPriority(*(str++));
|
2020-04-24 11:50:40 +08:00
|
|
|
TagSet tags;
|
|
|
|
|
|
|
|
while(str < key.end()) {
|
|
|
|
uint8_t size = *(str++);
|
|
|
|
tags.addTag(TransactionTagRef(str, size));
|
|
|
|
str += size;
|
|
|
|
}
|
|
|
|
|
|
|
|
return TagThrottleKey(tags, autoThrottled, priority);
|
|
|
|
}
|
|
|
|
|
|
|
|
TagThrottleValue TagThrottleValue::fromValue(const ValueRef& value) {
|
|
|
|
TagThrottleValue throttleValue;
|
|
|
|
BinaryReader reader(value, IncludeVersion());
|
|
|
|
reader >> throttleValue;
|
|
|
|
return throttleValue;
|
|
|
|
}
|
|
|
|
|
2020-04-17 23:07:01 +08:00
|
|
|
namespace ThrottleApi {
|
2020-04-10 07:55:56 +08:00
|
|
|
void signalThrottleChange(Transaction &tr) {
|
|
|
|
tr.atomicOp(tagThrottleSignalKey, LiteralStringRef("XXXXXXXXXX\x00\x00\x00\x00"), MutationRef::SetVersionstampedValue);
|
|
|
|
}
|
|
|
|
|
2020-04-18 02:48:02 +08:00
|
|
|
ACTOR Future<Void> updateThrottleCount(Transaction *tr, int64_t delta) {
|
|
|
|
state Future<Optional<Value>> countVal = tr->get(tagThrottleCountKey);
|
|
|
|
state Future<Optional<Value>> limitVal = tr->get(tagThrottleLimitKey);
|
|
|
|
|
|
|
|
wait(success(countVal) && success(limitVal));
|
|
|
|
|
|
|
|
int64_t count = 0;
|
|
|
|
int64_t limit = 0;
|
|
|
|
|
|
|
|
if(countVal.get().present()) {
|
|
|
|
BinaryReader reader(countVal.get().get(), Unversioned());
|
|
|
|
reader >> count;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(limitVal.get().present()) {
|
|
|
|
BinaryReader reader(limitVal.get().get(), Unversioned());
|
|
|
|
reader >> limit;
|
|
|
|
}
|
|
|
|
|
|
|
|
count += delta;
|
|
|
|
|
|
|
|
if(count > limit) {
|
|
|
|
throw too_many_tag_throttles();
|
|
|
|
}
|
|
|
|
|
|
|
|
BinaryWriter writer(Unversioned());
|
|
|
|
writer << count;
|
|
|
|
|
|
|
|
tr->set(tagThrottleCountKey, writer.toValue());
|
|
|
|
return Void();
|
|
|
|
}
|
|
|
|
|
2020-04-24 11:50:40 +08:00
|
|
|
ACTOR Future<std::vector<TagThrottleInfo>> getThrottledTags(Database db, int limit) {
|
2020-04-10 07:55:56 +08:00
|
|
|
state Transaction tr(db);
|
|
|
|
|
|
|
|
loop {
|
|
|
|
try {
|
2020-04-24 11:50:40 +08:00
|
|
|
Standalone<RangeResultRef> throttles = wait(tr.getRange(tagThrottleKeys, limit));
|
|
|
|
std::vector<TagThrottleInfo> results;
|
|
|
|
for(auto throttle : throttles) {
|
|
|
|
results.push_back(TagThrottleInfo(TagThrottleKey::fromKey(throttle.key), TagThrottleValue::fromValue(throttle.value)));
|
2020-04-10 07:55:56 +08:00
|
|
|
}
|
|
|
|
return results;
|
|
|
|
}
|
|
|
|
catch(Error& e) {
|
|
|
|
wait(tr.onError(e));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-25 02:31:16 +08:00
|
|
|
ACTOR Future<Void> throttleTags(Database db, TagSet tags, double tpsRate, double initialDuration, bool autoThrottled, TransactionPriority priority, Optional<double> expirationTime) {
|
2020-04-10 07:55:56 +08:00
|
|
|
state Transaction tr(db);
|
2020-04-24 11:50:40 +08:00
|
|
|
state Key key = TagThrottleKey(tags, autoThrottled, priority).toKey();
|
2020-04-10 07:55:56 +08:00
|
|
|
|
2020-04-22 07:35:25 +08:00
|
|
|
ASSERT(initialDuration > 0);
|
|
|
|
|
2020-04-24 11:50:40 +08:00
|
|
|
TagThrottleValue throttle(tpsRate, expirationTime.present() ? expirationTime.get() : 0, initialDuration);
|
2020-04-10 07:55:56 +08:00
|
|
|
BinaryWriter wr(IncludeVersion());
|
|
|
|
wr << throttle;
|
|
|
|
state Value value = wr.toValue();
|
|
|
|
|
|
|
|
loop {
|
|
|
|
try {
|
2020-04-18 02:48:02 +08:00
|
|
|
if(!autoThrottled) {
|
|
|
|
Optional<Value> oldThrottle = wait(tr.get(key));
|
|
|
|
if(!oldThrottle.present()) {
|
|
|
|
wait(updateThrottleCount(&tr, 1));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-10 07:55:56 +08:00
|
|
|
tr.set(key, value);
|
|
|
|
|
|
|
|
if(!autoThrottled) {
|
|
|
|
signalThrottleChange(tr);
|
|
|
|
}
|
|
|
|
|
|
|
|
wait(tr.commit());
|
|
|
|
return Void();
|
|
|
|
}
|
|
|
|
catch(Error& e) {
|
|
|
|
wait(tr.onError(e));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-25 02:31:16 +08:00
|
|
|
ACTOR Future<bool> unthrottleTags(Database db, TagSet tags, bool autoThrottled, TransactionPriority priority) {
|
2020-04-10 07:55:56 +08:00
|
|
|
state Transaction tr(db);
|
2020-04-24 11:50:40 +08:00
|
|
|
state Key key = TagThrottleKey(tags, autoThrottled, priority).toKey();
|
2020-05-02 12:36:28 +08:00
|
|
|
state bool removed = false;
|
|
|
|
|
2020-04-10 07:55:56 +08:00
|
|
|
|
|
|
|
loop {
|
|
|
|
try {
|
|
|
|
state Optional<Value> value = wait(tr.get(key));
|
|
|
|
if(value.present()) {
|
2020-04-24 11:50:40 +08:00
|
|
|
if(!autoThrottled) {
|
2020-04-18 02:48:02 +08:00
|
|
|
wait(updateThrottleCount(&tr, -1));
|
|
|
|
}
|
|
|
|
|
2020-04-10 07:55:56 +08:00
|
|
|
tr.clear(key);
|
|
|
|
signalThrottleChange(tr);
|
|
|
|
|
2020-05-02 12:36:28 +08:00
|
|
|
// Report that we are removing this tag if we ever see it present.
|
|
|
|
// This protects us from getting confused if the transaction is maybe committed.
|
|
|
|
// It's ok if someone else actually ends up removing this tag at the same time
|
|
|
|
// and we aren't the ones to actually do it.
|
|
|
|
removed = true;
|
2020-04-10 07:55:56 +08:00
|
|
|
wait(tr.commit());
|
|
|
|
}
|
|
|
|
|
2020-05-02 12:36:28 +08:00
|
|
|
return removed;
|
2020-04-10 07:55:56 +08:00
|
|
|
}
|
|
|
|
catch(Error& e) {
|
|
|
|
wait(tr.onError(e));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-02 12:36:28 +08:00
|
|
|
ACTOR Future<bool> unthrottleTags(Database db, KeyRef beginKey, KeyRef endKey, bool onlyExpiredThrottles) {
|
2020-04-10 07:55:56 +08:00
|
|
|
state Transaction tr(db);
|
|
|
|
|
2020-04-24 11:50:40 +08:00
|
|
|
state KeySelector begin = firstGreaterOrEqual(beginKey);
|
2020-05-02 12:36:28 +08:00
|
|
|
state KeySelector end = firstGreaterOrEqual(endKey);
|
2020-04-10 07:55:56 +08:00
|
|
|
|
2020-05-02 12:36:28 +08:00
|
|
|
state bool removed = false;
|
2020-04-10 07:55:56 +08:00
|
|
|
|
|
|
|
loop {
|
|
|
|
try {
|
|
|
|
state Standalone<RangeResultRef> tags = wait(tr.getRange(begin, end, 1000));
|
2020-05-02 12:36:28 +08:00
|
|
|
state uint64_t unthrottledTags = 0;
|
2020-04-18 02:48:02 +08:00
|
|
|
uint64_t manualUnthrottledTags = 0;
|
2020-04-10 07:55:56 +08:00
|
|
|
for(auto tag : tags) {
|
2020-05-02 12:36:28 +08:00
|
|
|
if(onlyExpiredThrottles) {
|
|
|
|
double expirationTime = TagThrottleValue::fromValue(tag.value).expirationTime;
|
|
|
|
if(expirationTime == 0 || expirationTime > now()) {
|
|
|
|
continue;
|
|
|
|
}
|
2020-04-10 07:55:56 +08:00
|
|
|
}
|
2020-05-02 12:36:28 +08:00
|
|
|
|
|
|
|
bool autoThrottled = TagThrottleKey::fromKey(tag.key).autoThrottled;
|
|
|
|
if(!autoThrottled) {
|
2020-04-18 02:48:02 +08:00
|
|
|
++manualUnthrottledTags;
|
2020-04-10 07:55:56 +08:00
|
|
|
}
|
2020-04-24 11:50:40 +08:00
|
|
|
|
2020-05-02 12:36:28 +08:00
|
|
|
removed = true;
|
2020-04-24 11:50:40 +08:00
|
|
|
tr.clear(tag.key);
|
2020-04-10 07:55:56 +08:00
|
|
|
}
|
|
|
|
|
2020-04-18 02:48:02 +08:00
|
|
|
if(manualUnthrottledTags > 0) {
|
|
|
|
wait(updateThrottleCount(&tr, -manualUnthrottledTags));
|
|
|
|
}
|
|
|
|
|
2020-05-02 21:47:25 +08:00
|
|
|
if(unthrottledTags > 0) {
|
2020-04-10 07:55:56 +08:00
|
|
|
signalThrottleChange(tr);
|
|
|
|
}
|
|
|
|
|
|
|
|
wait(tr.commit());
|
|
|
|
|
|
|
|
if(!tags.more) {
|
2020-05-02 21:47:25 +08:00
|
|
|
return removed;
|
2020-04-10 07:55:56 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
ASSERT(tags.size() > 0);
|
|
|
|
begin = KeySelector(firstGreaterThan(tags[tags.size()-1].key), tags.arena());
|
|
|
|
}
|
|
|
|
catch(Error& e) {
|
|
|
|
wait(tr.onError(e));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-02 12:36:28 +08:00
|
|
|
Future<bool> unthrottleManual(Database db) {
|
|
|
|
return unthrottleTags(db, tagThrottleKeysPrefix, tagThrottleAutoKeysPrefix, false);
|
|
|
|
}
|
|
|
|
|
|
|
|
Future<bool> unthrottleAuto(Database db) {
|
|
|
|
return unthrottleTags(db, tagThrottleAutoKeysPrefix, tagThrottleKeys.end, false);
|
2020-04-10 07:55:56 +08:00
|
|
|
}
|
|
|
|
|
2020-05-02 12:36:28 +08:00
|
|
|
Future<bool> unthrottleAll(Database db) {
|
|
|
|
return unthrottleTags(db, tagThrottleKeys.begin, tagThrottleKeys.end, false);
|
2020-04-10 07:55:56 +08:00
|
|
|
}
|
|
|
|
|
2020-05-02 12:36:28 +08:00
|
|
|
Future<bool> expire(Database db) {
|
|
|
|
return unthrottleTags(db, tagThrottleKeys.begin, tagThrottleKeys.end, true);
|
2020-04-10 07:55:56 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
ACTOR Future<Void> enableAuto(Database db, bool enabled) {
|
|
|
|
state Transaction tr(db);
|
|
|
|
|
|
|
|
loop {
|
|
|
|
try {
|
|
|
|
Optional<Value> value = wait(tr.get(tagThrottleAutoEnabledKey));
|
|
|
|
if(!value.present() || (enabled && value.get() != LiteralStringRef("1") || (!enabled && value.get() != LiteralStringRef("0")))) {
|
|
|
|
tr.set(tagThrottleAutoEnabledKey, LiteralStringRef(enabled ? "1" : "0"));
|
|
|
|
signalThrottleChange(tr);
|
|
|
|
|
|
|
|
wait(tr.commit());
|
|
|
|
}
|
|
|
|
return Void();
|
|
|
|
}
|
|
|
|
catch(Error& e) {
|
|
|
|
wait(tr.onError(e));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|