Repository: influxdata/influxdb
File: tsdb/engine/tsm1/compact_test.go (Go)

Code before:

	e, err := NewEngine(tsdb.InmemIndexName)
	require.NoError(t, err, "create engine")
	e.SetEnabled(false)
	require.NoError(t, e.Open(), "open engine")
	defer func() { require.NoError(t, e.Close(), "close engine") }()
	e.Compactor = tsm1.NewCompactor()
	defer e.Compactor.Close()

	for _, test := range tests {
		t.Run(test.name, func(t *testing.T) {
			ffs := newFakeFileStore(withExtFileStats(test.files), withDefaultBlockCount(test.defaultBlockCount))
			cp := tsm1.NewDefaultPlanner(ffs, test.testShardTime)

			e.MaxPointsPerBlock = tsdb.DefaultMaxPointsPerBlock
			e.CompactionPlan = cp
			e.Compactor.FileStore = ffs

			// Arbitrary group length to use in Scheduler.SetDepth
			mockGroupLen := 5
			// Set the scheduler depth for our lower level groups.
			e.Scheduler.SetDepth(1, mockGroupLen)
			e.Scheduler.SetDepth(2, mockGroupLen)

			// Validate our test case based on compact_property_test.go
			validationErr := ValidateTestCase(test, test.expectedResult())
			require.NoError(t, validationErr, "test validation failed")

			// Normally this is called within PlanCompactions but because we want to simulate already running
			// some compactions we will set them manually here.
			atomic.StoreInt64(&e.Stats.TSMCompactionsActive[0], int64(mockGroupLen))
			atomic.StoreInt64(&e.Stats.TSMCompactionsActive[1], int64(mockGroupLen))

			// Plan and check results.
			level1Groups, level2Groups, Level3Groups, Level4Groups, Level5Groups := e.PlanCompactions()
			results := test.expectedResult()
			compareLevelGroups(t, results.level1Groups, level1Groups, "unexpected level 1 Group")
			compareLevelGroups(t, results.level2Groups, level2Groups, "unexpected level 2 Group")
			compareLevelGroups(t, results.level3Groups, Level3Groups, "unexpected level 3 Group")
			compareLevelGroups(t, results.level4Groups, Level4Groups, "unexpected level 4 Group")
			compareLevelGroups(t, results.level5Groups, Level5Groups, "unexpected level 5 Group")

			// Remove all the returned compaction levels and verify that no TSM files are marked as in-use.
			// Calling e.PlanCompactions followed by e.ReleaseCompactionPlans replicates the code structure
			// of the e.compact loop.
			e.ReleaseCompactionPlans(level1Groups, level2Groups, Level3Groups, Level4Groups, Level5Groups)
			require.Zero(t, cp.InUseCount(), "some TSM files were not released properly")
		})
	}
}


Review comment (refactor):
looks like this is validating the expectedResults() twice: once as they are extracted from `test` and then again as the second argument. 

It should be `validationErr := ValidateTestCase(test, level1Groups, level2Groups, Level3Groups, Level4Groups, Level5Groups))` where the groups come from `e.PlanCompactions()`... line 4526/4511

Code after:

	e, err := NewEngine(tsdb.InmemIndexName)
	require.NoError(t, err, "create engine")
	e.SetEnabled(false)
	require.NoError(t, e.Open(), "open engine")
	defer func() { require.NoError(t, e.Close(), "close engine") }()
	e.Compactor = tsm1.NewCompactor()
	defer e.Compactor.Close()

	for _, test := range tests {
		t.Run(test.name, func(t *testing.T) {
			ffs := newFakeFileStore(withExtFileStats(test.files), withDefaultBlockCount(test.defaultBlockCount))
			cp := tsm1.NewDefaultPlanner(ffs, test.testShardTime)

			e.MaxPointsPerBlock = tsdb.DefaultMaxPointsPerBlock
			e.CompactionPlan = cp
			e.Compactor.FileStore = ffs

			// Arbitrary group length to use in Scheduler.SetDepth
			mockGroupLen := 5
			// Set the scheduler depth for our lower level groups.
			e.Scheduler.SetDepth(1, mockGroupLen)
			e.Scheduler.SetDepth(2, mockGroupLen)

			// Normally this is called within PlanCompactions but because we want to simulate already running
			// some compactions we will set them manually here.
			atomic.StoreInt64(&e.Stats.TSMCompactionsActive[0], int64(mockGroupLen))
			atomic.StoreInt64(&e.Stats.TSMCompactionsActive[1], int64(mockGroupLen))

			// Plan and check results.
			level1Groups, level2Groups, Level3Groups, Level4Groups, Level5Groups := e.PlanCompactions()
			results := test.expectedResult()

			// Validate our test case based on compact_property_test.go
			validationErr := ValidateTestCase(test, TestLevelResults{
				level1Groups: level1Groups,
				level2Groups: level2Groups,
				level3Groups: Level3Groups,
				level4Groups: Level4Groups,
				level5Groups: Level5Groups,
			})
			assert.NoError(t, validationErr, "test validation failed")

			compareLevelGroups(t, results.level1Groups, level1Groups, "unexpected level 1 Group")
			compareLevelGroups(t, results.level2Groups, level2Groups, "unexpected level 2 Group")
			compareLevelGroups(t, results.level3Groups, Level3Groups, "unexpected level 3 Group")
			compareLevelGroups(t, results.level4Groups, Level4Groups, "unexpected level 4 Group")
			compareLevelGroups(t, results.level5Groups, Level5Groups, "unexpected level 5 Group")

			// Remove all the returned compaction levels and verify that no TSM files are marked as in-use.
			// Calling e.PlanCompactions followed by e.ReleaseCompactionPlans replicates the code structure