kv.go 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  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. if resp, err := p.cache.Get(r); err == nil || err == cache.ErrCompacted {
  35. return resp, err
  36. }
  37. }
  38. resp, err := p.c.Do(ctx, RangeRequestToOp(r))
  39. if err != nil {
  40. p.cache.Add(r, (*pb.RangeResponse)(resp.Get()))
  41. }
  42. return (*pb.RangeResponse)(resp.Get()), err
  43. }
  44. func (p *kvProxy) Put(ctx context.Context, r *pb.PutRequest) (*pb.PutResponse, error) {
  45. resp, err := p.c.Do(ctx, PutRequestToOp(r))
  46. return (*pb.PutResponse)(resp.Put()), err
  47. }
  48. func (p *kvProxy) DeleteRange(ctx context.Context, r *pb.DeleteRangeRequest) (*pb.DeleteRangeResponse, error) {
  49. resp, err := p.c.Do(ctx, DelRequestToOp(r))
  50. return (*pb.DeleteRangeResponse)(resp.Del()), err
  51. }
  52. func (p *kvProxy) Txn(ctx context.Context, r *pb.TxnRequest) (*pb.TxnResponse, error) {
  53. txn := p.c.Txn(ctx)
  54. cmps := make([]clientv3.Cmp, len(r.Compare))
  55. thenops := make([]clientv3.Op, len(r.Success))
  56. elseops := make([]clientv3.Op, len(r.Failure))
  57. for i := range r.Compare {
  58. cmps[i] = (clientv3.Cmp)(*r.Compare[i])
  59. }
  60. for i := range r.Success {
  61. thenops[i] = requestOpToOp(r.Success[i])
  62. }
  63. for i := range r.Failure {
  64. elseops[i] = requestOpToOp(r.Failure[i])
  65. }
  66. resp, err := txn.If(cmps...).Then(thenops...).Else(elseops...).Commit()
  67. return (*pb.TxnResponse)(resp), err
  68. }
  69. func (p *kvProxy) Close() error {
  70. return p.c.Close()
  71. }
  72. func requestOpToOp(union *pb.RequestOp) clientv3.Op {
  73. switch tv := union.Request.(type) {
  74. case *pb.RequestOp_RequestRange:
  75. if tv.RequestRange != nil {
  76. return RangeRequestToOp(tv.RequestRange)
  77. }
  78. case *pb.RequestOp_RequestPut:
  79. if tv.RequestPut != nil {
  80. return PutRequestToOp(tv.RequestPut)
  81. }
  82. case *pb.RequestOp_RequestDeleteRange:
  83. if tv.RequestDeleteRange != nil {
  84. return DelRequestToOp(tv.RequestDeleteRange)
  85. }
  86. }
  87. panic("unknown request")
  88. }
  89. func RangeRequestToOp(r *pb.RangeRequest) clientv3.Op {
  90. opts := []clientv3.OpOption{}
  91. if len(r.RangeEnd) != 0 {
  92. opts = append(opts, clientv3.WithRange(string(r.RangeEnd)))
  93. }
  94. opts = append(opts, clientv3.WithRev(r.Revision))
  95. opts = append(opts, clientv3.WithLimit(r.Limit))
  96. opts = append(opts, clientv3.WithSort(
  97. clientv3.SortTarget(r.SortTarget),
  98. clientv3.SortOrder(r.SortOrder)),
  99. )
  100. if r.Serializable {
  101. opts = append(opts, clientv3.WithSerializable())
  102. }
  103. return clientv3.OpGet(string(r.Key), opts...)
  104. }
  105. func PutRequestToOp(r *pb.PutRequest) clientv3.Op {
  106. opts := []clientv3.OpOption{}
  107. opts = append(opts, clientv3.WithLease(clientv3.LeaseID(r.Lease)))
  108. return clientv3.OpPut(string(r.Key), string(r.Value), opts...)
  109. }
  110. func DelRequestToOp(r *pb.DeleteRangeRequest) clientv3.Op {
  111. opts := []clientv3.OpOption{}
  112. if len(r.RangeEnd) != 0 {
  113. opts = append(opts, clientv3.WithRange(string(r.RangeEnd)))
  114. }
  115. return clientv3.OpDelete(string(r.Key), opts...)
  116. }
  117. func (p *kvProxy) Compact(ctx context.Context, r *pb.CompactionRequest) (*pb.CompactionResponse, error) {
  118. panic("unimplemented")
  119. }