Skip to content

Commit

Permalink
fix flow state update error
Browse files Browse the repository at this point in the history
  • Loading branch information
danli001 authored and s8sg committed Jun 15, 2023
1 parent 25aa405 commit 5e26ab8
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 22 deletions.
27 changes: 5 additions & 22 deletions core/sdk/executor/executor.go
Original file line number Diff line number Diff line change
Expand Up @@ -182,33 +182,16 @@ func (fexec *FlowExecutor) getDynamicBranchOptions(nodeUniqueId string) ([]strin
// incrementCounter increment counter by given term, if doesn't exist init with increment by
func (fexec *FlowExecutor) incrementCounter(counter string, incrementBy int) (int, error) {
var serr error
count := 0
for i := 0; i < counterUpdateRetryCount; i++ {
encoded, err := fexec.stateStore.Get(counter)
count, err := fexec.stateStore.Incr(counter, int64(incrementBy))
if err != nil {
// if doesn't exist try to create
err := fexec.stateStore.Set(counter, fmt.Sprintf("%d", incrementBy))
if err != nil {
serr = fmt.Errorf("failed to update counter %s, error %v", counter, err)
continue
}
return incrementBy, nil
}

current, err := strconv.Atoi(encoded)
if err != nil {
return 0, fmt.Errorf("failed to update counter %s, error %v", counter, err)
serr = fmt.Errorf("failed to update counter %s, error %v", counter, err)
continue
}

count = current + incrementBy
counterStr := fmt.Sprintf("%d", count)

err = fexec.stateStore.Update(counter, encoded, counterStr)
if err == nil {
return count, nil
}
serr = err
return int(count), nil
}

return 0, fmt.Errorf("failed to update counter after max retry for %s, error %v", counter, serr)
}

Expand Down
2 changes: 2 additions & 0 deletions core/sdk/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ type StateStore interface {
Set(key string, value string) error
// Get a value
Get(key string) (string, error)
// Increase the value of key with a given increment
Incr(key string, value int64) (int64, error)
// Compare and Update a value
Update(key string, oldValue string, newValue string) error
// Cleanup all the resources in StateStore (called only once in a request span)
Expand Down

0 comments on commit 5e26ab8

Please sign in to comment.