forked from charithe/gcgrpcpool
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgcgrpcpool.go
209 lines (177 loc) · 5.41 KB
/
gcgrpcpool.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
/*
* Copyright 2016 Charith Ellawala
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package gcgrpcpool
import (
"fmt"
"sync"
log "github.com/sirupsen/logrus"
"github.com/charithe/gcgrpcpool/gcgrpc"
"github.com/golang/groupcache"
"github.com/golang/groupcache/consistenthash"
pb "github.com/golang/groupcache/groupcachepb"
"golang.org/x/net/context"
"google.golang.org/grpc"
)
const defaultReplicas = 50
type GRPCPool struct {
self string
opts GRPCPoolOptions
mu sync.Mutex
peers *consistenthash.Map
grpcGetters map[string]*grpcGetter
}
type GRPCPoolOptions struct {
Replicas int
HashFn consistenthash.Hash
PeerDialOptions []grpc.DialOption
}
func NewGRPCPool(self string, server *grpc.Server) *GRPCPool {
return NewGRPCPoolOptions(self, server, nil)
}
var grpcPoolCreated bool
func NewGRPCPoolOptions(self string, server *grpc.Server, opts *GRPCPoolOptions) *GRPCPool {
if grpcPoolCreated {
panic("NewGRPCPool must be called only once")
}
grpcPoolCreated = true
pool := &GRPCPool{
self: self,
grpcGetters: make(map[string]*grpcGetter),
}
if opts != nil {
pool.opts = *opts
}
if pool.opts.Replicas == 0 {
pool.opts.Replicas = defaultReplicas
}
if pool.opts.PeerDialOptions == nil {
pool.opts.PeerDialOptions = []grpc.DialOption{grpc.WithInsecure()}
}
pool.peers = consistenthash.New(pool.opts.Replicas, pool.opts.HashFn)
groupcache.RegisterPeerPicker(func() groupcache.PeerPicker { return pool })
gcgrpc.RegisterPeerServer(server, pool)
return pool
}
func (gp *GRPCPool) Set(peers ...string) {
gp.mu.Lock()
defer gp.mu.Unlock()
gp.peers = consistenthash.New(gp.opts.Replicas, gp.opts.HashFn)
tempGetters := make(map[string]*grpcGetter, len(peers))
for _, peer := range peers {
if getter, exists := gp.grpcGetters[peer]; exists == true {
tempGetters[peer] = getter
gp.peers.Add(peer)
delete(gp.grpcGetters, peer)
} else {
getter, err := newGRPCGetter(peer, gp.opts.PeerDialOptions...)
if err != nil {
log.WithError(err).Warnf("Failed to open connection to [%s]", peer)
} else {
tempGetters[peer] = getter
gp.peers.Add(peer)
}
}
}
for p, g := range gp.grpcGetters {
g.close()
delete(gp.grpcGetters, p)
}
gp.grpcGetters = tempGetters
}
func (gp *GRPCPool) PickPeer(key string) (groupcache.ProtoGetter, bool) {
gp.mu.Lock()
defer gp.mu.Unlock()
if gp.peers.IsEmpty() {
return nil, false
}
if peer := gp.peers.Get(key); peer != gp.self {
return gp.grpcGetters[peer], true
}
return nil, false
}
func (gp *GRPCPool) Retrieve(ctx context.Context, req *gcgrpc.RetrieveRequest) (*gcgrpc.RetrieveResponse, error) {
group := groupcache.GetGroup(req.Group)
if group == nil {
log.Warnf("Unable to find group [%s]", req.Group)
return nil, fmt.Errorf("Unable to find group [%s]", req.Group)
}
group.Stats.ServerRequests.Add(1)
var value []byte
err := group.Get(ctx, req.Key, groupcache.AllocatingByteSliceSink(&value))
if err != nil {
log.WithError(err).Warnf("Failed to retrieve [%s]", req)
return nil, fmt.Errorf("Failed to retrieve [%s]: %v", req, err)
}
return &gcgrpc.RetrieveResponse{Value: value}, nil
}
func (gp *GRPCPool) AddPeers(ctx context.Context, peers *gcgrpc.Peers) (*gcgrpc.Ack, error) {
gp.mu.Lock()
defer gp.mu.Unlock()
for _, peer := range peers.PeerAddr {
if _, exists := gp.grpcGetters[peer]; exists != true {
getter, err := newGRPCGetter(peer, gp.opts.PeerDialOptions...)
if err != nil {
log.WithError(err).Warnf("Failed to open connection to [%s]", peer)
} else {
log.Infof("Adding peer [%s]", peer)
gp.grpcGetters[peer] = getter
gp.peers.Add(peer)
}
}
}
return &gcgrpc.Ack{}, nil
}
func (gp *GRPCPool) RemovePeers(ctx context.Context, peers *gcgrpc.Peers) (*gcgrpc.Ack, error) {
gp.mu.Lock()
defer gp.mu.Unlock()
for _, peer := range peers.PeerAddr {
if p, exists := gp.grpcGetters[peer]; exists == true {
log.Infof("Removing peer [%s]", peer)
p.close()
delete(gp.grpcGetters, peer)
}
}
return &gcgrpc.Ack{}, nil
}
func (gp *GRPCPool) SetPeers(ctx context.Context, peers *gcgrpc.Peers) (*gcgrpc.Ack, error) {
gp.Set(peers.PeerAddr...)
return &gcgrpc.Ack{}, nil
}
type grpcGetter struct {
address string
conn *grpc.ClientConn
}
func newGRPCGetter(address string, dialOpts ...grpc.DialOption) (*grpcGetter, error) {
conn, err := grpc.Dial(address, dialOpts...)
if err != nil {
return nil, fmt.Errorf("Failed to connect to [%s]: %v", address, err)
}
return &grpcGetter{address: address, conn: conn}, nil
}
func (g *grpcGetter) Get(ctx groupcache.Context, in *pb.GetRequest, out *pb.GetResponse) error {
client := gcgrpc.NewPeerClient(g.conn)
resp, err := client.Retrieve(context.Background(), &gcgrpc.RetrieveRequest{Group: *in.Group, Key: *in.Key})
if err != nil {
return fmt.Errorf("Failed to GET [%s]: %v", in, err)
}
out.Value = resp.Value
return nil
}
func (g *grpcGetter) close() {
if g.conn != nil {
g.conn.Close()
}
}