Skip to content

Commit

Permalink
GOCBC-1008: Convert exists error to cas mismatch for subdoc
Browse files Browse the repository at this point in the history
Motivation
----------
When applicable we covert full doc DocAlreadyExists errors to
CasMismatch. We need to do the same for subdoc mutations i.e. do
the conversion when the doc level flags are not set to Add.

Changes
-------
Update the translateMemdError to convert exists error to cas mismatch
for mutatein when the operation is not a doc level Add.

Change-Id: I143b91065cc3f968675134f64eff407e8c22fe2f
Reviewed-on: http://review.couchbase.org/c/gocbcore/+/140327
Reviewed-by: Brett Lawson <brett19@gmail.com>
Tested-by: Charles Dixon <chvckd@gmail.com>
  • Loading branch information
chvck committed Nov 16, 2020
1 parent 0ec8fca commit 3187be4
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 2 deletions.
65 changes: 64 additions & 1 deletion crudcomponent_subdoc_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package gocbcore

import (
"bytes"

"errors"
"github.com/couchbase/gocbcore/v9/memd"
)

Expand Down Expand Up @@ -335,3 +335,66 @@ func (suite *StandardTestSuite) TestTombstones() {
}))
s.Wait(0)
}

func (suite *StandardTestSuite) TestSubdocCasMismatch() {
agent, s := suite.GetAgentAndHarness()

s.PushOp(agent.Set(SetOptions{
Key: []byte("testSubdocCasMismatch"),
Value: []byte("{\"x\":\"xattrs\", \"y\":\"yattrs\" }"),
CollectionName: suite.CollectionName,
ScopeName: suite.ScopeName,
}, func(res *StoreResult, err error) {
s.Wrap(func() {
if err != nil {
s.Fatalf("Set operation failed: %v", err)
}
})
}))
s.Wait(0)

s.PushOp(agent.MutateIn(MutateInOptions{
Key: []byte("testSubdocCasMismatch"),
Ops: []SubDocOp{
{
Op: memd.SubDocOpDictSet,
Flags: memd.SubdocFlagNone,
Path: "x",
Value: []byte("\"x value\""),
},
},
CollectionName: suite.CollectionName,
ScopeName: suite.ScopeName,
Cas: 1234,
}, func(res *MutateInResult, err error) {
s.Wrap(func() {
if !errors.Is(err, ErrCasMismatch) {
s.Fatalf("Mutate operation should have failed with Cas Mismatch but was: %v", err)
}
})
}))
s.Wait(0)

// With Add flag
s.PushOp(agent.MutateIn(MutateInOptions{
Key: []byte("testSubdocCasMismatch"),
Ops: []SubDocOp{
{
Op: memd.SubDocOpDictSet,
Flags: memd.SubdocFlagNone,
Path: "x",
Value: []byte("\"x value\""),
},
},
CollectionName: suite.CollectionName,
ScopeName: suite.ScopeName,
Flags: memd.SubdocDocFlagAddDoc,
}, func(res *MutateInResult, err error) {
s.Wrap(func() {
if !errors.Is(err, ErrDocumentExists) {
s.Fatalf("Mutate operation should have failed with Exists but was: %v", err)
}
})
}))
s.Wait(0)
}
3 changes: 2 additions & 1 deletion errmapcomponent.go
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,8 @@ func translateMemdError(err error, req *memdQRequest) error {
case ErrMemdBusy:
return errTemporaryFailure
case ErrMemdKeyExists:
if req.Command == memd.CmdReplace || (req.Command == memd.CmdDelete && req.Cas != 0) {
if req.Command == memd.CmdReplace || (req.Command == memd.CmdDelete && req.Cas != 0) ||
(req.Command == memd.CmdSubDocMultiMutation && req.Cas != 0) {
return errCasMismatch
}
return errDocumentExists
Expand Down

0 comments on commit 3187be4

Please sign in to comment.