This repository was archived by the owner on Apr 27, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 27
Expand file tree
/
Copy pathFlyteSandboxClient.java
More file actions
205 lines (176 loc) · 6.65 KB
/
FlyteSandboxClient.java
File metadata and controls
205 lines (176 loc) · 6.65 KB
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
/*
* Copyright 2021-2022 Flyte Authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package org.flyte.utils;
import static org.flyte.examples.FlyteEnvironment.DEVELOPMENT_DOMAIN;
import static org.flyte.examples.FlyteEnvironment.PROJECT;
import flyteidl.admin.ExecutionOuterClass;
import flyteidl.core.Execution;
import flyteidl.core.IdentifierOuterClass;
import flyteidl.core.IdentifierOuterClass.ResourceType;
import flyteidl.core.Literals;
import flyteidl.core.Literals.LiteralMap;
import flyteidl.service.AdminServiceGrpc;
import io.grpc.ManagedChannel;
import io.grpc.ManagedChannelBuilder;
import java.util.Objects;
import java.util.concurrent.TimeUnit;
import org.rnorth.ducttape.unreliables.Unreliables;
public class FlyteSandboxClient {
private static final int EXECUTION_TIMEOUT_SECONDS = 300;
private final String version;
private final AdminServiceGrpc.AdminServiceBlockingStub stub;
FlyteSandboxClient(String version, AdminServiceGrpc.AdminServiceBlockingStub stub) {
this.version = version;
this.stub = stub;
}
public static FlyteSandboxClient create() {
String version = String.valueOf(System.currentTimeMillis());
String address = FlyteSandboxContainer.INSTANCE.getHost();
int port = FlyteSandboxContainer.INSTANCE.getMappedPort(30080);
ManagedChannel channel =
ManagedChannelBuilder.forTarget(address + ":" + port).usePlaintext().enableRetry().build();
AdminServiceGrpc.AdminServiceBlockingStub stub = AdminServiceGrpc.newBlockingStub(channel);
return new FlyteSandboxClient(version, stub);
}
public Literals.LiteralMap createExecution(String name, String domain) {
return createExecution(
IdentifierOuterClass.Identifier.newBuilder()
.setResourceType(ResourceType.LAUNCH_PLAN)
.setDomain(domain)
.setProject(PROJECT)
.setName(name)
.setVersion(version)
.build(),
LiteralMap.getDefaultInstance(),
domain);
}
public Literals.LiteralMap createTaskExecution(String name, Literals.LiteralMap inputs) {
return createExecution(
IdentifierOuterClass.Identifier.newBuilder()
.setResourceType(IdentifierOuterClass.ResourceType.TASK)
.setDomain(DEVELOPMENT_DOMAIN)
.setProject(PROJECT)
.setName(name)
.setVersion(version)
.build(),
inputs,
DEVELOPMENT_DOMAIN);
}
public Literals.LiteralMap createExecution(String name, Literals.LiteralMap inputs) {
return createExecution(
IdentifierOuterClass.Identifier.newBuilder()
.setResourceType(IdentifierOuterClass.ResourceType.LAUNCH_PLAN)
.setDomain(DEVELOPMENT_DOMAIN)
.setProject(PROJECT)
.setName(name)
.setVersion(version)
.build(),
inputs,
DEVELOPMENT_DOMAIN);
}
private Literals.LiteralMap createExecution(
IdentifierOuterClass.Identifier id, Literals.LiteralMap inputs, String domain) {
ExecutionOuterClass.ExecutionCreateResponse response =
stub.createExecution(
ExecutionOuterClass.ExecutionCreateRequest.newBuilder()
.setDomain(domain)
.setProject(PROJECT)
.setInputs(inputs)
.setSpec(ExecutionOuterClass.ExecutionSpec.newBuilder().setLaunchPlan(id).build())
.build());
IdentifierOuterClass.WorkflowExecutionIdentifier executionId = response.getId();
Unreliables.retryUntilTrue(
EXECUTION_TIMEOUT_SECONDS,
TimeUnit.SECONDS,
() -> {
ExecutionOuterClass.Execution execution =
stub.getExecution(
ExecutionOuterClass.WorkflowExecutionGetRequest.newBuilder()
.setId(executionId)
.build());
return !isRunning(execution.getClosure().getPhase());
});
ExecutionOuterClass.Execution execution =
stub.getExecution(
ExecutionOuterClass.WorkflowExecutionGetRequest.newBuilder()
.setId(executionId)
.build());
if (execution.getClosure().getPhase() != Execution.WorkflowExecution.Phase.SUCCEEDED) {
throw new RuntimeException(
"Workflow didn't succeed. [Phase="
+ execution.getClosure().getPhase()
+ "] [Error: "
+ execution.getClosure().getError().getMessage());
}
ExecutionOuterClass.WorkflowExecutionGetDataResponse executionData =
stub.getExecutionData(
ExecutionOuterClass.WorkflowExecutionGetDataRequest.newBuilder()
.setId(executionId)
.build());
return executionData.getFullOutputs();
}
private boolean isRunning(Execution.WorkflowExecution.Phase phase) {
switch (phase) {
case SUCCEEDING:
case QUEUED:
case RUNNING:
case UNDEFINED:
return true;
case TIMED_OUT:
case SUCCEEDED:
case ABORTED:
case ABORTING:
case FAILED:
case FAILING:
case UNRECOGNIZED:
return false;
}
return false;
}
public void registerWorkflows(String classpath, String domain) {
try {
jflyte(
"jflyte",
"register",
"workflows",
"-p=" + PROJECT,
"-d=" + domain,
"-v=" + version,
"-cp=" + classpath);
} catch (Exception e) {
throw new RuntimeException("Could not register workflows from: " + classpath, e);
}
}
public void registerWorkflows(String classpath) {
registerWorkflows(classpath, DEVELOPMENT_DOMAIN);
}
public void serializeWorkflows(String classpath, String folder) {
jflyte("jflyte", "serialize", "workflows", "-cp=" + classpath, "-f=" + folder);
}
private void jflyte(String... cmd) {
JFlyteContainer jflyte = new JFlyteContainer(cmd);
jflyte.start();
Long exitCode = jflyte.getCurrentContainerInfo().getState().getExitCodeLong();
if (!Objects.equals(exitCode, 0L)) {
throw new RuntimeException(
"Container terminated with non-zero exit code: "
+ exitCode
+ " error: "
+ jflyte.getCurrentContainerInfo().getState().getError());
}
}
}