v2_server.go 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  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 etcdserver
  15. import (
  16. "context"
  17. "time"
  18. "go.etcd.io/etcd/etcdserver/api/v2store"
  19. pb "go.etcd.io/etcd/etcdserver/etcdserverpb"
  20. )
  21. type RequestV2 pb.Request
  22. type RequestV2Handler interface {
  23. Post(ctx context.Context, r *RequestV2) (Response, error)
  24. Put(ctx context.Context, r *RequestV2) (Response, error)
  25. Delete(ctx context.Context, r *RequestV2) (Response, error)
  26. QGet(ctx context.Context, r *RequestV2) (Response, error)
  27. Get(ctx context.Context, r *RequestV2) (Response, error)
  28. Head(ctx context.Context, r *RequestV2) (Response, error)
  29. }
  30. type reqV2HandlerEtcdServer struct {
  31. reqV2HandlerStore
  32. s *EtcdServer
  33. }
  34. type reqV2HandlerStore struct {
  35. store v2store.Store
  36. applier ApplierV2
  37. }
  38. func NewStoreRequestV2Handler(s v2store.Store, applier ApplierV2) RequestV2Handler {
  39. return &reqV2HandlerStore{s, applier}
  40. }
  41. func (a *reqV2HandlerStore) Post(ctx context.Context, r *RequestV2) (Response, error) {
  42. return a.applier.Post(r), nil
  43. }
  44. func (a *reqV2HandlerStore) Put(ctx context.Context, r *RequestV2) (Response, error) {
  45. return a.applier.Put(r), nil
  46. }
  47. func (a *reqV2HandlerStore) Delete(ctx context.Context, r *RequestV2) (Response, error) {
  48. return a.applier.Delete(r), nil
  49. }
  50. func (a *reqV2HandlerStore) QGet(ctx context.Context, r *RequestV2) (Response, error) {
  51. return a.applier.QGet(r), nil
  52. }
  53. func (a *reqV2HandlerStore) Get(ctx context.Context, r *RequestV2) (Response, error) {
  54. if r.Wait {
  55. wc, err := a.store.Watch(r.Path, r.Recursive, r.Stream, r.Since)
  56. return Response{Watcher: wc}, err
  57. }
  58. ev, err := a.store.Get(r.Path, r.Recursive, r.Sorted)
  59. return Response{Event: ev}, err
  60. }
  61. func (a *reqV2HandlerStore) Head(ctx context.Context, r *RequestV2) (Response, error) {
  62. ev, err := a.store.Get(r.Path, r.Recursive, r.Sorted)
  63. return Response{Event: ev}, err
  64. }
  65. func (a *reqV2HandlerEtcdServer) Post(ctx context.Context, r *RequestV2) (Response, error) {
  66. return a.processRaftRequest(ctx, r)
  67. }
  68. func (a *reqV2HandlerEtcdServer) Put(ctx context.Context, r *RequestV2) (Response, error) {
  69. return a.processRaftRequest(ctx, r)
  70. }
  71. func (a *reqV2HandlerEtcdServer) Delete(ctx context.Context, r *RequestV2) (Response, error) {
  72. return a.processRaftRequest(ctx, r)
  73. }
  74. func (a *reqV2HandlerEtcdServer) QGet(ctx context.Context, r *RequestV2) (Response, error) {
  75. return a.processRaftRequest(ctx, r)
  76. }
  77. func (a *reqV2HandlerEtcdServer) processRaftRequest(ctx context.Context, r *RequestV2) (Response, error) {
  78. data, err := ((*pb.Request)(r)).Marshal()
  79. if err != nil {
  80. return Response{}, err
  81. }
  82. ch := a.s.w.Register(r.ID)
  83. start := time.Now()
  84. a.s.r.Propose(ctx, data)
  85. proposalsPending.Inc()
  86. defer proposalsPending.Dec()
  87. select {
  88. case x := <-ch:
  89. resp := x.(Response)
  90. return resp, resp.Err
  91. case <-ctx.Done():
  92. proposalsFailed.Inc()
  93. a.s.w.Trigger(r.ID, nil) // GC wait
  94. return Response{}, a.s.parseProposeCtxErr(ctx.Err(), start)
  95. case <-a.s.stopping:
  96. }
  97. return Response{}, ErrStopped
  98. }
  99. func (s *EtcdServer) Do(ctx context.Context, r pb.Request) (Response, error) {
  100. r.ID = s.reqIDGen.Next()
  101. h := &reqV2HandlerEtcdServer{
  102. reqV2HandlerStore: reqV2HandlerStore{
  103. store: s.v2store,
  104. applier: s.applyV2,
  105. },
  106. s: s,
  107. }
  108. rp := &r
  109. resp, err := ((*RequestV2)(rp)).Handle(ctx, h)
  110. resp.Term, resp.Index = s.Term(), s.CommittedIndex()
  111. return resp, err
  112. }
  113. // Handle interprets r and performs an operation on s.store according to r.Method
  114. // and other fields. If r.Method is "POST", "PUT", "DELETE", or a "GET" with
  115. // Quorum == true, r will be sent through consensus before performing its
  116. // respective operation. Do will block until an action is performed or there is
  117. // an error.
  118. func (r *RequestV2) Handle(ctx context.Context, v2api RequestV2Handler) (Response, error) {
  119. if r.Method == "GET" && r.Quorum {
  120. r.Method = "QGET"
  121. }
  122. switch r.Method {
  123. case "POST":
  124. return v2api.Post(ctx, r)
  125. case "PUT":
  126. return v2api.Put(ctx, r)
  127. case "DELETE":
  128. return v2api.Delete(ctx, r)
  129. case "QGET":
  130. return v2api.QGet(ctx, r)
  131. case "GET":
  132. return v2api.Get(ctx, r)
  133. case "HEAD":
  134. return v2api.Head(ctx, r)
  135. }
  136. return Response{}, ErrUnknownMethod
  137. }
  138. func (r *RequestV2) String() string {
  139. rpb := pb.Request(*r)
  140. return rpb.String()
  141. }