elect_command.go 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. // Copyright 2016 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 command
  15. import (
  16. "errors"
  17. "os"
  18. "os/signal"
  19. "github.com/coreos/etcd/clientv3"
  20. "github.com/coreos/etcd/clientv3/concurrency"
  21. "github.com/spf13/cobra"
  22. "golang.org/x/net/context"
  23. )
  24. var (
  25. electListen bool
  26. )
  27. // NewElectCommand returns the cobra command for "elect".
  28. func NewElectCommand() *cobra.Command {
  29. cmd := &cobra.Command{
  30. Use: "elect <election-name> [proposal]",
  31. Short: "elect observes and participates in leader election",
  32. Run: electCommandFunc,
  33. }
  34. cmd.Flags().BoolVarP(&electListen, "listen", "l", false, "observation mode")
  35. return cmd
  36. }
  37. func electCommandFunc(cmd *cobra.Command, args []string) {
  38. if len(args) != 1 && len(args) != 2 {
  39. ExitWithError(ExitBadArgs, errors.New("elect takes one election name argument and an optional proposal argument."))
  40. }
  41. c := mustClientFromCmd(cmd)
  42. var err error
  43. if len(args) == 1 {
  44. if !electListen {
  45. ExitWithError(ExitBadArgs, errors.New("no proposal argument but -l not set"))
  46. }
  47. err = observe(c, args[0])
  48. } else {
  49. if electListen {
  50. ExitWithError(ExitBadArgs, errors.New("proposal given but -l is set"))
  51. }
  52. err = campaign(c, args[0], args[1])
  53. }
  54. if err != nil {
  55. ExitWithError(ExitError, err)
  56. }
  57. }
  58. func observe(c *clientv3.Client, election string) error {
  59. e := concurrency.NewElection(c, election)
  60. ctx, cancel := context.WithCancel(context.TODO())
  61. donec := make(chan struct{})
  62. sigc := make(chan os.Signal, 1)
  63. signal.Notify(sigc, os.Interrupt, os.Kill)
  64. go func() {
  65. <-sigc
  66. cancel()
  67. }()
  68. go func() {
  69. for resp := range e.Observe(ctx) {
  70. display.Get(resp)
  71. }
  72. close(donec)
  73. }()
  74. <-donec
  75. select {
  76. case <-ctx.Done():
  77. default:
  78. return errors.New("elect: observer lost")
  79. }
  80. return nil
  81. }
  82. func campaign(c *clientv3.Client, election string, prop string) error {
  83. e := concurrency.NewElection(c, election)
  84. ctx, cancel := context.WithCancel(context.TODO())
  85. donec := make(chan struct{})
  86. sigc := make(chan os.Signal, 1)
  87. signal.Notify(sigc, os.Interrupt, os.Kill)
  88. go func() {
  89. <-sigc
  90. cancel()
  91. close(donec)
  92. }()
  93. s, serr := concurrency.NewSession(c)
  94. if serr != nil {
  95. return serr
  96. }
  97. if err := e.Campaign(ctx, prop); err != nil {
  98. return err
  99. }
  100. // print key since elected
  101. resp, err := c.Get(ctx, e.Key())
  102. if err != nil {
  103. return err
  104. }
  105. display.Get(*resp)
  106. select {
  107. case <-donec:
  108. case <-s.Done():
  109. return errors.New("elect: session expired")
  110. }
  111. return e.Resign()
  112. }