aboutsummaryrefslogtreecommitdiff
path: root/src/zenserver/hub/hydration.h
blob: fc2f309b2b63c35a2f2a0ef2aff44012dac1f5b1 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
// Copyright Epic Games, Inc. All Rights Reserved.

#pragma once

#include <zencore/compactbinary.h>

#include <filesystem>
#include <optional>

namespace zen {

class WorkerThreadPool;

struct HydrationConfig
{
	// Location of server state to hydrate/dehydrate
	std::filesystem::path ServerStateDir;
	// Temporary directory available for use during hydration/dehydration
	std::filesystem::path TempDir;
	// Module ID of the server state being hydrated/dehydrated
	std::string ModuleId;
	// Back-end specific target specification (e.g. S3 bucket, file path, etc)
	std::string TargetSpecification;
	// Full config object when using --hub-hydration-target-config (mutually exclusive with TargetSpecification)
	CbObject Options;

	struct ThreadingOptions
	{
		WorkerThreadPool*  WorkerPool;
		std::atomic<bool>* AbortFlag;
		std::atomic<bool>* PauseFlag;
	};

	// External threading for parallel I/O and hashing. If not set, work runs inline on the caller's thread.
	std::optional<ThreadingOptions> Threading;
};

/**
 * @brief State hydration strategy interface
 *
 * An instance of this interface is used to perform hydration OR
 * dehydration of server state. It's expected to be used only once
 * and not reused.
 *
 */
struct HydrationStrategyBase
{
	virtual ~HydrationStrategyBase() = default;

	// Set up the hydration target from Config. Must be called before Hydrate/Dehydrate.
	virtual void Configure(const HydrationConfig& Config) = 0;

	// Upload server state to the configured target. ServerStateDir is wiped on success.
	// On failure, ServerStateDir is left intact.
	virtual void Dehydrate(const CbObject& CachedState) = 0;

	// Download state from the configured target into ServerStateDir. Returns cached state for the next Dehydrate.
	// On failure, ServerStateDir is wiped and an empty CbObject is returned.
	virtual CbObject Hydrate() = 0;

	// Delete all stored data for this module from the configured backend, then clean ServerStateDir and TempDir.
	virtual void Obliterate() = 0;
};

// Create a configured hydrator based on Config. Ready to call Hydrate/Dehydrate immediately.
std::unique_ptr<HydrationStrategyBase> CreateHydrator(const HydrationConfig& Config);

#if ZEN_WITH_TESTS
void hydration_forcelink();
#endif	// ZEN_WITH_TESTS

}  // namespace zen