key.go 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  1. // Copyright 2015 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 v3rpc implements etcd v3 RPC system based on gRPC.
  15. package v3rpc
  16. import (
  17. "sort"
  18. "github.com/coreos/etcd/etcdserver"
  19. "github.com/coreos/etcd/etcdserver/api/v3rpc/rpctypes"
  20. pb "github.com/coreos/etcd/etcdserver/etcdserverpb"
  21. "github.com/coreos/pkg/capnslog"
  22. "golang.org/x/net/context"
  23. )
  24. var (
  25. plog = capnslog.NewPackageLogger("github.com/coreos/etcd", "etcdserver/api/v3rpc")
  26. // Max operations per txn list. For example, Txn.Success can have at most 128 operations,
  27. // and Txn.Failure can have at most 128 operations.
  28. MaxOpsPerTxn = 128
  29. )
  30. type kvServer struct {
  31. hdr header
  32. kv etcdserver.RaftKV
  33. }
  34. func NewKVServer(s *etcdserver.EtcdServer) pb.KVServer {
  35. return &kvServer{hdr: newHeader(s), kv: s}
  36. }
  37. func (s *kvServer) Range(ctx context.Context, r *pb.RangeRequest) (*pb.RangeResponse, error) {
  38. if err := checkRangeRequest(r); err != nil {
  39. return nil, err
  40. }
  41. resp, err := s.kv.Range(ctx, r)
  42. if err != nil {
  43. return nil, togRPCError(err)
  44. }
  45. if resp.Header == nil {
  46. plog.Panic("unexpected nil resp.Header")
  47. }
  48. s.hdr.fill(resp.Header)
  49. return resp, nil
  50. }
  51. func (s *kvServer) Put(ctx context.Context, r *pb.PutRequest) (*pb.PutResponse, error) {
  52. if err := checkPutRequest(r); err != nil {
  53. return nil, err
  54. }
  55. resp, err := s.kv.Put(ctx, r)
  56. if err != nil {
  57. return nil, togRPCError(err)
  58. }
  59. if resp.Header == nil {
  60. plog.Panic("unexpected nil resp.Header")
  61. }
  62. s.hdr.fill(resp.Header)
  63. return resp, nil
  64. }
  65. func (s *kvServer) DeleteRange(ctx context.Context, r *pb.DeleteRangeRequest) (*pb.DeleteRangeResponse, error) {
  66. if err := checkDeleteRequest(r); err != nil {
  67. return nil, err
  68. }
  69. resp, err := s.kv.DeleteRange(ctx, r)
  70. if err != nil {
  71. return nil, togRPCError(err)
  72. }
  73. if resp.Header == nil {
  74. plog.Panic("unexpected nil resp.Header")
  75. }
  76. s.hdr.fill(resp.Header)
  77. return resp, nil
  78. }
  79. func (s *kvServer) Txn(ctx context.Context, r *pb.TxnRequest) (*pb.TxnResponse, error) {
  80. if err := checkTxnRequest(r); err != nil {
  81. return nil, err
  82. }
  83. resp, err := s.kv.Txn(ctx, r)
  84. if err != nil {
  85. return nil, togRPCError(err)
  86. }
  87. if resp.Header == nil {
  88. plog.Panic("unexpected nil resp.Header")
  89. }
  90. s.hdr.fill(resp.Header)
  91. return resp, nil
  92. }
  93. func (s *kvServer) Compact(ctx context.Context, r *pb.CompactionRequest) (*pb.CompactionResponse, error) {
  94. resp, err := s.kv.Compact(ctx, r)
  95. if err != nil {
  96. return nil, togRPCError(err)
  97. }
  98. if resp.Header == nil {
  99. plog.Panic("unexpected nil resp.Header")
  100. }
  101. s.hdr.fill(resp.Header)
  102. return resp, nil
  103. }
  104. func checkRangeRequest(r *pb.RangeRequest) error {
  105. if len(r.Key) == 0 {
  106. return rpctypes.ErrGRPCEmptyKey
  107. }
  108. return nil
  109. }
  110. func checkPutRequest(r *pb.PutRequest) error {
  111. if len(r.Key) == 0 {
  112. return rpctypes.ErrGRPCEmptyKey
  113. }
  114. if r.IgnoreValue && len(r.Value) != 0 {
  115. return rpctypes.ErrGRPCValueProvided
  116. }
  117. if r.IgnoreLease && r.Lease != 0 {
  118. return rpctypes.ErrGRPCLeaseProvided
  119. }
  120. return nil
  121. }
  122. func checkDeleteRequest(r *pb.DeleteRangeRequest) error {
  123. if len(r.Key) == 0 {
  124. return rpctypes.ErrGRPCEmptyKey
  125. }
  126. return nil
  127. }
  128. func checkTxnRequest(r *pb.TxnRequest) error {
  129. if len(r.Compare) > MaxOpsPerTxn || len(r.Success) > MaxOpsPerTxn || len(r.Failure) > MaxOpsPerTxn {
  130. return rpctypes.ErrGRPCTooManyOps
  131. }
  132. for _, c := range r.Compare {
  133. if len(c.Key) == 0 {
  134. return rpctypes.ErrGRPCEmptyKey
  135. }
  136. }
  137. for _, u := range r.Success {
  138. if err := checkRequestOp(u); err != nil {
  139. return err
  140. }
  141. }
  142. if err := checkRequestDupKeys(r.Success); err != nil {
  143. return err
  144. }
  145. for _, u := range r.Failure {
  146. if err := checkRequestOp(u); err != nil {
  147. return err
  148. }
  149. }
  150. return checkRequestDupKeys(r.Failure)
  151. }
  152. // checkRequestDupKeys gives rpctypes.ErrGRPCDuplicateKey if the same key is modified twice
  153. func checkRequestDupKeys(reqs []*pb.RequestOp) error {
  154. // check put overlap
  155. keys := make(map[string]struct{})
  156. for _, requ := range reqs {
  157. tv, ok := requ.Request.(*pb.RequestOp_RequestPut)
  158. if !ok {
  159. continue
  160. }
  161. preq := tv.RequestPut
  162. if preq == nil {
  163. continue
  164. }
  165. if _, ok := keys[string(preq.Key)]; ok {
  166. return rpctypes.ErrGRPCDuplicateKey
  167. }
  168. keys[string(preq.Key)] = struct{}{}
  169. }
  170. // no need to check deletes if no puts; delete overlaps are permitted
  171. if len(keys) == 0 {
  172. return nil
  173. }
  174. // sort keys for range checking
  175. sortedKeys := []string{}
  176. for k := range keys {
  177. sortedKeys = append(sortedKeys, k)
  178. }
  179. sort.Strings(sortedKeys)
  180. // check put overlap with deletes
  181. for _, requ := range reqs {
  182. tv, ok := requ.Request.(*pb.RequestOp_RequestDeleteRange)
  183. if !ok {
  184. continue
  185. }
  186. dreq := tv.RequestDeleteRange
  187. if dreq == nil {
  188. continue
  189. }
  190. if dreq.RangeEnd == nil {
  191. if _, found := keys[string(dreq.Key)]; found {
  192. return rpctypes.ErrGRPCDuplicateKey
  193. }
  194. } else {
  195. lo := sort.SearchStrings(sortedKeys, string(dreq.Key))
  196. hi := sort.SearchStrings(sortedKeys, string(dreq.RangeEnd))
  197. if lo != hi {
  198. // element between lo and hi => overlap
  199. return rpctypes.ErrGRPCDuplicateKey
  200. }
  201. }
  202. }
  203. return nil
  204. }
  205. func checkRequestOp(u *pb.RequestOp) error {
  206. // TODO: ensure only one of the field is set.
  207. switch uv := u.Request.(type) {
  208. case *pb.RequestOp_RequestRange:
  209. if uv.RequestRange != nil {
  210. return checkRangeRequest(uv.RequestRange)
  211. }
  212. case *pb.RequestOp_RequestPut:
  213. if uv.RequestPut != nil {
  214. return checkPutRequest(uv.RequestPut)
  215. }
  216. case *pb.RequestOp_RequestDeleteRange:
  217. if uv.RequestDeleteRange != nil {
  218. return checkDeleteRequest(uv.RequestDeleteRange)
  219. }
  220. default:
  221. // empty op / nil entry
  222. return rpctypes.ErrGRPCKeyNotFound
  223. }
  224. return nil
  225. }