20
20
import com .uber .cadence .ActivityTaskCanceledEventAttributes ;
21
21
import com .uber .cadence .ActivityTaskCompletedEventAttributes ;
22
22
import com .uber .cadence .ActivityTaskFailedEventAttributes ;
23
- import com .uber .cadence .ActivityTaskStartedEventAttributes ;
24
23
import com .uber .cadence .ActivityTaskTimedOutEventAttributes ;
25
24
import com .uber .cadence .ActivityType ;
26
25
import com .uber .cadence .HistoryEvent ;
@@ -37,30 +36,35 @@ final class ActivityDecisionContext {
37
36
38
37
private final class ActivityCancellationHandler implements Consumer <Exception > {
39
38
39
+ private final long scheduledEventId ;
40
+
40
41
private final String activityId ;
41
42
42
43
private final BiConsumer <byte [], Exception > callback ;
43
44
44
45
private ActivityCancellationHandler (
45
- String activityId , BiConsumer <byte [], Exception > callaback ) {
46
+ long scheduledEventId , String activityId , BiConsumer <byte [], Exception > callaback ) {
47
+ this .scheduledEventId = scheduledEventId ;
46
48
this .activityId = activityId ;
47
49
this .callback = callaback ;
48
50
}
49
51
50
52
@ Override
51
53
public void accept (Exception cause ) {
52
- if (!scheduledActivities .containsKey (activityId )) {
54
+ if (!scheduledActivities .containsKey (scheduledEventId )) {
53
55
// Cancellation handlers are not deregistered. So they fire after an activity completion.
54
56
return ;
55
57
}
56
58
decisions .requestCancelActivityTask (
57
- activityId ,
59
+ scheduledEventId ,
58
60
() -> {
59
61
OpenRequestInfo <byte [], ActivityType > scheduled =
60
- scheduledActivities .remove (activityId );
62
+ scheduledActivities .remove (scheduledEventId );
61
63
if (scheduled == null ) {
62
64
throw new IllegalArgumentException (
63
- "Activity \" " + activityId + "\" wasn't scheduled" );
65
+ String .format (
66
+ "Activity with activityId=%s and scheduledEventId=%d wasn't found" ,
67
+ activityId , scheduledEventId ));
64
68
}
65
69
callback .accept (null , new CancellationException ("Cancelled by request" ));
66
70
});
@@ -69,7 +73,8 @@ public void accept(Exception cause) {
69
73
70
74
private final DecisionsHelper decisions ;
71
75
72
- private final Map <String , OpenRequestInfo <byte [], ActivityType >> scheduledActivities =
76
+ // key is scheduledEventId
77
+ private final Map <Long , OpenRequestInfo <byte [], ActivityType >> scheduledActivities =
73
78
new HashMap <>();
74
79
75
80
ActivityDecisionContext (DecisionsHelper decisions ) {
@@ -106,21 +111,19 @@ Consumer<Exception> scheduleActivityTask(
106
111
tl .setName (taskList );
107
112
attributes .setTaskList (tl );
108
113
}
109
- decisions .scheduleActivityTask (attributes );
114
+ long scheduledEventId = decisions .scheduleActivityTask (attributes );
110
115
context .setCompletionHandle (callback );
111
- scheduledActivities .put (attributes . getActivityId () , context );
116
+ scheduledActivities .put (scheduledEventId , context );
112
117
return new ActivityDecisionContext .ActivityCancellationHandler (
113
- attributes .getActivityId (), callback );
118
+ scheduledEventId , attributes .getActivityId (), callback );
114
119
}
115
120
116
- void handleActivityTaskStarted (ActivityTaskStartedEventAttributes attributes ) {}
117
-
118
121
void handleActivityTaskCanceled (HistoryEvent event ) {
119
122
ActivityTaskCanceledEventAttributes attributes = event .getActivityTaskCanceledEventAttributes ();
120
- String activityId = decisions .getActivityId (attributes );
121
123
if (decisions .handleActivityTaskCanceled (event )) {
122
124
CancellationException e = new CancellationException ();
123
- OpenRequestInfo <byte [], ActivityType > scheduled = scheduledActivities .remove (activityId );
125
+ OpenRequestInfo <byte [], ActivityType > scheduled =
126
+ scheduledActivities .remove (attributes .getScheduledEventId ());
124
127
if (scheduled != null ) {
125
128
BiConsumer <byte [], Exception > completionHandle = scheduled .getCompletionCallback ();
126
129
// It is OK to fail with subclass of CancellationException when cancellation requested.
@@ -134,9 +137,9 @@ void handleActivityTaskCanceled(HistoryEvent event) {
134
137
void handleActivityTaskCompleted (HistoryEvent event ) {
135
138
ActivityTaskCompletedEventAttributes attributes =
136
139
event .getActivityTaskCompletedEventAttributes ();
137
- String activityId = decisions .getActivityId (attributes );
138
- if ( decisions . handleActivityTaskClosed ( activityId )) {
139
- OpenRequestInfo < byte [], ActivityType > scheduled = scheduledActivities .remove (activityId );
140
+ if ( decisions .handleActivityTaskClosed (attributes . getScheduledEventId ())) {
141
+ OpenRequestInfo < byte [], ActivityType > scheduled =
142
+ scheduledActivities .remove (attributes . getScheduledEventId () );
140
143
if (scheduled != null ) {
141
144
byte [] result = attributes .getResult ();
142
145
BiConsumer <byte [], Exception > completionHandle = scheduled .getCompletionCallback ();
@@ -147,15 +150,15 @@ void handleActivityTaskCompleted(HistoryEvent event) {
147
150
148
151
void handleActivityTaskFailed (HistoryEvent event ) {
149
152
ActivityTaskFailedEventAttributes attributes = event .getActivityTaskFailedEventAttributes ();
150
- String activityId = decisions .getActivityId (attributes );
151
- if ( decisions . handleActivityTaskClosed ( activityId )) {
152
- OpenRequestInfo < byte [], ActivityType > scheduled = scheduledActivities .remove (activityId );
153
+ if ( decisions .handleActivityTaskClosed (attributes . getScheduledEventId ())) {
154
+ OpenRequestInfo < byte [], ActivityType > scheduled =
155
+ scheduledActivities .remove (attributes . getScheduledEventId () );
153
156
if (scheduled != null ) {
154
157
String reason = attributes .getReason ();
155
158
byte [] details = attributes .getDetails ();
156
159
ActivityTaskFailedException failure =
157
160
new ActivityTaskFailedException (
158
- event .getEventId (), scheduled .getUserContext (), activityId , reason , details );
161
+ event .getEventId (), scheduled .getUserContext (), null , reason , details );
159
162
BiConsumer <byte [], Exception > completionHandle = scheduled .getCompletionCallback ();
160
163
completionHandle .accept (null , failure );
161
164
}
@@ -164,15 +167,15 @@ void handleActivityTaskFailed(HistoryEvent event) {
164
167
165
168
void handleActivityTaskTimedOut (HistoryEvent event ) {
166
169
ActivityTaskTimedOutEventAttributes attributes = event .getActivityTaskTimedOutEventAttributes ();
167
- String activityId = decisions .getActivityId (attributes );
168
- if ( decisions . handleActivityTaskClosed ( activityId )) {
169
- OpenRequestInfo < byte [], ActivityType > scheduled = scheduledActivities .remove (activityId );
170
+ if ( decisions .handleActivityTaskClosed (attributes . getScheduledEventId ())) {
171
+ OpenRequestInfo < byte [], ActivityType > scheduled =
172
+ scheduledActivities .remove (attributes . getScheduledEventId () );
170
173
if (scheduled != null ) {
171
174
TimeoutType timeoutType = attributes .getTimeoutType ();
172
175
byte [] details = attributes .getDetails ();
173
176
ActivityTaskTimeoutException failure =
174
177
new ActivityTaskTimeoutException (
175
- event .getEventId (), scheduled .getUserContext (), activityId , timeoutType , details );
178
+ event .getEventId (), scheduled .getUserContext (), null , timeoutType , details );
176
179
BiConsumer <byte [], Exception > completionHandle = scheduled .getCompletionCallback ();
177
180
completionHandle .accept (null , failure );
178
181
}
0 commit comments