Skip to content

Commit 1864dec

Browse files
lyzx2001ti-chi-bot
authored andcommitted
This is an automated cherry-pick of pingcap#45241
Signed-off-by: ti-chi-bot <ti-community-prow-bot@tidb.io>
1 parent 121c32b commit 1864dec

File tree

3 files changed

+107
-3
lines changed

3 files changed

+107
-3
lines changed

br/pkg/lightning/backend/kv/BUILD.bazel

+9
Original file line numberDiff line numberDiff line change
@@ -48,14 +48,23 @@ go_test(
4848
name = "kv_test",
4949
timeout = "short",
5050
srcs = [
51+
<<<<<<< HEAD
52+
=======
53+
"base_test.go",
54+
"kv2sql_test.go",
55+
>>>>>>> de99f81e580 (lightning: fix lightning failed to log encoding error (#45241))
5156
"session_internal_test.go",
5257
"session_test.go",
5358
"sql2kv_test.go",
5459
],
5560
embed = [":kv"],
5661
flaky = True,
5762
race = "on",
63+
<<<<<<< HEAD
5864
shard_count = 16,
65+
=======
66+
shard_count = 19,
67+
>>>>>>> de99f81e580 (lightning: fix lightning failed to log encoding error (#45241))
5968
deps = [
6069
"//br/pkg/lightning/backend/encode",
6170
"//br/pkg/lightning/common",

br/pkg/lightning/backend/kv/base.go

+23-3
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,10 @@ import (
3737
"go.uber.org/zap/zapcore"
3838
)
3939

40+
const (
41+
maxLogLength = 512 * 1024
42+
)
43+
4044
// ExtraHandleColumnInfo is the column info of extra handle column.
4145
var ExtraHandleColumnInfo = model.NewExtraHandleColInfo()
4246

@@ -77,6 +81,7 @@ var kindStr = [...]string{
7781

7882
// MarshalLogArray implements the zapcore.ArrayMarshaler interface
7983
func (row RowArrayMarshaller) MarshalLogArray(encoder zapcore.ArrayEncoder) error {
84+
var totalLength = 0
8085
for _, datum := range row {
8186
kind := datum.Kind()
8287
var str string
@@ -94,6 +99,14 @@ func (row RowArrayMarshaller) MarshalLogArray(encoder zapcore.ArrayEncoder) erro
9499
return err
95100
}
96101
}
102+
if len(str) > maxLogLength {
103+
str = str[0:1024] + " (truncated)"
104+
}
105+
totalLength += len(str)
106+
if totalLength >= maxLogLength {
107+
encoder.AppendString("The row has been truncated, and the log has exited early.")
108+
return nil
109+
}
97110
if err := encoder.AppendObject(zapcore.ObjectMarshalerFunc(func(enc zapcore.ObjectEncoder) error {
98111
enc.AddString("kind", kindStr[kind])
99112
enc.AddString("val", redact.String(str))
@@ -307,9 +320,16 @@ func (e *BaseKVEncoder) LogKVConvertFailed(row []types.Datum, j int, colInfo *mo
307320
log.ShortError(err),
308321
)
309322

310-
e.logger.Error("failed to convert kv value", logutil.RedactAny("origVal", original.GetValue()),
311-
zap.Stringer("fieldType", &colInfo.FieldType), zap.String("column", colInfo.Name.O),
312-
zap.Int("columnID", j+1))
323+
if len(original.GetString()) >= maxLogLength {
324+
originalPrefix := original.GetString()[0:1024] + " (truncated)"
325+
e.logger.Error("failed to convert kv value", logutil.RedactAny("origVal", originalPrefix),
326+
zap.Stringer("fieldType", &colInfo.FieldType), zap.String("column", colInfo.Name.O),
327+
zap.Int("columnID", j+1))
328+
} else {
329+
e.logger.Error("failed to convert kv value", logutil.RedactAny("origVal", original.GetValue()),
330+
zap.Stringer("fieldType", &colInfo.FieldType), zap.String("column", colInfo.Name.O),
331+
zap.Int("columnID", j+1))
332+
}
313333
return errors.Annotatef(
314334
err,
315335
"failed to cast value as %s for column `%s` (#%d)", &colInfo.FieldType, colInfo.Name.O, j+1,
+75
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
// Copyright 2023 PingCAP, Inc.
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
package kv
16+
17+
import (
18+
"os"
19+
"path/filepath"
20+
"strings"
21+
"testing"
22+
23+
"github.com/pingcap/tidb/br/pkg/lightning/backend/encode"
24+
"github.com/pingcap/tidb/br/pkg/lightning/log"
25+
"github.com/pingcap/tidb/parser/model"
26+
"github.com/pingcap/tidb/parser/mysql"
27+
"github.com/pingcap/tidb/table"
28+
"github.com/pingcap/tidb/table/tables"
29+
"github.com/pingcap/tidb/types"
30+
"github.com/stretchr/testify/require"
31+
)
32+
33+
func TestLogKVConvertFailed(t *testing.T) {
34+
tempPath := filepath.Join(t.TempDir(), "/temp.txt")
35+
logCfg := &log.Config{File: tempPath, FileMaxSize: 1}
36+
err := log.InitLogger(logCfg, "info")
37+
require.NoError(t, err)
38+
39+
modelName := model.NewCIStr("c1")
40+
modelState := model.StatePublic
41+
modelFieldType := *types.NewFieldType(mysql.TypeTiny)
42+
c1 := &model.ColumnInfo{ID: 1, Name: modelName, State: modelState, Offset: 0, FieldType: modelFieldType}
43+
cols := []*model.ColumnInfo{c1}
44+
tblInfo := &model.TableInfo{ID: 1, Columns: cols, PKIsHandle: false, State: model.StatePublic}
45+
var tbl table.Table
46+
tbl, err = tables.TableFromMeta(NewPanickingAllocators(0), tblInfo)
47+
require.NoError(t, err)
48+
49+
var baseKVEncoder *BaseKVEncoder
50+
baseKVEncoder, err = NewBaseKVEncoder(&encode.EncodingConfig{
51+
Table: tbl,
52+
SessionOptions: encode.SessionOptions{
53+
SQLMode: mysql.ModeStrictAllTables,
54+
Timestamp: 1234567890,
55+
},
56+
Logger: log.L(),
57+
})
58+
var newString strings.Builder
59+
for i := 0; i < 100000; i++ {
60+
newString.WriteString("test_test_test_test_")
61+
}
62+
newDatum := types.NewStringDatum(newString.String())
63+
rows := []types.Datum{}
64+
for i := 0; i <= 10; i++ {
65+
rows = append(rows, newDatum)
66+
}
67+
err = baseKVEncoder.LogKVConvertFailed(rows, 6, c1, err)
68+
require.NoError(t, err)
69+
70+
var content []byte
71+
content, err = os.ReadFile(tempPath)
72+
require.NoError(t, err)
73+
require.LessOrEqual(t, 500, len(string(content)))
74+
require.NotContains(t, content, "exceeds maximum file size")
75+
}

0 commit comments

Comments
 (0)