Skip to content

Commit df5a2a3

Browse files
author
Jérémy James Toussaint
committed
Using single Docker instance
1 parent bd1b65c commit df5a2a3

File tree

9 files changed

+144
-144
lines changed

9 files changed

+144
-144
lines changed

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

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -105,8 +105,7 @@ public PreComputeResponse runPreCompute(TaskDescription taskDescription,
105105

106106
return PreComputeResponse.builder()
107107
.isSuccessful(
108-
preComputeService.runStandardPreCompute(taskDescription,
109-
workerpoolAuth))
108+
preComputeService.runStandardPreCompute(taskDescription))
110109
.build();
111110
}
112111

src/main/java/com/iexec/worker/compute/pre/PreComputeService.java

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -50,19 +50,8 @@ public PreComputeService(
5050
this.dockerService = dockerService;
5151
}
5252

53-
public boolean runStandardPreCompute(TaskDescription taskDescription, WorkerpoolAuthorization workerpoolAuth) {
53+
public boolean runStandardPreCompute(TaskDescription taskDescription) {
5454
String chainTaskId = taskDescription.getChainTaskId();
55-
// Why do we need smsService for standard compute??
56-
Optional<TaskSecrets> oTaskSecrets = smsService.fetchTaskSecrets(workerpoolAuth);
57-
if (oTaskSecrets.isEmpty()) {
58-
log.warn("No secrets fetched for this task, will continue [chainTaskId:{}]:", chainTaskId);
59-
} else {
60-
String datasetSecretFilePath = workerConfigService.getDatasetSecretFilePath(chainTaskId);
61-
String beneficiarySecretFilePath = workerConfigService.getBeneficiarySecretFilePath(chainTaskId);
62-
String enclaveSecretFilePath = workerConfigService.getEnclaveSecretFilePath(chainTaskId);
63-
smsService.saveSecrets(chainTaskId, oTaskSecrets.get(), datasetSecretFilePath,
64-
beneficiarySecretFilePath, enclaveSecretFilePath);
65-
}
6655
boolean isDatasetDecryptionNeeded = dataService.isDatasetDecryptionNeeded(chainTaskId);
6756
boolean isDatasetDecrypted = false;
6857
if (isDatasetDecryptionNeeded) {
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
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.docker;
18+
19+
import com.github.dockerjava.api.DockerClient;
20+
import com.github.dockerjava.core.DefaultDockerClientConfig;
21+
import com.github.dockerjava.core.DockerClientConfig;
22+
import com.github.dockerjava.core.DockerClientImpl;
23+
import com.github.dockerjava.httpclient5.ApacheDockerHttpClient;
24+
import com.github.dockerjava.transport.DockerHttpClient;
25+
26+
class Docker {
27+
28+
private static DockerClient dockerClient;
29+
30+
private Docker() {
31+
}
32+
33+
public static DockerClient getClient() {
34+
if (dockerClient == null) {
35+
DockerClientConfig config =
36+
DefaultDockerClientConfig.createDefaultConfigBuilder()
37+
.withDockerTlsVerify(false)
38+
.build();
39+
DockerHttpClient httpClient = new ApacheDockerHttpClient.Builder()
40+
.dockerHost(config.getDockerHost())
41+
.sslConfig(config.getSSLConfig())
42+
.build();
43+
dockerClient = DockerClientImpl.getInstance(config, httpClient);
44+
}
45+
return dockerClient;
46+
}
47+
48+
}

src/main/java/com/iexec/worker/docker/DockerClientService.java

Lines changed: 62 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -16,17 +16,11 @@
1616

1717
package com.iexec.worker.docker;
1818

19-
import com.github.dockerjava.api.DockerClient;
2019
import com.github.dockerjava.api.command.CreateContainerCmd;
2120
import com.github.dockerjava.api.command.PullImageResultCallback;
2221
import com.github.dockerjava.api.model.*;
23-
import com.github.dockerjava.core.DefaultDockerClientConfig;
24-
import com.github.dockerjava.core.DockerClientConfig;
25-
import com.github.dockerjava.core.DockerClientImpl;
2622
import com.github.dockerjava.core.NameParser;
2723
import com.github.dockerjava.core.command.ExecStartResultCallback;
28-
import com.github.dockerjava.httpclient5.ApacheDockerHttpClient;
29-
import com.github.dockerjava.transport.DockerHttpClient;
3024
import com.iexec.common.utils.ArgsUtils;
3125
import com.iexec.common.utils.WaitUtils;
3226
import com.iexec.worker.sgx.SgxService;
@@ -46,57 +40,53 @@ class DockerClientService {
4640
public DockerClientService() {
4741
String networkId = createNetwork(WORKER_DOCKER_NETWORK);
4842
if (!networkId.isEmpty()) {
49-
log.info("Created network [networkName:{}, networkId:{}]", WORKER_DOCKER_NETWORK, networkId);
43+
log.info("Created network [networkName:{}, networkId:{}]",
44+
WORKER_DOCKER_NETWORK, networkId);
5045
}
5146
}
5247

53-
private static DockerClient getDockerClient() {
54-
DockerClientConfig config = DefaultDockerClientConfig.createDefaultConfigBuilder().withDockerTlsVerify(false).build();
55-
DockerHttpClient httpClient = new ApacheDockerHttpClient.Builder()
56-
.dockerHost(config.getDockerHost())
57-
.sslConfig(config.getSSLConfig())
58-
.build();
59-
return DockerClientImpl.getInstance(config, httpClient);
60-
}
61-
6248
// network
6349

6450
String createNetwork(String networkName) {
6551
if (!getNetworkId(networkName).isEmpty()) {
6652
return "";
6753
}
6854
try {
69-
return getDockerClient().createNetworkCmd()
55+
return Docker.getClient().createNetworkCmd()
7056
.withName(networkName)
7157
.withDriver("bridge")
7258
.exec().getId();
7359
} catch (Exception e) {
74-
log.error("Failed to create network [networkName:{}, exception:{}]", networkName, e.getMessage());
60+
log.error("Failed to create network [networkName:{}, " +
61+
"exception:{}]", networkName, e.getMessage());
7562
e.printStackTrace();
7663
}
7764
return "";
7865
}
7966

8067
String getNetworkId(String networkName) {
8168
try {
82-
for (Network network : getDockerClient().listNetworksCmd().withNameFilter(networkName).exec()) {
69+
for (Network network :
70+
Docker.getClient().listNetworksCmd().withNameFilter(networkName).exec()) {
8371
if (network.getName().equals(networkName)) {
8472
return network.getId();
8573
}
8674
}
8775
} catch (Exception e) {
88-
log.error("Failed to check if network is created [networkName:{}, exception:{}]", networkName, e.getMessage());
76+
log.error("Failed to check if network is created [networkName:{}," +
77+
" exception:{}]", networkName, e.getMessage());
8978
e.printStackTrace();
9079
}
9180
return "";
9281
}
9382

9483
boolean removeNetwork(String networkId) {
9584
try {
96-
getDockerClient().removeNetworkCmd(networkId).exec();
85+
Docker.getClient().removeNetworkCmd(networkId).exec();
9786
return true;
9887
} catch (Exception e) {
99-
log.error("Failed to create network [networkId:{}, exception:{}]", networkId, e.getMessage());
88+
log.error("Failed to create network [networkId:{}, exception:{}]"
89+
, networkId, e.getMessage());
10090
e.printStackTrace();
10191
}
10292
return false;
@@ -105,20 +95,22 @@ boolean removeNetwork(String networkId) {
10595
// image
10696

10797
public boolean pullImage(String imageName) {
108-
NameParser.ReposTag repoAndTag = NameParser.parseRepositoryTag(imageName);
98+
NameParser.ReposTag repoAndTag =
99+
NameParser.parseRepositoryTag(imageName);
109100
if (repoAndTag.repos == null || repoAndTag.tag == null) {
110101
return false;
111102
}
112103

113104
try {
114-
getDockerClient().pullImageCmd(repoAndTag.repos)
105+
Docker.getClient().pullImageCmd(repoAndTag.repos)
115106
.withTag(repoAndTag.tag)
116107
.exec(new PullImageResultCallback() {
117108
})
118109
.awaitCompletion(1, TimeUnit.MINUTES);
119110
return true;
120111
} catch (Exception e) {
121-
log.error("Failed to pull image (imageName parsing) [imageName:{}, exception:{}]",
112+
log.error("Failed to pull image (imageName parsing) " +
113+
"[imageName:{}, exception:{}]",
122114
imageName, e.getMessage());
123115
e.printStackTrace();
124116
}
@@ -127,7 +119,8 @@ public boolean pullImage(String imageName) {
127119

128120
public String getImageId(String imageName) {
129121
try {
130-
List<Image> images = getDockerClient().listImagesCmd().withDanglingFilter(false)
122+
List<Image> images =
123+
Docker.getClient().listImagesCmd().withDanglingFilter(false)
131124
.withImageNameFilter(imageName).exec();
132125
for (Image image : images) {
133126
if (image == null || image.getRepoTags() == null) {
@@ -139,7 +132,7 @@ public String getImageId(String imageName) {
139132
}
140133
}
141134
} catch (Exception e) {
142-
log.error("Failed to check image pull [imageName:{}]", imageName);
135+
log.error("Failed to get image id [imageName:{}]", imageName);
143136
e.printStackTrace();
144137
}
145138
return "";
@@ -160,7 +153,8 @@ public String createContainer(DockerRunRequest dockerRunRequest) {
160153
String oldContainerId = getContainerId(containerName);
161154

162155
if (!oldContainerId.isEmpty()) {
163-
log.info("Found duplicate container with the same name, will remove it [containerName:{}, containerId:{}]",
156+
log.info("Found duplicate container with the same name, will " +
157+
"remove it [containerName:{}, containerId:{}]",
164158
containerName, oldContainerId);
165159
stopContainer(oldContainerId);
166160
removeContainer(oldContainerId);
@@ -177,15 +171,19 @@ public String createContainer(DockerRunRequest dockerRunRequest) {
177171

178172
if (dockerRunRequest.isSgx()) {
179173
hostConfig
180-
//.withDeviceCgroupRules(Arrays.asList("r", "w", "m")) //SgxService.SGX_CGROUP_PERMISSIONS) <--- why do we need this?
181-
.withDevices(Device.parse(SgxService.SGX_DEVICE_PATH + ":" + SgxService.SGX_DEVICE_PATH));
174+
//.withDeviceCgroupRules(Arrays.asList("r", "w", "m"))
175+
// SgxService.SGX_CGROUP_PERMISSIONS) <--- why do we need
176+
// this?
177+
.withDevices(Device.parse(SgxService.SGX_DEVICE_PATH +
178+
":" + SgxService.SGX_DEVICE_PATH));
182179
}
183180

184181
if (dockerRunRequest.getImageUri() == null || dockerRunRequest.getImageUri().isEmpty()) {
185182
return "";
186183
}
187184

188-
CreateContainerCmd createContainerCmd = getDockerClient().createContainerCmd(dockerRunRequest.getImageUri())
185+
CreateContainerCmd createContainerCmd =
186+
Docker.getClient().createContainerCmd(dockerRunRequest.getImageUri())
189187
.withName(containerName)
190188
.withHostConfig(hostConfig);
191189

@@ -204,7 +202,8 @@ public String createContainer(DockerRunRequest dockerRunRequest) {
204202
try {
205203
return createContainerCmd.exec().getId();
206204
} catch (Exception e) {
207-
log.error("Failed to create container [containerName:{}, exception:{}]",
205+
log.error("Failed to create container [containerName:{}, " +
206+
"exception:{}]",
208207
containerName, e.toString());
209208
e.printStackTrace();
210209
}
@@ -213,7 +212,7 @@ public String createContainer(DockerRunRequest dockerRunRequest) {
213212

214213
String getContainerId(String containerName) {
215214
try {
216-
return getDockerClient().listContainersCmd()
215+
return Docker.getClient().listContainersCmd()
217216
.withShowAll(true)
218217
.withNameFilter(Collections.singleton(containerName))
219218
.exec()
@@ -222,7 +221,8 @@ String getContainerId(String containerName) {
222221
.map(Container::getId)
223222
.orElse("");
224223
} catch (Exception e) {
225-
log.error("Failed to get docker container id [containerName:{}, exception:{}]",
224+
log.error("Failed to get docker container id [containerName:{}, " +
225+
"exception:{}]",
226226
containerName, e.getMessage());
227227
e.printStackTrace();
228228
return "";
@@ -234,9 +234,10 @@ public String getContainerStatus(String containerId) {
234234
return "";
235235
}
236236
try {
237-
return getDockerClient().inspectContainerCmd(containerId).exec().getState().getStatus();
237+
return Docker.getClient().inspectContainerCmd(containerId).exec().getState().getStatus();
238238
} catch (Exception e) {
239-
log.error("Failed to get container status [containerName:{}, containerId:{}, exception:{}]",
239+
log.error("Failed to get container status [containerName:{}, " +
240+
"containerId:{}, exception:{}]",
240241
getContainerName(containerId), containerId, e.getMessage());
241242
e.printStackTrace();
242243
}
@@ -249,17 +250,19 @@ public boolean startContainer(String containerId) {
249250
return false;
250251
}
251252
try {
252-
getDockerClient().startContainerCmd(containerId).exec();
253+
Docker.getClient().startContainerCmd(containerId).exec();
253254
return true;
254255
} catch (Exception e) {
255-
log.error("Failed to start container [containerName:{}, containerId:{}, exception:{}]",
256+
log.error("Failed to start container [containerName:{}, " +
257+
"containerId:{}, exception:{}]",
256258
getContainerName(containerId), containerId, e.getMessage());
257259
e.printStackTrace();
258260
}
259261
return false;
260262
}
261263

262-
public void waitContainerUntilExitOrTimeout(String containerId, Date executionTimeoutDate) {
264+
public void waitContainerUntilExitOrTimeout(String containerId,
265+
Date executionTimeoutDate) {
263266
boolean isExited = false;
264267
boolean isTimeout = false;
265268

@@ -271,8 +274,10 @@ public void waitContainerUntilExitOrTimeout(String containerId, Date executionTi
271274
String containerName = getContainerName(containerId);
272275
while (!isExited && !isTimeout) {
273276
if (seconds % 60 == 0) { //don't display logs too often
274-
log.info("Still running [containerName:{}, containerId:{}, status:{}, isExited:{}, isTimeout:{}]",
275-
containerName, containerId, getContainerStatus(containerId), isExited, isTimeout);
277+
log.info("Still running [containerName:{}, containerId:{}, " +
278+
"status:{}, isExited:{}, isTimeout:{}]",
279+
containerName, containerId,
280+
getContainerStatus(containerId), isExited, isTimeout);
276281
}
277282

278283
WaitUtils.sleep(1);
@@ -282,7 +287,8 @@ public void waitContainerUntilExitOrTimeout(String containerId, Date executionTi
282287
}
283288

284289
if (isTimeout) {
285-
log.warn("Container reached timeout, stopping [containerId:{}, containerName:{}]",
290+
log.warn("Container reached timeout, stopping [containerId:{}, " +
291+
"containerName:{}]",
286292
containerName, containerId);
287293
}
288294
}
@@ -292,13 +298,15 @@ public Optional<DockerLogs> getContainerLogs(String containerId) {
292298
ByteArrayOutputStream stderr = new ByteArrayOutputStream();
293299

294300
try {
295-
getDockerClient()
301+
Docker.getClient()
296302
.logContainerCmd(containerId).withStdOut(true).withStdErr(true)
297303
.exec(new ExecStartResultCallback(stdout, stderr))
298304
.awaitCompletion();
299305
} catch (Exception e) {
300-
log.error("Failed to get docker logs [containerName:{}, containerId:{}, stderr:{}, exception:{}]",
301-
getContainerName(containerId), containerId, stderr.toString(), e.getMessage());
306+
log.error("Failed to get docker logs [containerName:{}, " +
307+
"containerId:{}, stderr:{}, exception:{}]",
308+
getContainerName(containerId), containerId,
309+
stderr.toString(), e.getMessage());
302310
e.printStackTrace();
303311
return Optional.empty();
304312
}
@@ -318,10 +326,11 @@ public boolean stopContainer(String containerId) {
318326
}
319327

320328
try {
321-
getDockerClient().stopContainerCmd(containerId).exec();
329+
Docker.getClient().stopContainerCmd(containerId).exec();
322330
return true;
323331
} catch (Exception e) {
324-
log.error("Failed to stop container [containerName:{}, containerId:{}, exception:{}]",
332+
log.error("Failed to stop container [containerName:{}, " +
333+
"containerId:{}, exception:{}]",
325334
getContainerName(containerId), containerId, e.getMessage());
326335
e.printStackTrace();
327336
}
@@ -333,10 +342,11 @@ public boolean removeContainer(String containerId) {
333342
return false;
334343
}
335344
try {
336-
getDockerClient().removeContainerCmd(containerId).exec();
345+
Docker.getClient().removeContainerCmd(containerId).exec();
337346
return true;
338347
} catch (Exception e) {
339-
log.error("Failed to remove container [containerName:{}, containerId:{}, exception:{}]",
348+
log.error("Failed to remove container [containerName:{}, " +
349+
"containerId:{}, exception:{}]",
340350
getContainerName(containerId), containerId, e.getMessage());
341351
e.printStackTrace();
342352
}
@@ -345,7 +355,7 @@ public boolean removeContainer(String containerId) {
345355

346356
private String getContainerName(String containerId) {
347357
try {
348-
return getDockerClient().listContainersCmd()
358+
return Docker.getClient().listContainersCmd()
349359
.withIdFilter(Collections.singleton(containerId))
350360
.exec()
351361
.stream()
@@ -354,7 +364,8 @@ private String getContainerName(String containerId) {
354364
.map(name -> name[0])
355365
.orElse("");
356366
} catch (Exception e) {
357-
log.error("Failed to get docker container name [containerId:{}, exception:{}]",
367+
log.error("Failed to get docker container name [containerId:{}, " +
368+
"exception:{}]",
358369
containerId, e.getMessage());
359370
e.printStackTrace();
360371
return "";

0 commit comments

Comments
 (0)