-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver_setup.go
More file actions
162 lines (130 loc) · 3.35 KB
/
server_setup.go
File metadata and controls
162 lines (130 loc) · 3.35 KB
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
package raft
import (
"fmt"
"log"
"net"
"net/rpc"
"sync"
)
// Server
type Server struct {
mu sync.Mutex
serverId int
peersIds []int
RPCServer *rpc.Server
listener net.Listener
peerClients map[int]*rpc.Client
ready <-chan interface{}
quit chan interface{}
wg sync.WaitGroup
raftLogic *RaftNode // Added in RaftLogic component
minRPCLatency int
}
func NewServer(serverId int, peersIds []int, ready <-chan interface{}, minRPCLatency int) *Server {
this := new(Server)
this.serverId = serverId
this.peersIds = peersIds
this.peerClients = make(map[int]*rpc.Client)
this.ready = ready
this.quit = make(chan interface{})
this.minRPCLatency = minRPCLatency
return this
}
func (this *Server) Serve() {
this.mu.Lock()
// Add in logic component
this.raftLogic = NewRaftNode(this.serverId, this.peersIds, this, this.ready)
// Create a new RPC server
this.RPCServer = rpc.NewServer()
this.RPCServer.RegisterName("RaftNode", this)
var err error
if this.listener, err = net.Listen("tcp", ":0"); err != nil {
log.Fatal(err)
}
log.Printf("[%v] listening at %v", this.serverId, this.listener.Addr())
this.mu.Unlock()
this.wg.Add(1)
go func() {
defer this.wg.Done()
for {
conn, err := this.listener.Accept()
if err != nil {
select {
case <-this.quit:
return
default:
log.Fatal("accept error:", err)
}
}
this.wg.Add(1)
go func() {
this.RPCServer.ServeConn(conn)
this.wg.Done()
}()
}
}()
}
func (this *Server) GetCurrentAddress() net.Addr {
this.mu.Lock()
defer this.mu.Unlock()
return this.listener.Addr()
}
func (this *Server) SendRPCCallTo(id int, serviceMethod string, args interface{}, reply interface{}) error {
this.mu.Lock()
peer := this.peerClients[id]
this.mu.Unlock()
if peer == nil {
return fmt.Errorf("call client %d after it'this closed", id)
} else {
return peer.Call(serviceMethod, args, reply)
}
}
func (this *Server) Shutdown() {
this.raftLogic.KillNode() // Make sure heartbeats and requests stop
close(this.quit)
this.listener.Close()
this.wg.Wait()
}
/* Functions that facilitate peer to peer connection/disconnection */
func (this *Server) ConnectToPeer(peerId int, addr net.Addr) error {
this.mu.Lock()
defer this.mu.Unlock()
if this.peerClients[peerId] == nil {
client, err := rpc.Dial(addr.Network(), addr.String())
if err != nil {
return err
}
this.peerClients[peerId] = client
}
return nil
}
func (this *Server) DisconnectPeer(peerId int) error {
this.mu.Lock()
defer this.mu.Unlock()
if this.peerClients[peerId] != nil {
err := this.peerClients[peerId].Close()
this.peerClients[peerId] = nil
return err
}
return nil
}
func (this *Server) DisconnectAll() {
this.mu.Lock()
defer this.mu.Unlock()
for id := range this.peerClients {
if this.peerClients[id] != nil {
this.peerClients[id].Close()
this.peerClients[id] = nil
}
}
}
// Register Custom Methods here:
/* To actually add a delay for each request, a wrapper */
func (this *Server) RequestVote(args RequestVoteArgs, reply *RequestVoteReply) error {
sleepMs(this.minRPCLatency + args.Latency) // Add Latency
return this.raftLogic.HandleRequestVote(args, reply)
}
func (this *Server) AppendEntries(args AppendEntriesArgs, reply *AppendEntriesReply) error {
sleepMs(this.minRPCLatency + args.Latency) // Add Latency
return this.raftLogic.HandleAppendEntries(args, reply)
}