Files
isp-config-service/store/store.go
T
ykomissarov 5d4b54c39c Merge remote-tracking branch 'origin/v2' into v2_send_config_schema
# Conflicts:
#	Gopkg.lock
#	cluster/const.go
#	store/apply.go
#	store/state/modules.go
#	store/state/state.go
#	store/store.go
#	subs/client.go
2019-10-11 18:22:55 +03:00

119 lines
2.6 KiB
Go

package store
import (
"encoding/binary"
"github.com/hashicorp/raft"
log "github.com/integration-system/isp-log"
jsoniter "github.com/json-iterator/go"
"github.com/mohae/deepcopy"
"github.com/pkg/errors"
"io"
"isp-config-service/cluster"
"isp-config-service/codes"
"isp-config-service/store/state"
"sync"
)
var (
json = jsoniter.ConfigFastest
)
type Store struct {
state state.State
lock sync.RWMutex
handlers map[uint64]func([]byte) error
}
func (s *Store) Apply(l *raft.Log) interface{} {
var returnError error
s.lock.Lock()
defer s.lock.Unlock()
if len(l.Data) < 8 {
log.Fatalf(codes.ApplyLogCommandError, "invalid log data command: %s", l.Data)
}
command := binary.BigEndian.Uint64(l.Data[:8])
if handler, ok := s.handlers[command]; ok {
err := handler(l.Data[8:])
if err != nil {
returnError = err
}
} else {
log.Fatalf(codes.ApplyLogCommandError, "unknown log command %s", command)
}
var logResponse cluster.ApplyLogResponse
if returnError != nil {
logResponse.ApplyError = returnError.Error()
}
return logResponse
}
func (s *Store) Snapshot() (raft.FSMSnapshot, error) {
s.lock.Lock()
copied := deepcopy.Copy(s.state).(state.State)
s.lock.Unlock()
return &fsmSnapshot{copied}, nil
}
func (s *Store) Restore(rc io.ReadCloser) error {
state2 := state.State{}
if err := json.NewDecoder(rc).Decode(&state2); err != nil {
return errors.WithMessage(err, "unmarshal store")
}
s.state = state2
return nil
}
func (s *Store) GetReadState() state.ReadState {
return s.state
}
func (s *Store) VisitReadState(f func(state.ReadState)) {
s.lock.RLock()
defer s.lock.RUnlock()
f(s.GetReadState())
}
func (s *Store) VisitState(f func(state.State)) {
s.lock.Lock()
defer s.lock.Unlock()
f(s.state)
}
type fsmSnapshot struct {
state 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(st state.State) *Store {
store := &Store{
state: st,
}
store.handlers = map[uint64]func([]byte) error{
cluster.UpdateBackendDeclarationCommand: store.applyUpdateBackendDeclarationCommand,
cluster.DeleteBackendDeclarationCommand: store.applyDeleteBackendDeclarationCommand,
cluster.ModuleConnectedCommand: store.applyModuleConnectedCommand,
cluster.UpdateConfigSchemaCommand: store.applyUpdateConfigSchema,
}
return store
}