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

Code before:
		if gen > maxGeneration {
			maxGeneration = gen
			maxSequence = seq
		}
		if gen == maxGeneration && seq > maxSequence {
			maxSequence = seq
		}
		if s, ok := minSeqByGen[gen]; !ok || seq < s {
			minSeqByGen[gen] = seq
		}
	}

	// Ensure the output level is at least as high as the highest input level.
	// A generation's level is min(its lowest sequence, 4). The output level is
	// min(maxSequence+1, 4). When cold or forced compaction groups L1 files
	// (high gen, seq=1) with L4 files (low gen, seq>=4), the per-generation
	// maxSequence can be as low as 1, producing L2 output from L4 input data.
	// Bump maxSequence so the output level does not regress.
	for _, minSeq := range minSeqByGen {
		level := minSeq
		if level > 4 {
			level = 4
		}
		if maxSequence+1 < level {
			maxSequence = level - 1
		}
	}

	// For each TSM file, create a TSM reader
	var trs []*TSMReader
	for _, file := range tsmFiles {
		select {
		case <-intC:
			return nil, errCompactionAborted{}
		default:
		}

		tr, err := c.FileStore.TSMReader(file)
		if err != nil {
			return nil, errCompactionAborted{fmt.Errorf("error creating reader for %q: %w", file, err)}
		}
		if tr == nil {
			// This would be a bug if this occurred as tsmFiles passed in should only be
			// assigned to one compaction at any one time.  A nil tr would mean the file
			// doesn't exist.
			return nil, errCompactionAborted{fmt.Errorf("bad plan: %s", file)}
		}
		defer tr.Unref() // inform that we're done with this reader when this method returns.
		trs = append(trs, tr)
	}


Review comment (suggestion):
I find the logic here somewhat confusing. I've created a branch with an alternative implementation that gets the same result, but makes it clear what is being calculated.

For your consideration:
https://github.com/influxdata/influxdb/tree/gw/max_seq_fix_minInputLevel

Diff:
https://github.com/influxdata/influxdb/compare/DSB/max_seq_fix..gw/max_seq_fix_minInputLevel

Code after:
		if gen > maxGeneration {
			maxGeneration = gen
			maxSequence = seq
		}
		if gen == maxGeneration && seq > maxSequence {
			maxSequence = seq
		}
		if s, ok := minSeqByGen[gen]; !ok || seq < s {
			minSeqByGen[gen] = seq
		}
	}

	// Compute the highest compaction level among all input generations.
	// A generation's level = min(its minimum sequence number, 4).
	var maxInputLevel int
	for _, minSeq := range minSeqByGen {
		maxInputLevel = max(maxInputLevel, min(minSeq, 4))
	}

	// Ensure the output level (min(maxSequence+1, 4)) does not regress
	// below any input level. This matters when cold/forced compaction
	// groups L1 files (high gen, seq=1) with L4 files (low gen, seq>=4).
	if maxSequence+1 < maxInputLevel {
		maxSequence = maxInputLevel - 1
	}

	// For each TSM file, create a TSM reader
	var trs []*TSMReader
	for _, file := range tsmFiles {
		select {
		case <-intC:
			return nil, errCompactionAborted{}
		default:
		}

		tr, err := c.FileStore.TSMReader(file)
		if err != nil {
			return nil, errCompactionAborted{fmt.Errorf("error creating reader for %q: %w", file, err)}
		}
		if tr == nil {
			// This would be a bug if this occurred as tsmFiles passed in should only be
			// assigned to one compaction at any one time.  A nil tr would mean the file
			// doesn't exist.
			return nil, errCompactionAborted{fmt.Errorf("bad plan: %s", file)}
		}
		defer tr.Unref() // inform that we're done with this reader when this method returns.
		trs = append(trs, tr)
	}

	if len(trs) == 0 {
		logger.Debug("No input files")