-
Notifications
You must be signed in to change notification settings - Fork 46
/
transport.go
379 lines (309 loc) · 8.2 KB
/
transport.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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
package chord
import (
"errors"
"fmt"
"github.com/arriqaaq/chord/models"
"golang.org/x/net/context"
"google.golang.org/grpc"
"net"
"sync"
"sync/atomic"
"time"
)
var (
emptyNode = &models.Node{}
emptyRequest = &models.ER{}
emptyGetResponse = &models.GetResponse{}
emptySetResponse = &models.SetResponse{}
emptyDeleteResponse = &models.DeleteResponse{}
emptyRequestKeysResponse = &models.RequestKeysResponse{}
)
func Dial(addr string, opts ...grpc.DialOption) (*grpc.ClientConn, error) {
return grpc.Dial(addr, opts...)
}
/*
Transport enables a node to talk to the other nodes in
the ring
*/
type Transport interface {
Start() error
Stop() error
//RPC
GetSuccessor(*models.Node) (*models.Node, error)
FindSuccessor(*models.Node, []byte) (*models.Node, error)
GetPredecessor(*models.Node) (*models.Node, error)
Notify(*models.Node, *models.Node) error
CheckPredecessor(*models.Node) error
SetPredecessor(*models.Node, *models.Node) error
SetSuccessor(*models.Node, *models.Node) error
//Storage
GetKey(*models.Node, string) (*models.GetResponse, error)
SetKey(*models.Node, string, string) error
DeleteKey(*models.Node, string) error
RequestKeys(*models.Node, []byte, []byte) ([]*models.KV, error)
DeleteKeys(*models.Node, []string) error
}
type GrpcTransport struct {
config *Config
timeout time.Duration
maxIdle time.Duration
sock *net.TCPListener
pool map[string]*grpcConn
poolMtx sync.RWMutex
server *grpc.Server
shutdown int32
}
// func NewGrpcTransport(config *Config) (models.ChordClient, error) {
func NewGrpcTransport(config *Config) (*GrpcTransport, error) {
addr := config.Addr
// Try to start the listener
listener, err := net.Listen("tcp", addr)
if err != nil {
return nil, err
}
pool := make(map[string]*grpcConn)
// Setup the transport
grp := &GrpcTransport{
sock: listener.(*net.TCPListener),
timeout: config.Timeout,
maxIdle: config.MaxIdle,
pool: pool,
config: config,
}
grp.server = grpc.NewServer(config.ServerOpts...)
// Done
return grp, nil
}
type grpcConn struct {
addr string
client models.ChordClient
conn *grpc.ClientConn
lastActive time.Time
}
func (g *grpcConn) Close() {
g.conn.Close()
}
func (g *GrpcTransport) registerNode(node *Node) {
models.RegisterChordServer(g.server, node)
}
func (g *GrpcTransport) GetServer() *grpc.Server {
return g.server
}
// Gets an outbound connection to a host
func (g *GrpcTransport) getConn(
addr string,
) (models.ChordClient, error) {
g.poolMtx.RLock()
if atomic.LoadInt32(&g.shutdown) == 1 {
g.poolMtx.Unlock()
return nil, fmt.Errorf("TCP transport is shutdown")
}
cc, ok := g.pool[addr]
g.poolMtx.RUnlock()
if ok {
return cc.client, nil
}
var conn *grpc.ClientConn
var err error
conn, err = Dial(addr, g.config.DialOpts...)
if err != nil {
return nil, err
}
client := models.NewChordClient(conn)
cc = &grpcConn{addr, client, conn, time.Now()}
g.poolMtx.Lock()
if g.pool == nil {
g.poolMtx.Unlock()
return nil, errors.New("must instantiate node before using")
}
g.pool[addr] = cc
g.poolMtx.Unlock()
return client, nil
}
func (g *GrpcTransport) Start() error {
// Start RPC server
go g.listen()
// Reap old connections
go g.reapOld()
return nil
}
// Returns an outbound TCP connection to the pool
func (g *GrpcTransport) returnConn(o *grpcConn) {
// Update the last asctive time
o.lastActive = time.Now()
// Push back into the pool
g.poolMtx.Lock()
defer g.poolMtx.Unlock()
if atomic.LoadInt32(&g.shutdown) == 1 {
o.conn.Close()
return
}
g.pool[o.addr] = o
}
// Shutdown the TCP transport
func (g *GrpcTransport) Stop() error {
atomic.StoreInt32(&g.shutdown, 1)
// Close all the connections
g.poolMtx.Lock()
g.server.Stop()
for _, conn := range g.pool {
conn.Close()
}
g.pool = nil
g.poolMtx.Unlock()
return nil
}
// Closes old outbound connections
func (g *GrpcTransport) reapOld() {
ticker := time.NewTicker(60 * time.Second)
for {
if atomic.LoadInt32(&g.shutdown) == 1 {
return
}
select {
case <-ticker.C:
g.reap()
}
}
}
func (g *GrpcTransport) reap() {
g.poolMtx.Lock()
defer g.poolMtx.Unlock()
for host, conn := range g.pool {
if time.Since(conn.lastActive) > g.maxIdle {
conn.Close()
delete(g.pool, host)
}
}
}
// Listens for inbound connections
func (g *GrpcTransport) listen() {
g.server.Serve(g.sock)
}
// GetSuccessor the successor ID of a remote node.
func (g *GrpcTransport) GetSuccessor(node *models.Node) (*models.Node, error) {
client, err := g.getConn(node.Addr)
if err != nil {
return nil, err
}
ctx, cancel := context.WithTimeout(context.Background(), g.timeout)
defer cancel()
return client.GetSuccessor(ctx, emptyRequest)
}
// FindSuccessor the successor ID of a remote node.
func (g *GrpcTransport) FindSuccessor(node *models.Node, id []byte) (*models.Node, error) {
// fmt.Println("yo", node.Id, id)
client, err := g.getConn(node.Addr)
if err != nil {
return nil, err
}
ctx, cancel := context.WithTimeout(context.Background(), g.timeout)
defer cancel()
return client.FindSuccessor(ctx, &models.ID{Id: id})
}
// GetPredecessor the successor ID of a remote node.
func (g *GrpcTransport) GetPredecessor(node *models.Node) (*models.Node, error) {
client, err := g.getConn(node.Addr)
if err != nil {
return nil, err
}
ctx, cancel := context.WithTimeout(context.Background(), g.timeout)
defer cancel()
return client.GetPredecessor(ctx, emptyRequest)
}
func (g *GrpcTransport) SetPredecessor(node *models.Node, pred *models.Node) error {
client, err := g.getConn(node.Addr)
if err != nil {
return err
}
ctx, cancel := context.WithTimeout(context.Background(), g.timeout)
defer cancel()
_, err = client.SetPredecessor(ctx, pred)
return err
}
func (g *GrpcTransport) SetSuccessor(node *models.Node, succ *models.Node) error {
client, err := g.getConn(node.Addr)
if err != nil {
return err
}
ctx, cancel := context.WithTimeout(context.Background(), g.timeout)
defer cancel()
_, err = client.SetSuccessor(ctx, succ)
return err
}
func (g *GrpcTransport) Notify(node, pred *models.Node) error {
client, err := g.getConn(node.Addr)
if err != nil {
return err
}
ctx, cancel := context.WithTimeout(context.Background(), g.timeout)
defer cancel()
_, err = client.Notify(ctx, pred)
return err
}
func (g *GrpcTransport) CheckPredecessor(node *models.Node) error {
client, err := g.getConn(node.Addr)
if err != nil {
return err
}
ctx, cancel := context.WithTimeout(context.Background(), g.timeout)
defer cancel()
_, err = client.CheckPredecessor(ctx, &models.ID{Id: node.Id})
return err
}
func (g *GrpcTransport) GetKey(node *models.Node, key string) (*models.GetResponse, error) {
client, err := g.getConn(node.Addr)
if err != nil {
return nil, err
}
ctx, cancel := context.WithTimeout(context.Background(), g.timeout)
defer cancel()
return client.XGet(ctx, &models.GetRequest{Key: key})
}
func (g *GrpcTransport) SetKey(node *models.Node, key, value string) error {
client, err := g.getConn(node.Addr)
if err != nil {
return err
}
ctx, cancel := context.WithTimeout(context.Background(), g.timeout)
defer cancel()
_, err = client.XSet(ctx, &models.SetRequest{Key: key, Value: value})
return err
}
func (g *GrpcTransport) DeleteKey(node *models.Node, key string) error {
client, err := g.getConn(node.Addr)
if err != nil {
return err
}
ctx, cancel := context.WithTimeout(context.Background(), g.timeout)
defer cancel()
_, err = client.XDelete(ctx, &models.DeleteRequest{Key: key})
return err
}
func (g *GrpcTransport) RequestKeys(node *models.Node, from, to []byte) ([]*models.KV, error) {
client, err := g.getConn(node.Addr)
if err != nil {
return nil, err
}
ctx, cancel := context.WithTimeout(context.Background(), g.timeout)
defer cancel()
val, err := client.XRequestKeys(
ctx, &models.RequestKeysRequest{From: from, To: to},
)
if err != nil {
return nil, err
}
return val.Values, nil
}
func (g *GrpcTransport) DeleteKeys(node *models.Node, keys []string) error {
client, err := g.getConn(node.Addr)
if err != nil {
return err
}
ctx, cancel := context.WithTimeout(context.Background(), g.timeout)
defer cancel()
_, err = client.XMultiDelete(
ctx, &models.MultiDeleteRequest{Keys: keys},
)
return err
}