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
|
// Copyright Epic Games, Inc. All Rights Reserved.
#include <zencore/compactbinarybuilder.h>
#include <zencore/compactbinaryvalidation.h>
#include <zencore/config.h>
#include <zencore/except.h>
#include <zencore/filesystem.h>
#include <zencore/fmtutils.h>
#include <zencore/logging.h>
#include <zencore/memory/fmalloc.h>
#include <zencore/memory/llm.h>
#include <zencore/memory/memory.h>
#include <zencore/memory/memorytrace.h>
#include <zencore/memory/newdelete.h>
#include <zencore/scopeguard.h>
#include <zencore/sentryintegration.h>
#include <zencore/session.h>
#include <zencore/string.h>
#include <zencore/thread.h>
#include <zencore/trace.h>
#include <zentelemetry/otlptrace.h>
#include <zenutil/config/commandlineoptions.h>
#include <zenutil/service.h>
#include "diag/logging.h"
#include "compute/computeserver.h"
#include "storage/storageconfig.h"
#include "storage/zenstorageserver.h"
#include "hub/zenhubserver.h"
#if ZEN_PLATFORM_WINDOWS
# include <zencore/windows.h>
# include <zenutil/windows/windowsservice.h>
#endif
//////////////////////////////////////////////////////////////////////////
// We don't have any doctest code in this file but this is needed to bring
// in some shared code into the executable
#if ZEN_WITH_TESTS
# define ZEN_TEST_WITH_RUNNER 1
# include <zencore/testing.h>
#endif
namespace zen::utils {
std::atomic_uint32_t SignalCounter[NSIG] = {0};
static void
SignalCallbackHandler(int SigNum)
{
if (SigNum >= 0 && SigNum < NSIG)
{
SignalCounter[SigNum].fetch_add(1);
}
}
} // namespace zen::utils
namespace zen {
//////////////////////////////////////////////////////////////////////////
#if ZEN_PLATFORM_WINDOWS
/** Windows Service wrapper for Zen servers
*
* This class wraps a Zen server main entry point (the Main template parameter)
* into a Windows Service by implementing the WindowsService interface.
*
* The Main type needs to implement the virtual functions from the ZenServerMain
* base class, which provides the actual server logic.
*/
template<class Main>
class ZenWindowsService : public WindowsService
{
public:
ZenWindowsService(typename Main::Config& ServerOptions) : m_EntryPoint(ServerOptions) {}
ZenWindowsService(const ZenWindowsService&) = delete;
ZenWindowsService& operator=(const ZenWindowsService&) = delete;
virtual int Run() override { return m_EntryPoint.Run(); }
private:
Main m_EntryPoint;
};
#endif // ZEN_PLATFORM_WINDOWS
} // namespace zen
//////////////////////////////////////////////////////////////////////////
namespace zen {
/** Application main entry point template
*
* This function handles common application startup tasks while allowing
* different server types to be plugged in via the Main template parameter.
*
* On Windows, this function also handles platform-specific service
* installation and uninstallation.
*
* The Main type needs to implement the virtual functions from the ZenServerMain
* base class, which provides the actual server logic.
*
* The Main type is also expected to provide the following members:
*
* typedef Config -- Server configuration type, derived from ZenServerConfig
* typedef Configurator -- Server configuration handler type, implements ZenServerConfiguratorBase
*
*/
template<class Main>
int
AppMain(int argc, char* argv[])
{
using namespace std::literals;
signal(SIGINT, utils::SignalCallbackHandler);
signal(SIGTERM, utils::SignalCallbackHandler);
#if ZEN_PLATFORM_LINUX
IgnoreChildSignals();
#endif
try
{
typename Main::Config ServerOptions;
#if ZEN_WITH_TRACE
TraceInit("zenserver");
ServerOptions.HasTraceCommandlineOptions = GetTraceOptionsFromCommandline(/* out */ ServerOptions.TraceCmdLineOptions);
if (ServerOptions.HasTraceCommandlineOptions)
{
TraceConfigure(ServerOptions.TraceCmdLineOptions);
}
#endif // ZEN_WITH_TRACE
{
ZEN_OTEL_SPAN("AppMain::Configure");
typename Main::Configurator Configurator(ServerOptions);
Configurator.Configure(argc, argv);
}
if (ServerOptions.Detach)
{
#if ZEN_PLATFORM_LINUX | ZEN_PLATFORM_MAC
// Detach ourselves from any parent process
setsid();
#endif
}
LimitHardwareConcurrency(ServerOptions.CoreLimit);
std::string_view DeleteReason;
if (ServerOptions.IsCleanStart)
{
DeleteReason = "clean start requested"sv;
}
else if (!ServerOptions.BaseSnapshotDir.empty())
{
DeleteReason = "will initialize state from base snapshot"sv;
}
if (!DeleteReason.empty())
{
if (IsDir(ServerOptions.DataDir))
{
ZEN_CONSOLE_INFO("Deleting files from '{}' ({})", ServerOptions.DataDir, DeleteReason);
DeleteDirectories(ServerOptions.DataDir);
}
}
if (!IsDir(ServerOptions.DataDir))
{
ServerOptions.IsFirstRun = true;
CreateDirectories(ServerOptions.DataDir);
}
if (!ServerOptions.BaseSnapshotDir.empty())
{
ZEN_CONSOLE_INFO("Copying snapshot from '{}' into '{}", ServerOptions.BaseSnapshotDir, ServerOptions.DataDir);
CopyTree(ServerOptions.BaseSnapshotDir, ServerOptions.DataDir, {.EnableClone = true});
}
ZEN_MEMSCOPE(GetZenserverTag());
#if ZEN_PLATFORM_WINDOWS
if (ServerOptions.InstallService)
{
WindowsService::Install();
std::exit(0);
}
if (ServerOptions.UninstallService)
{
WindowsService::Delete();
std::exit(0);
}
ZenWindowsService<Main> App(ServerOptions);
return App.ServiceMain();
#else
if (ServerOptions.InstallService || ServerOptions.UninstallService)
{
throw std::runtime_error("Service mode is not supported on this platform");
}
Main App(ServerOptions);
return App.Run();
#endif
}
catch (const OptionParseException& ParseEx)
{
// The parsing error already outputs all the details so no need to output the command line here
fprintf(stderr, ZEN_APP_NAME " ERROR: %s\n", ParseEx.what());
return 1;
}
catch (const AssertException& AssertEx)
{
fprintf(stderr, ZEN_APP_NAME " ERROR: Caught assert exception in main: '%s'\n", AssertEx.FullDescription().c_str());
return 1;
}
catch (const std::exception& Ex)
{
fprintf(stderr, ZEN_APP_NAME " ERROR: Caught exception in main: '%s'\n", Ex.what());
return 1;
}
}
} // namespace zen
//////////////////////////////////////////////////////////////////////////
#if ZEN_WITH_TESTS
int
test_main(int argc, char** argv)
{
# if ZEN_PLATFORM_WINDOWS
setlocale(LC_ALL, "en_us.UTF8");
# endif // ZEN_PLATFORM_WINDOWS
zen::logging::InitializeLogging();
zen::logging::SetLogLevel(zen::logging::level::Debug);
zen::MaximizeOpenFileCount();
return ZEN_RUN_TESTS(argc, argv);
}
#endif
int
main(int argc, char* argv[])
{
#if ZEN_PLATFORM_WINDOWS
setlocale(LC_ALL, "en_us.UTF8");
#endif // ZEN_PLATFORM_WINDOWS
zen::CommandLineConverter ArgConverter(argc, argv);
using namespace zen;
using namespace std::literals;
// note: doctest has locally (in thirdparty) been fixed to not cause shutdown
// crashes due to TLS destructors
//
// mimalloc on the other hand might still be causing issues, in which case
// we should work out either how to eliminate the mimalloc dependency or how
// to configure it in a way that doesn't cause shutdown issues
#if 0
auto _ = zen::MakeGuard([] {
// Allow some time for worker threads to unravel, in an effort
// to prevent shutdown races in TLS object destruction, mainly due to
// threads which we don't directly control (Windows thread pool) and
// therefore can't join.
//
// This isn't a great solution, but for now it seems to help reduce
// shutdown crashes observed in some situations.
WaitForThreads(1000);
});
#endif
enum
{
kHub,
kStore,
kCompute,
kTest
} ServerMode = kStore;
if (argc >= 2)
{
if (argv[1] == "hub"sv)
{
ServerMode = kHub;
}
else if ((argv[1] == "store"sv) || (argv[1] == "storage"sv))
{
ServerMode = kStore;
}
else if (argv[1] == "compute"sv)
{
ServerMode = kCompute;
}
else if (argv[1] == "test"sv)
{
ServerMode = kTest;
}
}
switch (ServerMode)
{
case kTest:
#if ZEN_WITH_TESTS
return test_main(argc, argv);
#else
fprintf(stderr, "test option not available in release mode!\n");
exit(5);
#endif
break;
case kHub:
return AppMain<ZenHubServerMain>(argc, argv);
case kCompute:
#if ZEN_WITH_COMPUTE_SERVICES
return AppMain<ZenComputeServerMain>(argc, argv);
#else
fprintf(stderr, "compute services are not compiled in!\n");
exit(5);
#endif
default:
case kStore:
return AppMain<ZenStorageServerMain>(argc, argv);
}
}
|