fusenapi/shared/sm.go

91 lines
2.5 KiB
Go
Raw Normal View History

2023-08-09 08:54:52 +00:00
package shared
2023-07-25 11:32:51 +00:00
import (
"context"
"encoding/gob"
"io"
"sync"
2023-07-30 10:50:27 +00:00
sm "github.com/lni/dragonboat/v4/statemachine"
2023-07-25 11:32:51 +00:00
"gorm.io/gorm"
)
2023-07-30 15:17:02 +00:00
// FsStateMachine is a simple key-value store as an FSM.
type FsStateMachine struct {
2023-07-30 10:50:27 +00:00
shardID uint64
replicaID uint64
2023-07-25 11:32:51 +00:00
2023-07-30 10:50:27 +00:00
mu sync.Mutex
2023-07-25 11:32:51 +00:00
store map[int64]*UserState
2023-07-30 10:50:27 +00:00
gdb *gorm.DB
// waiter *WaitCallback
// ra *raft.Raft // The consensus mechanism
2023-07-25 11:32:51 +00:00
}
2023-07-30 15:17:02 +00:00
func NewFsStateMachine(shardID uint64, replicaID uint64) sm.IStateMachine {
return &FsStateMachine{
shardID: shardID,
replicaID: replicaID,
store: make(map[int64]*UserState),
2023-08-22 05:43:13 +00:00
// gdb: initalize.InitMysql("fsreaderwriter:XErSYmLELKMnf3Dh@tcp(fusen.cdmigcvz3rle.us-east-2.rds.amazonaws.com:3306)/fusen"),
2023-07-30 15:17:02 +00:00
}
}
2023-07-30 10:50:27 +00:00
// Lookup performs local lookup on the ExampleStateMachine instance. In this example,
// we always return the Count value as a little endian binary encoded byte
// slice.
2023-07-30 15:17:02 +00:00
func (s *FsStateMachine) Lookup(query interface{}) (item interface{}, err error) {
2023-07-30 10:50:27 +00:00
s.mu.Lock()
defer s.mu.Unlock()
userid := query.(int64)
if us, ok := s.store[userid]; ok {
2023-07-25 11:32:51 +00:00
return us, nil
}
2023-07-30 10:50:27 +00:00
return nil, nil
2023-07-25 11:32:51 +00:00
}
2023-07-30 10:50:27 +00:00
// Update updates the object using the specified committed raft entry.
2023-07-30 15:17:02 +00:00
func (s *FsStateMachine) Update(e sm.Entry) (result sm.Result, err error) {
2023-08-09 08:17:24 +00:00
ctx := context.TODO()
ctx = context.WithValue(ctx, ctxEntry{}, &e)
ctx = context.WithValue(ctx, ctxSM{}, s)
return FsPasser.ExecuteWithBytes(ctx, e.Cmd)
2023-07-25 11:32:51 +00:00
}
2023-07-30 10:50:27 +00:00
// SaveSnapshot saves the current IStateMachine state into a snapshot using the
// specified io.Writer object.
2023-07-30 15:17:02 +00:00
func (s *FsStateMachine) SaveSnapshot(w io.Writer,
2023-07-30 10:50:27 +00:00
fc sm.ISnapshotFileCollection, done <-chan struct{}) error {
// as shown above, the only state that can be saved is the Count variable
// there is no external file in this IStateMachine example, we thus leave
// the fc untouched
2023-07-25 11:32:51 +00:00
2023-07-30 10:50:27 +00:00
s.mu.Lock()
defer s.mu.Unlock()
2023-07-25 11:32:51 +00:00
2023-07-30 10:50:27 +00:00
return gob.NewEncoder(w).Encode(&s.store)
2023-07-25 11:32:51 +00:00
}
2023-07-30 10:50:27 +00:00
// RecoverFromSnapshot recovers the state using the provided snapshot.
2023-07-30 15:17:02 +00:00
func (s *FsStateMachine) RecoverFromSnapshot(r io.Reader,
2023-07-30 10:50:27 +00:00
files []sm.SnapshotFile,
done <-chan struct{}) error {
// restore the Count variable, that is the only state we maintain in this
// example, the input files is expected to be empty
err := gob.NewDecoder(r).Decode(&s.store)
if err != nil {
2023-07-25 11:32:51 +00:00
return err
}
return nil
}
2023-07-30 10:50:27 +00:00
// Close closes the IStateMachine instance. There is nothing for us to cleanup
// or release as this is a pure in memory data store. Note that the Close
// method is not guaranteed to be called as node can crash at any time.
2023-07-30 15:17:02 +00:00
func (s *FsStateMachine) Close() error { return nil }