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
|
// Copyright Epic Games, Inc. All Rights Reserved.
#include <zencore/filesystem.h>
#include <zencore/fmtutils.h>
#include <zencore/httpserver.h>
#include <zencore/iobuffer.h>
#include <zencore/refcount.h>
#include <zencore/string.h>
#include <zencore/thread.h>
#include <zencore/timer.h>
#include <zencore/windows.h>
#include <zenstore/cas.h>
#include <fmt/format.h>
#include <mimalloc-new-delete.h>
#include <mimalloc.h>
#include <spdlog/spdlog.h>
#include <asio.hpp>
#include <list>
#include <lua.hpp>
#include <optional>
#include <regex>
#include <unordered_map>
//////////////////////////////////////////////////////////////////////////
// We don't have any doctest code in this file but this is needed to bring
// in some shared code into the executable
#define DOCTEST_CONFIG_IMPLEMENT
#include <doctest/doctest.h>
#undef DOCTEST_CONFIG_IMPLEMENT
//////////////////////////////////////////////////////////////////////////
#include "casstore.h"
#include "config.h"
#include "diag/crashreport.h"
#include "diag/logging.h"
//////////////////////////////////////////////////////////////////////////
// Services
//
#include "admin/admin.h"
#include "cache/kvcache.h"
#include "cache/structuredcache.h"
#include "diag/diagsvcs.h"
#include "experimental/usnjournal.h"
#include "projectstore.h"
#include "testing/launch.h"
#include "upstream/jupiter.h"
#include "upstream/zen.h"
#include "zenstore/gc.h"
#include "zenstore/scrub.h"
#define ZEN_APP_NAME "Zen store"
class ZenServer
{
public:
void Initialize(int BasePort, int ParentPid)
{
using namespace fmt::literals;
spdlog::info(ZEN_APP_NAME " initializing");
if (ParentPid)
{
m_Process.Initialize(ParentPid);
}
// Prototype config system, let's see how this pans out
ZenServiceConfig ServiceConfig;
ParseServiceConfig(m_DataRoot, /* out */ ServiceConfig);
// Ok so now we're configured, let's kick things off
zen::CasStoreConfiguration Config;
Config.RootDirectory = m_DataRoot / "CAS";
m_CasStore->Initialize(Config);
spdlog::info("instantiating project service");
m_ProjectStore = new zen::ProjectStore(*m_CasStore, m_DataRoot / "Builds");
m_HttpProjectService.reset(new zen::HttpProjectService{*m_CasStore, m_ProjectStore});
m_LocalProjectService = zen::LocalProjectService::New(*m_CasStore, m_ProjectStore);
m_HttpLaunchService = std::make_unique<zen::HttpLaunchService>(*m_CasStore);
if (ServiceConfig.LegacyCacheEnabled)
{
spdlog::info("instantiating legacy cache service");
m_CacheService.reset(new zen::HttpKvCacheService());
}
else
{
spdlog::info("NOT instantiating legacy cache service");
}
if (ServiceConfig.StructuredCacheEnabled)
{
spdlog::info("instantiating structured cache service");
m_StructuredCacheService.reset(new zen::HttpStructuredCacheService(m_DataRoot / "cache", *m_CasStore));
}
else
{
spdlog::info("NOT instantiating structured cache service");
}
m_Http.Initialize(BasePort);
m_Http.AddEndpoint(m_HealthService);
m_Http.AddEndpoint(m_TestService);
m_Http.AddEndpoint(m_AdminService);
if (m_HttpProjectService)
{
m_Http.AddEndpoint(*m_HttpProjectService);
}
m_Http.AddEndpoint(m_CasService);
if (m_CacheService)
{
spdlog::info("instantiating legacy cache service");
m_Http.AddEndpoint(*m_CacheService);
}
if (m_StructuredCacheService)
{
m_Http.AddEndpoint(*m_StructuredCacheService);
}
if (m_HttpLaunchService)
{
m_Http.AddEndpoint(*m_HttpLaunchService);
}
// Experimental
//
// m_ZenMesh.Start(1337);
}
void Run()
{
if (m_Process.IsValid())
{
EnqueueTimer();
}
if (!m_TestMode)
{
spdlog::info("__________ _________ __ ");
spdlog::info("\\____ /____ ____ / _____// |_ ___________ ____ ");
spdlog::info(" / // __ \\ / \\ \\_____ \\\\ __\\/ _ \\_ __ \\_/ __ \\ ");
spdlog::info(" / /\\ ___/| | \\ / \\| | ( <_> ) | \\/\\ ___/ ");
spdlog::info("/_______ \\___ >___| / /_______ /|__| \\____/|__| \\___ >");
spdlog::info(" \\/ \\/ \\/ \\/ \\/ ");
}
spdlog::info(ZEN_APP_NAME " now running");
m_Http.Run(m_TestMode);
spdlog::info(ZEN_APP_NAME " exiting");
m_IoContext.stop();
}
void RequestExit(int ExitCode)
{
RequestApplicationExit(ExitCode);
m_Http.RequestExit();
}
void Cleanup() { spdlog::info(ZEN_APP_NAME " cleaning up"); }
void SetTestMode(bool State) { m_TestMode = State; }
void SetDataRoot(std::filesystem::path Root) { m_DataRoot = Root; }
void EnqueueTimer()
{
m_PidCheckTimer.expires_after(std::chrono::seconds(1));
m_PidCheckTimer.async_wait([this](const asio::error_code&) { CheckOwnerPid(); });
}
void CheckOwnerPid()
{
if (m_Process.IsRunning())
{
EnqueueTimer();
}
else
{
spdlog::info(ZEN_APP_NAME " exiting since parent process id {} is gone", m_Process.Pid());
RequestExit(0);
}
}
private:
bool m_TestMode = false;
std::filesystem::path m_DataRoot;
asio::io_context m_IoContext;
asio::steady_timer m_PidCheckTimer{m_IoContext};
zen::Process m_Process;
zen::HttpServer m_Http;
std::unique_ptr<zen::CasStore> m_CasStore{zen::CreateCasStore()};
zen::CasGc m_Gc{*m_CasStore};
zen::CasScrubber m_Scrubber{*m_CasStore};
HttpTestService m_TestService;
zen::HttpCasService m_CasService{*m_CasStore};
std::unique_ptr<zen::HttpKvCacheService> m_CacheService;
zen::RefPtr<zen::ProjectStore> m_ProjectStore;
zen::Ref<zen::LocalProjectService> m_LocalProjectService;
std::unique_ptr<zen::HttpLaunchService> m_HttpLaunchService;
std::unique_ptr<zen::HttpProjectService> m_HttpProjectService;
std::unique_ptr<zen::HttpStructuredCacheService> m_StructuredCacheService;
HttpAdminService m_AdminService;
HttpHealthService m_HealthService;
zen::Mesh m_ZenMesh{m_IoContext};
};
int
main(int argc, char* argv[])
{
mi_version();
ZenServerOptions GlobalOptions;
ParseGlobalCliOptions(argc, argv, GlobalOptions);
InitializeCrashReporting(GlobalOptions.DataDir / "crashdumps");
InitializeLogging(GlobalOptions);
spdlog::info("zen cache server starting on port {}", GlobalOptions.BasePort);
try
{
std::unique_ptr<std::thread> ShutdownThread;
std::unique_ptr<zen::NamedEvent> ShutdownEvent;
ZenServer Cache;
Cache.SetDataRoot(GlobalOptions.DataDir);
Cache.SetTestMode(GlobalOptions.IsTest);
Cache.Initialize(GlobalOptions.BasePort, GlobalOptions.OwnerPid);
if (!GlobalOptions.ChildId.empty())
{
zen::ExtendableStringBuilder<64> ShutdownEventName;
ShutdownEventName << GlobalOptions.ChildId << "_Shutdown";
ShutdownEvent.reset(new zen::NamedEvent{ShutdownEventName});
zen::NamedEvent ParentEvent{GlobalOptions.ChildId};
ParentEvent.Set();
ShutdownThread.reset(new std::thread{[&] {
ShutdownEvent->Wait();
spdlog::info("shutdown signal received");
Cache.RequestExit(0);
}});
}
Cache.Run();
Cache.Cleanup();
if (ShutdownEvent)
{
ShutdownEvent->Set();
ShutdownThread->join();
}
}
catch (std::exception& e)
{
SPDLOG_CRITICAL("Caught exception in main: {}", e.what());
}
return 0;
}
|