-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpeer.go
More file actions
168 lines (129 loc) · 4.52 KB
/
Copy pathpeer.go
File metadata and controls
168 lines (129 loc) · 4.52 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
163
164
165
166
167
168
package raft
import (
"context"
"net"
"sync"
"github.com/navgeet/raft/internal/errors"
pb "github.com/navgeet/raft/internal/protobuf"
"google.golang.org/grpc"
"google.golang.org/grpc/credentials/insecure"
)
var (
errNoConnectionToPeer = errors.New("no connection with peer")
)
// Peer is an interface representing a component responsible for establishing a connection
// with and making RPCs to a Raft server.
type Peer interface {
// ID returns the ID of the peer.
ID() string
// Connect establishes a connection with the peer.
Connect() error
// Disconnect tears down a connection with the peer.
Disconnect() error
// AppendEntries sends an AppendEntriesRequest to the peer and returns an AppendEntriesResponse and an error
// if the request was unsuccessful.
AppendEntries(request AppendEntriesRequest) (AppendEntriesResponse, error)
// RequestVote sends a RequestVoteRequest to the peer and returns a RequestVoteResponse and an error
// if the request was unsuccessful.
RequestVote(request RequestVoteRequest) (RequestVoteResponse, error)
// InstallSnapshot sends a InstallSnapshotRequest to the peer and returns a InstallSnapshotResponse and an error
// if the request was unsuccessful.
InstallSnapshot(request InstallSnapshotRequest) (InstallSnapshotResponse, error)
}
// peer is an implementation of the Peer interface that is responsible for establishing
// a connection with a remote server using gRPC. This implementation is concurrent
// safe.
type peer struct {
// The gRPC client for making Raft protocol calls to the peer.
client pb.RaftClient
// The ID of this peer.
id string
// The network address of this peer.
address net.Addr
// The gRPC client connection to communicate with the peer.
conn *grpc.ClientConn
// Prevents a race condition between disconnect/connect and the RPCs.
mu sync.RWMutex
}
// newPeer creates a new instance of a peer with
// the provided ID and network address.
func newPeer(id string, address net.Addr) *peer {
return &peer{id: id, address: address}
}
func (p *peer) ID() string {
return p.id
}
func (p *peer) Address() net.Addr {
return p.address
}
func (p *peer) Connect() error {
p.mu.Lock()
defer p.mu.Unlock()
if p.client != nil {
return nil
}
conn, err := grpc.Dial(p.address.String(), []grpc.DialOption{grpc.WithTransportCredentials(insecure.NewCredentials())}...)
if err != nil {
return errors.WrapError(err, "failed to connect to peer: ID = %s, address = %s",
p.id, p.address.String())
}
p.client = pb.NewRaftClient(conn)
p.conn = conn
return nil
}
func (p *peer) Disconnect() error {
p.mu.Lock()
defer p.mu.Unlock()
if p.client == nil {
return nil
}
if err := p.conn.Close(); err != nil {
return errors.WrapError(err, "failed to close connection to to peer: ID = %s, address = %s",
p.id, p.address.String())
}
p.conn = nil
p.client = nil
return nil
}
func (p *peer) AppendEntries(request AppendEntriesRequest) (AppendEntriesResponse, error) {
p.mu.RLock()
defer p.mu.RUnlock()
if p.client == nil {
return AppendEntriesResponse{}, errNoConnectionToPeer
}
pbRequest := makeProtoAppendEntriesRequest(request)
pbResponse, err := p.client.AppendEntries(context.Background(), pbRequest, []grpc.CallOption{}...)
if err != nil {
return AppendEntriesResponse{}, errors.WrapError(err, "AppendEntries RPC failed: ID = %s, address = %s",
p.id, p.address.String())
}
return makeAppendEntriesResponse(pbResponse), nil
}
func (p *peer) RequestVote(request RequestVoteRequest) (RequestVoteResponse, error) {
p.mu.RLock()
defer p.mu.RUnlock()
if p.client == nil {
return RequestVoteResponse{}, errNoConnectionToPeer
}
pbRequest := makeProtoRequestVoteRequest(request)
pbResponse, err := p.client.RequestVote(context.Background(), pbRequest, []grpc.CallOption{}...)
if err != nil {
return RequestVoteResponse{}, errors.WrapError(err, "RequestVote RPC failed: ID = %s, address = %s",
p.id, p.address.String())
}
return makeRequestVoteResponse(pbResponse), nil
}
func (p *peer) InstallSnapshot(request InstallSnapshotRequest) (InstallSnapshotResponse, error) {
p.mu.RLock()
defer p.mu.RUnlock()
if p.client == nil {
return InstallSnapshotResponse{}, errNoConnectionToPeer
}
pbRequest := makeProtoInstallSnapshotRequest(request)
pbResponse, err := p.client.InstallSnapshot(context.Background(), pbRequest, []grpc.CallOption{}...)
if err != nil {
return InstallSnapshotResponse{}, errors.WrapError(err, "InstallSnapshot RPC failed: ID = %s, address = %s",
p.id, p.address.String())
}
return makeInstallSnapshotResponse(pbResponse), nil
}