Browse Source

clientv3: add WithIgnoreLease option

sharat 9 years ago
parent
commit
d3191d1afb
3 changed files with 53 additions and 2 deletions
  1. 41 0
      clientv3/integration/kv_test.go
  2. 1 1
      clientv3/kv.go
  3. 11 1
      clientv3/op.go

+ 41 - 0
clientv3/integration/kv_test.go

@@ -146,6 +146,47 @@ func TestKVPutWithIgnoreValue(t *testing.T) {
 	}
 	}
 }
 }
 
 
+// TestKVPutWithIgnoreLease ensures that Put with WithIgnoreLease does not affect the existing lease for the key.
+func TestKVPutWithIgnoreLease(t *testing.T) {
+	defer testutil.AfterTest(t)
+
+	clus := integration.NewClusterV3(t, &integration.ClusterConfig{Size: 1})
+	defer clus.Terminate(t)
+
+	kv := clientv3.NewKV(clus.RandClient())
+
+	lapi := clientv3.NewLease(clus.RandClient())
+	defer lapi.Close()
+
+	resp, err := lapi.Grant(context.Background(), 10)
+	if err != nil {
+		t.Errorf("failed to create lease %v", err)
+	}
+
+	if _, err := kv.Put(context.TODO(), "zoo", "bar", clientv3.WithIgnoreLease()); err != rpctypes.ErrKeyNotFound {
+		t.Fatalf("err expected %v, got %v", rpctypes.ErrKeyNotFound, err)
+	}
+
+	if _, err := kv.Put(context.TODO(), "zoo", "bar", clientv3.WithLease(resp.ID)); err != nil {
+		t.Fatal(err)
+	}
+
+	if _, err := kv.Put(context.TODO(), "zoo", "bar1", clientv3.WithIgnoreLease()); err != nil {
+		t.Fatal(err)
+	}
+
+	rr, rerr := kv.Get(context.TODO(), "zoo")
+	if rerr != nil {
+		t.Fatal(rerr)
+	}
+	if len(rr.Kvs) != 1 {
+		t.Fatalf("len(rr.Kvs) expected 1, got %d", len(rr.Kvs))
+	}
+	if rr.Kvs[0].Lease != int64(resp.ID) {
+		t.Fatalf("lease expected %v, got %v", resp.ID, rr.Kvs[0].Lease)
+	}
+}
+
 func TestKVPutWithRequireLeader(t *testing.T) {
 func TestKVPutWithRequireLeader(t *testing.T) {
 	defer testutil.AfterTest(t)
 	defer testutil.AfterTest(t)
 
 

+ 1 - 1
clientv3/kv.go

@@ -148,7 +148,7 @@ func (kv *kv) do(ctx context.Context, op Op) (OpResponse, error) {
 		}
 		}
 	case tPut:
 	case tPut:
 		var resp *pb.PutResponse
 		var resp *pb.PutResponse
-		r := &pb.PutRequest{Key: op.key, Value: op.val, Lease: int64(op.leaseID), PrevKv: op.prevKV, IgnoreValue: op.ignoreValue}
+		r := &pb.PutRequest{Key: op.key, Value: op.val, Lease: int64(op.leaseID), PrevKv: op.prevKV, IgnoreValue: op.ignoreValue, IgnoreLease: op.ignoreLease}
 		resp, err = kv.remote.Put(ctx, r)
 		resp, err = kv.remote.Put(ctx, r)
 		if err == nil {
 		if err == nil {
 			return OpResponse{put: (*PutResponse)(resp)}, nil
 			return OpResponse{put: (*PutResponse)(resp)}, nil

+ 11 - 1
clientv3/op.go

@@ -54,6 +54,7 @@ type Op struct {
 
 
 	// for put
 	// for put
 	ignoreValue bool
 	ignoreValue bool
+	ignoreLease bool
 
 
 	// progressNotify is for progress updates.
 	// progressNotify is for progress updates.
 	progressNotify bool
 	progressNotify bool
@@ -97,7 +98,7 @@ func (op Op) toRequestOp() *pb.RequestOp {
 	case tRange:
 	case tRange:
 		return &pb.RequestOp{Request: &pb.RequestOp_RequestRange{RequestRange: op.toRangeRequest()}}
 		return &pb.RequestOp{Request: &pb.RequestOp_RequestRange{RequestRange: op.toRangeRequest()}}
 	case tPut:
 	case tPut:
-		r := &pb.PutRequest{Key: op.key, Value: op.val, Lease: int64(op.leaseID), PrevKv: op.prevKV, IgnoreValue: op.ignoreValue}
+		r := &pb.PutRequest{Key: op.key, Value: op.val, Lease: int64(op.leaseID), PrevKv: op.prevKV, IgnoreValue: op.ignoreValue, IgnoreLease: op.ignoreLease}
 		return &pb.RequestOp{Request: &pb.RequestOp_RequestPut{RequestPut: r}}
 		return &pb.RequestOp{Request: &pb.RequestOp_RequestPut{RequestPut: r}}
 	case tDeleteRange:
 	case tDeleteRange:
 		r := &pb.DeleteRangeRequest{Key: op.key, RangeEnd: op.end, PrevKv: op.prevKV}
 		r := &pb.DeleteRangeRequest{Key: op.key, RangeEnd: op.end, PrevKv: op.prevKV}
@@ -372,6 +373,15 @@ func WithIgnoreValue() OpOption {
 	}
 	}
 }
 }
 
 
+// WithIgnoreLease updates the key using its current lease.
+// Empty lease should be passed when ignore_lease is set.
+// Returns an error if the key does not exist.
+func WithIgnoreLease() OpOption {
+	return func(op *Op) {
+		op.ignoreLease = true
+	}
+}
+
 // LeaseOp represents an Operation that lease can execute.
 // LeaseOp represents an Operation that lease can execute.
 type LeaseOp struct {
 type LeaseOp struct {
 	id LeaseID
 	id LeaseID