aboutsummaryrefslogtreecommitdiff
path: root/src/zenserver/hub/zenhubserver.cpp
blob: 269de28c2a63909590dc613f3dc5021770fc5519 (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
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
// Copyright Epic Games, Inc. All Rights Reserved.

#include "zenhubserver.h"

#include "frontend/frontend.h"
#include "httphubservice.h"
#include "hub.h"

#include <zencore/config.h>
#include <zencore/fmtutils.h>
#include <zencore/memory/llm.h>
#include <zencore/memory/memorytrace.h>
#include <zencore/memory/tagtrace.h>
#include <zencore/scopeguard.h>
#include <zencore/sentryintegration.h>
#include <zencore/windows.h>
#include <zenhttp/httpapiservice.h>
#include <zenutil/service.h>
#include <zenutil/workerpools.h>

ZEN_THIRD_PARTY_INCLUDES_START
#include <cxxopts.hpp>
ZEN_THIRD_PARTY_INCLUDES_END

namespace zen {

const std::string&
GetDefaultConsulTokenEnvVariableName()
{
	static const std::string Name = "CONSUL_HTTP_TOKEN";
	return Name;
}

void
ZenHubServerConfigurator::AddCliOptions(cxxopts::Options& Options)
{
	const char* DefaultInstanceHttp = "asio";

#if ZEN_WITH_HTTPSYS
	if (!windows::IsRunningOnWine())
	{
		DefaultInstanceHttp = "httpsys";
	}
#endif

	Options.add_option("hub",
					   "",
					   "upstream-notification-endpoint",
					   "Endpoint URL for upstream notifications",
					   cxxopts::value<std::string>(m_ServerOptions.UpstreamNotificationEndpoint)->default_value(""),
					   "");

	Options.add_option("hub",
					   "",
					   "instance-id",
					   "Instance ID for use in notifications",
					   cxxopts::value<std::string>(m_ServerOptions.InstanceId)->default_value(""),
					   "");

	Options.add_option("hub",
					   "",
					   "consul-endpoint",
					   "Consul endpoint URL for service registration (empty = disabled)",
					   cxxopts::value<std::string>(m_ServerOptions.ConsulEndpoint)->default_value(""),
					   "");

	Options.add_option("hub",
					   "",
					   "consul-token-env",
					   fmt::format("Name of environment variable that holds the consul access token (defaults to '{}')",
								   GetDefaultConsulTokenEnvVariableName()),
					   cxxopts::value<std::string>(m_ServerOptions.ConsulTokenEnv)->default_value(""),
					   "<envvariable>");

	Options.add_option("hub",
					   "",
					   "consul-health-interval-seconds",
					   "Interval in seconds between Consul health checks",
					   cxxopts::value<uint32_t>(m_ServerOptions.ConsulHealthIntervalSeconds)->default_value("10"),
					   "<seconds>");

	Options.add_option("hub",
					   "",
					   "consul-deregister-after-seconds",
					   "Seconds after which Consul deregisters an unhealthy service",
					   cxxopts::value<uint32_t>(m_ServerOptions.ConsulDeregisterAfterSeconds)->default_value("30"),
					   "<seconds>");

	Options.add_option("hub",
					   "",
					   "hub-base-port-number",
					   "Base port number for provisioned instances",
					   cxxopts::value<uint16_t>(m_ServerOptions.HubBasePortNumber)->default_value("21000"),
					   "");

	Options.add_option("hub",
					   "",
					   "hub-instance-limit",
					   "Maximum number of provisioned instances for this hub",
					   cxxopts::value<int>(m_ServerOptions.HubInstanceLimit)->default_value("1000"),
					   "");

	Options.add_option("hub",
					   "",
					   "hub-instance-http",
					   "Select HTTP server implementation for provisioned instances (asio|"
#if ZEN_WITH_HTTPSYS
					   "httpsys|"
#endif
					   "null)",
					   cxxopts::value<std::string>(m_ServerOptions.HubInstanceHttpClass)->default_value(DefaultInstanceHttp),
					   "<instance http class>");

	Options.add_option("hub",
					   "",
					   "hub-instance-http-threads",
					   "Number of http server connection threads for provisioned instances",
					   cxxopts::value<unsigned int>(m_ServerOptions.HubInstanceHttpThreadCount),
					   "<instance http threads>");
	Options.add_option("hub",
					   "",
					   "hub-instance-corelimit",
					   "Limit concurrency of provisioned instances",
					   cxxopts::value(m_ServerOptions.HubInstanceCoreLimit),
					   "<instance core limit>");

	Options.add_option("hub",
					   "",
					   "hub-instance-config",
					   "Path to Lua config file for provisioned instances",
					   cxxopts::value(m_ServerOptions.HubInstanceConfigPath),
					   "<instance config>");

	Options.add_option("hub",
					   "",
					   "hub-hydration-target-spec",
					   "Specification for hydration target. 'file://<path>' prefix indicates file storage at <path>. Defaults to "
					   "<data-dir>/servers/hydration_storage",
					   cxxopts::value(m_ServerOptions.HydrationTargetSpecification),
					   "<hydration-target-spec>");

#if ZEN_PLATFORM_WINDOWS
	Options.add_option("hub",
					   "",
					   "hub-use-job-object",
					   "Enable the use of a Windows Job Object for child process management",
					   cxxopts::value<bool>(m_ServerOptions.HubUseJobObject)->default_value("true"),
					   "");
#endif	// ZEN_PLATFORM_WINDOWS
}

void
ZenHubServerConfigurator::AddConfigOptions(LuaConfig::Options& Options)
{
	ZEN_UNUSED(Options);
}

void
ZenHubServerConfigurator::ApplyOptions(cxxopts::Options& Options)
{
	ZEN_UNUSED(Options);
}

void
ZenHubServerConfigurator::OnConfigFileParsed(LuaConfig::Options& LuaOptions)
{
	ZEN_UNUSED(LuaOptions);
}

void
ZenHubServerConfigurator::ValidateOptions()
{
}

///////////////////////////////////////////////////////////////////////////

ZenHubServer::ZenHubServer()
{
}

ZenHubServer::~ZenHubServer()
{
	Cleanup();
}

void
ZenHubServer::OnModuleStateChanged(std::string_view					 HubInstanceId,
								   std::string_view					 ModuleId,
								   const HubProvisionedInstanceInfo& Info,
								   HubInstanceState					 PreviousState,
								   HubInstanceState					 NewState)
{
	ZEN_UNUSED(PreviousState);
	if (!m_ConsulClient)
	{
		return;
	}

	if (NewState == HubInstanceState::Provisioning || NewState == HubInstanceState::Provisioned)
	{
		consul::ServiceRegistrationInfo ServiceInfo{
			.ServiceId				= std::string(ModuleId),
			.ServiceName			= "zen-storage",
			.Port					= Info.Port,
			.HealthEndpoint			= "health",
			.Tags					= std::vector<std::pair<std::string, std::string>>{std::make_pair("module", std::string(ModuleId)),
																	   std::make_pair("zen-hub", std::string(HubInstanceId)),
																	   std::make_pair("version", std::string(ZEN_CFG_VERSION))},
			.HealthIntervalSeconds	= NewState == HubInstanceState::Provisioning
										  ? 0u
										  : m_ConsulHealthIntervalSeconds,	// Disable health checks while not finished provisioning
			.DeregisterAfterSeconds = NewState == HubInstanceState::Provisioning
										  ? 0u
										  : m_ConsulDeregisterAfterSeconds};  // Disable health checks while not finished provisioning

		if (!m_ConsulClient->RegisterService(ServiceInfo))
		{
			ZEN_WARN("Failed to register storage server instance for module '{}' with Consul, continuing anyway", ModuleId);
		}
		else
		{
			ZEN_INFO("Registered storage server instance for module '{}' at port {} with Consul as '{}'",
					 ModuleId,
					 Info.Port,
					 ServiceInfo.ServiceName);
		}
	}
	else if (NewState == HubInstanceState::Unprovisioned)
	{
		if (!m_ConsulClient->DeregisterService(ModuleId))
		{
			ZEN_WARN("Failed to deregister storage server instance for module '{}' at port {} from Consul, continuing anyway",
					 ModuleId,
					 Info.Port);
		}
		else
		{
			ZEN_INFO("Deregistered storage server instance for module '{}' at port {} from Consul", ModuleId, Info.Port);
		}
	}
	// Transitional states (Deprovisioning, Hibernating, Waking, Recovering, Crashed)
	// and Hibernated are intentionally ignored.
}

int
ZenHubServer::Initialize(const ZenHubServerConfig& ServerConfig, ZenServerState::ZenServerEntry* ServerEntry)
{
	ZEN_TRACE_CPU("ZenHubServer::Initialize");
	ZEN_MEMSCOPE(GetZenserverTag());

	ZEN_INFO(ZEN_APP_NAME " initializing in HUB server mode");

	const int EffectiveBasePort = ZenServerBase::Initialize(ServerConfig, ServerEntry);
	if (EffectiveBasePort < 0)
	{
		return EffectiveBasePort;
	}

	// This is a workaround to make sure we can have automated tests. Without
	// this the ranges for different child zen hub processes could overlap with
	// the main test range.
	ZenServerEnvironment::SetBaseChildId(1000);

	m_DebugOptionForcedCrash = ServerConfig.ShouldCrash;

	InitializeState(ServerConfig);
	InitializeConsulRegistration(ServerConfig, EffectiveBasePort);
	InitializeServices(ServerConfig);
	RegisterServices(ServerConfig);

	ZenServerBase::Finalize();

	return EffectiveBasePort;
}

void
ZenHubServer::Cleanup()
{
	ZEN_TRACE_CPU("ZenHubServer::Cleanup");
	ZEN_INFO(ZEN_APP_NAME " cleaning up");
	try
	{
		m_IoContext.stop();
		if (m_IoRunner.joinable())
		{
			m_IoRunner.join();
		}

		ShutdownServices();
		if (m_Http)
		{
			m_Http->Close();
		}

		if (m_Hub)
		{
			m_Hub->Shutdown();
		}

		m_FrontendService.reset();
		m_HubService.reset();
		m_ApiService.reset();
		m_Hub.reset();

		m_ConsulRegistration.reset();
		m_ConsulClient.reset();
	}
	catch (const std::exception& Ex)
	{
		ZEN_ERROR("exception thrown during Cleanup() in {}: '{}'", ZEN_APP_NAME, Ex.what());
	}
}

void
ZenHubServer::InitializeState(const ZenHubServerConfig& ServerConfig)
{
	ZEN_UNUSED(ServerConfig);
}

void
ZenHubServer::InitializeServices(const ZenHubServerConfig& ServerConfig)
{
	ZEN_UNUSED(ServerConfig);

	ZEN_INFO("instantiating Hub");
	m_Hub = std::make_unique<Hub>(
		Hub::Configuration{.UseJobObject				 = ServerConfig.HubUseJobObject,
						   .BasePortNumber				 = ServerConfig.HubBasePortNumber,
						   .InstanceLimit				 = ServerConfig.HubInstanceLimit,
						   .InstanceHttpThreadCount		 = ServerConfig.HubInstanceHttpThreadCount,
						   .InstanceCoreLimit			 = ServerConfig.HubInstanceCoreLimit,
						   .InstanceConfigPath			 = ServerConfig.HubInstanceConfigPath,
						   .HydrationTargetSpecification = ServerConfig.HydrationTargetSpecification},
		ZenServerEnvironment(ZenServerEnvironment::Hub,
							 ServerConfig.DataDir / "hub",
							 ServerConfig.DataDir / "servers",
							 ServerConfig.HubInstanceHttpClass),
		&GetMediumWorkerPool(EWorkloadType::Background),
		m_ConsulClient ? Hub::AsyncModuleStateChangeCallbackFunc{[this, HubInstanceId = fmt::format("zen-hub-{}", ServerConfig.InstanceId)](
																	 std::string_view				   ModuleId,
																	 const HubProvisionedInstanceInfo& Info,
																	 HubInstanceState				   PreviousState,
																	 HubInstanceState				   NewState) {
			OnModuleStateChanged(HubInstanceId, ModuleId, Info, PreviousState, NewState);
		}}
					   : Hub::AsyncModuleStateChangeCallbackFunc{});

	ZEN_INFO("instantiating API service");
	m_ApiService = std::make_unique<zen::HttpApiService>(*m_Http);

	ZEN_INFO("instantiating hub service");
	m_HubService = std::make_unique<HttpHubService>(*m_Hub);
	m_HubService->SetNotificationEndpoint(ServerConfig.UpstreamNotificationEndpoint, ServerConfig.InstanceId);

	m_FrontendService = std::make_unique<HttpFrontendService>(m_ContentRoot, m_StatusService);
}

void
ZenHubServer::RegisterServices(const ZenHubServerConfig& ServerConfig)
{
	ZEN_UNUSED(ServerConfig);

	if (m_HubService)
	{
		m_Http->RegisterService(*m_HubService);
	}

	if (m_ApiService)
	{
		m_Http->RegisterService(*m_ApiService);
	}

	if (m_FrontendService)
	{
		m_Http->RegisterService(*m_FrontendService);
	}
}

void
ZenHubServer::InitializeConsulRegistration(const ZenHubServerConfig& ServerConfig, int EffectivePort)
{
	if (ServerConfig.ConsulEndpoint.empty())
	{
		ZEN_INFO("Consul registration disabled (no endpoint configured)");
		return;
	}

	ZEN_INFO("Initializing Consul registration with endpoint: {}", ServerConfig.ConsulEndpoint);

	std::string ConsulAccessTokenEnvName =
		ServerConfig.ConsulTokenEnv.empty() ? GetDefaultConsulTokenEnvVariableName() : ServerConfig.ConsulTokenEnv;
	std::string ConsulAccessToken = GetEnvVariable(ConsulAccessTokenEnvName);
	if (ConsulAccessToken.empty())
	{
		if (!ServerConfig.ConsulTokenEnv.empty())
		{
			ZEN_WARN("Consul token environment variable '{}' is not set or empty", ServerConfig.ConsulTokenEnv);
		}
	}
	else
	{
		ZEN_INFO("Consul token read from environment variable '{}'", ConsulAccessTokenEnvName);
	}

	try
	{
		m_ConsulClient				   = std::make_unique<consul::ConsulClient>(ServerConfig.ConsulEndpoint, ConsulAccessToken);
		m_ConsulHealthIntervalSeconds  = ServerConfig.ConsulHealthIntervalSeconds;
		m_ConsulDeregisterAfterSeconds = ServerConfig.ConsulDeregisterAfterSeconds;

		consul::ServiceRegistrationInfo Info;
		Info.ServiceId	 = fmt::format("zen-hub-{}", ServerConfig.InstanceId);
		Info.ServiceName = "zen-hub";
		// Info.Address		= "localhost";	// Let the consul agent figure out out external address // TODO: Info.BaseUri?
		Info.Port			= static_cast<uint16_t>(EffectivePort);
		Info.HealthEndpoint = "hub/health";
		Info.Tags			= std::vector<std::pair<std::string, std::string>>{
			  std::make_pair("zen-hub", Info.ServiceId),
			  std::make_pair("version", std::string(ZEN_CFG_VERSION)),
			  std::make_pair("base-port-number", fmt::format("{}", ServerConfig.HubBasePortNumber)),
			  std::make_pair("instance-limit", fmt::format("{}", ServerConfig.HubInstanceLimit)),
			  std::make_pair("use-job-object", fmt::format("{}", ServerConfig.HubUseJobObject))};
		Info.HealthIntervalSeconds	= ServerConfig.ConsulHealthIntervalSeconds;
		Info.DeregisterAfterSeconds = ServerConfig.ConsulDeregisterAfterSeconds;

		m_ConsulRegistration = std::make_unique<consul::ServiceRegistration>(m_ConsulClient.get(), Info);

		ZEN_INFO("Consul service registration initiated for service ID: {}", Info.ServiceId);
	}
	catch (const std::exception& Ex)
	{
		// REQ-F-12: Hub should start successfully even if Consul registration fails
		ZEN_WARN("Failed to initialize Consul registration (hub will continue without it): {}", Ex.what());
		m_ConsulRegistration.reset();
		m_ConsulClient.reset();
	}
}

void
ZenHubServer::Run()
{
	if (m_ProcessMonitor.IsActive())
	{
		CheckOwnerPid();
	}

	if (!m_TestMode)
	{
		// clang-format off
		ZEN_INFO(R"(__________                ___ ___      ___.    )" "\n"
				 R"(\____    /____   ____    /   |   \ __ _\_ |__  )" "\n"
				 R"(  /     // __ \ /    \  /    ~    \  |  \ __ \ )" "\n"
				 R"( /     /\  ___/|   |  \ \    Y    /  |  / \_\ \)" "\n"
				 R"(/_______ \___  >___|  /  \___|_  /|____/|___  /)" "\n"
				 R"(        \/   \/     \/         \/           \/ )");
		// clang-format on

		ExtendableStringBuilder<256> BuildOptions;
		GetBuildOptions(BuildOptions, '\n');
		ZEN_INFO("Build options ({}/{}, {}):\n{}", GetOperatingSystemName(), GetCpuName(), GetCompilerName(), BuildOptions);
	}

	ZEN_INFO(ZEN_APP_NAME " now running as HUB (pid: {})", GetCurrentProcessId());

#if ZEN_PLATFORM_WINDOWS
	if (zen::windows::IsRunningOnWine())
	{
		ZEN_INFO("detected Wine session - " ZEN_APP_NAME " is not formally tested on Wine and may therefore not work or perform well");
	}
#endif

#if ZEN_USE_SENTRY
	ZEN_INFO("sentry crash handler {}", m_UseSentry ? "ENABLED" : "DISABLED");
	if (m_UseSentry)
	{
		SentryIntegration::ClearCaches();
	}
#endif

	if (m_DebugOptionForcedCrash)
	{
		ZEN_DEBUG_BREAK();
	}

	const bool IsInteractiveMode = IsInteractiveSession();	// &&!m_TestMode;

	if (m_ConsulRegistration)
	{
		if (!m_ConsulRegistration->IsRegistered())
		{
			ZEN_INFO("Waiting for consul integration to register...");
			m_ConsulRegistration->WaitForReadyEvent(2000);
		}
		if (!m_ConsulRegistration->IsRegistered())
		{
			m_ConsulClient.reset();
			m_ConsulRegistration.reset();
			ZEN_WARN("Consul registration failed, running without consul integration");
		}
	}

	SetNewState(kRunning);

	OnReady();

	m_Http->Run(IsInteractiveMode);

	SetNewState(kShuttingDown);

	ZEN_INFO(ZEN_APP_NAME " exiting");
}

//////////////////////////////////////////////////////////////////////////////////

ZenHubServerMain::ZenHubServerMain(ZenHubServerConfig& ServerOptions) : ZenServerMain(ServerOptions), m_ServerOptions(ServerOptions)
{
}

void
ZenHubServerMain::DoRun(ZenServerState::ZenServerEntry* Entry)
{
	ZenHubServer Server;
	Server.SetServerMode("Hub");
	Server.SetDataRoot(m_ServerOptions.DataDir);
	Server.SetContentRoot(m_ServerOptions.ContentDir);
	Server.SetTestMode(m_ServerOptions.IsTest);
	Server.SetDedicatedMode(m_ServerOptions.IsDedicated);
	Server.SetAllowPortProbing(!m_ServerOptions.IsDedicated && m_ServerOptions.AllowPortProbing);

	const int EffectiveBasePort = Server.Initialize(m_ServerOptions, Entry);
	if (EffectiveBasePort == -1)
	{
		// Server.Initialize has already logged what the issue is - just exit with failure code here.
		std::exit(1);
	}

	Entry->EffectiveListenPort = uint16_t(EffectiveBasePort);
	if (EffectiveBasePort != m_ServerOptions.BasePort)
	{
		ZEN_INFO(ZEN_APP_NAME " - relocated to base port {}", EffectiveBasePort);
		m_ServerOptions.BasePort = EffectiveBasePort;
	}

	std::unique_ptr<std::thread> ShutdownThread;
	std::unique_ptr<NamedEvent>	 ShutdownEvent;

	ExtendableStringBuilder<64> ShutdownEventName;
	ShutdownEventName << "Zen_" << m_ServerOptions.BasePort << "_Shutdown";
	ShutdownEvent.reset(new NamedEvent{ShutdownEventName});

	// Monitor shutdown signals

	ShutdownThread.reset(new std::thread{[&] {
		SetCurrentThreadName("shutdown_mon");

		ZEN_INFO("shutdown monitor thread waiting for shutdown signal '{}' for process {}", ShutdownEventName, zen::GetCurrentProcessId());

		if (ShutdownEvent->Wait())
		{
			ZEN_INFO("shutdown signal for pid {} received", zen::GetCurrentProcessId());
			Server.RequestExit(0);
		}
		else
		{
			ZEN_INFO("shutdown signal wait() failed");
		}
	}});

	auto CleanupShutdown = MakeGuard([&ShutdownEvent, &ShutdownThread] {
		ReportServiceStatus(ServiceStatus::Stopping);

		if (ShutdownEvent)
		{
			ShutdownEvent->Set();
		}
		if (ShutdownThread && ShutdownThread->joinable())
		{
			ShutdownThread->join();
		}
	});

	// If we have a parent process, establish the mechanisms we need
	// to be able to communicate readiness with the parent

	Server.SetIsReadyFunc([&] {
		std::error_code Ec;
		m_LockFile.Update(MakeLockData(true), Ec);
		ReportServiceStatus(ServiceStatus::Running);
		NotifyReady();
	});

	Server.Run();
}

}  // namespace zen