2021-05-12 07:57:05 +08:00
|
|
|
/*
|
2021-05-13 08:23:32 +08:00
|
|
|
* SimpleConfigConsumer.actor.cpp
|
2021-05-12 07:57:05 +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.
|
|
|
|
*/
|
|
|
|
|
2021-08-06 09:50:11 +08:00
|
|
|
#include "fdbserver/ConfigBroadcastInterface.h"
|
2021-05-13 08:23:32 +08:00
|
|
|
#include "fdbserver/SimpleConfigConsumer.h"
|
2021-08-10 02:49:57 +08:00
|
|
|
#include "flow/actorcompiler.h" // must be last include
|
2021-05-12 07:57:05 +08:00
|
|
|
|
|
|
|
class SimpleConfigConsumerImpl {
|
|
|
|
ConfigFollowerInterface cfi;
|
2021-05-23 15:41:15 +08:00
|
|
|
Version lastSeenVersion{ 0 };
|
2021-05-24 07:44:06 +08:00
|
|
|
double pollingInterval;
|
2021-05-15 02:03:22 +08:00
|
|
|
Optional<double> compactionInterval;
|
2021-05-12 07:57:05 +08:00
|
|
|
|
2021-05-14 15:41:02 +08:00
|
|
|
UID id;
|
2021-05-12 07:57:05 +08:00
|
|
|
CounterCollection cc;
|
|
|
|
Counter compactRequest;
|
|
|
|
Counter successfulChangeRequest;
|
|
|
|
Counter failedChangeRequest;
|
2021-05-13 02:56:45 +08:00
|
|
|
Counter snapshotRequest;
|
2021-05-12 07:57:05 +08:00
|
|
|
Future<Void> logger;
|
|
|
|
|
2021-05-25 01:47:43 +08:00
|
|
|
ACTOR static Future<Void> compactor(SimpleConfigConsumerImpl* self, ConfigBroadcaster* broadcaster) {
|
2021-05-15 02:03:22 +08:00
|
|
|
if (!self->compactionInterval.present()) {
|
|
|
|
wait(Never());
|
|
|
|
return Void();
|
|
|
|
}
|
2021-05-12 07:57:05 +08:00
|
|
|
loop {
|
2021-05-25 01:47:43 +08:00
|
|
|
state Version compactionVersion = self->lastSeenVersion;
|
2021-05-15 02:03:22 +08:00
|
|
|
wait(delayJittered(self->compactionInterval.get()));
|
2021-05-25 01:47:43 +08:00
|
|
|
wait(self->cfi.compact.getReply(ConfigFollowerCompactRequest{ compactionVersion }));
|
|
|
|
++self->compactRequest;
|
|
|
|
broadcaster->compact(compactionVersion);
|
2021-05-12 07:57:05 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-07-19 08:07:10 +08:00
|
|
|
ACTOR static Future<Version> getCommittedVersion(SimpleConfigConsumerImpl* self) {
|
|
|
|
ConfigFollowerGetCommittedVersionReply committedVersionReply =
|
|
|
|
wait(self->cfi.getCommittedVersion.getReply(ConfigFollowerGetCommittedVersionRequest{}));
|
2021-10-15 06:03:53 +08:00
|
|
|
return committedVersionReply.lastCommitted;
|
2021-07-19 08:07:10 +08:00
|
|
|
}
|
|
|
|
|
2021-05-23 15:41:15 +08:00
|
|
|
ACTOR static Future<Void> fetchChanges(SimpleConfigConsumerImpl* self, ConfigBroadcaster* broadcaster) {
|
2021-05-25 01:47:43 +08:00
|
|
|
wait(getSnapshotAndChanges(self, broadcaster));
|
2021-05-12 07:57:05 +08:00
|
|
|
loop {
|
|
|
|
try {
|
2021-07-19 08:07:10 +08:00
|
|
|
state Version committedVersion = wait(getCommittedVersion(self));
|
|
|
|
ASSERT_GE(committedVersion, self->lastSeenVersion);
|
|
|
|
if (committedVersion > self->lastSeenVersion) {
|
|
|
|
ConfigFollowerGetChangesReply reply = wait(self->cfi.getChanges.getReply(
|
|
|
|
ConfigFollowerGetChangesRequest{ self->lastSeenVersion, committedVersion }));
|
|
|
|
++self->successfulChangeRequest;
|
|
|
|
for (const auto& versionedMutation : reply.changes) {
|
|
|
|
TraceEvent te(SevDebug, "ConsumerFetchedMutation", self->id);
|
|
|
|
te.detail("Version", versionedMutation.version)
|
|
|
|
.detail("ConfigClass", versionedMutation.mutation.getConfigClass())
|
|
|
|
.detail("KnobName", versionedMutation.mutation.getKnobName());
|
|
|
|
if (versionedMutation.mutation.isSet()) {
|
|
|
|
te.detail("Op", "Set")
|
|
|
|
.detail("KnobValue", versionedMutation.mutation.getValue().toString());
|
|
|
|
} else {
|
|
|
|
te.detail("Op", "Clear");
|
|
|
|
}
|
2021-06-07 15:29:36 +08:00
|
|
|
}
|
2021-07-19 08:07:10 +08:00
|
|
|
self->lastSeenVersion = committedVersion;
|
|
|
|
broadcaster->applyChanges(reply.changes, committedVersion, reply.annotations);
|
2021-05-15 02:03:22 +08:00
|
|
|
}
|
2021-05-24 07:44:06 +08:00
|
|
|
wait(delayJittered(self->pollingInterval));
|
2021-05-12 07:57:05 +08:00
|
|
|
} catch (Error& e) {
|
|
|
|
++self->failedChangeRequest;
|
|
|
|
if (e.code() == error_code_version_already_compacted) {
|
2021-06-17 02:34:20 +08:00
|
|
|
TEST(true); // SimpleConfigConsumer get version_already_compacted error
|
2021-05-25 01:47:43 +08:00
|
|
|
wait(getSnapshotAndChanges(self, broadcaster));
|
2021-05-12 07:57:05 +08:00
|
|
|
} else {
|
|
|
|
throw e;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-05-25 01:47:43 +08:00
|
|
|
ACTOR static Future<Void> getSnapshotAndChanges(SimpleConfigConsumerImpl* self, ConfigBroadcaster* broadcaster) {
|
2021-07-19 08:07:10 +08:00
|
|
|
state Version committedVersion = wait(getCommittedVersion(self));
|
|
|
|
ConfigFollowerGetSnapshotAndChangesReply reply = wait(
|
|
|
|
self->cfi.getSnapshotAndChanges.getReply(ConfigFollowerGetSnapshotAndChangesRequest{ committedVersion }));
|
2021-05-25 01:47:43 +08:00
|
|
|
++self->snapshotRequest;
|
|
|
|
TraceEvent(SevDebug, "ConfigConsumerGotSnapshotAndChanges", self->id)
|
2021-05-19 06:28:44 +08:00
|
|
|
.detail("SnapshotVersion", reply.snapshotVersion)
|
|
|
|
.detail("SnapshotSize", reply.snapshot.size())
|
2021-07-19 08:07:10 +08:00
|
|
|
.detail("ChangesVersion", committedVersion)
|
2021-05-25 01:47:43 +08:00
|
|
|
.detail("ChangesSize", reply.changes.size())
|
|
|
|
.detail("AnnotationsSize", reply.annotations.size());
|
2021-07-19 08:07:10 +08:00
|
|
|
ASSERT_GE(committedVersion, self->lastSeenVersion);
|
|
|
|
self->lastSeenVersion = committedVersion;
|
2021-05-23 15:41:15 +08:00
|
|
|
broadcaster->applySnapshotAndChanges(
|
2021-07-19 08:07:10 +08:00
|
|
|
std::move(reply.snapshot), reply.snapshotVersion, reply.changes, committedVersion, reply.annotations);
|
2021-05-12 07:57:05 +08:00
|
|
|
return Void();
|
|
|
|
}
|
|
|
|
|
2021-05-14 15:41:02 +08:00
|
|
|
static ConfigFollowerInterface getConfigFollowerInterface(ConfigFollowerInterface const& cfi) { return cfi; }
|
2021-05-13 08:23:32 +08:00
|
|
|
|
2021-05-14 15:41:02 +08:00
|
|
|
static ConfigFollowerInterface getConfigFollowerInterface(ServerCoordinators const& coordinators) {
|
|
|
|
return ConfigFollowerInterface(coordinators.configServers[0]);
|
2021-05-12 07:57:05 +08:00
|
|
|
}
|
|
|
|
|
2021-05-14 15:41:02 +08:00
|
|
|
public:
|
2021-05-23 15:41:15 +08:00
|
|
|
template <class ConfigSource>
|
|
|
|
SimpleConfigConsumerImpl(ConfigSource const& configSource,
|
|
|
|
double const& pollingInterval,
|
2021-05-18 06:31:03 +08:00
|
|
|
Optional<double> const& compactionInterval)
|
2021-05-23 15:41:15 +08:00
|
|
|
: pollingInterval(pollingInterval), compactionInterval(compactionInterval),
|
|
|
|
id(deterministicRandom()->randomUniqueID()), cc("ConfigConsumer"), compactRequest("CompactRequest", cc),
|
|
|
|
successfulChangeRequest("SuccessfulChangeRequest", cc), failedChangeRequest("FailedChangeRequest", cc),
|
|
|
|
snapshotRequest("SnapshotRequest", cc) {
|
|
|
|
cfi = getConfigFollowerInterface(configSource);
|
2021-05-14 15:41:02 +08:00
|
|
|
logger = traceCounters(
|
|
|
|
"ConfigConsumerMetrics", id, SERVER_KNOBS->WORKER_LOGGING_INTERVAL, &cc, "ConfigConsumerMetrics");
|
2021-05-12 07:57:05 +08:00
|
|
|
}
|
|
|
|
|
2021-05-25 01:47:43 +08:00
|
|
|
Future<Void> consume(ConfigBroadcaster& broadcaster) {
|
|
|
|
return fetchChanges(this, &broadcaster) || compactor(this, &broadcaster);
|
|
|
|
}
|
2021-05-14 15:41:02 +08:00
|
|
|
|
|
|
|
UID getID() const { return id; }
|
2021-05-12 07:57:05 +08:00
|
|
|
};
|
|
|
|
|
2021-05-14 15:41:02 +08:00
|
|
|
SimpleConfigConsumer::SimpleConfigConsumer(ConfigFollowerInterface const& cfi,
|
2021-05-23 15:41:15 +08:00
|
|
|
double pollingInterval,
|
2021-05-18 06:31:03 +08:00
|
|
|
Optional<double> compactionInterval)
|
2021-08-03 03:32:11 +08:00
|
|
|
: impl(PImpl<SimpleConfigConsumerImpl>::create(cfi, pollingInterval, compactionInterval)) {}
|
2021-05-12 07:57:05 +08:00
|
|
|
|
2021-05-14 15:41:02 +08:00
|
|
|
SimpleConfigConsumer::SimpleConfigConsumer(ServerCoordinators const& coordinators,
|
2021-05-23 15:41:15 +08:00
|
|
|
double pollingInterval,
|
2021-05-18 06:31:03 +08:00
|
|
|
Optional<double> compactionInterval)
|
2021-08-03 03:32:11 +08:00
|
|
|
: impl(PImpl<SimpleConfigConsumerImpl>::create(coordinators, pollingInterval, compactionInterval)) {}
|
2021-05-12 07:57:05 +08:00
|
|
|
|
|
|
|
Future<Void> SimpleConfigConsumer::consume(ConfigBroadcaster& broadcaster) {
|
2021-08-03 03:32:11 +08:00
|
|
|
return impl->consume(broadcaster);
|
2021-05-12 07:57:05 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
SimpleConfigConsumer::~SimpleConfigConsumer() = default;
|
2021-05-14 15:41:02 +08:00
|
|
|
|
|
|
|
UID SimpleConfigConsumer::getID() const {
|
2021-08-03 03:32:11 +08:00
|
|
|
return impl->getID();
|
2021-05-14 15:41:02 +08:00
|
|
|
}
|