Skip to content

Commit

Permalink
Merge pull request #580 from iExecBlockchainComputing/feature/refacto…
Browse files Browse the repository at this point in the history
…r-unnotified-detectors

Refactor unnotified detectors
  • Loading branch information
mcornaton authored May 12, 2023
2 parents a4a6b66 + b6428d0 commit f25a94a
Show file tree
Hide file tree
Showing 8 changed files with 129 additions and 184 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ All notable changes to this project will be documented in this file.
- Remove unused methods in `IexecHubService`. (#572)
- Clean unused Replicate methods and update tests. (#573)
- Clean unused `ReplicateStatus#RESULT_UPLOAD_REQUEST_FAILED`. (#575)
- Refactor unnotified detectors to avoid code duplication. (#580)
### Dependency Upgrades
- Upgrade to `iexec-common` 8.0.0-NEXT-SNAPSHOT. (#571 #575)
- Add new `iexec-commons-poco` 1.0.2-NEXT-SNAPSHOT dependency. (#571 #574)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
import com.iexec.common.replicate.ReplicateStatus;
import com.iexec.commons.poco.chain.ChainContributionStatus;
import com.iexec.core.chain.IexecHubService;
import com.iexec.core.chain.Web3jService;
import com.iexec.core.configuration.CronConfiguration;
import com.iexec.core.replicate.ReplicatesService;
import com.iexec.core.task.TaskService;
Expand All @@ -28,80 +27,28 @@
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;

import java.util.List;

@Slf4j
@Service
public class ContributionUnnotifiedDetector extends UnnotifiedAbstractDetector {

private static final int LESS_OFTEN_DETECTOR_FREQUENCY = 10;
private final List<TaskStatus> dectectWhenOffchainTaskStatuses;
private final ReplicateStatus offchainCompleting;
private final ReplicateStatus offchainCompleted;
private final ChainContributionStatus onchainCompleted;
private final int detectorRate;

private int detectorOccurrence;

public ContributionUnnotifiedDetector(TaskService taskService,
ReplicatesService replicatesService,
IexecHubService iexecHubService,
Web3jService web3jService,
CronConfiguration cronConfiguration) {
super(taskService, replicatesService, iexecHubService, web3jService);
dectectWhenOffchainTaskStatuses = TaskStatus.getWaitingContributionStatuses();
offchainCompleting = ReplicateStatus.CONTRIBUTING;
offchainCompleted = ReplicateStatus.CONTRIBUTED;
onchainCompleted = ChainContributionStatus.CONTRIBUTED;
this.detectorRate = cronConfiguration.getContribute();
super(
taskService,
replicatesService,
iexecHubService,
TaskStatus.getWaitingContributionStatuses(),
ReplicateStatus.CONTRIBUTING,
ReplicateStatus.CONTRIBUTED,
ChainContributionStatus.CONTRIBUTED,
cronConfiguration.getContribute());
}

/**
* Detects onchain CONTRIBUTED only if replicates are offchain CONTRIBUTING and
* onchain CONTRIBUTED if replicates are not CONTRIBUTED.
* The second detection is not always ran, depending on the detector run occurrences.
*/
@Override
@Scheduled(fixedRateString = "#{@cronConfiguration.getContribute()}")
public void detectOnChainChanges() {
detectOnchainContributedWhenOffchainContributing();

detectorOccurrence++;
if (detectorOccurrence % LESS_OFTEN_DETECTOR_FREQUENCY == 0) {
detectOnchainContributed();
}
super.detectOnChainChanges();
}

/*
* Detecting onchain CONTRIBUTED only if replicates are offchain CONTRIBUTING
* (worker didn't notify last offchain CONTRIBUTED)
* We want to detect them very often since it's highly probable
*/
public void detectOnchainContributedWhenOffchainContributing() {
log.debug("Detect onchain Contributed (when offchain Contributing) [retryIn:{}]",
this.detectorRate);
dectectOnchainCompletedWhenOffchainCompleting(
dectectWhenOffchainTaskStatuses,
offchainCompleting,
offchainCompleted,
onchainCompleted
);
}

/*
* Detecting onchain CONTRIBUTED if replicates are not CONTRIBUTED
* (worker didn't notify any status)
* We want to detect them:
* - Frequently but no so often since it's eth node resource consuming and less probable
* - When we receive a CANT_CONTRIBUTE_SINCE_TASK_NOT_ACTIVE
*/
public void detectOnchainContributed() {
log.debug("Detect onchain Contributed [retryIn:{}]", this.detectorRate * LESS_OFTEN_DETECTOR_FREQUENCY);
dectectOnchainCompleted(
dectectWhenOffchainTaskStatuses,
offchainCompleting,
offchainCompleted,
onchainCompleted
);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
import com.iexec.common.replicate.ReplicateStatus;
import com.iexec.commons.poco.chain.ChainContributionStatus;
import com.iexec.core.chain.IexecHubService;
import com.iexec.core.chain.Web3jService;
import com.iexec.core.configuration.CronConfiguration;
import com.iexec.core.replicate.ReplicatesService;
import com.iexec.core.task.TaskService;
Expand All @@ -28,76 +27,28 @@
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;

import java.util.List;

@Slf4j
@Service
public class RevealUnnotifiedDetector extends UnnotifiedAbstractDetector {

private static final int LESS_OFTEN_DETECTOR_FREQUENCY = 10;
private final List<TaskStatus> dectectWhenTaskStatuses;
private final ReplicateStatus offchainCompleting;
private final ReplicateStatus offchainCompleted;
private final ChainContributionStatus onchainCompleted;
private final CronConfiguration cronConfiguration;

private int detectorOccurrence;

public RevealUnnotifiedDetector(TaskService taskService,
ReplicatesService replicatesService,
IexecHubService iexecHubService,
Web3jService web3jService,
CronConfiguration cronConfiguration) {
super(taskService, replicatesService, iexecHubService, web3jService);
this.cronConfiguration = cronConfiguration;
dectectWhenTaskStatuses = TaskStatus.getWaitingRevealStatuses();
offchainCompleting = ReplicateStatus.REVEALING;
offchainCompleted = ReplicateStatus.REVEALED;
onchainCompleted = ChainContributionStatus.REVEALED;
super(
taskService,
replicatesService,
iexecHubService,
TaskStatus.getWaitingRevealStatuses(),
ReplicateStatus.REVEALING,
ReplicateStatus.REVEALED,
ChainContributionStatus.REVEALED,
cronConfiguration.getReveal());
}

@Override
@Scheduled(fixedRateString = "#{@cronConfiguration.getReveal()}")
public void detectOnChainChanges() {
detectOnchainRevealedWhenOffchainRevealed();

detectorOccurrence++;
if (detectorOccurrence % LESS_OFTEN_DETECTOR_FREQUENCY == 0) {
detectOnchainRevealed();
}
super.detectOnChainChanges();
}

/*
* Detecting onchain REVEALED only if replicates are offchain REVEALING
* (worker didn't notify last offchain REVEALED)
* We want to detect them very often since it's highly probable
*/
public void detectOnchainRevealedWhenOffchainRevealed() {
log.debug("Detect onchain Revealed (when offchain Revealing) [retryIn:{}]",
cronConfiguration.getReveal());
dectectOnchainCompletedWhenOffchainCompleting(
dectectWhenTaskStatuses,
offchainCompleting,
offchainCompleted,
onchainCompleted
);
}

/*
* Detecting onchain REVEALED if replicates are not REVEALED
* (worker didn't notify any status)
* We want to detect them:
* - Frequently but no so often since it's eth node resource consuming and less probable
* - When we receive a CANT_REVEAL
*/
public void detectOnchainRevealed() {
log.debug("Detect onchain Revealed [retryIn:{}]",
cronConfiguration.getReveal() * LESS_OFTEN_DETECTOR_FREQUENCY);
dectectOnchainCompleted(
dectectWhenTaskStatuses,
offchainCompleting,
offchainCompleted,
onchainCompleted
);
}

}
Loading

0 comments on commit f25a94a

Please sign in to comment.