kv.go 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. // Copyright 2016 The etcd Authors
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. package grpcproxy
  15. import (
  16. "github.com/coreos/etcd/clientv3"
  17. pb "github.com/coreos/etcd/etcdserver/etcdserverpb"
  18. "github.com/coreos/etcd/proxy/grpcproxy/cache"
  19. "golang.org/x/net/context"
  20. )
  21. type kvProxy struct {
  22. c *clientv3.Client
  23. cache cache.Cache
  24. }
  25. func NewKvProxy(c *clientv3.Client) *kvProxy {
  26. return &kvProxy{
  27. c: c,
  28. cache: cache.NewCache(cache.DefaultMaxEntries),
  29. }
  30. }
  31. func (p *kvProxy) Range(ctx context.Context, r *pb.RangeRequest) (*pb.RangeResponse, error) {
  32. // if request set Serializable, serve it from local cache first
  33. if r.Serializable {
  34. resp, err := p.cache.Get(r)
  35. switch err {
  36. case nil:
  37. return resp, nil
  38. case cache.ErrCompacted:
  39. return nil, err
  40. }
  41. }
  42. resp, err := p.c.Do(ctx, RangeRequestToOp(r))
  43. if err != nil {
  44. return nil, err
  45. }
  46. p.cache.Add(r, (*pb.RangeResponse)(resp.Get()))
  47. return (*pb.RangeResponse)(resp.Get()), nil
  48. }
  49. func (p *kvProxy) Put(ctx context.Context, r *pb.PutRequest) (*pb.PutResponse, error) {
  50. resp, err := p.c.Do(ctx, PutRequestToOp(r))
  51. return (*pb.PutResponse)(resp.Put()), err
  52. }
  53. func (p *kvProxy) DeleteRange(ctx context.Context, r *pb.DeleteRangeRequest) (*pb.DeleteRangeResponse, error) {
  54. resp, err := p.c.Do(ctx, DelRequestToOp(r))
  55. return (*pb.DeleteRangeResponse)(resp.Del()), err
  56. }
  57. func (p *kvProxy) Txn(ctx context.Context, r *pb.TxnRequest) (*pb.TxnResponse, error) {
  58. txn := p.c.Txn(ctx)
  59. cmps := make([]clientv3.Cmp, len(r.Compare))
  60. thenops := make([]clientv3.Op, len(r.Success))
  61. elseops := make([]clientv3.Op, len(r.Failure))
  62. for i := range r.Compare {
  63. cmps[i] = (clientv3.Cmp)(*r.Compare[i])
  64. }
  65. for i := range r.Success {
  66. thenops[i] = requestOpToOp(r.Success[i])
  67. }
  68. for i := range r.Failure {
  69. elseops[i] = requestOpToOp(r.Failure[i])
  70. }
  71. resp, err := txn.If(cmps...).Then(thenops...).Else(elseops...).Commit()
  72. return (*pb.TxnResponse)(resp), err
  73. }
  74. func (p *kvProxy) Close() error {
  75. return p.c.Close()
  76. }
  77. func requestOpToOp(union *pb.RequestOp) clientv3.Op {
  78. switch tv := union.Request.(type) {
  79. case *pb.RequestOp_RequestRange:
  80. if tv.RequestRange != nil {
  81. return RangeRequestToOp(tv.RequestRange)
  82. }
  83. case *pb.RequestOp_RequestPut:
  84. if tv.RequestPut != nil {
  85. return PutRequestToOp(tv.RequestPut)
  86. }
  87. case *pb.RequestOp_RequestDeleteRange:
  88. if tv.RequestDeleteRange != nil {
  89. return DelRequestToOp(tv.RequestDeleteRange)
  90. }
  91. }
  92. panic("unknown request")
  93. }
  94. func RangeRequestToOp(r *pb.RangeRequest) clientv3.Op {
  95. opts := []clientv3.OpOption{}
  96. if len(r.RangeEnd) != 0 {
  97. opts = append(opts, clientv3.WithRange(string(r.RangeEnd)))
  98. }
  99. opts = append(opts, clientv3.WithRev(r.Revision))
  100. opts = append(opts, clientv3.WithLimit(r.Limit))
  101. opts = append(opts, clientv3.WithSort(
  102. clientv3.SortTarget(r.SortTarget),
  103. clientv3.SortOrder(r.SortOrder)),
  104. )
  105. if r.Serializable {
  106. opts = append(opts, clientv3.WithSerializable())
  107. }
  108. return clientv3.OpGet(string(r.Key), opts...)
  109. }
  110. func PutRequestToOp(r *pb.PutRequest) clientv3.Op {
  111. opts := []clientv3.OpOption{}
  112. opts = append(opts, clientv3.WithLease(clientv3.LeaseID(r.Lease)))
  113. return clientv3.OpPut(string(r.Key), string(r.Value), opts...)
  114. }
  115. func DelRequestToOp(r *pb.DeleteRangeRequest) clientv3.Op {
  116. opts := []clientv3.OpOption{}
  117. if len(r.RangeEnd) != 0 {
  118. opts = append(opts, clientv3.WithRange(string(r.RangeEnd)))
  119. }
  120. return clientv3.OpDelete(string(r.Key), opts...)
  121. }
  122. func (p *kvProxy) Compact(ctx context.Context, r *pb.CompactionRequest) (*pb.CompactionResponse, error) {
  123. panic("unimplemented")
  124. }