Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor Replicate#getLastRelevantStatus to remove the Optional return type #524

Merged
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 7 additions & 18 deletions src/main/java/com/iexec/core/contribution/ContributionHelper.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@

import java.util.HashSet;
import java.util.List;
import java.util.Optional;
import java.util.Set;

public class ContributionHelper {
Expand All @@ -38,12 +37,9 @@ static int getContributedWeight(List<Replicate> replicates, String contribution)
int groupWeight = 0;
for (Replicate replicate : replicates) {

Optional<ReplicateStatus> lastRelevantStatus = replicate.getLastRelevantStatus();
if (lastRelevantStatus.isEmpty()) {
continue;
}
ReplicateStatus lastRelevantStatus = replicate.getLastRelevantStatus();

boolean isContributed = lastRelevantStatus.get().equals(ReplicateStatus.CONTRIBUTED);
boolean isContributed = lastRelevantStatus.equals(ReplicateStatus.CONTRIBUTED);
boolean haveSameContribution = contribution.equals(replicate.getContributionHash());
boolean hasWeight = replicate.getWorkerWeight() > 0;

Expand All @@ -64,14 +60,11 @@ static int getPendingWeight(List<Replicate> replicates, long maxExecutionTime) {

for (Replicate replicate : replicates) {

Optional<ReplicateStatus> lastRelevantStatus = replicate.getLastRelevantStatus();
if (lastRelevantStatus.isEmpty()) {
continue;
}
ReplicateStatus lastRelevantStatus = replicate.getLastRelevantStatus();

boolean isCreatedLessThanOnePeriodAgo = !replicate.isCreatedMoreThanNPeriodsAgo(1, maxExecutionTime);
boolean isNotContributed = !lastRelevantStatus.get().equals(ReplicateStatus.CONTRIBUTED);
boolean isNotFailed = !lastRelevantStatus.get().equals(ReplicateStatus.FAILED);
boolean isNotContributed = !lastRelevantStatus.equals(ReplicateStatus.CONTRIBUTED);
boolean isNotFailed = !lastRelevantStatus.equals(ReplicateStatus.FAILED);
boolean hasWeight = replicate.getWorkerWeight() > 0;

if (isCreatedLessThanOnePeriodAgo && isNotContributed && isNotFailed && hasWeight) {
Expand All @@ -92,12 +85,8 @@ static Set<String> getDistinctContributions(List<Replicate> replicates) {

for (Replicate replicate : replicates) {

Optional<ReplicateStatus> lastRelevantStatus = replicate.getLastRelevantStatus();
if (lastRelevantStatus.isEmpty()) {
continue;
}

if (lastRelevantStatus.get().equals(ReplicateStatus.CONTRIBUTED)) {
ReplicateStatus lastRelevantStatus = replicate.getLastRelevantStatus();
if (lastRelevantStatus.equals(ReplicateStatus.CONTRIBUTED)) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I tend to prefer the inverted syntax for NPE protection

Suggested change
if (lastRelevantStatus.equals(ReplicateStatus.CONTRIBUTED)) {
if (ReplicateStatus.CONTRIBUTED.equals(lastRelevantStatus)) {

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Using == is a bit better for enums. Please see 566e532 :)

distinctContributions.add(replicate.getContributionHash());
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@
import lombok.extern.slf4j.Slf4j;

import java.util.List;
import java.util.Optional;

import static com.iexec.common.replicate.ReplicateStatus.WORKER_LOST;
import static com.iexec.common.replicate.ReplicateStatus.getMissingStatuses;
Expand Down Expand Up @@ -60,16 +59,16 @@ void dectectOnchainCompletedWhenOffchainCompleting(List<TaskStatus> detectWhenOf
ChainContributionStatus onchainCompleted) {
for (Task task : taskService.findByCurrentStatus(detectWhenOffChainTaskStatuses)) {
for (Replicate replicate : replicatesService.getReplicates(task.getChainTaskId())) {
Optional<ReplicateStatus> lastRelevantStatus = replicate.getLastRelevantStatus();
if (lastRelevantStatus.isEmpty() || !lastRelevantStatus.get().equals(offchainCompleting)) {
ReplicateStatus lastRelevantStatus = replicate.getLastRelevantStatus();
if (!lastRelevantStatus.equals(offchainCompleting)) {
continue;
}

boolean statusTrueOnChain = iexecHubService.isStatusTrueOnChain(task.getChainTaskId(), replicate.getWalletAddress(), onchainCompleted);

if (statusTrueOnChain) {
log.info("Detected confirmed missing update (replicate) [is:{}, should:{}, taskId:{}]",
lastRelevantStatus.get(), onchainCompleted, task.getChainTaskId());
lastRelevantStatus, onchainCompleted, task.getChainTaskId());
updateReplicateStatuses(task, replicate, offchainCompleted);
}
}
Expand All @@ -82,17 +81,17 @@ void dectectOnchainCompleted(List<TaskStatus> detectWhenOffChainTaskStatuses,
ChainContributionStatus onchainCompleted) {
for (Task task : taskService.findByCurrentStatus(detectWhenOffChainTaskStatuses)) {
for (Replicate replicate : replicatesService.getReplicates(task.getChainTaskId())) {
Optional<ReplicateStatus> lastRelevantStatus = replicate.getLastRelevantStatus();
ReplicateStatus lastRelevantStatus = replicate.getLastRelevantStatus();

if (lastRelevantStatus.isEmpty() || lastRelevantStatus.get().equals(offchainCompleted)) {
if (lastRelevantStatus.equals(offchainCompleted)) {
continue;
}

boolean statusTrueOnChain = iexecHubService.isStatusTrueOnChain(task.getChainTaskId(), replicate.getWalletAddress(), onchainCompleted);

if (statusTrueOnChain) {
log.info("Detected confirmed missing update (replicate) [is:{}, should:{}, taskId:{}]",
lastRelevantStatus.get(), onchainCompleted, task.getChainTaskId());
lastRelevantStatus, onchainCompleted, task.getChainTaskId());
updateReplicateStatuses(task, replicate, offchainCompleted);
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package com.iexec.core.replicate;

import lombok.AllArgsConstructor;
import lombok.Getter;

/**
* Exception that can be thrown
* when searching for a {@link com.iexec.common.replicate.ReplicateStatus} fails.
*/
@AllArgsConstructor
@Getter
public class NoReplicateStatusException extends RuntimeException {
private final String chainTaskId;
}
41 changes: 19 additions & 22 deletions src/main/java/com/iexec/core/replicate/Replicate.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,28 +19,30 @@
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.iexec.common.chain.ChainReceipt;
import com.iexec.common.replicate.ReplicateStatus;
import com.iexec.common.replicate.ReplicateStatusCause;
import com.iexec.common.replicate.ReplicateStatusDetails;
import com.iexec.common.replicate.ReplicateStatusModifier;
import com.iexec.common.replicate.ReplicateStatusUpdate;

import com.iexec.common.replicate.*;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.util.*;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

import static com.iexec.common.replicate.ReplicateStatus.*;
import static com.iexec.common.replicate.ReplicateStatusUpdate.*;
import static com.iexec.common.replicate.ReplicateStatus.CREATED;
import static com.iexec.common.replicate.ReplicateStatus.WORKER_LOST;
import static com.iexec.common.replicate.ReplicateStatusUpdate.poolManagerRequest;


@Data
@NoArgsConstructor
@JsonIgnoreProperties(ignoreUnknown = true)
public class Replicate {

private List<ReplicateStatusUpdate> statusUpdateList;
private List<ReplicateStatusUpdate> statusUpdateList = new ArrayList<>(
mcornaton marked this conversation as resolved.
Show resolved Hide resolved
// a new replicate should only be created by the scheduler
List.of(poolManagerRequest(CREATED))
);
private String walletAddress;
private String resultLink;
private String chainCallbackData;
Expand All @@ -52,9 +54,6 @@ public class Replicate {
public Replicate(String walletAddress, String chainTaskId) {
this.chainTaskId = chainTaskId;
this.walletAddress = walletAddress;
this.statusUpdateList = new ArrayList<>();
// a new replicate should only be create by the scheduler
this.statusUpdateList.add(poolManagerRequest(CREATED));
this.contributionHash = "";
}

Expand All @@ -64,7 +63,7 @@ public ReplicateStatus getCurrentStatus() {
}

@JsonIgnore
public Optional<ReplicateStatus> getLastRelevantStatus() { // FIXME: remove Optional and add a no-args constructor
public ReplicateStatus getLastRelevantStatus() {
// ignore cases like: WORKER_LOST and RECOVERING

List<ReplicateStatus> statusList = getStatusUpdateList().stream()
mcornaton marked this conversation as resolved.
Show resolved Hide resolved
Expand All @@ -77,11 +76,11 @@ public Optional<ReplicateStatus> getLastRelevantStatus() { // FIXME: remove Opt

for (int i = statusList.size() - 1; i >= 0; i--) {
if (!ignoredStatuses.contains(statusList.get(i))) {
return Optional.of(statusList.get(i));
return statusList.get(i);
}
}

return Optional.empty();
throw new NoReplicateStatusException(chainTaskId);
mcornaton marked this conversation as resolved.
Show resolved Hide resolved
}

@JsonIgnore
Expand Down Expand Up @@ -149,15 +148,13 @@ public boolean isBusyComputing() {
}

public boolean isRecoverable() {
Optional<ReplicateStatus> currentStatus = getLastRelevantStatus();
if (currentStatus.isEmpty()) return false;
return ReplicateStatus.isRecoverable(currentStatus.get());
ReplicateStatus currentStatus = getLastRelevantStatus();
return ReplicateStatus.isRecoverable(currentStatus);
}

public boolean isBeforeStatus(ReplicateStatus status) {
Optional<ReplicateStatus> currentStatus = getLastRelevantStatus();
if (currentStatus.isEmpty()) return false;
return currentStatus.get().ordinal() < status.ordinal();
ReplicateStatus currentStatus = getLastRelevantStatus();
return currentStatus.ordinal() < status.ordinal();
}

boolean isStatusBeforeWorkerLostEqualsTo(ReplicateStatus status) {
Expand Down
37 changes: 9 additions & 28 deletions src/main/java/com/iexec/core/replicate/ReplicateSupplyService.java
Original file line number Diff line number Diff line change
Expand Up @@ -345,12 +345,8 @@ private Optional<TaskNotificationType> recoverReplicateInContributionPhase(Task
String chainTaskId = task.getChainTaskId();
String walletAddress = replicate.getWalletAddress();

if (replicate.getLastRelevantStatus().isEmpty()) {
return Optional.empty();
}

boolean beforeContributing = replicate.isBeforeStatus(ReplicateStatus.CONTRIBUTING);
boolean didReplicateStartContributing = replicate.getLastRelevantStatus().get().equals(ReplicateStatus.CONTRIBUTING);
boolean didReplicateStartContributing = replicate.getLastRelevantStatus().equals(ReplicateStatus.CONTRIBUTING);
boolean didReplicateContributeOnChain = replicatesService.didReplicateContributeOnchain(chainTaskId, walletAddress);

if (beforeContributing) {
Expand All @@ -373,11 +369,7 @@ private Optional<TaskNotificationType> recoverReplicateInContributionPhase(Task
}

Replicate replicateWithLatestChanges = oReplicateWithLatestChanges.get();
if (replicateWithLatestChanges.getLastRelevantStatus().isEmpty()) {
return Optional.empty();
}

boolean didReplicateContribute = replicateWithLatestChanges.getLastRelevantStatus().get()
boolean didReplicateContribute = replicateWithLatestChanges.getLastRelevantStatus()
.equals(ReplicateStatus.CONTRIBUTED);

if (didReplicateContribute) {
Expand Down Expand Up @@ -408,12 +400,8 @@ private Optional<TaskNotificationType> recoverReplicateInRevealPhase(Task task,
String chainTaskId = task.getChainTaskId();
String walletAddress = replicate.getWalletAddress();

if (replicate.getLastRelevantStatus().isEmpty()) {
return Optional.empty();
}

boolean isInStatusContributed = replicate.getLastRelevantStatus().get().equals(ReplicateStatus.CONTRIBUTED);
boolean didReplicateStartRevealing = replicate.getLastRelevantStatus().get().equals(ReplicateStatus.REVEALING);
boolean isInStatusContributed = replicate.getLastRelevantStatus().equals(ReplicateStatus.CONTRIBUTED);
boolean didReplicateStartRevealing = replicate.getLastRelevantStatus().equals(ReplicateStatus.REVEALING);
jbern0rd marked this conversation as resolved.
Show resolved Hide resolved
boolean didReplicateRevealOnChain = replicatesService.didReplicateRevealOnchain(chainTaskId, walletAddress);

if (isInStatusContributed) {
Expand All @@ -437,14 +425,11 @@ private Optional<TaskNotificationType> recoverReplicateInRevealPhase(Task task,
return Optional.empty();
}
replicate = oReplicateWithLatestChanges.get();
if (replicate.getLastRelevantStatus().isEmpty()) {
return Optional.empty();
}

boolean didReplicateReveal = replicate.getLastRelevantStatus().get()
boolean didReplicateReveal = replicate.getLastRelevantStatus()
.equals(ReplicateStatus.REVEALED);

boolean wasReplicateRequestedToUpload = replicate.getLastRelevantStatus().get()
boolean wasReplicateRequestedToUpload = replicate.getLastRelevantStatus()
.equals(ReplicateStatus.RESULT_UPLOAD_REQUESTED);

if (didReplicateReveal) {
Expand All @@ -470,14 +455,10 @@ private Optional<TaskNotificationType> recoverReplicateInResultUploadPhase(Task
String chainTaskId = task.getChainTaskId();
String walletAddress = replicate.getWalletAddress();

if (replicate.getLastRelevantStatus().isEmpty()) {
return Optional.empty();
}

boolean wasReplicateRequestedToUpload = replicate.getLastRelevantStatus().get().equals(ReplicateStatus.RESULT_UPLOAD_REQUESTED);
boolean didReplicateStartUploading = replicate.getLastRelevantStatus().get().equals(ReplicateStatus.RESULT_UPLOADING);
boolean wasReplicateRequestedToUpload = replicate.getLastRelevantStatus().equals(ReplicateStatus.RESULT_UPLOAD_REQUESTED);
boolean didReplicateStartUploading = replicate.getLastRelevantStatus().equals(ReplicateStatus.RESULT_UPLOADING);
boolean didReplicateUploadWithoutNotifying = replicatesService.isResultUploaded(task.getChainTaskId());
boolean hasReplicateAlreadyUploaded = replicate.getLastRelevantStatus().get().equals(ReplicateStatus.RESULT_UPLOADED);
boolean hasReplicateAlreadyUploaded = replicate.getLastRelevantStatus().equals(ReplicateStatus.RESULT_UPLOADED);

if (wasReplicateRequestedToUpload) {
return Optional.of(TaskNotificationType.PLEASE_UPLOAD);
Expand Down
6 changes: 3 additions & 3 deletions src/main/java/com/iexec/core/replicate/ReplicatesList.java
Original file line number Diff line number Diff line change
Expand Up @@ -69,8 +69,8 @@ public ReplicatesList(String chainTaskId, List<Replicate> replicates) {
public int getNbValidContributedWinners(String contributionHash) {
int nbValidWinners = 0;
for (Replicate replicate : replicates) {
Optional<ReplicateStatus> oStatus = replicate.getLastRelevantStatus();
if (oStatus.isPresent() && oStatus.get().equals(CONTRIBUTED)
ReplicateStatus status = replicate.getLastRelevantStatus();
if (status.equals(CONTRIBUTED)
&& contributionHash.equals(replicate.getContributionHash())) {
nbValidWinners++;
}
Expand All @@ -94,7 +94,7 @@ public int getNbReplicatesWithLastRelevantStatus(ReplicateStatus... listStatus)
int nbReplicates = 0;
for (Replicate replicate : replicates) {
for (ReplicateStatus status : listStatus) {
if (Objects.equals(replicate.getLastRelevantStatus().orElse(null), status)) {
if (Objects.equals(replicate.getLastRelevantStatus(), status)) {
nbReplicates++;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -608,11 +608,7 @@ public boolean didReplicateRevealOnchain(String chainTaskId, String walletAddres
}

public void setRevealTimeoutStatusIfNeeded(String chainTaskId, Replicate replicate) {
Optional<ReplicateStatus> oStatus = replicate.getLastRelevantStatus();
if (oStatus.isEmpty()) {
return;
}
ReplicateStatus status = oStatus.get();
ReplicateStatus status = replicate.getLastRelevantStatus();
if (status.equals(REVEALING) || status.equals(CONTRIBUTED)) {
ReplicateStatusUpdate statusUpdate = ReplicateStatusUpdate.poolManagerRequest(FAILED, REVEAL_TIMEOUT);
updateReplicateStatus(chainTaskId, replicate.getWalletAddress(), statusUpdate);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -377,7 +377,6 @@ void running2RunningFailed(Task task) {
boolean notAllReplicatesFailed = replicatesOfAliveWorkers
.stream()
.map(Replicate::getLastRelevantStatus)
.map(Optional::get)
.anyMatch(Predicate.not(ReplicateStatus::isFailedBeforeComputed));

if (notAllReplicatesFailed) {
Expand Down Expand Up @@ -498,8 +497,7 @@ void resultUploading2Uploaded(Task task) {

boolean isReplicateUploading = replicate.getCurrentStatus() == ReplicateStatus.RESULT_UPLOADING;
boolean isReplicateRecoveringToUpload = replicate.getCurrentStatus() == ReplicateStatus.RECOVERING &&
replicate.getLastRelevantStatus().isPresent() &&
replicate.getLastRelevantStatus().get() == ReplicateStatus.RESULT_UPLOADING;
replicate.getLastRelevantStatus() == ReplicateStatus.RESULT_UPLOADING;

if (!isReplicateUploading && !isReplicateRecoveringToUpload) {
requestUpload(task);
Expand Down
Loading