Browse Source

etcdserver: prevExist=true + condition is compareAndSwap

PrevExist indicates the key should exist. Condition compares with
an existing key. So PrevExist+condition = CompareAndSwap not Update.
Xiang Li 10 years ago
parent
commit
98f8dfbc9d
2 changed files with 8 additions and 6 deletions
  1. 5 1
      etcdserver/server.go
  2. 3 5
      etcdserver/server_test.go

+ 5 - 1
etcdserver/server.go

@@ -702,7 +702,11 @@ func (s *EtcdServer) applyRequest(r pb.Request) Response {
 		switch {
 		switch {
 		case existsSet:
 		case existsSet:
 			if exists {
 			if exists {
-				return f(s.store.Update(r.Path, r.Val, expr))
+				if r.PrevIndex == 0 && r.PrevValue == "" {
+					return f(s.store.Update(r.Path, r.Val, expr))
+				} else {
+					return f(s.store.CompareAndSwap(r.Path, r.PrevValue, r.PrevIndex, r.Val, expr))
+				}
 			}
 			}
 			return f(s.store.Create(r.Path, r.Dir, r.Val, false, expr))
 			return f(s.store.Create(r.Path, r.Dir, r.Val, false, expr))
 		case r.PrevIndex > 0 || r.PrevValue != "":
 		case r.PrevIndex > 0 || r.PrevValue != "":

+ 3 - 5
etcdserver/server_test.go

@@ -235,20 +235,18 @@ func TestApplyRequest(t *testing.T) {
 				},
 				},
 			},
 			},
 		},
 		},
-		// PUT with PrevExist=true *and* PrevIndex set ==> Update
-		// TODO(jonboulle): is this expected?!
+		// PUT with PrevExist=true *and* PrevIndex set ==> CompareAndSwap
 		{
 		{
 			pb.Request{Method: "PUT", ID: 1, PrevExist: pbutil.Boolp(true), PrevIndex: 1},
 			pb.Request{Method: "PUT", ID: 1, PrevExist: pbutil.Boolp(true), PrevIndex: 1},
 			Response{Event: &store.Event{}},
 			Response{Event: &store.Event{}},
 			[]testutil.Action{
 			[]testutil.Action{
 				{
 				{
-					Name:   "Update",
-					Params: []interface{}{"", "", time.Time{}},
+					Name:   "CompareAndSwap",
+					Params: []interface{}{"", "", uint64(1), "", time.Time{}},
 				},
 				},
 			},
 			},
 		},
 		},
 		// PUT with PrevExist=false *and* PrevIndex set ==> Create
 		// PUT with PrevExist=false *and* PrevIndex set ==> Create
-		// TODO(jonboulle): is this expected?!
 		{
 		{
 			pb.Request{Method: "PUT", ID: 1, PrevExist: pbutil.Boolp(false), PrevIndex: 1},
 			pb.Request{Method: "PUT", ID: 1, PrevExist: pbutil.Boolp(false), PrevIndex: 1},
 			Response{Event: &store.Event{}},
 			Response{Event: &store.Event{}},