Skip to content

Commit 04c2ed5

Browse files
committed
Monitoring slow queries via table log can be disable via monitor-queries = false
Monitoring slow queries preserve log table via tracking last slow query timestamp
1 parent fbd6f07 commit 04c2ed5

File tree

3 files changed

+33
-12
lines changed

3 files changed

+33
-12
lines changed

cluster/srv.go

+14-8
Original file line numberDiff line numberDiff line change
@@ -170,6 +170,8 @@ type ServerMonitor struct {
170170
SSTPort string `json:"sstPort"` //used to send data to dbjobs
171171
Agent string `json:"agent"` //used to provision service in orchestrator
172172
BinaryLogFiles map[string]uint `json:"binaryLogFiles"`
173+
MaxSlowQueryTimestamp int64 `json:"maxSlowQueryTimestamp"`
174+
IsInSlowQueryCapture bool
173175
}
174176

175177
type serverList []*ServerMonitor
@@ -1228,16 +1230,18 @@ func (server *ServerMonitor) Capture() error {
12281230

12291231
func (server *ServerMonitor) SaveInfos() error {
12301232
type Save struct {
1231-
Variables map[string]string `json:"variables"`
1232-
ProcessList []dbhelper.Processlist `json:"processlist"`
1233-
Status map[string]string `json:"status"`
1234-
SlaveStatus []dbhelper.SlaveStatus `json:"slavestatus"`
1233+
Variables map[string]string `json:"variables"`
1234+
ProcessList []dbhelper.Processlist `json:"processlist"`
1235+
Status map[string]string `json:"status"`
1236+
SlaveStatus []dbhelper.SlaveStatus `json:"slavestatus"`
1237+
MaxSlowQueryTimestamp int64 `json:"maxSlowQueryTimestamp"`
12351238
}
12361239
var clsave Save
12371240
clsave.Variables = server.Variables
12381241
clsave.Status = server.Status
12391242
clsave.ProcessList = server.FullProcessList
12401243
clsave.SlaveStatus = server.LastSeenReplications
1244+
clsave.MaxSlowQueryTimestamp = server.MaxSlowQueryTimestamp
12411245
saveJSON, _ := json.MarshalIndent(clsave, "", "\t")
12421246
err := ioutil.WriteFile(server.Datadir+"/serverstate.json", saveJSON, 0644)
12431247
if err != nil {
@@ -1248,10 +1252,11 @@ func (server *ServerMonitor) SaveInfos() error {
12481252

12491253
func (server *ServerMonitor) ReloadSaveInfosVariables() error {
12501254
type Save struct {
1251-
Variables map[string]string `json:"variables"`
1252-
ProcessList []dbhelper.Processlist `json:"processlist"`
1253-
Status map[string]string `json:"status"`
1254-
SlaveStatus []dbhelper.SlaveStatus `json:"slavestatus"`
1255+
Variables map[string]string `json:"variables"`
1256+
ProcessList []dbhelper.Processlist `json:"processlist"`
1257+
Status map[string]string `json:"status"`
1258+
SlaveStatus []dbhelper.SlaveStatus `json:"slavestatus"`
1259+
MaxSlowQueryTimestamp int64 `json:"maxSlowQueryTimestamp"`
12551260
}
12561261

12571262
var clsave Save
@@ -1266,6 +1271,7 @@ func (server *ServerMonitor) ReloadSaveInfosVariables() error {
12661271
return err
12671272
}
12681273
server.Variables = clsave.Variables
1274+
server.MaxSlowQueryTimestamp = clsave.MaxSlowQueryTimestamp
12691275
return nil
12701276
}
12711277

cluster/srv_get.go

+14-4
Original file line numberDiff line numberDiff line change
@@ -514,6 +514,7 @@ func (server *ServerMonitor) GetNewDBConn() (*sqlx.DB, error) {
514514
}
515515

516516
func (server *ServerMonitor) GetSlowLogTable() {
517+
517518
if server.ClusterGroup.IsInFailover() {
518519
return
519520
}
@@ -523,6 +524,14 @@ func (server *ServerMonitor) GetSlowLogTable() {
523524
if server.IsDown() {
524525
return
525526
}
527+
if !server.GetCluster().GetConf().MonitorQueries {
528+
return
529+
}
530+
if server.IsInSlowQueryCapture {
531+
return
532+
}
533+
server.IsInSlowQueryCapture = true
534+
defer func() { server.IsInSlowQueryCapture = false }()
526535
f, err := os.OpenFile(server.Datadir+"/log/log_slow_query.log", os.O_CREATE|os.O_APPEND|os.O_WRONLY, 0600)
527536
if err != nil {
528537
server.ClusterGroup.LogPrintf(LvlErr, "Error writing slow queries %s", err)
@@ -538,11 +547,10 @@ func (server *ServerMonitor) GetSlowLogTable() {
538547
slowqueries := []dbhelper.LogSlow{}
539548

540549
if server.DBVersion.IsMySQLOrPercona() {
541-
err = server.Conn.Select(&slowqueries, "SELECT FLOOR(UNIX_TIMESTAMP(start_time)) as start_time, user_host,TIME_TO_SEC(query_time) AS query_time,TIME_TO_SEC(lock_time) AS lock_time,rows_sent,rows_examined,db,last_insert_id,insert_id,server_id,sql_text,thread_id, 0 as rows_affected FROM mysql.slow_log")
550+
err = server.Conn.Select(&slowqueries, "SELECT FLOOR(UNIX_TIMESTAMP(start_time)) as start_time, user_host,TIME_TO_SEC(query_time) AS query_time,TIME_TO_SEC(lock_time) AS lock_time,rows_sent,rows_examined,db,last_insert_id,insert_id,server_id,sql_text,thread_id, 0 as rows_affected FROM mysql.slow_log WHERE start_time > FROM_UNIXTIME("+strconv.FormatInt(server.MaxSlowQueryTimestamp+1, 10)+")")
542551
} else {
543-
err = server.Conn.Select(&slowqueries, "SELECT FLOOR(UNIX_TIMESTAMP(start_time)) as start_time, user_host,TIME_TO_SEC(query_time) AS query_time,TIME_TO_SEC(lock_time) AS lock_time,rows_sent,rows_examined,db,last_insert_id,insert_id,server_id,sql_text,thread_id,0 as rows_affected FROM mysql.slow_log")
552+
err = server.Conn.Select(&slowqueries, "SELECT FLOOR(UNIX_TIMESTAMP(start_time)) as start_time, user_host,TIME_TO_SEC(query_time) AS query_time,TIME_TO_SEC(lock_time) AS lock_time,rows_sent,rows_examined,db,last_insert_id,insert_id,server_id,sql_text,thread_id,0 as rows_affected FROM mysql.slow_log WHERE start_time > FROM_UNIXTIME("+strconv.FormatInt(server.MaxSlowQueryTimestamp+1, 10)+")")
544553
}
545-
546554
if err != nil {
547555
server.ClusterGroup.LogPrintf(LvlErr, "Could not get slow queries from table %s", err)
548556
}
@@ -560,8 +568,10 @@ func (server *ServerMonitor) GetSlowLogTable() {
560568
s.Start_time,
561569
strings.Replace(strings.Replace(s.Sql_text.String, "\r\n", " ", -1), "\n", " ", -1),
562570
)
571+
server.MaxSlowQueryTimestamp = s.Start_time
563572
}
564-
server.ExecQueryNoBinLog("TRUNCATE mysql.slow_log")
573+
574+
// server.ExecQueryNoBinLog("TRUNCATE mysql.slow_log")
565575
}
566576

567577
func (server *ServerMonitor) GetTables() []v3.Table {

etc/opensvc/cluster-api/cluster-demo/stephane.toml

+5
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,11 @@ prov-proxy-service-type = "docker"
3434
prov-proxy-disk-type = "volume"
3535
prov-proxy-volume-data = "tank"
3636
test=true
37+
38+
39+
alert-pushover-app-token="ad5t1i6m4491ioauubaitqtg1kewtu"
40+
alert-pushover-user-token="uixs8gufi8859u8k8zqeojx7i948fi"
41+
3742
#monitoring-ssl-cert="/Users/apple/.ssh/id_rsa"
3843
#monitoring-ssl-key="/Users/apple/.ssh/id_rsa.pub"
3944

0 commit comments

Comments
 (0)