@@ -94,7 +94,7 @@ private void onPossibleCompletion() {
94
94
95
95
if (timedOut == false ) {
96
96
for (final PublicationTarget target : publicationTargets ) {
97
- if (target .publicationTargetStateMachine . isActive ()) {
97
+ if (target .isActive ()) {
98
98
return ;
99
99
}
100
100
}
@@ -119,7 +119,7 @@ private void onPossibleCompletion() {
119
119
private boolean publicationCompletedIffAllTargetsInactiveOrTimedOut () {
120
120
if (timedOut == false ) {
121
121
for (final PublicationTarget target : publicationTargets ) {
122
- if (target .publicationTargetStateMachine . isActive ()) {
122
+ if (target .isActive ()) {
123
123
return isCompleted == false ;
124
124
}
125
125
}
@@ -135,10 +135,10 @@ private void onPossibleCommitFailure() {
135
135
136
136
final CoordinationState .VoteCollection possiblySuccessfulNodes = new CoordinationState .VoteCollection ();
137
137
for (PublicationTarget publicationTarget : publicationTargets ) {
138
- if (publicationTarget .publicationTargetStateMachine . mayCommitInFuture ()) {
138
+ if (publicationTarget .mayCommitInFuture ()) {
139
139
possiblySuccessfulNodes .addVote (publicationTarget .discoveryNode );
140
140
} else {
141
- assert publicationTarget .publicationTargetStateMachine . isFailed () : publicationTarget . publicationTargetStateMachine ;
141
+ assert publicationTarget .isFailed () : publicationTarget ;
142
142
}
143
143
}
144
144
@@ -180,84 +180,37 @@ enum PublicationTargetState {
180
180
APPLIED_COMMIT ,
181
181
}
182
182
183
- static class PublicationTargetStateMachine {
184
- private PublicationTargetState state = PublicationTargetState .NOT_STARTED ;
185
-
186
- public void setState (PublicationTargetState newState ) {
187
- switch (newState ) {
188
- case NOT_STARTED :
189
- assert false : state + " -> " + newState ;
190
- break ;
191
- case SENT_PUBLISH_REQUEST :
192
- assert state == PublicationTargetState .NOT_STARTED : state + " -> " + newState ;
193
- break ;
194
- case WAITING_FOR_QUORUM :
195
- assert state == PublicationTargetState .SENT_PUBLISH_REQUEST : state + " -> " + newState ;
196
- break ;
197
- case SENT_APPLY_COMMIT :
198
- assert state == PublicationTargetState .WAITING_FOR_QUORUM : state + " -> " + newState ;
199
- break ;
200
- case APPLIED_COMMIT :
201
- assert state == PublicationTargetState .SENT_APPLY_COMMIT : state + " -> " + newState ;
202
- break ;
203
- case FAILED :
204
- assert state != PublicationTargetState .APPLIED_COMMIT : state + " -> " + newState ;
205
- break ;
206
- }
207
- state = newState ;
208
- }
209
-
210
- public boolean isActive () {
211
- return state != PublicationTargetState .FAILED
212
- && state != PublicationTargetState .APPLIED_COMMIT ;
213
- }
214
-
215
- public boolean isWaitingForQuorum () {
216
- return state == PublicationTargetState .WAITING_FOR_QUORUM ;
217
- }
218
-
219
- public boolean mayCommitInFuture () {
220
- return (state == PublicationTargetState .NOT_STARTED
221
- || state == PublicationTargetState .SENT_PUBLISH_REQUEST
222
- || state == PublicationTargetState .WAITING_FOR_QUORUM );
223
- }
224
-
225
- public boolean isFailed () {
226
- return state == PublicationTargetState .FAILED ;
227
- }
228
-
229
- @ Override
230
- public String toString () {
231
- return state .toString ();
232
- }
233
- }
234
-
235
183
private class PublicationTarget {
236
184
private final DiscoveryNode discoveryNode ;
237
- private final PublicationTargetStateMachine publicationTargetStateMachine = new PublicationTargetStateMachine ();
238
185
private boolean ackIsPending = true ;
186
+ private PublicationTargetState state = PublicationTargetState .NOT_STARTED ;
239
187
240
188
private PublicationTarget (DiscoveryNode discoveryNode ) {
241
189
this .discoveryNode = discoveryNode ;
242
190
}
243
191
244
192
@ Override
245
193
public String toString () {
246
- return discoveryNode .getId ();
194
+ return "PublicationTarget{" +
195
+ "discoveryNode=" + discoveryNode +
196
+ ", state=" + state +
197
+ ", ackIsPending=" + ackIsPending +
198
+ '}' ;
247
199
}
248
200
249
201
public void sendPublishRequest () {
250
- if (publicationTargetStateMachine . isFailed ()) {
202
+ if (isFailed ()) {
251
203
return ;
252
204
}
253
- publicationTargetStateMachine .setState (PublicationTargetState .SENT_PUBLISH_REQUEST );
205
+ assert state == PublicationTargetState .NOT_STARTED : state + " -> " + PublicationTargetState .SENT_PUBLISH_REQUEST ;
206
+ state = PublicationTargetState .SENT_PUBLISH_REQUEST ;
254
207
Publication .this .sendPublishRequest (discoveryNode , publishRequest , new PublicationTarget .PublishResponseHandler ());
255
208
// TODO Can this ^ fail with an exception? Target should be failed if so.
256
209
assert publicationCompletedIffAllTargetsInactiveOrTimedOut ();
257
210
}
258
211
259
212
void handlePublishResponse (PublishResponse publishResponse ) {
260
- assert publicationTargetStateMachine . isWaitingForQuorum () : publicationTargetStateMachine ;
213
+ assert isWaitingForQuorum () : this ;
261
214
logger .trace ("handlePublishResponse: handling [{}] from [{}])" , publishResponse , discoveryNode );
262
215
if (applyCommitRequest .isPresent ()) {
263
216
sendApplyCommit ();
@@ -272,23 +225,22 @@ void handlePublishResponse(PublishResponse publishResponse) {
272
225
}
273
226
274
227
public void sendApplyCommit () {
275
- publicationTargetStateMachine .setState (PublicationTargetState .SENT_APPLY_COMMIT );
228
+ assert state == PublicationTargetState .WAITING_FOR_QUORUM : state + " -> " + PublicationTargetState .SENT_APPLY_COMMIT ;
229
+ state = PublicationTargetState .SENT_APPLY_COMMIT ;
276
230
assert applyCommitRequest .isPresent ();
277
231
Publication .this .sendApplyCommit (discoveryNode , applyCommitRequest .get (), new PublicationTarget .ApplyCommitResponseHandler ());
278
232
assert publicationCompletedIffAllTargetsInactiveOrTimedOut ();
279
233
}
280
234
281
- public boolean isWaitingForQuorum () {
282
- return publicationTargetStateMachine .isWaitingForQuorum ();
283
- }
284
-
285
- public boolean isActive () {
286
- return publicationTargetStateMachine .isActive ();
235
+ public void setAppliedCommit () {
236
+ assert state == PublicationTargetState .SENT_APPLY_COMMIT : state + " -> " + PublicationTargetState .APPLIED_COMMIT ;
237
+ state = PublicationTargetState .APPLIED_COMMIT ;
238
+ ackOnce (null );
287
239
}
288
240
289
241
public void setFailed (Exception e ) {
290
- assert isActive () ;
291
- publicationTargetStateMachine . setState ( PublicationTargetState .FAILED ) ;
242
+ assert state != PublicationTargetState . APPLIED_COMMIT : state + " -> " + PublicationTargetState . FAILED ;
243
+ state = PublicationTargetState .FAILED ;
292
244
ackOnce (e );
293
245
}
294
246
@@ -307,11 +259,30 @@ private void ackOnce(Exception e) {
307
259
}
308
260
}
309
261
262
+ public boolean isActive () {
263
+ return state != PublicationTargetState .FAILED
264
+ && state != PublicationTargetState .APPLIED_COMMIT ;
265
+ }
266
+
267
+ public boolean isWaitingForQuorum () {
268
+ return state == PublicationTargetState .WAITING_FOR_QUORUM ;
269
+ }
270
+
271
+ public boolean mayCommitInFuture () {
272
+ return (state == PublicationTargetState .NOT_STARTED
273
+ || state == PublicationTargetState .SENT_PUBLISH_REQUEST
274
+ || state == PublicationTargetState .WAITING_FOR_QUORUM );
275
+ }
276
+
277
+ public boolean isFailed () {
278
+ return state == PublicationTargetState .FAILED ;
279
+ }
280
+
310
281
private class PublishResponseHandler implements ActionListener <PublishWithJoinResponse > {
311
282
312
283
@ Override
313
284
public void onResponse (PublishWithJoinResponse response ) {
314
- if (publicationTargetStateMachine . isFailed ()) {
285
+ if (isFailed ()) {
315
286
logger .debug ("PublishResponseHandler.handleResponse: already failed, ignoring response from [{}]" , discoveryNode );
316
287
assert publicationCompletedIffAllTargetsInactiveOrTimedOut ();
317
288
return ;
@@ -320,7 +291,8 @@ public void onResponse(PublishWithJoinResponse response) {
320
291
// TODO: check if we need to pass the full response here or if it's sufficient to just pass the optional join.
321
292
onPossibleJoin (discoveryNode , response );
322
293
323
- publicationTargetStateMachine .setState (PublicationTargetState .WAITING_FOR_QUORUM );
294
+ assert state == PublicationTargetState .SENT_PUBLISH_REQUEST : state + " -> " + PublicationTargetState .WAITING_FOR_QUORUM ;
295
+ state = PublicationTargetState .WAITING_FOR_QUORUM ;
324
296
handlePublishResponse (response .getPublishResponse ());
325
297
326
298
assert publicationCompletedIffAllTargetsInactiveOrTimedOut ();
@@ -335,10 +307,9 @@ public void onFailure(Exception e) {
335
307
} else {
336
308
logger .debug (() -> new ParameterizedMessage ("PublishResponseHandler: [{}] failed" , discoveryNode ), exp );
337
309
}
338
- publicationTargetStateMachine . setState ( PublicationTargetState . FAILED );
310
+ setFailed ( e );
339
311
onPossibleCommitFailure ();
340
312
assert publicationCompletedIffAllTargetsInactiveOrTimedOut ();
341
- ackOnce (exp );
342
313
}
343
314
344
315
}
@@ -347,15 +318,14 @@ private class ApplyCommitResponseHandler implements ActionListener<TransportResp
347
318
348
319
@ Override
349
320
public void onResponse (TransportResponse .Empty ignored ) {
350
- if (publicationTargetStateMachine . isFailed ()) {
321
+ if (isFailed ()) {
351
322
logger .debug ("ApplyCommitResponseHandler.handleResponse: already failed, ignoring response from [{}]" ,
352
323
discoveryNode );
353
324
return ;
354
325
}
355
- publicationTargetStateMachine . setState ( PublicationTargetState . APPLIED_COMMIT );
326
+ setAppliedCommit ( );
356
327
onPossibleCompletion ();
357
328
assert publicationCompletedIffAllTargetsInactiveOrTimedOut ();
358
- ackOnce (null );
359
329
}
360
330
361
331
@ Override
@@ -367,10 +337,9 @@ public void onFailure(Exception e) {
367
337
} else {
368
338
logger .debug (() -> new ParameterizedMessage ("ApplyCommitResponseHandler: [{}] failed" , discoveryNode ), exp );
369
339
}
370
- publicationTargetStateMachine . setState ( PublicationTargetState . FAILED );
340
+ setFailed ( e );
371
341
onPossibleCompletion ();
372
342
assert publicationCompletedIffAllTargetsInactiveOrTimedOut ();
373
- ackOnce (exp );
374
343
}
375
344
}
376
345
}
0 commit comments