301 lines
10 KiB
C++
301 lines
10 KiB
C++
/*
|
|
* SnapTest.cpp
|
|
*
|
|
* This source file is part of the FoundationDB open source project
|
|
*
|
|
* Copyright 2013-2026 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 <boost/lexical_cast.hpp>
|
|
#include "fdbclient/ManagementAPI.h"
|
|
#include "fdbclient/NativeAPI.actor.h"
|
|
#include "fdbclient/ReadYourWrites.h"
|
|
#include "fdbclient/SystemData.h"
|
|
#include "fdbclient/SimpleIni.h"
|
|
#include "fdbserver/core/Knobs.h"
|
|
#include "fdbserver/core/TesterInterface.h"
|
|
#include "fdbserver/core/WorkerInterface.h"
|
|
#include "fdbserver/core/FDBSimulationPolicy.h"
|
|
#include "BulkSetup.h"
|
|
#include "fdbserver/tester/workloads.h"
|
|
|
|
struct SnapTestWorkload : TestWorkload {
|
|
static constexpr auto NAME = "SnapTest";
|
|
|
|
public: // variables
|
|
int numSnaps; // num of snapshots to be taken
|
|
// FIXME: currently validation works on numSnap = 1
|
|
double maxSnapDelay; // max delay before which a snapshot will be taken
|
|
int testID; // test id
|
|
UID snapUID; // UID used for snap name
|
|
std::string restartInfoLocation; // file location to store the snap restore info
|
|
int maxRetryCntToRetrieveMessage; // number of retires to do trackLatest
|
|
bool skipCheck = false; // disable check if the exec fails
|
|
int retryLimit; // -1 if no limit
|
|
bool snapSucceeded = false; // When taking snapshot, tracks snapshot success
|
|
bool attemptDuplicateSnapshot = false;
|
|
|
|
public: // ctor & dtor
|
|
explicit SnapTestWorkload(WorkloadContext const& wcx)
|
|
: TestWorkload(wcx), numSnaps(0), maxSnapDelay(0.0), testID(0), snapUID() {
|
|
TraceEvent("SnapTestWorkloadConstructor").log();
|
|
std::string workloadName = "SnapTest";
|
|
maxRetryCntToRetrieveMessage = 10;
|
|
|
|
numSnaps = getOption(options, "numSnaps"_sr, 0);
|
|
maxSnapDelay = getOption(options, "maxSnapDelay"_sr, 25.0);
|
|
testID = getOption(options, "testID"_sr, 0);
|
|
restartInfoLocation = getOption(options, "restartInfoLocation"_sr, "simfdb/restartInfo.ini"_sr).toString();
|
|
// default behavior is to retry until success
|
|
retryLimit = getOption(options, "retryLimit"_sr, -1);
|
|
fdbSimulationPolicyState().allowLogSetKills = false;
|
|
{
|
|
double duplicateSnapshotProbability = getOption(options, "duplicateSnapshotProbability"_sr, 0.1);
|
|
if (deterministicRandom()->random01() < duplicateSnapshotProbability) {
|
|
attemptDuplicateSnapshot = true;
|
|
}
|
|
}
|
|
}
|
|
|
|
public: // workload functions
|
|
Future<Void> setup(Database const& cx) override {
|
|
TraceEvent("SnapTestWorkloadSetup").log();
|
|
return Void();
|
|
}
|
|
Future<Void> start(Database const& cx) override {
|
|
TraceEvent("SnapTestWorkloadStart").log();
|
|
if (clientId == 0) {
|
|
return _start(cx);
|
|
}
|
|
return Void();
|
|
}
|
|
|
|
Future<bool> check(Database const& cx) override {
|
|
TraceEvent("SnapTestWorkloadCheck").detail("ClientID", clientId).detail("TestID", testID);
|
|
if (clientId != 0) {
|
|
return true;
|
|
}
|
|
if (testID == 1) {
|
|
return snapSucceeded;
|
|
}
|
|
return true;
|
|
}
|
|
|
|
void getMetrics(std::vector<PerfMetric>& m) override { TraceEvent("SnapTestWorkloadGetMetrics"); }
|
|
|
|
void disableFailureInjectionWorkloads(std::set<std::string>& out) const override {
|
|
// Data movement is not allowed while taking snapshot
|
|
// FIXME: the data movement should be delayed while taking snapshots, the workload at present doesn't know the
|
|
// DD is disabled by the snapshot
|
|
out.insert("RandomMoveKeys");
|
|
|
|
// A combination of this workload only doing snapshot once and attrition fault injection means that it's
|
|
// possible that not all ss/tlog/coordinator data gets snapshotted. This workload does retry on snapshot errors
|
|
// but note that the absence of machines is not considered an error from snapshot request point of view.
|
|
// Since snapshot restart tests rely on snapshot data, attrition fault injection is disabled for this workload.
|
|
out.insert("Attrition");
|
|
}
|
|
|
|
Future<Void> _create_keys(Database cx, std::string prefix, bool even = true) {
|
|
Transaction tr(cx);
|
|
std::vector<int64_t> keys;
|
|
|
|
keys.reserve(1000);
|
|
for (int i = 0; i < 1000; i++) {
|
|
keys.push_back(deterministicRandom()->randomInt64(0, INT64_MAX - 2));
|
|
}
|
|
|
|
tr.reset();
|
|
while (true) {
|
|
Error err;
|
|
try {
|
|
for (auto id : keys) {
|
|
if (even) {
|
|
if (id % 2 != 0) {
|
|
id++;
|
|
}
|
|
} else {
|
|
if (id % 2 == 0) {
|
|
id++;
|
|
}
|
|
}
|
|
std::string Key1 = prefix + std::to_string(id);
|
|
Key key1Ref(Key1);
|
|
std::string Val1 = std::to_string(id);
|
|
Value val1Ref(Val1);
|
|
tr.set(key1Ref, val1Ref, AddConflictRange::False);
|
|
}
|
|
co_await tr.commit();
|
|
break;
|
|
} catch (Error& e) {
|
|
err = e;
|
|
}
|
|
co_await tr.onError(err);
|
|
}
|
|
}
|
|
|
|
Future<Void> _start(Database cx) {
|
|
Transaction tr(cx);
|
|
bool snapFailed = false;
|
|
Future<Void> duplicateSnapStatus;
|
|
|
|
if (testID == 0) {
|
|
// create even keys before the snapshot
|
|
co_await _create_keys(cx, "snapKey");
|
|
} else if (testID == 1) {
|
|
// create a snapshot
|
|
double toDelay = fmod(deterministicRandom()->randomUInt32(), maxSnapDelay);
|
|
TraceEvent("ToDelay").detail("Value", toDelay);
|
|
ASSERT(toDelay < maxSnapDelay);
|
|
co_await delay(toDelay);
|
|
|
|
int retry = 0;
|
|
while (true) {
|
|
snapUID = deterministicRandom()->randomUniqueID();
|
|
Error err;
|
|
try {
|
|
StringRef snapCmdRef = "/bin/snap_create.sh"_sr;
|
|
|
|
Future<Void> status = snapCreate(cx, snapCmdRef, snapUID);
|
|
if (attemptDuplicateSnapshot) {
|
|
co_await delay(deterministicRandom()->random01());
|
|
duplicateSnapStatus = snapCreate(cx, snapCmdRef, snapUID);
|
|
}
|
|
ErrorOr<Void> statusErr = co_await errorOr(status);
|
|
if (statusErr.isError() && statusErr.getError().code() != error_code_duplicate_snapshot_request) {
|
|
// First request is expected to fail with duplicate_snapshot_request error
|
|
// Any other errors should be thrown
|
|
throw statusErr.getError();
|
|
}
|
|
if (attemptDuplicateSnapshot) {
|
|
// If duplicate, the first request is discarded, wait for the latest one
|
|
co_await duplicateSnapStatus;
|
|
}
|
|
break;
|
|
} catch (Error& e) {
|
|
err = e;
|
|
}
|
|
TraceEvent("SnapTestCreateError")
|
|
.error(err)
|
|
.detail("SnapUID", snapUID)
|
|
.detail("Duplicate", attemptDuplicateSnapshot);
|
|
++retry;
|
|
// snap v2 can fail for many reasons, so retry until specified times and then fail it
|
|
if (retryLimit != -1 && retry > retryLimit) {
|
|
snapFailed = true;
|
|
break;
|
|
}
|
|
// increase the retry wait time to avoid endless retry where DD is always disabled by snapshot
|
|
co_await delay(retry * SERVER_KNOBS->SNAP_MINIMUM_TIME_GAP);
|
|
}
|
|
CSimpleIni ini;
|
|
ini.SetUnicode();
|
|
ini.LoadFile(restartInfoLocation.c_str());
|
|
std::string uidStr = snapUID.toString();
|
|
ini.SetValue("RESTORE", "RestoreSnapUID", uidStr.c_str());
|
|
ini.SetValue("RESTORE", "BackupFailed", format("%d", snapFailed).c_str());
|
|
ini.SaveFile(restartInfoLocation.c_str());
|
|
// write the snapUID to a file
|
|
auto const severity = snapFailed ? SevError : SevInfo;
|
|
TraceEvent(severity, "SnapshotCreateStatus").detail("Status", !snapFailed ? "Success" : "Failure");
|
|
snapSucceeded = !snapFailed;
|
|
} else if (testID == 2) {
|
|
// create odd keys after the snapshot
|
|
co_await _create_keys(cx, "snapKey", false /*even*/);
|
|
} else if (testID == 3) {
|
|
CSimpleIni ini;
|
|
ini.SetUnicode();
|
|
ini.LoadFile(restartInfoLocation.c_str());
|
|
bool backupFailed = atoi(ini.GetValue("RESTORE", "BackupFailed"));
|
|
if (backupFailed) {
|
|
// since backup failed, skip the restore checking
|
|
TraceEvent(SevWarnAlways, "BackupFailedSkippingRestoreCheck").log();
|
|
co_return;
|
|
}
|
|
KeySelector begin = firstGreaterOrEqual(normalKeys.begin);
|
|
KeySelector end = firstGreaterOrEqual(normalKeys.end);
|
|
int cnt = 0;
|
|
// read the entire normalKeys range and look at keys prefixed
|
|
// with snapKeys 1) validate that all key ids are even ie -
|
|
// created before snap 2) values are same as the key id 3) # of
|
|
// keys adds up to the total keys created before snap
|
|
tr.reset();
|
|
while (true) {
|
|
Error err;
|
|
try {
|
|
RangeResult kvRange = co_await tr.getRange(begin, end, 1000);
|
|
if (!kvRange.more && kvRange.empty()) {
|
|
TraceEvent("SnapTestNoMoreEntries").log();
|
|
break;
|
|
}
|
|
|
|
for (int i = 0; i < kvRange.size(); i++) {
|
|
if (kvRange[i].key.startsWith("snapKey"_sr)) {
|
|
std::string tmp1 = kvRange[i].key.substr(7).toString();
|
|
int64_t id = strtol(tmp1.c_str(), nullptr, 0);
|
|
if (id % 2 != 0) {
|
|
throw operation_failed();
|
|
}
|
|
++cnt;
|
|
std::string tmp2 = kvRange[i].value.toString();
|
|
int64_t value = strtol(tmp2.c_str(), nullptr, 0);
|
|
if (id != value) {
|
|
throw operation_failed();
|
|
}
|
|
}
|
|
}
|
|
begin = firstGreaterThan(kvRange.end()[-1].key);
|
|
} catch (Error& e) {
|
|
err = e;
|
|
}
|
|
if (err.isValid()) {
|
|
co_await tr.onError(err);
|
|
}
|
|
}
|
|
if (cnt != 1000) {
|
|
TraceEvent(SevError, "SnapTestVerifyCntValue").detail("Value", cnt);
|
|
throw operation_failed();
|
|
}
|
|
} else if (testID == 4) {
|
|
// create a snapshot with a non whitelisted binary path and operation
|
|
// should fail
|
|
bool testedFailure = false;
|
|
snapFailed = false;
|
|
while (true) {
|
|
snapUID = deterministicRandom()->randomUniqueID();
|
|
try {
|
|
StringRef snapCmdRef = "/bin/snap_create1.sh"_sr;
|
|
Future<Void> status = snapCreate(cx, snapCmdRef, snapUID);
|
|
co_await status;
|
|
break;
|
|
} catch (Error& e) {
|
|
if (e.code() == error_code_snap_not_fully_recovered_unsupported) {
|
|
snapFailed = true;
|
|
break;
|
|
}
|
|
if (e.code() == error_code_snap_path_not_whitelisted) {
|
|
testedFailure = true;
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
ASSERT(testedFailure || snapFailed);
|
|
}
|
|
co_await delay(0.0);
|
|
}
|
|
};
|
|
|
|
WorkloadFactory<SnapTestWorkload> SnapTestWorkloadFactory;
|