forked from asonawalla/gazette
/
client.go
236 lines (193 loc) · 5.79 KB
/
client.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
package consumer
import (
"context"
"errors"
"sync"
"time"
log "github.com/sirupsen/logrus"
"google.golang.org/grpc"
"github.com/LiveRamp/gazette/pkg/journal"
"github.com/LiveRamp/gazette/pkg/keepalive"
)
// Client interacts with a Gazette Consumer to maintain an updated pool
// of GRPC client connections to live Consumer endpoints, along with a local
// snapshot of Consumer topology for selecting appropriate endpoints.
type Client struct {
service *grpc.ClientConn
// Drives periodic polls of the consumer's state.
refreshTicker *time.Ticker
// Signals the Client.serve goroutine.
signalCh chan clientSignal
// Once attached to a *Client instance, |state|, |conns|, and |index| are read
// only, allowing for unlocked concurrent use of the maps. Modifications are
// made by constructing new maps and swapping out the instances
// (guarded by |mu|).
state ConsumerState
conns poolConns
index partitionShards
mu sync.Mutex
}
// A collection of ClientConn instances indexed on Dialed endpoint address.
type poolConns map[string]*grpc.ClientConn
// A collection of ConsumerState_Shard instances indexed on shard partition.
type partitionShards map[journal.Name]ConsumerState_Shard
// Used to signal Client.serve.
type clientSignal bool
const (
shardClientCacheErrBackoff = time.Second * 5
shardClientCachePollInterval = time.Second * 30
// Signals to Client.serve that to begin a refresh.
signalRefresh clientSignal = false
// Signals to Client.serve to halt.
signalStop clientSignal = true
)
var (
ErrNoSuchConsumerPartition = errors.New("no such consumer partition")
ErrNoReadyPartitionClient = errors.New("no ready consumer partition replica client")
)
func NewClient(endpoint string) (*Client, error) {
var conn, err = grpc.Dial(endpoint,
grpc.WithBlock(),
grpc.WithDialer(keepalive.DialerFunc),
grpc.WithInsecure())
if err != nil {
return nil, err
}
var cc = &Client{
service: conn,
refreshTicker: time.NewTicker(shardClientCachePollInterval),
signalCh: make(chan clientSignal),
conns: make(poolConns),
index: make(partitionShards),
}
if err = cc.update(); err != nil {
return nil, err
}
go cc.serve()
return cc, nil
}
// PartitionClient maps |partition| to its live endpoint and ConsumerState_Shard.
func (c *Client) PartitionClient(partition journal.Name) (*grpc.ClientConn, ConsumerState_Shard, error) {
c.mu.Lock()
var index, conns = c.index, c.conns
c.mu.Unlock()
if shard, ok := index[partition]; !ok {
return nil, shard, ErrNoSuchConsumerPartition
} else if len(shard.Replicas) == 0 || shard.Replicas[0].Status != ConsumerState_Replica_PRIMARY {
return nil, shard, ErrNoReadyPartitionClient
} else if conn, ok := conns[shard.Replicas[0].Endpoint]; !ok {
return nil, shard, ErrNoReadyPartitionClient
} else {
return conn, shard, nil
}
}
func (c *Client) State() ConsumerState {
c.mu.Lock()
defer c.mu.Unlock()
return c.state
}
// Invalidate triggers an immediate refresh of Client state.
func (c *Client) Invalidate() {
select {
case c.signalCh <- signalRefresh:
// Pass.
default:
// An update is already in progress.
}
}
// Close shuts down a Client.
func (c *Client) Close() {
c.signalCh <- signalStop
// Block until closed.
for range c.signalCh {
}
}
func (c *Client) serve() {
for {
var signal clientSignal
select {
case <-c.refreshTicker.C:
case signal = <-c.signalCh:
}
if signal == signalStop {
break
}
if err := c.update(); err != nil {
log.WithField("err", err).Warn("failed to refresh Shard routes")
time.Sleep(shardClientCacheErrBackoff)
}
}
for ep, conn := range c.conns {
if err := conn.Close(); err != nil {
log.WithFields(log.Fields{"endpoint": ep, "err": err}).
Warn("failed to close GRPC ClientConn")
}
}
c.refreshTicker.Stop()
close(c.signalCh)
return
}
func (c *Client) update() error {
var state, err = NewConsumerClient(c.service).
CurrentConsumerState(context.Background(), &Empty{})
if err != nil {
return err
}
c.mu.Lock()
var oldConns = c.conns
c.mu.Unlock()
var conns = make(map[string]*grpc.ClientConn)
var index = make(partitionShards)
var prepare = func(ep string) {
if _, ok := conns[ep]; ok {
// Pass.
} else if conn, ok := oldConns[ep]; ok {
conns[ep] = conn
} else if conn, err = grpc.Dial(ep,
grpc.WithDialer(keepalive.DialerFunc),
grpc.WithInsecure(),
); err != nil {
// Without grpc.WithBlock, Dial should return immediately, with
// connections attempted in the background. We fatal rather than return
// the error, because 1) this really should not happen, and 2) if it does,
// returning would leak grpc.ClientConn instances (and TCP sockets).
log.WithFields(log.Fields{"endpoint": ep, "err": err}).
Fatal("non-blocking grpc.Dial returned error")
} else {
log.WithField("endpoint", ep).Debug("built new GRPC ClientConn")
conns[ep] = conn
}
}
// Ensure GRPC connections exist to each client.
for _, ep := range state.Endpoints {
prepare(ep)
}
// TODO(johnny): At present, shards can have primary replicas which are not
// captured by |state.Endpoints|. Build connections to these, too.
// Issue #50.
for _, shard := range state.Shards {
for _, replica := range shard.Replicas {
if replica.Status == ConsumerState_Replica_PRIMARY {
prepare(replica.Endpoint)
}
}
}
for _, s := range state.Shards {
index[s.Partition] = s
}
c.mu.Lock()
c.state, c.conns, c.index = *state, conns, index
c.mu.Unlock()
// Tear down endpoint connections which are no longer needed.
for ep, conn := range oldConns {
if _, ok := conns[ep]; ok {
continue // This endpoint is still in use.
}
if err := conn.Close(); err != nil {
log.WithFields(log.Fields{"endpoint": ep, "err": err}).
Warn("failed to close GRPC ClientConn")
}
log.WithField("endpoint", ep).Info("closed GRPC ClientConn")
}
return nil
}