2019-05-22 21:23:23 +00:00
|
|
|
package netutil
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"sync"
|
2021-09-07 17:54:17 +00:00
|
|
|
"time"
|
2019-05-22 21:23:23 +00:00
|
|
|
|
2022-05-02 12:01:50 +00:00
|
|
|
"github.com/VictoriaMetrics/VictoriaMetrics/lib/fasttime"
|
2019-05-22 21:23:23 +00:00
|
|
|
"github.com/VictoriaMetrics/VictoriaMetrics/lib/handshake"
|
2022-05-02 12:01:50 +00:00
|
|
|
"github.com/VictoriaMetrics/metrics"
|
2019-05-22 21:23:23 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// ConnPool is a connection pool with ZSTD-compressed connections.
|
|
|
|
type ConnPool struct {
|
|
|
|
mu sync.Mutex
|
|
|
|
d *TCPDialer
|
|
|
|
|
2022-05-11 11:11:03 +00:00
|
|
|
// concurrentDialsCh limits the number of concurrent dials the ConnPool can make.
|
|
|
|
// This should prevent from creating an excees number of connections during temporary
|
|
|
|
// spikes in workload at vmselect and vmstorage nodes.
|
|
|
|
// See https://github.com/VictoriaMetrics/VictoriaMetrics/issues/2552
|
|
|
|
concurrentDialsCh chan struct{}
|
|
|
|
|
2019-05-22 21:23:23 +00:00
|
|
|
name string
|
|
|
|
handshakeFunc handshake.Func
|
|
|
|
compressionLevel int
|
|
|
|
|
2022-05-02 12:01:50 +00:00
|
|
|
conns []connWithTimestamp
|
2022-06-20 14:25:08 +00:00
|
|
|
|
|
|
|
// lastDialError contains the last error seen when dialing remote addr.
|
|
|
|
// When it is non-nil and conns is empty, then ConnPool.Get() return this error.
|
|
|
|
// This reduces the time needed for dialing unavailable remote storage systems.
|
|
|
|
// See https://github.com/VictoriaMetrics/VictoriaMetrics/issues/711#issuecomment-1160363187
|
|
|
|
lastDialError error
|
2022-05-02 12:01:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type connWithTimestamp struct {
|
|
|
|
bc *handshake.BufferedConn
|
|
|
|
lastActiveTime uint64
|
2019-05-22 21:23:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// NewConnPool creates a new connection pool for the given addr.
|
|
|
|
//
|
|
|
|
// Name is used in exported metrics.
|
|
|
|
// handshakeFunc is used for handshaking after the connection establishing.
|
|
|
|
// The compression is disabled if compressionLevel <= 0.
|
2022-06-20 12:14:47 +00:00
|
|
|
func NewConnPool(name, addr string, handshakeFunc handshake.Func, compressionLevel int, dialTimeout time.Duration) *ConnPool {
|
2022-05-02 12:01:50 +00:00
|
|
|
cp := &ConnPool{
|
2022-06-20 12:14:47 +00:00
|
|
|
d: NewTCPDialer(name, addr, dialTimeout),
|
2022-05-11 11:11:03 +00:00
|
|
|
concurrentDialsCh: make(chan struct{}, 8),
|
2019-05-22 21:23:23 +00:00
|
|
|
|
|
|
|
name: name,
|
|
|
|
handshakeFunc: handshakeFunc,
|
|
|
|
compressionLevel: compressionLevel,
|
|
|
|
}
|
2022-06-20 14:25:08 +00:00
|
|
|
cp.checkAvailability(true)
|
2022-05-02 12:01:50 +00:00
|
|
|
_ = metrics.NewGauge(fmt.Sprintf(`vm_tcpdialer_conns_idle{name=%q, addr=%q}`, name, addr), func() float64 {
|
|
|
|
cp.mu.Lock()
|
|
|
|
n := len(cp.conns)
|
|
|
|
cp.mu.Unlock()
|
|
|
|
return float64(n)
|
|
|
|
})
|
2022-06-20 14:25:08 +00:00
|
|
|
_ = metrics.NewGauge(fmt.Sprintf(`vm_tcpdialer_addr_available{name=%q, addr=%q}`, name, addr), func() float64 {
|
|
|
|
cp.mu.Lock()
|
|
|
|
isAvailable := len(cp.conns) > 0 || cp.lastDialError == nil
|
|
|
|
cp.mu.Unlock()
|
|
|
|
if isAvailable {
|
|
|
|
return 1
|
|
|
|
}
|
|
|
|
return 0
|
|
|
|
})
|
2022-05-02 12:01:50 +00:00
|
|
|
connPoolsMu.Lock()
|
|
|
|
connPools = append(connPools, cp)
|
|
|
|
connPoolsMu.Unlock()
|
|
|
|
return cp
|
2019-05-22 21:23:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Addr returns the address where connections are established.
|
|
|
|
func (cp *ConnPool) Addr() string {
|
|
|
|
return cp.d.addr
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get returns free connection from the pool.
|
|
|
|
func (cp *ConnPool) Get() (*handshake.BufferedConn, error) {
|
2022-06-20 14:25:08 +00:00
|
|
|
bc, err := cp.tryGetConn()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if bc != nil {
|
|
|
|
// Fast path - obtained the connection from pool.
|
2019-05-22 21:23:23 +00:00
|
|
|
return bc, nil
|
|
|
|
}
|
2022-06-20 14:25:08 +00:00
|
|
|
return cp.getConnSlow()
|
|
|
|
}
|
2019-05-22 21:23:23 +00:00
|
|
|
|
2022-06-20 14:25:08 +00:00
|
|
|
func (cp *ConnPool) getConnSlow() (*handshake.BufferedConn, error) {
|
2022-05-11 11:11:03 +00:00
|
|
|
// Limit the number of concurrent dials.
|
|
|
|
// This should help https://github.com/VictoriaMetrics/VictoriaMetrics/issues/2552
|
|
|
|
cp.concurrentDialsCh <- struct{}{}
|
|
|
|
defer func() {
|
|
|
|
<-cp.concurrentDialsCh
|
|
|
|
}()
|
|
|
|
// Make an attempt to get already established connections from the pool.
|
|
|
|
// It may appear there while waiting for cp.concurrentDialsCh.
|
2022-06-20 14:25:08 +00:00
|
|
|
bc, err := cp.tryGetConn()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if bc != nil {
|
2022-05-11 11:11:03 +00:00
|
|
|
return bc, nil
|
|
|
|
}
|
2019-05-22 21:23:23 +00:00
|
|
|
// Pool is empty. Create new connection.
|
2022-06-20 14:25:08 +00:00
|
|
|
return cp.dialAndHandshake()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (cp *ConnPool) dialAndHandshake() (*handshake.BufferedConn, error) {
|
2019-05-22 21:23:23 +00:00
|
|
|
c, err := cp.d.Dial()
|
|
|
|
if err != nil {
|
2022-06-20 14:25:08 +00:00
|
|
|
err = fmt.Errorf("cannot dial %s: %w", cp.d.Addr(), err)
|
|
|
|
}
|
|
|
|
cp.mu.Lock()
|
|
|
|
cp.lastDialError = err
|
|
|
|
cp.mu.Unlock()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2019-05-22 21:23:23 +00:00
|
|
|
}
|
2022-05-11 11:11:03 +00:00
|
|
|
bc, err := cp.handshakeFunc(c, cp.compressionLevel)
|
|
|
|
if err != nil {
|
2022-06-20 14:25:08 +00:00
|
|
|
// Do not put handshake error to cp.lastDialError, because handshake
|
|
|
|
// is perfomed on an already established connection.
|
2020-06-30 19:58:18 +00:00
|
|
|
err = fmt.Errorf("cannot perform %q handshake with server %q: %w", cp.name, cp.d.Addr(), err)
|
2019-05-22 21:23:23 +00:00
|
|
|
_ = c.Close()
|
|
|
|
return nil, err
|
|
|
|
}
|
2022-06-20 14:25:08 +00:00
|
|
|
return bc, err
|
2019-05-22 21:23:23 +00:00
|
|
|
}
|
|
|
|
|
2022-06-20 14:25:08 +00:00
|
|
|
func (cp *ConnPool) tryGetConn() (*handshake.BufferedConn, error) {
|
2022-05-11 11:11:03 +00:00
|
|
|
cp.mu.Lock()
|
|
|
|
defer cp.mu.Unlock()
|
|
|
|
if len(cp.conns) == 0 {
|
2022-06-20 14:25:08 +00:00
|
|
|
return nil, cp.lastDialError
|
2022-05-11 11:11:03 +00:00
|
|
|
}
|
|
|
|
c := cp.conns[len(cp.conns)-1]
|
|
|
|
bc := c.bc
|
|
|
|
c.bc = nil
|
|
|
|
cp.conns = cp.conns[:len(cp.conns)-1]
|
2022-06-20 14:25:08 +00:00
|
|
|
return bc, nil
|
2022-05-11 11:11:03 +00:00
|
|
|
}
|
|
|
|
|
2019-05-22 21:23:23 +00:00
|
|
|
// Put puts bc back to the pool.
|
|
|
|
//
|
|
|
|
// Do not put broken and closed connections to the pool!
|
|
|
|
func (cp *ConnPool) Put(bc *handshake.BufferedConn) {
|
2021-09-07 17:54:17 +00:00
|
|
|
if err := bc.SetDeadline(time.Time{}); err != nil {
|
|
|
|
// Close the connection instead of returning it to the pool,
|
|
|
|
// since it may be broken.
|
|
|
|
_ = bc.Close()
|
|
|
|
return
|
|
|
|
}
|
2019-05-22 21:23:23 +00:00
|
|
|
cp.mu.Lock()
|
2022-05-02 12:01:50 +00:00
|
|
|
cp.conns = append(cp.conns, connWithTimestamp{
|
|
|
|
bc: bc,
|
|
|
|
lastActiveTime: fasttime.UnixTimestamp(),
|
|
|
|
})
|
|
|
|
cp.mu.Unlock()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (cp *ConnPool) closeIdleConns() {
|
|
|
|
// Close connections, which were idle for more than 30 seconds.
|
2022-05-11 11:11:03 +00:00
|
|
|
// This should reduce the number of connections after sudden spikes in query rate.
|
|
|
|
// See https://github.com/VictoriaMetrics/VictoriaMetrics/issues/2508
|
2022-05-02 12:01:50 +00:00
|
|
|
deadline := fasttime.UnixTimestamp() - 30
|
|
|
|
var activeConns []connWithTimestamp
|
|
|
|
cp.mu.Lock()
|
|
|
|
conns := cp.conns
|
|
|
|
for _, c := range conns {
|
|
|
|
if c.lastActiveTime > deadline {
|
|
|
|
activeConns = append(activeConns, c)
|
|
|
|
} else {
|
|
|
|
_ = c.bc.Close()
|
|
|
|
c.bc = nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
cp.conns = activeConns
|
2019-05-22 21:23:23 +00:00
|
|
|
cp.mu.Unlock()
|
|
|
|
}
|
2022-05-02 12:01:50 +00:00
|
|
|
|
2022-06-20 14:25:08 +00:00
|
|
|
func (cp *ConnPool) checkAvailability(force bool) {
|
|
|
|
cp.mu.Lock()
|
|
|
|
hasDialError := cp.lastDialError != nil
|
|
|
|
cp.mu.Unlock()
|
|
|
|
if hasDialError || force {
|
|
|
|
bc, _ := cp.dialAndHandshake()
|
|
|
|
if bc != nil {
|
|
|
|
cp.Put(bc)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-05-02 12:01:50 +00:00
|
|
|
func init() {
|
|
|
|
go func() {
|
|
|
|
for {
|
|
|
|
time.Sleep(17 * time.Second)
|
2022-06-20 14:25:08 +00:00
|
|
|
forEachConnPool(func(cp *ConnPool) {
|
2022-05-02 12:01:50 +00:00
|
|
|
cp.closeIdleConns()
|
2022-06-20 14:25:08 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
go func() {
|
|
|
|
for {
|
|
|
|
time.Sleep(time.Second)
|
|
|
|
forEachConnPool(func(cp *ConnPool) {
|
|
|
|
cp.checkAvailability(false)
|
|
|
|
})
|
2022-05-02 12:01:50 +00:00
|
|
|
}
|
|
|
|
}()
|
|
|
|
}
|
|
|
|
|
|
|
|
var connPoolsMu sync.Mutex
|
|
|
|
var connPools []*ConnPool
|
2022-06-20 14:25:08 +00:00
|
|
|
|
|
|
|
func forEachConnPool(f func(cp *ConnPool)) {
|
|
|
|
connPoolsMu.Lock()
|
2022-06-21 10:30:58 +00:00
|
|
|
var wg sync.WaitGroup
|
2022-06-20 14:25:08 +00:00
|
|
|
for _, cp := range connPools {
|
2022-06-21 10:30:58 +00:00
|
|
|
wg.Add(1)
|
|
|
|
go func(cp *ConnPool) {
|
|
|
|
defer wg.Done()
|
|
|
|
f(cp)
|
|
|
|
}(cp)
|
2022-06-20 14:25:08 +00:00
|
|
|
}
|
2022-06-21 10:30:58 +00:00
|
|
|
wg.Wait()
|
2022-06-20 14:25:08 +00:00
|
|
|
connPoolsMu.Unlock()
|
|
|
|
}
|