2021-04-15 13:06:37 +08:00
|
|
|
/*
|
2021-04-17 08:58:00 +08:00
|
|
|
* SimpleConfigTransaction.actor.cpp
|
2021-04-15 13:06:37 +08:00
|
|
|
*
|
|
|
|
* This source file is part of the FoundationDB open source project
|
|
|
|
*
|
|
|
|
* Copyright 2013-2018 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 <algorithm>
|
|
|
|
|
|
|
|
#include "fdbclient/CommitTransaction.h"
|
2021-06-03 13:14:58 +08:00
|
|
|
#include "fdbclient/DatabaseContext.h"
|
2021-06-03 14:40:52 +08:00
|
|
|
#include "fdbclient/IKnobCollection.h"
|
2021-06-03 01:04:46 +08:00
|
|
|
#include "fdbclient/SimpleConfigTransaction.h"
|
|
|
|
#include "fdbserver/Knobs.h"
|
2021-04-15 13:06:37 +08:00
|
|
|
#include "flow/Arena.h"
|
|
|
|
#include "flow/actorcompiler.h" // This must be the last #include.
|
|
|
|
|
2021-04-17 08:58:00 +08:00
|
|
|
class SimpleConfigTransactionImpl {
|
2021-05-24 16:25:50 +08:00
|
|
|
ConfigTransactionCommitRequest toCommit;
|
|
|
|
Future<Version> getVersionFuture;
|
2021-04-17 08:58:00 +08:00
|
|
|
ConfigTransactionInterface cti;
|
2021-04-23 11:43:23 +08:00
|
|
|
int numRetries{ 0 };
|
2021-05-01 00:43:18 +08:00
|
|
|
bool committed{ false };
|
2021-05-15 15:23:21 +08:00
|
|
|
Optional<UID> dID;
|
2021-06-03 13:14:58 +08:00
|
|
|
Database cx;
|
2021-04-15 13:06:37 +08:00
|
|
|
|
2021-05-01 00:34:36 +08:00
|
|
|
ACTOR static Future<Version> getReadVersion(SimpleConfigTransactionImpl* self) {
|
2021-05-15 15:23:21 +08:00
|
|
|
if (self->dID.present()) {
|
|
|
|
TraceEvent("SimpleConfigTransactionGettingReadVersion", self->dID.get());
|
|
|
|
}
|
2021-04-17 08:58:00 +08:00
|
|
|
ConfigTransactionGetVersionRequest req;
|
|
|
|
ConfigTransactionGetVersionReply reply =
|
|
|
|
wait(self->cti.getVersion.getReply(ConfigTransactionGetVersionRequest{}));
|
2021-05-15 15:23:21 +08:00
|
|
|
if (self->dID.present()) {
|
|
|
|
TraceEvent("SimpleConfigTransactionGotReadVersion", self->dID.get()).detail("Version", reply.version);
|
|
|
|
}
|
2021-04-15 13:06:37 +08:00
|
|
|
return reply.version;
|
|
|
|
}
|
|
|
|
|
2021-04-17 08:58:00 +08:00
|
|
|
ACTOR static Future<Optional<Value>> get(SimpleConfigTransactionImpl* self, KeyRef key) {
|
2021-05-24 16:25:50 +08:00
|
|
|
if (!self->getVersionFuture.isValid()) {
|
|
|
|
self->getVersionFuture = getReadVersion(self);
|
2021-04-15 13:06:37 +08:00
|
|
|
}
|
2021-06-07 15:29:36 +08:00
|
|
|
state ConfigKey configKey = ConfigKey::decodeKey(key);
|
2021-05-24 16:25:50 +08:00
|
|
|
Version version = wait(self->getVersionFuture);
|
2021-05-16 03:41:16 +08:00
|
|
|
if (self->dID.present()) {
|
|
|
|
TraceEvent("SimpleConfigTransactionGettingValue", self->dID.get())
|
|
|
|
.detail("ConfigClass", configKey.configClass)
|
|
|
|
.detail("KnobName", configKey.knobName);
|
|
|
|
}
|
2021-06-03 01:04:46 +08:00
|
|
|
ConfigTransactionGetReply reply =
|
|
|
|
wait(self->cti.get.getReply(ConfigTransactionGetRequest{ version, configKey }));
|
2021-05-16 03:41:16 +08:00
|
|
|
if (self->dID.present()) {
|
2021-06-03 01:04:46 +08:00
|
|
|
TraceEvent("SimpleConfigTransactionGotValue", self->dID.get())
|
|
|
|
.detail("Value", reply.value.get().toString());
|
|
|
|
}
|
|
|
|
if (reply.value.present()) {
|
|
|
|
return reply.value.get().toValue();
|
|
|
|
} else {
|
|
|
|
return {};
|
2021-05-16 03:41:16 +08:00
|
|
|
}
|
2021-04-15 13:06:37 +08:00
|
|
|
}
|
|
|
|
|
2021-05-29 18:58:18 +08:00
|
|
|
ACTOR static Future<Standalone<RangeResultRef>> getConfigClasses(SimpleConfigTransactionImpl* self) {
|
|
|
|
if (!self->getVersionFuture.isValid()) {
|
|
|
|
self->getVersionFuture = getReadVersion(self);
|
|
|
|
}
|
|
|
|
Version version = wait(self->getVersionFuture);
|
|
|
|
ConfigTransactionGetConfigClassesReply reply =
|
|
|
|
wait(self->cti.getClasses.getReply(ConfigTransactionGetConfigClassesRequest{ version }));
|
|
|
|
Standalone<RangeResultRef> result;
|
|
|
|
for (const auto& configClass : reply.configClasses) {
|
|
|
|
result.push_back_deep(result.arena(), KeyValueRef(configClass, ""_sr));
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
ACTOR static Future<Standalone<RangeResultRef>> getKnobs(SimpleConfigTransactionImpl* self,
|
|
|
|
Optional<Key> configClass) {
|
|
|
|
if (!self->getVersionFuture.isValid()) {
|
|
|
|
self->getVersionFuture = getReadVersion(self);
|
|
|
|
}
|
|
|
|
Version version = wait(self->getVersionFuture);
|
|
|
|
ConfigTransactionGetKnobsReply reply =
|
|
|
|
wait(self->cti.getKnobs.getReply(ConfigTransactionGetKnobsRequest{ version, configClass }));
|
|
|
|
Standalone<RangeResultRef> result;
|
|
|
|
for (const auto& knobName : reply.knobNames) {
|
|
|
|
result.push_back_deep(result.arena(), KeyValueRef(knobName, ""_sr));
|
2021-04-24 02:39:26 +08:00
|
|
|
}
|
2021-05-29 18:58:18 +08:00
|
|
|
return result;
|
2021-04-24 02:39:26 +08:00
|
|
|
}
|
|
|
|
|
2021-04-17 08:58:00 +08:00
|
|
|
ACTOR static Future<Void> commit(SimpleConfigTransactionImpl* self) {
|
2021-05-24 16:25:50 +08:00
|
|
|
if (!self->getVersionFuture.isValid()) {
|
|
|
|
self->getVersionFuture = getReadVersion(self);
|
2021-05-12 02:12:19 +08:00
|
|
|
}
|
2021-05-24 16:25:50 +08:00
|
|
|
wait(store(self->toCommit.version, self->getVersionFuture));
|
|
|
|
self->toCommit.annotation.timestamp = now();
|
|
|
|
wait(self->cti.commit.getReply(self->toCommit));
|
2021-05-01 00:43:18 +08:00
|
|
|
self->committed = true;
|
2021-04-15 13:06:37 +08:00
|
|
|
return Void();
|
|
|
|
}
|
|
|
|
|
|
|
|
public:
|
2021-06-03 13:14:58 +08:00
|
|
|
SimpleConfigTransactionImpl(Database const& cx) : cx(cx) {
|
|
|
|
auto coordinators = cx->getConnectionFile()->getConnectionString().coordinators();
|
2021-04-15 13:06:37 +08:00
|
|
|
std::sort(coordinators.begin(), coordinators.end());
|
2021-04-17 08:58:00 +08:00
|
|
|
cti = ConfigTransactionInterface(coordinators[0]);
|
2021-04-15 13:06:37 +08:00
|
|
|
}
|
|
|
|
|
2021-05-15 14:02:40 +08:00
|
|
|
SimpleConfigTransactionImpl(ConfigTransactionInterface const& cti) : cti(cti) {}
|
|
|
|
|
2021-04-16 03:44:45 +08:00
|
|
|
void set(KeyRef key, ValueRef value) {
|
2021-05-30 02:48:47 +08:00
|
|
|
if (key == configTransactionDescriptionKey) {
|
2021-05-24 16:25:50 +08:00
|
|
|
toCommit.annotation.description = KeyRef(toCommit.arena, value);
|
2021-05-18 04:02:55 +08:00
|
|
|
} else {
|
2021-06-03 01:04:46 +08:00
|
|
|
ConfigKey configKey = ConfigKeyRef::decodeKey(key);
|
2021-06-10 11:50:00 +08:00
|
|
|
auto knobValue = IKnobCollection::parseKnobValue(
|
|
|
|
configKey.knobName.toString(), value.toString(), IKnobCollection::Type::TEST);
|
2021-06-12 23:21:28 +08:00
|
|
|
toCommit.mutations.emplace_back_deep(toCommit.arena, configKey, knobValue.contents());
|
2021-05-15 14:02:40 +08:00
|
|
|
}
|
2021-04-16 03:44:45 +08:00
|
|
|
}
|
2021-04-15 13:06:37 +08:00
|
|
|
|
2021-04-25 04:37:15 +08:00
|
|
|
void clear(KeyRef key) {
|
2021-05-30 02:48:47 +08:00
|
|
|
if (key == configTransactionDescriptionKey) {
|
2021-05-24 16:25:50 +08:00
|
|
|
toCommit.annotation.description = ""_sr;
|
2021-05-18 04:02:55 +08:00
|
|
|
} else {
|
2021-06-03 12:36:34 +08:00
|
|
|
toCommit.mutations.emplace_back_deep(
|
|
|
|
toCommit.arena, ConfigKeyRef::decodeKey(key), Optional<KnobValueRef>{});
|
2021-05-18 04:02:55 +08:00
|
|
|
}
|
2021-04-15 13:06:37 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
Future<Optional<Value>> get(KeyRef key) { return get(this, key); }
|
|
|
|
|
2021-05-29 18:58:18 +08:00
|
|
|
Future<Standalone<RangeResultRef>> getRange(KeyRangeRef keys) {
|
|
|
|
if (keys == configClassKeys) {
|
|
|
|
return getConfigClasses(this);
|
2021-05-30 02:48:47 +08:00
|
|
|
} else if (keys == globalConfigKnobKeys) {
|
2021-05-29 19:31:35 +08:00
|
|
|
return getKnobs(this, {});
|
2021-05-30 02:48:47 +08:00
|
|
|
} else if (configKnobKeys.contains(keys) && keys.singleKeyRange()) {
|
|
|
|
const auto configClass = keys.begin.removePrefix(configKnobKeys.begin);
|
2021-05-29 18:58:18 +08:00
|
|
|
return getKnobs(this, configClass);
|
|
|
|
} else {
|
|
|
|
throw invalid_config_db_range_read();
|
|
|
|
}
|
|
|
|
}
|
2021-04-24 02:39:26 +08:00
|
|
|
|
2021-04-15 13:06:37 +08:00
|
|
|
Future<Void> commit() { return commit(this); }
|
|
|
|
|
2021-04-23 11:43:23 +08:00
|
|
|
Future<Void> onError(Error const& e) {
|
|
|
|
// TODO: Improve this:
|
|
|
|
if (e.code() == error_code_transaction_too_old) {
|
|
|
|
reset();
|
|
|
|
return delay((1 << numRetries++) * 0.01 * deterministicRandom()->random01());
|
|
|
|
}
|
|
|
|
throw e;
|
|
|
|
}
|
|
|
|
|
2021-05-01 00:34:36 +08:00
|
|
|
Future<Version> getReadVersion() {
|
2021-05-24 16:25:50 +08:00
|
|
|
if (!getVersionFuture.isValid())
|
|
|
|
getVersionFuture = getReadVersion(this);
|
|
|
|
return getVersionFuture;
|
2021-04-23 11:43:23 +08:00
|
|
|
}
|
2021-04-16 03:44:45 +08:00
|
|
|
|
2021-05-24 16:25:50 +08:00
|
|
|
Optional<Version> getCachedReadVersion() const {
|
|
|
|
if (getVersionFuture.isValid() && getVersionFuture.isReady() && !getVersionFuture.isError()) {
|
|
|
|
return getVersionFuture.get();
|
2021-05-01 00:55:38 +08:00
|
|
|
} else {
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-05-24 16:25:50 +08:00
|
|
|
Version getCommittedVersion() const { return committed ? getVersionFuture.get() : ::invalidVersion; }
|
2021-05-01 00:43:18 +08:00
|
|
|
|
2021-04-16 03:44:45 +08:00
|
|
|
void reset() {
|
2021-05-24 16:25:50 +08:00
|
|
|
getVersionFuture = Future<Version>{};
|
|
|
|
toCommit = {};
|
2021-05-16 03:41:16 +08:00
|
|
|
committed = false;
|
2021-04-16 03:44:45 +08:00
|
|
|
}
|
2021-04-23 11:43:23 +08:00
|
|
|
|
|
|
|
void fullReset() {
|
|
|
|
numRetries = 0;
|
2021-05-16 03:41:16 +08:00
|
|
|
dID = {};
|
2021-04-23 11:43:23 +08:00
|
|
|
reset();
|
|
|
|
}
|
2021-04-15 13:06:37 +08:00
|
|
|
|
2021-05-24 16:25:50 +08:00
|
|
|
size_t getApproximateSize() const { return toCommit.expectedSize(); }
|
2021-05-15 15:23:21 +08:00
|
|
|
|
2021-05-16 03:41:16 +08:00
|
|
|
void debugTransaction(UID dID) {
|
|
|
|
this->dID = dID;
|
|
|
|
}
|
2021-05-14 07:39:58 +08:00
|
|
|
|
2021-06-03 13:14:58 +08:00
|
|
|
void checkDeferredError(Error const& deferredError) const {
|
|
|
|
if (deferredError.code() != invalid_error_code) {
|
|
|
|
throw deferredError;
|
|
|
|
}
|
|
|
|
if (cx.getPtr()) {
|
|
|
|
cx->checkDeferredError();
|
|
|
|
}
|
|
|
|
}
|
2021-05-01 00:34:36 +08:00
|
|
|
}; // SimpleConfigTransactionImpl
|
|
|
|
|
|
|
|
Future<Version> SimpleConfigTransaction::getReadVersion() {
|
2021-06-11 05:49:05 +08:00
|
|
|
return impl().getReadVersion();
|
2021-05-01 00:34:36 +08:00
|
|
|
}
|
|
|
|
|
2021-05-15 15:23:21 +08:00
|
|
|
Optional<Version> SimpleConfigTransaction::getCachedReadVersion() const {
|
2021-06-11 05:49:05 +08:00
|
|
|
return impl().getCachedReadVersion();
|
2021-05-01 00:34:36 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
Future<Optional<Value>> SimpleConfigTransaction::get(Key const& key, bool snapshot) {
|
2021-06-11 05:49:05 +08:00
|
|
|
return impl().get(key);
|
2021-05-01 00:34:36 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
Future<Standalone<RangeResultRef>> SimpleConfigTransaction::getRange(KeySelector const& begin,
|
|
|
|
KeySelector const& end,
|
|
|
|
int limit,
|
|
|
|
bool snapshot,
|
|
|
|
bool reverse) {
|
2021-06-11 05:49:05 +08:00
|
|
|
return impl().getRange(KeyRangeRef(begin.getKey(), end.getKey()));
|
2021-05-01 00:34:36 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
Future<Standalone<RangeResultRef>> SimpleConfigTransaction::getRange(KeySelector begin,
|
|
|
|
KeySelector end,
|
|
|
|
GetRangeLimits limits,
|
|
|
|
bool snapshot,
|
|
|
|
bool reverse) {
|
2021-06-11 05:49:05 +08:00
|
|
|
return impl().getRange(KeyRangeRef(begin.getKey(), end.getKey()));
|
2021-05-01 00:34:36 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void SimpleConfigTransaction::set(KeyRef const& key, ValueRef const& value) {
|
2021-06-11 05:49:05 +08:00
|
|
|
impl().set(key, value);
|
2021-04-15 13:06:37 +08:00
|
|
|
}
|
|
|
|
|
2021-05-01 00:34:36 +08:00
|
|
|
void SimpleConfigTransaction::clear(KeyRef const& key) {
|
2021-06-11 05:49:05 +08:00
|
|
|
impl().clear(key);
|
2021-04-25 04:37:15 +08:00
|
|
|
}
|
|
|
|
|
2021-04-17 08:58:00 +08:00
|
|
|
Future<Void> SimpleConfigTransaction::commit() {
|
2021-06-11 05:49:05 +08:00
|
|
|
return impl().commit();
|
2021-04-15 13:06:37 +08:00
|
|
|
}
|
|
|
|
|
2021-05-01 02:32:53 +08:00
|
|
|
Version SimpleConfigTransaction::getCommittedVersion() const {
|
2021-06-11 05:49:05 +08:00
|
|
|
return impl().getCommittedVersion();
|
2021-05-01 00:34:36 +08:00
|
|
|
}
|
|
|
|
|
2021-05-01 02:32:53 +08:00
|
|
|
int64_t SimpleConfigTransaction::getApproximateSize() const {
|
2021-06-11 05:49:05 +08:00
|
|
|
return impl().getApproximateSize();
|
2021-05-01 00:34:36 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void SimpleConfigTransaction::setOption(FDBTransactionOptions::Option option, Optional<StringRef> value) {
|
|
|
|
// TODO: Support using this option to determine atomicity
|
|
|
|
}
|
|
|
|
|
2021-04-17 08:58:00 +08:00
|
|
|
Future<Void> SimpleConfigTransaction::onError(Error const& e) {
|
2021-06-11 05:49:05 +08:00
|
|
|
return impl().onError(e);
|
2021-04-15 13:06:37 +08:00
|
|
|
}
|
|
|
|
|
2021-05-01 00:34:36 +08:00
|
|
|
void SimpleConfigTransaction::cancel() {
|
2021-05-15 15:23:21 +08:00
|
|
|
// TODO: Implement someday
|
2021-05-01 00:34:36 +08:00
|
|
|
throw client_invalid_operation();
|
2021-04-23 11:43:23 +08:00
|
|
|
}
|
|
|
|
|
2021-04-17 08:58:00 +08:00
|
|
|
void SimpleConfigTransaction::reset() {
|
2021-06-11 05:49:05 +08:00
|
|
|
return impl().reset();
|
2021-04-16 03:44:45 +08:00
|
|
|
}
|
|
|
|
|
2021-04-23 11:43:23 +08:00
|
|
|
void SimpleConfigTransaction::fullReset() {
|
2021-06-11 05:49:05 +08:00
|
|
|
return impl().fullReset();
|
2021-04-23 11:43:23 +08:00
|
|
|
}
|
|
|
|
|
2021-05-01 00:34:36 +08:00
|
|
|
void SimpleConfigTransaction::debugTransaction(UID dID) {
|
2021-06-11 05:49:05 +08:00
|
|
|
impl().debugTransaction(dID);
|
2021-05-01 00:34:36 +08:00
|
|
|
}
|
|
|
|
|
2021-05-15 15:23:21 +08:00
|
|
|
void SimpleConfigTransaction::checkDeferredError() const {
|
2021-06-11 05:49:05 +08:00
|
|
|
impl().checkDeferredError(deferredError);
|
2021-05-01 00:34:36 +08:00
|
|
|
}
|
|
|
|
|
2021-06-03 13:14:58 +08:00
|
|
|
SimpleConfigTransaction::SimpleConfigTransaction(Database const& cx)
|
2021-06-11 05:49:05 +08:00
|
|
|
: _impl(std::make_unique<SimpleConfigTransactionImpl>(cx)) {}
|
2021-04-15 13:06:37 +08:00
|
|
|
|
2021-05-15 14:02:40 +08:00
|
|
|
SimpleConfigTransaction::SimpleConfigTransaction(ConfigTransactionInterface const& cti)
|
2021-06-11 05:49:05 +08:00
|
|
|
: _impl(std::make_unique<SimpleConfigTransactionImpl>(cti)) {}
|
2021-05-15 14:02:40 +08:00
|
|
|
|
2021-04-17 08:58:00 +08:00
|
|
|
SimpleConfigTransaction::~SimpleConfigTransaction() = default;
|