|
| 1 | +// Copyright 2024 PingCAP, Inc. |
| 2 | +// SPDX-License-Identifier: Apache-2.0 |
| 3 | + |
| 4 | +package vip |
| 5 | + |
| 6 | +import ( |
| 7 | + "context" |
| 8 | + |
| 9 | + "github.com/pingcap/tiproxy/lib/config" |
| 10 | + "github.com/pingcap/tiproxy/pkg/manager/elect" |
| 11 | + clientv3 "go.etcd.io/etcd/client/v3" |
| 12 | + "go.uber.org/zap" |
| 13 | +) |
| 14 | + |
| 15 | +const ( |
| 16 | + // vipKey is the key in etcd for VIP election. |
| 17 | + vipKey = "vip" |
| 18 | + // sessionTTL is the session's TTL in seconds for VIP election. |
| 19 | + sessionTTL = 5 |
| 20 | +) |
| 21 | + |
| 22 | +type VIPManager interface { |
| 23 | + Start(context.Context, *clientv3.Client) error |
| 24 | + OnElected() |
| 25 | + OnRetired() |
| 26 | + Close() |
| 27 | +} |
| 28 | + |
| 29 | +var _ VIPManager = (*vipManager)(nil) |
| 30 | + |
| 31 | +type vipManager struct { |
| 32 | + operation NetworkOperation |
| 33 | + cfgGetter config.ConfigGetter |
| 34 | + election elect.Election |
| 35 | + lg *zap.Logger |
| 36 | +} |
| 37 | + |
| 38 | +func NewVIPManager(lg *zap.Logger, cfgGetter config.ConfigGetter) (*vipManager, error) { |
| 39 | + cfg := cfgGetter.GetConfig() |
| 40 | + if len(cfg.HA.VirtualIP) == 0 && len(cfg.HA.Interface) == 0 { |
| 41 | + return nil, nil |
| 42 | + } |
| 43 | + vm := &vipManager{ |
| 44 | + cfgGetter: cfgGetter, |
| 45 | + lg: lg.With(zap.String("address", cfg.HA.VirtualIP), zap.String("link", cfg.HA.Interface)), |
| 46 | + } |
| 47 | + if len(cfg.HA.VirtualIP) == 0 || len(cfg.HA.Interface) == 0 { |
| 48 | + vm.lg.Warn("Both address and link must be specified to enable VIP. VIP is disabled") |
| 49 | + return nil, nil |
| 50 | + } |
| 51 | + operation, err := NewNetworkOperation(cfg.HA.VirtualIP, cfg.HA.Interface) |
| 52 | + if err != nil { |
| 53 | + vm.lg.Error("init network operation failed", zap.Error(err)) |
| 54 | + return nil, err |
| 55 | + } |
| 56 | + vm.operation = operation |
| 57 | + return vm, nil |
| 58 | +} |
| 59 | + |
| 60 | +func getID(cfg *config.Config) (string, error) { |
| 61 | + return cfg.HA.VirtualIP, nil |
| 62 | +} |
| 63 | + |
| 64 | +func (vm *vipManager) Start(ctx context.Context, etcdCli *clientv3.Client) error { |
| 65 | + cfg := vm.cfgGetter.GetConfig() |
| 66 | + addr, err := getID(cfg) |
| 67 | + if err != nil { |
| 68 | + return err |
| 69 | + } |
| 70 | + |
| 71 | + electionCfg := elect.DefaultElectionConfig(sessionTTL) |
| 72 | + election := elect.NewElection(vm.lg, etcdCli, electionCfg, addr, vipKey, vm) |
| 73 | + vm.election = election |
| 74 | + // Check the ownership at startup just in case the node is just down and restarted. |
| 75 | + // Before it was down, it may be either the owner or not. |
| 76 | + if election.IsOwner() { |
| 77 | + vm.OnElected() |
| 78 | + } else { |
| 79 | + vm.OnRetired() |
| 80 | + } |
| 81 | + vm.election.Start(ctx) |
| 82 | + return nil |
| 83 | +} |
| 84 | + |
| 85 | +func (vm *vipManager) OnElected() { |
| 86 | + hasIP, err := vm.operation.HasIP() |
| 87 | + if err != nil { |
| 88 | + vm.lg.Error("checking addresses failed", zap.Error(err)) |
| 89 | + return |
| 90 | + } |
| 91 | + if hasIP { |
| 92 | + vm.lg.Info("already has VIP, do nothing") |
| 93 | + return |
| 94 | + } |
| 95 | + if err := vm.operation.AddIP(); err != nil { |
| 96 | + vm.lg.Error("adding address failed", zap.Error(err)) |
| 97 | + return |
| 98 | + } |
| 99 | + if err := vm.operation.SendARP(); err != nil { |
| 100 | + vm.lg.Error("broadcast ARP failed", zap.Error(err)) |
| 101 | + return |
| 102 | + } |
| 103 | + vm.lg.Info("adding VIP success") |
| 104 | +} |
| 105 | + |
| 106 | +func (vm *vipManager) OnRetired() { |
| 107 | + hasIP, err := vm.operation.HasIP() |
| 108 | + if err != nil { |
| 109 | + vm.lg.Error("checking addresses failed", zap.Error(err)) |
| 110 | + return |
| 111 | + } |
| 112 | + if !hasIP { |
| 113 | + vm.lg.Info("does not have VIP, do nothing") |
| 114 | + return |
| 115 | + } |
| 116 | + if err := vm.operation.DeleteIP(); err != nil { |
| 117 | + vm.lg.Error("deleting address failed", zap.Error(err)) |
| 118 | + return |
| 119 | + } |
| 120 | + vm.lg.Info("deleting VIP success") |
| 121 | +} |
| 122 | + |
| 123 | +func (vm *vipManager) Close() { |
| 124 | + // The OnRetired() will be called when the election is closed. |
| 125 | + if vm.election != nil { |
| 126 | + vm.election.Close() |
| 127 | + } |
| 128 | +} |
0 commit comments