key.go 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  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. return nil
  115. }
  116. func checkDeleteRequest(r *pb.DeleteRangeRequest) error {
  117. if len(r.Key) == 0 {
  118. return rpctypes.ErrGRPCEmptyKey
  119. }
  120. return nil
  121. }
  122. func checkTxnRequest(r *pb.TxnRequest) error {
  123. if len(r.Compare) > MaxOpsPerTxn || len(r.Success) > MaxOpsPerTxn || len(r.Failure) > MaxOpsPerTxn {
  124. return rpctypes.ErrGRPCTooManyOps
  125. }
  126. for _, c := range r.Compare {
  127. if len(c.Key) == 0 {
  128. return rpctypes.ErrGRPCEmptyKey
  129. }
  130. }
  131. for _, u := range r.Success {
  132. if err := checkRequestOp(u); err != nil {
  133. return err
  134. }
  135. }
  136. if err := checkRequestDupKeys(r.Success); err != nil {
  137. return err
  138. }
  139. for _, u := range r.Failure {
  140. if err := checkRequestOp(u); err != nil {
  141. return err
  142. }
  143. }
  144. return checkRequestDupKeys(r.Failure)
  145. }
  146. // checkRequestDupKeys gives rpctypes.ErrGRPCDuplicateKey if the same key is modified twice
  147. func checkRequestDupKeys(reqs []*pb.RequestOp) error {
  148. // check put overlap
  149. keys := make(map[string]struct{})
  150. for _, requ := range reqs {
  151. tv, ok := requ.Request.(*pb.RequestOp_RequestPut)
  152. if !ok {
  153. continue
  154. }
  155. preq := tv.RequestPut
  156. if preq == nil {
  157. continue
  158. }
  159. if _, ok := keys[string(preq.Key)]; ok {
  160. return rpctypes.ErrGRPCDuplicateKey
  161. }
  162. keys[string(preq.Key)] = struct{}{}
  163. }
  164. // no need to check deletes if no puts; delete overlaps are permitted
  165. if len(keys) == 0 {
  166. return nil
  167. }
  168. // sort keys for range checking
  169. sortedKeys := []string{}
  170. for k := range keys {
  171. sortedKeys = append(sortedKeys, k)
  172. }
  173. sort.Strings(sortedKeys)
  174. // check put overlap with deletes
  175. for _, requ := range reqs {
  176. tv, ok := requ.Request.(*pb.RequestOp_RequestDeleteRange)
  177. if !ok {
  178. continue
  179. }
  180. dreq := tv.RequestDeleteRange
  181. if dreq == nil {
  182. continue
  183. }
  184. if dreq.RangeEnd == nil {
  185. if _, found := keys[string(dreq.Key)]; found {
  186. return rpctypes.ErrGRPCDuplicateKey
  187. }
  188. } else {
  189. lo := sort.SearchStrings(sortedKeys, string(dreq.Key))
  190. hi := sort.SearchStrings(sortedKeys, string(dreq.RangeEnd))
  191. if lo != hi {
  192. // element between lo and hi => overlap
  193. return rpctypes.ErrGRPCDuplicateKey
  194. }
  195. }
  196. }
  197. return nil
  198. }
  199. func checkRequestOp(u *pb.RequestOp) error {
  200. // TODO: ensure only one of the field is set.
  201. switch uv := u.Request.(type) {
  202. case *pb.RequestOp_RequestRange:
  203. if uv.RequestRange != nil {
  204. return checkRangeRequest(uv.RequestRange)
  205. }
  206. case *pb.RequestOp_RequestPut:
  207. if uv.RequestPut != nil {
  208. return checkPutRequest(uv.RequestPut)
  209. }
  210. case *pb.RequestOp_RequestDeleteRange:
  211. if uv.RequestDeleteRange != nil {
  212. return checkDeleteRequest(uv.RequestDeleteRange)
  213. }
  214. default:
  215. // empty op
  216. return nil
  217. }
  218. return nil
  219. }