client.go 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. // Copyright 2015 The etcd Authors
  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 client
  15. import "net/rpc"
  16. type Status struct {
  17. // State gives the human-readable status of an agent (e.g., "started" or "terminated")
  18. State string
  19. // TODO: gather more informations
  20. // TODO: memory usage, raft information, etc..
  21. }
  22. type Agent interface {
  23. ID() uint64
  24. // Start starts a new etcd with the given args on the agent machine.
  25. Start(args ...string) (int, error)
  26. // Stop stops the existing etcd the agent started.
  27. Stop() error
  28. // Restart restarts the existing etcd the agent stopped.
  29. Restart() (int, error)
  30. // Cleanup stops the exiting etcd the agent started, then archives log and its data dir.
  31. Cleanup() error
  32. // Terminate stops the exiting etcd the agent started and removes its data dir.
  33. Terminate() error
  34. // DropPort drops all network packets at the given port.
  35. DropPort(port int) error
  36. // RecoverPort stops dropping all network packets at the given port.
  37. RecoverPort(port int) error
  38. // SetLatency slows down network by introducing latency.
  39. SetLatency(ms, rv int) error
  40. // RemoveLatency removes latency introduced by SetLatency.
  41. RemoveLatency() error
  42. // Status returns the status of etcd on the agent
  43. Status() (Status, error)
  44. }
  45. type agent struct {
  46. endpoint string
  47. rpcClient *rpc.Client
  48. }
  49. func NewAgent(endpoint string) (Agent, error) {
  50. c, err := rpc.DialHTTP("tcp", endpoint)
  51. if err != nil {
  52. return nil, err
  53. }
  54. return &agent{endpoint, c}, nil
  55. }
  56. func (a *agent) Start(args ...string) (int, error) {
  57. var pid int
  58. err := a.rpcClient.Call("Agent.RPCStart", args, &pid)
  59. if err != nil {
  60. return -1, err
  61. }
  62. return pid, nil
  63. }
  64. func (a *agent) Stop() error {
  65. return a.rpcClient.Call("Agent.RPCStop", struct{}{}, nil)
  66. }
  67. func (a *agent) Restart() (int, error) {
  68. var pid int
  69. err := a.rpcClient.Call("Agent.RPCRestart", struct{}{}, &pid)
  70. if err != nil {
  71. return -1, err
  72. }
  73. return pid, nil
  74. }
  75. func (a *agent) Cleanup() error {
  76. return a.rpcClient.Call("Agent.RPCCleanup", struct{}{}, nil)
  77. }
  78. func (a *agent) Terminate() error {
  79. return a.rpcClient.Call("Agent.RPCTerminate", struct{}{}, nil)
  80. }
  81. func (a *agent) DropPort(port int) error {
  82. return a.rpcClient.Call("Agent.RPCDropPort", port, nil)
  83. }
  84. func (a *agent) RecoverPort(port int) error {
  85. return a.rpcClient.Call("Agent.RPCRecoverPort", port, nil)
  86. }
  87. func (a *agent) SetLatency(ms, rv int) error {
  88. return a.rpcClient.Call("Agent.RPCSetLatency", []int{ms, rv}, nil)
  89. }
  90. func (a *agent) RemoveLatency() error {
  91. return a.rpcClient.Call("Agent.RPCRemoveLatency", struct{}{}, nil)
  92. }
  93. func (a *agent) Status() (Status, error) {
  94. var s Status
  95. err := a.rpcClient.Call("Agent.RPCStatus", struct{}{}, &s)
  96. return s, err
  97. }
  98. func (a *agent) ID() uint64 {
  99. panic("not implemented")
  100. }