Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

lightning: fix lightning failed to log encoding error #45241

Merged
merged 14 commits into from
Jul 14, 2023
Merged
Show file tree
Hide file tree
Changes from 12 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion br/pkg/lightning/backend/kv/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ go_test(
name = "kv_test",
timeout = "short",
srcs = [
"base_test.go",
"kv2sql_test.go",
"session_internal_test.go",
"session_test.go",
Expand All @@ -56,7 +57,7 @@ go_test(
embed = [":kv"],
flaky = True,
race = "on",
shard_count = 18,
shard_count = 19,
deps = [
"//br/pkg/lightning/backend/encode",
"//br/pkg/lightning/common",
Expand Down
22 changes: 19 additions & 3 deletions br/pkg/lightning/backend/kv/base.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ var kindStr = [...]string{

// MarshalLogArray implements the zapcore.ArrayMarshaler interface
func (row RowArrayMarshaller) MarshalLogArray(encoder zapcore.ArrayEncoder) error {
var totalLength = 0
for _, datum := range row {
kind := datum.Kind()
var str string
Expand All @@ -94,6 +95,14 @@ func (row RowArrayMarshaller) MarshalLogArray(encoder zapcore.ArrayEncoder) erro
return err
}
}
if len(str) > 512*1024 {
D3Hunter marked this conversation as resolved.
Show resolved Hide resolved
str = str[0:1024] + " (truncated)"
}
totalLength += len(str)
if totalLength >= 512*1024 {
encoder.AppendString("The row has been truncated, and the log has exited early.")
return nil
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We can also log some information, to let user know we have exited early.

Maybe by encoder.AppendString()

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

modified

}
if err := encoder.AppendObject(zapcore.ObjectMarshalerFunc(func(enc zapcore.ObjectEncoder) error {
enc.AddString("kind", kindStr[kind])
enc.AddString("val", redact.String(str))
Expand Down Expand Up @@ -307,9 +316,16 @@ func (e *BaseKVEncoder) LogKVConvertFailed(row []types.Datum, j int, colInfo *mo
log.ShortError(err),
)

e.logger.Error("failed to convert kv value", logutil.RedactAny("origVal", original.GetValue()),
zap.Stringer("fieldType", &colInfo.FieldType), zap.String("column", colInfo.Name.O),
zap.Int("columnID", j+1))
if len(original.GetString()) >= 512*1024 {
originalPrefix := original.GetString()[0:1024] + " (truncated)"
e.logger.Error("failed to convert kv value", logutil.RedactAny("origVal", originalPrefix),
zap.Stringer("fieldType", &colInfo.FieldType), zap.String("column", colInfo.Name.O),
zap.Int("columnID", j+1))
} else {
e.logger.Error("failed to convert kv value", logutil.RedactAny("origVal", original.GetValue()),
zap.Stringer("fieldType", &colInfo.FieldType), zap.String("column", colInfo.Name.O),
zap.Int("columnID", j+1))
}
return errors.Annotatef(
err,
"failed to cast value as %s for column `%s` (#%d)", &colInfo.FieldType, colInfo.Name.O, j+1,
Expand Down
75 changes: 75 additions & 0 deletions br/pkg/lightning/backend/kv/base_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
// Copyright 2023 PingCAP, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package kv

import (
"os"
"path/filepath"
"strings"
"testing"

"github.com/pingcap/tidb/br/pkg/lightning/backend/encode"
"github.com/pingcap/tidb/br/pkg/lightning/log"
"github.com/pingcap/tidb/parser/model"
"github.com/pingcap/tidb/parser/mysql"
"github.com/pingcap/tidb/table"
"github.com/pingcap/tidb/table/tables"
"github.com/pingcap/tidb/types"
"github.com/stretchr/testify/require"
)

func TestLogKVConvertFailed(t *testing.T) {
tempPath := filepath.Join(t.TempDir(), "/temp.txt")
logCfg := &log.Config{File: tempPath, FileMaxSize: 1}
err := log.InitLogger(logCfg, "info")
require.NoError(t, err)

modelName := model.NewCIStr("c1")
modelState := model.StatePublic
modelFieldType := *types.NewFieldType(mysql.TypeTiny)
c1 := &model.ColumnInfo{ID: 1, Name: modelName, State: modelState, Offset: 0, FieldType: modelFieldType}
cols := []*model.ColumnInfo{c1}
tblInfo := &model.TableInfo{ID: 1, Columns: cols, PKIsHandle: false, State: model.StatePublic}
var tbl table.Table
tbl, err = tables.TableFromMeta(NewPanickingAllocators(0), tblInfo)
require.NoError(t, err)

var baseKVEncoder *BaseKVEncoder
baseKVEncoder, err = NewBaseKVEncoder(&encode.EncodingConfig{
Table: tbl,
SessionOptions: encode.SessionOptions{
SQLMode: mysql.ModeStrictAllTables,
Timestamp: 1234567890,
},
Logger: log.L(),
})
var newString strings.Builder
for i := 0; i < 100000; i++ {
newString.WriteString("test_test_test_test_")
}
newDatum := types.NewStringDatum(newString.String())
rows := []types.Datum{}
for i := 0; i <= 10; i++ {
rows = append(rows, newDatum)
}
err = baseKVEncoder.LogKVConvertFailed(rows, 6, c1, err)
require.NoError(t, err)

var content []byte
content, err = os.ReadFile(tempPath)
require.NoError(t, err)
require.LessOrEqual(t, 500, len(string(content)))
lance6716 marked this conversation as resolved.
Show resolved Hide resolved
require.NotContains(t, content, "exceeds maximum file size")
}