blob: cd26be4120cd7d9d2656375097fb1f8d1add213e (
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
|
// Copyright Epic Games, Inc. All Rights Reserved.
#pragma once
#include <zencompute/functionservice.h>
#if ZEN_WITH_COMPUTE_SERVICES
# include <filesystem>
# include <vector>
namespace zen::compute {
struct SubmitResult
{
bool IsAccepted = false;
std::string Reason;
};
/** Base interface for classes implementing a remote execution "runner"
*/
class FunctionRunner : public RefCounted
{
FunctionRunner(FunctionRunner&&) = delete;
FunctionRunner& operator=(FunctionRunner&&) = delete;
public:
FunctionRunner(std::filesystem::path BasePath);
virtual ~FunctionRunner() = 0;
virtual void Shutdown() = 0;
virtual void RegisterWorker(const CbPackage& WorkerPackage) = 0;
[[nodiscard]] virtual SubmitResult SubmitAction(Ref<RunnerAction> Action) = 0;
[[nodiscard]] virtual size_t GetSubmittedActionCount() = 0;
[[nodiscard]] virtual bool IsHealthy() = 0;
[[nodiscard]] virtual size_t QueryCapacity();
[[nodiscard]] virtual std::vector<SubmitResult> SubmitActions(const std::vector<Ref<RunnerAction>>& Actions);
protected:
std::filesystem::path m_ActionsPath;
bool m_DumpActions = false;
void MaybeDumpAction(int ActionLsn, const CbObject& ActionObject);
};
/** Base class for RunnerGroup that operates on generic FunctionRunner references.
* All scheduling, capacity, and lifecycle logic lives here.
*/
class BaseRunnerGroup
{
public:
size_t QueryCapacity();
SubmitResult SubmitAction(Ref<RunnerAction> Action);
std::vector<SubmitResult> SubmitActions(const std::vector<Ref<RunnerAction>>& Actions);
size_t GetSubmittedActionCount();
void RegisterWorker(CbPackage Worker);
void Shutdown();
size_t GetRunnerCount()
{
return m_RunnersLock.WithSharedLock([this] { return m_Runners.size(); });
}
protected:
void AddRunnerInternal(FunctionRunner* Runner);
RwLock m_RunnersLock;
std::vector<Ref<FunctionRunner>> m_Runners;
std::atomic<int> m_NextSubmitIndex{0};
};
/** Typed RunnerGroup that adds type-safe runner addition and predicate-based removal.
*/
template<typename RunnerType>
struct RunnerGroup : public BaseRunnerGroup
{
void AddRunner(RunnerType* Runner) { AddRunnerInternal(Runner); }
template<typename Predicate>
size_t RemoveRunnerIf(Predicate&& Pred)
{
size_t RemovedCount = 0;
m_RunnersLock.WithExclusiveLock([&] {
auto It = m_Runners.begin();
while (It != m_Runners.end())
{
if (Pred(static_cast<RunnerType&>(**It)))
{
(*It)->Shutdown();
It = m_Runners.erase(It);
++RemovedCount;
}
else
{
++It;
}
}
});
return RemovedCount;
}
};
/**
* This represents an action going through different stages of scheduling and execution.
*/
struct RunnerAction : public RefCounted
{
explicit RunnerAction(FunctionServiceSession* OwnerSession);
~RunnerAction();
int ActionLsn = 0;
WorkerDesc Worker;
IoHash ActionId;
CbObject ActionObj;
int Priority = 0;
enum class State
{
New,
Pending,
Submitting,
Running,
Completed,
Failed,
_Count
};
static const char* ToString(State _)
{
switch (_)
{
case State::New:
return "New";
case State::Pending:
return "Pending";
case State::Submitting:
return "Submitting";
case State::Running:
return "Running";
case State::Completed:
return "Completed";
case State::Failed:
return "Failed";
default:
return "Unknown";
}
}
uint64_t Timestamps[static_cast<int>(State::_Count)] = {};
State ActionState() const { return m_ActionState; }
void SetActionState(State NewState);
bool IsSuccess() const { return ActionState() == State::Completed; }
bool IsCompleted() const { return ActionState() == State::Completed || ActionState() == State::Failed; }
void SetResult(CbPackage&& Result);
CbPackage& GetResult();
private:
std::atomic<State> m_ActionState = State::New;
FunctionServiceSession* m_OwnerSession = nullptr;
CbPackage m_Result;
};
} // namespace zen::compute
#endif // ZEN_WITH_COMPUTE_SERVICES
|