2022-02-15 08:03:37 +08:00
|
|
|
/*
|
|
|
|
* TagThrottler.h
|
|
|
|
*
|
|
|
|
* This source file is part of the FoundationDB open source project
|
|
|
|
*
|
|
|
|
* Copyright 2013-2022 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,
|
2022-03-07 10:09:17 +08:00
|
|
|
*
|
2022-02-15 08:03:37 +08:00
|
|
|
* 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 "fdbserver/TagThrottler.h"
|
2022-03-07 10:09:17 +08:00
|
|
|
#include "fdbserver/RkTagThrottleCollection.h"
|
2022-03-24 03:29:38 +08:00
|
|
|
#include "flow/actorcompiler.h" // must be last include
|
2022-02-15 10:17:55 +08:00
|
|
|
|
|
|
|
class TagThrottlerImpl {
|
2022-02-15 11:08:23 +08:00
|
|
|
Database db;
|
|
|
|
UID id;
|
2022-02-15 10:17:55 +08:00
|
|
|
RkTagThrottleCollection throttledTags;
|
|
|
|
uint64_t throttledTagChangeId{ 0 };
|
|
|
|
bool autoThrottlingEnabled{ false };
|
2022-03-03 11:28:41 +08:00
|
|
|
Future<Void> expiredTagThrottleCleanup;
|
2022-02-15 10:17:55 +08:00
|
|
|
|
|
|
|
ACTOR static Future<Void> monitorThrottlingChanges(TagThrottlerImpl* self) {
|
2022-02-15 08:03:37 +08:00
|
|
|
state bool committed = false;
|
|
|
|
loop {
|
2022-02-15 11:08:23 +08:00
|
|
|
state ReadYourWritesTransaction tr(self->db);
|
2022-02-15 08:03:37 +08:00
|
|
|
|
|
|
|
loop {
|
|
|
|
try {
|
|
|
|
tr.setOption(FDBTransactionOptions::ACCESS_SYSTEM_KEYS);
|
|
|
|
tr.setOption(FDBTransactionOptions::PRIORITY_SYSTEM_IMMEDIATE);
|
|
|
|
|
|
|
|
state Future<RangeResult> throttledTagKeys = tr.getRange(tagThrottleKeys, CLIENT_KNOBS->TOO_MANY);
|
|
|
|
state Future<Optional<Value>> autoThrottlingEnabled = tr.get(tagThrottleAutoEnabledKey);
|
|
|
|
|
|
|
|
if (!committed) {
|
|
|
|
BinaryWriter limitWriter(Unversioned());
|
|
|
|
limitWriter << SERVER_KNOBS->MAX_MANUAL_THROTTLED_TRANSACTION_TAGS;
|
|
|
|
tr.set(tagThrottleLimitKey, limitWriter.toValue());
|
|
|
|
}
|
|
|
|
|
|
|
|
wait(success(throttledTagKeys) && success(autoThrottlingEnabled));
|
|
|
|
|
|
|
|
if (autoThrottlingEnabled.get().present() &&
|
|
|
|
autoThrottlingEnabled.get().get() == LiteralStringRef("0")) {
|
2022-07-20 04:15:51 +08:00
|
|
|
CODE_PROBE(true, "Auto-throttling disabled");
|
2022-02-15 08:03:37 +08:00
|
|
|
if (self->autoThrottlingEnabled) {
|
2022-02-15 11:08:23 +08:00
|
|
|
TraceEvent("AutoTagThrottlingDisabled", self->id).log();
|
2022-02-15 08:03:37 +08:00
|
|
|
}
|
|
|
|
self->autoThrottlingEnabled = false;
|
|
|
|
} else if (autoThrottlingEnabled.get().present() &&
|
|
|
|
autoThrottlingEnabled.get().get() == LiteralStringRef("1")) {
|
2022-07-20 04:15:51 +08:00
|
|
|
CODE_PROBE(true, "Auto-throttling enabled");
|
2022-02-15 08:03:37 +08:00
|
|
|
if (!self->autoThrottlingEnabled) {
|
2022-02-15 11:08:23 +08:00
|
|
|
TraceEvent("AutoTagThrottlingEnabled", self->id).log();
|
2022-02-15 08:03:37 +08:00
|
|
|
}
|
|
|
|
self->autoThrottlingEnabled = true;
|
|
|
|
} else {
|
2022-07-20 04:15:51 +08:00
|
|
|
CODE_PROBE(true, "Auto-throttling unspecified");
|
2022-02-15 08:03:37 +08:00
|
|
|
if (autoThrottlingEnabled.get().present()) {
|
2022-02-15 11:08:23 +08:00
|
|
|
TraceEvent(SevWarnAlways, "InvalidAutoTagThrottlingValue", self->id)
|
2022-02-15 08:03:37 +08:00
|
|
|
.detail("Value", autoThrottlingEnabled.get().get());
|
|
|
|
}
|
|
|
|
self->autoThrottlingEnabled = SERVER_KNOBS->AUTO_TAG_THROTTLING_ENABLED;
|
|
|
|
if (!committed)
|
|
|
|
tr.set(tagThrottleAutoEnabledKey,
|
|
|
|
LiteralStringRef(self->autoThrottlingEnabled ? "1" : "0"));
|
|
|
|
}
|
|
|
|
|
|
|
|
RkTagThrottleCollection updatedTagThrottles;
|
|
|
|
|
2022-02-15 11:08:23 +08:00
|
|
|
TraceEvent("RatekeeperReadThrottledTags", self->id)
|
2022-02-15 08:03:37 +08:00
|
|
|
.detail("NumThrottledTags", throttledTagKeys.get().size());
|
|
|
|
for (auto entry : throttledTagKeys.get()) {
|
|
|
|
TagThrottleKey tagKey = TagThrottleKey::fromKey(entry.key);
|
|
|
|
TagThrottleValue tagValue = TagThrottleValue::fromValue(entry.value);
|
|
|
|
|
|
|
|
ASSERT(tagKey.tags.size() == 1); // Currently, only 1 tag per throttle is supported
|
|
|
|
|
|
|
|
if (tagValue.expirationTime == 0 ||
|
|
|
|
tagValue.expirationTime > now() + tagValue.initialDuration) {
|
2022-07-20 04:15:51 +08:00
|
|
|
CODE_PROBE(true, "Converting tag throttle duration to absolute time");
|
2022-02-15 08:03:37 +08:00
|
|
|
tagValue.expirationTime = now() + tagValue.initialDuration;
|
|
|
|
BinaryWriter wr(IncludeVersion(ProtocolVersion::withTagThrottleValueReason()));
|
|
|
|
wr << tagValue;
|
|
|
|
state Value value = wr.toValue();
|
|
|
|
|
|
|
|
tr.set(entry.key, value);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (tagValue.expirationTime > now()) {
|
|
|
|
TransactionTag tag = *tagKey.tags.begin();
|
|
|
|
Optional<ClientTagThrottleLimits> oldLimits =
|
|
|
|
self->throttledTags.getManualTagThrottleLimits(tag, tagKey.priority);
|
|
|
|
|
|
|
|
if (tagKey.throttleType == TagThrottleType::AUTO) {
|
|
|
|
updatedTagThrottles.autoThrottleTag(
|
2022-02-15 11:08:23 +08:00
|
|
|
self->id, tag, 0, tagValue.tpsRate, tagValue.expirationTime);
|
2022-03-24 06:01:59 +08:00
|
|
|
updatedTagThrottles.incrementBusyTagCount(tagValue.reason);
|
2022-02-15 08:03:37 +08:00
|
|
|
} else {
|
2022-02-15 11:08:23 +08:00
|
|
|
updatedTagThrottles.manualThrottleTag(self->id,
|
2022-02-15 08:03:37 +08:00
|
|
|
tag,
|
|
|
|
tagKey.priority,
|
|
|
|
tagValue.tpsRate,
|
|
|
|
tagValue.expirationTime,
|
|
|
|
oldLimits);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
self->throttledTags = std::move(updatedTagThrottles);
|
|
|
|
++self->throttledTagChangeId;
|
|
|
|
|
|
|
|
state Future<Void> watchFuture = tr.watch(tagThrottleSignalKey);
|
|
|
|
wait(tr.commit());
|
|
|
|
committed = true;
|
|
|
|
|
|
|
|
wait(watchFuture);
|
2022-02-15 11:08:23 +08:00
|
|
|
TraceEvent("RatekeeperThrottleSignaled", self->id).log();
|
2022-07-20 04:15:51 +08:00
|
|
|
CODE_PROBE(true, "Tag throttle changes detected");
|
2022-02-15 08:03:37 +08:00
|
|
|
break;
|
|
|
|
} catch (Error& e) {
|
2022-02-15 11:08:23 +08:00
|
|
|
TraceEvent("RatekeeperMonitorThrottlingChangesError", self->id).error(e);
|
2022-02-15 08:03:37 +08:00
|
|
|
wait(tr.onError(e));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-03-12 00:13:34 +08:00
|
|
|
Future<Void> tryUpdateAutoThrottling(TransactionTag tag, double rate, double busyness, TagThrottledReason reason) {
|
2022-02-15 10:54:12 +08:00
|
|
|
// NOTE: before the comparison with MIN_TAG_COST, the busiest tag rate also compares with MIN_TAG_PAGES_RATE
|
|
|
|
// currently MIN_TAG_PAGES_RATE > MIN_TAG_COST in our default knobs.
|
|
|
|
if (busyness > SERVER_KNOBS->AUTO_THROTTLE_TARGET_TAG_BUSYNESS && rate > SERVER_KNOBS->MIN_TAG_COST) {
|
2022-07-20 04:15:51 +08:00
|
|
|
CODE_PROBE(true, "Transaction tag auto-throttled");
|
2022-03-12 00:14:26 +08:00
|
|
|
Optional<double> clientRate = throttledTags.autoThrottleTag(id, tag, busyness);
|
2022-03-24 06:01:59 +08:00
|
|
|
// TODO: Increment tag throttle counts here?
|
2022-02-15 10:54:12 +08:00
|
|
|
if (clientRate.present()) {
|
|
|
|
TagSet tags;
|
|
|
|
tags.addTag(tag);
|
|
|
|
|
2022-02-15 11:08:23 +08:00
|
|
|
Reference<DatabaseContext> dbRef = Reference<DatabaseContext>::addRef(db.getPtr());
|
2022-02-15 10:54:12 +08:00
|
|
|
return ThrottleApi::throttleTags(dbRef,
|
|
|
|
tags,
|
|
|
|
clientRate.get(),
|
|
|
|
SERVER_KNOBS->AUTO_TAG_THROTTLE_DURATION,
|
|
|
|
TagThrottleType::AUTO,
|
|
|
|
TransactionPriority::DEFAULT,
|
|
|
|
now() + SERVER_KNOBS->AUTO_TAG_THROTTLE_DURATION,
|
|
|
|
reason);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return Void();
|
|
|
|
}
|
|
|
|
|
2022-02-15 10:17:55 +08:00
|
|
|
public:
|
2022-03-03 11:28:41 +08:00
|
|
|
TagThrottlerImpl(Database db, UID id) : db(db), id(id) {
|
|
|
|
expiredTagThrottleCleanup = recurring([this]() { ThrottleApi::expire(this->db.getReference()); },
|
|
|
|
SERVER_KNOBS->TAG_THROTTLE_EXPIRED_CLEANUP_INTERVAL);
|
|
|
|
}
|
2022-02-15 10:17:55 +08:00
|
|
|
Future<Void> monitorThrottlingChanges() { return monitorThrottlingChanges(this); }
|
|
|
|
|
|
|
|
void addRequests(TransactionTag tag, int count) { throttledTags.addRequests(tag, count); }
|
|
|
|
uint64_t getThrottledTagChangeId() const { return throttledTagChangeId; }
|
|
|
|
PrioritizedTransactionTagMap<ClientTagThrottleLimits> getClientRates() {
|
|
|
|
return throttledTags.getClientRates(autoThrottlingEnabled);
|
|
|
|
}
|
|
|
|
int64_t autoThrottleCount() const { return throttledTags.autoThrottleCount(); }
|
2022-03-07 10:09:17 +08:00
|
|
|
uint32_t busyReadTagCount() const { return throttledTags.getBusyReadTagCount(); }
|
|
|
|
uint32_t busyWriteTagCount() const { return throttledTags.getBusyWriteTagCount(); }
|
2022-02-15 10:17:55 +08:00
|
|
|
int64_t manualThrottleCount() const { return throttledTags.manualThrottleCount(); }
|
|
|
|
bool isAutoThrottlingEnabled() const { return autoThrottlingEnabled; }
|
2022-02-15 10:54:12 +08:00
|
|
|
|
2022-03-12 00:13:34 +08:00
|
|
|
Future<Void> tryUpdateAutoThrottling(StorageQueueInfo const& ss) {
|
2022-02-15 10:54:12 +08:00
|
|
|
// NOTE: we just keep it simple and don't differentiate write-saturation and read-saturation at the moment. In
|
|
|
|
// most of situation, this works. More indicators besides queue size and durability lag could be investigated in
|
|
|
|
// the future
|
2022-03-12 00:13:34 +08:00
|
|
|
auto storageQueue = ss.getStorageQueueBytes();
|
|
|
|
auto storageDurabilityLag = ss.getDurabilityLag();
|
2022-04-03 04:29:34 +08:00
|
|
|
std::vector<Future<Void>> futures;
|
2022-02-15 10:54:12 +08:00
|
|
|
if (storageQueue > SERVER_KNOBS->AUTO_TAG_THROTTLE_STORAGE_QUEUE_BYTES ||
|
|
|
|
storageDurabilityLag > SERVER_KNOBS->AUTO_TAG_THROTTLE_DURABILITY_LAG_VERSIONS) {
|
2022-03-12 00:13:34 +08:00
|
|
|
for (const auto& busyWriteTag : ss.busiestWriteTags) {
|
2022-04-03 04:29:34 +08:00
|
|
|
futures.push_back(tryUpdateAutoThrottling(busyWriteTag.tag,
|
|
|
|
busyWriteTag.rate,
|
|
|
|
busyWriteTag.fractionalBusyness,
|
|
|
|
TagThrottledReason::BUSY_WRITE));
|
2022-02-15 10:54:12 +08:00
|
|
|
}
|
2022-03-12 00:13:34 +08:00
|
|
|
for (const auto& busyReadTag : ss.busiestReadTags) {
|
2022-04-03 04:29:34 +08:00
|
|
|
futures.push_back(tryUpdateAutoThrottling(
|
|
|
|
busyReadTag.tag, busyReadTag.rate, busyReadTag.fractionalBusyness, TagThrottledReason::BUSY_READ));
|
2022-02-15 10:54:12 +08:00
|
|
|
}
|
|
|
|
}
|
2022-04-03 04:29:34 +08:00
|
|
|
return waitForAll(futures);
|
2022-02-15 10:17:55 +08:00
|
|
|
}
|
2022-02-15 10:54:12 +08:00
|
|
|
|
2022-02-15 08:03:37 +08:00
|
|
|
}; // class TagThrottlerImpl
|
|
|
|
|
2022-02-15 11:08:23 +08:00
|
|
|
TagThrottler::TagThrottler(Database db, UID id) : impl(PImpl<TagThrottlerImpl>::create(db, id)) {}
|
2022-02-15 10:17:55 +08:00
|
|
|
TagThrottler::~TagThrottler() = default;
|
2022-02-15 08:03:37 +08:00
|
|
|
Future<Void> TagThrottler::monitorThrottlingChanges() {
|
2022-02-15 10:17:55 +08:00
|
|
|
return impl->monitorThrottlingChanges();
|
|
|
|
}
|
|
|
|
void TagThrottler::addRequests(TransactionTag tag, int count) {
|
|
|
|
impl->addRequests(tag, count);
|
|
|
|
}
|
|
|
|
uint64_t TagThrottler::getThrottledTagChangeId() const {
|
|
|
|
return impl->getThrottledTagChangeId();
|
|
|
|
}
|
|
|
|
PrioritizedTransactionTagMap<ClientTagThrottleLimits> TagThrottler::getClientRates() {
|
|
|
|
return impl->getClientRates();
|
|
|
|
}
|
|
|
|
int64_t TagThrottler::autoThrottleCount() const {
|
|
|
|
return impl->autoThrottleCount();
|
|
|
|
}
|
|
|
|
uint32_t TagThrottler::busyReadTagCount() const {
|
|
|
|
return impl->busyReadTagCount();
|
|
|
|
}
|
|
|
|
uint32_t TagThrottler::busyWriteTagCount() const {
|
|
|
|
return impl->busyWriteTagCount();
|
|
|
|
}
|
|
|
|
int64_t TagThrottler::manualThrottleCount() const {
|
|
|
|
return impl->manualThrottleCount();
|
|
|
|
}
|
|
|
|
bool TagThrottler::isAutoThrottlingEnabled() const {
|
|
|
|
return impl->isAutoThrottlingEnabled();
|
|
|
|
}
|
2022-03-12 00:13:34 +08:00
|
|
|
Future<Void> TagThrottler::tryUpdateAutoThrottling(StorageQueueInfo const& ss) {
|
|
|
|
return impl->tryUpdateAutoThrottling(ss);
|
2022-02-15 08:03:37 +08:00
|
|
|
}
|