Skip to content

Commit 4b19215

Browse files
Gustedearl-warren
authored andcommitted
[GITEA] Add slow SQL query warning
- Databases are one of the most important parts of Forgejo, every interaction with Forgejo uses the database in one way or another. Therefore, it is important to maintain the database and recognize when Forgejo is not doing well with the database. Forgejo already has the option to log *every* SQL query along with its execution time, but monitoring becomes impractical for larger instances and takes up unnecessary storage in the logs. - Add a QoL enhancement that allows instance administrators to specify a threshold value beyond which query execution time is logged as a warning in the xorm logger. The default value is a conservative five seconds to avoid this becoming a source of spam in the logs. - The use case for this patch is that with an instance the size of Codeberg, monitoring SQL logs is not very fruitful and most of them are uninteresting. Recently, in the context of persistent deadlock issues (https://codeberg.org/forgejo/forgejo/issues/220), I have noticed that certain queries hold locks on tables like comment and issue for several seconds. This patch helps to identify which queries these are and when they happen. - Added unit test. (cherry picked from commit 24bbe7886fb4cb9a38c8dab8c44f4c9cbfa25481) (cherry picked from commit 6e29145b3c1455498531593d38e6a914941a12cb) (cherry picked from commit 63731e30712872bd2395eb3cf36d9996e5793645) (cherry picked from commit 3ce1a097369c132654de70df707b867e47bd1c40) (cherry picked from commit a64426907de788cc0937a7a2b16af4d2f26f7fe6)
1 parent 3cbd9fb commit 4b19215

File tree

3 files changed

+68
-0
lines changed

3 files changed

+68
-0
lines changed

models/db/engine.go

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,13 @@ import (
1111
"io"
1212
"reflect"
1313
"strings"
14+
"time"
1415

16+
"code.gitea.io/gitea/modules/log"
1517
"code.gitea.io/gitea/modules/setting"
1618

1719
"xorm.io/xorm"
20+
"xorm.io/xorm/contexts"
1821
"xorm.io/xorm/names"
1922
"xorm.io/xorm/schemas"
2023

@@ -147,6 +150,13 @@ func InitEngine(ctx context.Context) error {
147150
xormEngine.SetConnMaxLifetime(setting.Database.ConnMaxLifetime)
148151
xormEngine.SetDefaultContext(ctx)
149152

153+
if setting.Database.SlowQueryTreshold > 0 {
154+
xormEngine.AddHook(&SlowQueryHook{
155+
Treshold: setting.Database.SlowQueryTreshold,
156+
Logger: log.GetLogger("xorm"),
157+
})
158+
}
159+
150160
SetDefaultEngine(ctx, xormEngine)
151161
return nil
152162
}
@@ -300,3 +310,21 @@ func SetLogSQL(ctx context.Context, on bool) {
300310
sess.Engine().ShowSQL(on)
301311
}
302312
}
313+
314+
type SlowQueryHook struct {
315+
Treshold time.Duration
316+
Logger log.Logger
317+
}
318+
319+
var _ contexts.Hook = &SlowQueryHook{}
320+
321+
func (SlowQueryHook) BeforeProcess(c *contexts.ContextHook) (context.Context, error) {
322+
return c.Ctx, nil
323+
}
324+
325+
func (h *SlowQueryHook) AfterProcess(c *contexts.ContextHook) error {
326+
if c.ExecuteTime >= h.Treshold {
327+
h.Logger.Log(8, log.WARN, "[Slow SQL Query] %s %v - %v", c.SQL, c.Args, c.ExecuteTime)
328+
}
329+
return nil
330+
}

models/db/engine_test.go

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,19 @@ package db_test
66
import (
77
"path/filepath"
88
"testing"
9+
"time"
910

1011
"code.gitea.io/gitea/models/db"
1112
issues_model "code.gitea.io/gitea/models/issues"
1213
"code.gitea.io/gitea/models/unittest"
14+
"code.gitea.io/gitea/modules/log"
1315
"code.gitea.io/gitea/modules/setting"
16+
"code.gitea.io/gitea/modules/test"
1417

1518
_ "code.gitea.io/gitea/cmd" // for TestPrimaryKeys
1619

1720
"github.com/stretchr/testify/assert"
21+
"xorm.io/xorm"
1822
)
1923

2024
func TestDumpDatabase(t *testing.T) {
@@ -85,3 +89,37 @@ func TestPrimaryKeys(t *testing.T) {
8589
}
8690
}
8791
}
92+
93+
func TestSlowQuery(t *testing.T) {
94+
lc, cleanup := test.NewLogChecker("slow-query")
95+
lc.StopMark("[Slow SQL Query]")
96+
defer cleanup()
97+
98+
e := db.GetEngine(db.DefaultContext)
99+
engine, ok := e.(*xorm.Engine)
100+
assert.True(t, ok)
101+
102+
// It's not possible to clean this up with XORM, but it's luckily not harmful
103+
// to leave around.
104+
engine.AddHook(&db.SlowQueryHook{
105+
Treshold: time.Second * 10,
106+
Logger: log.GetLogger("slow-query"),
107+
})
108+
109+
// NOOP query.
110+
e.Exec("SELECT 1 WHERE false;")
111+
112+
_, stopped := lc.Check(100 * time.Millisecond)
113+
assert.False(t, stopped)
114+
115+
engine.AddHook(&db.SlowQueryHook{
116+
Treshold: 0, // Every query should be logged.
117+
Logger: log.GetLogger("slow-query"),
118+
})
119+
120+
// NOOP query.
121+
e.Exec("SELECT 1 WHERE false;")
122+
123+
_, stopped = lc.Check(100 * time.Millisecond)
124+
assert.True(t, stopped)
125+
}

modules/setting/database.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ var (
4444
ConnMaxLifetime time.Duration
4545
IterateBufferSize int
4646
AutoMigration bool
47+
SlowQueryTreshold time.Duration
4748
}{
4849
Timeout: 500,
4950
IterateBufferSize: 50,
@@ -86,6 +87,7 @@ func loadDBSetting(rootCfg ConfigProvider) {
8687
Database.DBConnectRetries = sec.Key("DB_RETRIES").MustInt(10)
8788
Database.DBConnectBackoff = sec.Key("DB_RETRY_BACKOFF").MustDuration(3 * time.Second)
8889
Database.AutoMigration = sec.Key("AUTO_MIGRATION").MustBool(true)
90+
Database.SlowQueryTreshold = sec.Key("SLOW_QUERY_TRESHOLD").MustDuration(5 * time.Second)
8991
}
9092

9193
// DBConnStr returns database connection string

0 commit comments

Comments
 (0)