added etcd support

This commit is contained in:
ston1th 2020-11-15 18:39:16 +01:00
commit 85a8ba23bd
21 changed files with 767 additions and 247 deletions

View file

@ -4,32 +4,27 @@ import (
"context"
"fmt"
"net"
"net/http"
"os"
"path/filepath"
"runtime"
"time"
"git.giftfish.de/ston1th/raftbbolt"
"git.giftfish.de/ston1th/vipman/pkg/config"
"git.giftfish.de/ston1th/vipman/pkg/raft/tls"
"github.com/go-logr/logr"
"git.giftfish.de/ston1th/vipman/pkg/util"
hclog "github.com/hashicorp/go-hclog"
"github.com/hashicorp/raft"
"k8s.io/klog/v2"
)
type raftLog string
func (r *raftLog) Write(p []byte) (n int, err error) {
return os.Stdout.Write(append([]byte(*r), p...))
}
func (c *Cluster) Start(log logr.Logger, raftcfg *config.Config) error {
func (c *Cluster) Start(raftcfg *config.Config) error {
err := c.checkConfig()
if err != nil {
return err
}
cfg := raftcfg.Cluster.Raft
hcl := hclog.New(&hclog.LoggerOptions{
Name: "raft",
Level: hclog.LevelFromString(cfg.Level),
Level: hclog.LevelFromString(cfg.LogLevel),
Output: hclog.DefaultOutput,
TimeFormat: "I0102 15:04:05.000000",
})
@ -38,19 +33,20 @@ func (c *Cluster) Start(log logr.Logger, raftcfg *config.Config) error {
rc.ProtocolVersion = 3
rc.LocalID = raft.ServerID(cfg.ID)
rc.Logger = hcl
c.id = cfg.ID
pool := 3
timeout := 10 * time.Second
var transport *raft.NetworkTransport
if cfg.TLS != nil {
log.Info("transport: tls")
c.V(1).Info("setup", "transport", "tls")
s, err := tls.NewStream(cfg)
if err != nil {
return err
}
transport = raft.NewNetworkTransportWithLogger(s, pool, timeout, hcl)
} else {
log.Info("transport: tcp")
c.V(1).Info("setup", "transport", "tcp")
address, err := net.ResolveTCPAddr("tcp", cfg.Address)
if err != nil {
return err
@ -104,16 +100,13 @@ func (c *Cluster) Start(log logr.Logger, raftcfg *config.Config) error {
}
}
// Create RAFT instance
c.r, err = raft.NewRaft(rc, (*fsm)(c.kv), logStore, stableStore, snapshots, transport)
c.r, err = raft.NewRaft(rc, (*fsm)(c.kvm), logStore, stableStore, snapshots, transport)
if err != nil {
return err
}
c.kv.r = c.r
c.kvm.r = c.r
t := time.NewTicker(time.Second)
time.Sleep(time.Second * 3)
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
leading := false
@ -128,6 +121,7 @@ func (c *Cluster) Start(log logr.Logger, raftcfg *config.Config) error {
}
select {
case <-t.C:
case leader := <-c.r.LeaderCh():
if leader {
if following {
@ -163,7 +157,6 @@ func (c *Cluster) Start(log logr.Logger, raftcfg *config.Config) error {
}
close(c.done)
return nil
case <-t.C:
}
}
return nil
@ -171,49 +164,16 @@ func (c *Cluster) Start(log logr.Logger, raftcfg *config.Config) error {
func (c *Cluster) leader(ctx context.Context) {
defer func() {
handleCrash()
util.HandleCrash()
}()
c.Callbacks.Leader(ctx, c.kv)
c.callbacks.Leader(ctx, c)
}
func (c *Cluster) follower(ctx context.Context) {
defer func() {
handleCrash()
util.HandleCrash()
}()
c.Callbacks.Follower(ctx, c.kv)
}
var reallyCrash = false
func handleCrash() {
if r := recover(); r != nil {
logPanic(r)
if reallyCrash {
panic(r)
}
}
}
// logPanic logs the caller tree when a panic occurs (except in the special case of http.ErrAbortHandler).
func logPanic(r interface{}) {
if r == http.ErrAbortHandler {
// honor the http.ErrAbortHandler sentinel panic value:
// ErrAbortHandler is a sentinel panic value to abort a handler.
// While any panic from ServeHTTP aborts the response to the client,
// panicking with ErrAbortHandler also suppresses logging of a stack trace to the server's error log.
return
}
// Same as stdlib http server code. Manually allocate stack trace buffer size
// to prevent excessively large logs
const size = 64 << 10
stacktrace := make([]byte, size)
stacktrace = stacktrace[:runtime.Stack(stacktrace, false)]
if _, ok := r.(string); ok {
klog.Errorf("Observed a panic: %s\n%s", r, stacktrace)
} else {
klog.Errorf("Observed a panic: %#v (%v)\n%s", r, r, stacktrace)
}
c.callbacks.Follower(ctx, c)
}
func (c *Cluster) Stepdown() {