Skip to content

Commit

Permalink
Address review comments
Browse files Browse the repository at this point in the history
  • Loading branch information
tdeebswihart committed Oct 30, 2023
1 parent 3709d8e commit 734eb22
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 20 deletions.
21 changes: 1 addition & 20 deletions client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@ import (
enumspb "go.temporal.io/api/enums/v1"
historypb "go.temporal.io/api/history/v1"
"go.temporal.io/api/operatorservice/v1"
"go.temporal.io/api/temporalproto"
"go.temporal.io/api/workflowservice/v1"

"go.temporal.io/sdk/converter"
Expand Down Expand Up @@ -750,23 +749,5 @@ type HistoryJSONOptions struct {
// HistoryFromJSON deserializes history from a reader of JSON bytes. This does
// not close the reader if it is closeable.
func HistoryFromJSON(r io.Reader, options HistoryJSONOptions) (*historypb.History, error) {
hist := &historypb.History{}
// We set DiscardUnknown here because the history may have been created by a previous
// version of our protos
dec := temporalproto.NewJSONDecoder(r, true)
if err := dec.Decode(hist); err != nil {
return nil, err
}

// If there is a last event ID, slice the rest off
if options.LastEventID > 0 {
for i, event := range hist.Events {
if event.EventId == options.LastEventID {
// Inclusive
hist.Events = hist.Events[:i+1]
break
}
}
}
return hist, nil
return internal.HistoryFromJSON(r, options.LastEventID)
}
30 changes: 30 additions & 0 deletions internal/internal_worker.go
Original file line number Diff line number Diff line change
Expand Up @@ -222,6 +222,12 @@ type (

capabilities *workflowservice.GetSystemInfoResponse_Capabilities
}

// HistoryJSONOptions are options for HistoryFromJSON.
HistoryJSONOptions struct {
// LastEventID, if set, will only load history up to this ID (inclusive).
LastEventID int64
}
)

var debugMode = os.Getenv("TEMPORAL_DEBUG") != ""
Expand Down Expand Up @@ -1421,6 +1427,30 @@ func (aw *WorkflowReplayer) replayWorkflowHistory(logger log.Logger, service wor
return fmt.Errorf("replay workflow doesn't return the same result as the last event, resp: %[1]T{%[1]v}, last: %[2]T{%[2]v}", resp, resp, last)
}

// HistoryFromJSON deserializes history from a reader of JSON bytes. This does
// not close the reader if it is closeable.
func HistoryFromJSON(r io.Reader, lastEventID int64) (*historypb.History, error) {
hist := &historypb.History{}
// We set DiscardUnknown here because the history may have been created by a previous
// version of our protos
dec := temporalproto.NewJSONDecoder(r, true)
if err := dec.Decode(hist); err != nil {
return nil, err
}

// If there is a last event ID, slice the rest off
if lastEventID > 0 {
for i, event := range hist.Events {
if event.EventId == lastEventID {
// Inclusive
hist.Events = hist.Events[:i+1]
break
}
}
}
return hist, nil
}

func extractHistoryFromFile(jsonfileName string, lastEventID int64) (*historypb.History, error) {
reader, err := os.Open(jsonfileName)
if err != nil {
Expand Down
18 changes: 18 additions & 0 deletions internal/internal_worker_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2789,6 +2789,24 @@ func TestWorkerBuildIDAndSessionPanic(t *testing.T) {
require.Equal(t, "cannot set both EnableSessionWorker and UseBuildIDForVersioning", recovered)
}

func TestHistoryFromJSON(t *testing.T) {
// Load sample history and just make sure it has the right event count
r, err := os.Open("testdata/sampleHistory.json")
require.NoError(t, err)
hist, err := HistoryFromJSON(r, 0)
require.NoError(t, err)
require.NoError(t, r.Close())
require.Len(t, hist.Events, 11)

// Only load up through event 5 and confirm
r, err = os.Open("testdata/sampleHistory.json")
require.NoError(t, err)
hist, err = HistoryFromJSON(r, 5)
require.NoError(t, err)
require.NoError(t, r.Close())
require.Len(t, hist.Events, 5)
}

func aliasNameClash1(context.Context) (string, error) { return "func1", nil }
func aliasNameClash2(context.Context) (string, error) { return "func2", nil }

Expand Down

0 comments on commit 734eb22

Please sign in to comment.