txn_command.go 5.4 KB

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