mirror of
https://github.com/nestriness/cdc-file-transfer.git
synced 2026-01-30 10:25: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.
214 lines
8.4 KiB
C++
214 lines
8.4 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 <string>
|
|
#include <vector>
|
|
|
|
#include "absl/flags/flag.h"
|
|
#include "absl/flags/parse.h"
|
|
#include "absl_helper/jedec_size_flag.h"
|
|
#include "cdc_fuse_fs/cdc_fuse_fs.h"
|
|
#include "cdc_fuse_fs/config_stream_client.h"
|
|
#include "cdc_fuse_fs/constants.h"
|
|
#include "common/gamelet_component.h"
|
|
#include "common/log.h"
|
|
#include "common/path.h"
|
|
#include "data_store/data_provider.h"
|
|
#include "data_store/disk_data_store.h"
|
|
#include "data_store/grpc_reader.h"
|
|
#include "grpcpp/channel.h"
|
|
#include "grpcpp/create_channel.h"
|
|
#include "grpcpp/support/channel_arguments.h"
|
|
|
|
namespace cdc_ft {
|
|
namespace {
|
|
|
|
constexpr char kFuseFilename[] = "cdc_fuse_fs";
|
|
constexpr char kLibFuseFilename[] = "libfuse.so";
|
|
|
|
bool IsUpToDate(const std::string& components_arg) {
|
|
// Components are expected to reside in the same dir as the executable.
|
|
std::string component_dir;
|
|
absl::Status status = path::GetExeDir(&component_dir);
|
|
if (!status.ok()) {
|
|
// Should(TM) be super rare, so just log an error.
|
|
LOG_DEBUG("Failed to exe dir: %s", status.ToString());
|
|
return false;
|
|
}
|
|
|
|
std::vector<GameletComponent> components =
|
|
GameletComponent::FromCommandLineArgs(components_arg);
|
|
if (components.size() == 0) {
|
|
LOG_DEBUG("Invalid components arg '%s'", components_arg);
|
|
return false;
|
|
}
|
|
|
|
std::vector<GameletComponent> our_components;
|
|
status = GameletComponent::Get({path::Join(component_dir, kFuseFilename),
|
|
path::Join(component_dir, kLibFuseFilename)},
|
|
&our_components);
|
|
if (!status.ok()) {
|
|
LOG_DEBUG("Failed to get component data: %s", status.ToString())
|
|
return false;
|
|
}
|
|
|
|
if (components != our_components) {
|
|
LOG_DEBUG("Component mismatch, args don't match ours '%s' != '%s'",
|
|
GameletComponent::ToCommandLineArgs(components),
|
|
GameletComponent::ToCommandLineArgs(our_components));
|
|
return false;
|
|
}
|
|
|
|
return true;
|
|
}
|
|
|
|
} // namespace
|
|
} // namespace cdc_ft
|
|
|
|
ABSL_FLAG(std::string, instance, "", "Gamelet instance id");
|
|
ABSL_FLAG(
|
|
std::string, components, "",
|
|
"Whitespace-separated triples filename, size and timestamp of the "
|
|
"workstation version of this binary and dependencies. Used for a fast "
|
|
"up-to-date check.");
|
|
ABSL_FLAG(uint16_t, port, 0, "Port to connect to on localhost");
|
|
ABSL_FLAG(cdc_ft::JedecSize, prefetch_size, cdc_ft::JedecSize(512 << 10),
|
|
"Additional data to request from the server when a FUSE read of "
|
|
"maximum size is detected. This amount is added to the original "
|
|
"request. Supports common unit suffixes K, M, G");
|
|
ABSL_FLAG(std::string, cache_dir, "/var/cache/asset_streaming",
|
|
"Cache directory to store data chunks.");
|
|
ABSL_FLAG(int, cache_dir_levels, 2,
|
|
"Fanout of sub-directories to create within the cache directory.");
|
|
ABSL_FLAG(int, verbosity, 0, "Log verbosity");
|
|
ABSL_FLAG(bool, stats, false, "Enable statistics");
|
|
ABSL_FLAG(bool, check, false, "Execute consistency check");
|
|
ABSL_FLAG(cdc_ft::JedecSize, cache_capacity,
|
|
cdc_ft::JedecSize(cdc_ft::DiskDataStore::kDefaultCapacity),
|
|
"Cache capacity. Supports common unit suffixes K, M, G.");
|
|
ABSL_FLAG(uint32_t, cleanup_timeout, cdc_ft::DataProvider::kCleanupTimeoutSec,
|
|
"Period in seconds at which instance cache cleanups are run");
|
|
ABSL_FLAG(uint32_t, access_idle_timeout, cdc_ft::DataProvider::kAccessIdleSec,
|
|
"Do not run instance cache cleanups for this many seconds after the "
|
|
"last file access");
|
|
|
|
static_assert(static_cast<int>(absl::StatusCode::kOk) == 0, "kOk != 0");
|
|
|
|
// Usage: cdc_fuse_fs <ABSL_FLAGs> -- mount_dir [-d|-s|..]
|
|
// Any args after -- are FUSE args, search third_party/fuse for FUSE_OPT_KEY or
|
|
// FUSE_LIB_OPT (there doesn't seem to be a place where they're all described).
|
|
int main(int argc, char* argv[]) {
|
|
// Parse absl flags.
|
|
std::vector<char*> mount_args = absl::ParseCommandLine(argc, argv);
|
|
std::string instance = absl::GetFlag(FLAGS_instance);
|
|
std::string components = absl::GetFlag(FLAGS_components);
|
|
uint16_t port = absl::GetFlag(FLAGS_port);
|
|
std::string cache_dir = absl::GetFlag(FLAGS_cache_dir);
|
|
int cache_dir_levels = absl::GetFlag(FLAGS_cache_dir_levels);
|
|
int verbosity = absl::GetFlag(FLAGS_verbosity);
|
|
bool stats = absl::GetFlag(FLAGS_stats);
|
|
bool consistency_check = absl::GetFlag(FLAGS_check);
|
|
uint64_t cache_capacity = absl::GetFlag(FLAGS_cache_capacity).Size();
|
|
unsigned int dp_cleanup_timeout = absl::GetFlag(FLAGS_cleanup_timeout);
|
|
unsigned int dp_access_idle_timeout =
|
|
absl::GetFlag(FLAGS_access_idle_timeout);
|
|
|
|
// Log to console. Logs are streamed back to the workstation through the SSH
|
|
// session.
|
|
cdc_ft::Log::Initialize(std::make_unique<cdc_ft::ConsoleLog>(
|
|
cdc_ft::Log::VerbosityToLogLevel(verbosity)));
|
|
|
|
// Perform up-to-date check.
|
|
if (!cdc_ft::IsUpToDate(components)) {
|
|
printf("%s\n", cdc_ft::kFuseNotUpToDate);
|
|
return 0;
|
|
}
|
|
printf("%s\n", cdc_ft::kFuseUpToDate);
|
|
fflush(stdout);
|
|
|
|
// Create fs. The rest of the flags are mount flags, so pass them along.
|
|
absl::Status status = cdc_ft::cdc_fuse_fs::Initialize(
|
|
static_cast<int>(mount_args.size()), mount_args.data());
|
|
if (!status.ok()) {
|
|
LOG_ERROR("Failed to initialize file system: %s", status.ToString());
|
|
return static_cast<int>(status.code());
|
|
}
|
|
|
|
// Create disk data store.
|
|
absl::StatusOr<std::unique_ptr<cdc_ft::DiskDataStore>> store =
|
|
cdc_ft::DiskDataStore::Create(cache_dir_levels, cache_dir, false);
|
|
if (!store.ok()) {
|
|
LOG_ERROR("Failed to initialize the chunk cache in directory '%s': %s",
|
|
absl::GetFlag(FLAGS_cache_dir), store.status().ToString());
|
|
return 1;
|
|
}
|
|
LOG_INFO("Setting cache capacity to '%u'", cache_capacity);
|
|
store.value()->SetCapacity(cache_capacity);
|
|
LOG_INFO("Caching chunks in '%s'", store.value()->RootDir());
|
|
|
|
// Start a gRpc client.
|
|
std::string client_address = absl::StrFormat("localhost:%u", port);
|
|
grpc::ChannelArguments channel_args;
|
|
channel_args.SetMaxReceiveMessageSize(-1);
|
|
std::shared_ptr<grpc::Channel> grpc_channel = grpc::CreateCustomChannel(
|
|
client_address, grpc::InsecureChannelCredentials(), channel_args);
|
|
std::vector<std::unique_ptr<cdc_ft::DataStoreReader>> readers;
|
|
readers.emplace_back(
|
|
std::make_unique<cdc_ft::GrpcReader>(grpc_channel, stats));
|
|
cdc_ft::GrpcReader* grpc_reader =
|
|
static_cast<cdc_ft::GrpcReader*>(readers[0].get());
|
|
|
|
// Send all cached content ids to the client if statistics are enabled.
|
|
if (stats) {
|
|
LOG_INFO("Sending all cached content ids");
|
|
absl::StatusOr<std::vector<cdc_ft::ContentIdProto>> ids =
|
|
store.value()->List();
|
|
if (!ids.ok()) {
|
|
LOG_ERROR("Failed to get all cached content ids: %s",
|
|
ids.status().ToString());
|
|
return 1;
|
|
}
|
|
status = grpc_reader->SendCachedContentIds(*ids);
|
|
if (!status.ok()) {
|
|
LOG_ERROR("Failed to send all cached content ids: %s", status.ToString());
|
|
return 1;
|
|
}
|
|
}
|
|
|
|
// Create data provider.
|
|
size_t prefetch_size = absl::GetFlag(FLAGS_prefetch_size).Size();
|
|
cdc_ft::DataProvider data_provider(std::move(*store), std::move(readers),
|
|
prefetch_size, dp_cleanup_timeout,
|
|
dp_access_idle_timeout);
|
|
|
|
cdc_ft::cdc_fuse_fs::SetConfigClient(
|
|
std::make_unique<cdc_ft::ConfigStreamGrpcClient>(
|
|
std::move(instance), std::move(grpc_channel)));
|
|
|
|
// Run FUSE.
|
|
LOG_INFO("Running filesystem");
|
|
status = cdc_ft::cdc_fuse_fs::Run(&data_provider, consistency_check);
|
|
if (!status.ok()) {
|
|
LOG_ERROR("Filesystem stopped with error: %s", status.ToString());
|
|
}
|
|
LOG_INFO("Filesystem ran successfully and shuts down");
|
|
|
|
data_provider.Shutdown();
|
|
cdc_ft::cdc_fuse_fs::Shutdown();
|
|
cdc_ft::Log::Shutdown();
|
|
|
|
static_assert(static_cast<int>(absl::StatusCode::kOk) == 0, "kOk != 0");
|
|
return static_cast<int>(status.code());
|
|
}
|