txn.go 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  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 clientv3
  15. import (
  16. "context"
  17. "sync"
  18. pb "go.etcd.io/etcd/etcdserver/etcdserverpb"
  19. "google.golang.org/grpc"
  20. )
  21. // Txn is the interface that wraps mini-transactions.
  22. //
  23. // Txn(context.TODO()).If(
  24. // Compare(Value(k1), ">", v1),
  25. // Compare(Version(k1), "=", 2)
  26. // ).Then(
  27. // OpPut(k2,v2), OpPut(k3,v3)
  28. // ).Else(
  29. // OpPut(k4,v4), OpPut(k5,v5)
  30. // ).Commit()
  31. //
  32. type Txn interface {
  33. // If takes a list of comparison. If all comparisons passed in succeed,
  34. // the operations passed into Then() will be executed. Or the operations
  35. // passed into Else() will be executed.
  36. If(cs ...Cmp) Txn
  37. // Then takes a list of operations. The Ops list will be executed, if the
  38. // comparisons passed in If() succeed.
  39. Then(ops ...Op) Txn
  40. // Else takes a list of operations. The Ops list will be executed, if the
  41. // comparisons passed in If() fail.
  42. Else(ops ...Op) Txn
  43. // Commit tries to commit the transaction.
  44. Commit() (*TxnResponse, error)
  45. }
  46. type txn struct {
  47. kv *kv
  48. ctx context.Context
  49. mu sync.Mutex
  50. cif bool
  51. cthen bool
  52. celse bool
  53. isWrite bool
  54. cmps []*pb.Compare
  55. sus []*pb.RequestOp
  56. fas []*pb.RequestOp
  57. callOpts []grpc.CallOption
  58. }
  59. func (txn *txn) If(cs ...Cmp) Txn {
  60. txn.mu.Lock()
  61. defer txn.mu.Unlock()
  62. if txn.cif {
  63. panic("cannot call If twice!")
  64. }
  65. if txn.cthen {
  66. panic("cannot call If after Then!")
  67. }
  68. if txn.celse {
  69. panic("cannot call If after Else!")
  70. }
  71. txn.cif = true
  72. for i := range cs {
  73. txn.cmps = append(txn.cmps, (*pb.Compare)(&cs[i]))
  74. }
  75. return txn
  76. }
  77. func (txn *txn) Then(ops ...Op) Txn {
  78. txn.mu.Lock()
  79. defer txn.mu.Unlock()
  80. if txn.cthen {
  81. panic("cannot call Then twice!")
  82. }
  83. if txn.celse {
  84. panic("cannot call Then after Else!")
  85. }
  86. txn.cthen = true
  87. for _, op := range ops {
  88. txn.isWrite = txn.isWrite || op.isWrite()
  89. txn.sus = append(txn.sus, op.toRequestOp())
  90. }
  91. return txn
  92. }
  93. func (txn *txn) Else(ops ...Op) Txn {
  94. txn.mu.Lock()
  95. defer txn.mu.Unlock()
  96. if txn.celse {
  97. panic("cannot call Else twice!")
  98. }
  99. txn.celse = true
  100. for _, op := range ops {
  101. txn.isWrite = txn.isWrite || op.isWrite()
  102. txn.fas = append(txn.fas, op.toRequestOp())
  103. }
  104. return txn
  105. }
  106. func (txn *txn) Commit() (*TxnResponse, error) {
  107. txn.mu.Lock()
  108. defer txn.mu.Unlock()
  109. r := &pb.TxnRequest{Compare: txn.cmps, Success: txn.sus, Failure: txn.fas}
  110. var resp *pb.TxnResponse
  111. var err error
  112. resp, err = txn.kv.remote.Txn(txn.ctx, r, txn.callOpts...)
  113. if err != nil {
  114. return nil, toErr(txn.ctx, err)
  115. }
  116. return (*TxnResponse)(resp), nil
  117. }