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

#pragma once

#include "compute/apply.h"

#if ZEN_WITH_COMPUTE_SERVICES

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

#	include <atomic>
#	include <chrono>
#	include <memory>
#	include <unordered_map>
#	include <unordered_set>

namespace zen {

class CbObjectWriter;
class CasStore;
class CidStore;
class ZenCacheStore;
struct CloudCacheClientOptions;
class CloudCacheTokenProvider;
struct UpstreamAuthConfig;
class AuthMgr;

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

struct UpstreamApplyRecord
{
	CbObject WorkerDescriptor;
	CbObject Action;
};

struct UpstreamApplyOptions
{
	std::chrono::seconds HealthCheckInterval{5};
	std::chrono::seconds UpdatesInterval{5};
	uint32_t			 ThreadCount  = 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{};
	bool			   Success = false;
};

struct GetUpstreamApplyResult
{
	CbPackage		   OutputPackage{};
	int64_t			   TotalAttachmentBytes{};
	int64_t			   TotalRawAttachmentBytes{};
	UpstreamApplyError Error{};
	int64_t			   Bytes{};
	double			   ElapsedSeconds{};
	std::string		   StdOut{};
	std::string		   StdErr{};
	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{};
};

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

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

struct UpstreamApplyEndpointStats
{
	std::atomic_uint64_t PostCount{};
	std::atomic_uint64_t CompleteCount{};
	std::atomic_uint64_t UpdateCount{};
	std::atomic_uint64_t ErrorCount{};
	std::atomic<double>	 UpBytes{};
	std::atomic<double>	 DownBytes{};
	std::atomic<double>	 SecondsUp{};
	std::atomic<double>	 SecondsDown{};
};

/**
 * 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(const UpstreamApplyRecord& ApplyRecord) = 0;
	virtual GetUpstreamApplyUpdatesResult GetUpdates()										= 0;
	virtual UpstreamApplyEndpointStats&	  Stats()											= 0;

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

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

	virtual bool Initialize()													   = 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, CasStore& CasStore, CidStore& CidStore);
};

}  // namespace zen

#endif	// ZEN_WITH_COMPUTE_SERVICES