Track goids when running with tracing enabled (#1413)

* Track goids when running with tracing enabled

* Linting
This commit is contained in:
Kegsay 2020-09-08 17:30:05 +01:00 committed by GitHub
parent 668a722ee0
commit b4bd0cc0f5
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 38 additions and 0 deletions

View file

@ -22,7 +22,10 @@ import (
"io" "io"
"os" "os"
"regexp" "regexp"
"runtime"
"strconv"
"strings" "strings"
"sync"
"time" "time"
"github.com/matrix-org/dendrite/internal/config" "github.com/matrix-org/dendrite/internal/config"
@ -31,6 +34,7 @@ import (
) )
var tracingEnabled = os.Getenv("DENDRITE_TRACE_SQL") == "1" var tracingEnabled = os.Getenv("DENDRITE_TRACE_SQL") == "1"
var goidToWriter sync.Map
type traceInterceptor struct { type traceInterceptor struct {
sqlmw.NullInterceptor sqlmw.NullInterceptor
@ -40,6 +44,8 @@ func (in *traceInterceptor) StmtQueryContext(ctx context.Context, stmt driver.St
startedAt := time.Now() startedAt := time.Now()
rows, err := stmt.QueryContext(ctx, args) rows, err := stmt.QueryContext(ctx, args)
trackGoID(query)
logrus.WithField("duration", time.Since(startedAt)).WithField(logrus.ErrorKey, err).Debug("executed sql query ", query, " args: ", args) logrus.WithField("duration", time.Since(startedAt)).WithField(logrus.ErrorKey, err).Debug("executed sql query ", query, " args: ", args)
return rows, err return rows, err
@ -49,6 +55,8 @@ func (in *traceInterceptor) StmtExecContext(ctx context.Context, stmt driver.Stm
startedAt := time.Now() startedAt := time.Now()
result, err := stmt.ExecContext(ctx, args) result, err := stmt.ExecContext(ctx, args)
trackGoID(query)
logrus.WithField("duration", time.Since(startedAt)).WithField(logrus.ErrorKey, err).Debug("executed sql query ", query, " args: ", args) logrus.WithField("duration", time.Since(startedAt)).WithField(logrus.ErrorKey, err).Debug("executed sql query ", query, " args: ", args)
return result, err return result, err
@ -75,6 +83,19 @@ func (in *traceInterceptor) RowsNext(c context.Context, rows driver.Rows, dest [
return err return err
} }
func trackGoID(query string) {
thisGoID := goid()
if _, ok := goidToWriter.Load(thisGoID); ok {
return // we're on a writer goroutine
}
q := strings.TrimSpace(query)
if strings.HasPrefix(q, "SELECT") {
return // SELECTs can go on other goroutines
}
logrus.Warnf("unsafe goid: SQL executed not on an ExclusiveWriter: %s", q)
}
// Open opens a database specified by its database driver name and a driver-specific data source name, // Open opens a database specified by its database driver name and a driver-specific data source name,
// usually consisting of at least a database name and connection information. Includes tracing driver // usually consisting of at least a database name and connection information. Includes tracing driver
// if DENDRITE_TRACE_SQL=1 // if DENDRITE_TRACE_SQL=1
@ -119,3 +140,14 @@ func Open(dbProperties *config.DatabaseOptions) (*sql.DB, error) {
func init() { func init() {
registerDrivers() registerDrivers()
} }
func goid() int {
var buf [64]byte
n := runtime.Stack(buf[:], false)
idField := strings.Fields(strings.TrimPrefix(string(buf[:n]), "goroutine "))[0]
id, err := strconv.Atoi(idField)
if err != nil {
panic(fmt.Sprintf("cannot get goroutine id: %v", err))
}
return id
}

View file

@ -60,6 +60,12 @@ func (w *ExclusiveWriter) run() {
if !w.running.CAS(false, true) { if !w.running.CAS(false, true) {
return return
} }
if tracingEnabled {
gid := goid()
goidToWriter.Store(gid, w)
defer goidToWriter.Delete(gid)
}
defer w.running.Store(false) defer w.running.Store(false)
for task := range w.todo { for task := range w.todo {
if task.db != nil && task.txn != nil { if task.db != nil && task.txn != nil {