added simple kv store

This commit is contained in:
ston1th 2020-11-14 00:54:18 +01:00
commit b0d13076e0
10 changed files with 312 additions and 105 deletions

View file

@ -26,23 +26,41 @@ func main() {
if err != nil {
klog.Fatalf("Initialization failed: %s", err)
}
c, err := cluster.InitCluster(cluster.LeaderCallbacks{
OnStartedLeading: func(ctx context.Context) {
c, err := cluster.InitCluster(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:
}
klog.Infof("[%s] leading", cfg.LocalPeer.ID)
}
},
OnStoppedLeading: func() {
klog.Infof("[%s] stopped leading", cfg.LocalPeer.ID)
},
OnNewLeader: func(id string) {
klog.Infof("[%s] new leader: %s", cfg.LocalPeer.ID, id)
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:
}
}
},
})
if err != nil {