@@ -86,14 +86,9 @@ public ReplicateSupplyService(ReplicatesService replicatesService,
86
86
*/
87
87
@ Retryable (value = {OptimisticLockingFailureException .class }, maxAttempts = 5 )
88
88
Optional <ReplicateTaskSummary > getAvailableReplicateTaskSummary (long workerLastBlock , String walletAddress ) {
89
- // return empty if max computing task is reached or if the worker is not found
90
- if (!workerService .canAcceptMoreWorks (walletAddress )) {
91
- return Optional .empty ();
92
- }
93
-
94
89
// return empty if the worker is not sync
95
90
//TODO Check if worker node is sync
96
- boolean isWorkerLastBlockAvailable = workerLastBlock > 0 ;
91
+ final boolean isWorkerLastBlockAvailable = workerLastBlock > 0 ;
97
92
if (!isWorkerLastBlockAvailable ) {
98
93
return Optional .empty ();
99
94
}
@@ -102,13 +97,16 @@ Optional<ReplicateTaskSummary> getAvailableReplicateTaskSummary(long workerLastB
102
97
return Optional .empty ();
103
98
}
104
99
105
- // TODO : Remove this, the optional can never be empty
106
- // This is covered in workerService.canAcceptMoreWorks
107
- Optional <Worker > optional = workerService .getWorker (walletAddress );
100
+ final Optional <Worker > optional = workerService .getWorker (walletAddress );
108
101
if (optional .isEmpty ()) {
109
102
return Optional .empty ();
110
103
}
111
- Worker worker = optional .get ();
104
+ final Worker worker = optional .get ();
105
+
106
+ // return empty if max computing task is reached or if the worker is not found
107
+ if (!workerService .canAcceptMoreWorks (worker )) {
108
+ return Optional .empty ();
109
+ }
112
110
113
111
return getReplicateTaskSummaryForAnyAvailableTask (
114
112
walletAddress ,
@@ -161,8 +159,8 @@ private Optional<ReplicateTaskSummary> getReplicateTaskSummary(Task task, String
161
159
chainTaskId ,
162
160
task .getEnclaveChallenge ());
163
161
ReplicateTaskSummaryBuilder replicateTaskSummary = ReplicateTaskSummary .builder ()
164
- .workerpoolAuthorization (authorization );
165
- if (task .isTeeTask ()){
162
+ .workerpoolAuthorization (authorization );
163
+ if (task .isTeeTask ()) {
166
164
replicateTaskSummary .smsUrl (task .getSmsUrl ());
167
165
}
168
166
return Optional .of (replicateTaskSummary .build ());
@@ -173,7 +171,7 @@ private Optional<ReplicateTaskSummary> getReplicateTaskSummary(Task task, String
173
171
* tries to accept the task - i.e. create a new {@link Replicate}
174
172
* for that task on that worker.
175
173
*
176
- * @param task {@link Task} needing at least one new {@link Replicate}.
174
+ * @param task {@link Task} needing at least one new {@link Replicate}.
177
175
* @param walletAddress Wallet address of a worker looking for new {@link Task}.
178
176
* @return {@literal true} if the task has been accepted,
179
177
* {@literal false} otherwise.
@@ -184,22 +182,6 @@ private boolean acceptOrRejectTask(Task task, String walletAddress) {
184
182
}
185
183
186
184
final String chainTaskId = task .getChainTaskId ();
187
- final Optional <ReplicatesList > oReplicatesList = replicatesService .getReplicatesList (chainTaskId );
188
- // Check is only here to prevent
189
- // "`Optional.get()` without `isPresent()` warning".
190
- // This case should not happen.
191
- if (oReplicatesList .isEmpty ()) {
192
- return false ;
193
- }
194
-
195
- final ReplicatesList replicatesList = oReplicatesList .get ();
196
-
197
- final boolean hasWorkerAlreadyParticipated =
198
- replicatesList .hasWorkerAlreadyParticipated (walletAddress );
199
- if (hasWorkerAlreadyParticipated ) {
200
- return false ;
201
- }
202
-
203
185
final Lock lock = taskAccessForNewReplicateLocks
204
186
.computeIfAbsent (chainTaskId , k -> new ReentrantLock ());
205
187
if (!lock .tryLock ()) {
@@ -209,33 +191,56 @@ private boolean acceptOrRejectTask(Task task, String walletAddress) {
209
191
}
210
192
211
193
try {
212
- final boolean taskNeedsMoreContributions = ConsensusHelper .doesTaskNeedMoreContributionsForConsensus (
213
- chainTaskId ,
214
- replicatesList .getReplicates (),
215
- task .getTrust (),
216
- task .getMaxExecutionTime ());
217
-
218
- if (!taskNeedsMoreContributions
219
- || taskService .isConsensusReached (replicatesList )) {
220
- return false ;
221
- }
222
-
223
- replicatesService .addNewReplicate (chainTaskId , walletAddress );
224
- workerService .addChainTaskIdToWorker (chainTaskId , walletAddress );
194
+ return replicatesService .getReplicatesList (chainTaskId )
195
+ .map (replicatesList -> acceptOrRejectTask (task , walletAddress , replicatesList ))
196
+ .orElse (false );
225
197
} finally {
226
198
// We should always unlock the task
227
199
// so that it could be taken by another replicate
228
200
// if there's any issue.
229
201
lock .unlock ();
230
202
}
203
+ }
231
204
232
- return true ;
205
+ /**
206
+ * Given a {@link Task}, a {@code walletAddress} of a worker and a {@link ReplicatesList},
207
+ * tries to accept the task - i.e. create a new {@link Replicate}
208
+ * for that task on that worker.
209
+ *
210
+ * @param task {@link Task} needing at least one new {@link Replicate}.
211
+ * @param walletAddress Wallet address of a worker looking for new {@link Task}.
212
+ * @param replicatesList Replicates of given {@link Task}.
213
+ * @return {@literal true} if the task has been accepted,
214
+ * {@literal false} otherwise.
215
+ */
216
+ boolean acceptOrRejectTask (Task task , String walletAddress , ReplicatesList replicatesList ) {
217
+ final boolean hasWorkerAlreadyParticipated =
218
+ replicatesList .hasWorkerAlreadyParticipated (walletAddress );
219
+ if (hasWorkerAlreadyParticipated ) {
220
+ return false ;
221
+ }
222
+
223
+ final String chainTaskId = replicatesList .getChainTaskId ();
224
+ final boolean taskNeedsMoreContributions = ConsensusHelper .doesTaskNeedMoreContributionsForConsensus (
225
+ chainTaskId ,
226
+ replicatesList .getReplicates (),
227
+ task .getTrust (),
228
+ task .getMaxExecutionTime ());
229
+
230
+ if (!taskNeedsMoreContributions
231
+ || taskService .isConsensusReached (replicatesList )) {
232
+ return false ;
233
+ }
234
+
235
+ return workerService .addChainTaskIdToWorker (chainTaskId , walletAddress )
236
+ .map (worker -> replicatesService .addNewReplicate (replicatesList , walletAddress ))
237
+ .orElse (false );
233
238
}
234
239
235
240
/**
236
241
* Get notifications missed by the worker during the time it was absent.
237
- *
238
- * @param blockNumber last seen blocknumber by the worker
242
+ *
243
+ * @param blockNumber last seen blocknumber by the worker
239
244
* @param walletAddress of the worker
240
245
* @return list of missed notifications. Can be empty if no notification is found
241
246
*/
@@ -264,7 +269,7 @@ public List<TaskNotification> getMissedTaskNotifications(long blockNumber, Strin
264
269
continue ;
265
270
}
266
271
TaskNotificationExtra taskNotificationExtra =
267
- getTaskNotificationExtra (task , taskNotificationType .get (), walletAddress , enclaveChallenge );
272
+ getTaskNotificationExtra (task , taskNotificationType .get (), walletAddress , enclaveChallenge );
268
273
269
274
TaskNotification taskNotification = TaskNotification .builder ()
270
275
.chainTaskId (chainTaskId )
@@ -286,7 +291,7 @@ public List<TaskNotification> getMissedTaskNotifications(long blockNumber, Strin
286
291
private TaskNotificationExtra getTaskNotificationExtra (Task task , TaskNotificationType taskNotificationType , String walletAddress , String enclaveChallenge ) {
287
292
TaskNotificationExtra taskNotificationExtra = TaskNotificationExtra .builder ().build ();
288
293
289
- switch (taskNotificationType ){
294
+ switch (taskNotificationType ) {
290
295
case PLEASE_CONTRIBUTE :
291
296
WorkerpoolAuthorization authorization = signatureService .createAuthorization (
292
297
walletAddress , task .getChainTaskId (), enclaveChallenge );
@@ -312,7 +317,7 @@ public Optional<TaskNotificationType> getTaskNotificationType(Task task, Replica
312
317
// CONTRIBUTION_TIMEOUT or CONSENSUS_REACHED without contribution
313
318
if (task .getCurrentStatus ().equals (TaskStatus .CONTRIBUTION_TIMEOUT )
314
319
|| (task .getCurrentStatus ().equals (TaskStatus .CONSENSUS_REACHED )
315
- && !replicate .containsContributedStatus ())) {
320
+ && !replicate .containsContributedStatus ())) {
316
321
return Optional .of (TaskNotificationType .PLEASE_ABORT );
317
322
}
318
323
0 commit comments