init
This commit is contained in:
1 parent
48eaf48797
commit
e32e55df15
20 files changed
+369
-117
No files matched your search
@@ -0,0 +1,32 @@
|
||||
package state
|
||||
|
||||
import "github.com/integration-system/isp-lib/structure"
|
||||
|
||||
type Mesh struct {
|
||||
modulesMap map[string]NodesMap
|
||||
}
|
||||
|
||||
func (m *Mesh) UpsertBackend(backend structure.BackendDeclaration) {
|
||||
address := backend.Address.GetAddress()
|
||||
if nodes, ok := m.modulesMap[backend.ModuleName]; ok {
|
||||
nodes[address] = backend
|
||||
} else {
|
||||
m.modulesMap[backend.ModuleName] = NodesMap{address: backend}
|
||||
}
|
||||
}
|
||||
|
||||
func (m *Mesh) DeleteBackend(backend structure.BackendDeclaration) {
|
||||
address := backend.Address.GetAddress()
|
||||
if nodes, ok := m.modulesMap[backend.ModuleName]; ok {
|
||||
delete(nodes, address)
|
||||
if len(nodes) == 0 {
|
||||
delete(m.modulesMap, backend.ModuleName)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
type NodesMap map[string]structure.BackendDeclaration
|
||||
|
||||
func NewMesh() *Mesh {
|
||||
return &Mesh{modulesMap: make(map[string]NodesMap)}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
package state
|
||||
|
||||
type state struct {
|
||||
mesh *Mesh
|
||||
}
|
||||
|
||||
func newState() state {
|
||||
return state{
|
||||
mesh: NewMesh(),
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,81 @@
|
||||
package state
|
||||
|
||||
import (
|
||||
"github.com/hashicorp/raft"
|
||||
jsoniter "github.com/json-iterator/go"
|
||||
"github.com/mohae/deepcopy"
|
||||
"github.com/pkg/errors"
|
||||
"io"
|
||||
"sync"
|
||||
)
|
||||
|
||||
var (
|
||||
json = jsoniter.ConfigFastest
|
||||
)
|
||||
|
||||
type Store struct {
|
||||
state state
|
||||
lock sync.Mutex
|
||||
}
|
||||
|
||||
func (f *Store) Apply(l *raft.Log) interface{} {
|
||||
f.lock.Lock()
|
||||
defer f.lock.Unlock()
|
||||
return nil
|
||||
}
|
||||
|
||||
// Snapshot returns a snapshot of the key-value store.
|
||||
func (f *Store) Snapshot() (raft.FSMSnapshot, error) {
|
||||
f.lock.Lock()
|
||||
defer f.lock.Unlock()
|
||||
|
||||
copied := deepcopy.Copy(f.state).(state)
|
||||
|
||||
return &fsmSnapshot{copied}, nil
|
||||
}
|
||||
|
||||
// Restore stores the key-value store to a previous state.
|
||||
func (f *Store) Restore(rc io.ReadCloser) error {
|
||||
s := state{}
|
||||
|
||||
if err := json.NewDecoder(rc).Decode(&s); err != nil {
|
||||
return errors.WithMessage(err, "unmarshal state")
|
||||
}
|
||||
|
||||
f.state = s
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
type fsmSnapshot struct {
|
||||
state state
|
||||
}
|
||||
|
||||
func (f *fsmSnapshot) Persist(sink raft.SnapshotSink) error {
|
||||
err := func() error {
|
||||
b, err := json.Marshal(f.state)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if _, err := sink.Write(b); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return sink.Close()
|
||||
}()
|
||||
|
||||
if err != nil {
|
||||
_ = sink.Cancel()
|
||||
}
|
||||
|
||||
return err
|
||||
}
|
||||
|
||||
func (f *fsmSnapshot) Release() {}
|
||||
|
||||
func NewStateStore() *Store {
|
||||
return &Store{
|
||||
state: newState(),
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user