added etcd kv
This commit is contained in:
parent
ce7d138c60
commit
fd9f8fc1fd
8 changed files with 54 additions and 71 deletions
|
|
@ -66,47 +66,7 @@ func main() {
|
||||||
sigs := make(chan os.Signal)
|
sigs := make(chan os.Signal)
|
||||||
signal.Notify(sigs, syscall.SIGINT, syscall.SIGTERM)
|
signal.Notify(sigs, syscall.SIGINT, syscall.SIGTERM)
|
||||||
<-sigs
|
<-sigs
|
||||||
log.Info("stopping vipman")
|
log.Info("vipman shutdown")
|
||||||
c.Stop()
|
c.Stop()
|
||||||
//DeleteIPs(n, cfg.VirtualIPs)
|
log.Info("vipman stopped")
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
|
||||||
c, err := raft.NewCluster(cluster.Callbacks{
|
|
||||||
Leader: func(ctx context.Context, kv cluster.KV) {
|
|
||||||
ticker := time.NewTicker(time.Second * 10)
|
|
||||||
for {
|
|
||||||
s := kv.Get("state")
|
|
||||||
if s == nil {
|
|
||||||
s = []byte{0}
|
|
||||||
} else {
|
|
||||||
s[0] = s[0] + 1
|
|
||||||
}
|
|
||||||
kv.Set("state", s)
|
|
||||||
s = kv.Get("state")
|
|
||||||
klog.Infof("[%s] l state: %d", cfg.LocalPeer.ID, s[0])
|
|
||||||
select {
|
|
||||||
case <-ctx.Done():
|
|
||||||
klog.Infof("[%s] leading canceled", cfg.LocalPeer.ID)
|
|
||||||
return
|
|
||||||
case <-ticker.C:
|
|
||||||
}
|
|
||||||
}
|
|
||||||
},
|
|
||||||
Follower: func(ctx context.Context, kv cluster.KV) {
|
|
||||||
ticker := time.NewTicker(time.Second * 2)
|
|
||||||
for {
|
|
||||||
s := kv.Get("state")
|
|
||||||
if s != nil {
|
|
||||||
klog.Infof("[%s] f state: %d", cfg.LocalPeer.ID, s[0])
|
|
||||||
}
|
|
||||||
select {
|
|
||||||
case <-ctx.Done():
|
|
||||||
klog.Infof("[%s] following canceled", cfg.LocalPeer.ID)
|
|
||||||
return
|
|
||||||
case <-ticker.C:
|
|
||||||
}
|
|
||||||
}
|
|
||||||
},
|
|
||||||
})
|
|
||||||
*/
|
|
||||||
|
|
|
||||||
|
|
@ -15,7 +15,7 @@ type Cluster interface {
|
||||||
}
|
}
|
||||||
|
|
||||||
type KV interface {
|
type KV interface {
|
||||||
Get(k string) []byte
|
Get(k string) ([]byte, error)
|
||||||
Set(k string, v []byte) error
|
Set(k string, v []byte) error
|
||||||
Delete(k string) error
|
Delete(k string) error
|
||||||
}
|
}
|
||||||
|
|
@ -29,4 +29,5 @@ type CallbackContext interface {
|
||||||
type Callbacks struct {
|
type Callbacks struct {
|
||||||
Leader func(context.Context, CallbackContext)
|
Leader func(context.Context, CallbackContext)
|
||||||
Follower func(context.Context, CallbackContext)
|
Follower func(context.Context, CallbackContext)
|
||||||
|
Cleanup func(context.Context, CallbackContext)
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -63,6 +63,9 @@ func NewVIPController(cfg *config.Config) (callbacks cluster.Callbacks, err erro
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
Cleanup: func(ctx context.Context, cc cluster.CallbackContext) {
|
||||||
|
deleteIPs(cc, n, cfg.VirtualIPs)
|
||||||
|
},
|
||||||
}
|
}
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -1,6 +1,7 @@
|
||||||
package etcd
|
package etcd
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"context"
|
||||||
"errors"
|
"errors"
|
||||||
|
|
||||||
"git.giftfish.de/ston1th/vipman/pkg/cluster"
|
"git.giftfish.de/ston1th/vipman/pkg/cluster"
|
||||||
|
|
@ -9,16 +10,19 @@ import (
|
||||||
"github.com/go-logr/logr"
|
"github.com/go-logr/logr"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
var ErrKeyNotFound = errors.New("key not found")
|
||||||
|
|
||||||
type Cluster struct {
|
type Cluster struct {
|
||||||
logr.Logger
|
logr.Logger
|
||||||
cluster.KV
|
|
||||||
cli *clientv3.Client
|
cli *clientv3.Client
|
||||||
|
ctx context.Context
|
||||||
|
|
||||||
stepdown chan struct{}
|
stepdown chan struct{}
|
||||||
stop chan struct{}
|
stop chan struct{}
|
||||||
done chan struct{}
|
done chan struct{}
|
||||||
callbacks cluster.Callbacks
|
callbacks cluster.Callbacks
|
||||||
|
|
||||||
|
kvPrefix string
|
||||||
id string
|
id string
|
||||||
keyID string
|
keyID string
|
||||||
session bool
|
session bool
|
||||||
|
|
@ -28,13 +32,36 @@ type Cluster struct {
|
||||||
func NewCluster(log logr.Logger) cluster.Cluster {
|
func NewCluster(log logr.Logger) cluster.Cluster {
|
||||||
return &Cluster{
|
return &Cluster{
|
||||||
Logger: log,
|
Logger: log,
|
||||||
KV: &kv{},
|
ctx: context.Background(),
|
||||||
stepdown: make(chan struct{}, 1),
|
stepdown: make(chan struct{}, 1),
|
||||||
stop: make(chan struct{}, 1),
|
stop: make(chan struct{}, 1),
|
||||||
done: make(chan struct{}, 1),
|
done: make(chan struct{}, 1),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (c *Cluster) Get(k string) (v []byte, err error) {
|
||||||
|
r, err := c.cli.Get(c.ctx, c.kvPrefix+"/"+k)
|
||||||
|
if err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if len(r.Kvs) == 0 {
|
||||||
|
err = ErrKeyNotFound
|
||||||
|
return
|
||||||
|
}
|
||||||
|
v = r.Kvs[0].Value
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c *Cluster) Set(k string, v []byte) (err error) {
|
||||||
|
_, err = c.cli.Put(c.ctx, c.kvPrefix+"/"+k, string(v))
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c *Cluster) Delete(k string) (err error) {
|
||||||
|
_, err = c.cli.Delete(c.ctx, c.kvPrefix+"/"+k)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
func (c *Cluster) SetCallbacks(callbacks cluster.Callbacks) error {
|
func (c *Cluster) SetCallbacks(callbacks cluster.Callbacks) error {
|
||||||
c.callbacks = callbacks
|
c.callbacks = callbacks
|
||||||
return c.checkConfig()
|
return c.checkConfig()
|
||||||
|
|
|
||||||
|
|
@ -73,16 +73,18 @@ func (c *Cluster) Start(etcdcfg *config.Config) error {
|
||||||
c.session = true
|
c.session = true
|
||||||
|
|
||||||
prefix := cfg.Prefix + "/" + cfg.ClusterName
|
prefix := cfg.Prefix + "/" + cfg.ClusterName
|
||||||
|
c.kvPrefix = prefix + "/kv"
|
||||||
|
ePrefix := prefix + "/e"
|
||||||
c.id = fmt.Sprintf("%x", s.Lease())
|
c.id = fmt.Sprintf("%x", s.Lease())
|
||||||
c.keyID = fmt.Sprintf("%s/%s", prefix, c.id)
|
c.keyID = ePrefix + "/" + c.id
|
||||||
|
|
||||||
e := concurrency.NewElection(s, prefix)
|
e := concurrency.NewElection(s, ePrefix)
|
||||||
ctxCampaign, cancelCampaign := context.WithCancel(context.Background())
|
ctxCampaign, cancelCampaign := context.WithCancel(context.Background())
|
||||||
go func() {
|
go func() {
|
||||||
errc := make(chan error, 1)
|
errc := make(chan error, 1)
|
||||||
for {
|
for {
|
||||||
go func() {
|
go func() {
|
||||||
errc <- e.Campaign(ctxCampaign, "leader")
|
errc <- e.Campaign(ctxCampaign, "")
|
||||||
}()
|
}()
|
||||||
select {
|
select {
|
||||||
case err := <-errc:
|
case err := <-errc:
|
||||||
|
|
@ -141,12 +143,16 @@ func (c *Cluster) Start(etcdcfg *config.Config) error {
|
||||||
e.Resign(ctxResign)
|
e.Resign(ctxResign)
|
||||||
case <-c.stop:
|
case <-c.stop:
|
||||||
t.Stop()
|
t.Stop()
|
||||||
|
cancel()
|
||||||
cancelObserve()
|
cancelObserve()
|
||||||
cancelCampaign()
|
cancelCampaign()
|
||||||
cancel()
|
|
||||||
if leading {
|
if leading {
|
||||||
ctxResign, _ := context.WithTimeout(context.Background(), time.Second*2)
|
ctx, _ := context.WithTimeout(context.Background(), time.Second*2)
|
||||||
e.Resign(ctxResign)
|
e.Resign(ctx)
|
||||||
|
}
|
||||||
|
if c.callbacks.Cleanup != nil {
|
||||||
|
ctx, _ := context.WithTimeout(context.Background(), time.Second*2)
|
||||||
|
c.callbacks.Cleanup(ctx, c)
|
||||||
}
|
}
|
||||||
s.Close()
|
s.Close()
|
||||||
close(c.done)
|
close(c.done)
|
||||||
|
|
|
||||||
|
|
@ -1,18 +0,0 @@
|
||||||
package etcd
|
|
||||||
|
|
||||||
//TODO
|
|
||||||
|
|
||||||
type kv struct {
|
|
||||||
}
|
|
||||||
|
|
||||||
func (kv *kv) Get(k string) []byte {
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func (kv *kv) Set(k string, v []byte) error {
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func (kv *kv) Delete(k string) error {
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
@ -18,10 +18,10 @@ type kv struct {
|
||||||
r *raft.Raft
|
r *raft.Raft
|
||||||
}
|
}
|
||||||
|
|
||||||
func (kv *kv) Get(k string) []byte {
|
func (kv *kv) Get(k string) ([]byte, error) {
|
||||||
kv.mu.Lock()
|
kv.mu.Lock()
|
||||||
defer kv.mu.Unlock()
|
defer kv.mu.Unlock()
|
||||||
return kv.m[k]
|
return kv.m[k], nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (kv *kv) Set(k string, v []byte) error {
|
func (kv *kv) Set(k string, v []byte) error {
|
||||||
|
|
|
||||||
|
|
@ -155,6 +155,10 @@ func (c *Cluster) Start(raftcfg *config.Config) error {
|
||||||
if leading {
|
if leading {
|
||||||
c.r.LeadershipTransfer().Error()
|
c.r.LeadershipTransfer().Error()
|
||||||
}
|
}
|
||||||
|
if c.callbacks.Cleanup != nil {
|
||||||
|
ctx, _ := context.WithTimeout(context.Background(), time.Second*2)
|
||||||
|
c.callbacks.Cleanup(ctx, c)
|
||||||
|
}
|
||||||
close(c.done)
|
close(c.done)
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue