Repository: pingcap/tidb
File: tests/realtikvtest/addindextest3/temp_index_test.go (Go)

Code before:
		runInsert := false
		testfailpoint.EnableCall(t, "github.com/pingcap/tidb/pkg/ddl/ingest/beforeBackendIngest", func() {
			if !runInsert {
				tk2 := testkit.NewTestKit(t, store)
				tk2.MustExec("use test")
				for _, data := range tc.incrOp {
					tk2.MustExec(data)
				}
				runInsert = true
			}
		})
		if tc.expectedErr != "" {
			tk.MustGetErrMsg(tc.createIndex, tc.expectedErr)
		} else {
			tk.MustExec(tc.createIndex)
			tk.MustExec(tc.adminCheck)
		}

		tkBuilder := ddl.NewTaskKeyBuilder()
		taskKey := tkBuilder.Build(jobID)

		require.True(t, runInsert, "%s: never ran incrOp: %s", tc.name, tc.incrOp[0])
		query := fmt.Sprintf(`select id from all_global_tasks where task_key like '%s' order by id`, fmt.Sprintf("%%%s%%", taskKey))
		t.Log(query)
		taskIDRows := tk.MustQuery(query).Rows()
		require.Len(t, taskIDRows, 2)
		taskID := taskIDRows[0][0].(string)
		mergeTaskID := taskIDRows[1][0].(string)
		readIdxCntSQL := fmt.Sprintf("select json_extract(summary, '$.row_count') from all_subtasks where task_key = %s and step = 1", taskID)
		tk.MustQuery(readIdxCntSQL).Check(testkit.Rows(tc.readIdxRowCnt...))
		mergeCntSQL := fmt.Sprintf("select json_extract(summary, '$.row_count') from all_subtasks where task_key = %s and step = 4", mergeTaskID)
		tk.MustQuery(mergeCntSQL).Check(testkit.Rows(tc.mergeIdxCnt...))
	}
}


Review comment (suggestion):
This assertion will panic with an index out of bounds error if `tc.incrOp` is an empty slice. Before accessing `tc.incrOp[0]`, add a guard to check that the slice is not empty, or adjust the error message to avoid indexing.
```suggestion
		var incrOpMsg string
		if len(tc.incrOp) > 0 {
			incrOpMsg = tc.incrOp[0]
		} else {
			incrOpMsg = "<empty incrOp>"
		}
		require.True(t, runInsert, "%s: never ran incrOp: %s", tc.name, incrOpMsg)
```

Code after:
				job.Type == model.ActionMultiSchemaChange {
				jobID = job.ID
			}
		})
		runInsert := false
		testfailpoint.EnableCall(t, "github.com/pingcap/tidb/pkg/ddl/ingest/beforeBackendIngest", func() {
			if !runInsert {
				tk2 := testkit.NewTestKit(t, store)
				tk2.MustExec("use test")
				for _, data := range tc.incrOp {
					tk2.MustExec(data)
				}
				runInsert = true
			}
		})
		if tc.expectedErr != "" {
			tk.MustGetErrMsg(tc.createIndex, tc.expectedErr)
		} else {
			tk.MustExec(tc.createIndex)
			tk.MustExec(tc.adminCheck)
		}

		tkBuilder := ddl.NewTaskKeyBuilder()
		taskKey := tkBuilder.Build(jobID)

		require.True(t, runInsert, "%s: never ran incrOp: %s", tc.name, tc.incrOp[0])
		query := fmt.Sprintf(`select id from all_global_tasks where task_key like '%s' order by id`, fmt.Sprintf("%%%s%%", taskKey))
		t.Log(query)
		taskIDRows := tk.MustQuery(query).Rows()
		require.Len(t, taskIDRows, 2)
		taskID := taskIDRows[0][0].(string)
		mergeTaskID := taskIDRows[1][0].(string)
		readIdxCntSQL := fmt.Sprintf("select json_extract(summary, '$.row_count') from all_subtasks where task_key = %s and step = 1", taskID)
		tk.MustQuery(readIdxCntSQL).Check(testkit.Rows(tc.readIdxRowCnt...))
		mergeCntSQL := fmt.Sprintf("select json_extract(summary, '$.row_count') from all_subtasks where task_key = %s and step = 4", mergeTaskID)
		tk.MustQuery(mergeCntSQL).Check(testkit.Rows(tc.mergeIdxCnt...))
	}
}

func TestMergeTempIndexStuck(t *testing.T) {
	store := realtikvtest.CreateMockStoreAndSetup(t)
	tk := testkit.NewTestKit(t, store)
	tk.MustExec("use test")
	tk.MustExec("drop table if exists t")
	tk.MustExec("create table t(id int primary key, a bigint)")
	var (
		workerNum = 5
		batchNum  = 10
		pkBegin   = 0
		pkEnd     = 50 // workerNum * batchNum = pkEnd - pkBegin
		rowNum    = 100