fusen-render/main.go
2023-07-28 19:04:21 +08:00

89 lines
1.9 KiB
Go

package fusenrender
import (
"fmt"
"log"
"net"
"os"
"time"
"github.com/hashicorp/raft"
)
func main() {
}
func StartNode(ServerID string, RaftBind string, serverconfigs []*ConfigServer) *FsmQueue {
fsm := NewQueueFSM(fmt.Sprintf("/tmp/fusenrender/%s", ServerID))
var retainSnapshotCount = 2
// var ServerID string = "fs1"
// var RaftBind string = "localhost:5500"
var RaftDir string = fmt.Sprintf("/tmp/raftdir/%s", ServerID)
// Setup Raft configuration.
config := raft.DefaultConfig()
config.LocalID = raft.ServerID(ServerID)
// Setup Raft communication.
addr, err := net.ResolveTCPAddr("tcp", RaftBind)
if err != nil {
panic(err)
}
transport, err := raft.NewTCPTransport(RaftBind, addr, 3, 30*time.Second, os.Stderr)
if err != nil {
panic(err)
}
// Create the snapshot store. This allows the Raft to truncate the log.
snapshots, err := raft.NewFileSnapshotStore(RaftDir, retainSnapshotCount, os.Stderr)
if err != nil {
panic(fmt.Errorf("file snapshot store: %s", err))
}
// Create the log store and stable store.
logStore := raft.NewInmemStore()
stableStore := raft.NewInmemStore()
// Create the Raft system.
fsm.ra, err = raft.NewRaft(config, fsm, logStore, stableStore, snapshots, transport)
if err != nil {
panic(err)
}
var dup map[string]bool = make(map[string]bool)
var rserver []raft.Server = []raft.Server{
{
Suffrage: raft.Voter,
ID: config.LocalID,
Address: transport.LocalAddr(),
},
}
dup[string(config.LocalID)] = true
for _, cfg := range serverconfigs {
if _, ok := dup[cfg.ServerID]; !ok {
dup[cfg.ServerID] = true
rserver = append(rserver, raft.Server{
Suffrage: raft.Voter,
ID: raft.ServerID(cfg.ServerID),
Address: raft.ServerAddress(cfg.Address()),
})
}
}
configuration := raft.Configuration{
Servers: rserver,
}
fu := fsm.ra.BootstrapCluster(configuration)
if err := fu.Error(); err != nil {
log.Println(err)
}
waitForCluster(fsm.ra)
return fsm
}