2020-01-30 02:16:45 +08:00
|
|
|
/*
|
|
|
|
* BackupProgress.actor.cpp
|
|
|
|
*
|
|
|
|
* This source file is part of the FoundationDB open source project
|
|
|
|
*
|
|
|
|
* Copyright 2013-2020 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.
|
|
|
|
*/
|
|
|
|
|
2019-10-01 04:16:28 +08:00
|
|
|
#include "fdbserver/BackupProgress.actor.h"
|
|
|
|
|
|
|
|
#include "fdbclient/NativeAPI.actor.h"
|
|
|
|
#include "fdbclient/SystemData.h"
|
2019-10-02 00:44:57 +08:00
|
|
|
#include "flow/UnitTest.h"
|
2019-10-01 04:16:28 +08:00
|
|
|
#include "flow/actorcompiler.h" // This must be the last #include.
|
|
|
|
|
|
|
|
void BackupProgress::addBackupStatus(const WorkerBackupStatus& status) {
|
|
|
|
auto& it = progress[status.epoch];
|
|
|
|
auto lb = it.lower_bound(status.tag);
|
|
|
|
if (lb != it.end() && status.tag == lb->first) {
|
|
|
|
if (lb->second < status.version) {
|
|
|
|
lb->second = status.version;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
it.insert(lb, { status.tag, status.version });
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-04 08:25:21 +08:00
|
|
|
void BackupProgress::updateTagVersions(std::map<Tag, Version>* tagVersions, std::set<Tag>* tags,
|
|
|
|
const std::map<Tag, Version>& progress, Version endVersion, LogEpoch epoch) {
|
|
|
|
for (const auto& [tag, savedVersion] : progress) {
|
|
|
|
tags->erase(tag);
|
|
|
|
if (savedVersion < endVersion - 1) {
|
|
|
|
tagVersions->insert({ tag, savedVersion + 1 });
|
|
|
|
TraceEvent("BW", dbgid)
|
|
|
|
.detail("OldEpoch", epoch)
|
|
|
|
.detail("Tag", tag.toString())
|
|
|
|
.detail("BeginVersion", savedVersion + 1)
|
|
|
|
.detail("EndVersion", endVersion);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-03-10 01:17:38 +08:00
|
|
|
|
2020-02-21 08:28:27 +08:00
|
|
|
std::map<std::tuple<LogEpoch, Version, int>, std::map<Tag, Version>> BackupProgress::getUnfinishedBackup() {
|
|
|
|
std::map<std::tuple<LogEpoch, Version, int>, std::map<Tag, Version>> toRecruit;
|
2019-10-01 04:16:28 +08:00
|
|
|
|
2020-01-05 06:33:50 +08:00
|
|
|
if (!backupStartedValue.present()) return toRecruit; // No active backups
|
|
|
|
|
2019-10-02 05:55:08 +08:00
|
|
|
for (const auto& [epoch, info] : epochInfos) {
|
|
|
|
std::set<Tag> tags = enumerateLogRouterTags(info.logRouterTags);
|
2019-10-01 04:16:28 +08:00
|
|
|
std::map<Tag, Version> tagVersions;
|
2020-03-04 08:25:21 +08:00
|
|
|
auto progressIt = progress.lower_bound(epoch);
|
|
|
|
if (progressIt != progress.end() && progressIt->first == epoch) {
|
|
|
|
updateTagVersions(&tagVersions, &tags, progressIt->second, info.epochEnd, epoch);
|
|
|
|
} else {
|
2020-03-10 01:17:38 +08:00
|
|
|
auto rit =
|
|
|
|
std::find_if(progress.rbegin(), progress.rend(),
|
|
|
|
[=](const std::pair<LogEpoch, std::map<Tag, Version>>& p) { return p.first < epoch; });
|
2020-03-04 13:15:36 +08:00
|
|
|
if (!(rit == progress.rend())) {
|
2020-03-04 08:25:21 +08:00
|
|
|
// A partial recovery can result in empty epoch that copies previous
|
|
|
|
// epoch's version range. In this case, we should check previous
|
|
|
|
// epoch's savedVersion.
|
|
|
|
int savedMore = 0;
|
|
|
|
for (auto [tag, version] : rit->second) {
|
2020-03-04 13:15:36 +08:00
|
|
|
if (version >= info.epochBegin) {
|
2020-03-04 08:25:21 +08:00
|
|
|
savedMore++;
|
|
|
|
}
|
|
|
|
}
|
2020-03-04 13:15:36 +08:00
|
|
|
if (savedMore > 0) {
|
2020-03-05 08:27:24 +08:00
|
|
|
// TODO: check the logRouterTags are the same
|
|
|
|
// ASSERT(info.logRouterTags == rit->second.size());
|
2020-03-04 08:25:21 +08:00
|
|
|
|
|
|
|
updateTagVersions(&tagVersions, &tags, rit->second, info.epochEnd, epoch);
|
2019-10-01 04:16:28 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-03-04 08:25:21 +08:00
|
|
|
|
2019-10-01 04:16:28 +08:00
|
|
|
for (const Tag tag : tags) { // tags without progress data
|
2019-12-17 05:50:52 +08:00
|
|
|
tagVersions.insert({ tag, info.epochBegin });
|
2019-10-01 04:16:28 +08:00
|
|
|
TraceEvent("BW", dbgid)
|
|
|
|
.detail("OldEpoch", epoch)
|
|
|
|
.detail("Tag", tag.toString())
|
2019-12-17 05:50:52 +08:00
|
|
|
.detail("BeginVersion", info.epochBegin)
|
2019-10-02 05:55:08 +08:00
|
|
|
.detail("EndVersion", info.epochEnd);
|
2019-10-01 04:16:28 +08:00
|
|
|
}
|
|
|
|
if (!tagVersions.empty()) {
|
2020-02-21 08:28:27 +08:00
|
|
|
toRecruit[{ epoch, info.epochEnd, info.logRouterTags }] = tagVersions;
|
2019-10-01 04:16:28 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return toRecruit;
|
|
|
|
}
|
|
|
|
|
2019-12-17 05:50:52 +08:00
|
|
|
// Save each tag's savedVersion for all epochs into "bStatus".
|
2019-10-01 04:16:28 +08:00
|
|
|
ACTOR Future<Void> getBackupProgress(Database cx, UID dbgid, Reference<BackupProgress> bStatus) {
|
|
|
|
state Transaction tr(cx);
|
|
|
|
|
|
|
|
loop {
|
|
|
|
try {
|
|
|
|
tr.setOption(FDBTransactionOptions::ACCESS_SYSTEM_KEYS);
|
|
|
|
tr.setOption(FDBTransactionOptions::PRIORITY_SYSTEM_IMMEDIATE);
|
2020-01-17 11:16:23 +08:00
|
|
|
tr.setOption(FDBTransactionOptions::LOCK_AWARE);
|
|
|
|
state Future<Optional<Value>> fValue = tr.get(backupStartedKey);
|
|
|
|
state Standalone<RangeResultRef> results = wait(tr.getRange(backupProgressKeys, CLIENT_KNOBS->TOO_MANY));
|
2019-10-01 04:16:28 +08:00
|
|
|
ASSERT(!results.more && results.size() < CLIENT_KNOBS->TOO_MANY);
|
|
|
|
|
2020-01-17 11:16:23 +08:00
|
|
|
Optional<Value> value = wait(fValue);
|
2020-01-05 06:33:50 +08:00
|
|
|
bStatus->setBackupStartedValue(value);
|
2019-10-01 04:16:28 +08:00
|
|
|
for (auto& it : results) {
|
|
|
|
const UID workerID = decodeBackupProgressKey(it.key);
|
|
|
|
const WorkerBackupStatus status = decodeBackupProgressValue(it.value);
|
|
|
|
bStatus->addBackupStatus(status);
|
|
|
|
TraceEvent("GotBackupProgress", dbgid)
|
|
|
|
.detail("W", workerID)
|
|
|
|
.detail("Epoch", status.epoch)
|
|
|
|
.detail("Version", status.version)
|
|
|
|
.detail("Tag", status.tag.toString());
|
|
|
|
}
|
|
|
|
return Void();
|
|
|
|
} catch (Error& e) {
|
|
|
|
wait(tr.onError(e));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-10-02 00:44:57 +08:00
|
|
|
TEST_CASE("/BackupProgress/Unfinished") {
|
2019-10-02 05:55:08 +08:00
|
|
|
std::map<LogEpoch, ILogSystem::EpochTagsVersionsInfo> epochInfos;
|
2019-10-01 04:16:28 +08:00
|
|
|
|
2019-10-02 05:55:08 +08:00
|
|
|
const int epoch1 = 2, begin1 = 1, end1 = 100;
|
2019-10-02 00:44:57 +08:00
|
|
|
const Tag tag1(tagLocalityLogRouter, 0);
|
2019-10-02 05:55:08 +08:00
|
|
|
epochInfos.insert({ epoch1, ILogSystem::EpochTagsVersionsInfo(1, begin1, end1) });
|
|
|
|
BackupProgress progress(UID(0, 0), epochInfos);
|
2020-01-05 06:33:50 +08:00
|
|
|
progress.setBackupStartedValue(Optional<Value>(LiteralStringRef("1")));
|
2019-10-01 04:16:28 +08:00
|
|
|
|
2020-02-21 08:28:27 +08:00
|
|
|
std::map<std::tuple<LogEpoch, Version, int>, std::map<Tag, Version>> unfinished = progress.getUnfinishedBackup();
|
2019-10-02 00:44:57 +08:00
|
|
|
|
|
|
|
ASSERT(unfinished.size() == 1);
|
2020-02-21 08:28:27 +08:00
|
|
|
for (const auto [epochVersionCount, tagVersion] : unfinished) {
|
|
|
|
ASSERT(std::get<0>(epochVersionCount) == epoch1 && std::get<1>(epochVersionCount) == end1 &&
|
|
|
|
std::get<2>(epochVersionCount) == 1);
|
2019-12-17 05:50:52 +08:00
|
|
|
ASSERT(tagVersion.size() == 1 && tagVersion.begin()->first == tag1 && tagVersion.begin()->second == begin1);
|
2019-10-02 00:44:57 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
const int saved1 = 50;
|
2019-12-17 05:50:52 +08:00
|
|
|
WorkerBackupStatus status1(epoch1, saved1, tag1);
|
2019-10-02 00:44:57 +08:00
|
|
|
progress.addBackupStatus(status1);
|
|
|
|
unfinished = progress.getUnfinishedBackup();
|
|
|
|
ASSERT(unfinished.size() == 1);
|
2020-02-21 08:28:27 +08:00
|
|
|
for (const auto [epochVersionCount, tagVersion] : unfinished) {
|
|
|
|
ASSERT(std::get<0>(epochVersionCount) == epoch1 && std::get<1>(epochVersionCount) == end1 &&
|
|
|
|
std::get<2>(epochVersionCount) == 1);
|
2019-12-17 05:50:52 +08:00
|
|
|
ASSERT(tagVersion.size() == 1 && tagVersion.begin()->first == tag1 && tagVersion.begin()->second == saved1 + 1);
|
2019-10-02 00:44:57 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return Void();
|
|
|
|
}
|