Skip to content

Database governance built for humans and agents — controlling changes and access across every major database.

chore(hive): remove connection pool (#13231)

Azusain3b9fbc82b046
diff --git a/backend/plugin/db/hive/dump.go b/backend/plugin/db/hive/dump.go
index bbcbff8ff5..c9f8b96bae 100644
--- a/backend/plugin/db/hive/dump.go
+++ b/backend/plugin/db/hive/dump.go
@@ -66,13 +66,7 @@ func (d *Driver) Dump(ctx context.Context, out io.Writer) (string, error) {
}
schema := database.Schemas[0]
- conn, err := d.connPool.Get()
- if err != nil {
- return "", err
- }
- defer d.connPool.Put(conn)
-
- cursor := conn.Cursor()
+ cursor := d.conn.Cursor()
cursor.Exec(ctx, fmt.Sprintf("use %s", databaseName))
if cursor.Err != nil {
return "", cursor.Err
@@ -80,7 +74,7 @@ func (d *Driver) Dump(ctx context.Context, out io.Writer) (string, error) {
// dump managed tables.
for _, table := range schema.GetTables() {
- tableDDL, err := showCreateDDL(ctx, conn, "TABLE", table.Name)
+ tableDDL, err := showCreateDDL(ctx, d.conn, "TABLE", table.Name)
if err != nil {
return "", errors.Wrapf(err, "failed to dump table %s", table.Name)
}
@@ -114,7 +108,7 @@ func (d *Driver) Dump(ctx context.Context, out io.Writer) (string, error) {
// dump external tables.
for _, extTable := range schema.GetExternalTables() {
- tabDDL, err := showCreateDDL(ctx, conn, "TABLE", extTable.Name)
+ tabDDL, err := showCreateDDL(ctx, d.conn, "TABLE", extTable.Name)
if err != nil {
return "", errors.Wrapf(err, "failed to dump table %s", extTable.Name)
}
@@ -123,7 +117,7 @@ func (d *Driver) Dump(ctx context.Context, out io.Writer) (string, error) {
// dump views.
for _, view := range schema.GetViews() {
- viewDDL, err := showCreateDDL(ctx, conn, "VIEW", view.Name)
+ viewDDL, err := showCreateDDL(ctx, d.conn, "VIEW", view.Name)
if err != nil {
return "", errors.Wrapf(err, "failed to dump view %s", view.Name)
}
diff --git a/backend/plugin/db/hive/hive.go b/backend/plugin/db/hive/hive.go
index 1d5819c53a..2478fd8b48 100644
--- a/backend/plugin/db/hive/hive.go
+++ b/backend/plugin/db/hive/hive.go
@@ -35,15 +35,13 @@ func init() {
}
type Driver struct {
- config db.ConnectionConfig
- ctx db.ConnectionContext
- connPool *FixedConnPool
- conn *gohive.Connection
+ config db.ConnectionConfig
+ ctx db.ConnectionContext
+ conn *gohive.Connection
}
var (
- _ db.Driver = (*Driver)(nil)
- numMaxConn = 5
+ _ db.Driver = (*Driver)(nil)
)
func (d *Driver) Open(_ context.Context, _ storepb.Engine, config db.ConnectionConfig) (db.Driver, error) {
@@ -54,49 +52,46 @@ func (d *Driver) Open(_ context.Context, _ storepb.Engine, config db.ConnectionC
d.config = config
d.ctx = config.ConnectionContext
- if d.connPool == nil {
- if config.SASLConfig == nil {
- config.SASLConfig = &db.PlainSASLConfig{
- Username: config.Username,
- Password: config.Password,
- }
- }
- if !config.SASLConfig.Check() {
- return nil, errors.New("SASL settings error")
- }
- pool, err := createHiveConnPool(numMaxConn, &config)
- if err != nil {
- return nil, err
+ port, err := strconv.Atoi(config.Port)
+ if err != nil {
+ return nil, errors.Errorf("conversion failure for 'port' [string -> int]")
+ }
+
+ var authConnParam = "NONE"
+ hiveConfig := gohive.NewConnectConfiguration()
+ switch t := config.SASLConfig.(type) {
+ case *db.KerberosConfig:
+ hiveConfig.Hostname = t.Instance
+ hiveConfig.Service = t.Primary
+ db.KrbEnvLock()
+ defer db.KrbEnvUnlock()
+ if err := config.SASLConfig.InitEnv(); err != nil {
+ return nil, errors.Wrapf(err, "failed to init SASL environment")
}
- d.connPool = pool
+ authConnParam = "KERBEROS"
+ case *db.PlainSASLConfig:
+ hiveConfig.Username = t.Username
+ hiveConfig.Password = t.Password
}
- newConn, err := d.connPool.Get()
+ conn, err := gohive.Connect(config.Host, port, authConnParam, hiveConfig)
if err != nil {
- err = errors.Wrapf(err, "failed to get connection from pool")
- // release resources.
- if closeErr := d.connPool.Destroy(); closeErr != nil {
- err = multierr.Combine(closeErr, err)
- }
return nil, err
}
+ d.conn = conn
if config.Database != "" {
- cursor := newConn.Cursor()
+ cursor := d.conn.Cursor()
cursor.Exec(context.Background(), fmt.Sprintf("use %s", config.Database))
if cursor.Err != nil {
- return nil, multierr.Combine(newConn.Close(), cursor.Err)
+ return nil, multierr.Combine(d.conn.Close(), cursor.Err)
}
}
-
- d.conn = newConn
-
return d, nil
}
func (d *Driver) Close(_ context.Context) error {
- d.connPool.Put(d.conn)
- return d.connPool.Destroy()
+ return d.conn.Close()
}
func (d *Driver) Ping(ctx context.Context) error {
@@ -146,12 +141,6 @@ func (d *Driver) QueryConn(ctx context.Context, _ *sql.Conn, statement string, q
return nil, errors.Wrapf(err, "failed to split statements")
}
- conn, err := d.connPool.Get()
- if err != nil {
- return nil, err
- }
- defer d.connPool.Put(conn)
-
var results []*v1pb.QueryResult
for _, singleSQL := range singleSQLs {
statement := util.TrimStatement(singleSQL.Text)
@@ -159,7 +148,7 @@ func (d *Driver) QueryConn(ctx context.Context, _ *sql.Conn, statement string, q
statement = fmt.Sprintf("EXPLAIN %s", statement)
}
- result, err := runSingleStatement(ctx, conn, statement)
+ result, err := runSingleStatement(ctx, d.conn, statement)
if err != nil {
return nil, err
}
diff --git a/backend/plugin/db/hive/hive_conn_pool.go b/backend/plugin/db/hive/hive_conn_pool.go
deleted file mode 100644
index 42bffbe5b0..0000000000
--- a/backend/plugin/db/hive/hive_conn_pool.go
+++ /dev/null
@@ -1,153 +0,0 @@
-package hive
-
-import (
- "strconv"
- "sync"
-
- "github.com/beltran/gohive"
- "github.com/pkg/errors"
- "go.uber.org/multierr"
-
- "github.com/bytebase/bytebase/backend/plugin/db"
-)
-
-type FixedConnPool struct {
- BasicConfig *db.ConnectionConfig
- HiveConfig *gohive.ConnectConfiguration
- Port int
- RWMutex sync.RWMutex
- Connections chan *gohive.Connection
- IsActivated bool
- NumMaxConns int
-}
-
-func createHiveConnPool(
- numMaxConn int,
- config *db.ConnectionConfig,
-) (
- *FixedConnPool, error,
-) {
- conns := make(chan *gohive.Connection, numMaxConn)
- hiveConfig := gohive.NewConnectConfiguration()
- hiveConfig.Database = config.Database
-
- switch t := config.SASLConfig.(type) {
- case *db.KerberosConfig:
- hiveConfig.Hostname = t.Instance
- hiveConfig.Service = t.Primary
-
- case *db.PlainSASLConfig:
- hiveConfig.Username = t.Username
- hiveConfig.Password = t.Password
-
- default:
- return nil, errors.Errorf("invalid SASL config")
- }
-
- port, err := strconv.Atoi(config.Port)
- if err != nil {
- return nil, errors.Errorf("conversion failure for 'port' [string -> int]")
- }
-
- saslTypName := config.SASLConfig.GetTypeName()
- if saslTypName == db.SASLTypeKerberos {
- db.KrbEnvLock()
- defer db.KrbEnvUnlock()
- if err := config.SASLConfig.InitEnv(); err != nil {
- return nil, errors.Wrapf(err, "failed to init SASL environment")
- }
- }
-
- for i := 0; i < numMaxConn; i++ {
- conn, err := gohive.Connect(config.Host, port, string(saslTypName), hiveConfig)
- // release resources if err.
- if err != nil || conn == nil {
- errs := multierr.Combine(errors.New("failed to establish Hive connection"), err)
- close(conns)
- for conn := range conns {
- errs = multierr.Combine(conn.Close(), errs)
- }
- return nil, errs
- }
- conns <- conn
- }
-
- return &FixedConnPool{
- RWMutex: sync.RWMutex{},
- Connections: conns,
- IsActivated: true,
- NumMaxConns: numMaxConn,
- BasicConfig: config,
- HiveConfig: hiveConfig,
- Port: port,
- },
- nil
-}
-
-func (pool *FixedConnPool) Get() (*gohive.Connection, error) {
- pool.RWMutex.RLock()
- if !pool.IsActivated {
- pool.RWMutex.RUnlock()
- return nil, errors.New("connection pool has been closed")
- }
- pool.RWMutex.RUnlock()
-
- var conn *gohive.Connection
-
- select {
- case conn = <-pool.Connections:
-
- default:
- saslTypName := pool.BasicConfig.SASLConfig.GetTypeName()
- if saslTypName == db.SASLTypeKerberos {
- db.KrbEnvLock()
- defer db.KrbEnvUnlock()
- if err := pool.BasicConfig.SASLConfig.InitEnv(); err != nil {
- return nil, errors.Wrapf(err, "failed to init SASL environment")
- }
- }
- var err error
- conn, err = gohive.Connect(pool.BasicConfig.Host, pool.Port, string(saslTypName), pool.HiveConfig)
- if err != nil || conn == nil {
- return nil, errors.Wrapf(err, "failed to get Hive connection")
- }
- }
-
- return conn, nil
-}
-
-func (pool *FixedConnPool) Put(conn *gohive.Connection) {
- pool.RWMutex.RLock()
- if !pool.IsActivated {
- conn.Close()
- pool.RWMutex.RUnlock()
- return
- }
- pool.RWMutex.RUnlock()
- select {
- case pool.Connections <- conn:
- return
- default:
- conn.Close()
- }
-}
-
-func (pool *FixedConnPool) Destroy() error {
- pool.RWMutex.Lock()
- if pool.IsActivated {
- pool.IsActivated = false
- close(pool.Connections)
- } else {
- pool.RWMutex.Unlock()
- return errors.New("connection pool has been closed already")
- }
- pool.RWMutex.Unlock()
-
- var errs error
- for conn := range pool.Connections {
- if err := conn.Close(); err != nil {
- errs = multierr.Combine(err, errs)
- }
- }
- return errs
-}
diff --git a/backend/plugin/db/hive/role.go b/backend/plugin/db/hive/role.go
index 225e52607d..652ce6433c 100644
--- a/backend/plugin/db/hive/role.go
+++ b/backend/plugin/db/hive/role.go
@@ -29,11 +29,7 @@ func (*Driver) FindRole(_ context.Context, _ string) (*db.DatabaseRoleMessage, e
}
func (d *Driver) ListRole(ctx context.Context) ([]*db.DatabaseRoleMessage, error) {
- conn, err := d.connPool.Get()
- if err != nil {
- return nil, err
- }
- roleResult, err := runSingleStatement(ctx, conn, "SHOW ROLES")
+ roleResult, err := runSingleStatement(ctx, d.conn, "SHOW ROLES")
if err != nil {
return nil, errors.Wrapf(err, "failed to get roles")
}
@@ -56,11 +52,7 @@ func (d *Driver) DeleteRole(ctx context.Context, roleName string) error {
}
func (d *Driver) GetRoleGrant(ctx context.Context, roleName string) (string, error) {
- conn, err := d.connPool.Get()
- if err != nil {
- return "", err
- }
- grantResult, err := runSingleStatement(ctx, conn, fmt.Sprintf("SHOW GRANT ROLE %s", roleName))
+ grantResult, err := runSingleStatement(ctx, d.conn, fmt.Sprintf("SHOW GRANT ROLE %s", roleName))
if err != nil {
return "", errors.Wrapf(err, "failed to get grant from %s", roleName)
}
diff --git a/backend/plugin/db/hive/sync.go b/backend/plugin/db/hive/sync.go
index 22b116db0d..e161959a36 100644
--- a/backend/plugin/db/hive/sync.go
+++ b/backend/plugin/db/hive/sync.go
@@ -65,12 +65,7 @@ func (*Driver) CheckSlowQueryLogEnabled(_ context.Context) error {
}
func (d *Driver) getVersion(ctx context.Context) (string, error) {
- conn, err := d.connPool.Get()
- if err != nil {
- return "", err
- }
- defer d.connPool.Put(conn)
- result, err := runSingleStatement(ctx, conn, "SELECT VERSION()")
+ result, err := runSingleStatement(ctx, d.conn, "SELECT VERSION()")
if err != nil {
return "", errors.Wrap(err, "failed to get version from instance")
}
@@ -89,12 +84,7 @@ func (d *Driver) getVersion(ctx context.Context) (string, error) {
func (d *Driver) getDatabaseNames(ctx context.Context) ([]string, error) {
var databaseNames []string
- conn, err := d.connPool.Get()
- if err != nil {
- return nil, err
- }
- defer d.connPool.Put(conn)
- result, err := runSingleStatement(ctx, conn, "SHOW DATABASES")
+ result, err := runSingleStatement(ctx, d.conn, "SHOW DATABASES")
if err != nil {
return nil, errors.Wrap(err, "failed to get version from instance")
}
@@ -108,12 +98,7 @@ func (d *Driver) getDatabaseNames(ctx context.Context) ([]string, error) {
}
func (d *Driver) listTablesNames(ctx context.Context, databaseName string) ([]string, error) {
- conn, err := d.connPool.Get()
- if err != nil {
- return nil, err
- }
- defer d.connPool.Put(conn)
- result, err := runSingleStatement(ctx, conn, fmt.Sprintf("SHOW TABLES FROM %s", databaseName))
+ result, err := runSingleStatement(ctx, d.conn, fmt.Sprintf("SHOW TABLES FROM %s", databaseName))
if err != nil {
return nil, errors.Wrap(err, "failed to get version from instance")
}
@@ -204,12 +189,7 @@ func (d *Driver) getTables(ctx context.Context, databaseName string) (
func (d *Driver) getPartitions(ctx context.Context, databaseName, tableName string) ([]*storepb.TablePartitionMetadata, error) {
// partitions.
- conn, err := d.connPool.Get()
- if err != nil {
- return nil, err
- }
- defer d.connPool.Put(conn)
- partitionResult, err := runSingleStatement(ctx, conn, fmt.Sprintf("SHOW PARTITIONS `%s`.`%s`", databaseName, tableName))
+ partitionResult, err := runSingleStatement(ctx, d.conn, fmt.Sprintf("SHOW PARTITIONS `%s`.`%s`", databaseName, tableName))
if err != nil {
slog.Debug("failed to get partitions", log.BBError(err))
return nil, nil
@@ -267,16 +247,8 @@ func (d *Driver) getTableInfo(ctx context.Context, tabName string, databaseName
totalSize int
numRows int
)
- conn, err := d.connPool.Get()
- if err != nil {
- return nil, err
- }
- cursor := conn.Cursor()
- defer func() {
- cursor.Close()
- d.connPool.Put(conn)
- }()
+ cursor := d.conn.Cursor()
cursor.Exec(ctx, fmt.Sprintf("DESCRIBE FORMATTED `%s`.`%s`", databaseName, tabName))
if cursor.Err != nil {
return nil, errors.Wrapf(cursor.Err, "failed to describe table %s", tabName)