txn.go 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  1. // Copyright 2017 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 leasing
  15. import (
  16. "context"
  17. "strings"
  18. v3 "go.etcd.io/etcd/clientv3"
  19. v3pb "go.etcd.io/etcd/etcdserver/etcdserverpb"
  20. )
  21. type txnLeasing struct {
  22. v3.Txn
  23. lkv *leasingKV
  24. ctx context.Context
  25. cs []v3.Cmp
  26. opst []v3.Op
  27. opse []v3.Op
  28. }
  29. func (txn *txnLeasing) If(cs ...v3.Cmp) v3.Txn {
  30. txn.cs = append(txn.cs, cs...)
  31. txn.Txn = txn.Txn.If(cs...)
  32. return txn
  33. }
  34. func (txn *txnLeasing) Then(ops ...v3.Op) v3.Txn {
  35. txn.opst = append(txn.opst, ops...)
  36. txn.Txn = txn.Txn.Then(ops...)
  37. return txn
  38. }
  39. func (txn *txnLeasing) Else(ops ...v3.Op) v3.Txn {
  40. txn.opse = append(txn.opse, ops...)
  41. txn.Txn = txn.Txn.Else(ops...)
  42. return txn
  43. }
  44. func (txn *txnLeasing) Commit() (*v3.TxnResponse, error) {
  45. if resp, err := txn.eval(); resp != nil || err != nil {
  46. return resp, err
  47. }
  48. return txn.serverTxn()
  49. }
  50. func (txn *txnLeasing) eval() (*v3.TxnResponse, error) {
  51. // TODO: wait on keys in comparisons
  52. thenOps, elseOps := gatherOps(txn.opst), gatherOps(txn.opse)
  53. ops := make([]v3.Op, 0, len(thenOps)+len(elseOps))
  54. ops = append(ops, thenOps...)
  55. ops = append(ops, elseOps...)
  56. for _, ch := range txn.lkv.leases.NotifyOps(ops) {
  57. select {
  58. case <-ch:
  59. case <-txn.ctx.Done():
  60. return nil, txn.ctx.Err()
  61. }
  62. }
  63. txn.lkv.leases.mu.RLock()
  64. defer txn.lkv.leases.mu.RUnlock()
  65. succeeded, ok := txn.lkv.leases.evalCmp(txn.cs)
  66. if !ok || txn.lkv.leases.header == nil {
  67. return nil, nil
  68. }
  69. if ops = txn.opst; !succeeded {
  70. ops = txn.opse
  71. }
  72. resps, ok := txn.lkv.leases.evalOps(ops)
  73. if !ok {
  74. return nil, nil
  75. }
  76. return &v3.TxnResponse{Header: copyHeader(txn.lkv.leases.header), Succeeded: succeeded, Responses: resps}, nil
  77. }
  78. // fallback computes the ops to fetch all possible conflicting
  79. // leasing keys for a list of ops.
  80. func (txn *txnLeasing) fallback(ops []v3.Op) (fbOps []v3.Op) {
  81. for _, op := range ops {
  82. if op.IsGet() {
  83. continue
  84. }
  85. lkey, lend := txn.lkv.pfx+string(op.KeyBytes()), ""
  86. if len(op.RangeBytes()) > 0 {
  87. lend = txn.lkv.pfx + string(op.RangeBytes())
  88. }
  89. fbOps = append(fbOps, v3.OpGet(lkey, v3.WithRange(lend)))
  90. }
  91. return fbOps
  92. }
  93. func (txn *txnLeasing) guardKeys(ops []v3.Op) (cmps []v3.Cmp) {
  94. seen := make(map[string]bool)
  95. for _, op := range ops {
  96. key := string(op.KeyBytes())
  97. if op.IsGet() || len(op.RangeBytes()) != 0 || seen[key] {
  98. continue
  99. }
  100. rev := txn.lkv.leases.Rev(key)
  101. cmps = append(cmps, v3.Compare(v3.CreateRevision(txn.lkv.pfx+key), "<", rev+1))
  102. seen[key] = true
  103. }
  104. return cmps
  105. }
  106. func (txn *txnLeasing) guardRanges(ops []v3.Op) (cmps []v3.Cmp, err error) {
  107. for _, op := range ops {
  108. if op.IsGet() || len(op.RangeBytes()) == 0 {
  109. continue
  110. }
  111. key, end := string(op.KeyBytes()), string(op.RangeBytes())
  112. maxRevLK, err := txn.lkv.revokeRange(txn.ctx, key, end)
  113. if err != nil {
  114. return nil, err
  115. }
  116. opts := append(v3.WithLastRev(), v3.WithRange(end))
  117. getResp, err := txn.lkv.kv.Get(txn.ctx, key, opts...)
  118. if err != nil {
  119. return nil, err
  120. }
  121. maxModRev := int64(0)
  122. if len(getResp.Kvs) > 0 {
  123. maxModRev = getResp.Kvs[0].ModRevision
  124. }
  125. noKeyUpdate := v3.Compare(v3.ModRevision(key).WithRange(end), "<", maxModRev+1)
  126. noLeaseUpdate := v3.Compare(
  127. v3.CreateRevision(txn.lkv.pfx+key).WithRange(txn.lkv.pfx+end),
  128. "<",
  129. maxRevLK+1)
  130. cmps = append(cmps, noKeyUpdate, noLeaseUpdate)
  131. }
  132. return cmps, nil
  133. }
  134. func (txn *txnLeasing) guard(ops []v3.Op) ([]v3.Cmp, error) {
  135. cmps := txn.guardKeys(ops)
  136. rangeCmps, err := txn.guardRanges(ops)
  137. return append(cmps, rangeCmps...), err
  138. }
  139. func (txn *txnLeasing) commitToCache(txnResp *v3pb.TxnResponse, userTxn v3.Op) {
  140. ops := gatherResponseOps(txnResp.Responses, []v3.Op{userTxn})
  141. txn.lkv.leases.mu.Lock()
  142. for _, op := range ops {
  143. key := string(op.KeyBytes())
  144. if op.IsDelete() && len(op.RangeBytes()) > 0 {
  145. end := string(op.RangeBytes())
  146. for k := range txn.lkv.leases.entries {
  147. if inRange(k, key, end) {
  148. txn.lkv.leases.delete(k, txnResp.Header)
  149. }
  150. }
  151. } else if op.IsDelete() {
  152. txn.lkv.leases.delete(key, txnResp.Header)
  153. }
  154. if op.IsPut() {
  155. txn.lkv.leases.Update(op.KeyBytes(), op.ValueBytes(), txnResp.Header)
  156. }
  157. }
  158. txn.lkv.leases.mu.Unlock()
  159. }
  160. func (txn *txnLeasing) revokeFallback(fbResps []*v3pb.ResponseOp) error {
  161. for _, resp := range fbResps {
  162. _, err := txn.lkv.revokeLeaseKvs(txn.ctx, resp.GetResponseRange().Kvs)
  163. if err != nil {
  164. return err
  165. }
  166. }
  167. return nil
  168. }
  169. func (txn *txnLeasing) serverTxn() (*v3.TxnResponse, error) {
  170. if err := txn.lkv.waitSession(txn.ctx); err != nil {
  171. return nil, err
  172. }
  173. userOps := gatherOps(append(txn.opst, txn.opse...))
  174. userTxn := v3.OpTxn(txn.cs, txn.opst, txn.opse)
  175. fbOps := txn.fallback(userOps)
  176. defer closeAll(txn.lkv.leases.LockWriteOps(userOps))
  177. for {
  178. cmps, err := txn.guard(userOps)
  179. if err != nil {
  180. return nil, err
  181. }
  182. resp, err := txn.lkv.kv.Txn(txn.ctx).If(cmps...).Then(userTxn).Else(fbOps...).Commit()
  183. if err != nil {
  184. for _, cmp := range cmps {
  185. txn.lkv.leases.Evict(strings.TrimPrefix(string(cmp.Key), txn.lkv.pfx))
  186. }
  187. return nil, err
  188. }
  189. if resp.Succeeded {
  190. txn.commitToCache((*v3pb.TxnResponse)(resp), userTxn)
  191. userResp := resp.Responses[0].GetResponseTxn()
  192. userResp.Header = resp.Header
  193. return (*v3.TxnResponse)(userResp), nil
  194. }
  195. if err := txn.revokeFallback(resp.Responses); err != nil {
  196. return nil, err
  197. }
  198. }
  199. }