Skip to content

Commit

Permalink
session: use the uniform log format for session (#9517) (#9990)
Browse files Browse the repository at this point in the history
  • Loading branch information
jackysp authored and zz-jason committed Apr 2, 2019
1 parent 43cf3c9 commit 46d5d0e
Show file tree
Hide file tree
Showing 6 changed files with 142 additions and 77 deletions.
18 changes: 11 additions & 7 deletions session/bench_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,18 @@ package session
import (
"fmt"
"math/rand"
"strconv"
"testing"
"time"

"github.com/pingcap/log"
"github.com/pingcap/tidb/domain"
"github.com/pingcap/tidb/kv"
"github.com/pingcap/tidb/store/mockstore"
"github.com/pingcap/tidb/util/logutil"
"github.com/pingcap/tidb/util/sqlexec"
log "github.com/sirupsen/logrus"
"go.uber.org/zap"
"go.uber.org/zap/zapcore"
"golang.org/x/net/context"
)

Expand All @@ -33,16 +37,16 @@ var bigCount = 10000
func prepareBenchSession() (Session, *domain.Domain, kv.Storage) {
store, err := mockstore.NewMockTikvStore()
if err != nil {
log.Fatal(err)
logutil.Logger(context.Background()).Fatal(err.Error())
}
domain, err := BootstrapSession(store)
if err != nil {
log.Fatal(err)
logutil.Logger(context.Background()).Fatal(err.Error())
}
log.SetLevel(log.ErrorLevel)
log.SetLevel(zapcore.ErrorLevel)
se, err := CreateSession4Test(store)
if err != nil {
log.Fatal(err)
logutil.Logger(context.Background()).Fatal(err.Error())
}
mustExecute(se, "use test")
return se, domain, store
Expand Down Expand Up @@ -88,10 +92,10 @@ func readResult(ctx context.Context, rs sqlexec.RecordSet, count int) {
for count > 0 {
err := rs.Next(ctx, chk)
if err != nil {
log.Fatal(err)
logutil.Logger(ctx).Fatal("read result failed", zap.Error(err))
}
if chk.NumRows() == 0 {
log.Fatal(count)
logutil.Logger(ctx).Fatal(strconv.Itoa(count))
}
count -= chk.NumRows()
}
Expand Down
34 changes: 20 additions & 14 deletions session/bootstrap.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,9 @@ import (
"github.com/pingcap/tidb/infoschema"
"github.com/pingcap/tidb/sessionctx/variable"
"github.com/pingcap/tidb/util/chunk"
"github.com/pingcap/tidb/util/logutil"
"github.com/pingcap/tidb/util/timeutil"
log "github.com/sirupsen/logrus"
"go.uber.org/zap"
"golang.org/x/net/context"
)

Expand Down Expand Up @@ -215,7 +216,8 @@ const (
func bootstrap(s Session) {
b, err := checkBootstrapped(s)
if err != nil {
log.Fatal(err)
logutil.Logger(context.Background()).Fatal("check bootstrap error",
zap.Error(err))
}
if b {
upgrade(s)
Expand Down Expand Up @@ -268,7 +270,8 @@ func checkBootstrapped(s Session) (bool, error) {
// Check if system db exists.
_, err := s.Execute(context.Background(), fmt.Sprintf("USE %s;", mysql.SystemDB))
if err != nil && infoschema.ErrDatabaseNotExists.NotEqual(err) {
log.Fatal(err)
logutil.Logger(context.Background()).Fatal("check bootstrap error",
zap.Error(err))
}
// Check bootstrapped variable value in TiDB table.
sVal, _, err := getTiDBVar(s, bootstrappedVar)
Expand Down Expand Up @@ -424,14 +427,17 @@ func upgrade(s Session) {
// Check if TiDB is already upgraded.
v, err1 := getBootstrapVersion(s)
if err1 != nil {
log.Fatal(err1)
logutil.Logger(context.Background()).Fatal("upgrade error",
zap.Error(err1))
}
if v >= currentBootstrapVersion {
// It is already bootstrapped/upgraded by a higher version TiDB server.
return
}
log.Errorf("[Upgrade] upgrade from %d to %d error", ver, currentBootstrapVersion)
log.Fatal(err)
logutil.Logger(context.Background()).Fatal("[Upgrade] upgrade error",
zap.Int64("from", ver),
zap.Int("to", currentBootstrapVersion),
zap.Error(err))
}
}

Expand Down Expand Up @@ -503,7 +509,7 @@ func doReentrantDDL(s Session, sql string, ignorableErrs ...error) {
}
}
if err != nil {
log.Fatal(err)
logutil.Logger(context.Background()).Fatal("doReentrantDDL error", zap.Error(err))
}
}

Expand All @@ -521,7 +527,7 @@ func upgradeToVer11(s Session) {
if terror.ErrorEqual(err, infoschema.ErrColumnExists) {
return
}
log.Fatal(err)
logutil.Logger(context.Background()).Fatal("upgradeToVer11 error", zap.Error(err))
}
mustExecute(s, "UPDATE HIGH_PRIORITY mysql.user SET References_priv='Y'")
}
Expand Down Expand Up @@ -582,7 +588,7 @@ func upgradeToVer13(s Session) {
if terror.ErrorEqual(err, infoschema.ErrColumnExists) {
continue
}
log.Fatal(err)
logutil.Logger(context.Background()).Fatal("upgradeToVer13 error", zap.Error(err))
}
}
mustExecute(s, "UPDATE HIGH_PRIORITY mysql.user SET Create_tmp_table_priv='Y',Lock_tables_priv='Y',Create_view_priv='Y',Show_view_priv='Y',Create_routine_priv='Y',Alter_routine_priv='Y',Event_priv='Y'")
Expand All @@ -607,7 +613,7 @@ func upgradeToVer14(s Session) {
if terror.ErrorEqual(err, infoschema.ErrColumnExists) {
continue
}
log.Fatal(err)
logutil.Logger(context.Background()).Fatal("upgradeToVer14 error", zap.Error(err))
}
}
}
Expand All @@ -616,7 +622,7 @@ func upgradeToVer15(s Session) {
var err error
_, err = s.Execute(context.Background(), CreateGCDeleteRangeTable)
if err != nil {
log.Fatal(err)
logutil.Logger(context.Background()).Fatal("upgradeToVer15 error", zap.Error(err))
}
}

Expand Down Expand Up @@ -760,20 +766,20 @@ func doDMLWorks(s Session) {
// Check if TiDB is already bootstrapped.
b, err1 := checkBootstrapped(s)
if err1 != nil {
log.Fatal(err1)
logutil.Logger(context.Background()).Fatal("doDMLWorks error", zap.Error(err1))
}
if b {
return
}
log.Fatal(err)
logutil.Logger(context.Background()).Fatal("doDMLWorks error", zap.Error(err))
}
}

func mustExecute(s Session, sql string) {
_, err := s.Execute(context.Background(), sql)
if err != nil {
debug.PrintStack()
log.Fatal(err)
logutil.Logger(context.Background()).Fatal("mustExecute error", zap.Error(err))
}
}

Expand Down
Loading

0 comments on commit 46d5d0e

Please sign in to comment.