Repository: pingcap/tidb
File: pkg/executor/test/analyzetest/memorycontrol/memory_control_test.go (Go)

Code before:

func TestAnalyzeV2MemoryUsageMetricNeverNegative(t *testing.T) {
	store, _ := testkit.CreateMockStoreAndDomain(t)
	oldChildTrackers := executor.GlobalAnalyzeMemoryTracker.GetChildrenForTest()
	for _, tracker := range oldChildTrackers {
		tracker.Detach()
	}
	defer func() {
		for _, tracker := range oldChildTrackers {
			tracker.AttachTo(executor.GlobalAnalyzeMemoryTracker)
		}
	}()
	executor.GlobalAnalyzeMemoryTracker.ReplaceBytesUsed(0)

	tk := testkit.NewTestKit(t, store)
	tk.MustExec("set @@tidb_analyze_version=2")
	tk.MustExec("set @@tidb_build_sampling_stats_concurrency=1")
	tk.MustExec("use test")
	tk.MustExec("drop table if exists t_mem_usage")
	tk.MustExec("create table t_mem_usage(a text collate utf8mb4_general_ci)")
	tk.MustExec("insert into t_mem_usage values (repeat('a', 32000))")
	for range 11 {
		tk.MustExec("insert into t_mem_usage select a from t_mem_usage")
	}
	tk.MustExec("insert into t_mem_usage select a from t_mem_usage limit 952")

	tk.MustExec("analyze table t_mem_usage with 1.0 samplerate;")
}


Review comment (suggestion):
We can
1. Change the type to `varchar` or 
2. Do an explicit set for the var `tidb_analyze_skip_column_types` to ensure that we will not skip the text type when building stats. 

Code after:
}

func TestAnalyzeV2MemoryUsageMetricNeverNegative(t *testing.T) {
	// This test should be fast because the whole package is marked as `timeout = "short"` in Bazel.
	const valueLen = 8 * 1024
	intest.Assert(statistics.MaxSampleValueLength > valueLen)

	store, _ := testkit.CreateMockStoreAndDomain(t)
	oldChildTrackers := executor.GlobalAnalyzeMemoryTracker.GetChildrenForTest()
	for _, tracker := range oldChildTrackers {
		tracker.Detach()
	}
	defer func() {
		for _, tracker := range oldChildTrackers {
			tracker.AttachTo(executor.GlobalAnalyzeMemoryTracker)
		}
	}()
	executor.GlobalAnalyzeMemoryTracker.ReplaceBytesUsed(0)

	tk := testkit.NewTestKit(t, store)
	tk.MustExec("set @@tidb_analyze_version=2")
	tk.MustExec("set @@tidb_build_sampling_stats_concurrency=1")
	tk.MustExec("set @@tidb_analyze_skip_column_types = ''")
	tk.MustExec("use test")
	tk.MustExec("drop table if exists t_mem_usage")
	tk.MustExec("create table t_mem_usage(a text collate utf8mb4_general_ci)")
	tk.MustExec(fmt.Sprintf("insert into t_mem_usage values (repeat('a', %d))", valueLen))
	for range 6 {
		tk.MustExec("insert into t_mem_usage select a from t_mem_usage")
	}

	tk.MustExec("analyze table t_mem_usage with 1.0 samplerate;")
}

func TestAnalyzeSessionMemTrackerDetachOnClose(t *testing.T) {
	store := testkit.CreateMockStore(t)
	tk := testkit.NewTestKit(t, store)
	tk.MustExec("use test")

	err := tk.ExecToErr("analyze table test.not_exists with 1 topn")
	require.Error(t, err)

	vars := tk.Session().GetSessionVars()
	// Clear any residue from the analyze statement to make the delta deterministic.
	vars.MemTracker.ReplaceBytesUsed(0)
	base := executor.GlobalAnalyzeMemoryTracker.BytesConsumed()
	vars.MemTracker.Consume(1024)
	require.Equal(t, base+1024, executor.GlobalAnalyzeMemoryTracker.BytesConsumed())

	tk.Session().Close()
	require.Equal(t, base, executor.GlobalAnalyzeMemoryTracker.BytesConsumed())