Skip to content
This repository has been archived by the owner on Mar 24, 2023. It is now read-only.

make the state manager safe to use in parallel #932

Merged
merged 4 commits into from
May 10, 2019

Conversation

laverya
Copy link
Member

@laverya laverya commented May 9, 2019

What I Did

make the state manager safe to use in parallel

How I Did it

All state updates are now protected by locks.

How to verify it

Look at the unit test that attempts to create a race condition by hammering one field of the state in parallel

Description for the Changelog

Fixed a race condition in ship state updates

Picture of a Ship (not required but encouraged)

USS John Paul Jones (DD-932)

add a test that makes many updates to the state in parallel

add a function to update and return updated state
pkg/state/manager.go Outdated Show resolved Hide resolved
pkg/state/manager.go Outdated Show resolved Hide resolved
}

// SerializeNamespace serializes to disk the namespace to use for helm template
func (m *MManager) SerializeNamespace(namespace string) error {
debug := level.Debug(log.With(m.Logger, "method", "serializeHelmValues"))
debug := level.Debug(log.With(m.Logger, "method", "serializeNamespace"))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👏

return nil
state.V1.Kustomize = kustomize
return state, nil
})
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am loving all the code we get to remove as a side effect of this refactor 😄

Copy link
Member

@dexhorthy dexhorthy left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this is pretty close. Let me know what you think about those suggestions

@@ -497,7 +493,7 @@ func TestMManager_ResetLifecycle(t *testing.T) {
t.Run(tt.name, func(t *testing.T) {
req := require.New(t)
m := &MManager{
Logger: log.NewNopLogger(),
Logger: &logger.TestLogger{T: t},
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👏

there can never be too many tests
Name: metadata.Name,
updatedState, err := updater(currentState.Versioned())
if err != nil {
return VersionedState{}, errors.Wrap(err, "run state update function in safe updater")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

any reason not to return nil here?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah - it's not nilable

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh is State not an interface?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🤦‍♂
I wrote that when it returned VersionedState

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This compiles for me

diff --git a/pkg/state/manager.go b/pkg/state/manager.go
index e2842e5e..adee747a 100644
--- a/pkg/state/manager.go
+++ b/pkg/state/manager.go
@@ -92,7 +92,7 @@ func (m *MManager) StateUpdate(updater Update) (State, error) {
 
        currentState, err := m.TryLoad()
        if err != nil {
-               return VersionedState{}, errors.Wrap(err, "tryLoad in safe updater")
+               return nil, errors.Wrap(err, "tryLoad in safe updater")
        }
 
        updatedState, err := updater(currentState.Versioned())

Copy link
Member Author

@laverya laverya May 10, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah - thus the commit (made at the same time as your comment - 10:54 😆)

@laverya laverya merged commit a96433a into replicatedhq:master May 10, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants