Repository: pingcap/tidb
File: pkg/domain/plan_replayer_slow_log_test.go (Go)

Code before:
	tk.MustExec("set global tidb_enable_stmt_summary = 1")
	tk.MustExec("set global tidb_stmt_summary_refresh_interval = 3600")
	tk.MustExec("set global tidb_stmt_summary_internal_query = 0")

	// Setup test data
	tk.MustExec("delete from mysql.plan_replayer_task")
	tk.MustExec("insert into mysql.plan_replayer_task (sql_digest, plan_digest) values ('test_digest', 'test_plan')")

	// Trigger plan replayer collection
	prHandle := dom.GetPlanReplayerHandle()
	err = prHandle.CollectPlanReplayerTask()
	require.NoError(t, err)

	// Wait for slow log to be written
	time.Sleep(500 * time.Millisecond)

	// Check slow log for Is_internal field
	slowLogContent, err := os.ReadFile(slowLogFile)
	if err == nil && len(slowLogContent) > 0 {
		slowLogStr := string(slowLogContent)
		lines := strings.Split(slowLogStr, "\n")

		for i, line := range lines {
			if strings.Contains(line, "plan_replayer_task") && strings.Contains(strings.ToLower(line), "select") {
				// Found the SELECT query, look for Is_internal field
				for j := i + 1; j < len(lines) && j < i+30; j++ {
					if strings.HasPrefix(lines[j], "# Is_internal:") {
						isInternal := strings.TrimSpace(strings.TrimPrefix(lines[j], "# Is_internal:"))
						require.Equal(t, "true", isInternal, "plan_replayer query should be marked as internal in slow log")
						break
					}
				}
				break
			}
		}
	}

	// Check statement summary - internal queries should NOT appear
	result := tk.MustQuery(`select count(*) from information_schema.statements_summary
		where digest_text like '%plan_replayer_task%'
		and digest_text not like '%statements_summary%'
		and digest_text not like '%delete from%'
		and digest_text not like '%insert into%'`).Rows()
	count := result[0][0].(string)
	require.Equal(t, "0", count, "Internal plan_replayer queries should not appear in statement_summary when internal query recording is disabled")

	// Cleanup
	tk.MustExec("delete from mysql.plan_replayer_task")
}


Review comment (suggestion):
It seems that this is checking the `Is_internal` field of the statement following the query `SELECT sql_digest, plan_digest FROM mysql.plan_replayer_task;`. 
I'm not sure if this process fully matches the intended test scenario, but the result does indeed meet expectations.

Rest LGTM

Code after:
	tk.MustExec("set global tidb_enable_stmt_summary = 1")
	tk.MustExec("set global tidb_stmt_summary_refresh_interval = 3600")
	tk.MustExec("set global tidb_stmt_summary_internal_query = 0")

	// Setup test data
	tk.MustExec("delete from mysql.plan_replayer_task")
	tk.MustExec("insert into mysql.plan_replayer_task (sql_digest, plan_digest) values ('test_digest', 'test_plan')")

	// Trigger plan replayer collection
	prHandle := dom.GetPlanReplayerHandle()
	err = prHandle.CollectPlanReplayerTask()
	require.NoError(t, err)

	// Wait for slow log to be written
	time.Sleep(500 * time.Millisecond)

	// Check slow log for Is_internal field
	slowLogContent, err := os.ReadFile(slowLogFile)
	if err == nil && len(slowLogContent) > 0 {
		slowLogStr := string(slowLogContent)
		lines := strings.Split(slowLogStr, "\n")

		for i, line := range lines {
			if strings.Contains(line, "select sql_digest, plan_digest from mysql.plan_replayer_task") && strings.Contains(strings.ToLower(line), "select") {
				t.Logf("Found matching line at index %d", i)
				// Found the SELECT query, look for Is_internal field
				// Find the start of this slow log entry: go backward until we find a line not starting with '#'
				startIdx := i - 1
				for startIdx > 0 && strings.HasPrefix(lines[startIdx], "#") {
					startIdx--
				}
				// Now startIdx points to the line before the first '#' line (the SQL query line)
				// Search for Is_internal field from startIdx+1 to i+10
				for j := startIdx + 1; j < len(lines) && j <= i; j++ {
					if strings.HasPrefix(lines[j], "# Is_internal:") {
						isInternal := strings.TrimSpace(strings.TrimPrefix(lines[j], "# Is_internal:"))
						require.Equal(t, "true", isInternal, "plan_replayer query should be marked as internal in slow log")
						// break
					}
				}
				break
			}
		}
	}

	// Check statement summary - internal queries should NOT appear
	result := tk.MustQuery(`select count(*) from information_schema.statements_summary
		where digest_text like '%plan_replayer_task%'
		and digest_text not like '%statements_summary%'
		and digest_text not like '%delete from%'
		and digest_text not like '%insert into%'`).Rows()