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

@ -3,39 +3,37 @@ package cluster
import (
"context"
"errors"
"github.com/hashicorp/raft"
)
// Cluster - The Cluster object manages the state of the cluster for a particular node
type Cluster struct {
stateMachine FSM
stop chan struct{}
done chan struct{}
Callbacks LeaderCallbacks
kv *KV
r *raft.Raft
stop chan struct{}
done chan struct{}
Callbacks Callbacks
}
type LeaderCallbacks struct {
// OnStartedLeading is called when a LeaderElector client starts leading
OnStartedLeading func(context.Context)
// OnStoppedLeading is called when a LeaderElector client stops leading
OnStoppedLeading func()
// OnNewLeader is called when the client observes a leader that is
// not the previously observed leader. This includes the first observed
// leader when the client starts.
OnNewLeader func(string)
type Callbacks struct {
Leader func(context.Context, *KV)
Follower func(context.Context, *KV)
}
// InitCluster - Will attempt to initialise all of the required settings for the cluster
func InitCluster(callbacks LeaderCallbacks) (*Cluster, error) {
if callbacks.OnStartedLeading == nil {
return nil, errors.New("OnStartedLeading is nil")
func InitCluster(callbacks Callbacks) (*Cluster, error) {
if callbacks.Leader == nil {
return nil, errors.New("Leader is nil")
}
if callbacks.OnStoppedLeading == nil {
return nil, errors.New("OnStoppedLeading is nil")
}
if callbacks.OnNewLeader == nil {
return nil, errors.New("OnNewLeader is nil")
if callbacks.Follower == nil {
return nil, errors.New("Follower is nil")
}
c := &Cluster{
kv: &KV{
m: make(map[string][]byte),
},
stop: make(chan struct{}, 1),
done: make(chan struct{}, 1),
Callbacks: callbacks,