Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature watch etcd #198

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
77 changes: 42 additions & 35 deletions checker/etcd_leader_checker.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,15 @@
"time"

"github.com/cybertec-postgresql/vip-manager/vipconfig"
client "go.etcd.io/etcd/client/v3"
"go.etcd.io/etcd/api/v3/mvccpb"
clientv3 "go.etcd.io/etcd/client/v3"
)

// EtcdLeaderChecker is used to check state of the leader key in Etcd
type EtcdLeaderChecker struct {
key string
nodename string
kapi client.KV
client *clientv3.Client
}

// naming this c_conf to avoid conflict with conf in etcd_leader_checker.go
Expand Down Expand Up @@ -71,58 +72,64 @@
return nil, err
}

cfg := client.Config{
Endpoints: eConf.Endpoints,
TLS: tlsConfig,
cfg := clientv3.Config{
Endpoints: eConf.Endpoints,
TLS: tlsConfig,
DialKeepAliveTimeout: 5 * time.Second,
DialKeepAliveTime: 5 * time.Second,
Username: eConf.EtcdUser,
Password: eConf.EtcdPassword,
}
c, err := client.New(cfg)
c, err := clientv3.New(cfg)
if err != nil {
return nil, err
}
e.kapi = c.KV
e.client = c
return e, nil
}

// GetChangeNotificationStream checks the status in the loop
func (e *EtcdLeaderChecker) GetChangeNotificationStream(ctx context.Context, out chan<- bool) error {
var state bool
var alreadyConnected = false
checkLoop:
for {
resp, err := e.kapi.Get(ctx, e.key)

if err != nil {
if ctx.Err() != nil {
break checkLoop
}
log.Printf("etcd error: %s", err)
out <- false
time.Sleep(time.Duration(eConf.Interval) * time.Millisecond)
continue
}
defer e.client.Close()

if !alreadyConnected {
log.Printf("etcd checker started up, found key %s", e.key)
alreadyConnected = true
// get current state from etcd
resp, err := e.client.Get(ctx, e.key)
if err != nil {
if ctx.Err() != nil {
return ctx.Err()
}
log.Printf("etcd error: %s", err)
out <- false
time.Sleep(time.Duration(eConf.Interval) * time.Millisecond)
}

for _, kv := range resp.Kvs {
if eConf.Verbose {
log.Println("Leader from DCS:", string(kv.Value))
}
state = string(kv.Value) == e.nodename
}
// process responce

Check failure on line 108 in checker/etcd_leader_checker.go

View workflow job for this annotation

GitHub Actions / Build & Test (ubuntu-latest)

`responce` is a misspelling of `response` (misspell)
pashagolub marked this conversation as resolved.
Show resolved Hide resolved
for _, kv := range resp.Kvs {
log.Println("Current Leader from DCS:", string(kv.Value))
state = string(kv.Value) == e.nodename
}

select {
case <-ctx.Done():
return ctx.Err()
case out <- state:
time.Sleep(time.Duration(eConf.Interval) * time.Millisecond)
}

watchChan := e.client.Watch(ctx, e.key)
fmt.Println("set WATCH on " + e.key)

select {
case <-ctx.Done():
break checkLoop
case out <- state:
time.Sleep(time.Duration(eConf.Interval) * time.Millisecond)
continue
// process watching changes for key
for watchResp := range watchChan {
for _, event := range watchResp.Events {
// new val on key
if event.Type == mvccpb.PUT {
state = string(event.Kv.Value) == e.nodename
out <- state
}
fmt.Printf("Event received! %s executed on %q with value %q\n", event.Type, event.Kv.Key, event.Kv.Value)
}
}

Expand Down
1 change: 1 addition & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@ func main() {
log.Fatalf("Leader checker returned the following error: %s", err)
}
wg.Done()
panic("Leader checker process fail")
}()

wg.Add(1)
Expand Down
Loading