-
Notifications
You must be signed in to change notification settings - Fork 199
/
connection.go
596 lines (504 loc) · 16.5 KB
/
connection.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
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
// Copyright 2014-2022 Aerospike, Inc.
//
// 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 aerospike
import (
"bytes"
"compress/zlib"
"crypto/tls"
"io"
"net"
"runtime"
"strconv"
"sync"
"time"
"github.com/aerospike/aerospike-client-go/v7/logger"
"github.com/aerospike/aerospike-client-go/v7/types"
"github.com/aerospike/aerospike-client-go/v7/types/histogram"
)
const _BUFF_ADJUST_INTERVAL = 5 * time.Second
var (
// DefaultBufferSize specifies the initial size of the connection buffer when it is created.
// If not big enough (as big as the average record), it will be reallocated to size again
// which will be more expensive.
DefaultBufferSize = 64 * 1024 // 64 KiB
// MaxBufferSize protects against allocating massive memory blocks
// for buffers. Tweak this number if you are returning a lot of
// large records in your requests.
MaxBufferSize = 1024 * 1024 * 120 // 120 MiB
// PoolCutOffBufferSize specifies the largest buffer size that will be pooled. Anything larger will be
// allocated per request and thrown away afterwards to avoid allocating very big buffers.
PoolCutOffBufferSize = 1024 * 1024 // 1MiB
// MinBufferSize specifies the smallest buffer size that would be allocated for connections. Smaller buffer
// requests will allocate at least this amount of memory. This protects against allocating too many small
// buffers that would require reallocation and putting pressure on the GC.
MinBufferSize = 8 * 1024 // 16 KiB
)
// Connection represents a connection with a timeout.
// Connections maintain a buffer to minimize requesting buffers from the pool.
// If a returned record requires a bigger buffer, the connection will borrow a larger
// buffer from the pool and temporarily use it, returning it after the request.
// A histogram keeps track of the sizes of buffers used for the connection, and the median
// value is used to resize the connection buffer on intervals to optimize memory usage and
// minimize GC pressure.
type Connection struct {
node *Node
// timeouts
socketTimeout time.Duration
deadline time.Time
// duration after which connection is considered idle
idleTimeout time.Duration
idleDeadline time.Time
// connection object
conn net.Conn
// histogram to adjust the buff size to optimal value over time
buffHist *histogram.Log2
bufferAdjustDeadline time.Time
// to avoid having a buffer pool and contention
dataBuffer []byte
// This is a reference to the original data buffer.
// After a big buffer is used temporarily, we will use
// this field to reset the dataBuffer field to the original
// smaller buffer.
origDataBuffer []byte
compressed bool
inflater io.ReadCloser
// inflater may consume more bytes than required.
// LimitReader is used to avoid that problem.
limitReader *io.LimitedReader
closer sync.Once
grpcConn bool
grpcReadCallback func() ([]byte, Error)
grpcReader io.ReadWriter
}
// makes sure that the connection is closed eventually, even if it is not consumed
func connectionFinalizer(c *Connection) {
c.Close()
}
// errToAerospikeErr will convert golang's net and io errors into *AerospikeError
// If the errors is nil, nil be returned. If conn is not nil, its node value
// will be set for the error.
func errToAerospikeErr(conn *Connection, err error) (aerr Error) {
if err == nil {
return nil
}
if terr, ok := err.(net.Error); ok {
if terr.Timeout() {
if conn != nil && conn.node != nil {
conn.node.stats.ConnectionsTimeoutErrors.IncrementAndGet()
}
aerr = newErrorAndWrap(err, types.TIMEOUT)
} else {
aerr = newErrorAndWrap(err, types.NETWORK_ERROR)
}
} else {
aerr = newErrorAndWrap(err, types.NETWORK_ERROR)
}
// set node if exists
if conn != nil {
aerr.setNode(conn.node)
}
return aerr
}
// newGrpcFakeConnection creates a connection that fakes a real connection for when grpc connections are required.
// These connections only support reading to allow parsing of the returned payload.
func newGrpcFakeConnection(payload []byte, callback func() ([]byte, Error)) *Connection {
buf := bytes.NewBuffer(payload)
return &Connection{
grpcConn: true,
grpcReader: buf,
grpcReadCallback: callback,
limitReader: &io.LimitedReader{R: buf, N: 0},
}
}
// newConnection creates a connection on the network and returns the pointer
// A minimum timeout of 2 seconds will always be applied.
// If the connection is not established in the specified timeout,
// an error will be returned
func newConnection(address string, timeout time.Duration) (*Connection, Error) {
newConn := &Connection{dataBuffer: buffPool.Get(DefaultBufferSize)}
newConn.buffHist = histogram.NewLog2(32)
newConn.bufferAdjustDeadline = time.Now().Add(_BUFF_ADJUST_INTERVAL)
newConn.origDataBuffer = newConn.dataBuffer
runtime.SetFinalizer(newConn, connectionFinalizer)
// don't wait indefinitely
if timeout == 0 {
timeout = 5 * time.Second
}
conn, err := net.DialTimeout("tcp", address, timeout)
if err != nil {
logger.Logger.Debug("Connection to address `%s` failed to establish with error: %s", address, err.Error())
return nil, errToAerospikeErr(nil, err)
}
newConn.conn = conn
newConn.limitReader = &io.LimitedReader{R: conn, N: 0}
// set timeout at the last possible moment
if err := newConn.SetTimeout(time.Now().Add(timeout), timeout); err != nil {
newConn.Close()
return nil, err
}
return newConn, nil
}
// NewConnection creates a TLS connection on the network and returns the pointer.
// A minimum timeout of 2 seconds will always be applied.
// If the connection is not established in the specified timeout,
// an error will be returned
func NewConnection(policy *ClientPolicy, host *Host) (*Connection, Error) {
address := net.JoinHostPort(host.Name, strconv.Itoa(host.Port))
conn, err := newConnection(address, policy.Timeout)
if err != nil {
return nil, err
}
if policy.TlsConfig == nil {
return conn, nil
}
// Use version dependent clone function to clone the config
tlsConfig := policy.TlsConfig.Clone()
tlsConfig.ServerName = host.TLSName
sconn := tls.Client(conn.conn, tlsConfig)
if err := sconn.Handshake(); err != nil {
nerr := newWrapNetworkError(err)
if cerr := sconn.Close(); cerr != nil {
logger.Logger.Debug("Closing connection after handshake error failed: %s", cerr.Error())
nerr = chainErrors(newWrapNetworkError(cerr), nerr)
}
return nil, nerr
}
if host.TLSName != "" && !tlsConfig.InsecureSkipVerify {
if err := sconn.VerifyHostname(host.TLSName); err != nil {
nerr := newWrapNetworkError(err)
if cerr := sconn.Close(); cerr != nil {
logger.Logger.Debug("Closing connection after VerifyHostName error failed: %s", cerr.Error())
nerr = chainErrors(newWrapNetworkError(cerr), nerr)
}
logger.Logger.Error("Connection to address `%s` failed to establish with error: %s", address, err.Error())
return nil, nerr
}
}
conn.conn = sconn
return conn, nil
}
// Write writes the slice to the connection buffer.
func (ctn *Connection) Write(buf []byte) (total int, aerr Error) {
var err error
// make sure all bytes are written
// Don't worry about the loop, timeout has been set elsewhere
if err = ctn.updateDeadline(); err == nil {
if total, err = ctn.conn.Write(buf); err == nil {
return total, nil
}
// If all bytes are written, ignore any potential error
// The error will bubble up on the next network io if it matters.
if total == len(buf) {
return total, nil
}
}
aerr = chainErrors(errToAerospikeErr(ctn, err), aerr)
if ctn.node != nil {
ctn.node.incrErrorCount()
ctn.node.stats.ConnectionsFailed.IncrementAndGet()
}
// the line should happen before .Close()
ctn.Close()
return total, aerr
}
// Read reads from connection buffer to the provided slice.
func (ctn *Connection) Read(buf []byte, length int) (total int, aerr Error) {
if ctn.grpcConn {
// grpc fake conn
return ctn.grpcRead(buf, length)
}
var err error
// if all bytes are not read, retry until successful
// Don't worry about the loop; we've already set the timeout elsewhere
for total < length {
var r int
if err = ctn.updateDeadline(); err != nil {
break
}
if !ctn.compressed {
r, err = ctn.conn.Read(buf[total:length])
} else {
r, err = ctn.inflater.Read(buf[total:length])
if err == io.EOF && total+r == length {
ctn.compressed = false
err = ctn.inflater.Close()
}
}
total += r
if err != nil {
break
}
}
if total == length {
// If all required bytes are read, ignore any potential error.
// The error will bubble up on the next network io if it matters.
return total, nil
}
aerr = chainErrors(errToAerospikeErr(ctn, err), aerr)
if ctn.node != nil {
ctn.node.incrErrorCount()
ctn.node.stats.ConnectionsFailed.IncrementAndGet()
}
// the line should happen before .Close()
ctn.Close()
return total, aerr
}
// Reads the grpc payload
func (ctn *Connection) grpcReadNext() (aerr Error) {
// if there is no payload set, ask for the next chunk
if ctn.grpcReadCallback != nil {
grpcPayload, err := ctn.grpcReadCallback()
if err != nil {
return err
}
if _, err := ctn.grpcReader.Write(grpcPayload); err != nil {
errToAerospikeErr(ctn, io.EOF)
}
if ctn.compressed {
ctn.limitReader.R = ctn.grpcReader
}
return nil
}
return errToAerospikeErr(ctn, io.EOF)
}
// Reads the grpc payload
func (ctn *Connection) grpcRead(buf []byte, length int) (total int, aerr Error) {
var err error
// if all bytes are not read, retry until successful
// Don't worry about the loop; we've already set the timeout elsewhere
for total < length {
var r int
if !ctn.compressed {
r, err = ctn.grpcReader.Read(buf[total:length])
} else {
r, err = ctn.inflater.Read(buf[total:length])
if err == io.EOF && total+r == length {
ctn.compressed = false
err = ctn.inflater.Close()
}
}
total += r
if err != nil {
if err == io.EOF {
if err := ctn.grpcReadNext(); err != nil {
return total, err
}
continue
}
break
}
}
if total == length {
// If all required bytes are read, ignore any potential error.
// The error will bubble up on the next network io if it matters.
return total, nil
}
aerr = chainErrors(errToAerospikeErr(ctn, err), aerr)
return total, aerr
}
// IsConnected returns true if the connection is not closed yet.
func (ctn *Connection) IsConnected() bool {
return ctn.conn != nil
}
// updateDeadline sets connection timeout for both read and write operations.
// this function is called before each read and write operation. If deadline has passed,
// the function will return a TIMEOUT error.
func (ctn *Connection) updateDeadline() Error {
now := time.Now()
var socketDeadline time.Time
if ctn.deadline.IsZero() {
if ctn.socketTimeout > 0 {
socketDeadline = now.Add(ctn.socketTimeout)
}
} else {
if now.After(ctn.deadline) {
return newError(types.TIMEOUT)
}
if ctn.socketTimeout == 0 {
socketDeadline = ctn.deadline
} else {
tDeadline := now.Add(ctn.socketTimeout)
if tDeadline.After(ctn.deadline) {
socketDeadline = ctn.deadline
} else {
socketDeadline = tDeadline
}
}
// floor to a millisecond to avoid too short timeouts
if socketDeadline.Sub(now) < time.Millisecond {
socketDeadline = now.Add(time.Millisecond)
}
}
if err := ctn.conn.SetDeadline(socketDeadline); err != nil {
if ctn.node != nil {
ctn.node.stats.ConnectionsFailed.IncrementAndGet()
}
return errToAerospikeErr(ctn, err)
}
return nil
}
// SetTimeout sets connection timeout for both read and write operations.
func (ctn *Connection) SetTimeout(deadline time.Time, socketTimeout time.Duration) Error {
ctn.deadline = deadline
ctn.socketTimeout = socketTimeout
return nil
}
// Close closes the connection
func (ctn *Connection) Close() {
ctn.closer.Do(func() {
if ctn != nil && ctn.conn != nil {
// deregister
if ctn.node != nil {
ctn.node.connectionCount.DecrementAndGet()
ctn.node.stats.ConnectionsClosed.IncrementAndGet()
}
if err := ctn.conn.Close(); err != nil {
logger.Logger.Warn(err.Error())
}
ctn.conn = nil
// put the data buffer back in the pool in case it gets used again
buffPool.Put(ctn.dataBuffer)
ctn.dataBuffer = nil
ctn.origDataBuffer = nil
ctn.node = nil
}
})
}
// Login will send authentication information to the server.
func (ctn *Connection) login(policy *ClientPolicy, hashedPassword []byte, sessionInfo *sessionInfo) Error {
// need to authenticate
if policy.RequiresAuthentication() {
var err Error
command := newLoginCommand(ctn.dataBuffer)
if !sessionInfo.isValid() {
err = command.login(policy, ctn, hashedPassword)
} else {
err = command.authenticateViaToken(policy, ctn, sessionInfo.token)
if err != nil && err.Matches(types.INVALID_CREDENTIAL, types.EXPIRED_SESSION) {
// invalidate the token
if ctn.node != nil {
ctn.node.resetSessionInfo()
}
// retry via user/pass
if hashedPassword != nil {
command = newLoginCommand(ctn.dataBuffer)
err = command.login(policy, ctn, hashedPassword)
}
}
}
if err != nil {
if ctn.node != nil {
ctn.node.stats.ConnectionsFailed.IncrementAndGet()
}
// Socket not authenticated. Do not put back into pool.
ctn.Close()
return err
}
si := command.sessionInfo()
if ctn.node != nil && si.isValid() {
ctn.node.sessionInfo.Set(si)
}
}
return nil
}
// Login will send authentication information to the server.
// This function is provided for using the connection in conjunction with external libraries.
// The password will be hashed every time, which is a slow operation.
func (ctn *Connection) Login(policy *ClientPolicy) Error {
if !policy.RequiresAuthentication() {
return nil
}
hashedPassword, err := hashPassword(policy.Password)
if err != nil {
return err
}
return ctn.login(policy, hashedPassword, nil)
}
// RequestInfo gets info values by name from the specified connection.
// Timeout should already be set on the connection.
func (ctn *Connection) RequestInfo(names ...string) (map[string]string, Error) {
info, err := newInfo(ctn, names...)
if err != nil {
return nil, err
}
return info.parseMultiResponse()
}
// setIdleTimeout sets the idle timeout for the connection.
func (ctn *Connection) setIdleTimeout(timeout time.Duration) {
ctn.idleTimeout = timeout
}
// isIdle returns true if the connection has reached the idle deadline.
func (ctn *Connection) isIdle() bool {
return ctn.idleTimeout > 0 && time.Now().After(ctn.idleDeadline)
}
func selectWithinRange[T int | uint | int64 | uint64](min, val, max T) T {
if val < min {
return min
} else if val > max {
return max
}
return val
}
// refresh extends the idle deadline of the connection.
func (ctn *Connection) refresh() {
now := time.Now()
ctn.idleDeadline = now.Add(ctn.idleTimeout)
if ctn.inflater != nil {
ctn.inflater.Close()
}
ctn.compressed = false
ctn.inflater = nil
ctn.dataBuffer = ctn.origDataBuffer
// adjust buffer size
if now.After(ctn.bufferAdjustDeadline) {
ctn.bufferAdjustDeadline = now.Add(_BUFF_ADJUST_INTERVAL)
newBuffSize := selectWithinRange(MinBufferSize, int(ctn.buffHist.Median()), PoolCutOffBufferSize)
ctn.buffHist.Reset()
// Do not go lower than 1K and larger than max allowed buffer size
if newBuffSize != len(ctn.dataBuffer) {
ctn.origDataBuffer = nil
// put the current buffer back in the pool
buffPool.Put(ctn.dataBuffer)
// Get a new one from the pool
ctn.dataBuffer = buffPool.Get(int(newBuffSize))
ctn.origDataBuffer = ctn.dataBuffer
}
}
}
// initInflater sets up the zlib inflater to read compressed data from the connection
func (ctn *Connection) initInflater(enabled bool, length int) Error {
ctn.compressed = enabled
ctn.inflater = nil
if ctn.compressed {
ctn.limitReader.N = int64(length)
r, err := zlib.NewReader(ctn.limitReader)
if err != nil {
return newCommonError(err)
}
ctn.inflater = r
}
return nil
}
// KeepConnection decides if a connection should be kept
// based on the error type.
func KeepConnection(err Error) bool {
// Do not keep connection on client errors.
if err.resultCode() < 0 {
return false
}
return !err.Matches(types.QUERY_TERMINATED,
types.SCAN_ABORT,
types.QUERY_ABORTED,
types.TIMEOUT)
}