-
Notifications
You must be signed in to change notification settings - Fork 490
/
client.go
executable file
·362 lines (317 loc) · 9.23 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
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
//
// Licensed to the Apache Software Foundation (ASF) under one or more
// contributor license agreements. See the NOTICE file distributed with
// this work for additional information regarding copyright ownership.
// The ASF licenses this file to You 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 dataproxy
import (
"bytes"
"context"
"errors"
"math"
"sync"
"github.com/apache/inlong/inlong-sdk/dataproxy-sdk-twins/dataproxy-sdk-golang/connpool"
"github.com/apache/inlong/inlong-sdk/dataproxy-sdk-twins/dataproxy-sdk-golang/discoverer"
"github.com/apache/inlong/inlong-sdk/dataproxy-sdk-twins/dataproxy-sdk-golang/framer"
"github.com/apache/inlong/inlong-sdk/dataproxy-sdk-twins/dataproxy-sdk-golang/logger"
"github.com/panjf2000/gnet/v2"
"go.uber.org/atomic"
)
// variables
var (
ErrInvalidGroupID = errors.New("invalid group ID")
ErrInvalidURL = errors.New("invalid URL")
ErrNoEndpoint = errors.New("service has no endpoints")
ErrNoAvailableWorker = errors.New("no available worker")
)
// Client is the interface of a DataProxy client
type Client interface {
// Send sends a message and wait for the result.
Send(ctx context.Context, msg Message) error
// SendAsync sends a message asynchronously, when the message is sent or timeout, the callback will be called.
SendAsync(ctx context.Context, msg Message, callback Callback)
// Close flushes all the message to the server and wait for the results or timeout, then close the producer.
Close()
}
type client struct {
*gnet.BuiltinEventEngine // inherited from the default event engin
options *Options // config options
discoverer discoverer.Discoverer // service discoverer
connPool connpool.EndpointRestrictedConnPool // connection pool
netClient *gnet.Client // client side network manager
workers []*worker // worker to do send and receive jobs
curWorkerIndex atomic.Uint64 // current worker index
log logger.Logger // debug logger
metrics *metrics // metrics
framer framer.Framer // response framer
closeOnce sync.Once //
}
// NewClient Creates a dataproxy-go client instance
func NewClient(opts ...Option) (Client, error) {
// default v1 options
options := &Options{}
for _, o := range opts {
o(options)
}
err := options.ValidateAndSetDefault()
if err != nil {
return nil, err
}
// the client struct
cli := &client{
options: options,
log: options.Logger,
}
err = cli.initAll()
if err != nil {
cli.Close()
return nil, err
}
err = cli.netClient.Start()
if err != nil {
cli.Close()
return nil, err
}
return cli, nil
}
func (c *client) initAll() error {
// the following initialization order must not be changed。
err := c.initDiscoverer()
if err != nil {
return err
}
err = c.initNetClient()
if err != nil {
return err
}
err = c.initConns()
if err != nil {
return err
}
err = c.initFramer()
if err != nil {
return err
}
err = c.initMetrics()
if err != nil {
return err
}
err = c.initWorkers()
if err != nil {
return err
}
return nil
}
func (c *client) initDiscoverer() error {
dis, err := NewDiscoverer(c.options.URL, c.options.GroupID, c.options.UpdateInterval, c.options.Logger)
if err != nil {
return err
}
c.discoverer = dis
dis.AddEventHandler(c)
return nil
}
func (c *client) initNetClient() error {
netClient, err := gnet.NewClient(
c,
gnet.WithLogger(c.options.Logger),
gnet.WithWriteBufferCap(c.options.WriteBufferSize),
gnet.WithReadBufferCap(c.options.ReadBufferSize),
gnet.WithSocketSendBuffer(c.options.SocketSendBufferSize),
gnet.WithSocketRecvBuffer(c.options.SocketRecvBufferSize))
if err != nil {
return err
}
// save net client
c.netClient = netClient
return nil
}
func (c *client) initConns() error {
epList := c.discoverer.GetEndpoints()
epLen := len(epList)
if epLen == 0 {
return ErrNoEndpoint
}
endpoints := make([]string, epLen)
for i := 0; i < epLen; i++ {
endpoints[i] = epList[i].Addr
}
// maximum connection number per endpoint is 3
connsPerEndpoint := c.options.WorkerNum/epLen + 1
connsPerEndpoint = int(math.Min(3, float64(connsPerEndpoint)))
pool, err := connpool.NewConnPool(endpoints, connsPerEndpoint, 512, c, c.log)
if err != nil {
return err
}
c.connPool = pool
return nil
}
func (c *client) initFramer() error {
framer, err := framer.NewLengthField(framer.LengthFieldCfg{
MaxFrameLen: 64 * 1024,
FieldOffset: 0,
FieldLength: 4,
Adjustment: 0,
BytesToStrip: 0,
})
if err != nil {
return err
}
c.framer = framer
return nil
}
func (c *client) initMetrics() error {
m, err := newMetrics(c.options.MetricsName, c.options.MetricsRegistry)
if err != nil {
return err
}
c.metrics = m
return nil
}
func (c *client) initWorkers() error {
c.workers = make([]*worker, 0, c.options.WorkerNum)
for i := 0; i < c.options.WorkerNum; i++ {
w, err := c.createWorker(i)
if err != nil {
return err
}
c.workers = append(c.workers, w)
}
return nil
}
func (c *client) Dial(addr string) (gnet.Conn, error) {
return c.netClient.Dial("tcp", addr)
}
func (c *client) Send(ctx context.Context, msg Message) error {
worker, err := c.getWorker()
if err != nil {
return ErrNoAvailableWorker
}
return worker.send(ctx, msg)
}
func (c *client) SendAsync(ctx context.Context, msg Message, cb Callback) {
worker, err := c.getWorker()
if err != nil {
if cb != nil {
cb(msg, ErrNoAvailableWorker)
}
return
}
worker.sendAsync(ctx, msg, cb)
}
func (c *client) getWorker() (*worker, error) {
index := c.curWorkerIndex.Load()
w := c.workers[index%uint64(len(c.workers))]
c.curWorkerIndex.Add(1)
if w.available() {
return w, nil
}
c.metrics.incError(workerBusy.strCode)
return nil, workerBusy
}
func (c *client) Close() {
c.closeOnce.Do(func() {
if c.discoverer != nil {
c.discoverer.Close()
}
for _, w := range c.workers {
w.close()
}
if c.netClient != nil {
_ = c.netClient.Stop()
}
})
}
func (c *client) createWorker(index int) (*worker, error) {
return newWorker(c, index, c.options)
}
func (c *client) getConn() (gnet.Conn, error) {
return c.connPool.Get()
}
func (c *client) putConn(conn gnet.Conn, err error) {
c.connPool.Put(conn, err)
}
func (c *client) OnBoot(e gnet.Engine) gnet.Action {
c.log.Info("client boot")
return gnet.None
}
func (c *client) OnShutdown(e gnet.Engine) {
c.log.Info("client shutdown")
}
func (c *client) OnOpen(conn gnet.Conn) ([]byte, gnet.Action) {
c.log.Debug("connection opened: ", conn.RemoteAddr())
return nil, gnet.None
}
func (c *client) OnClose(conn gnet.Conn, err error) gnet.Action {
c.log.Warn("connection closed: ", conn.RemoteAddr(), ", err: ", err)
if err != nil {
c.metrics.incError(errConnClosedByPeer.strCode)
}
return gnet.None
}
func (c *client) OnTraffic(conn gnet.Conn) (action gnet.Action) {
// c.log.Debug("response received")
for {
total := conn.InboundBuffered()
if total < heartbeatRspLen {
break
}
buf, _ := conn.Peek(total)
// if it is a heartbeat response, skip it and read the next package
if bytes.Equal(buf[:heartbeatRspLen], heartbeatRsp) {
_, err := conn.Discard(heartbeatRspLen)
if err != nil {
c.metrics.incError(errConnReadFailed.getStrCode())
c.log.Error("discard connection stream failed, err", err)
// read failed, close the connection
return gnet.Close
}
c.log.Debug("heartbeat rsp receive")
continue
}
length, payloadOffset, payloadOffsetEnd, err := c.framer.ReadFrame(buf)
if err == framer.ErrIncompleteFrame {
break
}
if err != nil {
c.metrics.incError(errConnReadFailed.getStrCode())
c.log.Error("invalid packet from stream connection, close it, err:", err)
// read failed, close the connection
return gnet.Close
}
frame, _ := conn.Peek(length)
_, err = conn.Discard(length)
if err != nil {
c.metrics.incError(errConnReadFailed.getStrCode())
c.log.Error("discard connection stream failed, err", err)
// read failed, close the connection
return gnet.Close
}
// handle response
c.onResponse(frame[payloadOffset:payloadOffsetEnd])
}
return gnet.None
}
func (c *client) onResponse(frame []byte) {
rsp := batchRsp{}
rsp.decode(frame)
index := getWorkerIndex(rsp.workerID)
if index < 0 || index >= len(c.workers) {
c.log.Errorf("invalid worker index from response, index=%d", index)
return
}
c.workers[index].onRsp(&rsp)
}
func (c *client) OnEndpointUpdate(all, add, del discoverer.EndpointList) {
c.connPool.UpdateEndpoints(all.Addresses(), add.Addresses(), del.Addresses())
}