do
This commit is contained in:
parent
049aedf115
commit
7fd48edef8
39
fsm/fsm.go
39
fsm/fsm.go
@ -4,6 +4,7 @@ import (
|
|||||||
"context"
|
"context"
|
||||||
"encoding/gob"
|
"encoding/gob"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"fusenapi/initalize"
|
||||||
"fusenapi/model/gmodel"
|
"fusenapi/model/gmodel"
|
||||||
"fusenapi/utils/auth"
|
"fusenapi/utils/auth"
|
||||||
"io"
|
"io"
|
||||||
@ -16,11 +17,10 @@ import (
|
|||||||
"gorm.io/gorm"
|
"gorm.io/gorm"
|
||||||
)
|
)
|
||||||
|
|
||||||
// StateCluster is a simple key-value store as an FSM.
|
// FsStateMachine is a simple key-value store as an FSM.
|
||||||
type StateCluster struct {
|
type FsStateMachine struct {
|
||||||
shardID uint64
|
shardID uint64
|
||||||
replicaID uint64
|
replicaID uint64
|
||||||
nh *dragonboat.NodeHost
|
|
||||||
|
|
||||||
mu sync.Mutex
|
mu sync.Mutex
|
||||||
store map[int64]*UserState
|
store map[int64]*UserState
|
||||||
@ -30,9 +30,24 @@ type StateCluster struct {
|
|||||||
// ra *raft.Raft // The consensus mechanism
|
// ra *raft.Raft // The consensus mechanism
|
||||||
}
|
}
|
||||||
|
|
||||||
func (fsm *StateCluster) GetUserState(Userid int64) (us *UserState, err error) {
|
func NewFsStateMachine(shardID uint64, replicaID uint64) sm.IStateMachine {
|
||||||
|
return &FsStateMachine{
|
||||||
|
shardID: shardID,
|
||||||
|
replicaID: replicaID,
|
||||||
|
store: make(map[int64]*UserState),
|
||||||
|
gdb: initalize.InitMysql("fusentest:XErSYmLELKMnf3Dh@tcp(110.41.19.98:3306)/fusentest"),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
ius, err := fsm.nh.SyncRead(context.TODO(), fsm.shardID, Userid)
|
type SharedState struct {
|
||||||
|
shardID uint64
|
||||||
|
replicaID uint64
|
||||||
|
nh *dragonboat.NodeHost
|
||||||
|
}
|
||||||
|
|
||||||
|
func (fsm *SharedState) GetUserState(Userid int64) (us *UserState, err error) {
|
||||||
|
|
||||||
|
ius, err := nh.SyncRead(context.TODO(), fsm.shardID, Userid)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Println(err)
|
log.Println(err)
|
||||||
return nil, err
|
return nil, err
|
||||||
@ -47,9 +62,9 @@ func (fsm *StateCluster) GetUserState(Userid int64) (us *UserState, err error) {
|
|||||||
Key: Userid,
|
Key: Userid,
|
||||||
}
|
}
|
||||||
|
|
||||||
cs := fsm.nh.GetNoOPSession(128)
|
cs := nh.GetNoOPSession(128)
|
||||||
err = cmd.Encode(func(buf []byte) error {
|
err = cmd.Encode(func(buf []byte) error {
|
||||||
result, err := fsm.nh.SyncPropose(context.TODO(), cs, buf)
|
result, err := nh.SyncPropose(context.TODO(), cs, buf)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
@ -69,7 +84,7 @@ func (fsm *StateCluster) GetUserState(Userid int64) (us *UserState, err error) {
|
|||||||
// Lookup performs local lookup on the ExampleStateMachine instance. In this example,
|
// Lookup performs local lookup on the ExampleStateMachine instance. In this example,
|
||||||
// we always return the Count value as a little endian binary encoded byte
|
// we always return the Count value as a little endian binary encoded byte
|
||||||
// slice.
|
// slice.
|
||||||
func (s *StateCluster) Lookup(query interface{}) (item interface{}, err error) {
|
func (s *FsStateMachine) Lookup(query interface{}) (item interface{}, err error) {
|
||||||
s.mu.Lock()
|
s.mu.Lock()
|
||||||
defer s.mu.Unlock()
|
defer s.mu.Unlock()
|
||||||
|
|
||||||
@ -82,7 +97,7 @@ func (s *StateCluster) Lookup(query interface{}) (item interface{}, err error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Update updates the object using the specified committed raft entry.
|
// Update updates the object using the specified committed raft entry.
|
||||||
func (s *StateCluster) Update(e sm.Entry) (result sm.Result, err error) {
|
func (s *FsStateMachine) Update(e sm.Entry) (result sm.Result, err error) {
|
||||||
|
|
||||||
var cmd Command
|
var cmd Command
|
||||||
err = cmd.Decode(e.Cmd)
|
err = cmd.Decode(e.Cmd)
|
||||||
@ -129,7 +144,7 @@ func (s *StateCluster) Update(e sm.Entry) (result sm.Result, err error) {
|
|||||||
|
|
||||||
// SaveSnapshot saves the current IStateMachine state into a snapshot using the
|
// SaveSnapshot saves the current IStateMachine state into a snapshot using the
|
||||||
// specified io.Writer object.
|
// specified io.Writer object.
|
||||||
func (s *StateCluster) SaveSnapshot(w io.Writer,
|
func (s *FsStateMachine) SaveSnapshot(w io.Writer,
|
||||||
fc sm.ISnapshotFileCollection, done <-chan struct{}) error {
|
fc sm.ISnapshotFileCollection, done <-chan struct{}) error {
|
||||||
// as shown above, the only state that can be saved is the Count variable
|
// 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
|
// there is no external file in this IStateMachine example, we thus leave
|
||||||
@ -142,7 +157,7 @@ func (s *StateCluster) SaveSnapshot(w io.Writer,
|
|||||||
}
|
}
|
||||||
|
|
||||||
// RecoverFromSnapshot recovers the state using the provided snapshot.
|
// RecoverFromSnapshot recovers the state using the provided snapshot.
|
||||||
func (s *StateCluster) RecoverFromSnapshot(r io.Reader,
|
func (s *FsStateMachine) RecoverFromSnapshot(r io.Reader,
|
||||||
files []sm.SnapshotFile,
|
files []sm.SnapshotFile,
|
||||||
done <-chan struct{}) error {
|
done <-chan struct{}) error {
|
||||||
// restore the Count variable, that is the only state we maintain in this
|
// restore the Count variable, that is the only state we maintain in this
|
||||||
@ -159,4 +174,4 @@ func (s *StateCluster) RecoverFromSnapshot(r io.Reader,
|
|||||||
// Close closes the IStateMachine instance. There is nothing for us to cleanup
|
// 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
|
// 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.
|
// method is not guaranteed to be called as node can crash at any time.
|
||||||
func (s *StateCluster) Close() error { return nil }
|
func (s *FsStateMachine) Close() error { return nil }
|
||||||
|
29
fsm/main.go
29
fsm/main.go
@ -5,7 +5,6 @@ import (
|
|||||||
"encoding/gob"
|
"encoding/gob"
|
||||||
"flag"
|
"flag"
|
||||||
"fmt"
|
"fmt"
|
||||||
"fusenapi/initalize"
|
|
||||||
"fusenapi/utils/autoconfig"
|
"fusenapi/utils/autoconfig"
|
||||||
"log"
|
"log"
|
||||||
"net"
|
"net"
|
||||||
@ -17,26 +16,26 @@ import (
|
|||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/hashicorp/raft"
|
"github.com/hashicorp/raft"
|
||||||
"github.com/lni/dragonboat"
|
"github.com/lni/dragonboat/v4"
|
||||||
"github.com/lni/dragonboat/config"
|
"github.com/lni/dragonboat/v4/config"
|
||||||
"github.com/lni/dragonboat/logger"
|
"github.com/lni/dragonboat/v4/logger"
|
||||||
"gorm.io/gorm"
|
"gorm.io/gorm"
|
||||||
)
|
)
|
||||||
|
|
||||||
func test1() {
|
func test1() {
|
||||||
log.SetFlags(log.Llongfile)
|
// log.SetFlags(log.Llongfile)
|
||||||
|
|
||||||
fsm := StartNode("fs1", "localhost:5500", nil, initalize.InitMysql("fusentest:XErSYmLELKMnf3Dh@tcp(110.41.19.98:3306)/fusentest"))
|
// fsm := StartNode("fs1", "localhost:5500", nil, initalize.InitMysql("fusentest:XErSYmLELKMnf3Dh@tcp(110.41.19.98:3306)/fusentest"))
|
||||||
|
|
||||||
time.Sleep(time.Second * 5)
|
// time.Sleep(time.Second * 5)
|
||||||
|
|
||||||
for i := 0; i < 30; i++ {
|
// for i := 0; i < 30; i++ {
|
||||||
go log.Println(fsm.GetUserState(39))
|
// go log.Println(fsm.GetUserState(39))
|
||||||
}
|
// }
|
||||||
|
|
||||||
log.Println(fsm.GetUserState(39))
|
// log.Println(fsm.GetUserState(39))
|
||||||
|
|
||||||
select {}
|
// select {}
|
||||||
}
|
}
|
||||||
|
|
||||||
var addresses []string = []string{
|
var addresses []string = []string{
|
||||||
@ -125,7 +124,7 @@ func StartNode(replicaID uint64, exampleShardID uint64, addr string, gdb *gorm.D
|
|||||||
panic(err)
|
panic(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
if err := nh.StartReplica(initialMembers, false, NewSMQueue, rc); err != nil {
|
if err := nh.StartReplica(initialMembers, false, New, rc); err != nil {
|
||||||
fmt.Fprintf(os.Stderr, "failed to add cluster, %v\n", err)
|
fmt.Fprintf(os.Stderr, "failed to add cluster, %v\n", err)
|
||||||
os.Exit(1)
|
os.Exit(1)
|
||||||
}
|
}
|
||||||
@ -135,9 +134,9 @@ func StartNode(replicaID uint64, exampleShardID uint64, addr string, gdb *gorm.D
|
|||||||
}
|
}
|
||||||
|
|
||||||
// StartNode 启动节点
|
// StartNode 启动节点
|
||||||
func StartNode1(ServerID string, RaftBind string, serverconfigs []*autoconfig.ConfigServer, gdb *gorm.DB) *StateCluster {
|
func StartNode1(ServerID string, RaftBind string, serverconfigs []*autoconfig.ConfigServer, gdb *gorm.DB) *FsStateMachine {
|
||||||
|
|
||||||
fsm := &StateCluster{
|
fsm := &FsStateMachine{
|
||||||
store: make(map[int64]*UserState),
|
store: make(map[int64]*UserState),
|
||||||
|
|
||||||
gdb: gdb,
|
gdb: gdb,
|
||||||
|
@ -18,7 +18,7 @@ import (
|
|||||||
|
|
||||||
type ServiceContext struct {
|
type ServiceContext struct {
|
||||||
Config config.Config
|
Config config.Config
|
||||||
SharedState *fsm.StateCluster
|
SharedState *fsm.FsStateMachine
|
||||||
|
|
||||||
MysqlConn *gorm.DB
|
MysqlConn *gorm.DB
|
||||||
AllModels *gmodel.AllModelsGen
|
AllModels *gmodel.AllModelsGen
|
||||||
|
@ -17,7 +17,7 @@ import (
|
|||||||
|
|
||||||
type ServiceContext struct {
|
type ServiceContext struct {
|
||||||
Config config.Config
|
Config config.Config
|
||||||
SharedState *fsm.StateCluster
|
SharedState *fsm.FsStateMachine
|
||||||
|
|
||||||
MysqlConn *gorm.DB
|
MysqlConn *gorm.DB
|
||||||
AllModels *gmodel.AllModelsGen
|
AllModels *gmodel.AllModelsGen
|
||||||
|
@ -17,7 +17,7 @@ import (
|
|||||||
|
|
||||||
type ServiceContext struct {
|
type ServiceContext struct {
|
||||||
Config config.Config
|
Config config.Config
|
||||||
SharedState *fsm.StateCluster
|
SharedState *fsm.FsStateMachine
|
||||||
|
|
||||||
MysqlConn *gorm.DB
|
MysqlConn *gorm.DB
|
||||||
AllModels *gmodel.AllModelsGen
|
AllModels *gmodel.AllModelsGen
|
||||||
|
@ -17,7 +17,7 @@ import (
|
|||||||
|
|
||||||
type ServiceContext struct {
|
type ServiceContext struct {
|
||||||
Config config.Config
|
Config config.Config
|
||||||
SharedState *fsm.StateCluster
|
SharedState *fsm.FsStateMachine
|
||||||
|
|
||||||
MysqlConn *gorm.DB
|
MysqlConn *gorm.DB
|
||||||
AllModels *gmodel.AllModelsGen
|
AllModels *gmodel.AllModelsGen
|
||||||
|
@ -17,7 +17,7 @@ import (
|
|||||||
|
|
||||||
type ServiceContext struct {
|
type ServiceContext struct {
|
||||||
Config config.Config
|
Config config.Config
|
||||||
SharedState *fsm.StateCluster
|
SharedState *fsm.FsStateMachine
|
||||||
|
|
||||||
MysqlConn *gorm.DB
|
MysqlConn *gorm.DB
|
||||||
AllModels *gmodel.AllModelsGen
|
AllModels *gmodel.AllModelsGen
|
||||||
|
@ -17,7 +17,7 @@ import (
|
|||||||
|
|
||||||
type ServiceContext struct {
|
type ServiceContext struct {
|
||||||
Config config.Config
|
Config config.Config
|
||||||
SharedState *fsm.StateCluster
|
SharedState *fsm.FsStateMachine
|
||||||
|
|
||||||
MysqlConn *gorm.DB
|
MysqlConn *gorm.DB
|
||||||
AllModels *gmodel.AllModelsGen
|
AllModels *gmodel.AllModelsGen
|
||||||
|
@ -17,7 +17,7 @@ import (
|
|||||||
|
|
||||||
type ServiceContext struct {
|
type ServiceContext struct {
|
||||||
Config config.Config
|
Config config.Config
|
||||||
SharedState *fsm.StateCluster
|
SharedState *fsm.FsStateMachine
|
||||||
|
|
||||||
MysqlConn *gorm.DB
|
MysqlConn *gorm.DB
|
||||||
AllModels *gmodel.AllModelsGen
|
AllModels *gmodel.AllModelsGen
|
||||||
|
@ -17,7 +17,7 @@ import (
|
|||||||
|
|
||||||
type ServiceContext struct {
|
type ServiceContext struct {
|
||||||
Config config.Config
|
Config config.Config
|
||||||
SharedState *fsm.StateCluster
|
SharedState *fsm.FsStateMachine
|
||||||
|
|
||||||
MysqlConn *gorm.DB
|
MysqlConn *gorm.DB
|
||||||
AllModels *gmodel.AllModelsGen
|
AllModels *gmodel.AllModelsGen
|
||||||
|
@ -17,7 +17,7 @@ import (
|
|||||||
|
|
||||||
type ServiceContext struct {
|
type ServiceContext struct {
|
||||||
Config config.Config
|
Config config.Config
|
||||||
SharedState *fsm.StateCluster
|
SharedState *fsm.FsStateMachine
|
||||||
|
|
||||||
MysqlConn *gorm.DB
|
MysqlConn *gorm.DB
|
||||||
AllModels *gmodel.AllModelsGen
|
AllModels *gmodel.AllModelsGen
|
||||||
|
@ -17,7 +17,7 @@ import (
|
|||||||
|
|
||||||
type ServiceContext struct {
|
type ServiceContext struct {
|
||||||
Config config.Config
|
Config config.Config
|
||||||
SharedState *fsm.StateCluster
|
SharedState *fsm.FsStateMachine
|
||||||
|
|
||||||
MysqlConn *gorm.DB
|
MysqlConn *gorm.DB
|
||||||
AllModels *gmodel.AllModelsGen
|
AllModels *gmodel.AllModelsGen
|
||||||
|
@ -16,7 +16,7 @@ import (
|
|||||||
|
|
||||||
type ServiceContext struct {
|
type ServiceContext struct {
|
||||||
Config config.Config
|
Config config.Config
|
||||||
SharedState *fsm.StateCluster
|
SharedState *fsm.FsStateMachine
|
||||||
|
|
||||||
MysqlConn *gorm.DB
|
MysqlConn *gorm.DB
|
||||||
AllModels *gmodel.AllModelsGen
|
AllModels *gmodel.AllModelsGen
|
||||||
|
@ -17,7 +17,7 @@ import (
|
|||||||
|
|
||||||
type ServiceContext struct {
|
type ServiceContext struct {
|
||||||
Config config.Config
|
Config config.Config
|
||||||
SharedState *fsm.StateCluster
|
SharedState *fsm.FsStateMachine
|
||||||
|
|
||||||
MysqlConn *gorm.DB
|
MysqlConn *gorm.DB
|
||||||
AllModels *gmodel.AllModelsGen
|
AllModels *gmodel.AllModelsGen
|
||||||
|
@ -17,7 +17,7 @@ import (
|
|||||||
|
|
||||||
type ServiceContext struct {
|
type ServiceContext struct {
|
||||||
Config config.Config
|
Config config.Config
|
||||||
SharedState *fsm.StateCluster
|
SharedState *fsm.FsStateMachine
|
||||||
|
|
||||||
MysqlConn *gorm.DB
|
MysqlConn *gorm.DB
|
||||||
AllModels *gmodel.AllModelsGen
|
AllModels *gmodel.AllModelsGen
|
||||||
|
@ -19,7 +19,7 @@ import (
|
|||||||
|
|
||||||
type ServiceContext struct {
|
type ServiceContext struct {
|
||||||
Config config.Config
|
Config config.Config
|
||||||
SharedState *fsm.StateCluster
|
SharedState *fsm.FsStateMachine
|
||||||
|
|
||||||
MysqlConn *gorm.DB
|
MysqlConn *gorm.DB
|
||||||
AllModels *gmodel.AllModelsGen
|
AllModels *gmodel.AllModelsGen
|
||||||
|
@ -17,7 +17,7 @@ import (
|
|||||||
|
|
||||||
type ServiceContext struct {
|
type ServiceContext struct {
|
||||||
Config config.Config
|
Config config.Config
|
||||||
SharedState *fsm.StateCluster
|
SharedState *fsm.FsStateMachine
|
||||||
|
|
||||||
MysqlConn *gorm.DB
|
MysqlConn *gorm.DB
|
||||||
AllModels *gmodel.AllModelsGen
|
AllModels *gmodel.AllModelsGen
|
||||||
|
@ -17,7 +17,7 @@ import (
|
|||||||
|
|
||||||
type ServiceContext struct {
|
type ServiceContext struct {
|
||||||
Config config.Config
|
Config config.Config
|
||||||
SharedState *fsm.StateCluster
|
SharedState *fsm.FsStateMachine
|
||||||
|
|
||||||
MysqlConn *gorm.DB
|
MysqlConn *gorm.DB
|
||||||
AllModels *gmodel.AllModelsGen
|
AllModels *gmodel.AllModelsGen
|
||||||
|
@ -51,7 +51,7 @@ func NormalAfterLogic(w http.ResponseWriter, r *http.Request, resp *Response) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func RequestParse(w http.ResponseWriter, r *http.Request, state *fsm.StateCluster, LogicRequest any) (*auth.UserInfo, error) {
|
func RequestParse(w http.ResponseWriter, r *http.Request, state *fsm.FsStateMachine, LogicRequest any) (*auth.UserInfo, error) {
|
||||||
|
|
||||||
token, info, err := auth.ParseJwtTokenHeader[auth.UserInfo](r) //解析Token头, 和payload信息
|
token, info, err := auth.ParseJwtTokenHeader[auth.UserInfo](r) //解析Token头, 和payload信息
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user