Skip to content

Commit

Permalink
Merge pull request #5880 from xiang90/put_prev
Browse files Browse the repository at this point in the history
add options to return prev_kv
  • Loading branch information
xiang90 authored Jul 6, 2016
2 parents a78cdea + 7ec8221 commit 234c30c
Show file tree
Hide file tree
Showing 11 changed files with 493 additions and 366 deletions.
27 changes: 18 additions & 9 deletions Documentation/dev-guide/apispec/swagger/rpc.swagger.json
Original file line number Diff line number Diff line change
Expand Up @@ -671,10 +671,10 @@
"format": "byte",
"description": "key is the first key to delete in the range."
},
"preserveKVs": {
"prev_kv": {
"type": "boolean",
"format": "boolean",
"description": "If preserveKVs is set, the deleted KVs will be preserved for delete events\nThe preserved KVs will be returned as response.\nIt requires read permission to read the deleted KVs."
"description": "If prev_kv is set, etcd gets the previous key-value pairs before deleting it.\nThe previous key-value pairs will be returned in the delte response."
},
"range_end": {
"type": "string",
Expand All @@ -686,20 +686,20 @@
"etcdserverpbDeleteRangeResponse": {
"type": "object",
"properties": {
"KVs": {
"type": "array",
"items": {
"$ref": "#/definitions/mvccpbKeyValue"
},
"description": "if preserveKVs is set in the request, the deleted KVs will be returned."
},
"deleted": {
"type": "string",
"format": "int64",
"description": "deleted is the number of keys deleted by the delete range request."
},
"header": {
"$ref": "#/definitions/etcdserverpbResponseHeader"
},
"prev_kvs": {
"type": "array",
"items": {
"$ref": "#/definitions/mvccpbKeyValue"
},
"description": "if prev_kv is set in the request, the previous key-value pairs will be returned."
}
}
},
Expand Down Expand Up @@ -933,6 +933,11 @@
"format": "int64",
"description": "lease is the lease ID to associate with the key in the key-value store. A lease\nvalue of 0 indicates no lease."
},
"prev_kv": {
"type": "boolean",
"format": "boolean",
"description": "If prev_kv is set, etcd gets the previous key-value pair before changing it.\nThe previous key-value pair will be returned in the put response."
},
"value": {
"type": "string",
"format": "byte",
Expand All @@ -945,6 +950,10 @@
"properties": {
"header": {
"$ref": "#/definitions/etcdserverpbResponseHeader"
},
"prev_kv": {
"$ref": "#/definitions/mvccpbKeyValue",
"description": "if prev_kv is set in the request, the previous key-value pair will be returned."
}
}
},
Expand Down
4 changes: 2 additions & 2 deletions clientv3/kv.go
Original file line number Diff line number Diff line change
Expand Up @@ -156,14 +156,14 @@ func (kv *kv) do(ctx context.Context, op Op) (OpResponse, error) {
}
case tPut:
var resp *pb.PutResponse
r := &pb.PutRequest{Key: op.key, Value: op.val, Lease: int64(op.leaseID)}
r := &pb.PutRequest{Key: op.key, Value: op.val, Lease: int64(op.leaseID), PrevKv: op.prevKV}
resp, err = kv.remote.Put(ctx, r)
if err == nil {
return OpResponse{put: (*PutResponse)(resp)}, nil
}
case tDeleteRange:
var resp *pb.DeleteRangeResponse
r := &pb.DeleteRangeRequest{Key: op.key, RangeEnd: op.end, PreserveKVs: op.preserveKVs}
r := &pb.DeleteRangeRequest{Key: op.key, RangeEnd: op.end, PrevKv: op.prevKV}
resp, err = kv.remote.DeleteRange(ctx, r)
if err == nil {
return OpResponse{del: (*DeleteResponse)(resp)}, nil
Expand Down
17 changes: 3 additions & 14 deletions clientv3/op.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,11 +45,9 @@ type Op struct {
// for range, watch
rev int64

// for watch, put, delete
prevKV bool

// for delete
preserveKVs bool

// progressNotify is for progress updates.
progressNotify bool

Expand All @@ -76,10 +74,10 @@ func (op Op) toRequestOp() *pb.RequestOp {
}
return &pb.RequestOp{Request: &pb.RequestOp_RequestRange{RequestRange: r}}
case tPut:
r := &pb.PutRequest{Key: op.key, Value: op.val, Lease: int64(op.leaseID)}
r := &pb.PutRequest{Key: op.key, Value: op.val, Lease: int64(op.leaseID), PrevKv: op.prevKV}
return &pb.RequestOp{Request: &pb.RequestOp_RequestPut{RequestPut: r}}
case tDeleteRange:
r := &pb.DeleteRangeRequest{Key: op.key, RangeEnd: op.end, PreserveKVs: op.preserveKVs}
r := &pb.DeleteRangeRequest{Key: op.key, RangeEnd: op.end, PrevKv: op.prevKV}

return &pb.RequestOp{Request: &pb.RequestOp_RequestDeleteRange{RequestDeleteRange: r}}
default:
Expand Down Expand Up @@ -133,8 +131,6 @@ func OpPut(key, val string, opts ...OpOption) Op {
panic("unexpected serializable in put")
case ret.countOnly:
panic("unexpected countOnly in put")
case ret.preserveKVs:
panic("unexpected preserveKVs in put")
}
return ret
}
Expand All @@ -153,8 +149,6 @@ func opWatch(key string, opts ...OpOption) Op {
panic("unexpected serializable in watch")
case ret.countOnly:
panic("unexpected countOnly in watch")
case ret.preserveKVs:
panic("unexpected preserveKVs in watch")
}
return ret
}
Expand Down Expand Up @@ -266,11 +260,6 @@ func withTop(target SortTarget, order SortOrder) []OpOption {
return []OpOption{WithPrefix(), WithSort(target, order), WithLimit(1)}
}

// WithPreserveKVs preserves the deleted KVs for attaching in responses.
func WithPreserveKVs() OpOption {
return func(op *Op) { op.preserveKVs = true }
}

// WithProgressNotify makes watch server send periodic progress updates.
// Progress updates have zero events in WatchResponse.
func WithProgressNotify() OpOption {
Expand Down
8 changes: 4 additions & 4 deletions etcdctl/ctlv3/command/del_command.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ import (

var (
delPrefix bool
delKVs bool
delPrevKV bool
)

// NewDelCommand returns the cobra command for "del".
Expand All @@ -35,7 +35,7 @@ func NewDelCommand() *cobra.Command {
}

cmd.Flags().BoolVar(&delPrefix, "prefix", false, "delete keys with matching prefix")
cmd.Flags().BoolVar(&delKVs, "preserve-kvs", false, "preserve and return deleted key-value pairs")
cmd.Flags().BoolVar(&delPrevKV, "prev-kv", false, "return deleted key-value pairs")

return cmd
}
Expand Down Expand Up @@ -68,8 +68,8 @@ func getDelOp(cmd *cobra.Command, args []string) (string, []clientv3.OpOption) {
if delPrefix {
opts = append(opts, clientv3.WithPrefix())
}
if delKVs {
opts = append(opts, clientv3.WithPreserveKVs())
if delPrevKV {
opts = append(opts, clientv3.WithPrevKV())
}

return key, opts
Expand Down
9 changes: 7 additions & 2 deletions etcdctl/ctlv3/command/printer.go
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ type simplePrinter struct {

func (s *simplePrinter) Del(resp v3.DeleteResponse) {
fmt.Println(resp.Deleted)
for _, kv := range resp.KVs {
for _, kv := range resp.PrevKvs {
printKV(s.isHex, kv)
}
}
Expand All @@ -119,7 +119,12 @@ func (s *simplePrinter) Get(resp v3.GetResponse) {
}
}

func (s *simplePrinter) Put(r v3.PutResponse) { fmt.Println("OK") }
func (s *simplePrinter) Put(r v3.PutResponse) {
fmt.Println("OK")
if r.PrevKv != nil {
printKV(s.isHex, r.PrevKv)
}
}

func (s *simplePrinter) Txn(resp v3.TxnResponse) {
if resp.Succeeded {
Expand Down
7 changes: 6 additions & 1 deletion etcdctl/ctlv3/command/put_command.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@ import (
)

var (
leaseStr string
leaseStr string
putPrevKV bool
)

// NewPutCommand returns the cobra command for "put".
Expand All @@ -49,6 +50,7 @@ will store the content of the file to <key>.
Run: putCommandFunc,
}
cmd.Flags().StringVar(&leaseStr, "lease", "0", "lease ID (in hexadecimal) to attach to the key")
cmd.Flags().BoolVar(&putPrevKV, "prev-kv", false, "return changed key-value pairs")
return cmd
}

Expand Down Expand Up @@ -85,6 +87,9 @@ func getPutOp(cmd *cobra.Command, args []string) (string, string, []clientv3.OpO
if id != 0 {
opts = append(opts, clientv3.WithLease(clientv3.LeaseID(id)))
}
if putPrevKV {
opts = append(opts, clientv3.WithPrevKV())
}

return key, value, opts
}
23 changes: 21 additions & 2 deletions etcdserver/apply.go
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,22 @@ func (a *applierV3backend) Put(txnID int64, p *pb.PutRequest) (*pb.PutResponse,
rev int64
err error
)

var rr *mvcc.RangeResult
if p.PrevKv {
if txnID != noTxn {
rr, err = a.s.KV().TxnRange(txnID, p.Key, nil, mvcc.RangeOptions{})
if err != nil {
return nil, err
}
} else {
rr, err = a.s.KV().Range(p.Key, nil, mvcc.RangeOptions{})
if err != nil {
return nil, err
}
}
}

if txnID != noTxn {
rev, err = a.s.KV().TxnPut(txnID, p.Key, p.Value, lease.LeaseID(p.Lease))
if err != nil {
Expand All @@ -174,6 +190,9 @@ func (a *applierV3backend) Put(txnID int64, p *pb.PutRequest) (*pb.PutResponse,
rev = a.s.KV().Put(p.Key, p.Value, leaseID)
}
resp.Header.Revision = rev
if rr != nil && len(rr.KVs) != 0 {
resp.PrevKv = &rr.KVs[0]
}
return resp, nil
}

Expand All @@ -192,7 +211,7 @@ func (a *applierV3backend) DeleteRange(txnID int64, dr *pb.DeleteRangeRequest) (
}

var rr *mvcc.RangeResult
if dr.PreserveKVs {
if dr.PrevKv {
if txnID != noTxn {
rr, err = a.s.KV().TxnRange(txnID, dr.Key, dr.RangeEnd, mvcc.RangeOptions{})
if err != nil {
Expand All @@ -218,7 +237,7 @@ func (a *applierV3backend) DeleteRange(txnID int64, dr *pb.DeleteRangeRequest) (
resp.Deleted = n
if rr != nil {
for i := range rr.KVs {
resp.KVs = append(resp.KVs, &rr.KVs[i])
resp.PrevKvs = append(resp.PrevKvs, &rr.KVs[i])
}
}
resp.Header.Revision = rev
Expand Down
7 changes: 5 additions & 2 deletions etcdserver/apply_auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,9 @@ func (aa *authApplierV3) Put(txnID int64, r *pb.PutRequest) (*pb.PutResponse, er
if !aa.as.IsPutPermitted(aa.user, r.Key) {
return nil, auth.ErrPermissionDenied
}
if r.PrevKv && !aa.as.IsRangePermitted(aa.user, r.Key, nil) {
return nil, auth.ErrPermissionDenied
}
return aa.applierV3.Put(txnID, r)
}

Expand All @@ -70,7 +73,7 @@ func (aa *authApplierV3) DeleteRange(txnID int64, r *pb.DeleteRangeRequest) (*pb
if !aa.as.IsDeleteRangePermitted(aa.user, r.Key, r.RangeEnd) {
return nil, auth.ErrPermissionDenied
}
if r.PreserveKVs && !aa.as.IsRangePermitted(aa.user, r.Key, r.RangeEnd) {
if r.PrevKv && !aa.as.IsRangePermitted(aa.user, r.Key, r.RangeEnd) {
return nil, auth.ErrPermissionDenied
}

Expand Down Expand Up @@ -103,7 +106,7 @@ func (aa *authApplierV3) checkTxnReqsPermission(reqs []*pb.RequestOp) bool {
continue
}

if tv.RequestDeleteRange.PreserveKVs && !aa.as.IsRangePermitted(aa.user, tv.RequestDeleteRange.Key, tv.RequestDeleteRange.RangeEnd) {
if tv.RequestDeleteRange.PrevKv && !aa.as.IsRangePermitted(aa.user, tv.RequestDeleteRange.Key, tv.RequestDeleteRange.RangeEnd) {
return false
}

Expand Down
Loading

0 comments on commit 234c30c

Please sign in to comment.