@@ -138,11 +138,14 @@ type (
138
138
}
139
139
140
140
historyIteratorImpl struct {
141
- iteratorFunc func (nextPageToken []byte ) (* historypb.History , []byte , error )
142
- execution * commonpb.WorkflowExecution
143
- nextPageToken []byte
144
- namespace string
145
- service workflowservice.WorkflowServiceClient
141
+ iteratorFunc func (nextPageToken []byte ) (* historypb.History , []byte , error )
142
+ execution * commonpb.WorkflowExecution
143
+ nextPageToken []byte
144
+ namespace string
145
+ service workflowservice.WorkflowServiceClient
146
+ // maxEventID is the maximum eventID that the history iterator is expected to return.
147
+ // 0 means that the iterator will return all history events.
148
+ maxEventID int64
146
149
metricsHandler metrics.Handler
147
150
taskQueue string
148
151
}
@@ -869,6 +872,7 @@ func (wtp *workflowTaskPoller) toWorkflowTask(response *workflowservice.PollWork
869
872
nextPageToken : response .NextPageToken ,
870
873
namespace : wtp .namespace ,
871
874
service : wtp .service ,
875
+ maxEventID : response .GetStartedEventId (),
872
876
metricsHandler : wtp .metricsHandler ,
873
877
taskQueue : wtp .taskQueueName ,
874
878
}
@@ -886,6 +890,7 @@ func (h *historyIteratorImpl) GetNextPage() (*historypb.History, error) {
886
890
h .service ,
887
891
h .namespace ,
888
892
h .execution ,
893
+ h .maxEventID ,
889
894
h .metricsHandler ,
890
895
h .taskQueue ,
891
896
)
@@ -912,6 +917,7 @@ func newGetHistoryPageFunc(
912
917
service workflowservice.WorkflowServiceClient ,
913
918
namespace string ,
914
919
execution * commonpb.WorkflowExecution ,
920
+ lastEventID int64 ,
915
921
metricsHandler metrics.Handler ,
916
922
taskQueue string ,
917
923
) func (nextPageToken []byte ) (* historypb.History , []byte , error ) {
@@ -941,6 +947,19 @@ func newGetHistoryPageFunc(
941
947
} else {
942
948
h = resp .History
943
949
}
950
+
951
+ size := len (h .Events )
952
+ // While the SDK is processing a workflow task, the workflow task could timeout and server would start
953
+ // a new workflow task or the server looses the workflow task if it is a speculative workflow task. In either
954
+ // case, the new workflow task could have events that are beyond the last event ID that the SDK expects to process.
955
+ // In such cases, the SDK should return error indicating that the workflow task is stale since the result will not be used.
956
+ if size > 0 && lastEventID > 0 &&
957
+ h .Events [size - 1 ].GetEventId () > lastEventID {
958
+ return nil , nil , fmt .Errorf ("history contains events past expected last event ID (%v) " +
959
+ "likely this means the current workflow task is no longer valid" , lastEventID )
960
+
961
+ }
962
+
944
963
return h , resp .NextPageToken , nil
945
964
}
946
965
}
0 commit comments