mirror of
https://github.com/EchoVault/SugarDB.git
synced 2025-10-29 10:32:36 +08:00
Moved raft init logic to raft.go file
This commit is contained in:
@@ -14,7 +14,6 @@ import (
|
|||||||
"plugin"
|
"plugin"
|
||||||
"strings"
|
"strings"
|
||||||
"sync"
|
"sync"
|
||||||
"time"
|
|
||||||
|
|
||||||
"github.com/hashicorp/memberlist"
|
"github.com/hashicorp/memberlist"
|
||||||
"github.com/hashicorp/raft"
|
"github.com/hashicorp/raft"
|
||||||
@@ -240,62 +239,6 @@ func (server *Server) Start() {
|
|||||||
server.config.Addr = addr
|
server.config.Addr = addr
|
||||||
}
|
}
|
||||||
|
|
||||||
raftConfig := raft.DefaultConfig()
|
|
||||||
raftConfig.LocalID = raft.ServerID(conf.ServerID)
|
|
||||||
|
|
||||||
raftLogStore := raft.NewInmemStore()
|
|
||||||
raftStableStore := raft.NewInmemStore()
|
|
||||||
raftSnapshotStore := raft.NewInmemSnapshotStore()
|
|
||||||
|
|
||||||
raftAddr := fmt.Sprintf("%s:%d", conf.Addr, conf.ClusterPort)
|
|
||||||
raftAdvertiseAddr, err := net.ResolveTCPAddr("tcp", raftAddr)
|
|
||||||
if err != nil {
|
|
||||||
log.Fatal("Could not resolve advertise address.")
|
|
||||||
}
|
|
||||||
|
|
||||||
raftTransport, err := raft.NewTCPTransport(
|
|
||||||
raftAddr,
|
|
||||||
raftAdvertiseAddr,
|
|
||||||
10,
|
|
||||||
500*time.Millisecond,
|
|
||||||
os.Stdout,
|
|
||||||
)
|
|
||||||
|
|
||||||
if err != nil {
|
|
||||||
log.Fatal(err)
|
|
||||||
}
|
|
||||||
|
|
||||||
// Start raft server
|
|
||||||
raftServer, err := raft.NewRaft(
|
|
||||||
raftConfig,
|
|
||||||
&raft.MockFSM{},
|
|
||||||
raftLogStore,
|
|
||||||
raftStableStore,
|
|
||||||
raftSnapshotStore,
|
|
||||||
raftTransport,
|
|
||||||
)
|
|
||||||
|
|
||||||
if err != nil {
|
|
||||||
log.Fatalf("Could not start node with error; %s", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
server.raft = raftServer
|
|
||||||
|
|
||||||
if conf.JoinAddr == "" {
|
|
||||||
// Bootstrap raft cluster
|
|
||||||
if err := server.raft.BootstrapCluster(raft.Configuration{
|
|
||||||
Servers: []raft.Server{
|
|
||||||
{
|
|
||||||
Suffrage: raft.Voter,
|
|
||||||
ID: raft.ServerID(conf.ServerID),
|
|
||||||
Address: raft.ServerAddress(raftAddr),
|
|
||||||
},
|
|
||||||
},
|
|
||||||
}).Error(); err != nil {
|
|
||||||
log.Fatal(err)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if conf.HTTP {
|
if conf.HTTP {
|
||||||
server.StartHTTP()
|
server.StartHTTP()
|
||||||
} else {
|
} else {
|
||||||
|
|||||||
@@ -1,13 +1,76 @@
|
|||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"fmt"
|
||||||
"io"
|
"io"
|
||||||
|
"log"
|
||||||
|
"net"
|
||||||
|
"os"
|
||||||
|
"time"
|
||||||
|
|
||||||
"github.com/hashicorp/raft"
|
"github.com/hashicorp/raft"
|
||||||
)
|
)
|
||||||
|
|
||||||
func (server *Server) RaftInit() {
|
func (server *Server) RaftInit() {
|
||||||
// Triggered after MemberList init
|
// Triggered after MemberList init
|
||||||
|
|
||||||
|
conf := server.config
|
||||||
|
|
||||||
|
raftConfig := raft.DefaultConfig()
|
||||||
|
raftConfig.LocalID = raft.ServerID(conf.ServerID)
|
||||||
|
|
||||||
|
raftLogStore := raft.NewInmemStore()
|
||||||
|
raftStableStore := raft.NewInmemStore()
|
||||||
|
raftSnapshotStore := raft.NewInmemSnapshotStore()
|
||||||
|
|
||||||
|
raftAddr := fmt.Sprintf("%s:%d", conf.Addr, conf.ClusterPort)
|
||||||
|
raftAdvertiseAddr, err := net.ResolveTCPAddr("tcp", raftAddr)
|
||||||
|
if err != nil {
|
||||||
|
log.Fatal("Could not resolve advertise address.")
|
||||||
|
}
|
||||||
|
|
||||||
|
raftTransport, err := raft.NewTCPTransport(
|
||||||
|
raftAddr,
|
||||||
|
raftAdvertiseAddr,
|
||||||
|
10,
|
||||||
|
500*time.Millisecond,
|
||||||
|
os.Stdout,
|
||||||
|
)
|
||||||
|
|
||||||
|
if err != nil {
|
||||||
|
log.Fatal(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Start raft server
|
||||||
|
raftServer, err := raft.NewRaft(
|
||||||
|
raftConfig,
|
||||||
|
&raft.MockFSM{},
|
||||||
|
raftLogStore,
|
||||||
|
raftStableStore,
|
||||||
|
raftSnapshotStore,
|
||||||
|
raftTransport,
|
||||||
|
)
|
||||||
|
|
||||||
|
if err != nil {
|
||||||
|
log.Fatalf("Could not start node with error; %s", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
server.raft = raftServer
|
||||||
|
|
||||||
|
if conf.JoinAddr == "" {
|
||||||
|
// Bootstrap raft cluster
|
||||||
|
if err := server.raft.BootstrapCluster(raft.Configuration{
|
||||||
|
Servers: []raft.Server{
|
||||||
|
{
|
||||||
|
Suffrage: raft.Voter,
|
||||||
|
ID: raft.ServerID(conf.ServerID),
|
||||||
|
Address: raft.ServerAddress(raftAddr),
|
||||||
|
},
|
||||||
|
},
|
||||||
|
}).Error(); err != nil {
|
||||||
|
log.Fatal(err)
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (server *Server) RaftShutdown() {
|
func (server *Server) RaftShutdown() {
|
||||||
|
|||||||
Reference in New Issue
Block a user