aboutsummaryrefslogtreecommitdiff
path: root/src/zenserver/hub/storageserverinstance.h
blob: 1b0078d875738b9dd9d387e5061f64001539cfe4 (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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
// Copyright Epic Games, Inc. All Rights Reserved.

#pragma once

#include <zencore/compactbinary.h>
#include <zenutil/zenserverprocess.h>

#include <atomic>
#include <filesystem>

namespace zen {

/**
 * Storage Server Instance
 *
 * This class manages the lifecycle of a storage server instance, and
 * provides functions to query its state. There should be one instance
 * per module ID.
 */
class StorageServerInstance
{
public:
	struct Configuration
	{
		uint16_t			  BasePort;
		std::filesystem::path HydrationTempPath;
		std::string			  HydrationTargetSpecification;
		CbObject			  HydrationOptions;
		uint32_t			  HttpThreadCount = 0;	// Automatic
		int					  CoreLimit		  = 0;	// Automatic
		std::filesystem::path ConfigPath;
	};

	StorageServerInstance(ZenServerEnvironment& RunEnvironment, const Configuration& Config, std::string_view ModuleId);
	~StorageServerInstance();

	inline std::string_view GetModuleId() const { return m_ModuleId; }
	inline uint16_t			GetBasePort() const { return m_Config.BasePort; }
	ProcessMetrics			GetProcessMetrics() const;

#if ZEN_PLATFORM_WINDOWS
	void SetJobObject(JobObject* InJobObject) { m_JobObject = InJobObject; }
#endif

	class SharedLockedPtr
	{
	public:
		SharedLockedPtr(const SharedLockedPtr&) = delete;
		SharedLockedPtr();

		SharedLockedPtr(RwLock& Lock, StorageServerInstance* Instance, bool Wait);

		SharedLockedPtr(SharedLockedPtr&& Rhs);
		~SharedLockedPtr();

		SharedLockedPtr& operator=(const SharedLockedPtr&) = delete;
		SharedLockedPtr& operator						   =(SharedLockedPtr&& Rhs);

		operator bool() const { return m_Instance != nullptr; }

		std::string_view GetModuleId() const;
		uint16_t		 GetBasePort() const
		{
			ZEN_ASSERT(m_Instance);
			return m_Instance->GetBasePort();
		}
		bool IsRunning() const;

		ProcessMetrics GetProcessMetrics() const
		{
			ZEN_ASSERT(m_Instance);
			return m_Instance->GetProcessMetrics();
		}

#if ZEN_WITH_TESTS
		void TerminateForTesting() const;  // kills the child process to simulate a crash
#endif

	private:
		RwLock*				   m_Lock	  = nullptr;
		StorageServerInstance* m_Instance = nullptr;
	};

	[[nodiscard]] SharedLockedPtr LockShared(bool Wait) { return SharedLockedPtr(m_Lock, this, Wait); }

	class ExclusiveLockedPtr
	{
	public:
		ExclusiveLockedPtr(const ExclusiveLockedPtr&) = delete;
		ExclusiveLockedPtr();

		ExclusiveLockedPtr(RwLock& Lock, StorageServerInstance* Instance, bool Wait);

		ExclusiveLockedPtr(ExclusiveLockedPtr&& Rhs);
		~ExclusiveLockedPtr();

		ExclusiveLockedPtr& operator=(const ExclusiveLockedPtr&) = delete;
		ExclusiveLockedPtr& operator							 =(ExclusiveLockedPtr&& Rhs);

		operator bool() const { return m_Instance != nullptr; }

		std::string_view GetModuleId() const;
		uint16_t		 GetBasePort() const
		{
			ZEN_ASSERT(m_Instance);
			return m_Instance->GetBasePort();
		}
		bool IsRunning() const;

		void Provision();
		void Deprovision();
		void Hibernate();
		void Wake();

	private:
		RwLock*				   m_Lock	  = nullptr;
		StorageServerInstance* m_Instance = nullptr;
	};

	[[nodiscard]] ExclusiveLockedPtr LockExclusive(bool Wait) { return ExclusiveLockedPtr(m_Lock, this, Wait); }

private:
	void ProvisionLocked();
	void DeprovisionLocked();

	void HibernateLocked();
	void WakeLocked();

	mutable RwLock		m_Lock;
	const Configuration m_Config;
	std::string			m_ModuleId;
	ZenServerInstance	m_ServerInstance;

	std::filesystem::path m_BaseDir;

	std::filesystem::path m_TempDir;

#if ZEN_PLATFORM_WINDOWS
	JobObject* m_JobObject = nullptr;
#endif

	void SpawnServerProcess();

	void Hydrate();
	void Dehydrate();

	friend class SharedLockedPtr;
	friend class ExclusiveLockedPtr;
};

}  // namespace zen