-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.go
170 lines (135 loc) · 4.06 KB
/
server.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
package raft
import (
"context"
"fmt"
"github.com/lquyet/raft/config"
proto "github.com/lquyet/raft/pb"
"google.golang.org/grpc"
"google.golang.org/grpc/credentials/insecure"
"net"
"os"
"os/signal"
"sync"
"syscall"
)
// Server holds references to set up a Raft Node
type Server struct {
mu sync.Mutex
id int32 // Node identifier
addr string // Address of this server
peerIds []int32 // List of peers in cluster
peerAddrs map[int32]string // Map of peer IDs to network addresses
peerClients map[int32]proto.RaftServiceClient // RPC clients to peers
peerClientConns map[int32]*grpc.ClientConn
ready chan interface{} // Channel to signal when the server is ready to start
stop chan os.Signal
config config.Config
wg sync.WaitGroup
raftModule *RaftModule // The consensus module
listener net.Listener
grpcServer *grpc.Server
proto.UnimplementedRaftServiceServer
}
func (s *Server) Submit(ctx context.Context, in *proto.SubmitRequest) (*proto.SubmitResponse, error) {
return s.raftModule.Submit(ctx, in)
}
func (s *Server) RequestVote(ctx context.Context, in *proto.RequestVoteRequest) (*proto.RequestVoteResponse, error) {
return s.raftModule.RequestVote(ctx, in)
}
func (s *Server) AppendEntries(ctx context.Context, in *proto.AppendEntriesRequest) (*proto.AppendEntriesResponse, error) {
return s.raftModule.AppendEntries(ctx, in)
}
func NewServer(serverId int32, peerIds []int32, peerAddrs map[int32]string, ready chan interface{}, addr string) *Server {
s := Server{}
s.id = serverId
s.peerIds = peerIds
s.peerAddrs = peerAddrs
s.ready = ready
s.addr = addr
s.peerClients = make(map[int32]proto.RaftServiceClient)
s.peerClientConns = make(map[int32]*grpc.ClientConn)
s.mu = sync.Mutex{}
return &s
}
func NewServerWithConfigFile(configName string, configType string, ready chan interface{}) *Server {
s := Server{}
s.config = config.Load(configName, configType)
s.id = s.config.Server.Id
s.peerIds = s.config.Server.PeerIds
s.peerAddrs = s.config.Server.PeerAddresses
s.ready = ready
s.addr = s.config.Server.Address
s.peerClients = make(map[int32]proto.RaftServiceClient)
s.peerClientConns = make(map[int32]*grpc.ClientConn)
s.mu = sync.Mutex{}
return &s
}
func (s *Server) Serve() {
s.mu.Lock()
s.raftModule = NewRaftModule(s.id, s.peerIds, s, s.ready)
s.mu.Unlock()
s.grpcServer = grpc.NewServer()
proto.RegisterRaftServiceServer(s.grpcServer, s)
s.wg = sync.WaitGroup{}
s.wg.Add(1)
go func() {
defer s.wg.Done()
listener, err := net.Listen("tcp", s.addr)
if err != nil {
panic(err)
}
//fmt.Println("Server started at", s.addr)
err = s.grpcServer.Serve(listener)
if err != nil {
panic(err)
}
fmt.Println("Shutdown gRPC server successfully")
}()
//close(s.ready)
s.stop = make(chan os.Signal, 1)
signal.Notify(s.stop, os.Interrupt, syscall.SIGTERM)
<-s.stop
s.grpcServer.GracefulStop()
}
func (s *Server) ConnectToPeer(peerId int32) error {
s.mu.Lock()
defer s.mu.Unlock()
//if _, found := s.peerClients[peerId]; !found {
// conn, err := grpc.Dial(s.peerAddrs[peerId], grpc.WithTransportCredentials(insecure.NewCredentials()))
// if err != nil {
// return err
// }
// s.peerClients[peerId] = proto.NewRaftServiceClient(conn)
// s.peerClientConns[peerId] = conn
//}
conn, err := grpc.Dial(s.peerAddrs[peerId], grpc.WithTransportCredentials(insecure.NewCredentials()))
if err != nil {
return err
}
s.peerClients[peerId] = proto.NewRaftServiceClient(conn)
s.peerClientConns[peerId] = conn
return nil
}
func (s *Server) GetRaftModule() *RaftModule {
return s.raftModule
}
func (s *Server) Shutdown() {
s.stop <- syscall.SIGTERM
s.wg.Wait()
s.raftModule.Stop()
}
func (s *Server) DisconnectAll() {
s.mu.Lock()
defer s.mu.Unlock()
for id := range s.peerClients {
s.peerClientConns[id].Close()
}
}
func (s *Server) DisconnectPeer(peerId int32) error {
s.mu.Lock()
defer s.mu.Unlock()
if s.peerClients[peerId] != nil {
s.peerClientConns[peerId].Close()
}
return nil
}