Skip to content

Commit a618a72

Browse files
committed
Using mutex to guard UpdateStatus
1 parent 98227f2 commit a618a72

File tree

2 files changed

+11
-3
lines changed

2 files changed

+11
-3
lines changed

pkg/deployment/context_impl.go

+9-3
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@ package deployment
2525
import (
2626
"context"
2727
"fmt"
28-
"sync/atomic"
2928

3029
"github.com/arangodb/arangosync/client"
3130
"github.com/arangodb/arangosync/tasks"
@@ -78,7 +77,10 @@ func (d *Deployment) GetSpec() api.DeploymentSpec {
7877
// GetStatus returns the current status of the deployment
7978
// together with the current version of that status.
8079
func (d *Deployment) GetStatus() (api.DeploymentStatus, int32) {
81-
version := atomic.LoadInt32(&d.status.version)
80+
d.status.mutex.Lock()
81+
defer d.status.mutex.Unlock()
82+
83+
version := d.status.version
8284
return *d.status.last.DeepCopy(), version
8385
}
8486

@@ -87,14 +89,18 @@ func (d *Deployment) GetStatus() (api.DeploymentStatus, int32) {
8789
// If the given last version does not match the actual last version of the status object,
8890
// an error is returned.
8991
func (d *Deployment) UpdateStatus(status api.DeploymentStatus, lastVersion int32, force ...bool) error {
90-
if !atomic.CompareAndSwapInt32(&d.status.version, lastVersion, lastVersion+1) {
92+
d.status.mutex.Lock()
93+
defer d.status.mutex.Unlock()
94+
95+
if d.status.version != lastVersion {
9196
// Status is obsolete
9297
d.deps.Log.Error().
9398
Int32("expected-version", lastVersion).
9499
Int32("actual-version", d.status.version).
95100
Msg("UpdateStatus version conflict error.")
96101
return maskAny(fmt.Errorf("Status conflict error. Expected version %d, got %d", lastVersion, d.status.version))
97102
}
103+
d.status.version++
98104
d.status.last = *status.DeepCopy()
99105
if err := d.updateCRStatus(force...); err != nil {
100106
return maskAny(err)

pkg/deployment/deployment.go

+2
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ package deployment
2525
import (
2626
"fmt"
2727
"reflect"
28+
"sync"
2829
"sync/atomic"
2930
"time"
3031

@@ -84,6 +85,7 @@ const (
8485
type Deployment struct {
8586
apiObject *api.ArangoDeployment // API object
8687
status struct {
88+
mutex sync.Mutex
8789
version int32
8890
last api.DeploymentStatus // Internal status copy of the CR
8991
}

0 commit comments

Comments
 (0)