Skip to content

Commit 24b3d1a

Browse files
author
Jérémy James Toussaint
committed
Refactored compute responses
1 parent 49f7d9b commit 24b3d1a

22 files changed

+424
-322
lines changed

src/main/java/com/iexec/worker/compute/ComputeManagerService.java

Lines changed: 55 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,13 @@
2424
import com.iexec.common.utils.IexecFileHelper;
2525
import com.iexec.common.worker.result.ResultUtils;
2626
import com.iexec.worker.chain.IexecHubService;
27+
import com.iexec.worker.compute.app.AppComputeResponse;
28+
import com.iexec.worker.compute.app.AppComputeService;
29+
import com.iexec.worker.compute.post.PostComputeResponse;
30+
import com.iexec.worker.compute.post.PostComputeService;
31+
import com.iexec.worker.compute.pre.PreComputeResponse;
32+
import com.iexec.worker.compute.pre.PreComputeService;
2733
import com.iexec.worker.config.WorkerConfigurationService;
28-
import com.iexec.worker.docker.DockerContainerLogs;
29-
import com.iexec.worker.docker.DockerRunResponse;
3034
import com.iexec.worker.docker.DockerService;
3135
import lombok.extern.slf4j.Slf4j;
3236
import org.springframework.stereotype.Service;
@@ -41,24 +45,24 @@ public class ComputeManagerService {
4145
private static final String STDOUT_FILENAME = "stdout.txt";
4246

4347
private final DockerService dockerService;
44-
private final PreComputeStepService preComputeStepService;
45-
private final ComputeStepService computeStepService;
46-
private final PostComputeStepService postComputeStepService;
48+
private final PreComputeService preComputeService;
49+
private final AppComputeService appComputeService;
50+
private final PostComputeService postComputeService;
4751
private final WorkerConfigurationService workerConfigService;
4852
private final IexecHubService iexecHubService;
4953

5054
public ComputeManagerService(
5155
DockerService dockerService,
52-
PreComputeStepService preComputeStepService,
53-
ComputeStepService computeStepService,
54-
PostComputeStepService postComputeStepService,
56+
PreComputeService preComputeService,
57+
AppComputeService appComputeService,
58+
PostComputeService postComputeService,
5559
WorkerConfigurationService workerConfigService,
5660
IexecHubService iexecHubService
5761
) {
5862
this.dockerService = dockerService;
59-
this.preComputeStepService = preComputeStepService;
60-
this.computeStepService = computeStepService;
61-
this.postComputeStepService = postComputeStepService;
63+
this.preComputeService = preComputeService;
64+
this.appComputeService = appComputeService;
65+
this.postComputeService = postComputeService;
6266
this.workerConfigService = workerConfigService;
6367
this.iexecHubService = iexecHubService;
6468
}
@@ -83,53 +87,54 @@ public boolean isAppDownloaded(String imageUri) {
8387
* non TEE: download secrets && decrypt dataset (TODO: rewritte or remove)
8488
* TEE: download post-compute image && create secure session
8589
*/
86-
public ComputeResponsesHolder runPreCompute(ComputeResponsesHolder computeResponsesHolder,
87-
TaskDescription taskDescription,
88-
WorkerpoolAuthorization workerpoolAuth) {
90+
public PreComputeResponse runPreCompute(TaskDescription taskDescription,
91+
WorkerpoolAuthorization workerpoolAuth) {
8992
log.info("Running pre-compute [chainTaskId:{}, isTee:{}]",
9093
taskDescription.getChainTaskId(),
9194
taskDescription.isTeeTask());
92-
boolean isSuccessful = false;
9395

9496
if (taskDescription.isTeeTask()) {
9597
String secureSessionId =
96-
preComputeStepService.runTeePreCompute(taskDescription,
98+
preComputeService.runTeePreCompute(taskDescription,
9799
workerpoolAuth);
98-
if (!secureSessionId.isEmpty()) {
99-
computeResponsesHolder.setSecureSessionId(secureSessionId);
100-
isSuccessful = true;
101-
}
102-
} else {
103-
isSuccessful =
104-
preComputeStepService.runStandardPreCompute(taskDescription, workerpoolAuth);
100+
return PreComputeResponse.builder()
101+
.isTeeTask(true)
102+
.secureSessionId(secureSessionId)
103+
.build();
105104
}
106-
computeResponsesHolder.setPreComputeDockerRunResponse(
107-
DockerRunResponse.builder().isSuccessful(isSuccessful).build());
108-
return computeResponsesHolder;
105+
106+
return PreComputeResponse.builder()
107+
.isSuccessful(
108+
preComputeService.runStandardPreCompute(taskDescription,
109+
workerpoolAuth))
110+
.build();
109111
}
110112

111-
public ComputeResponsesHolder runCompute(ComputeResponsesHolder computeResponsesHolder,
112-
TaskDescription taskDescription) {
113-
String chainTaskId = computeResponsesHolder.getChainTaskId();
113+
public AppComputeResponse runCompute(TaskDescription taskDescription,
114+
String secureSessionId) {
115+
String chainTaskId = taskDescription.getChainTaskId();
114116
log.info("Running compute [chainTaskId:{}, isTee:{}]", chainTaskId,
115117
taskDescription.isTeeTask());
116118

117-
DockerRunResponse dockerRunResponse =
118-
computeStepService.runCompute(taskDescription,
119-
computeResponsesHolder.getSecureSessionId());
119+
ComputeResponse computeResponse =
120+
appComputeService.runCompute(taskDescription, secureSessionId);
120121

121-
if (dockerRunResponse.isSuccessful() && dockerRunResponse.getDockerContainerLogs() != null) {
122+
if (computeResponse.isSuccessful() && !computeResponse.getStdout().isEmpty()) {
122123
// save /output/stdout.txt file
123124
String stdoutFilePath =
124125
workerConfigService.getTaskIexecOutDir(chainTaskId) + File.separator + STDOUT_FILENAME;
125126
File stdoutFile = FileHelper.createFileWithContent(stdoutFilePath
126-
, dockerRunResponse.getDockerContainerLogs().getStdout());
127+
, computeResponse.getStdout());
127128
log.info("Saved stdout file [path:{}]",
128129
stdoutFile.getAbsolutePath());
129130
//TODO Make sure stdout is properly written
130131
}
131-
computeResponsesHolder.setComputeDockerRunResponse(dockerRunResponse);
132-
return computeResponsesHolder;
132+
133+
return AppComputeResponse.builder()
134+
.isSuccessful(computeResponse.isSuccessful())
135+
.stdout(computeResponse.getStdout())
136+
.stderr(computeResponse.getStderr())
137+
.build();
133138
}
134139

135140
/*
@@ -140,34 +145,27 @@ public ComputeResponsesHolder runCompute(ComputeResponsesHolder computeResponses
140145
*
141146
* - Save stdout file
142147
*/
143-
public ComputeResponsesHolder runPostCompute(ComputeResponsesHolder computeResponsesHolder,
144-
TaskDescription taskDescription) {
148+
public PostComputeResponse runPostCompute(TaskDescription taskDescription,
149+
String secureSessionId) {
145150
String chainTaskId = taskDescription.getChainTaskId();
146151
log.info("Running post-compute [chainTaskId:{}, isTee:{}]",
147152
chainTaskId, taskDescription.isTeeTask());
148-
DockerRunResponse dockerRunResponse =
149-
DockerRunResponse.builder().isSuccessful(false).build();
150153

151154
if (taskDescription.isTeeTask()) {
152-
dockerRunResponse =
153-
postComputeStepService.runTeePostCompute(taskDescription,
154-
computeResponsesHolder.getSecureSessionId());
155-
} else {
156-
// TODO Use container
157-
if (postComputeStepService.runStandardPostCompute(taskDescription)) {
158-
dockerRunResponse =
159-
DockerRunResponse.builder().isSuccessful(true).build();
160-
}
161-
}
162-
163-
if (dockerRunResponse != null &&
164-
dockerRunResponse.getDockerContainerLogs() == null) {
165-
dockerRunResponse.setDockerContainerLogs(
166-
DockerContainerLogs.builder().stdout("").build());
155+
ComputeResponse computeResponse =
156+
postComputeService.runTeePostCompute(taskDescription,
157+
secureSessionId);
158+
return PostComputeResponse.builder()
159+
.isSuccessful(computeResponse.isSuccessful())
160+
.stdout(computeResponse.getStdout())
161+
.stderr(computeResponse.getStderr())
162+
.build();
167163
}
168164

169-
computeResponsesHolder.setPostComputeDockerRunResponse(dockerRunResponse);
170-
return computeResponsesHolder;
165+
// TODO Use container
166+
return PostComputeResponse.builder()
167+
.isSuccessful(postComputeService.runStandardPostCompute(taskDescription))
168+
.build();
171169
}
172170

173171

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
/*
2+
* Copyright 2020 IEXEC BLOCKCHAIN TECH
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package com.iexec.worker.compute;
18+
19+
public interface ComputeResponse {
20+
21+
boolean isSuccessful();
22+
String getStdout();
23+
String getStderr();
24+
25+
}

src/main/java/com/iexec/worker/compute/ComputeResponsesHolder.java

Lines changed: 0 additions & 70 deletions
This file was deleted.
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
/*
2+
* Copyright 2020 IEXEC BLOCKCHAIN TECH
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package com.iexec.worker.compute.app;
18+
19+
import com.iexec.worker.compute.ComputeResponse;
20+
import lombok.*;
21+
22+
@Data
23+
@Builder
24+
@Getter
25+
@NoArgsConstructor
26+
@AllArgsConstructor
27+
public class AppComputeResponse implements ComputeResponse {
28+
29+
private boolean isSuccessful;
30+
private String stdout;
31+
private String stderr;
32+
33+
}

src/main/java/com/iexec/worker/compute/ComputeStepService.java renamed to src/main/java/com/iexec/worker/compute/app/AppComputeService.java

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,15 +14,15 @@
1414
* limitations under the License.
1515
*/
1616

17-
package com.iexec.worker.compute;
17+
package com.iexec.worker.compute.app;
1818

1919
import com.iexec.common.task.TaskDescription;
2020
import com.iexec.common.utils.EnvUtils;
2121
import com.iexec.common.utils.FileHelper;
22+
import com.iexec.worker.compute.ComputeResponse;
2223
import com.iexec.worker.config.PublicConfigurationService;
2324
import com.iexec.worker.config.WorkerConfigurationService;
2425
import com.iexec.worker.docker.DockerRunRequest;
25-
import com.iexec.worker.docker.DockerRunResponse;
2626
import com.iexec.worker.docker.DockerService;
2727
import com.iexec.worker.tee.scone.SconeTeeService;
2828
import lombok.extern.slf4j.Slf4j;
@@ -34,14 +34,14 @@
3434

3535
@Slf4j
3636
@Service
37-
public class ComputeStepService {
37+
public class AppComputeService {
3838

3939
private final WorkerConfigurationService workerConfigService;
4040
private final DockerService dockerService;
4141
private final PublicConfigurationService publicConfigService;
4242
private final SconeTeeService sconeTeeService;
4343

44-
public ComputeStepService(
44+
public AppComputeService(
4545
WorkerConfigurationService workerConfigService,
4646
PublicConfigurationService publicConfigService,
4747
DockerService dockerService,
@@ -53,7 +53,8 @@ public ComputeStepService(
5353
this.sconeTeeService = sconeTeeService;
5454
}
5555

56-
public DockerRunResponse runCompute(TaskDescription taskDescription, String secureSessionId) {
56+
public ComputeResponse runCompute(TaskDescription taskDescription,
57+
String secureSessionId) {
5758
String chainTaskId = taskDescription.getChainTaskId();
5859
List<String> env = EnvUtils.getContainerEnvList(taskDescription);
5960
if (taskDescription.isTeeTask()) {

0 commit comments

Comments
 (0)