txn_command.go 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  1. // Copyright 2015 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 command
  15. import (
  16. "bufio"
  17. "fmt"
  18. "os"
  19. "strconv"
  20. "strings"
  21. "github.com/coreos/etcd/Godeps/_workspace/src/github.com/spf13/cobra"
  22. "github.com/coreos/etcd/Godeps/_workspace/src/golang.org/x/net/context"
  23. pb "github.com/coreos/etcd/etcdserver/etcdserverpb"
  24. )
  25. // NewTxnCommand returns the cobra command for "txn".
  26. func NewTxnCommand() *cobra.Command {
  27. return &cobra.Command{
  28. Use: "txn",
  29. Short: "Txn processes all the requests in one transaction.",
  30. Run: txnCommandFunc,
  31. }
  32. }
  33. // txnCommandFunc executes the "txn" command.
  34. func txnCommandFunc(cmd *cobra.Command, args []string) {
  35. if len(args) != 0 {
  36. ExitWithError(ExitBadArgs, fmt.Errorf("txn command does not accept argument."))
  37. }
  38. reader := bufio.NewReader(os.Stdin)
  39. next := compareState
  40. txn := &pb.TxnRequest{}
  41. for next != nil {
  42. next = next(txn, reader)
  43. }
  44. resp, err := mustClient(cmd).KV.Txn(context.Background(), txn)
  45. if err != nil {
  46. ExitWithError(ExitError, err)
  47. }
  48. if resp.Succeeded {
  49. fmt.Println("executed success request list")
  50. } else {
  51. fmt.Println("executed failure request list")
  52. }
  53. }
  54. type stateFunc func(txn *pb.TxnRequest, r *bufio.Reader) stateFunc
  55. func compareState(txn *pb.TxnRequest, r *bufio.Reader) stateFunc {
  56. fmt.Println("entry comparison[key target expected_result compare_value] (end with empty line):")
  57. line, err := r.ReadString('\n')
  58. if err != nil {
  59. ExitWithError(ExitInvalidInput, err)
  60. }
  61. if len(line) == 1 {
  62. return successState
  63. }
  64. // remove trialling \n
  65. line = line[:len(line)-1]
  66. c, err := parseCompare(line)
  67. if err != nil {
  68. ExitWithError(ExitInvalidInput, err)
  69. }
  70. txn.Compare = append(txn.Compare, c)
  71. return compareState
  72. }
  73. func successState(txn *pb.TxnRequest, r *bufio.Reader) stateFunc {
  74. fmt.Println("entry success request[method key value(end_range)] (end with empty line):")
  75. line, err := r.ReadString('\n')
  76. if err != nil {
  77. ExitWithError(ExitInvalidInput, err)
  78. }
  79. if len(line) == 1 {
  80. return failureState
  81. }
  82. // remove trialling \n
  83. line = line[:len(line)-1]
  84. ru, err := parseRequestUnion(line)
  85. if err != nil {
  86. ExitWithError(ExitInvalidInput, err)
  87. }
  88. txn.Success = append(txn.Success, ru)
  89. return successState
  90. }
  91. func failureState(txn *pb.TxnRequest, r *bufio.Reader) stateFunc {
  92. fmt.Println("entry failure request[method key value(end_range)] (end with empty line):")
  93. line, err := r.ReadString('\n')
  94. if err != nil {
  95. ExitWithError(ExitInvalidInput, err)
  96. }
  97. if len(line) == 1 {
  98. return nil
  99. }
  100. // remove trialling \n
  101. line = line[:len(line)-1]
  102. ru, err := parseRequestUnion(line)
  103. if err != nil {
  104. ExitWithError(ExitInvalidInput, err)
  105. }
  106. txn.Failure = append(txn.Failure, ru)
  107. return failureState
  108. }
  109. func parseRequestUnion(line string) (*pb.RequestUnion, error) {
  110. parts := strings.Split(line, " ")
  111. if len(parts) < 2 {
  112. return nil, fmt.Errorf("invalid txn compare request: %s", line)
  113. }
  114. ru := &pb.RequestUnion{}
  115. key := []byte(parts[1])
  116. switch parts[0] {
  117. case "r", "range":
  118. if len(parts) == 3 {
  119. ru.Request = &pb.RequestUnion_RequestRange{
  120. RequestRange: &pb.RangeRequest{
  121. Key: key,
  122. RangeEnd: []byte(parts[2]),
  123. }}
  124. } else {
  125. ru.Request = &pb.RequestUnion_RequestRange{
  126. RequestRange: &pb.RangeRequest{
  127. Key: key,
  128. }}
  129. }
  130. case "p", "put":
  131. ru.Request = &pb.RequestUnion_RequestPut{
  132. RequestPut: &pb.PutRequest{
  133. Key: key,
  134. Value: []byte(parts[2]),
  135. }}
  136. case "d", "deleteRange":
  137. if len(parts) == 3 {
  138. ru.Request = &pb.RequestUnion_RequestDeleteRange{
  139. RequestDeleteRange: &pb.DeleteRangeRequest{
  140. Key: key,
  141. RangeEnd: []byte(parts[2]),
  142. }}
  143. } else {
  144. ru.Request = &pb.RequestUnion_RequestDeleteRange{
  145. RequestDeleteRange: &pb.DeleteRangeRequest{
  146. Key: key,
  147. }}
  148. }
  149. default:
  150. return nil, fmt.Errorf("invalid txn request: %s", line)
  151. }
  152. return ru, nil
  153. }
  154. func parseCompare(line string) (*pb.Compare, error) {
  155. parts := strings.Split(line, " ")
  156. if len(parts) != 4 {
  157. return nil, fmt.Errorf("invalid txn compare request: %s", line)
  158. }
  159. var err error
  160. c := &pb.Compare{}
  161. c.Key = []byte(parts[0])
  162. switch parts[1] {
  163. case "ver", "version":
  164. tv, _ := c.TargetUnion.(*pb.Compare_Version)
  165. if tv != nil {
  166. tv.Version, err = strconv.ParseInt(parts[3], 10, 64)
  167. if err != nil {
  168. return nil, fmt.Errorf("invalid txn compare request: %s", line)
  169. }
  170. }
  171. case "c", "create":
  172. tv, _ := c.TargetUnion.(*pb.Compare_CreateRevision)
  173. if tv != nil {
  174. tv.CreateRevision, err = strconv.ParseInt(parts[3], 10, 64)
  175. if err != nil {
  176. return nil, fmt.Errorf("invalid txn compare request: %s", line)
  177. }
  178. }
  179. case "m", "mod":
  180. tv, _ := c.TargetUnion.(*pb.Compare_ModRevision)
  181. if tv != nil {
  182. tv.ModRevision, err = strconv.ParseInt(parts[3], 10, 64)
  183. if err != nil {
  184. return nil, fmt.Errorf("invalid txn compare request: %s", line)
  185. }
  186. }
  187. case "val", "value":
  188. tv, _ := c.TargetUnion.(*pb.Compare_Value)
  189. if tv != nil {
  190. tv.Value = []byte(parts[3])
  191. }
  192. default:
  193. return nil, fmt.Errorf("invalid txn compare request: %s", line)
  194. }
  195. switch parts[2] {
  196. case "g", "greater":
  197. c.Result = pb.Compare_GREATER
  198. case "e", "equal":
  199. c.Result = pb.Compare_EQUAL
  200. case "l", "less":
  201. c.Result = pb.Compare_LESS
  202. default:
  203. return nil, fmt.Errorf("invalid txn compare request: %s", line)
  204. }
  205. return c, nil
  206. }