txn.go 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. // Copyright 2016 CoreOS, Inc.
  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. "sync"
  17. "github.com/coreos/etcd/Godeps/_workspace/src/golang.org/x/net/context"
  18. pb "github.com/coreos/etcd/etcdserver/etcdserverpb"
  19. )
  20. //
  21. // Tx.If(
  22. // Compare(Value(k1), ">", v1),
  23. // Compare(Version(k1), "=", 2)
  24. // ).Then(
  25. // OpPut(k2,v2), OpPut(k3,v3)
  26. // ).Else(
  27. // OpPut(k4,v4), OpPut(k5,v5)
  28. // ).Commit()
  29. type Txn interface {
  30. // If takes a list of comparison. If all comparisons passed in succeed,
  31. // the operations passed into Then() will be executed. Or the operations
  32. // passed into Else() will be executed.
  33. If(cs ...Cmp) Txn
  34. // Then takes a list of operations. The Ops list will be executed, if the
  35. // comparisons passed in If() succeed.
  36. Then(ops ...Op) Txn
  37. // Else takes a list of operations. The Ops list will be executed, if the
  38. // comparisons passed in If() fail.
  39. Else(ops ...Op) Txn
  40. // Commit tries to commit the transaction.
  41. Commit() (*TxnResponse, error)
  42. // TODO: add a Do for shortcut the txn without any condition?
  43. }
  44. type txn struct {
  45. kv *kv
  46. mu sync.Mutex
  47. cif bool
  48. cthen bool
  49. celse bool
  50. isWrite bool
  51. cmps []*pb.Compare
  52. sus []*pb.RequestUnion
  53. fas []*pb.RequestUnion
  54. }
  55. func (txn *txn) If(cs ...Cmp) Txn {
  56. txn.mu.Lock()
  57. defer txn.mu.Unlock()
  58. if txn.cif {
  59. panic("cannot call If twice!")
  60. }
  61. if txn.cthen {
  62. panic("cannot call If after Then!")
  63. }
  64. if txn.celse {
  65. panic("cannot call If after Else!")
  66. }
  67. txn.cif = true
  68. for _, cmp := range cs {
  69. txn.cmps = append(txn.cmps, (*pb.Compare)(&cmp))
  70. }
  71. return txn
  72. }
  73. func (txn *txn) Then(ops ...Op) Txn {
  74. txn.mu.Lock()
  75. defer txn.mu.Unlock()
  76. if txn.cthen {
  77. panic("cannot call Then twice!")
  78. }
  79. if txn.celse {
  80. panic("cannot call Then after Else!")
  81. }
  82. txn.cthen = true
  83. for _, op := range ops {
  84. txn.isWrite = txn.isWrite || op.isWrite()
  85. txn.sus = append(txn.sus, op.toRequestUnion())
  86. }
  87. return txn
  88. }
  89. func (txn *txn) Else(ops ...Op) Txn {
  90. txn.mu.Lock()
  91. defer txn.mu.Unlock()
  92. if txn.celse {
  93. panic("cannot call Else twice!")
  94. }
  95. txn.celse = true
  96. for _, op := range ops {
  97. txn.isWrite = txn.isWrite || op.isWrite()
  98. txn.fas = append(txn.fas, op.toRequestUnion())
  99. }
  100. return txn
  101. }
  102. func (txn *txn) Commit() (*TxnResponse, error) {
  103. txn.mu.Lock()
  104. defer txn.mu.Unlock()
  105. kv := txn.kv
  106. for {
  107. r := &pb.TxnRequest{Compare: txn.cmps, Success: txn.sus, Failure: txn.fas}
  108. resp, err := kv.getRemote().Txn(context.TODO(), r)
  109. if err == nil {
  110. return (*TxnResponse)(resp), nil
  111. }
  112. if txn.isWrite {
  113. return nil, err
  114. }
  115. if isRPCError(err) {
  116. return nil, err
  117. }
  118. if nerr := kv.switchRemote(err); nerr != nil {
  119. return nil, nerr
  120. }
  121. }
  122. }