-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.go
196 lines (169 loc) · 3.99 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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
package liteRaft
import (
"fmt"
"log"
"math/rand"
"net"
"net/rpc"
"os"
"sync"
"time"
)
/*
const (
retainSnapshotCount = 2
raftTimeout = 2 * time.Second
)
type command struct {
Op string `json:"op,omitempty"`
Key string `json:"key,omitempty"`
Value string `json:"value,omitempty"`
Addr string `json:"addr,omitempty"`
}
*/
// Server wraps liteRaft.ConsensusModule with an rpc.Server that exposes its methods as RPC endpoints
type Server struct {
mu sync.Mutex
serverId int
peerIds []int
cm *ConsensusModule
rpcProxy *RPCProxy
rpcServer *rpc.Server
listener net.Listener
peerClients map[int]*rpc.Client
ready <-chan interface{}
quit chan interface{}
wg sync.WaitGroup
}
func NewServer(serverId int, peerIds []int, ready <-chan interface{}) *Server {
s := new(Server)
s.serverId = serverId
s.peerIds = peerIds
s.peerClients = make(map[int]*rpc.Client)
s.ready = ready
s.quit = make(chan interface{})
return s
}
func (s *Server) Serve() {
s.mu.Lock()
s.cm = NewConsensusModule(s.serverId, s.peerIds, s, s.ready)
// create a new RPC server and reg an RPCProxy to fwd all methods to n.cm
s.rpcServer = rpc.NewServer()
s.rpcProxy = &RPCProxy{cm: s.cm}
s.rpcServer.RegisterName("ConsensusModule", s.rpcProxy)
var err error
s.listener, err = net.Listen("tcp", ":0")
if err != nil {
log.Fatal(err)
}
log.Printf("[%v] listening at %s", s.serverId, s.listener.Addr())
s.mu.Unlock()
s.wg.Add(1)
go func() {
defer s.wg.Done()
for {
conn, err := s.listener.Accept()
if err != nil {
select {
case <-s.quit:
return
default:
log.Fatal("accept error:", err)
}
}
s.wg.Add(1)
go func() {
s.rpcServer.ServeConn(conn)
s.wg.Done()
}()
}
}()
}
// close all client connection to peers for this server
func (s *Server) DisconnectAll() {
s.mu.Lock()
defer s.mu.Unlock()
for id := range s.peerClients {
if s.peerClients[id] != nil {
s.peerClients[id].Close()
s.peerClients[id] = nil
}
}
}
// close the server and wait for it to properly shutdown
func (s *Server) Shutdown() {
s.cm.Stop()
close(s.quit)
s.listener.Close()
s.wg.Wait()
}
func (s *Server) GetListenerAddr() net.Addr {
s.mu.Lock()
defer s.mu.Unlock()
return s.listener.Addr()
}
func (s *Server) ConnectToPeer(peerId int, addr net.Addr) error {
s.mu.Lock()
defer s.mu.Unlock()
if s.peerClients[peerId] == nil {
client, err := rpc.Dial(addr.Network(), addr.String())
if err != nil {
return err
}
s.peerClients[peerId] = client
}
return nil
}
func (s *Server) DisconnectPeer(peerId int) error {
s.mu.Lock()
defer s.mu.Unlock()
if s.peerClients[peerId] != nil {
err := s.peerClients[peerId].Close()
s.peerClients[peerId] = nil
return err
}
return nil
}
func (s *Server) Call(id int, serviceMethod string, args interface{}, reply interface{}) error {
s.mu.Lock()
peer := s.peerClients[id]
s.mu.Unlock()
if peer == nil {
return fmt.Errorf("call client %d after it's closed", id)
} else {
return peer.Call(serviceMethod, args, reply)
}
}
type RPCProxy struct {
cm *ConsensusModule
}
func (rpp *RPCProxy) RequestVote(args RequestVoteArgs, reply *RequestVoteReply) error {
if len(os.Getenv("RAFT_UNRELIABLE_RPC")) > 0 {
dice := rand.Intn(10)
if dice == 9 {
rpp.cm.debugLog("drop RequestVote")
return fmt.Errorf("RPC Failed")
} else if dice == 8 {
rpp.cm.debugLog("delay RequestVote")
time.Sleep(75 * time.Millisecond)
}
} else {
time.Sleep(time.Duration(1+rand.Intn(5)) * time.Millisecond)
}
return rpp.cm.RequestVote(args, reply)
}
func (rpp *RPCProxy) AppendEntries(args AppendEntriesArgs, reply *AppendEntriesReply) error {
if len(os.Getenv("RAFT_UNRELIABLE_RPC")) > 0 {
dice := rand.Intn(10)
if dice == 9 {
rpp.cm.debugLog("drop RequestVote")
return fmt.Errorf("RPC Failed")
} else if dice == 8 {
rpp.cm.debugLog("delay RequestVote")
time.Sleep(75 * time.Millisecond)
}
} else {
time.Sleep(time.Duration(1+rand.Intn(5)) * time.Millisecond)
}
return rpp.cm.AppendEntries(args, reply)
}