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

show import: fix show import might read data from prev line #44724

Merged
merged 3 commits into from
Jun 16, 2023
Merged
Show file tree
Hide file tree
Changes from all 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
2 changes: 1 addition & 1 deletion executor/importer/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ go_test(
embed = [":importer"],
flaky = True,
race = "on",
shard_count = 10,
shard_count = 11,
deps = [
"//br/pkg/errors",
"//br/pkg/lightning/config",
Expand Down
26 changes: 22 additions & 4 deletions executor/importer/job.go
Original file line number Diff line number Diff line change
Expand Up @@ -258,24 +258,42 @@ func FailJob(ctx context.Context, conn sqlexec.SQLExecutor, jobID int64, errorMs
}

func convert2JobInfo(row chunk.Row) (*JobInfo, error) {
// start_time, end_time, summary, error_message can be NULL, need to use row.IsNull() to check.
startTime, endTime := types.ZeroTime, types.ZeroTime
if !row.IsNull(2) {
startTime = row.GetTime(2)
}
if !row.IsNull(3) {
endTime = row.GetTime(3)
}

parameters := ImportParameters{}
parametersStr := row.GetString(8)
if err := json.Unmarshal([]byte(parametersStr), &parameters); err != nil {
return nil, errors.Trace(err)
}

var summary *JobSummary
summaryStr := row.GetString(12)
var summaryStr string
if !row.IsNull(12) {
summaryStr = row.GetString(12)
}
if len(summaryStr) > 0 {
summary = &JobSummary{}
if err := json.Unmarshal([]byte(summaryStr), summary); err != nil {
return nil, errors.Trace(err)
}
}

var errMsg string
if !row.IsNull(13) {
errMsg = row.GetString(13)
}
return &JobInfo{
ID: row.GetInt64(0),
CreateTime: row.GetTime(1),
StartTime: row.GetTime(2),
EndTime: row.GetTime(3),
StartTime: startTime,
EndTime: endTime,
TableSchema: row.GetString(4),
TableName: row.GetString(5),
TableID: row.GetInt64(6),
Expand All @@ -285,7 +303,7 @@ func convert2JobInfo(row chunk.Row) (*JobInfo, error) {
Status: row.GetString(10),
Step: row.GetString(11),
Summary: summary,
ErrorMessage: row.GetString(13),
ErrorMessage: errMsg,
}, nil
}

Expand Down
53 changes: 53 additions & 0 deletions executor/importer/job_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -279,3 +279,56 @@ func TestJobInfo_CanCancel(t *testing.T) {
require.Equal(t, c.canCancel, jobInfo.CanCancel(), c.status)
}
}

func TestGetJobInfoNullField(t *testing.T) {
store := testkit.CreateMockStore(t)
tk := testkit.NewTestKit(t, store)
ctx := context.Background()
conn := tk.Session().(sqlexec.SQLExecutor)
jobInfo := &importer.JobInfo{
TableSchema: "test",
TableName: "t",
TableID: 1,
CreatedBy: "user-for-test@%",
Parameters: importer.ImportParameters{
ColumnsAndVars: "(a, b, c)",
SetClause: "d = 1",
Format: importer.DataFormatCSV,
Options: map[string]interface{}{
"skip_rows": float64(1), // json unmarshal will convert number to float64
"detached": nil,
},
},
SourceFileSize: 123,
Status: "pending",
}
// create jobs
jobID1, err := importer.CreateJob(ctx, conn, jobInfo.TableSchema, jobInfo.TableName, jobInfo.TableID,
jobInfo.CreatedBy, &jobInfo.Parameters, jobInfo.SourceFileSize)
require.NoError(t, err)
require.NoError(t, importer.StartJob(ctx, conn, jobID1))
require.NoError(t, importer.FailJob(ctx, conn, jobID1, "failed"))
jobID2, err := importer.CreateJob(ctx, conn, jobInfo.TableSchema, jobInfo.TableName, jobInfo.TableID,
jobInfo.CreatedBy, &jobInfo.Parameters, jobInfo.SourceFileSize)
require.NoError(t, err)
gotJobInfos, err := importer.GetAllViewableJobs(ctx, conn, "", true)
require.NoError(t, err)
require.Len(t, gotJobInfos, 2)
// result should be in order, jobID1, jobID2
jobInfo.ID = jobID1
jobInfo.Status = "failed"
jobInfo.Step = importer.JobStepImporting
jobInfo.ErrorMessage = "failed"
jobInfoEqual(t, jobInfo, gotJobInfos[0])
require.False(t, gotJobInfos[0].StartTime.IsZero())
require.False(t, gotJobInfos[0].EndTime.IsZero())
jobInfo.ID = jobID2
jobInfo.Status = "pending"
jobInfo.Step = ""
// err msg of jobID2 should be empty
jobInfo.ErrorMessage = ""
jobInfoEqual(t, jobInfo, gotJobInfos[1])
// start/end time of jobID2 should be zero
require.True(t, gotJobInfos[1].StartTime.IsZero())
require.True(t, gotJobInfos[1].EndTime.IsZero())
}