txn_command.go 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  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. if len(parts) == 3 {
  129. ru.Request = &pb.RequestUnion_RequestRange{
  130. RequestRange: &pb.RangeRequest{
  131. Key: key,
  132. RangeEnd: []byte(parts[2]),
  133. }}
  134. } else {
  135. ru.Request = &pb.RequestUnion_RequestRange{
  136. RequestRange: &pb.RangeRequest{
  137. Key: key,
  138. }}
  139. }
  140. case "p", "put":
  141. ru.Request = &pb.RequestUnion_RequestPut{
  142. RequestPut: &pb.PutRequest{
  143. Key: key,
  144. Value: []byte(parts[2]),
  145. }}
  146. case "d", "deleteRange":
  147. if len(parts) == 3 {
  148. ru.Request = &pb.RequestUnion_RequestDeleteRange{
  149. RequestDeleteRange: &pb.DeleteRangeRequest{
  150. Key: key,
  151. RangeEnd: []byte(parts[2]),
  152. }}
  153. } else {
  154. ru.Request = &pb.RequestUnion_RequestDeleteRange{
  155. RequestDeleteRange: &pb.DeleteRangeRequest{
  156. Key: key,
  157. }}
  158. }
  159. default:
  160. return nil, fmt.Errorf("invalid txn request: %s", line)
  161. }
  162. return ru, nil
  163. }
  164. func parseCompare(line string) (*pb.Compare, error) {
  165. parts := strings.Split(line, " ")
  166. if len(parts) != 4 {
  167. return nil, fmt.Errorf("invalid txn compare request: %s", line)
  168. }
  169. var err error
  170. c := &pb.Compare{}
  171. c.Key = []byte(parts[0])
  172. switch parts[1] {
  173. case "ver", "version":
  174. tv, _ := c.TargetUnion.(*pb.Compare_Version)
  175. if tv != nil {
  176. tv.Version, err = strconv.ParseInt(parts[3], 10, 64)
  177. if err != nil {
  178. return nil, fmt.Errorf("invalid txn compare request: %s", line)
  179. }
  180. }
  181. case "c", "create":
  182. tv, _ := c.TargetUnion.(*pb.Compare_CreateRevision)
  183. if tv != nil {
  184. tv.CreateRevision, err = strconv.ParseInt(parts[3], 10, 64)
  185. if err != nil {
  186. return nil, fmt.Errorf("invalid txn compare request: %s", line)
  187. }
  188. }
  189. case "m", "mod":
  190. tv, _ := c.TargetUnion.(*pb.Compare_ModRevision)
  191. if tv != nil {
  192. tv.ModRevision, err = strconv.ParseInt(parts[3], 10, 64)
  193. if err != nil {
  194. return nil, fmt.Errorf("invalid txn compare request: %s", line)
  195. }
  196. }
  197. case "val", "value":
  198. tv, _ := c.TargetUnion.(*pb.Compare_Value)
  199. if tv != nil {
  200. tv.Value = []byte(parts[3])
  201. }
  202. default:
  203. return nil, fmt.Errorf("invalid txn compare request: %s", line)
  204. }
  205. switch parts[2] {
  206. case "g", "greater":
  207. c.Result = pb.Compare_GREATER
  208. case "e", "equal":
  209. c.Result = pb.Compare_EQUAL
  210. case "l", "less":
  211. c.Result = pb.Compare_LESS
  212. default:
  213. return nil, fmt.Errorf("invalid txn compare request: %s", line)
  214. }
  215. return c, nil
  216. }