aboutsummaryrefslogtreecommitdiff
path: root/zenserver/upstream/upstreamapply.h
blob: 4a095be6ca1c845d44ab6a06f288fa8e13b54481 (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
// Copyright Epic Games, Inc. All Rights Reserved.

#pragma once

#if ZEN_WITH_COMPUTE_SERVICES

#	include <zencore/compactbinarypackage.h>
#	include <zencore/iobuffer.h>
#	include <zencore/iohash.h>
#	include <zencore/stats.h>
#	include <zencore/zencore.h>

#	include <chrono>
#	include <map>
#	include <unordered_map>
#	include <unordered_set>

namespace zen {

class AuthMgr;
class CbObjectWriter;
class CidStore;
class CloudCacheTokenProvider;
class WorkerThreadPool;
class ZenCacheNamespace;
struct CloudCacheClientOptions;
struct UpstreamAuthConfig;

enum class UpstreamApplyState : int32_t
{
	Queued	  = 0,
	Executing = 1,
	Complete  = 2,
};

enum class UpstreamApplyType
{
	Simple = 0,
	Asset  = 1,
};

struct UpstreamApplyRecord
{
	CbObject						WorkerDescriptor;
	CbObject						Action;
	UpstreamApplyType				Type;
	std::map<std::string, uint64_t> Timepoints{};
};

struct UpstreamApplyOptions
{
	std::chrono::seconds HealthCheckInterval{5};
	std::chrono::seconds UpdatesInterval{5};
	uint32_t			 UpstreamThreadCount   = 4;
	uint32_t			 DownstreamThreadCount = 4;
	bool				 StatsEnabled		   = false;
};

struct UpstreamApplyError
{
	int32_t		ErrorCode{};
	std::string Reason{};

	explicit operator bool() const { return ErrorCode != 0; }
};

struct PostUpstreamApplyResult
{
	UpstreamApplyError				Error{};
	int64_t							Bytes{};
	double							ElapsedSeconds{};
	std::map<std::string, uint64_t> Timepoints{};
	bool							Success = false;
};

struct GetUpstreamApplyResult
{
	// UpstreamApplyType::Simple
	std::map<std::filesystem::path, IoHash> OutputFiles{};
	std::map<IoHash, IoBuffer>				FileData{};

	// UpstreamApplyType::Asset
	CbPackage OutputPackage{};
	int64_t	  TotalAttachmentBytes{};
	int64_t	  TotalRawAttachmentBytes{};

	UpstreamApplyError				Error{};
	int64_t							Bytes{};
	double							ElapsedSeconds{};
	std::string						StdOut{};
	std::string						StdErr{};
	std::string						Agent{};
	std::string						Detail{};
	std::map<std::string, uint64_t> Timepoints{};
	bool							Success = false;
};

using UpstreamApplyCompleted = std::unordered_map<IoHash, std::unordered_map<IoHash, GetUpstreamApplyResult>>;

struct GetUpstreamApplyUpdatesResult
{
	UpstreamApplyError	   Error{};
	int64_t				   Bytes{};
	double				   ElapsedSeconds{};
	UpstreamApplyCompleted Completed{};
	bool				   Success = false;
};

struct UpstreamApplyStatus
{
	UpstreamApplyState					  State{};
	GetUpstreamApplyResult				  Result{};
	std::chrono::steady_clock::time_point ExpireTime{};
	std::map<std::string, uint64_t>		  Timepoints{};
};

using UpstreamApplyTasks = std::unordered_map<IoHash, std::unordered_map<IoHash, UpstreamApplyStatus>>;

struct UpstreamEndpointHealth
{
	std::string Reason;
	bool		Ok = false;
};

struct UpstreamApplyEndpointStats
{
	metrics::Counter PostCount;
	metrics::Counter CompleteCount;
	metrics::Counter UpdateCount;
	metrics::Counter ErrorCount;
	metrics::Counter UpBytes;
	metrics::Counter DownBytes;
};

/**
 * The upstream apply endpoint is responsible for handling remote execution.
 */
class UpstreamApplyEndpoint
{
public:
	virtual ~UpstreamApplyEndpoint() = default;

	virtual UpstreamEndpointHealth		  Initialize()								 = 0;
	virtual bool						  IsHealthy() const							 = 0;
	virtual UpstreamEndpointHealth		  CheckHealth()								 = 0;
	virtual std::string_view			  DisplayName() const						 = 0;
	virtual PostUpstreamApplyResult		  PostApply(UpstreamApplyRecord ApplyRecord) = 0;
	virtual GetUpstreamApplyUpdatesResult GetUpdates(WorkerThreadPool& ThreadPool)	 = 0;
	virtual UpstreamApplyEndpointStats&	  Stats()									 = 0;

	static std::unique_ptr<UpstreamApplyEndpoint> CreateHordeEndpoint(const CloudCacheClientOptions& ComputeOptions,
																	  const UpstreamAuthConfig&		 ComputeAuthConfig,
																	  const CloudCacheClientOptions& StorageOptions,
																	  const UpstreamAuthConfig&		 StorageAuthConfig,
																	  CidStore&						 CidStore,
																	  AuthMgr&						 Mgr);
};

/**
 * Manages one or more upstream compute endpoints.
 */
class UpstreamApply
{
public:
	virtual ~UpstreamApply() = default;

	virtual bool Initialize()													   = 0;
	virtual bool IsHealthy() const												   = 0;
	virtual void RegisterEndpoint(std::unique_ptr<UpstreamApplyEndpoint> Endpoint) = 0;

	struct EnqueueResult
	{
		IoHash ApplyId{};
		bool   Success = false;
	};

	struct StatusResult
	{
		UpstreamApplyStatus Status{};
		bool				Success = false;
	};

	virtual EnqueueResult EnqueueUpstream(UpstreamApplyRecord ApplyRecord)			= 0;
	virtual StatusResult  GetStatus(const IoHash& WorkerId, const IoHash& ActionId) = 0;
	virtual void		  GetStatus(CbObjectWriter& CbO)							= 0;

	static std::unique_ptr<UpstreamApply> Create(const UpstreamApplyOptions& Options, CidStore& CidStore);
};

}  // namespace zen

#endif	// ZEN_WITH_COMPUTE_SERVICES