2020-10-22 00:24:15 +08:00
|
|
|
/*
|
|
|
|
* BackupContainerS3BlobStore.actor.cpp
|
|
|
|
*
|
|
|
|
* This source file is part of the FoundationDB open source project
|
|
|
|
*
|
2022-03-22 04:36:23 +08:00
|
|
|
* Copyright 2013-2022 Apple Inc. and the FoundationDB project authors
|
2020-10-22 00:24:15 +08:00
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
*/
|
|
|
|
|
2020-10-30 11:42:23 +08:00
|
|
|
#include "fdbclient/AsyncFileS3BlobStore.actor.h"
|
2020-10-22 00:24:15 +08:00
|
|
|
#include "fdbclient/BackupContainerS3BlobStore.h"
|
2021-06-26 15:07:27 +08:00
|
|
|
#include "fdbrpc/AsyncFileEncrypted.h"
|
2020-10-24 14:27:13 +08:00
|
|
|
#include "fdbrpc/AsyncFileReadAhead.actor.h"
|
2020-10-22 00:24:15 +08:00
|
|
|
#include "flow/actorcompiler.h" // This must be the last #include.
|
|
|
|
|
2020-10-22 01:22:42 +08:00
|
|
|
class BackupContainerS3BlobStoreImpl {
|
2020-10-22 00:24:15 +08:00
|
|
|
public:
|
2020-10-22 01:22:42 +08:00
|
|
|
// Backup files to under a single folder prefix with subfolders for each named backup
|
|
|
|
static const std::string DATAFOLDER;
|
|
|
|
|
|
|
|
// Indexfolder contains keys for which user-named backups exist. Backup names can contain an arbitrary
|
|
|
|
// number of slashes so the backup names are kept in a separate folder tree from their actual data.
|
|
|
|
static const std::string INDEXFOLDER;
|
|
|
|
|
2020-10-30 11:42:23 +08:00
|
|
|
ACTOR static Future<std::vector<std::string>> listURLs(Reference<S3BlobStoreEndpoint> bstore, std::string bucket) {
|
2020-10-22 01:22:42 +08:00
|
|
|
state std::string basePath = INDEXFOLDER + '/';
|
2020-10-30 11:42:23 +08:00
|
|
|
S3BlobStoreEndpoint::ListResult contents = wait(bstore->listObjects(bucket, basePath));
|
2020-10-22 01:22:42 +08:00
|
|
|
std::vector<std::string> results;
|
2020-12-27 13:46:20 +08:00
|
|
|
for (const auto& f : contents.objects) {
|
2020-10-22 01:22:42 +08:00
|
|
|
results.push_back(
|
|
|
|
bstore->getResourceURL(f.name.substr(basePath.size()), format("bucket=%s", bucket.c_str())));
|
|
|
|
}
|
|
|
|
return results;
|
2020-10-22 00:24:15 +08:00
|
|
|
}
|
|
|
|
|
2020-10-22 01:22:42 +08:00
|
|
|
class BackupFile : public IBackupFile, ReferenceCounted<BackupFile> {
|
|
|
|
public:
|
2021-01-16 03:56:28 +08:00
|
|
|
BackupFile(std::string fileName, Reference<IAsyncFile> file)
|
|
|
|
: IBackupFile(fileName), m_file(file), m_offset(0) {}
|
2020-10-22 00:24:15 +08:00
|
|
|
|
2021-01-26 09:55:43 +08:00
|
|
|
Future<Void> append(const void* data, int len) override {
|
2020-10-22 01:22:42 +08:00
|
|
|
Future<Void> r = m_file->write(data, len, m_offset);
|
|
|
|
m_offset += len;
|
|
|
|
return r;
|
|
|
|
}
|
2020-10-22 00:24:15 +08:00
|
|
|
|
2021-01-26 09:55:43 +08:00
|
|
|
Future<Void> finish() override {
|
2020-10-22 01:22:42 +08:00
|
|
|
Reference<BackupFile> self = Reference<BackupFile>::addRef(this);
|
|
|
|
return map(m_file->sync(), [=](Void _) {
|
|
|
|
self->m_file.clear();
|
|
|
|
return Void();
|
|
|
|
});
|
|
|
|
}
|
2020-10-22 00:24:15 +08:00
|
|
|
|
2021-01-16 03:35:10 +08:00
|
|
|
int64_t size() const override { return m_offset; }
|
|
|
|
|
2020-10-22 01:22:42 +08:00
|
|
|
void addref() final { return ReferenceCounted<BackupFile>::addref(); }
|
|
|
|
void delref() final { return ReferenceCounted<BackupFile>::delref(); }
|
|
|
|
|
|
|
|
private:
|
|
|
|
Reference<IAsyncFile> m_file;
|
2021-01-16 03:35:10 +08:00
|
|
|
int64_t m_offset;
|
2020-10-22 00:24:15 +08:00
|
|
|
};
|
|
|
|
|
2020-10-22 01:22:42 +08:00
|
|
|
ACTOR static Future<BackupContainerFileSystem::FilesAndSizesT> listFiles(
|
|
|
|
Reference<BackupContainerS3BlobStore> bc,
|
|
|
|
std::string path,
|
|
|
|
std::function<bool(std::string const&)> pathFilter) {
|
|
|
|
// pathFilter expects container based paths, so create a wrapper which converts a raw path
|
|
|
|
// to a container path by removing the known backup name prefix.
|
|
|
|
state int prefixTrim = bc->dataPath("").size();
|
|
|
|
std::function<bool(std::string const&)> rawPathFilter = [=](const std::string& folderPath) {
|
|
|
|
ASSERT(folderPath.size() >= prefixTrim);
|
|
|
|
return pathFilter(folderPath.substr(prefixTrim));
|
|
|
|
};
|
|
|
|
|
2020-10-30 11:42:23 +08:00
|
|
|
state S3BlobStoreEndpoint::ListResult result = wait(bc->m_bstore->listObjects(
|
2020-10-22 01:22:42 +08:00
|
|
|
bc->m_bucket, bc->dataPath(path), '/', std::numeric_limits<int>::max(), rawPathFilter));
|
|
|
|
BackupContainerFileSystem::FilesAndSizesT files;
|
2020-12-27 13:46:20 +08:00
|
|
|
for (const auto& o : result.objects) {
|
2020-10-22 01:22:42 +08:00
|
|
|
ASSERT(o.name.size() >= prefixTrim);
|
|
|
|
files.push_back({ o.name.substr(prefixTrim), o.size });
|
|
|
|
}
|
|
|
|
return files;
|
2020-10-22 00:24:15 +08:00
|
|
|
}
|
|
|
|
|
2020-10-22 01:22:42 +08:00
|
|
|
ACTOR static Future<Void> create(Reference<BackupContainerS3BlobStore> bc) {
|
|
|
|
wait(bc->m_bstore->createBucket(bc->m_bucket));
|
2020-10-22 00:24:15 +08:00
|
|
|
|
2020-10-22 01:22:42 +08:00
|
|
|
// Check/create the index entry
|
|
|
|
bool exists = wait(bc->m_bstore->objectExists(bc->m_bucket, bc->indexEntry()));
|
|
|
|
if (!exists) {
|
|
|
|
wait(bc->m_bstore->writeEntireFile(bc->m_bucket, bc->indexEntry(), ""));
|
|
|
|
}
|
2020-10-22 00:24:15 +08:00
|
|
|
|
2021-06-26 15:07:27 +08:00
|
|
|
if (bc->usesEncryption()) {
|
|
|
|
wait(bc->encryptionSetupComplete());
|
|
|
|
}
|
|
|
|
|
2020-10-22 01:22:42 +08:00
|
|
|
return Void();
|
2020-10-22 00:24:15 +08:00
|
|
|
}
|
|
|
|
|
2020-10-22 01:22:42 +08:00
|
|
|
ACTOR static Future<Void> deleteContainer(Reference<BackupContainerS3BlobStore> bc, int* pNumDeleted) {
|
|
|
|
bool e = wait(bc->exists());
|
|
|
|
if (!e) {
|
|
|
|
TraceEvent(SevWarnAlways, "BackupContainerDoesNotExist").detail("URL", bc->getURL());
|
|
|
|
throw backup_does_not_exist();
|
|
|
|
}
|
2020-10-22 00:24:15 +08:00
|
|
|
|
2020-10-22 01:22:42 +08:00
|
|
|
// First delete everything under the data prefix in the bucket
|
|
|
|
wait(bc->m_bstore->deleteRecursively(bc->m_bucket, bc->dataPath(""), pNumDeleted));
|
2020-10-22 00:24:15 +08:00
|
|
|
|
2020-10-22 01:22:42 +08:00
|
|
|
// Now that all files are deleted, delete the index entry
|
|
|
|
wait(bc->m_bstore->deleteObject(bc->m_bucket, bc->indexEntry()));
|
|
|
|
|
|
|
|
return Void();
|
|
|
|
}
|
|
|
|
};
|
2020-10-22 00:24:15 +08:00
|
|
|
|
2020-10-22 01:22:42 +08:00
|
|
|
const std::string BackupContainerS3BlobStoreImpl::DATAFOLDER = "data";
|
|
|
|
const std::string BackupContainerS3BlobStoreImpl::INDEXFOLDER = "backups";
|
2020-10-22 00:24:15 +08:00
|
|
|
|
2020-10-22 13:19:15 +08:00
|
|
|
std::string BackupContainerS3BlobStore::dataPath(const std::string& path) {
|
2020-10-22 01:22:42 +08:00
|
|
|
return BackupContainerS3BlobStoreImpl::DATAFOLDER + "/" + m_name + "/" + path;
|
2020-10-22 00:24:15 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Get the path of the backups's index entry
|
|
|
|
std::string BackupContainerS3BlobStore::indexEntry() {
|
2020-10-22 01:22:42 +08:00
|
|
|
return BackupContainerS3BlobStoreImpl::INDEXFOLDER + "/" + m_name;
|
2020-10-22 00:24:15 +08:00
|
|
|
}
|
|
|
|
|
2020-10-30 11:42:23 +08:00
|
|
|
BackupContainerS3BlobStore::BackupContainerS3BlobStore(Reference<S3BlobStoreEndpoint> bstore,
|
|
|
|
const std::string& name,
|
2021-06-26 15:07:27 +08:00
|
|
|
const S3BlobStoreEndpoint::ParametersT& params,
|
|
|
|
const Optional<std::string>& encryptionKeyFileName)
|
2020-10-22 00:24:15 +08:00
|
|
|
: m_bstore(bstore), m_name(name), m_bucket("FDB_BACKUPS_V2") {
|
2021-06-26 15:07:27 +08:00
|
|
|
setEncryptionKey(encryptionKeyFileName);
|
2020-10-22 00:24:15 +08:00
|
|
|
// Currently only one parameter is supported, "bucket"
|
2020-12-27 13:46:20 +08:00
|
|
|
for (const auto& [name, value] : params) {
|
|
|
|
if (name == "bucket") {
|
|
|
|
m_bucket = value;
|
2020-10-22 00:24:15 +08:00
|
|
|
continue;
|
|
|
|
}
|
2020-12-27 13:46:20 +08:00
|
|
|
TraceEvent(SevWarn, "BackupContainerS3BlobStoreInvalidParameter").detail("Name", name).detail("Value", value);
|
|
|
|
IBackupContainer::lastOpenError = format("Unknown URL parameter: '%s'", name.c_str());
|
2020-10-22 00:24:15 +08:00
|
|
|
throw backup_invalid_url();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void BackupContainerS3BlobStore::addref() {
|
|
|
|
return ReferenceCounted<BackupContainerS3BlobStore>::addref();
|
|
|
|
}
|
|
|
|
void BackupContainerS3BlobStore::delref() {
|
|
|
|
return ReferenceCounted<BackupContainerS3BlobStore>::delref();
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string BackupContainerS3BlobStore::getURLFormat() {
|
2020-10-30 11:42:23 +08:00
|
|
|
return S3BlobStoreEndpoint::getURLFormat(true) + " (Note: The 'bucket' parameter is required.)";
|
2020-10-22 00:24:15 +08:00
|
|
|
}
|
|
|
|
|
2020-10-22 13:19:15 +08:00
|
|
|
Future<Reference<IAsyncFile>> BackupContainerS3BlobStore::readFile(const std::string& path) {
|
2021-06-26 15:07:27 +08:00
|
|
|
Reference<IAsyncFile> f = makeReference<AsyncFileS3BlobStoreRead>(m_bstore, m_bucket, dataPath(path));
|
2021-07-21 06:24:52 +08:00
|
|
|
|
2021-06-26 15:07:27 +08:00
|
|
|
if (usesEncryption()) {
|
2021-06-28 09:55:57 +08:00
|
|
|
f = makeReference<AsyncFileEncrypted>(f, AsyncFileEncrypted::Mode::READ_ONLY);
|
2021-06-26 15:07:27 +08:00
|
|
|
}
|
|
|
|
f = makeReference<AsyncFileReadAheadCache>(f,
|
|
|
|
m_bstore->knobs.read_block_size,
|
|
|
|
m_bstore->knobs.read_ahead_blocks,
|
|
|
|
m_bstore->knobs.concurrent_reads_per_file,
|
|
|
|
m_bstore->knobs.read_cache_blocks_per_file);
|
|
|
|
return f;
|
2020-10-22 00:24:15 +08:00
|
|
|
}
|
|
|
|
|
2020-10-30 11:42:23 +08:00
|
|
|
Future<std::vector<std::string>> BackupContainerS3BlobStore::listURLs(Reference<S3BlobStoreEndpoint> bstore,
|
2020-10-22 13:19:15 +08:00
|
|
|
const std::string& bucket) {
|
2020-10-22 01:22:42 +08:00
|
|
|
return BackupContainerS3BlobStoreImpl::listURLs(bstore, bucket);
|
2020-10-22 00:24:15 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
Future<Reference<IBackupFile>> BackupContainerS3BlobStore::writeFile(const std::string& path) {
|
2021-06-26 15:07:27 +08:00
|
|
|
Reference<IAsyncFile> f = makeReference<AsyncFileS3BlobStoreWrite>(m_bstore, m_bucket, dataPath(path));
|
|
|
|
if (usesEncryption()) {
|
2021-06-28 09:55:57 +08:00
|
|
|
f = makeReference<AsyncFileEncrypted>(f, AsyncFileEncrypted::Mode::APPEND_ONLY);
|
2021-06-26 15:07:27 +08:00
|
|
|
}
|
|
|
|
return Future<Reference<IBackupFile>>(makeReference<BackupContainerS3BlobStoreImpl::BackupFile>(path, f));
|
2020-10-22 00:24:15 +08:00
|
|
|
}
|
|
|
|
|
2020-10-22 13:19:15 +08:00
|
|
|
Future<Void> BackupContainerS3BlobStore::deleteFile(const std::string& path) {
|
2020-10-22 00:24:15 +08:00
|
|
|
return m_bstore->deleteObject(m_bucket, dataPath(path));
|
|
|
|
}
|
|
|
|
|
|
|
|
Future<BackupContainerFileSystem::FilesAndSizesT> BackupContainerS3BlobStore::listFiles(
|
2020-10-22 13:19:15 +08:00
|
|
|
const std::string& path,
|
|
|
|
std::function<bool(std::string const&)> pathFilter) {
|
2020-10-22 01:22:42 +08:00
|
|
|
return BackupContainerS3BlobStoreImpl::listFiles(
|
|
|
|
Reference<BackupContainerS3BlobStore>::addRef(this), path, pathFilter);
|
2020-10-22 00:24:15 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
Future<Void> BackupContainerS3BlobStore::create() {
|
2020-10-22 01:22:42 +08:00
|
|
|
return BackupContainerS3BlobStoreImpl::create(Reference<BackupContainerS3BlobStore>::addRef(this));
|
2020-10-22 00:24:15 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
Future<bool> BackupContainerS3BlobStore::exists() {
|
|
|
|
return m_bstore->objectExists(m_bucket, indexEntry());
|
|
|
|
}
|
|
|
|
|
|
|
|
Future<Void> BackupContainerS3BlobStore::deleteContainer(int* pNumDeleted) {
|
2020-10-22 01:22:42 +08:00
|
|
|
return BackupContainerS3BlobStoreImpl::deleteContainer(Reference<BackupContainerS3BlobStore>::addRef(this),
|
|
|
|
pNumDeleted);
|
2020-10-22 00:24:15 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
std::string BackupContainerS3BlobStore::getBucket() const {
|
|
|
|
return m_bucket;
|
|
|
|
}
|