Skip to content

Commit 7ffdd87

Browse files
Merge pull request #394 from iExecBlockchainComputing/feature/next-action-on-replicate-status-and-cause
Next action depends on replicate status and cause
2 parents c3f6aeb + 77ea2fe commit 7ffdd87

File tree

5 files changed

+173
-10
lines changed

5 files changed

+173
-10
lines changed

src/main/java/com/iexec/core/chain/DealWatcherService.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ private void onDealEvent(DealEvent dealEvent) {
100100
*/
101101
private void handleDeal(String chainDealId) {
102102
Optional<ChainDeal> oChainDeal = iexecHubService.getChainDeal(chainDealId);
103-
if (!oChainDeal.isPresent()) {
103+
if (oChainDeal.isEmpty()) {
104104
log.error("Could not get chain deal [chainDealId:{}]", chainDealId);
105105
return;
106106
}

src/main/java/com/iexec/core/replicate/ReplicatesService.java

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
import com.iexec.common.chain.ChainContribution;
3737
import com.iexec.common.notification.TaskNotificationType;
3838
import com.iexec.common.replicate.ReplicateStatus;
39+
import com.iexec.common.replicate.ReplicateStatusCause;
3940
import com.iexec.common.replicate.ReplicateStatusDetails;
4041
import com.iexec.common.replicate.ReplicateStatusUpdate;
4142
import com.iexec.common.task.TaskDescription;
@@ -316,10 +317,13 @@ public Optional<TaskNotificationType> updateReplicateStatus(String chainTaskId,
316317
replicate.updateStatus(statusUpdate);
317318
replicatesRepository.save(replicatesList);
318319
applicationEventPublisher.publishEvent(new ReplicateUpdatedEvent(chainTaskId, walletAddress, statusUpdate));
319-
TaskNotificationType nextAction = ReplicateWorkflow.getInstance().getNextAction(newStatus);
320+
ReplicateStatusCause newStatusCause = statusUpdate.getDetails() != null ?
321+
statusUpdate.getDetails().getCause() : null;
322+
TaskNotificationType nextAction = ReplicateWorkflow.getInstance().getNextAction(newStatus, newStatusCause);
320323

321-
log.info("Replicate updated successfully [newStatus:{} nextAction:{}, chainTaskId:{}, walletAddress:{}]",
322-
replicate.getCurrentStatus(), nextAction, chainTaskId, walletAddress);
324+
log.info("Replicate updated successfully [newStatus:{}, newStatusCause:{} " +
325+
"nextAction:{}, chainTaskId:{}, walletAddress:{}]",
326+
replicate.getCurrentStatus(), newStatusCause, nextAction, chainTaskId, walletAddress);
323327

324328
return Optional.ofNullable(nextAction); // should we return a default action when null?
325329
}

src/main/java/com/iexec/core/workflow/ReplicateWorkflow.java

Lines changed: 37 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818

1919
import com.iexec.common.notification.TaskNotificationType;
2020
import com.iexec.common.replicate.ReplicateStatus;
21+
import com.iexec.common.replicate.ReplicateStatusCause;
2122

2223
import static com.iexec.common.notification.TaskNotificationType.*;
2324
import static com.iexec.common.replicate.ReplicateStatus.*;
@@ -31,7 +32,7 @@
3132
public class ReplicateWorkflow extends Workflow<ReplicateStatus> {
3233

3334
private static ReplicateWorkflow instance;
34-
private Map<ReplicateStatus, TaskNotificationType> actionMap = new LinkedHashMap<>();
35+
private final Map<ReplicateStatus, TaskNotificationType> actionMap = new LinkedHashMap<>();
3536

3637
private ReplicateWorkflow() {
3738
super();
@@ -171,7 +172,39 @@ private void setNextAction(ReplicateStatus whenStatus, TaskNotificationType next
171172
actionMap.putIfAbsent(whenStatus, nextAction);
172173
}
173174

174-
public TaskNotificationType getNextAction(ReplicateStatus whenStatus) {
175+
public TaskNotificationType getNextAction(ReplicateStatus whenStatus, ReplicateStatusCause whenCause) {
176+
TaskNotificationType nextAction = getNextActionWhenStatusAndCause(whenStatus, whenCause);
177+
if (nextAction == null){
178+
nextAction = getNextActionWhenStatus(whenStatus);
179+
}
180+
return nextAction;
181+
}
182+
183+
TaskNotificationType getNextActionWhenStatusAndCause(ReplicateStatus whenStatus, ReplicateStatusCause whenCause) {
184+
if (whenStatus == null){
185+
return null;
186+
}
187+
if (whenCause == null){
188+
return null;
189+
}
190+
switch (whenStatus){
191+
case APP_DOWNLOAD_FAILED:
192+
if (whenCause.equals(ReplicateStatusCause.APP_IMAGE_DOWNLOAD_FAILED)){
193+
return PLEASE_CONTRIBUTE;
194+
}
195+
return PLEASE_ABORT;
196+
case DATA_DOWNLOAD_FAILED:
197+
if (whenCause.equals(ReplicateStatusCause.DATASET_FILE_DOWNLOAD_FAILED)
198+
|| whenCause.equals(ReplicateStatusCause.INPUT_FILES_DOWNLOAD_FAILED)){
199+
return PLEASE_CONTRIBUTE;
200+
}
201+
return PLEASE_ABORT;
202+
default:
203+
return null;
204+
}
205+
}
206+
207+
TaskNotificationType getNextActionWhenStatus(ReplicateStatus whenStatus) {
175208
if (actionMap.containsKey(whenStatus)){
176209
return actionMap.get(whenStatus);
177210
}
@@ -185,11 +218,11 @@ private void setNextActions() {
185218

186219
setNextAction(APP_DOWNLOADING, PLEASE_CONTINUE);
187220
setNextAction(APP_DOWNLOADED, PLEASE_DOWNLOAD_DATA);
188-
setNextAction(APP_DOWNLOAD_FAILED, PLEASE_CONTRIBUTE);
221+
setNextAction(APP_DOWNLOAD_FAILED, PLEASE_ABORT);
189222

190223
setNextAction(DATA_DOWNLOADING, PLEASE_CONTINUE);
191224
setNextAction(DATA_DOWNLOADED, PLEASE_COMPUTE);
192-
setNextAction(DATA_DOWNLOAD_FAILED, PLEASE_CONTRIBUTE);
225+
setNextAction(DATA_DOWNLOAD_FAILED, PLEASE_ABORT);
193226

194227
setNextAction(COMPUTING, PLEASE_CONTINUE);
195228
setNextAction(COMPUTED, PLEASE_CONTRIBUTE);

src/main/resources/workflow/replicate-actions.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,10 @@
44
"START_FAILED" : "PLEASE_ABORT",
55
"APP_DOWNLOADING" : "PLEASE_CONTINUE",
66
"APP_DOWNLOADED" : "PLEASE_DOWNLOAD_DATA",
7-
"APP_DOWNLOAD_FAILED" : "PLEASE_CONTRIBUTE",
7+
"APP_DOWNLOAD_FAILED" : "PLEASE_ABORT",
88
"DATA_DOWNLOADING" : "PLEASE_CONTINUE",
99
"DATA_DOWNLOADED" : "PLEASE_COMPUTE",
10-
"DATA_DOWNLOAD_FAILED" : "PLEASE_CONTRIBUTE",
10+
"DATA_DOWNLOAD_FAILED" : "PLEASE_ABORT",
1111
"COMPUTING" : "PLEASE_CONTINUE",
1212
"COMPUTED" : "PLEASE_CONTRIBUTE",
1313
"COMPUTE_FAILED" : "PLEASE_ABORT",
Lines changed: 126 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,126 @@
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.core.workflow;
18+
19+
import com.iexec.common.notification.TaskNotificationType;
20+
import com.iexec.common.replicate.ReplicateStatus;
21+
import com.iexec.common.replicate.ReplicateStatusCause;
22+
import org.junit.Before;
23+
import org.junit.Test;
24+
import org.mockito.MockitoAnnotations;
25+
26+
import static org.assertj.core.api.Assertions.assertThat;
27+
28+
public class ReplicateWorkflowTests {
29+
30+
private ReplicateWorkflow replicateWorkflow;
31+
32+
@Before
33+
public void setup() {
34+
replicateWorkflow = ReplicateWorkflow.getInstance();
35+
}
36+
37+
@Test
38+
public void shouldNotGetNextActionWhenStatusSinceStatusIsNull(){
39+
assertThat(replicateWorkflow
40+
.getNextActionWhenStatus(null)).isNull();
41+
}
42+
43+
@Test
44+
public void shouldNotGetNextActionWhenStatusSinceStatusIsUnknown(){
45+
assertThat(replicateWorkflow
46+
.getNextActionWhenStatus(ReplicateStatus.ABORTED)) //unknown
47+
.isNull();
48+
}
49+
50+
@Test
51+
public void shouldNotGetNextActionWhenStatusAndCauseSinceCauseIsNull(){
52+
assertThat(replicateWorkflow
53+
.getNextActionWhenStatusAndCause(null,
54+
ReplicateStatusCause.INPUT_FILES_DOWNLOAD_FAILED)) //any
55+
.isNull();
56+
}
57+
58+
@Test
59+
public void shouldNotGetNextActionWhenStatusAndCauseSinceStatusIsUnknown(){
60+
assertThat(replicateWorkflow
61+
.getNextActionWhenStatusAndCause(ReplicateStatus.ABORTED, //unknown
62+
ReplicateStatusCause.ABORTED_BY_WORKER)) //any
63+
.isNull();
64+
}
65+
66+
// app
67+
68+
@Test
69+
public void shouldGetNextActionOnAppDownloadFailed(){
70+
assertThat(replicateWorkflow
71+
.getNextAction(ReplicateStatus.APP_DOWNLOAD_FAILED,
72+
null))
73+
.isEqualTo(TaskNotificationType.PLEASE_ABORT);
74+
}
75+
76+
@Test
77+
public void shouldGetNextActionOnAppDownloadFailedWithPostComputeFailed(){
78+
assertThat(replicateWorkflow
79+
.getNextAction(ReplicateStatus.APP_DOWNLOAD_FAILED,
80+
ReplicateStatusCause.POST_COMPUTE_FAILED))
81+
.isEqualTo(TaskNotificationType.PLEASE_ABORT);
82+
}
83+
84+
@Test
85+
public void shouldGetNextActionOnAppDownloadFailedWithAppImageDownloadFailed(){
86+
assertThat(replicateWorkflow
87+
.getNextAction(ReplicateStatus.APP_DOWNLOAD_FAILED,
88+
ReplicateStatusCause.APP_IMAGE_DOWNLOAD_FAILED))
89+
.isEqualTo(TaskNotificationType.PLEASE_CONTRIBUTE);
90+
}
91+
92+
// data
93+
94+
@Test
95+
public void shouldGetNextActionOnDataDownloadFailed(){
96+
assertThat(replicateWorkflow
97+
.getNextAction(ReplicateStatus.DATA_DOWNLOAD_FAILED,
98+
null))
99+
.isEqualTo(TaskNotificationType.PLEASE_ABORT);
100+
}
101+
102+
@Test
103+
public void shouldGetNextActionOnDataDownloadFailedWithPostComputeFailed(){
104+
assertThat(replicateWorkflow
105+
.getNextAction(ReplicateStatus.DATA_DOWNLOAD_FAILED,
106+
ReplicateStatusCause.POST_COMPUTE_FAILED))
107+
.isEqualTo(TaskNotificationType.PLEASE_ABORT);
108+
}
109+
110+
@Test
111+
public void shouldGetNextActionOnDataDownloadFailedWithDatasetDownloadFailed(){
112+
assertThat(replicateWorkflow
113+
.getNextAction(ReplicateStatus.DATA_DOWNLOAD_FAILED,
114+
ReplicateStatusCause.DATASET_FILE_DOWNLOAD_FAILED))
115+
.isEqualTo(TaskNotificationType.PLEASE_CONTRIBUTE);
116+
}
117+
118+
@Test
119+
public void shouldGetNextActionOnDataDownloadFailedWithInputFilesDownloadFailed(){
120+
assertThat(replicateWorkflow
121+
.getNextAction(ReplicateStatus.DATA_DOWNLOAD_FAILED,
122+
ReplicateStatusCause.INPUT_FILES_DOWNLOAD_FAILED))
123+
.isEqualTo(TaskNotificationType.PLEASE_CONTRIBUTE);
124+
}
125+
126+
}

0 commit comments

Comments
 (0)