txn_command.go 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  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. // TODO: enable grpc.WithTransportCredentials(creds)
  50. conn, err := grpc.Dial(endpoint, grpc.WithInsecure())
  51. if err != nil {
  52. ExitWithError(ExitBadConnection, err)
  53. }
  54. kv := pb.NewKVClient(conn)
  55. resp, err := kv.Txn(context.Background(), txn)
  56. if err != nil {
  57. ExitWithError(ExitError, err)
  58. }
  59. if resp.Succeeded {
  60. fmt.Println("executed success request list")
  61. } else {
  62. fmt.Println("executed failure request list")
  63. }
  64. }
  65. type stateFunc func(txn *pb.TxnRequest, r *bufio.Reader) stateFunc
  66. func compareState(txn *pb.TxnRequest, r *bufio.Reader) stateFunc {
  67. fmt.Println("entry comparison[key target expected_result compare_value] (end with empty line):")
  68. line, err := r.ReadString('\n')
  69. if err != nil {
  70. ExitWithError(ExitInvalidInput, err)
  71. }
  72. if len(line) == 1 {
  73. return successState
  74. }
  75. // remove trialling \n
  76. line = line[:len(line)-1]
  77. c, err := parseCompare(line)
  78. if err != nil {
  79. ExitWithError(ExitInvalidInput, err)
  80. }
  81. txn.Compare = append(txn.Compare, c)
  82. return compareState
  83. }
  84. func successState(txn *pb.TxnRequest, r *bufio.Reader) stateFunc {
  85. fmt.Println("entry success request[method key value(end_range)] (end with empty line):")
  86. line, err := r.ReadString('\n')
  87. if err != nil {
  88. ExitWithError(ExitInvalidInput, err)
  89. }
  90. if len(line) == 1 {
  91. return failureState
  92. }
  93. // remove trialling \n
  94. line = line[:len(line)-1]
  95. ru, err := parseRequestUnion(line)
  96. if err != nil {
  97. ExitWithError(ExitInvalidInput, err)
  98. }
  99. txn.Success = append(txn.Success, ru)
  100. return successState
  101. }
  102. func failureState(txn *pb.TxnRequest, r *bufio.Reader) stateFunc {
  103. fmt.Println("entry failure request[method key value(end_range)] (end with empty line):")
  104. line, err := r.ReadString('\n')
  105. if err != nil {
  106. ExitWithError(ExitInvalidInput, err)
  107. }
  108. if len(line) == 1 {
  109. return nil
  110. }
  111. // remove trialling \n
  112. line = line[:len(line)-1]
  113. ru, err := parseRequestUnion(line)
  114. if err != nil {
  115. ExitWithError(ExitInvalidInput, err)
  116. }
  117. txn.Failure = append(txn.Failure, ru)
  118. return failureState
  119. }
  120. func parseRequestUnion(line string) (*pb.RequestUnion, error) {
  121. parts := strings.Split(line, " ")
  122. if len(parts) < 2 {
  123. return nil, fmt.Errorf("invalid txn compare request: %s", line)
  124. }
  125. ru := &pb.RequestUnion{}
  126. key := []byte(parts[1])
  127. switch parts[0] {
  128. case "r", "range":
  129. if len(parts) == 3 {
  130. ru.Request = &pb.RequestUnion_RequestRange{
  131. RequestRange: &pb.RangeRequest{
  132. Key: key,
  133. RangeEnd: []byte(parts[2]),
  134. }}
  135. } else {
  136. ru.Request = &pb.RequestUnion_RequestRange{
  137. RequestRange: &pb.RangeRequest{
  138. Key: key,
  139. }}
  140. }
  141. case "p", "put":
  142. ru.Request = &pb.RequestUnion_RequestPut{
  143. RequestPut: &pb.PutRequest{
  144. Key: key,
  145. Value: []byte(parts[2]),
  146. }}
  147. case "d", "deleteRange":
  148. if len(parts) == 3 {
  149. ru.Request = &pb.RequestUnion_RequestDeleteRange{
  150. RequestDeleteRange: &pb.DeleteRangeRequest{
  151. Key: key,
  152. RangeEnd: []byte(parts[2]),
  153. }}
  154. } else {
  155. ru.Request = &pb.RequestUnion_RequestDeleteRange{
  156. RequestDeleteRange: &pb.DeleteRangeRequest{
  157. Key: key,
  158. }}
  159. }
  160. default:
  161. return nil, fmt.Errorf("invalid txn request: %s", line)
  162. }
  163. return ru, nil
  164. }
  165. func parseCompare(line string) (*pb.Compare, error) {
  166. parts := strings.Split(line, " ")
  167. if len(parts) != 4 {
  168. return nil, fmt.Errorf("invalid txn compare request: %s", line)
  169. }
  170. var err error
  171. c := &pb.Compare{}
  172. c.Key = []byte(parts[0])
  173. switch parts[1] {
  174. case "ver", "version":
  175. tv, _ := c.TargetUnion.(*pb.Compare_Version)
  176. if tv != nil {
  177. tv.Version, err = strconv.ParseInt(parts[3], 10, 64)
  178. if err != nil {
  179. return nil, fmt.Errorf("invalid txn compare request: %s", line)
  180. }
  181. }
  182. case "c", "create":
  183. tv, _ := c.TargetUnion.(*pb.Compare_CreateRevision)
  184. if tv != nil {
  185. tv.CreateRevision, err = strconv.ParseInt(parts[3], 10, 64)
  186. if err != nil {
  187. return nil, fmt.Errorf("invalid txn compare request: %s", line)
  188. }
  189. }
  190. case "m", "mod":
  191. tv, _ := c.TargetUnion.(*pb.Compare_ModRevision)
  192. if tv != nil {
  193. tv.ModRevision, err = strconv.ParseInt(parts[3], 10, 64)
  194. if err != nil {
  195. return nil, fmt.Errorf("invalid txn compare request: %s", line)
  196. }
  197. }
  198. case "val", "value":
  199. tv, _ := c.TargetUnion.(*pb.Compare_Value)
  200. if tv != nil {
  201. tv.Value = []byte(parts[3])
  202. }
  203. default:
  204. return nil, fmt.Errorf("invalid txn compare request: %s", line)
  205. }
  206. switch parts[2] {
  207. case "g", "greater":
  208. c.Result = pb.Compare_GREATER
  209. case "e", "equal":
  210. c.Result = pb.Compare_EQUAL
  211. case "l", "less":
  212. c.Result = pb.Compare_LESS
  213. default:
  214. return nil, fmt.Errorf("invalid txn compare request: %s", line)
  215. }
  216. return c, nil
  217. }