added etcd kv
This commit is contained in:
parent
ce7d138c60
commit
fd9f8fc1fd
8 changed files with 54 additions and 71 deletions
|
|
@ -1,6 +1,7 @@
|
|||
package etcd
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
|
||||
"git.giftfish.de/ston1th/vipman/pkg/cluster"
|
||||
|
|
@ -9,16 +10,19 @@ import (
|
|||
"github.com/go-logr/logr"
|
||||
)
|
||||
|
||||
var ErrKeyNotFound = errors.New("key not found")
|
||||
|
||||
type Cluster struct {
|
||||
logr.Logger
|
||||
cluster.KV
|
||||
cli *clientv3.Client
|
||||
ctx context.Context
|
||||
|
||||
stepdown chan struct{}
|
||||
stop chan struct{}
|
||||
done chan struct{}
|
||||
callbacks cluster.Callbacks
|
||||
|
||||
kvPrefix string
|
||||
id string
|
||||
keyID string
|
||||
session bool
|
||||
|
|
@ -28,13 +32,36 @@ type Cluster struct {
|
|||
func NewCluster(log logr.Logger) cluster.Cluster {
|
||||
return &Cluster{
|
||||
Logger: log,
|
||||
KV: &kv{},
|
||||
ctx: context.Background(),
|
||||
stepdown: make(chan struct{}, 1),
|
||||
stop: 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 {
|
||||
c.callbacks = callbacks
|
||||
return c.checkConfig()
|
||||
|
|
|
|||
|
|
@ -73,16 +73,18 @@ func (c *Cluster) Start(etcdcfg *config.Config) error {
|
|||
c.session = true
|
||||
|
||||
prefix := cfg.Prefix + "/" + cfg.ClusterName
|
||||
c.kvPrefix = prefix + "/kv"
|
||||
ePrefix := prefix + "/e"
|
||||
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())
|
||||
go func() {
|
||||
errc := make(chan error, 1)
|
||||
for {
|
||||
go func() {
|
||||
errc <- e.Campaign(ctxCampaign, "leader")
|
||||
errc <- e.Campaign(ctxCampaign, "")
|
||||
}()
|
||||
select {
|
||||
case err := <-errc:
|
||||
|
|
@ -141,12 +143,16 @@ func (c *Cluster) Start(etcdcfg *config.Config) error {
|
|||
e.Resign(ctxResign)
|
||||
case <-c.stop:
|
||||
t.Stop()
|
||||
cancel()
|
||||
cancelObserve()
|
||||
cancelCampaign()
|
||||
cancel()
|
||||
if leading {
|
||||
ctxResign, _ := context.WithTimeout(context.Background(), time.Second*2)
|
||||
e.Resign(ctxResign)
|
||||
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)
|
||||
}
|
||||
s.Close()
|
||||
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
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue