Files
netris-cdc-file-transfer/manifest/pending_assets_queue.h
chrschng 76bbdb01bb Merge dynamic manifest updates to Github (#7)
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.
2022-11-16 11:20:32 +01:00

103 lines
3.7 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.
#ifndef MANIFEST_PENDING_ASSETS_QUEUE_H
#define MANIFEST_PENDING_ASSETS_QUEUE_H
#include "absl/time/time.h"
#include "manifest/manifest_proto_defs.h"
namespace cdc_ft {
class ManifestBuilder;
// Holds an asset that was requested to be prioritized at a given point in time.
struct PriorityAsset {
// Relative Unix file path.
std::string rel_file_path;
// Timestamp when this request was received.
absl::Time received;
};
// Represents an asset that has not been fully processed yet.
struct PendingAsset {
PendingAsset() {}
PendingAsset(AssetProto::Type type, std::string relative_path,
std::string filename, absl::Time deadline)
: type(type),
relative_path(std::move(relative_path)),
filename(std::move(filename)),
deadline(deadline) {}
// The asset type (either FILE or DIRECTORY).
AssetProto::Type type = AssetProto::UNKNOWN;
// Relative unix path of the directory containing this asset.
std::string relative_path;
// File name of the asset that still needs processing.
std::string filename;
// If this asset was explicitly prioritized, this field is set to true,
// otherwise false.
bool prioritized = false;
// If a deadline is set, it means that this asset was prioritized
// (implicitly or explicitly) and should be processed by this deadline. Once
// this asset has been processed, the manifest should be flushed if the
// deadline has expired. Otherwise, additional related assets can be queued
// and processed (implicit prioritization).
absl::Time deadline;
};
// Queues assets that still need to be processed before they are completed.
class PendingAssetsQueue {
public:
// Signature for a callback function to accept items to dequeue.
using AcceptFunc = std::function<bool(const PendingAsset&)>;
// The |min_processing_time| is used to calculate the deadline by which a
// pending asset should be returned to the requesting instance.
PendingAssetsQueue(absl::Duration min_processing_time);
// Adds the given asset |pending| to the queue of assets to complete.
// PendingAssets without a deadline will be queued at the end, while those
// with a given deadline will be inserted after other assets having a
// deadline.
void Add(PendingAsset pending);
// Removes a PendingAsset from the queue and stores it in |pending|. If
// |accept| is given, then only items for which |accept| returns true are
// considered. Returns true if an item was stored in |pending|, otherwise
// false is returned.
bool Dequeue(PendingAsset* pending, AcceptFunc accept = nullptr);
// Returns true if the queue is empty, otherwise returns false.
bool Empty() const { return queue_.empty(); }
// Modifies the list of queued assets to prioritize the assets given in
// |prio_assets|. Returns the deadline by which the processed assets should be
// returned to the requested instance.
absl::Time Prioritize(const std::vector<PriorityAsset>& prio_assets,
ManifestBuilder* manifest_builder);
private:
const absl::Duration min_processing_time_;
std::list<PendingAsset> queue_;
};
} // namespace cdc_ft
#endif // MANIFEST_PENDING_ASSETS_QUEUE_H