new logger and etcd fixes

This commit is contained in:
ston1th 2021-10-09 16:52:06 +02:00
commit cbd4f38cca
11 changed files with 248 additions and 161 deletions

View file

@ -11,7 +11,7 @@ import (
)
type Cluster struct {
logr.Logger
log logr.Logger
cli *clientv3.Client
ctx context.Context
@ -24,18 +24,23 @@ type Cluster struct {
id string
keyID string
session bool
cancelSession func()
sessionCancel func()
fatal error
}
func NewCluster(log logr.Logger) cluster.Cluster {
return &Cluster{
Logger: log,
ctx: context.Background(),
stepdown: make(chan struct{}, 1),
stop: make(chan struct{}, 1),
done: make(chan struct{}, 1),
}
c := &Cluster{log: log}
c.Reset()
return c
}
func (c *Cluster) Reset() {
c.ctx = context.Background()
c.stepdown = make(chan struct{}, 1)
c.stop = make(chan struct{}, 1)
c.done = make(chan struct{}, 1)
c.session = false
c.fatal = nil
}
func (c *Cluster) Get(k string) (v []byte, err error) {
@ -83,6 +88,10 @@ func (c *Cluster) SetCallbacks(callbacks cluster.Callbacks) error {
return c.checkConfig()
}
func (c *Cluster) Logger() logr.Logger {
return c.log
}
func (c *Cluster) ID() string {
return c.id
}

View file

@ -6,6 +6,7 @@ import (
"fmt"
"time"
"git.giftfish.de/ston1th/haproxy-lb/pkg/cluster"
"git.giftfish.de/ston1th/haproxy-lb/pkg/config"
"git.giftfish.de/ston1th/haproxy-lb/pkg/util"
clientv3 "go.etcd.io/etcd/client/v3"
@ -60,10 +61,12 @@ func (c *Cluster) Start(etcdcfg *config.Config) error {
return err
}
ctxSsession, cancelSession := context.WithCancel(context.Background())
c.cancelSession = cancelSession
log := c.Logger()
log.Info("waiting for etcd")
sessionCtx, sessionCancel := context.WithCancel(context.Background())
c.sessionCancel = sessionCancel
s, err := concurrency.NewSession(c.cli,
concurrency.WithContext(ctxSsession),
concurrency.WithContext(sessionCtx),
concurrency.WithTTL(10),
)
if err != nil {
@ -78,32 +81,33 @@ func (c *Cluster) Start(etcdcfg *config.Config) error {
c.keyID = ePrefix + "/" + c.id
e := concurrency.NewElection(s, ePrefix)
ctxCampaign, cancelCampaign := context.WithCancel(context.Background())
campaignCtx, campaignCancel := context.WithCancel(context.Background())
go func() {
errc := make(chan error, 1)
for {
go func() {
errc <- e.Campaign(ctxCampaign, "")
errc <- e.Campaign(campaignCtx, "")
}()
select {
case err := <-errc:
if err != nil {
time.Sleep(time.Second)
log.Error(err, "campaign error")
c.Fatal(cluster.ErrRestart)
}
case <-ctxCampaign.Done():
case <-campaignCtx.Done():
return
}
}
}()
t := time.NewTicker(time.Second)
t := time.NewTicker(time.Second * 2)
ctx, cancel := context.WithCancel(context.Background())
ctxObserve, cancelObserve := context.WithCancel(context.Background())
observeCtx, observeCancel := context.WithCancel(context.Background())
leading := false
following := false
leaderChan := c.observe(ctxObserve, e.Observe(ctxObserve))
leaderChan := c.observe(observeCtx, e.Observe(observeCtx))
for {
if !leading && c.leaderID(e.Leader(ctxObserve)) == c.keyID {
if !leading && c.leaderID(e.Leader(observeCtx)) == c.keyID {
leading = true
go c.leader(ctx)
} else if !following && !leading {
@ -138,20 +142,19 @@ func (c *Cluster) Start(etcdcfg *config.Config) error {
case <-c.stepdown:
cancel()
ctx, cancel = context.WithCancel(context.Background())
ctxResign, _ := context.WithTimeout(context.Background(), time.Second*2)
e.Resign(ctxResign)
resignCtx, _ := context.WithTimeout(context.Background(), time.Second*2)
e.Resign(resignCtx)
case <-c.stop:
t.Stop()
cancel()
cancelObserve()
cancelCampaign()
observeCancel()
campaignCancel()
if leading {
ctx, _ := context.WithTimeout(context.Background(), time.Second*2)
e.Resign(ctx)
}
if c.callbacks.Cleanup != nil {
ctx, _ := context.WithTimeout(context.Background(), time.Second*2)
c.callbacks.Cleanup(ctx, c)
c.callbacks.Cleanup(context.Background(), c)
}
s.Close()
close(c.done)
@ -204,9 +207,10 @@ func (c *Cluster) Fatal(err error) {
func (c *Cluster) Stop() {
if !c.session {
c.cancelSession()
c.sessionCancel()
return
}
c.stop <- struct{}{}
close(c.stop)
<-c.done
c.cli.Close()