Skip to content

Commit

Permalink
Ut conflict resolver (#806)
Browse files Browse the repository at this point in the history
Correctly set replication state after resetting workflow execution history.

TransferQueue and TimerQueue processor rely on LastWriteVersion rather
than CurrentVersion on mutable state for validating tasks.  Resetting
history after conflict resolution could result in tripping up the processing
logic if relied on CurrentVersion.

Unit test for conflictResolver.

More logging on historyReplicator.
  • Loading branch information
wxing1292 authored and samarabbas committed Jun 5, 2018
1 parent 772d653 commit 166ef58
Show file tree
Hide file tree
Showing 12 changed files with 395 additions and 43 deletions.
4 changes: 4 additions & 0 deletions common/logging/tags.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,9 @@ const (
TagOffset = "offset"
TagScope = "scope"
TagFailover = "failover"
TagVersion = "version"
TagFirstEventID = "first-event-id"
TagNextEventID = "next-event-id"

// workflow logging tag values
// TagWorkflowComponent Values
Expand All @@ -63,6 +66,7 @@ const (
TagValueMatchingEngineComponent = "matching-engine"
TagValueReplicatorComponent = "replicator"
TagValueReplicationTaskProcessorComponent = "replication-task-processor"
TagValueHistoryReplicatorComponent = "history-replicator"

// TagHistoryBuilderAction values
TagValueActionWorkflowStarted = "add-workflowexecution-started-event"
Expand Down
18 changes: 12 additions & 6 deletions service/history/conflictResolver.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,16 +23,17 @@ package history
import (
"time"

"github.com/pborman/uuid"
"github.com/uber-common/bark"
"github.com/uber/cadence/.gen/go/shared"
"github.com/uber/cadence/common"
"github.com/uber/cadence/common/cluster"
"github.com/uber/cadence/common/persistence"
)

type (
conflictResolver struct {
shard ShardContext
clusterMetadata cluster.Metadata
context *workflowExecutionContext
historyMgr persistence.HistoryManager
hSerializerFactory persistence.HistorySerializerFactory
Expand All @@ -45,14 +46,15 @@ func newConflictResolver(shard ShardContext, context *workflowExecutionContext,

return &conflictResolver{
shard: shard,
clusterMetadata: shard.GetService().GetClusterMetadata(),
context: context,
historyMgr: historyMgr,
hSerializerFactory: persistence.NewHistorySerializerFactory(),
logger: logger,
}
}

func (r *conflictResolver) reset(replayEventID int64, startTime time.Time) (*mutableStateBuilder, error) {
func (r *conflictResolver) reset(requestID string, replayEventID int64, startTime time.Time) (*mutableStateBuilder, error) {
domainID := r.context.domainID
execution := r.context.workflowExecution
replayNextEventID := replayEventID + 1
Expand All @@ -62,8 +64,8 @@ func (r *conflictResolver) reset(replayEventID int64, startTime time.Time) (*mut
var resetMutableStateBuilder *mutableStateBuilder
var sBuilder *stateBuilder
var lastFirstEventID int64
var lastEvent *shared.HistoryEvent
eventsToApply := replayNextEventID - common.FirstEventID
requestID := uuid.New()
for hasMore := true; hasMore; hasMore = len(nextPageToken) > 0 {
history, nextPageToken, lastFirstEventID, err = r.getHistory(domainID, execution, common.FirstEventID,
replayNextEventID, nextPageToken)
Expand All @@ -80,21 +82,22 @@ func (r *conflictResolver) reset(replayEventID int64, startTime time.Time) (*mut
history.Events = history.Events[0:eventsToApply]
}

eventsToApply -= batchSize
eventsToApply -= int64(len(history.Events))

if len(history.Events) == 0 {
break
}

firstEvent := history.Events[0]
lastEvent = history.Events[len(history.Events)-1]
if firstEvent.GetEventId() == common.FirstEventID {
resetMutableStateBuilder = newMutableStateBuilderWithReplicationState(r.shard.GetConfig(), r.logger,
firstEvent.GetVersion())

sBuilder = newStateBuilder(r.shard, resetMutableStateBuilder, r.logger)
}

_, _, _, err = sBuilder.applyEvents(common.EmptyVersion, "", domainID, requestID, execution, history, nil)
_, _, _, err = sBuilder.applyEvents(domainID, requestID, execution, history, nil)
if err != nil {
return nil, err
}
Expand All @@ -107,6 +110,9 @@ func (r *conflictResolver) reset(replayEventID int64, startTime time.Time) (*mut
// the last updated time is not important here, since this should be updated with event time afterwards
resetMutableStateBuilder.executionInfo.LastUpdatedTimestamp = startTime

sourceCluster := r.clusterMetadata.ClusterNameForFailoverVersion(resetMutableStateBuilder.GetCurrentVersion())
resetMutableStateBuilder.updateReplicationStateLastEventID(sourceCluster, lastEvent.GetVersion(), replayEventID)

r.logger.Infof("All events applied for execution. WorkflowID: %v, RunID: %v, NextEventID: %v",
execution.GetWorkflowId(), execution.GetRunId(), resetMutableStateBuilder.GetNextEventID())

Expand Down Expand Up @@ -146,5 +152,5 @@ func (r *conflictResolver) getHistory(domainID string, execution shared.Workflow

executionHistory := &shared.History{}
executionHistory.Events = historyEvents
return executionHistory, nextPageToken, lastFirstEventID, nil
return executionHistory, response.NextPageToken, lastFirstEventID, nil
}
Loading

0 comments on commit 166ef58

Please sign in to comment.