mirror of
https://github.com/hanzoai/tasks.git
synced 2026-11-03 03:56:04 +00:00
This is a re-revert of https://github.com/temporalio/temporal/pull/5797 now that all changes moving forward will be part of 1.25 release. ## Why? The new logic to proxy all FE history read operations to the history service has been part of release 1.23 and it is now safe (w.r.t. FE and history services deployment ordering) as part of release 1.25 to start deprecating the legacy code.
89 lines
3.2 KiB
Go
89 lines
3.2 KiB
Go
// The MIT License
|
|
//
|
|
// Copyright (c) 2020 Temporal Technologies Inc. All rights reserved.
|
|
//
|
|
// Copyright (c) 2020 Uber Technologies, Inc.
|
|
//
|
|
// Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
// of this software and associated documentation files (the "Software"), to deal
|
|
// in the Software without restriction, including without limitation the rights
|
|
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
// copies of the Software, and to permit persons to whom the Software is
|
|
// furnished to do so, subject to the following conditions:
|
|
//
|
|
// The above copyright notice and this permission notice shall be included in
|
|
// all copies or substantial portions of the Software.
|
|
//
|
|
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
// THE SOFTWARE.
|
|
|
|
package events
|
|
|
|
import (
|
|
"fmt"
|
|
|
|
historypb "go.temporal.io/api/history/v1"
|
|
"go.temporal.io/api/serviceerror"
|
|
)
|
|
|
|
func VerifyHistoryIsComplete(
|
|
events []*historypb.HistoryEvent,
|
|
expectedFirstEventID int64,
|
|
expectedLastEventID int64,
|
|
isFirstPage bool,
|
|
isLastPage bool,
|
|
pageSize int,
|
|
) error {
|
|
|
|
nEvents := len(events)
|
|
if nEvents == 0 {
|
|
if isLastPage {
|
|
// we seem to be returning a non-nil pageToken on the lastPage which
|
|
// in turn cases the client to call getHistory again - only to find
|
|
// there are no more events to consume - bail out if this is the case here
|
|
return nil
|
|
}
|
|
return serviceerror.NewDataLoss("History contains zero events.")
|
|
}
|
|
|
|
firstEventID := events[0].GetEventId()
|
|
lastEventID := events[nEvents-1].GetEventId()
|
|
|
|
if !isFirstPage { // at least one page of history has been read previously
|
|
if firstEventID <= expectedFirstEventID {
|
|
// not first page and no events have been read in the previous pages - not possible
|
|
return serviceerror.NewDataLoss(fmt.Sprintf("Invalid history: expected first eventID to be > %v but got %v", expectedFirstEventID, firstEventID))
|
|
}
|
|
expectedFirstEventID = firstEventID
|
|
}
|
|
|
|
if !isLastPage {
|
|
// estimate lastEventID based on pageSize. This is a lower bound
|
|
// since the persistence layer counts "batch of events" as a single page
|
|
expectedLastEventID = expectedFirstEventID + int64(pageSize) - 1
|
|
}
|
|
|
|
nExpectedEvents := expectedLastEventID - expectedFirstEventID + 1
|
|
|
|
if firstEventID == expectedFirstEventID &&
|
|
((isLastPage && lastEventID == expectedLastEventID && int64(nEvents) == nExpectedEvents) ||
|
|
(!isLastPage && lastEventID >= expectedLastEventID && int64(nEvents) >= nExpectedEvents)) {
|
|
return nil
|
|
}
|
|
|
|
return serviceerror.NewDataLoss(fmt.Sprintf("Incomplete history: expected events [%v-%v] but got events [%v-%v] of length %v: isFirstPage=%v,isLastPage=%v,pageSize=%v",
|
|
expectedFirstEventID,
|
|
expectedLastEventID,
|
|
firstEventID,
|
|
lastEventID,
|
|
nEvents,
|
|
isFirstPage,
|
|
isLastPage,
|
|
pageSize))
|
|
}
|