2020-01-07 13:38:45 +00:00
|
|
|
/*
|
|
|
|
Copyright 2020 The Ceph-CSI Authors.
|
|
|
|
|
|
|
|
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 util
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"io/ioutil"
|
|
|
|
"sync"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/ceph/go-ceph/rados"
|
|
|
|
)
|
|
|
|
|
|
|
|
type connEntry struct {
|
|
|
|
conn *rados.Conn
|
|
|
|
lastUsed time.Time
|
|
|
|
users int
|
|
|
|
}
|
|
|
|
|
2020-03-26 12:45:45 +00:00
|
|
|
// ConnPool is the struct which contains details of connection entries in the pool and gc controlled params.
|
2020-01-07 13:38:45 +00:00
|
|
|
type ConnPool struct {
|
|
|
|
// interval to run the garbage collector
|
|
|
|
interval time.Duration
|
|
|
|
// timeout for a connEntry to get garbage collected
|
|
|
|
expiry time.Duration
|
|
|
|
// Timer used to schedule calls to the garbage collector
|
|
|
|
timer *time.Timer
|
|
|
|
// Mutex for loading and touching connEntry's from the conns Map
|
|
|
|
lock *sync.RWMutex
|
|
|
|
// all connEntry's in this pool
|
|
|
|
conns map[string]*connEntry
|
|
|
|
}
|
|
|
|
|
2020-03-26 12:45:45 +00:00
|
|
|
// NewConnPool creates a new connection pool instance and start the garbage collector running
|
2020-01-07 13:38:45 +00:00
|
|
|
// every @interval.
|
|
|
|
func NewConnPool(interval, expiry time.Duration) *ConnPool {
|
|
|
|
cp := ConnPool{
|
|
|
|
interval: interval,
|
|
|
|
expiry: expiry,
|
|
|
|
lock: &sync.RWMutex{},
|
|
|
|
conns: make(map[string]*connEntry),
|
|
|
|
}
|
|
|
|
cp.timer = time.AfterFunc(interval, cp.gc)
|
|
|
|
|
|
|
|
return &cp
|
|
|
|
}
|
|
|
|
|
|
|
|
// loop through all cp.conns and destroy objects that have not been used for cp.expiry.
|
|
|
|
func (cp *ConnPool) gc() {
|
|
|
|
cp.lock.Lock()
|
|
|
|
defer cp.lock.Unlock()
|
|
|
|
|
|
|
|
now := time.Now()
|
|
|
|
for key, ce := range cp.conns {
|
|
|
|
if ce.users == 0 && (now.Sub(ce.lastUsed)) > cp.expiry {
|
|
|
|
ce.destroy()
|
|
|
|
delete(cp.conns, key)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// schedule the next gc() run
|
|
|
|
cp.timer.Reset(cp.interval)
|
|
|
|
}
|
|
|
|
|
2020-03-26 12:45:45 +00:00
|
|
|
// Destroy stops the garbage collector and destroys all connections in the pool.
|
2020-01-07 13:38:45 +00:00
|
|
|
func (cp *ConnPool) Destroy() {
|
|
|
|
cp.timer.Stop()
|
|
|
|
// wait until gc() has finished, in case it is running
|
|
|
|
cp.lock.Lock()
|
|
|
|
defer cp.lock.Unlock()
|
|
|
|
|
|
|
|
for key, ce := range cp.conns {
|
|
|
|
if ce.users != 0 {
|
|
|
|
panic("this connEntry still has users, operations" +
|
|
|
|
"might still be in-flight")
|
|
|
|
}
|
|
|
|
|
|
|
|
ce.destroy()
|
|
|
|
delete(cp.conns, key)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-18 08:30:02 +00:00
|
|
|
func (cp *ConnPool) generateUniqueKey(monitors, user, keyfile string) (string, error) {
|
2020-01-07 13:38:45 +00:00
|
|
|
// the keyfile can be unique for operations, contents will be the same
|
2020-07-20 12:49:05 +00:00
|
|
|
key, err := ioutil.ReadFile(keyfile) // #nosec:G304, file inclusion via variable.
|
2020-01-07 13:38:45 +00:00
|
|
|
if err != nil {
|
2020-06-25 11:30:04 +00:00
|
|
|
return "", fmt.Errorf("could not open keyfile %s: %w", keyfile, err)
|
2020-01-07 13:38:45 +00:00
|
|
|
}
|
|
|
|
|
2020-03-18 08:30:02 +00:00
|
|
|
return fmt.Sprintf("%s|%s|%s", monitors, user, string(key)), nil
|
2020-01-07 13:38:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// getExisting returns the existing rados.Conn associated with the unique key.
|
|
|
|
//
|
2020-07-19 12:21:03 +00:00
|
|
|
// Requires: locked cp.lock because of ce.get().
|
2020-01-07 13:38:45 +00:00
|
|
|
func (cp *ConnPool) getConn(unique string) *rados.Conn {
|
|
|
|
ce, exists := cp.conns[unique]
|
|
|
|
if exists {
|
|
|
|
ce.get()
|
|
|
|
return ce.conn
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-03-26 12:45:45 +00:00
|
|
|
// Get returns a rados.Conn for the given arguments. Creates a new rados.Conn in
|
2020-03-18 08:30:02 +00:00
|
|
|
// case there is none. Use the returned rados.Conn to reduce the reference
|
|
|
|
// count with ConnPool.Put(unique).
|
|
|
|
func (cp *ConnPool) Get(monitors, user, keyfile string) (*rados.Conn, error) {
|
|
|
|
unique, err := cp.generateUniqueKey(monitors, user, keyfile)
|
2020-01-07 13:38:45 +00:00
|
|
|
if err != nil {
|
2020-06-25 11:30:04 +00:00
|
|
|
return nil, fmt.Errorf("failed to generate unique for connection: %w", err)
|
2020-01-07 13:38:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
cp.lock.RLock()
|
|
|
|
conn := cp.getConn(unique)
|
|
|
|
cp.lock.RUnlock()
|
|
|
|
if conn != nil {
|
|
|
|
return conn, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// construct and connect a new rados.Conn
|
|
|
|
args := []string{"-m", monitors, "--keyfile=" + keyfile}
|
2020-04-08 13:15:50 +00:00
|
|
|
conn, err = rados.NewConnWithUser(user)
|
2020-01-07 13:38:45 +00:00
|
|
|
if err != nil {
|
2020-06-25 11:30:04 +00:00
|
|
|
return nil, fmt.Errorf("creating a new connection failed: %w", err)
|
2020-01-07 13:38:45 +00:00
|
|
|
}
|
|
|
|
err = conn.ParseCmdLineArgs(args)
|
|
|
|
if err != nil {
|
2020-06-25 11:30:04 +00:00
|
|
|
return nil, fmt.Errorf("parsing cmdline args (%v) failed: %w", args, err)
|
2020-01-07 13:38:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
err = conn.Connect()
|
|
|
|
if err != nil {
|
2020-06-25 11:30:04 +00:00
|
|
|
return nil, fmt.Errorf("connecting failed: %w", err)
|
2020-01-07 13:38:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
ce := &connEntry{
|
|
|
|
conn: conn,
|
|
|
|
lastUsed: time.Now(),
|
|
|
|
users: 1,
|
|
|
|
}
|
|
|
|
|
|
|
|
cp.lock.Lock()
|
|
|
|
defer cp.lock.Unlock()
|
2021-05-06 09:49:27 +00:00
|
|
|
if oldConn := cp.getConn(unique); oldConn != nil {
|
2020-01-07 13:38:45 +00:00
|
|
|
// there was a race, oldConn already exists
|
|
|
|
ce.destroy()
|
|
|
|
return oldConn, nil
|
|
|
|
}
|
|
|
|
// this really is a new connection, add it to the map
|
|
|
|
cp.conns[unique] = ce
|
|
|
|
|
|
|
|
return conn, nil
|
|
|
|
}
|
|
|
|
|
2021-03-15 09:50:09 +00:00
|
|
|
// Copy adds an extra reference count to the used ConnEntry and returns the
|
|
|
|
// *rados.Conn if it was found.
|
|
|
|
func (cp *ConnPool) Copy(conn *rados.Conn) *rados.Conn {
|
|
|
|
cp.lock.Lock()
|
|
|
|
defer cp.lock.Unlock()
|
|
|
|
|
|
|
|
for _, ce := range cp.conns {
|
|
|
|
if ce.conn == conn {
|
|
|
|
ce.get()
|
|
|
|
return ce.conn
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-03-26 12:45:45 +00:00
|
|
|
// Put reduces the reference count of the rados.Conn object that was returned with
|
2020-01-07 13:38:45 +00:00
|
|
|
// ConnPool.Get().
|
|
|
|
func (cp *ConnPool) Put(conn *rados.Conn) {
|
|
|
|
cp.lock.Lock()
|
|
|
|
defer cp.lock.Unlock()
|
|
|
|
|
|
|
|
for _, ce := range cp.conns {
|
|
|
|
if ce.conn == conn {
|
|
|
|
ce.put()
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Add a reference to the connEntry.
|
|
|
|
// /!\ Only call this while holding the ConnPool.lock.
|
|
|
|
func (ce *connEntry) get() {
|
|
|
|
ce.lastUsed = time.Now()
|
|
|
|
ce.users++
|
|
|
|
}
|
|
|
|
|
|
|
|
// Reduce number of references. If this returns true, there are no more users.
|
|
|
|
// /!\ Only call this while holding the ConnPool.lock.
|
|
|
|
func (ce *connEntry) put() {
|
|
|
|
ce.users--
|
|
|
|
// do not call ce.destroy(), let ConnPool.gc() do that
|
|
|
|
}
|
|
|
|
|
|
|
|
// Destroy a connEntry object, close the connection to the Ceph cluster.
|
|
|
|
func (ce *connEntry) destroy() {
|
|
|
|
if ce.conn != nil {
|
|
|
|
ce.conn.Shutdown()
|
|
|
|
ce.conn = nil
|
|
|
|
}
|
|
|
|
}
|