foundationdb/fdbclient/IKnobCollection.cpp

91 lines
3.4 KiB
C++
Raw Normal View History

2021-06-03 14:40:52 +08:00
/*
* IKnobCollection.h
*
* 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 "fdbclient/IKnobCollection.h"
#include "fdbclient/ClientKnobCollection.h"
#include "fdbclient/ServerKnobCollection.h"
2021-06-10 11:50:00 +08:00
#include "fdbclient/TestKnobCollection.h"
2021-06-03 14:40:52 +08:00
2021-06-10 11:50:00 +08:00
std::unique_ptr<IKnobCollection> IKnobCollection::create(Type type, Randomize randomize, IsSimulated isSimulated) {
if (type == Type::CLIENT) {
return std::make_unique<ClientKnobCollection>(randomize, isSimulated);
} else if (type == Type::SERVER) {
return std::make_unique<ServerKnobCollection>(randomize, isSimulated);
} else if (type == Type::TEST) {
return std::make_unique<TestKnobCollection>(randomize, isSimulated);
}
UNSTOPPABLE_ASSERT(false);
2021-06-03 14:40:52 +08:00
}
2021-06-10 11:50:00 +08:00
KnobValue IKnobCollection::parseKnobValue(std::string const& knobName, std::string const& knobValue) const {
auto result = tryParseKnobValue(knobName, knobValue);
if (!result.present()) {
throw invalid_option();
}
return result.get();
2021-06-03 14:40:52 +08:00
}
2021-06-10 11:50:00 +08:00
void IKnobCollection::setKnob(std::string const& knobName, KnobValueRef const& knobValue) {
if (!trySetKnob(knobName, knobValue)) {
2021-06-18 13:05:28 +08:00
TraceEvent(SevWarnAlways, "FailedToSetKnob")
.detail("KnobName", knobName)
.detail("KnobValue", knobValue.toString());
throw invalid_option_value();
2021-06-10 11:50:00 +08:00
}
2021-06-03 14:40:52 +08:00
}
2021-06-10 11:50:00 +08:00
KnobValue IKnobCollection::parseKnobValue(std::string const& knobName, std::string const& knobValue, Type type) {
2021-06-03 14:40:52 +08:00
// TODO: Ideally it should not be necessary to create a template object to parse knobs
2021-06-10 11:50:00 +08:00
static std::unique_ptr<IKnobCollection> clientKnobCollection, serverKnobCollection, testKnobCollection;
if (type == Type::CLIENT) {
if (!clientKnobCollection) {
clientKnobCollection = create(type, Randomize::False, IsSimulated::False);
2021-06-10 11:50:00 +08:00
}
return clientKnobCollection->parseKnobValue(knobName, knobValue);
} else if (type == Type::SERVER) {
2021-06-03 14:40:52 +08:00
if (!serverKnobCollection) {
serverKnobCollection = create(type, Randomize::False, IsSimulated::False);
2021-06-03 14:40:52 +08:00
}
return serverKnobCollection->parseKnobValue(knobName, knobValue);
2021-06-10 11:50:00 +08:00
} else if (type == Type::TEST) {
if (!testKnobCollection) {
testKnobCollection = create(type, Randomize::False, IsSimulated::False);
2021-06-03 14:40:52 +08:00
}
2021-06-10 11:50:00 +08:00
return testKnobCollection->parseKnobValue(knobName, knobValue);
2021-06-03 14:40:52 +08:00
}
2021-06-10 11:50:00 +08:00
UNSTOPPABLE_ASSERT(false);
2021-06-03 14:40:52 +08:00
}
std::unique_ptr<IKnobCollection> IKnobCollection::globalKnobCollection =
IKnobCollection::create(IKnobCollection::Type::CLIENT, Randomize::False, IsSimulated::False);
void IKnobCollection::setGlobalKnobCollection(Type type, Randomize randomize, IsSimulated isSimulated) {
globalKnobCollection = create(type, randomize, isSimulated);
FLOW_KNOBS = &globalKnobCollection->getFlowKnobs();
}
IKnobCollection const& IKnobCollection::getGlobalKnobCollection() {
return *globalKnobCollection;
}
IKnobCollection& IKnobCollection::getMutableGlobalKnobCollection() {
return *globalKnobCollection;
}