aboutsummaryrefslogtreecommitdiff
path: root/src/zenserver/hub/hub.h
blob: 8ee9130f65d46ecdf439645895230434dfe18383 (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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
// Copyright Epic Games, Inc. All Rights Reserved.

#pragma once

#include "hubinstancestate.h"
#include "resourcemetrics.h"
#include "storageserverinstance.h"

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

#include <chrono>
#include <deque>
#include <filesystem>
#include <functional>
#include <memory>
#include <thread>
#include <unordered_map>

namespace zen {

class HttpClient;
class WorkerThreadPool;

/**
 * Hub
 *
 * Core logic for managing storage server instances on behalf of external clients.
 */

struct HubProvisionedInstanceInfo
{
	std::string BaseUri;
	uint16_t	Port;
};

class Hub
{
public:
	struct WatchDogConfiguration
	{
		std::chrono::milliseconds CycleInterval				   = std::chrono::seconds(3);
		std::chrono::milliseconds CycleProcessingBudget		   = std::chrono::milliseconds(500);
		std::chrono::milliseconds InstanceCheckThrottle		   = std::chrono::milliseconds(5);
		std::chrono::seconds	  ProvisionedInactivityTimeout = std::chrono::minutes(10);
		std::chrono::seconds	  HibernatedInactivityTimeout  = std::chrono::minutes(30);
		std::chrono::seconds	  InactivityCheckMargin		   = std::chrono::minutes(1);

		std::chrono::milliseconds ActivityCheckConnectTimeout = std::chrono::milliseconds(100);
		std::chrono::milliseconds ActivityCheckRequestTimeout = std::chrono::milliseconds(200);
	};

	struct Configuration
	{
		/** Enable or disable the use of a Windows Job Object for child process management.
		 *  When enabled, all spawned child processes are assigned to a job object with
		 *  JOB_OBJECT_LIMIT_KILL_ON_JOB_CLOSE, ensuring children are terminated if the hub
		 *  crashes or is force-killed.
		 */
		bool	 UseJobObject	= true;
		uint16_t BasePortNumber = 21000;

		int InstanceLimit = 1000;

		uint32_t			  InstanceHttpThreadCount = 0;	// Automatic
		int					  InstanceCoreLimit		  = 0;	// Automatic
		std::filesystem::path InstanceConfigPath;
		std::string			  HydrationTargetSpecification;
		CbObject			  HydrationOptions;

		WatchDogConfiguration WatchDog;

		ResourceMetrics ResourceLimits;
	};

	typedef std::function<
		void(std::string_view ModuleId, const HubProvisionedInstanceInfo& Info, HubInstanceState OldState, HubInstanceState NewState)>
		AsyncModuleStateChangeCallbackFunc;

	Hub(const Configuration&				 Config,
		ZenServerEnvironment&&				 RunEnvironment,
		WorkerThreadPool*					 OptionalWorkerPool		   = nullptr,
		AsyncModuleStateChangeCallbackFunc&& ModuleStateChangeCallback = {});
	~Hub();

	Hub(const Hub&) = delete;
	Hub& operator=(const Hub&) = delete;

	struct InstanceInfo
	{
		HubInstanceState					  State = HubInstanceState::Unprovisioned;
		std::chrono::system_clock::time_point StateChangeTime;
		ProcessMetrics						  Metrics;
		uint16_t							  Port = 0;
	};

	/**
	 * Deprovision all running instances
	 */
	void Shutdown();

	enum class EResponseCode
	{
		NotFound,
		Rejected,
		Accepted,
		Completed
	};

	struct Response
	{
		EResponseCode ResponseCode = EResponseCode::Rejected;
		std::string	  Message;
	};

	/**
	 * Provision a storage server instance for the given module ID.
	 *
	 * @param ModuleId The ID of the module to provision.
	 * @param OutInfo On success, information about the provisioned instance is returned here.
	 */
	Response Provision(std::string_view ModuleId, HubProvisionedInstanceInfo& OutInfo);

	/**
	 * Deprovision a storage server instance for the given module ID.
	 *
	 * @param ModuleId The ID of the module to deprovision.
	 */
	Response Deprovision(const std::string& ModuleId);

	/**
	 * Hibernate a storage server instance for the given module ID.
	 * The instance is shut down but its data is preserved; it can be woken later.
	 *
	 * @param ModuleId The ID of the module to hibernate.
	 */
	Response Hibernate(const std::string& ModuleId);

	/**
	 * Wake a hibernated storage server instance for the given module ID.
	 *
	 * @param ModuleId The ID of the module to wake.
	 */
	Response Wake(const std::string& ModuleId);

	/**
	 * Find info about storage server instance for the given module ID.
	 *
	 * @param ModuleId The ID of the module to find.
	 * @param OutInstanceInfo If found, the instance info will be returned here.
	 * @return true if the instance was found, false otherwise.
	 */
	bool Find(std::string_view ModuleId, InstanceInfo* OutInstanceInfo = nullptr);

	/**
	 * Enumerate a snapshot of all storage server instances.
	 *
	 * @param Callback The callback to invoke for each instance.
	 */
	void EnumerateModules(std::function<void(std::string_view ModuleId, const InstanceInfo&)> Callback);

	int GetInstanceCount();

	int GetMaxInstanceCount() const { return m_MaxInstanceCount.load(); }

	void GetMachineMetrics(SystemMetrics& OutSystemMetrict, DiskSpace& OutDiskSpace) const;

	bool IsInstancePort(uint16_t Port) const;

	const Configuration& GetConfig() const { return m_Config; }

#if ZEN_WITH_TESTS
	void TerminateModuleForTesting(const std::string& ModuleId);
#endif

private:
	const Configuration	 m_Config;
	ZenServerEnvironment m_RunEnvironment;
	WorkerThreadPool*	 m_WorkerPool = nullptr;
	Latch				 m_BackgroundWorkLatch;
	std::atomic<bool>	 m_ShutdownFlag = false;

	AsyncModuleStateChangeCallbackFunc m_ModuleStateChangeCallback;

	std::string			  m_HydrationTargetSpecification;
	CbObject			  m_HydrationOptions;
	std::filesystem::path m_HydrationTempPath;

#if ZEN_PLATFORM_WINDOWS
	JobObject m_JobObject;
#endif
	mutable RwLock							m_Lock;
	std::unordered_map<std::string, size_t> m_InstanceLookup;

	// Mirrors ProcessMetrics with atomic fields, enabling lock-free reads alongside watchdog writes.
	struct AtomicProcessMetrics
	{
		std::atomic<uint64_t> MemoryBytes		 = 0;
		std::atomic<uint64_t> KernelTimeMs		 = 0;
		std::atomic<uint64_t> UserTimeMs		 = 0;
		std::atomic<uint64_t> WorkingSetSize	 = 0;
		std::atomic<uint64_t> PeakWorkingSetSize = 0;
		std::atomic<uint64_t> PagefileUsage		 = 0;
		std::atomic<uint64_t> PeakPagefileUsage	 = 0;

		ProcessMetrics Load() const;
		void		   Store(const ProcessMetrics& Metrics);
		void		   Reset();
	};

	struct ActiveInstance
	{
		// Invariant: Instance == nullptr if and only if State == Unprovisioned.
		// Both fields are only created/destroyed under the hub's exclusive lock.
		// State is an atomic because the watchdog reads it under a shared instance lock
		// without holding the hub lock.
		std::unique_ptr<StorageServerInstance> Instance;
		std::atomic<HubInstanceState>		   State = HubInstanceState::Unprovisioned;

		// Process metrics - written by WatchDog (inside instance shared lock), read lock-free.
		AtomicProcessMetrics ProcessMetrics;

		// Activity tracking - written by WatchDog, reset on every state transition.
		std::atomic<uint64_t>							   LastKnownActivitySum = 0;
		std::atomic<std::chrono::system_clock::time_point> LastActivityTime		= std::chrono::system_clock::time_point::min();

		// Set in UpdateInstanceStateLocked on every state transition; read lock-free by Find/EnumerateModules.
		std::atomic<std::chrono::system_clock::time_point> StateChangeTime = std::chrono::system_clock::time_point::min();
	};

	// UpdateInstanceState is overloaded to accept a locked instance pointer (exclusive or shared) or the hub exclusive
	// lock scope as a proof token that the caller holds an appropriate lock before mutating ActiveInstance::State.
	// State mutation and notification (NotifyStateUpdate) are intentionally decoupled - see NotifyStateUpdate below.

	HubInstanceState UpdateInstanceState(const StorageServerInstance::ExclusiveLockedPtr& Instance,
										 size_t											  ActiveInstanceIndex,
										 HubInstanceState								  NewState)
	{
		ZEN_ASSERT(Instance);
		return UpdateInstanceStateLocked(ActiveInstanceIndex, NewState);
	}
	HubInstanceState UpdateInstanceState(const StorageServerInstance::SharedLockedPtr& Instance,
										 size_t										   ActiveInstanceIndex,
										 HubInstanceState							   NewState)
	{
		ZEN_ASSERT(Instance);
		return UpdateInstanceStateLocked(ActiveInstanceIndex, NewState);
	}
	HubInstanceState UpdateInstanceState(const RwLock::ExclusiveLockScope& HubLock, size_t ActiveInstanceIndex, HubInstanceState NewState)
	{
		ZEN_UNUSED(HubLock);
		return UpdateInstanceStateLocked(ActiveInstanceIndex, NewState);
	}
	HubInstanceState UpdateInstanceStateLocked(size_t ActiveInstanceIndex, HubInstanceState NewState);

	std::vector<ActiveInstance> m_ActiveInstances;
	std::deque<size_t>			m_FreeActiveInstanceIndexes;
	SystemMetrics				m_SystemMetrics;
	DiskSpace					m_DiskSpace;
	std::atomic<int>			m_MaxInstanceCount = 0;
	std::thread					m_WatchDog;

	Event m_WatchDogEvent;
	void  WatchDog();
	void  UpdateMachineMetrics();
	bool  CheckInstanceStatus(HttpClient&							   ActivityHttpClient,
							  StorageServerInstance::SharedLockedPtr&& LockedInstance,
							  size_t								   ActiveInstanceIndex);
	void  AttemptRecoverInstance(std::string_view ModuleId);

	bool	 CanProvisionInstanceLocked(std::string_view ModuleId, std::string& OutReason);
	uint16_t GetInstanceIndexAssignedPort(size_t ActiveInstanceIndex) const;

	Response InternalDeprovision(const std::string& ModuleId, std::function<bool(ActiveInstance& Instance)>&& DeprovisionGate);
	void	 CompleteProvision(StorageServerInstance::ExclusiveLockedPtr& Instance,
							   size_t									  ActiveInstanceIndex,
							   HubInstanceState							  OldState,
							   bool										  IsNewInstance);
	void	 CompleteDeprovision(StorageServerInstance::ExclusiveLockedPtr& Instance, size_t ActiveInstanceIndex);
	void	 CompleteHibernate(StorageServerInstance::ExclusiveLockedPtr& Instance, size_t ActiveInstanceIndex, HubInstanceState OldState);
	void	 CompleteWake(StorageServerInstance::ExclusiveLockedPtr& Instance, size_t ActiveInstanceIndex, HubInstanceState OldState);

	// Notifications may fire slightly out of sync with the Hub's internal State flag.
	// The guarantee is that notifications are sent in the correct order, but the State
	// flag may be updated either before or after the notification fires depending on the
	// code path. Callers must not assume a specific ordering between the two.
	void NotifyStateUpdate(std::string_view ModuleId,
						   HubInstanceState OldState,
						   HubInstanceState NewState,
						   uint16_t			BasePort,
						   std::string_view BaseUri);
};

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

}  // namespace zen