master.go 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. package tool
  2. import (
  3. "context"
  4. "github.com/coreos/etcd/clientv3"
  5. "github.com/coreos/etcd/mvcc/mvccpb"
  6. "github.com/hwholiday/gid/v2/library/log"
  7. "go.uber.org/zap"
  8. "time"
  9. )
  10. type master struct {
  11. cli *clientv3.Client
  12. ip string
  13. key string
  14. ttl int64
  15. isMasterNode bool
  16. revision int64
  17. id clientv3.LeaseID
  18. isClose bool
  19. }
  20. var MasterNode *master
  21. func InitMasterNode(etcdAddr []string, ip string, ttl int64) error {
  22. MasterNode = &master{
  23. key: "/gid/master",
  24. ttl: ttl,
  25. ip: ip,
  26. }
  27. var err error
  28. MasterNode.cli, err = clientv3.New(clientv3.Config{
  29. Endpoints: etcdAddr,
  30. DialTimeout: 5 * time.Second,
  31. })
  32. if err != nil {
  33. log.GetLogger().Error("[ApplyMasterNode] New", zap.Any("ip", ip), zap.Any("etcdAddr", etcdAddr), zap.Error(err))
  34. return err
  35. }
  36. go MasterNode.cornTTl()
  37. return nil
  38. }
  39. func (c *master) cornTTl() {
  40. if c == nil {
  41. panic("InitMasterNode is nil")
  42. }
  43. if err := c.applyMasterNode(); err != nil {
  44. panic(err)
  45. }
  46. c.watch()
  47. ticker := time.NewTicker(time.Duration(c.ttl) * time.Second)
  48. go func() {
  49. for {
  50. select {
  51. case _ = <-ticker.C:
  52. _ = c.applyMasterNode()
  53. }
  54. }
  55. }()
  56. }
  57. func (c *master) watch() {
  58. go func() {
  59. watcher := clientv3.NewWatcher(c.cli)
  60. watchChan := watcher.Watch(context.Background(), c.key, clientv3.WithRev(c.revision+1))
  61. for watchResp := range watchChan {
  62. for _, event := range watchResp.Events {
  63. switch event.Type {
  64. case mvccpb.DELETE:
  65. if !c.isClose {
  66. go c.applyMasterNode()
  67. }
  68. }
  69. }
  70. }
  71. }()
  72. }
  73. func (c *master) applyMasterNode() error {
  74. if c == nil {
  75. panic("InitMasterNode is nil")
  76. }
  77. lease := clientv3.NewLease(c.cli)
  78. if !c.isMasterNode {
  79. txn := clientv3.NewKV(c.cli).Txn(context.TODO())
  80. grantRes, err := lease.Grant(context.TODO(), c.ttl+1)
  81. if err != nil {
  82. log.GetLogger().Error("[ApplyMasterNode] New", zap.Any("ip", c.ip), zap.Error(err))
  83. c.isMasterNode = false
  84. return err
  85. }
  86. c.id = grantRes.ID
  87. txn.If(clientv3.Compare(clientv3.CreateRevision(c.key), "=", 0)).
  88. Then(clientv3.OpPut(c.key, c.ip, clientv3.WithLease(grantRes.ID))).
  89. Else()
  90. txnResp, err := txn.Commit()
  91. if err != nil {
  92. log.GetLogger().Error("[ApplyMasterNode] New", zap.Any("ip", c.ip), zap.Error(err))
  93. c.isMasterNode = false
  94. return err
  95. }
  96. if txnResp.Succeeded {
  97. c.isMasterNode = true
  98. } else {
  99. c.isMasterNode = false
  100. }
  101. if c.revision != txnResp.Header.Revision {
  102. c.revision = txnResp.Header.Revision
  103. }
  104. }
  105. _, err := lease.KeepAliveOnce(context.TODO(), c.id)
  106. if err != nil {
  107. c.isMasterNode = false
  108. log.GetLogger().Error("[ApplyMasterNode] New", zap.Any("ip", c.ip), zap.Error(err))
  109. return err
  110. }
  111. return nil
  112. }
  113. func (c *master) CloseApplyMasterNode() {
  114. if c != nil {
  115. c.isClose = true
  116. if _, err := c.cli.Delete(context.Background(), c.key); err != nil {
  117. log.GetLogger().Error("[CloseApplyMasterNode] Delete", zap.Any("ip", c.ip), zap.Error(err))
  118. }
  119. }
  120. }