2019-05-10 11:55:44 +08:00
|
|
|
/*
|
|
|
|
* RestoreRoleCommon.actor.cpp
|
|
|
|
*
|
|
|
|
* 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/NativeAPI.actor.h"
|
|
|
|
#include "fdbclient/MutationList.h"
|
2019-05-30 04:26:17 +08:00
|
|
|
#include "fdbclient/ReadYourWrites.h"
|
|
|
|
#include "fdbclient/RunTransaction.actor.h"
|
2019-05-10 11:55:44 +08:00
|
|
|
|
|
|
|
#include "fdbserver/RestoreUtil.h"
|
|
|
|
#include "fdbserver/RestoreRoleCommon.actor.h"
|
|
|
|
#include "fdbserver/RestoreLoader.actor.h"
|
|
|
|
#include "fdbserver/RestoreApplier.actor.h"
|
2020-07-17 06:30:27 +08:00
|
|
|
#include "fdbserver/RestoreController.actor.h"
|
2019-05-10 11:55:44 +08:00
|
|
|
|
2019-08-02 08:00:13 +08:00
|
|
|
#include "flow/actorcompiler.h" // This must be the last #include.
|
2019-05-10 11:55:44 +08:00
|
|
|
|
|
|
|
class Database;
|
|
|
|
struct RestoreWorkerData;
|
|
|
|
|
2019-08-02 08:00:13 +08:00
|
|
|
// id is the id of the worker to be monitored
|
2019-05-10 11:55:44 +08:00
|
|
|
// This actor is used for both restore loader and restore applier
|
|
|
|
ACTOR Future<Void> handleHeartbeat(RestoreSimpleRequest req, UID id) {
|
2019-08-02 08:00:13 +08:00
|
|
|
wait(delayJittered(5.0)); // Random jitter reduces heat beat monitor's pressure
|
2019-05-30 04:42:35 +08:00
|
|
|
req.reply.send(RestoreCommonReply(id));
|
2019-05-10 11:55:44 +08:00
|
|
|
return Void();
|
|
|
|
}
|
|
|
|
|
2020-01-17 08:19:51 +08:00
|
|
|
void handleFinishRestoreRequest(const RestoreFinishRequest& req, Reference<RestoreRoleData> self) {
|
|
|
|
self->resetPerRestoreRequest();
|
2020-01-24 06:40:08 +08:00
|
|
|
TraceEvent("FastRestoreRolePhaseFinishRestoreRequest", self->id())
|
2020-01-17 08:19:51 +08:00
|
|
|
.detail("FinishRestoreRequest", req.terminate)
|
2020-01-24 06:40:08 +08:00
|
|
|
.detail("Role", getRoleStr(self->role));
|
2019-06-05 02:40:23 +08:00
|
|
|
|
2019-08-02 08:00:13 +08:00
|
|
|
req.reply.send(RestoreCommonReply(self->id()));
|
|
|
|
}
|
2019-05-12 08:34:31 +08:00
|
|
|
|
2020-01-28 10:13:14 +08:00
|
|
|
// Multiple version batches may execute in parallel and init their version batches
|
2019-11-12 08:24:41 +08:00
|
|
|
ACTOR Future<Void> handleInitVersionBatchRequest(RestoreVersionBatchRequest req, Reference<RestoreRoleData> self) {
|
2020-01-24 06:40:08 +08:00
|
|
|
TraceEvent("FastRestoreRolePhaseInitVersionBatch", self->id())
|
|
|
|
.detail("BatchIndex", req.batchIndex)
|
|
|
|
.detail("Role", getRoleStr(self->role))
|
2020-02-28 15:45:48 +08:00
|
|
|
.detail("VersionBatchNotifiedVersion", self->versionBatchId.get());
|
2020-05-05 02:31:39 +08:00
|
|
|
// Loader destroy batchData once the batch finishes and self->finishedBatch.set(req.batchIndex);
|
|
|
|
ASSERT(self->finishedBatch.get() < req.batchIndex);
|
|
|
|
|
2020-01-15 06:18:41 +08:00
|
|
|
// batchId is continuous. (req.batchIndex-1) is the id of the just finished batch.
|
|
|
|
wait(self->versionBatchId.whenAtLeast(req.batchIndex - 1));
|
2019-11-04 09:20:41 +08:00
|
|
|
|
2020-01-15 06:18:41 +08:00
|
|
|
if (self->versionBatchId.get() == req.batchIndex - 1) {
|
2020-01-28 10:13:14 +08:00
|
|
|
self->initVersionBatch(req.batchIndex);
|
2020-02-28 15:50:09 +08:00
|
|
|
self->setVersionBatchState(req.batchIndex, ApplierVersionBatchState::INIT);
|
2020-01-28 10:13:14 +08:00
|
|
|
TraceEvent("FastRestoreInitVersionBatch")
|
|
|
|
.detail("BatchIndex", req.batchIndex)
|
2019-11-04 09:20:41 +08:00
|
|
|
.detail("Role", getRoleStr(self->role))
|
|
|
|
.detail("Node", self->id());
|
2020-01-15 06:18:41 +08:00
|
|
|
self->versionBatchId.set(req.batchIndex);
|
2019-11-04 09:20:41 +08:00
|
|
|
}
|
2019-05-10 11:55:44 +08:00
|
|
|
|
2019-05-30 04:42:35 +08:00
|
|
|
req.reply.send(RestoreCommonReply(self->id()));
|
2019-11-12 08:24:41 +08:00
|
|
|
return Void();
|
2019-05-10 11:55:44 +08:00
|
|
|
}
|
|
|
|
|
2020-02-11 07:06:03 +08:00
|
|
|
void updateProcessStats(Reference<RestoreRoleData> self) {
|
|
|
|
if (g_network->isSimulated()) {
|
|
|
|
// memUsage and cpuUsage are not relevant in the simulator,
|
|
|
|
// and relying on the actual values could break seed determinism
|
2020-08-27 01:07:44 +08:00
|
|
|
if (deterministicRandom()->random01() < 0.2) { // not fully utilized cpu
|
|
|
|
self->cpuUsage = deterministicRandom()->random01() * SERVER_KNOBS->FASTRESTORE_SCHED_TARGET_CPU_PERCENT;
|
|
|
|
} else if (deterministicRandom()->random01() < 0.6) { // achieved target cpu but cpu is not busy
|
|
|
|
self->cpuUsage = SERVER_KNOBS->FASTRESTORE_SCHED_TARGET_CPU_PERCENT +
|
|
|
|
deterministicRandom()->random01() * (SERVER_KNOBS->FASTRESTORE_SCHED_MAX_CPU_PERCENT -
|
|
|
|
SERVER_KNOBS->FASTRESTORE_SCHED_TARGET_CPU_PERCENT);
|
|
|
|
} else { // reach desired max cpu usage; use max cpu as 200 to simulate incorrect cpu profiling
|
|
|
|
self->cpuUsage =
|
|
|
|
SERVER_KNOBS->FASTRESTORE_SCHED_MAX_CPU_PERCENT +
|
|
|
|
deterministicRandom()->random01() * (200 - SERVER_KNOBS->FASTRESTORE_SCHED_MAX_CPU_PERCENT);
|
|
|
|
}
|
2020-02-11 07:06:03 +08:00
|
|
|
self->memory = 100.0;
|
|
|
|
self->residentMemory = 100.0;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
SystemStatistics sysStats = getSystemStatistics();
|
|
|
|
if (sysStats.initialized) {
|
|
|
|
self->cpuUsage = 100 * sysStats.processCPUSeconds / sysStats.elapsed;
|
|
|
|
self->memory = sysStats.processMemory;
|
|
|
|
self->residentMemory = sysStats.processResidentMemory;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-02-27 05:43:30 +08:00
|
|
|
// An actor is schedulable to run if the current worker has enough resourc, i.e.,
|
|
|
|
// the worker's memory usage is below the threshold;
|
|
|
|
// Exception: If the actor is working on the current version batch, we have to schedule
|
|
|
|
// the actor to run to avoid dead-lock.
|
|
|
|
// Future: When we release the actors that are blocked by memory usage, we should release them
|
|
|
|
// in increasing order of their version batch.
|
|
|
|
ACTOR Future<Void> isSchedulable(Reference<RestoreRoleData> self, int actorBatchIndex, std::string name) {
|
|
|
|
self->delayedActors++;
|
2020-05-01 09:47:13 +08:00
|
|
|
state double memoryThresholdBytes = SERVER_KNOBS->FASTRESTORE_MEMORY_THRESHOLD_MB_SOFT * 1024 * 1024;
|
2020-02-27 05:43:30 +08:00
|
|
|
loop {
|
|
|
|
double memory = getSystemStatistics().processMemory;
|
2020-02-27 06:40:01 +08:00
|
|
|
if (g_network->isSimulated() && BUGGIFY) {
|
|
|
|
// Intentionally randomly block actors for low memory reason.
|
|
|
|
// memory will be larger than threshold when deterministicRandom()->random01() > 1/2
|
2020-08-27 01:07:44 +08:00
|
|
|
if (deterministicRandom()->random01() < 0.4) { // enough memory
|
|
|
|
memory = SERVER_KNOBS->FASTRESTORE_MEMORY_THRESHOLD_MB_SOFT * deterministicRandom()->random01();
|
|
|
|
} else { // used too much memory, needs throttling
|
|
|
|
memory = SERVER_KNOBS->FASTRESTORE_MEMORY_THRESHOLD_MB_SOFT +
|
|
|
|
deterministicRandom()->random01() * SERVER_KNOBS->FASTRESTORE_MEMORY_THRESHOLD_MB_SOFT;
|
|
|
|
}
|
2020-02-27 06:40:01 +08:00
|
|
|
}
|
2020-05-01 09:47:13 +08:00
|
|
|
if (memory < memoryThresholdBytes || self->finishedBatch.get() + 1 == actorBatchIndex) {
|
|
|
|
if (memory >= memoryThresholdBytes) {
|
2020-05-05 02:20:53 +08:00
|
|
|
TraceEvent(SevWarn, "FastRestoreMemoryUsageAboveThreshold", self->id())
|
2020-06-25 13:10:54 +08:00
|
|
|
.suppressFor(5.0)
|
2020-05-05 02:20:53 +08:00
|
|
|
.detail("Role", getRoleStr(self->role))
|
2020-02-27 05:43:30 +08:00
|
|
|
.detail("BatchIndex", actorBatchIndex)
|
2020-04-05 08:33:59 +08:00
|
|
|
.detail("FinishedBatch", self->finishedBatch.get())
|
2020-05-01 09:47:13 +08:00
|
|
|
.detail("Actor", name)
|
|
|
|
.detail("Memory", memory);
|
2020-02-27 05:43:30 +08:00
|
|
|
}
|
|
|
|
self->delayedActors--;
|
|
|
|
break;
|
|
|
|
} else {
|
2020-05-05 02:20:53 +08:00
|
|
|
TraceEvent(SevInfo, "FastRestoreMemoryUsageAboveThresholdWait", self->id())
|
2020-06-25 13:10:54 +08:00
|
|
|
.suppressFor(5.0)
|
2020-05-05 02:20:53 +08:00
|
|
|
.detail("Role", getRoleStr(self->role))
|
2020-02-28 10:32:02 +08:00
|
|
|
.detail("BatchIndex", actorBatchIndex)
|
|
|
|
.detail("Actor", name)
|
|
|
|
.detail("CurrentMemory", memory);
|
2020-05-05 02:20:53 +08:00
|
|
|
// TODO: Set FASTRESTORE_WAIT_FOR_MEMORY_LATENCY to a large value. It should be able to avoided
|
2020-02-27 05:43:30 +08:00
|
|
|
wait(delay(SERVER_KNOBS->FASTRESTORE_WAIT_FOR_MEMORY_LATENCY) || self->checkMemory.onTrigger());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return Void();
|
|
|
|
}
|
|
|
|
|
2020-06-29 03:33:07 +08:00
|
|
|
// Updated process metrics will be used by scheduler for throttling as well
|
|
|
|
ACTOR Future<Void> updateProcessMetrics(Reference<RestoreRoleData> self) {
|
|
|
|
loop {
|
|
|
|
updateProcessStats(self);
|
|
|
|
wait(delay(SERVER_KNOBS->FASTRESTORE_UPDATE_PROCESS_STATS_INTERVAL));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-02-11 07:06:03 +08:00
|
|
|
ACTOR Future<Void> traceProcessMetrics(Reference<RestoreRoleData> self, std::string role) {
|
|
|
|
loop {
|
2020-06-30 01:22:07 +08:00
|
|
|
TraceEvent("FastRestoreTraceProcessMetrics", self->nodeID)
|
2020-02-11 07:06:03 +08:00
|
|
|
.detail("Role", role)
|
2020-06-30 01:22:07 +08:00
|
|
|
.detail("PipelinedMaxVersionBatchIndex", self->versionBatchId.get())
|
|
|
|
.detail("FinishedVersionBatchIndex", self->finishedBatch.get())
|
2020-07-31 00:22:06 +08:00
|
|
|
.detail("CurrentVersionBatchPhase", self->getVersionBatchState(self->finishedBatch.get() + 1))
|
2020-02-11 07:06:03 +08:00
|
|
|
.detail("CpuUsage", self->cpuUsage)
|
|
|
|
.detail("UsedMemory", self->memory)
|
|
|
|
.detail("ResidentMemory", self->residentMemory);
|
|
|
|
wait(delay(SERVER_KNOBS->FASTRESTORE_ROLE_LOGGING_DELAY));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-02-28 11:23:29 +08:00
|
|
|
ACTOR Future<Void> traceRoleVersionBatchProgress(Reference<RestoreRoleData> self, std::string role) {
|
|
|
|
loop {
|
|
|
|
int batchIndex = self->finishedBatch.get();
|
|
|
|
int maxBatchIndex = self->versionBatchId.get();
|
|
|
|
|
2020-02-28 16:08:01 +08:00
|
|
|
TraceEvent ev("FastRestoreVersionBatchProgressState", self->nodeID);
|
2020-02-28 16:23:58 +08:00
|
|
|
ev.detail("Role", role).detail("Node", self->nodeID).detail("FinishedBatch", batchIndex).detail("InitializedBatch", maxBatchIndex);
|
2020-02-28 11:23:29 +08:00
|
|
|
while (batchIndex <= maxBatchIndex) {
|
2020-02-28 16:23:58 +08:00
|
|
|
std::stringstream typeName;
|
|
|
|
typeName << "VersionBatch" << batchIndex;
|
|
|
|
ev.detail(typeName.str(), self->getVersionBatchState(batchIndex));
|
2020-02-28 11:23:29 +08:00
|
|
|
batchIndex++;
|
|
|
|
}
|
|
|
|
|
|
|
|
wait(delay(SERVER_KNOBS->FASTRESTORE_ROLE_LOGGING_DELAY));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-05-10 11:55:44 +08:00
|
|
|
//-------Helper functions
|
|
|
|
std::string getHexString(StringRef input) {
|
|
|
|
std::stringstream ss;
|
2019-08-02 08:00:13 +08:00
|
|
|
for (int i = 0; i < input.size(); i++) {
|
|
|
|
if (i % 4 == 0) ss << " ";
|
|
|
|
if (i == 12) { // The end of 12bytes, which is the version size for value
|
2019-05-10 11:55:44 +08:00
|
|
|
ss << "|";
|
|
|
|
}
|
2019-08-02 08:00:13 +08:00
|
|
|
if (i == (12 + 12)) { // The end of version + header
|
2019-05-10 11:55:44 +08:00
|
|
|
ss << "@";
|
|
|
|
}
|
2019-08-02 08:00:13 +08:00
|
|
|
ss << std::setfill('0') << std::setw(2) << std::hex
|
|
|
|
<< (int)input[i]; // [] operator moves the pointer in step of unit8
|
2019-05-10 11:55:44 +08:00
|
|
|
}
|
|
|
|
return ss.str();
|
|
|
|
}
|