Skip to content

Commit

Permalink
bugfix workflowstate encoding, missing generation field (dapr#7892)
Browse files Browse the repository at this point in the history
* bugfix workflowstate encoding, missing generation field

Signed-off-by: Fabian Martinez <[email protected]>

* feedback

Signed-off-by: Fabian Martinez <[email protected]>

---------

Signed-off-by: Fabian Martinez <[email protected]>
Co-authored-by: Yaron Schneider <[email protected]>
  • Loading branch information
famarting and yaron2 authored Sep 3, 2024
1 parent 10fb096 commit 09f6a0f
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 10 deletions.
21 changes: 11 additions & 10 deletions pkg/runtime/wfengine/backends/actors/workflowstate.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,13 @@ type workflowState struct {
config actorsBackendConfig
}

type serializableWorkflowState struct {
Inbox [][]byte
History [][]byte
CustomStatus string
Generation uint64
}

type workflowStateMetadata struct {
InboxLength int
HistoryLength int
Expand Down Expand Up @@ -205,14 +212,11 @@ func (s *workflowState) EncodeWorkflowState() ([]byte, error) {
}

// Encode workflowState
encodedState, err := actors.EncodeInternalActorData(&struct {
Inbox [][]byte
History [][]byte
CustomStatus string
}{
encodedState, err := actors.EncodeInternalActorData(&serializableWorkflowState{
Inbox: encodedInbox,
History: encodedHistory,
CustomStatus: s.CustomStatus,
Generation: s.Generation,
})
if err != nil {
return nil, err
Expand All @@ -224,11 +228,7 @@ func (s *workflowState) EncodeWorkflowState() ([]byte, error) {
// It only decodes the inbox, history, and custom status.
func (s *workflowState) DecodeWorkflowState(encodedState []byte) error {
// Decode workflowState
var decodedState struct {
Inbox [][]byte
History [][]byte
CustomStatus string
}
var decodedState serializableWorkflowState
err := actors.DecodeInternalActorData(bytes.NewReader(encodedState), &decodedState)
if err != nil {
return err
Expand All @@ -252,6 +252,7 @@ func (s *workflowState) DecodeWorkflowState(encodedState []byte) error {
s.Inbox[i] = event
}
s.CustomStatus = decodedState.CustomStatus
s.Generation = decodedState.Generation
return nil
}

Expand Down
59 changes: 59 additions & 0 deletions pkg/runtime/wfengine/backends/actors/workflowstate_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
/*
Copyright 2024 The Dapr Authors
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package actors

import (
"testing"
"time"

"github.com/microsoft/durabletask-go/backend"
"github.com/stretchr/testify/require"
"google.golang.org/protobuf/types/known/timestamppb"
)

func TestWorkflowStateSerialization(t *testing.T) {
time1 := time.Now()
time2 := time.Now().Add(3 * time.Hour).Add(3 * time.Minute)

wfs := NewWorkflowState(NewActorsBackendConfig("foo"))
wfs.AddToInbox(&backend.HistoryEvent{
EventId: -1,
Timestamp: timestamppb.New(time1),
})
wfs.History = append(wfs.History, &backend.HistoryEvent{
EventId: 2,
Timestamp: timestamppb.New(time2),
})
wfs.CustomStatus = "baz"
wfs.Generation = 33

encoded, err := wfs.EncodeWorkflowState()
require.NoError(t, err)

var decoded workflowState
err = decoded.DecodeWorkflowState(encoded)
require.NoError(t, err)

require.Len(t, decoded.Inbox, 1)
require.Equal(t, int32(-1), decoded.Inbox[0].GetEventId())
require.Equal(t, timestamppb.New(time1), decoded.Inbox[0].GetTimestamp())

require.Len(t, decoded.History, 1)
require.Equal(t, int32(2), decoded.History[0].GetEventId())
require.Equal(t, timestamppb.New(time2), decoded.History[0].GetTimestamp())

require.Equal(t, wfs.CustomStatus, decoded.CustomStatus)
require.Equal(t, wfs.Generation, decoded.Generation)
}

0 comments on commit 09f6a0f

Please sign in to comment.