mirror of
https://github.com/nestriness/cdc-file-transfer.git
synced 2026-01-30 10:35:37 +02:00
This change introduces dynamic manifest updates to asset streaming. Asset streaming describes the directory to be streamed in a manifest, which is a proto definition of all content metadata. This information is sufficient to answer `stat` and `readdir` calls in the FUSE layer without additional round-trips to the workstation. When a directory is streamed for the first time, the corresponding manifest is created in two steps: 1. The directory is traversed recursively and the inode information of all contained files and directories is written to the manifest. 2. The content of all identified files is processed to generate each file's chunk list. This list is part of the definition of a file in the manifest. * The chunk boundaries are identified using our implementation of the FastCDC algorithm. * The hash of each chunk is calculated using the BLAKE3 hash function. * The length and hash of each chunk is appended to the file's chunk list. Prior to this change, when the user mounted a workstation directory on a client, the asset streaming server pushed an intermediate manifest to the gamelet as soon as step 1 was completed. At this point, the FUSE client started serving the virtual file system and was ready to answer `stat` and `readdir` calls. In case the FUSE client received any call that required file contents, such as `read`, it would block the caller until the server completed step 2 above and pushed the final manifest to the client. This works well for large directories (> 100GB) with a reasonable number of files (< 100k). But when dealing with millions of tiny files, creating the full manifest can take several minutes. With this change, we introduce dynamic manifest updates. When the FUSE layer receives an `open` or `readdir` request for a file or directory that is incomplete, it sends an RPC to the workstation about what information is missing from the manifest. The workstation identifies the corresponding file chunker or directory scanner tasks and moves them to the front of the queue. As soon as the task is completed, the workstation pushes an updated intermediate manifest to the client which now includes the information to serve the FUSE request. The queued FUSE request is resumed and returns the result to the caller. While this does not reduce the required time to build the final manifest, it splits up the work into smaller tasks. This allows us to interrupt the current work and prioritize those tasks which are required to handle an incoming request from the client. While this still takes a round-trip to the workstation plus the processing time for the task, an updated manifest is received within a few seconds, which is much better than blocking for several minutes. This latency is only visible when serving data while the manifest is still being created. The situation improves as the manifest creation on the workstation progresses. As soon as the final manifest is pushed, all metadata can be served directly without having to wait for pending tasks.
164 lines
5.3 KiB
C++
164 lines
5.3 KiB
C++
// Copyright 2022 Google LLC
|
|
//
|
|
// 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 "manifest/manifest_iterator.h"
|
|
|
|
#include <google/protobuf/text_format.h>
|
|
|
|
#include <cassert>
|
|
#include <fstream>
|
|
|
|
#include "absl/strings/str_format.h"
|
|
#include "common/errno_mapping.h"
|
|
#include "common/log.h"
|
|
#include "common/path.h"
|
|
#include "common/status_macros.h"
|
|
#include "manifest/content_id.h"
|
|
|
|
namespace cdc_ft {
|
|
|
|
// Holds the iteration state for an opened DIRECTORY asset.
|
|
struct ManifestIterator::OpenedDirectory {
|
|
OpenedDirectory(AssetProto* dir) : dir(dir) {}
|
|
~OpenedDirectory() = default;
|
|
|
|
// The DIRECTORY proto that is being iterated over. The object is owned by the
|
|
// parent OpenedDirectory struct.
|
|
AssetProto* dir;
|
|
|
|
// Holds the currently loaded indirect asset list.
|
|
std::unique_ptr<AssetListProto> asset_list;
|
|
|
|
// Index of the next direct asset to be returned from this directory. If the
|
|
// index is equal to dir->dir_assets_size(), all direct assets have been
|
|
// exhausted.
|
|
int next_asset = 0;
|
|
|
|
// Index of the next indirect asset list to be read. If the index is equal to
|
|
// dir->dir_indirect_assets_size(), all indirect asset lists have been
|
|
// exhausted.
|
|
int next_asset_list = 0;
|
|
|
|
// Index of the next asset of the currently loaded indirect asset list. If the
|
|
// index is equal to asset_list->assets_size(), all assets in this list have
|
|
// been exhausted.
|
|
int next_asset_list_asset = 0;
|
|
};
|
|
|
|
ManifestIterator::ManifestIterator(DataStoreReader* data_store)
|
|
: last_opened_dir_(nullptr), data_store_(data_store) {
|
|
assert(data_store_ != nullptr);
|
|
}
|
|
|
|
ManifestIterator::~ManifestIterator() = default;
|
|
|
|
absl::Status ManifestIterator::Open(const ContentIdProto& manifest_id) {
|
|
Reset();
|
|
status_ = data_store_->GetProto(manifest_id, &manifest_);
|
|
if (status_.ok()) dirs_.emplace_back(manifest_.mutable_root_dir());
|
|
return status_;
|
|
}
|
|
|
|
absl::Status ManifestIterator::Open(const std::string& manifest_file) {
|
|
Reset();
|
|
errno = 0;
|
|
// Open input file.
|
|
std::ifstream fin(manifest_file, std::ios_base::in | std::ios_base::binary);
|
|
if (!fin) {
|
|
std::string msg =
|
|
absl::StrFormat("failed to open file '%s' for reading", manifest_file);
|
|
if (errno) {
|
|
status_ = ErrnoToCanonicalStatus(errno, "%s", msg);
|
|
} else {
|
|
status_ = absl::UnknownError(msg);
|
|
}
|
|
return status_;
|
|
}
|
|
// Parse proto.
|
|
if (!manifest_.ParseFromIstream(&fin)) {
|
|
status_ = absl::InternalError(absl::StrFormat(
|
|
"failed to parse Manifest proto from file '%s'", manifest_file));
|
|
return status_;
|
|
}
|
|
dirs_.emplace_back(manifest_.mutable_root_dir());
|
|
return absl::OkStatus();
|
|
}
|
|
|
|
bool ManifestIterator::Valid() const { return !dirs_.empty() && status_.ok(); }
|
|
|
|
AssetProto* ManifestIterator::MutableAsset(RepeatedAssetProto* assets,
|
|
int index) {
|
|
AssetProto* asset_pb = assets->Mutable(index);
|
|
// Recurse into sub-directories.
|
|
if (asset_pb->type() == AssetProto::DIRECTORY) dirs_.emplace_back(asset_pb);
|
|
return asset_pb;
|
|
}
|
|
|
|
void ManifestIterator::UpdateRelPath(const OpenedDirectory* od) {
|
|
if (last_opened_dir_ == od) return;
|
|
rel_path_.resize(0);
|
|
for (const auto& opened_dir : dirs_) {
|
|
path::AppendUnix(&rel_path_, opened_dir.dir->name());
|
|
}
|
|
last_opened_dir_ = od;
|
|
}
|
|
|
|
const AssetProto* ManifestIterator::NextEntry() {
|
|
while (!dirs_.empty() && status_.ok()) {
|
|
OpenedDirectory* od = &dirs_.back();
|
|
UpdateRelPath(od);
|
|
|
|
// First, iterate over the direct assets.
|
|
if (od->next_asset >= 0 && od->next_asset < od->dir->dir_assets_size()) {
|
|
return MutableAsset(od->dir->mutable_dir_assets(), od->next_asset++);
|
|
}
|
|
|
|
// Next, iterate over the currently loaded indirect asset list.
|
|
assert(od->next_asset_list_asset >= 0);
|
|
if (od->asset_list &&
|
|
od->next_asset_list_asset < od->asset_list->assets_size()) {
|
|
return MutableAsset(od->asset_list->mutable_assets(),
|
|
od->next_asset_list_asset++);
|
|
}
|
|
|
|
// Finally, load the next AssetListProto from the indirect assets.
|
|
assert(od->next_asset_list >= 0);
|
|
if (od->next_asset_list < od->dir->dir_indirect_assets_size()) {
|
|
// Create the proto, if needed.
|
|
if (!od->asset_list) od->asset_list = std::make_unique<AssetListProto>();
|
|
// Read the AssetListProto from the chunk store.
|
|
const ContentIdProto& asset_list_id =
|
|
od->dir->dir_indirect_assets(od->next_asset_list++);
|
|
od->next_asset_list_asset = 0;
|
|
status_ = data_store_->GetProto(asset_list_id, od->asset_list.get());
|
|
if (!status_.ok()) return nullptr;
|
|
// Restart the loop to read the first asset from the list.
|
|
continue;
|
|
}
|
|
|
|
// Nothing more to visit, we are done with this node.
|
|
dirs_.pop_back();
|
|
}
|
|
return nullptr;
|
|
}
|
|
|
|
void ManifestIterator::Reset() {
|
|
dirs_.clear();
|
|
last_opened_dir_ = nullptr;
|
|
status_ = absl::OkStatus();
|
|
rel_path_.resize(0);
|
|
}
|
|
|
|
} // namespace cdc_ft
|