Skip to content

Commit

Permalink
To solution for race condition with adding resource verion
Browse files Browse the repository at this point in the history
  • Loading branch information
pradumnapandit committed Mar 14, 2024
1 parent 22800e9 commit df3e61e
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 1 deletion.
11 changes: 11 additions & 0 deletions pkg/api/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,19 @@ type Metadata struct {
CreatedAt time.Time `json:"createdAt"`

Check failure on line 13 in pkg/api/api.go

View workflow job for this annotation

GitHub Actions / lint

File is not `goimports`-ed (goimports)
DeletedAt *time.Time `json:"deletedAt,omitempty"`
Generation int64 `json:"generation"`
ResourceVersion uint64 `json:"resourceVersion"`

Finalizers []string `json:"finalizers,omitempty"`
}

func (m *Metadata) GetResourceVersion() uint64 {
return m.ResourceVersion
}

func (m *Metadata) IncrementResourceVersion() {
m.ResourceVersion++
}

func (m *Metadata) GetID() string {
return m.ID
}
Expand Down Expand Up @@ -81,6 +90,7 @@ type Object interface {
GetDeletedAt() *time.Time
GetGeneration() int64
GetFinalizers() []string
GetResourceVersion() uint64

SetID(id string)
SetAnnotations(annotations map[string]string)
Expand All @@ -89,4 +99,5 @@ type Object interface {
SetDeletedAt(deleted *time.Time)
SetGeneration(generation int64)
SetFinalizers(finalizers []string)
IncrementResourceVersion()
}
13 changes: 12 additions & 1 deletion pkg/omap/omap.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@ type CreateStrategy[E api.Object] interface {
PrepareForCreate(obj E)
}


Check failure on line 26 in pkg/omap/omap.go

View workflow job for this annotation

GitHub Actions / lint

File is not `goimports`-ed (goimports)
var ErrResourceVersionNotLatest = errors.New("resourceVersion is not latest")

type Options[E api.Object] struct {
OmapName string
NewFunc func() E
Expand Down Expand Up @@ -146,6 +149,7 @@ func (s *Store[E]) Create(ctx context.Context, obj E) (E, error) {
}

obj.SetCreatedAt(time.Now())
obj.IncrementResourceVersion()

obj, err = s.set(ioCtx, obj)
if err != nil {
Expand Down Expand Up @@ -181,6 +185,7 @@ func (s *Store[E]) Delete(ctx context.Context, id string) error {

now := time.Now()
obj.SetDeletedAt(&now)
obj.IncrementResourceVersion()

if _, err := s.set(ioCtx, obj); err != nil {
return fmt.Errorf("failed to set object metadata: %w", err)
Expand Down Expand Up @@ -221,7 +226,7 @@ func (s *Store[E]) Update(ctx context.Context, obj E) (E, error) {
}
defer ioCtx.Destroy()

_, err = s.get(ioCtx, obj.GetID())
oldObj, err := s.get(ioCtx, obj.GetID())
if err != nil {
return utils.Zero[E](), err
}
Expand All @@ -233,11 +238,17 @@ func (s *Store[E]) Update(ctx context.Context, obj E) (E, error) {
return obj, nil
}

if oldObj.GetResourceVersion() != obj.GetResourceVersion() {
return utils.Zero[E](), fmt.Errorf("failed to update object: %w", ErrResourceVersionNotLatest)
}
obj.IncrementResourceVersion()

//Todo: update version
obj, err = s.set(ioCtx, obj)
if err != nil {
return utils.Zero[E](), err
}


s.enqueue(store.WatchEvent[E]{
Type: store.WatchEventTypeUpdated,
Expand Down

0 comments on commit df3e61e

Please sign in to comment.