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

#include "httphubservice.h"

#include "hub.h"
#include "storageserverinstance.h"

#include <zencore/compactbinarybuilder.h>
#include <zencore/fmtutils.h>
#include <zencore/logging.h>

namespace zen {

namespace {
	bool HandleFailureResults(HttpServerRequest& Request, const Hub::Response& Resp)
	{
		if (Resp.ResponseCode == Hub::EResponseCode::Rejected)
		{
			if (Resp.Message.empty())
			{
				Request.WriteResponse(HttpResponseCode::Conflict);
			}
			else
			{
				Request.WriteResponse(HttpResponseCode::Conflict, HttpContentType::kText, Resp.Message);
			}
			return true;
		}
		if (Resp.ResponseCode == Hub::EResponseCode::NotFound)
		{
			if (Resp.Message.empty())
			{
				Request.WriteResponse(HttpResponseCode::NotFound);
			}
			else
			{
				Request.WriteResponse(HttpResponseCode::NotFound, HttpContentType::kText, Resp.Message);
			}
			return true;
		}
		return false;
	}
}  // namespace

HttpHubService::HttpHubService(Hub& Hub) : m_Hub(Hub)
{
	using namespace std::literals;

	m_Router.AddMatcher("moduleid", [](std::string_view Str) -> bool {
		for (const auto C : Str)
		{
			if (std::isalnum(C) || C == '-')
			{
				// fine
			}
			else
			{
				// not fine
				return false;
			}
		}

		return true;
	});

	m_Router.RegisterRoute(
		"status",
		[this](HttpRouterRequest& Req) {
			CbObjectWriter Obj;
			Obj.BeginArray("modules");
			m_Hub.EnumerateModules([&Obj](std::string_view ModuleId, const Hub::InstanceInfo& Info) {
				Obj.BeginObject();
				{
					Obj << "moduleId" << ModuleId;
					Obj << "state" << ToString(Info.State);
					Obj << "port" << Info.Port;
					Obj.BeginObject("process_metrics");
					{
						Obj << "MemoryBytes" << Info.Metrics.MemoryBytes;
						Obj << "KernelTimeMs" << Info.Metrics.KernelTimeMs;
						Obj << "UserTimeMs" << Info.Metrics.UserTimeMs;
						Obj << "WorkingSetSize" << Info.Metrics.WorkingSetSize;
						Obj << "PeakWorkingSetSize" << Info.Metrics.PeakWorkingSetSize;
						Obj << "PagefileUsage" << Info.Metrics.PagefileUsage;
						Obj << "PeakPagefileUsage" << Info.Metrics.PeakPagefileUsage;
					}
					Obj.EndObject();
				}
				Obj.EndObject();
			});
			Obj.EndArray();
			Req.ServerRequest().WriteResponse(HttpResponseCode::OK, Obj.Save());
		},
		HttpVerb::kGet);

	m_Router.RegisterRoute(
		"modules/{moduleid}",
		[this](HttpRouterRequest& Req) {
			std::string_view ModuleId = Req.GetCapture(1);

			if (Req.ServerRequest().RequestVerb() == HttpVerb::kDelete)
			{
				HandleModuleDelete(Req.ServerRequest(), ModuleId);
			}
			else
			{
				HandleModuleGet(Req.ServerRequest(), ModuleId);
			}
		},
		HttpVerb::kGet | HttpVerb::kDelete);

	m_Router.RegisterRoute(
		"modules/{moduleid}/provision",
		[this](HttpRouterRequest& Req) {
			std::string_view ModuleId = Req.GetCapture(1);

			try
			{
				HubProvisionedInstanceInfo Info;
				Hub::Response			   Resp = m_Hub.Provision(ModuleId, Info);

				if (HandleFailureResults(Req.ServerRequest(), Resp))
				{
					return;
				}

				const HttpResponseCode HttpCode =
					(Resp.ResponseCode == Hub::EResponseCode::Accepted) ? HttpResponseCode::Accepted : HttpResponseCode::OK;
				CbObjectWriter Obj;
				Obj << "moduleId" << ModuleId;
				Obj << "baseUri" << Info.BaseUri;
				Obj << "port" << Info.Port;
				return Req.ServerRequest().WriteResponse(HttpCode, Obj.Save());
			}
			catch (const std::exception& Ex)
			{
				ZEN_ERROR("Exception while provisioning module '{}': {}", ModuleId, Ex.what());
				throw;
			}
		},
		HttpVerb::kPost);

	m_Router.RegisterRoute(
		"modules/{moduleid}/deprovision",
		[this](HttpRouterRequest& Req) {
			std::string_view ModuleId = Req.GetCapture(1);

			try
			{
				Hub::Response Resp = m_Hub.Deprovision(std::string(ModuleId));

				if (HandleFailureResults(Req.ServerRequest(), Resp))
				{
					return;
				}

				const HttpResponseCode HttpCode =
					(Resp.ResponseCode == Hub::EResponseCode::Accepted) ? HttpResponseCode::Accepted : HttpResponseCode::OK;
				CbObjectWriter Obj;
				Obj << "moduleId" << ModuleId;
				return Req.ServerRequest().WriteResponse(HttpCode, Obj.Save());
			}
			catch (const std::exception& Ex)
			{
				ZEN_ERROR("Exception while deprovisioning module '{}': {}", ModuleId, Ex.what());
				throw;
			}
		},
		HttpVerb::kPost);

	m_Router.RegisterRoute(
		"modules/{moduleid}/hibernate",
		[this](HttpRouterRequest& Req) {
			std::string_view ModuleId = Req.GetCapture(1);

			try
			{
				Hub::Response Resp = m_Hub.Hibernate(std::string(ModuleId));

				if (HandleFailureResults(Req.ServerRequest(), Resp))
				{
					return;
				}

				const HttpResponseCode HttpCode =
					(Resp.ResponseCode == Hub::EResponseCode::Accepted) ? HttpResponseCode::Accepted : HttpResponseCode::OK;
				CbObjectWriter Obj;
				Obj << "moduleId" << ModuleId;
				return Req.ServerRequest().WriteResponse(HttpCode, Obj.Save());
			}
			catch (const std::exception& Ex)
			{
				ZEN_ERROR("Exception while hibernating module '{}': {}", ModuleId, Ex.what());
				throw;
			}
		},
		HttpVerb::kPost);

	m_Router.RegisterRoute(
		"modules/{moduleid}/wake",
		[this](HttpRouterRequest& Req) {
			std::string_view ModuleId = Req.GetCapture(1);

			try
			{
				Hub::Response Resp = m_Hub.Wake(std::string(ModuleId));

				if (HandleFailureResults(Req.ServerRequest(), Resp))
				{
					return;
				}

				const HttpResponseCode HttpCode =
					(Resp.ResponseCode == Hub::EResponseCode::Accepted) ? HttpResponseCode::Accepted : HttpResponseCode::OK;
				CbObjectWriter Obj;
				Obj << "moduleId" << ModuleId;
				return Req.ServerRequest().WriteResponse(HttpCode, Obj.Save());
			}
			catch (const std::exception& Ex)
			{
				ZEN_ERROR("Exception while waking module '{}': {}", ModuleId, Ex.what());
				throw;
			}
		},
		HttpVerb::kPost);

	m_Router.RegisterRoute(
		"stats",
		[this](HttpRouterRequest& Req) {
			CbObjectWriter Obj;
			Obj << "currentInstanceCount" << m_Hub.GetInstanceCount();
			Obj << "maxInstanceCount" << m_Hub.GetMaxInstanceCount();
			Obj << "instanceLimit" << m_Hub.GetConfig().InstanceLimit;
			Req.ServerRequest().WriteResponse(HttpResponseCode::OK, Obj.Save());
		},
		HttpVerb::kGet);
}

HttpHubService::~HttpHubService()
{
}

const char*
HttpHubService::BaseUri() const
{
	return "/hub/";
}

void
HttpHubService::SetNotificationEndpoint(std::string_view UpstreamNotificationEndpoint, std::string_view InstanceId)
{
	ZEN_UNUSED(UpstreamNotificationEndpoint, InstanceId);
	// TODO: store these for use in notifications, on some interval/criteria which is currently TBD
}

void
HttpHubService::HandleRequest(zen::HttpServerRequest& Request)
{
	m_Router.HandleRequest(Request);
}

void
HttpHubService::HandleModuleGet(HttpServerRequest& Request, std::string_view ModuleId)
{
	Hub::InstanceInfo InstanceInfo;
	if (!m_Hub.Find(ModuleId, &InstanceInfo))
	{
		Request.WriteResponse(HttpResponseCode::NotFound);
		return;
	}

	CbObjectWriter Obj;
	Obj << "moduleId" << ModuleId;
	Obj << "state" << ToString(InstanceInfo.State);
	Request.WriteResponse(HttpResponseCode::OK, Obj.Save());
}

void
HttpHubService::HandleModuleDelete(HttpServerRequest& Request, std::string_view ModuleId)
{
	Hub::InstanceInfo InstanceInfo;
	if (!m_Hub.Find(ModuleId, &InstanceInfo))
	{
		Request.WriteResponse(HttpResponseCode::NotFound);
		return;
	}

	if (InstanceInfo.State == HubInstanceState::Provisioned || InstanceInfo.State == HubInstanceState::Hibernated ||
		InstanceInfo.State == HubInstanceState::Crashed)
	{
		try
		{
			Hub::Response Resp = m_Hub.Deprovision(std::string(ModuleId));

			if (HandleFailureResults(Request, Resp))
			{
				return;
			}

			// TODO: nuke all related storage

			const HttpResponseCode HttpCode =
				(Resp.ResponseCode == Hub::EResponseCode::Accepted) ? HttpResponseCode::Accepted : HttpResponseCode::OK;
			CbObjectWriter Obj;
			Obj << "moduleId" << ModuleId;
			return Request.WriteResponse(HttpCode, Obj.Save());
		}
		catch (const std::exception& Ex)
		{
			ZEN_ERROR("Exception while deprovisioning module '{}': {}", ModuleId, Ex.what());
			throw;
		}
	}

	// TODO: nuke all related storage

	CbObjectWriter Obj;
	Obj << "moduleId" << ModuleId;
	Request.WriteResponse(HttpResponseCode::OK, Obj.Save());
}

}  // namespace zen