Skip to content

Commit e87c6f1

Browse files
alivxxxjackysp
authored andcommitted
executor: replace logger with zap logger (#9521) (#10043)
1 parent 0896d15 commit e87c6f1

File tree

19 files changed

+90
-65
lines changed

19 files changed

+90
-65
lines changed

executor/adapter.go

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import (
2121
"time"
2222

2323
"github.com/pingcap/errors"
24+
"github.com/pingcap/log"
2425
"github.com/pingcap/parser/ast"
2526
"github.com/pingcap/parser/model"
2627
"github.com/pingcap/parser/mysql"
@@ -38,8 +39,8 @@ import (
3839
"github.com/pingcap/tidb/util/chunk"
3940
"github.com/pingcap/tidb/util/logutil"
4041
"github.com/pingcap/tidb/util/sqlexec"
41-
log "github.com/sirupsen/logrus"
4242
"go.uber.org/zap"
43+
"go.uber.org/zap/zapcore"
4344
"golang.org/x/net/context"
4445
)
4546

@@ -282,7 +283,7 @@ func (a *ExecStmt) handleNoDelayExecutor(ctx context.Context, sctx sessionctx.Co
282283
txnTS := uint64(0)
283284
// Don't active pending txn here.
284285
if txn, err1 := sctx.Txn(false); err1 != nil {
285-
log.Error(err1)
286+
logutil.Logger(ctx).Error("get current transaction failed", zap.Error(err))
286287
} else {
287288
if txn.Valid() {
288289
txnTS = txn.StartTS()
@@ -310,7 +311,7 @@ func (a *ExecStmt) buildExecutor(ctx sessionctx.Context) (Executor, error) {
310311
return nil, errors.Trace(err)
311312
}
312313
if isPointGet {
313-
log.Debugf("con:%d InitTxnWithStartTS %s", ctx.GetSessionVars().ConnectionID, a.Text)
314+
logutil.Logger(context.Background()).Debug("init txnStartTS with MaxUint64", zap.Uint64("conn", ctx.GetSessionVars().ConnectionID), zap.String("text", a.Text))
314315
err = ctx.InitTxnWithStartTS(math.MaxUint64)
315316
}
316317
if err != nil {
@@ -374,13 +375,13 @@ func (a *ExecStmt) logAudit() {
374375
// LogSlowQuery is used to print the slow query in the log files.
375376
func (a *ExecStmt) LogSlowQuery(txnTS uint64, succ bool) {
376377
level := log.GetLevel()
377-
if level < log.WarnLevel {
378+
if level > zapcore.WarnLevel {
378379
return
379380
}
380381
cfg := config.GetGlobalConfig()
381382
costTime := time.Since(a.StartTime)
382383
threshold := time.Duration(atomic.LoadUint64(&cfg.Log.SlowThreshold)) * time.Millisecond
383-
if costTime < threshold && level < log.DebugLevel {
384+
if costTime < threshold && level > zapcore.DebugLevel {
384385
return
385386
}
386387
sql := a.Text

executor/admin.go

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -30,10 +30,11 @@ import (
3030
"github.com/pingcap/tidb/tablecodec"
3131
"github.com/pingcap/tidb/types"
3232
"github.com/pingcap/tidb/util/chunk"
33+
"github.com/pingcap/tidb/util/logutil"
3334
"github.com/pingcap/tidb/util/ranger"
3435
"github.com/pingcap/tidb/util/timeutil"
3536
"github.com/pingcap/tipb/go-tipb"
36-
log "github.com/sirupsen/logrus"
37+
"go.uber.org/zap"
3738
"golang.org/x/net/context"
3839
)
3940

@@ -308,8 +309,9 @@ func (e *RecoverIndexExec) backfillIndex(ctx context.Context) (int64, int64, err
308309
totalScanCnt += result.scanRowCount
309310
if totalScanCnt-lastLogCnt >= 50000 {
310311
lastLogCnt = totalScanCnt
311-
log.Infof("[recover-index] recover table:%v, index:%v, totalAddedCnt:%v, totalScanCnt:%v, nextHandle: %v",
312-
e.table.Meta().Name.O, e.index.Meta().Name.O, totalAddedCnt, totalScanCnt, result.nextHandle)
312+
logutil.Logger(ctx).Info("recover index", zap.String("table", e.table.Meta().Name.O),
313+
zap.String("index", e.index.Meta().Name.O), zap.Int64("totalAddedCnt", totalAddedCnt),
314+
zap.Int64("totalScanCnt", totalScanCnt), zap.Int64("nextHandle", result.nextHandle))
313315
}
314316

315317
// no more rows
@@ -393,8 +395,9 @@ func (e *RecoverIndexExec) batchMarkDup(txn kv.Transaction, rows []recoverRows)
393395
}
394396

395397
if handle != rows[i].handle {
396-
log.Warnf("[recover-index] The constraint of unique index:%v is broken, handle:%v is not equal handle:%v with idxKey:%v.",
397-
e.index.Meta().Name.O, handle, rows[i].handle, key)
398+
logutil.Logger(context.Background()).Warn("recover index: the constraint of unique index is broken, handle in index is not equal to handle in table",
399+
zap.String("index", e.index.Meta().Name.O), zap.ByteString("indexKey", key),
400+
zap.Int64("handleInTable", rows[i].handle), zap.Int64("handleInIndex", handle))
398401
}
399402
}
400403
rows[i].skip = true
@@ -520,8 +523,8 @@ func (e *CleanupIndexExec) deleteDanglingIdx(txn kv.Transaction, values map[stri
520523
}
521524
e.removeCnt++
522525
if e.removeCnt%e.batchSize == 0 {
523-
log.Infof("[cleaning up dangling index] table: %v, index: %v, count: %v.",
524-
e.table.Meta().Name.String(), e.index.Meta().Name.String(), e.removeCnt)
526+
logutil.Logger(context.Background()).Info("clean up dangling index", zap.String("table", e.table.Meta().Name.String()),
527+
zap.String("index", e.index.Meta().Name.String()), zap.Uint64("count", e.removeCnt))
525528
}
526529
}
527530
}

executor/aggregate.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24,12 +24,12 @@ import (
2424
"github.com/pingcap/tidb/sessionctx"
2525
"github.com/pingcap/tidb/sessionctx/stmtctx"
2626
"github.com/pingcap/tidb/types"
27-
"github.com/pingcap/tidb/util"
2827
"github.com/pingcap/tidb/util/chunk"
2928
"github.com/pingcap/tidb/util/codec"
29+
"github.com/pingcap/tidb/util/logutil"
3030
"github.com/pingcap/tidb/util/set"
31-
log "github.com/sirupsen/logrus"
3231
"github.com/spaolacci/murmur3"
32+
"go.uber.org/zap"
3333
"golang.org/x/net/context"
3434
)
3535

@@ -317,9 +317,9 @@ func (w *HashAggPartialWorker) getChildInput() bool {
317317
}
318318

319319
func recoveryHashAgg(output chan *AfFinalResult, r interface{}) {
320+
err := errors.Errorf("%v", r)
320321
output <- &AfFinalResult{err: errors.Errorf("%v", r)}
321-
buf := util.GetStack()
322-
log.Errorf("panic in the recoverable goroutine: %v, stack trace:\n%s", r, buf)
322+
logutil.Logger(context.Background()).Error("parallel hash aggregation panicked", zap.Error(err))
323323
}
324324

325325
func (w *HashAggPartialWorker) run(ctx sessionctx.Context, waitGroup *sync.WaitGroup, finalConcurrency int) {
@@ -474,7 +474,7 @@ func (w *HashAggFinalWorker) getFinalResult(sctx sessionctx.Context) {
474474
partialResults := w.getPartialResult(sctx.GetSessionVars().StmtCtx, []byte(groupKey), w.partialResultMap)
475475
for i, af := range w.aggFuncs {
476476
if err := af.AppendFinalResult2Chunk(sctx, partialResults[i], result); err != nil {
477-
log.Error(errors.ErrorStack(err))
477+
logutil.Logger(context.Background()).Error("HashAggFinalWorker failed to append final result to Chunk", zap.Error(err))
478478
}
479479
}
480480
if len(w.aggFuncs) == 0 {

executor/analyze.go

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,10 @@ import (
2828
"github.com/pingcap/tidb/statistics"
2929
"github.com/pingcap/tidb/tablecodec"
3030
"github.com/pingcap/tidb/util/chunk"
31+
"github.com/pingcap/tidb/util/logutil"
3132
"github.com/pingcap/tidb/util/ranger"
3233
"github.com/pingcap/tipb/go-tipb"
33-
log "github.com/sirupsen/logrus"
34+
"go.uber.org/zap"
3435
"golang.org/x/net/context"
3536
)
3637

@@ -72,15 +73,16 @@ func (e *AnalyzeExec) Next(ctx context.Context, chk *chunk.Chunk) error {
7273
err = result.Err
7374
if errors.Trace(err) == errAnalyzeWorkerPanic {
7475
panicCnt++
76+
} else {
77+
logutil.Logger(ctx).Error("analyze failed", zap.Error(err))
7578
}
76-
log.Error(errors.ErrorStack(err))
7779
continue
7880
}
7981
for i, hg := range result.Hist {
8082
err1 := statsHandle.SaveStatsToStorage(result.PhysicalTableID, result.Count, result.IsIndex, hg, result.Cms[i], 1)
8183
if err1 != nil {
8284
err = err1
83-
log.Error(errors.ErrorStack(err))
85+
logutil.Logger(ctx).Error("save stats to storage failed", zap.Error(err))
8486
continue
8587
}
8688
}
@@ -122,7 +124,7 @@ func (e *AnalyzeExec) analyzeWorker(taskCh <-chan *analyzeTask, resultCh chan<-
122124
buf := make([]byte, 4096)
123125
stackSize := runtime.Stack(buf, false)
124126
buf = buf[:stackSize]
125-
log.Errorf("analyzeWorker panic stack is:\n%s", buf)
127+
logutil.Logger(context.Background()).Error("analyze worker panicked", zap.String("stack", string(buf)))
126128
metrics.PanicCounter.WithLabelValues(metrics.LabelAnalyze).Inc()
127129
resultCh <- statistics.AnalyzeResult{
128130
Err: errAnalyzeWorkerPanic,
@@ -246,7 +248,7 @@ func (e *AnalyzeIndexExec) buildStatsFromResult(result distsql.SelectResult, nee
246248
}
247249
if needCMS {
248250
if resp.Cms == nil {
249-
log.Warnf("nil CMS in response, table is %s, index is %s", e.idxInfo.Table.O, e.idxInfo.Name.O)
251+
logutil.Logger(context.TODO()).Warn("nil CMS in response", zap.String("table", e.idxInfo.Table.O), zap.String("index", e.idxInfo.Name.O))
250252
} else {
251253
err := cms.MergeCMSketch(statistics.CMSketchFromProto(resp.Cms))
252254
if err != nil {

executor/checksum.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,10 @@ import (
2323
"github.com/pingcap/tidb/sessionctx"
2424
"github.com/pingcap/tidb/sessionctx/variable"
2525
"github.com/pingcap/tidb/util/chunk"
26+
"github.com/pingcap/tidb/util/logutil"
2627
"github.com/pingcap/tidb/util/ranger"
2728
"github.com/pingcap/tipb/go-tipb"
28-
log "github.com/sirupsen/logrus"
29+
"go.uber.org/zap"
2930
"golang.org/x/net/context"
3031
)
3132

@@ -70,7 +71,7 @@ func (e *ChecksumTableExec) Open(ctx context.Context) error {
7071
result := <-resultCh
7172
if result.Error != nil {
7273
err = result.Error
73-
log.Error(errors.ErrorStack(err))
74+
logutil.Logger(ctx).Error("checksum failed", zap.Error(err))
7475
continue
7576
}
7677
e.handleResult(result)

executor/compiler.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,8 @@ import (
2424
"github.com/pingcap/tidb/metrics"
2525
plannercore "github.com/pingcap/tidb/planner/core"
2626
"github.com/pingcap/tidb/sessionctx"
27-
log "github.com/sirupsen/logrus"
27+
"github.com/pingcap/tidb/util/logutil"
28+
"go.uber.org/zap"
2829
"golang.org/x/net/context"
2930
)
3031

@@ -75,7 +76,7 @@ func logExpensiveQuery(stmtNode ast.StmtNode, finalPlan plannercore.Plan) (expen
7576
if len(sql) > logSQLLen {
7677
sql = fmt.Sprintf("%s len(%d)", sql[:logSQLLen], len(sql))
7778
}
78-
log.Warnf("[EXPENSIVE_QUERY] %s", sql)
79+
logutil.Logger(context.Background()).Warn("EXPENSIVE_QUERY", zap.String("SQL", sql))
7980
return
8081
}
8182

@@ -195,7 +196,7 @@ func GetInfoSchema(ctx sessionctx.Context) infoschema.InfoSchema {
195196
var is infoschema.InfoSchema
196197
if snap := sessVar.SnapshotInfoschema; snap != nil {
197198
is = snap.(infoschema.InfoSchema)
198-
log.Infof("con:%d use snapshot schema %d", sessVar.ConnectionID, is.SchemaMetaVersion())
199+
logutil.Logger(context.Background()).Info("use snapshot schema", zap.Uint64("conn", sessVar.ConnectionID), zap.Int64("schemaVersion", is.SchemaMetaVersion()))
199200
} else {
200201
is = sessVar.TxnCtx.InfoSchema.(infoschema.InfoSchema)
201202
}

executor/ddl.go

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,9 @@ import (
2727
"github.com/pingcap/tidb/sessionctx/variable"
2828
"github.com/pingcap/tidb/types"
2929
"github.com/pingcap/tidb/util/chunk"
30+
"github.com/pingcap/tidb/util/logutil"
3031
"github.com/pingcap/tidb/util/sqlexec"
31-
log "github.com/sirupsen/logrus"
32+
"go.uber.org/zap"
3233
"golang.org/x/net/context"
3334
)
3435

@@ -56,7 +57,7 @@ func (e *DDLExec) toErr(err error) error {
5657
checker := domain.NewSchemaChecker(dom, e.is.SchemaMetaVersion(), nil)
5758
txn, err1 := e.ctx.Txn(true)
5859
if err1 != nil {
59-
log.Error(err)
60+
logutil.Logger(context.Background()).Error("active txn failed", zap.Error(err))
6061
return errors.Trace(err1)
6162
}
6263
schemaInfoErr := checker.Check(txn.StartTS())
@@ -241,7 +242,10 @@ func (e *DDLExec) executeDropTable(s *ast.DropTableStmt) error {
241242
}
242243

243244
if config.CheckTableBeforeDrop {
244-
log.Warnf("admin check table `%s`.`%s` before drop.", fullti.Schema.O, fullti.Name.O)
245+
logutil.Logger(context.Background()).Warn("admin check table before drop",
246+
zap.String("database", fullti.Schema.O),
247+
zap.String("table", fullti.Name.O),
248+
)
245249
sql := fmt.Sprintf("admin check table `%s`.`%s`", fullti.Schema.O, fullti.Name.O)
246250
_, _, err = e.ctx.(sqlexec.RestrictedSQLExecutor).ExecRestrictedSQL(e.ctx, sql)
247251
if err != nil {

executor/distsql.go

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -36,10 +36,11 @@ import (
3636
"github.com/pingcap/tidb/table"
3737
"github.com/pingcap/tidb/types"
3838
"github.com/pingcap/tidb/util/chunk"
39+
"github.com/pingcap/tidb/util/logutil"
3940
"github.com/pingcap/tidb/util/memory"
4041
"github.com/pingcap/tidb/util/ranger"
4142
"github.com/pingcap/tipb/go-tipb"
42-
log "github.com/sirupsen/logrus"
43+
"go.uber.org/zap"
4344
"golang.org/x/net/context"
4445
)
4546

@@ -445,7 +446,7 @@ func (e *IndexLookUpExecutor) startIndexWorker(ctx context.Context, kvRanges []k
445446
e.ctx.StoreQueryFeedback(e.feedback)
446447
cancel()
447448
if err := result.Close(); err != nil {
448-
log.Error("close Select result failed:", errors.ErrorStack(err))
449+
logutil.Logger(ctx).Error("close Select result failed", zap.Error(err))
449450
}
450451
close(workCh)
451452
close(e.resultCh)
@@ -494,7 +495,7 @@ func (e *IndexLookUpExecutor) buildTableReader(ctx context.Context, handles []in
494495
tableReaderExec.runtimeStats = nil
495496
tableReader, err := e.dataReaderBuilder.buildTableReaderFromHandles(ctx, tableReaderExec, handles)
496497
if err != nil {
497-
log.Error(err)
498+
logutil.Logger(ctx).Error("build table reader from handles failed", zap.Error(err))
498499
return nil, errors.Trace(err)
499500
}
500501
return tableReader, nil
@@ -586,7 +587,7 @@ func (w *indexWorker) fetchHandles(ctx context.Context, result distsql.SelectRes
586587
buf := make([]byte, 4096)
587588
stackSize := runtime.Stack(buf, false)
588589
buf = buf[:stackSize]
589-
log.Errorf("indexWorker panic stack is:\n%s", buf)
590+
logutil.Logger(ctx).Error("indexWorker in IndexLookupExecutor panicked", zap.String("stack", string(buf)))
590591
err4Panic := errors.Errorf("%v", r)
591592
doneCh := make(chan error, 1)
592593
doneCh <- err4Panic
@@ -686,7 +687,7 @@ func (w *tableWorker) pickAndExecTask(ctx context.Context) {
686687
buf := make([]byte, 4096)
687688
stackSize := runtime.Stack(buf, false)
688689
buf = buf[:stackSize]
689-
log.Errorf("tableWorker panic stack is:\n%s", buf)
690+
logutil.Logger(ctx).Error("tableWorker in IndexLookUpExecutor panicked", zap.String("stack", string(buf)))
690691
task.doneCh <- errors.Errorf("%v", r)
691692
}
692693
}()
@@ -712,7 +713,7 @@ func (w *tableWorker) pickAndExecTask(ctx context.Context) {
712713
func (w *tableWorker) executeTask(ctx context.Context, task *lookupTableTask) error {
713714
tableReader, err := w.buildTblReader(ctx, task.handles)
714715
if err != nil {
715-
log.Error(err)
716+
logutil.Logger(ctx).Error("build table reader failed", zap.Error(err))
716717
return errors.Trace(err)
717718
}
718719
defer terror.Call(tableReader.Close)
@@ -727,7 +728,7 @@ func (w *tableWorker) executeTask(ctx context.Context, task *lookupTableTask) er
727728
chk := tableReader.newFirstChunk()
728729
err = tableReader.Next(ctx, chk)
729730
if err != nil {
730-
log.Error(err)
731+
logutil.Logger(ctx).Error("table reader fetch next chunk failed", zap.Error(err))
731732
return errors.Trace(err)
732733
}
733734
if chk.NumRows() == 0 {

executor/executor.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -42,8 +42,9 @@ import (
4242
"github.com/pingcap/tidb/util/admin"
4343
"github.com/pingcap/tidb/util/chunk"
4444
"github.com/pingcap/tidb/util/execdetails"
45+
"github.com/pingcap/tidb/util/logutil"
4546
"github.com/pingcap/tidb/util/memory"
46-
log "github.com/sirupsen/logrus"
47+
"go.uber.org/zap"
4748
)
4849

4950
var (
@@ -448,7 +449,7 @@ func (e *CheckTableExec) Next(ctx context.Context, chk *chunk.Chunk) error {
448449
err = e.doCheckTable(tb)
449450
}
450451
if err != nil {
451-
log.Warnf("%v error:%v", t.Name, errors.ErrorStack(err))
452+
logutil.Logger(ctx).Warn("check table failed", zap.String("tableName", t.Name.O), zap.Error(err))
452453
if admin.ErrDataInConsistent.Equal(err) {
453454
return ErrAdminCheckTable.GenWithStack("%v err:%v", t.Name, err)
454455
}
@@ -1177,7 +1178,7 @@ func (e *UnionExec) resultPuller(ctx context.Context, childID int) {
11771178
buf := make([]byte, 4096)
11781179
stackSize := runtime.Stack(buf, false)
11791180
buf = buf[:stackSize]
1180-
log.Errorf("resultPuller panic stack is:\n%s", buf)
1181+
logutil.Logger(ctx).Error("resultPuller panicked", zap.String("stack", string(buf)))
11811182
result.err = errors.Errorf("%v", r)
11821183
e.resultPool <- result
11831184
e.stopFetchData.Store(true)

executor/index_lookup_join.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,10 +30,11 @@ import (
3030
"github.com/pingcap/tidb/types"
3131
"github.com/pingcap/tidb/util/chunk"
3232
"github.com/pingcap/tidb/util/codec"
33+
"github.com/pingcap/tidb/util/logutil"
3334
"github.com/pingcap/tidb/util/memory"
3435
"github.com/pingcap/tidb/util/mvmap"
3536
"github.com/pingcap/tidb/util/ranger"
36-
log "github.com/sirupsen/logrus"
37+
"go.uber.org/zap"
3738
"golang.org/x/net/context"
3839
)
3940

@@ -305,7 +306,7 @@ func (ow *outerWorker) run(ctx context.Context, wg *sync.WaitGroup) {
305306
buf := make([]byte, 4096)
306307
stackSize := runtime.Stack(buf, false)
307308
buf = buf[:stackSize]
308-
log.Errorf("outerWorker panic stack is:\n%s", buf)
309+
logutil.Logger(ctx).Error("outerWorker panicked", zap.String("stack", string(buf)))
309310
task := &lookUpJoinTask{doneCh: make(chan error, 1)}
310311
task.doneCh <- errors.Errorf("%v", r)
311312
ow.pushToChan(ctx, task, ow.resultCh)
@@ -407,7 +408,7 @@ func (iw *innerWorker) run(ctx context.Context, wg *sync.WaitGroup) {
407408
buf := make([]byte, 4096)
408409
stackSize := runtime.Stack(buf, false)
409410
buf = buf[:stackSize]
410-
log.Errorf("innerWorker panic stack is:\n%s", buf)
411+
logutil.Logger(ctx).Error("innerWorker panicked", zap.String("stack", string(buf)))
411412
// "task != nil" is guaranteed when panic happened.
412413
task.doneCh <- errors.Errorf("%v", r)
413414
}

0 commit comments

Comments
 (0)