Skip to content

Commit 6ad9a67

Browse files
authored
Shift replayer to prefer io.Reader rather than filenames (#1234)
This allows us to move to `//go:embed` or use hard-coded strings (or any other data-source). Short-term plans are to use go:embed to read files once / not require runtime access to files, only build.
1 parent 610f39f commit 6ad9a67

File tree

2 files changed

+56
-17
lines changed

2 files changed

+56
-17
lines changed

internal/workflow_replayer.go

Lines changed: 34 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,10 @@ import (
2626
"encoding/json"
2727
"errors"
2828
"fmt"
29+
"io"
30+
"math"
31+
"os"
32+
2933
"github.com/golang/mock/gomock"
3034
"github.com/opentracing/opentracing-go"
3135
"github.com/pborman/uuid"
@@ -37,8 +41,6 @@ import (
3741
"go.uber.org/cadence/internal/common/backoff"
3842
"go.uber.org/cadence/internal/common/serializer"
3943
"go.uber.org/zap"
40-
"io/ioutil"
41-
"math"
4244
)
4345

4446
const (
@@ -133,19 +135,12 @@ func (r *WorkflowReplayer) ReplayWorkflowHistory(logger *zap.Logger, history *sh
133135
return r.replayWorkflowHistory(logger, service, replayDomainName, nil, history, nil)
134136
}
135137

136-
// ReplayWorkflowHistoryFromJSONFile executes a single decision task for the given json history file.
137-
// Use for testing the backwards compatibility of code changes and troubleshooting workflows in a debugger.
138-
// The logger is an optional parameter. Defaults to the noop logger.
139-
func (r *WorkflowReplayer) ReplayWorkflowHistoryFromJSONFile(logger *zap.Logger, jsonfileName string) error {
140-
return r.ReplayPartialWorkflowHistoryFromJSONFile(logger, jsonfileName, 0)
138+
func (r *WorkflowReplayer) ReplayWorkflowHistoryFromJSON(logger *zap.Logger, reader io.Reader) error {
139+
return r.ReplayPartialWorkflowHistoryFromJSON(logger, reader, 0)
141140
}
142141

143-
// ReplayPartialWorkflowHistoryFromJSONFile executes a single decision task for the given json history file up to provided
144-
// lastEventID(inclusive).
145-
// Use for testing backwards compatibility of code changes and troubleshooting workflows in a debugger.
146-
// The logger is an optional parameter. Defaults to the noop logger.
147-
func (r *WorkflowReplayer) ReplayPartialWorkflowHistoryFromJSONFile(logger *zap.Logger, jsonfileName string, lastEventID int64) error {
148-
history, err := extractHistoryFromFile(jsonfileName, lastEventID)
142+
func (r *WorkflowReplayer) ReplayPartialWorkflowHistoryFromJSON(logger *zap.Logger, reader io.Reader, lastEventID int64) error {
143+
history, err := extractHistoryFromReader(reader, lastEventID)
149144

150145
if err != nil {
151146
return err
@@ -162,6 +157,28 @@ func (r *WorkflowReplayer) ReplayPartialWorkflowHistoryFromJSONFile(logger *zap.
162157
return r.replayWorkflowHistory(logger, service, replayDomainName, nil, history, nil)
163158
}
164159

160+
// ReplayWorkflowHistoryFromJSONFile executes a single decision task for the given json history file.
161+
// Use for testing the backwards compatibility of code changes and troubleshooting workflows in a debugger.
162+
// The logger is an optional parameter. Defaults to the noop logger.
163+
func (r *WorkflowReplayer) ReplayWorkflowHistoryFromJSONFile(logger *zap.Logger, jsonfileName string) error {
164+
return r.ReplayPartialWorkflowHistoryFromJSONFile(logger, jsonfileName, 0)
165+
}
166+
167+
// ReplayPartialWorkflowHistoryFromJSONFile executes a single decision task for the given json history file up to provided
168+
// lastEventID(inclusive).
169+
// Use for testing backwards compatibility of code changes and troubleshooting workflows in a debugger.
170+
// The logger is an optional parameter. Defaults to the noop logger.
171+
func (r *WorkflowReplayer) ReplayPartialWorkflowHistoryFromJSONFile(logger *zap.Logger, jsonfileName string, lastEventID int64) error {
172+
file, err := os.Open(jsonfileName)
173+
if err != nil {
174+
return fmt.Errorf("could not open file: %w", err)
175+
}
176+
defer func() {
177+
_ = file.Close()
178+
}()
179+
return r.ReplayPartialWorkflowHistoryFromJSON(logger, file, lastEventID)
180+
}
181+
165182
// ReplayWorkflowExecution replays workflow execution loading it from Cadence service.
166183
// The logger is an optional parameter. Defaults to the noop logger.
167184
func (r *WorkflowReplayer) ReplayWorkflowExecution(
@@ -336,17 +353,17 @@ func (r *WorkflowReplayer) replayWorkflowHistory(
336353
return fmt.Errorf("replay workflow doesn't return the same result as the last event, resp: %v, last: %v", resp, last)
337354
}
338355

339-
func extractHistoryFromFile(jsonfileName string, lastEventID int64) (*shared.History, error) {
340-
raw, err := ioutil.ReadFile(jsonfileName)
356+
func extractHistoryFromReader(r io.Reader, lastEventID int64) (*shared.History, error) {
357+
raw, err := io.ReadAll(r)
341358
if err != nil {
342-
return nil, err
359+
return nil, fmt.Errorf("failed to read data: %w", err)
343360
}
344361

345362
var deserializedEvents []*shared.HistoryEvent
346363
err = json.Unmarshal(raw, &deserializedEvents)
347364

348365
if err != nil {
349-
return nil, err
366+
return nil, fmt.Errorf("invalid json contents: %w", err)
350367
}
351368

352369
if lastEventID <= 0 {

worker/worker.go

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ package worker
2424

2525
import (
2626
"context"
27+
"io"
2728

2829
"go.uber.org/cadence/.gen/go/cadence/workflowserviceclient"
2930
"go.uber.org/cadence/.gen/go/shared"
@@ -151,6 +152,8 @@ type (
151152
// See https://github.com/uber/cadence/blob/master/tools/cli/README.md for full documentation
152153
// Use for testing the backwards compatibility of code changes and troubleshooting workflows in a debugger.
153154
// The logger is an optional parameter. Defaults to the noop logger.
155+
//
156+
// Deprecated: prefer ReplayWorkflowHistoryFromJSON
154157
ReplayWorkflowHistoryFromJSONFile(logger *zap.Logger, jsonfileName string) error
155158

156159
// ReplayPartialWorkflowHistoryFromJSONFile executes a single decision task for the json history file upto provided
@@ -159,12 +162,31 @@ type (
159162
// See https://github.com/uber/cadence/blob/master/tools/cli/README.md for full documentation
160163
// Use for testing the backwards compatibility of code changes and troubleshooting workflows in a debugger.
161164
// The logger is an optional parameter. Defaults to the noop logger.
165+
//
166+
// Deprecated: prefer ReplayPartialWorkflowHistoryFromJSON
162167
ReplayPartialWorkflowHistoryFromJSONFile(logger *zap.Logger, jsonfileName string, lastEventID int64) error
163168

164169
// ReplayWorkflowExecution loads a workflow execution history from the Cadence service and executes a single decision task for it.
165170
// Use for testing the backwards compatibility of code changes and troubleshooting workflows in a debugger.
166171
// The logger is the only optional parameter. Defaults to the noop logger.
167172
ReplayWorkflowExecution(ctx context.Context, service workflowserviceclient.Interface, logger *zap.Logger, domain string, execution workflow.Execution) error
173+
174+
// ReplayWorkflowHistoryFromJSON executes a single decision task for the json history file downloaded from the cli.
175+
// To download the history file:
176+
// cadence workflow showid <workflow_id> -of <output_filename>
177+
// See https://github.com/uber/cadence/blob/master/tools/cli/README.md for full documentation
178+
// Use for testing the backwards compatibility of code changes and troubleshooting workflows in a debugger.
179+
// The logger is an optional parameter. Defaults to the noop logger.
180+
ReplayWorkflowHistoryFromJSON(logger *zap.Logger, reader io.Reader) error
181+
182+
// ReplayPartialWorkflowHistoryFromJSON executes a single decision task for the json history file upto provided
183+
// lastEventID(inclusive), downloaded from the cli.
184+
// To download the history file:
185+
// cadence workflow showid <workflow_id> -of <output_filename>
186+
// See https://github.com/uber/cadence/blob/master/tools/cli/README.md for full documentation
187+
// Use for testing the backwards compatibility of code changes and troubleshooting workflows in a debugger.
188+
// The logger is an optional parameter. Defaults to the noop logger.
189+
ReplayPartialWorkflowHistoryFromJSON(logger *zap.Logger, reader io.Reader, lastEventID int64) error
168190
}
169191

170192
// WorkflowShadower retrieves and replays workflow history from Cadence service to determine if there's any nondeterministic changes in the workflow definition

0 commit comments

Comments
 (0)