Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve logging of problems during db connection #1489

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 8 additions & 4 deletions go/db/db.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
"errors"
"fmt"
"regexp"
"strconv"
"strings"
"sync"
"time"
Expand Down Expand Up @@ -100,7 +101,8 @@ func openTopology(host string, port int, readTimeout int) (db *sql.DB, err error
return nil, err
}
}
if db, _, err = sqlutils.GetDB(mysql_uri); err != nil {
sqlUtilsLogger := SqlUtilsLogger{client_context: host + ":" + strconv.Itoa(port)}
if db, _, err = sqlutils.GetDB(mysql_uri, sqlUtilsLogger); err != nil {
return nil, err
}
if config.Config.MySQLConnectionLifetimeSeconds > 0 {
Expand All @@ -123,7 +125,8 @@ func openOrchestratorMySQLGeneric() (db *sql.DB, fromCache bool, err error) {
if config.Config.MySQLOrchestratorUseMutualTLS {
uri, _ = SetupMySQLOrchestratorTLS(uri)
}
return sqlutils.GetDB(uri)
sqlUtilsLogger := SqlUtilsLogger{client_context: config.Config.MySQLOrchestratorHost + ":" + config.Config.MySQLOrchestratorHost}
return sqlutils.GetDB(uri, sqlUtilsLogger)
}

func IsSQLite() bool {
Expand Down Expand Up @@ -174,7 +177,7 @@ func safeMySQLURI(dsn string) string {
func OpenOrchestrator() (db *sql.DB, err error) {
var fromCache bool
if IsSQLite() {
db, fromCache, err = sqlutils.GetSQLiteDB(config.Config.SQLite3DataFile)
db, fromCache, err = sqlutils.GetSQLiteDB(config.Config.SQLite3DataFile, nil)
if err == nil && !fromCache {
log.Debugf("Connected to orchestrator backend: sqlite on %v", config.Config.SQLite3DataFile)
}
Expand All @@ -191,7 +194,8 @@ func OpenOrchestrator() (db *sql.DB, err error) {
}
}
dsn := getMySQLURI()
db, fromCache, err = sqlutils.GetDB(dsn)
sqlUtilsLogger := SqlUtilsLogger{client_context: dsn}
db, fromCache, err = sqlutils.GetDB(dsn, sqlUtilsLogger)
if err == nil && !fromCache {
log.Debugf("Connected to orchestrator backend: %v", safeMySQLURI(dsn))

Expand Down
21 changes: 20 additions & 1 deletion go/db/tls.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ package db
import (
"crypto/tls"
"fmt"
"strconv"
"strings"
"time"

Expand Down Expand Up @@ -55,6 +56,23 @@ func init() {
metrics.Register("instance_tls.write_cache", writeInstanceTLSCacheCounter)
}

type SqlUtilsLogger struct {
client_context string
}

func (logger SqlUtilsLogger) OnError(caller_context string, query string, err error) error {
query = strings.Join(strings.Fields(query), " ") // trim whitespaces
query = strings.Replace(query, "%", "%%", -1) // escape %

msg := fmt.Sprintf("%+v(%+v) %+v: %+v",
caller_context,
logger.client_context,
query,
err)

return log.Errorf(msg)
}

func requiresTLS(host string, port int, mysql_uri string) bool {
cacheKey := fmt.Sprintf("%s:%d", host, port)

Expand All @@ -64,7 +82,8 @@ func requiresTLS(host string, port int, mysql_uri string) bool {
}

required := false
db, _, _ := sqlutils.GetDB(mysql_uri)
sqlUtilsLogger := SqlUtilsLogger{client_context: host + ":" + strconv.Itoa(port)}
db, _, _ := sqlutils.GetDB(mysql_uri, sqlUtilsLogger)
if err := db.Ping(); err != nil && (strings.Contains(err.Error(), Error3159) || strings.Contains(err.Error(), Error1045)) {
required = true
}
Expand Down
2 changes: 1 addition & 1 deletion go/raft/rel_store.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ func (relStore *RelationalStore) openDB() (*sql.DB, error) {

if relStore.backend == nil {
relStoreFile := filepath.Join(relStore.dataDir, raftStoreFile)
sqliteDB, _, err := sqlutils.GetSQLiteDB(relStoreFile)
sqliteDB, _, err := sqlutils.GetSQLiteDB(relStoreFile, nil)
if err != nil {
return nil, err
}
Expand Down