aboutsummaryrefslogtreecommitdiff
path: root/src/zenserver/hub/storageserverinstance.cpp
blob: f24379715c29c26c83655724c5197d9e7cca1b0e (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
// Copyright Epic Games, Inc. All Rights Reserved.

#include "storageserverinstance.h"

#include "hydration.h"

#include <zencore/assertfmt.h>
#include <zencore/filesystem.h>
#include <zencore/fmtutils.h>
#include <zencore/logging.h>

namespace zen {

StorageServerInstance::StorageServerInstance(ZenServerEnvironment& RunEnvironment,
											 std::string_view	   ModuleId,
											 std::filesystem::path FileHydrationPath,
											 std::filesystem::path HydrationTempPath)
: m_ModuleId(ModuleId)
, m_ServerInstance(RunEnvironment, ZenServerInstance::ServerMode::kStorageServer)
, m_HydrationPath(FileHydrationPath)
{
	m_BaseDir = RunEnvironment.CreateChildDir(ModuleId);
	m_TempDir = HydrationTempPath / ModuleId;
}

StorageServerInstance::~StorageServerInstance()
{
}

void
StorageServerInstance::SpawnServerProcess()
{
	ZEN_ASSERT_FORMAT(!m_ServerInstance.IsRunning(), "Storage server instance for module '{}' is already running", m_ModuleId);

	m_ServerInstance.SetServerExecutablePath(GetRunningExecutablePath());
	m_ServerInstance.SetDataDir(m_BaseDir);
#if ZEN_PLATFORM_WINDOWS
	m_ServerInstance.SetJobObject(m_JobObject);
#endif
	const uint16_t BasePort = m_ServerInstance.SpawnServerAndWaitUntilReady();

	ZEN_DEBUG("Storage server instance for module '{}' started, listening on port {}", m_ModuleId, BasePort);

	m_ServerInstance.EnableShutdownOnDestroy();
}

void
StorageServerInstance::Provision()
{
	RwLock::ExclusiveLockScope _(m_Lock);

	if (m_IsProvisioned)
	{
		ZEN_WARN("Storage server instance for module '{}' is already provisioned", m_ModuleId);

		return;
	}

	if (m_IsHibernated)
	{
		WakeLocked();
	}
	else
	{
		ZEN_INFO("Provisioning storage server instance for module '{}', at '{}'", m_ModuleId, m_BaseDir);

		Hydrate();

		SpawnServerProcess();
	}

	m_IsProvisioned = true;
}

void
StorageServerInstance::Deprovision()
{
	RwLock::ExclusiveLockScope _(m_Lock);

	if (!m_IsProvisioned)
	{
		ZEN_WARN("Attempted to deprovision storage server instance for module '{}' which is not provisioned", m_ModuleId);

		return;
	}

	ZEN_INFO("Deprovisioning storage server instance for module '{}'", m_ModuleId);

	m_ServerInstance.Shutdown();

	Dehydrate();

	m_IsProvisioned = false;
}

void
StorageServerInstance::Hibernate()
{
	// Signal server to shut down, but keep data around for later wake

	RwLock::ExclusiveLockScope _(m_Lock);

	if (!m_IsProvisioned)
	{
		ZEN_WARN("Attempted to hibernate storage server instance for module '{}' which is not provisioned", m_ModuleId);

		return;
	}

	if (m_IsHibernated)
	{
		ZEN_WARN("Storage server instance for module '{}' is already hibernated", m_ModuleId);

		return;
	}

	if (!m_ServerInstance.IsRunning())
	{
		ZEN_WARN("Attempted to hibernate storage server instance for module '{}' which is not running", m_ModuleId);

		// This is an unexpected state. Should consider the instance invalid?

		return;
	}

	try
	{
		m_ServerInstance.Shutdown();

		m_IsHibernated	= true;
		m_IsProvisioned = false;

		return;
	}
	catch (const std::exception& Ex)
	{
		ZEN_ERROR("Failed to hibernate storage server instance for module '{}': {}", m_ModuleId, Ex.what());
	}
}

void
StorageServerInstance::Wake()
{
	RwLock::ExclusiveLockScope _(m_Lock);
	WakeLocked();
}

void
StorageServerInstance::WakeLocked()
{
	// Start server in-place using existing data

	if (!m_IsHibernated)
	{
		ZEN_WARN("Attempted to wake storage server instance for module '{}' which is not hibernated", m_ModuleId);

		return;
	}

	ZEN_ASSERT_FORMAT(!m_ServerInstance.IsRunning(), "Storage server instance for module '{}' is already running", m_ModuleId);

	try
	{
		SpawnServerProcess();
		m_IsHibernated = false;
	}
	catch (const std::exception& Ex)
	{
		ZEN_ERROR("Failed to wake storage server instance for module '{}': {}", m_ModuleId, Ex.what());

		// TODO: this instance should be marked as invalid
	}
}

void
StorageServerInstance::Hydrate()
{
	HydrationConfig Config{.ServerStateDir		= m_BaseDir,
						   .TempDir				= m_TempDir,
						   .ModuleId			= m_ModuleId,
						   .TargetSpecification = WideToUtf8(m_HydrationPath.native())};

	std::unique_ptr<HydrationStrategyBase> Hydrator = CreateFileHydrator();

	Hydrator->Configure(Config);
	Hydrator->Hydrate();
}

void
StorageServerInstance::Dehydrate()
{
	HydrationConfig Config{.ServerStateDir		= m_BaseDir,
						   .TempDir				= m_TempDir,
						   .ModuleId			= m_ModuleId,
						   .TargetSpecification = WideToUtf8(m_HydrationPath.native())};

	std::unique_ptr<HydrationStrategyBase> Hydrator = CreateFileHydrator();

	Hydrator->Configure(Config);
	Hydrator->Dehydrate();
}

}  // namespace zen