Skip to content

Commit

Permalink
fix: ignore version conflicts in destroy calls
Browse files Browse the repository at this point in the history
Add a small reproducer.
Make the destroy code treat not found version as successful deletion.

Signed-off-by: Artem Chernyshev <[email protected]>
  • Loading branch information
Unix4ever committed Jul 25, 2024
1 parent 119f626 commit a316525
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 2 deletions.
6 changes: 4 additions & 2 deletions pkg/state/impl/etcd/etcd.go
Original file line number Diff line number Diff line change
Expand Up @@ -330,10 +330,12 @@ func (st *State) Destroy(ctx context.Context, resourcePointer resource.Pointer,
var foundVersion int64

txnGetKvs := txnResp.Responses[0].GetResponseRange().Kvs
if len(txnResp.Responses[0].GetResponseRange().Kvs) > 0 {
foundVersion = txnGetKvs[0].Version
if len(txnGetKvs) == 0 {
return nil
}

foundVersion = txnGetKvs[0].Version

return fmt.Errorf("failed to destroy: %w", ErrVersionConflict(resourcePointer, etcdVersion, foundVersion))
}

Expand Down
31 changes: 31 additions & 0 deletions pkg/state/impl/etcd/etcd_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,9 @@ import (
"github.com/cosi-project/runtime/pkg/state/conformance"
"github.com/cosi-project/runtime/pkg/state/impl/store"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
clientv3 "go.etcd.io/etcd/client/v3"
"golang.org/x/sync/errgroup"
"google.golang.org/grpc/metadata"

"github.com/cosi-project/state-etcd/pkg/state/impl/etcd"
Expand Down Expand Up @@ -57,6 +59,35 @@ func TestPreserveCreated(t *testing.T) {
})
}

func TestDestroy(t *testing.T) {
t.Parallel()

res := conformance.NewPathResource("default", "/")

withEtcd(t, func(s state.State) {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()

var eg errgroup.Group

err := s.Create(ctx, res)
assert.NoError(t, err)

for range 10 {
eg.Go(func() error {
err := s.Destroy(ctx, res.Metadata())
if err != nil && !state.IsNotFoundError(err) {
return err
}

return nil
})
}

require.NoError(t, eg.Wait())
})
}

func TestClearGRPCMetadata(t *testing.T) {
t.Parallel()

Expand Down

0 comments on commit a316525

Please sign in to comment.