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

Code before:
	for {
		sequence++

		// New TSM files are written to a temp file and renamed when fully completed.
		fileName := filepath.Join(c.Dir, c.formatFileName(generation, sequence)+"."+TSMFileExtension+"."+TmpTSMFileExtension)
		logger.Debug("Compacting files", zap.Int("file_count", len(src)), zap.String("output_file", fileName))

		// Write as much as possible to this file
		rollToNext, err := c.write(fileName, iter, throttle, logger)

		if rollToNext {
			// We've hit the max file limit and there is more to write.  Create a new file
			// and continue.
			files = append(files, fileName)
			logger.Debug("file size or block count exceeded, opening another output file", zap.String("output_file", fileName))
			continue
		} else if errors.Is(err, ErrNoValues) {
			logger.Debug("Dropping empty file", zap.String("output_file", fileName))
			// If the file only contained tombstoned entries, then it would be a 0 length
			// file that we can drop.
			if err := os.RemoveAll(fileName); err != nil {
				return nil, err
			}
			break
		} else if errors.As(err, &eInProgress) {
			if !os.IsExist(eInProgress.err) {
				logger.Error("error creating compaction file", zap.String("output_file", fileName), zap.Error(err))
			} else {
				// Don't clean up the file as another compaction is using it.  This should not happen as the
				// planner keeps track of which files are assigned to compaction plans now.
				logger.Warn("file exists, compaction in progress already", zap.String("output_file", fileName))
			}
			return nil, err
		} else if err != nil {
			// We hit an error and didn't finish the compaction.  Abort.
			// Remove any tmp files we already completed
			// discard later errors to return the first one from the write() call
			for _, f := range files {
				_ = os.RemoveAll(f)
			}
			// Remove the temp file
			// discard later errors to return the first one from the write() call
			_ = os.RemoveAll(fileName)
			return nil, err
		}

		files = append(files, fileName)
		break
	}

	return files, nil

Review comment (suggestion):
Didn't notice the `os.IsExist` last time. This has been deprecated for `errors.Is(err, fs.ErrExist)`. See https://pkg.go.dev/os#IsExist

Code after:

	for {
		sequence++

		// New TSM files are written to a temp file and renamed when fully completed.
		fileName := filepath.Join(c.Dir, c.formatFileName(generation, sequence)+"."+TSMFileExtension+"."+TmpTSMFileExtension)
		logger.Debug("Compacting files", zap.Int("file_count", len(src)), zap.String("output_file", fileName))

		// Write as much as possible to this file
		rollToNext, err := c.write(fileName, iter, throttle, logger)

		if rollToNext {
			// We've hit the max file limit and there is more to write.  Create a new file
			// and continue.
			files = append(files, fileName)
			logger.Debug("file size or block count exceeded, opening another output file", zap.String("output_file", fileName))
			continue
		} else if errors.Is(err, ErrNoValues) {
			logger.Debug("Dropping empty file", zap.String("output_file", fileName))
			// If the file only contained tombstoned entries, then it would be a 0 length
			// file that we can drop.
			if err := os.RemoveAll(fileName); err != nil {
				return nil, err
			}
			break
		} else if errors.As(err, &eInProgress) {
			if !errors.Is(eInProgress.err, fs.ErrExist) {
				logger.Error("error creating compaction file", zap.String("output_file", fileName), zap.Error(err))
			} else {
				// Don't clean up the file as another compaction is using it.  This should not happen as the
				// planner keeps track of which files are assigned to compaction plans now.
				logger.Warn("file exists, compaction in progress already", zap.String("output_file", fileName))
			}
			return nil, err
		} else if err != nil {
			// We hit an error and didn't finish the compaction.  Abort.
			// Remove any tmp files we already completed
			// discard later errors to return the first one from the write() call
			for _, f := range files {
				_ = os.RemoveAll(f)
			}
			// Remove the temp file
			// discard later errors to return the first one from the write() call
			_ = os.RemoveAll(fileName)
			return nil, err
		}

		files = append(files, fileName)
		break
	}