Skip to content

Commit 707b47a

Browse files
committed
Merge branch 'master' into newer-tools
2 parents 54cedf4 + 5454503 commit 707b47a

File tree

2 files changed

+65
-16
lines changed

2 files changed

+65
-16
lines changed

internal/workflow_replayer.go

Lines changed: 42 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,9 @@ import (
2626
"encoding/json"
2727
"errors"
2828
"fmt"
29-
"io/ioutil"
29+
"io"
3030
"math"
31+
"os"
3132

3233
"github.com/golang/mock/gomock"
3334
"github.com/opentracing/opentracing-go"
@@ -120,6 +121,16 @@ func (r *WorkflowReplayer) RegisterWorkflowWithOptions(w interface{}, options Re
120121
r.registry.RegisterWorkflowWithOptions(w, options)
121122
}
122123

124+
// RegisterActivity registers an activity function for this replayer
125+
func (r *WorkflowReplayer) RegisterActivity(a interface{}) {
126+
r.registry.RegisterActivity(a)
127+
}
128+
129+
// RegisterActivityWithOptions registers an activity function for this replayer with custom options, e.g. an explicit name.
130+
func (r *WorkflowReplayer) RegisterActivityWithOptions(a interface{}, options RegisterActivityOptions) {
131+
r.registry.RegisterActivityWithOptions(a, options)
132+
}
133+
123134
// ReplayWorkflowHistory executes a single decision task for the given history.
124135
// Use for testing backwards compatibility of code changes and troubleshooting workflows in a debugger.
125136
// The logger is an optional parameter. Defaults to the noop logger.
@@ -135,19 +146,12 @@ func (r *WorkflowReplayer) ReplayWorkflowHistory(logger *zap.Logger, history *sh
135146
return r.replayWorkflowHistory(logger, service, replayDomainName, nil, history, nil)
136147
}
137148

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

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

152156
if err != nil {
153157
return err
@@ -164,6 +168,28 @@ func (r *WorkflowReplayer) ReplayPartialWorkflowHistoryFromJSONFile(logger *zap.
164168
return r.replayWorkflowHistory(logger, service, replayDomainName, nil, history, nil)
165169
}
166170

171+
// ReplayWorkflowHistoryFromJSONFile executes a single decision task for the given json history file.
172+
// Use for testing the backwards compatibility of code changes and troubleshooting workflows in a debugger.
173+
// The logger is an optional parameter. Defaults to the noop logger.
174+
func (r *WorkflowReplayer) ReplayWorkflowHistoryFromJSONFile(logger *zap.Logger, jsonfileName string) error {
175+
return r.ReplayPartialWorkflowHistoryFromJSONFile(logger, jsonfileName, 0)
176+
}
177+
178+
// ReplayPartialWorkflowHistoryFromJSONFile executes a single decision task for the given json history file up to provided
179+
// lastEventID(inclusive).
180+
// Use for testing backwards compatibility of code changes and troubleshooting workflows in a debugger.
181+
// The logger is an optional parameter. Defaults to the noop logger.
182+
func (r *WorkflowReplayer) ReplayPartialWorkflowHistoryFromJSONFile(logger *zap.Logger, jsonfileName string, lastEventID int64) error {
183+
file, err := os.Open(jsonfileName)
184+
if err != nil {
185+
return fmt.Errorf("could not open file: %w", err)
186+
}
187+
defer func() {
188+
_ = file.Close()
189+
}()
190+
return r.ReplayPartialWorkflowHistoryFromJSON(logger, file, lastEventID)
191+
}
192+
167193
// ReplayWorkflowExecution replays workflow execution loading it from Cadence service.
168194
// The logger is an optional parameter. Defaults to the noop logger.
169195
func (r *WorkflowReplayer) ReplayWorkflowExecution(
@@ -338,17 +364,17 @@ func (r *WorkflowReplayer) replayWorkflowHistory(
338364
return fmt.Errorf("replay workflow doesn't return the same result as the last event, resp: %v, last: %v", resp, last)
339365
}
340366

341-
func extractHistoryFromFile(jsonfileName string, lastEventID int64) (*shared.History, error) {
342-
raw, err := ioutil.ReadFile(jsonfileName)
367+
func extractHistoryFromReader(r io.Reader, lastEventID int64) (*shared.History, error) {
368+
raw, err := io.ReadAll(r)
343369
if err != nil {
344-
return nil, err
370+
return nil, fmt.Errorf("failed to read data: %w", err)
345371
}
346372

347373
var deserializedEvents []*shared.HistoryEvent
348374
err = json.Unmarshal(raw, &deserializedEvents)
349375

350376
if err != nil {
351-
return nil, err
377+
return nil, fmt.Errorf("invalid json contents: %w", err)
352378
}
353379

354380
if lastEventID <= 0 {

worker/worker.go

Lines changed: 23 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/zap"
2930

@@ -141,6 +142,7 @@ type (
141142
// to ensure that new deployments are not going to break open workflows.
142143
WorkflowReplayer interface {
143144
WorkflowRegistry
145+
ActivityRegistry
144146

145147
// ReplayWorkflowHistory executes a single decision task for the given json history file.
146148
// Use for testing the backwards compatibility of code changes and troubleshooting workflows in a debugger.
@@ -152,6 +154,8 @@ type (
152154
// See https://github.com/uber/cadence/blob/master/tools/cli/README.md for full documentation
153155
// Use for testing the backwards compatibility of code changes and troubleshooting workflows in a debugger.
154156
// The logger is an optional parameter. Defaults to the noop logger.
157+
//
158+
// Deprecated: prefer ReplayWorkflowHistoryFromJSON
155159
ReplayWorkflowHistoryFromJSONFile(logger *zap.Logger, jsonfileName string) error
156160

157161
// ReplayPartialWorkflowHistoryFromJSONFile executes a single decision task for the json history file upto provided
@@ -160,12 +164,31 @@ type (
160164
// See https://github.com/uber/cadence/blob/master/tools/cli/README.md for full documentation
161165
// Use for testing the backwards compatibility of code changes and troubleshooting workflows in a debugger.
162166
// The logger is an optional parameter. Defaults to the noop logger.
167+
//
168+
// Deprecated: prefer ReplayPartialWorkflowHistoryFromJSON
163169
ReplayPartialWorkflowHistoryFromJSONFile(logger *zap.Logger, jsonfileName string, lastEventID int64) error
164170

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

171194
// 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)