github.com/m3db/m3@v1.5.1-0.20231129193456-75a402aa583b/src/dbnode/persist/fs/commitlog/files_test.go (about)

     1  // Copyright (c) 2018 Uber Technologies, Inc.
     2  //
     3  // Permission is hereby granted, free of charge, to any person obtaining a copy
     4  // of this software and associated documentation files (the "Software"), to deal
     5  // in the Software without restriction, including without limitation the rights
     6  // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
     7  // copies of the Software, and to permit persons to whom the Software is
     8  // furnished to do so, subject to the following conditions:
     9  //
    10  // The above copyright notice and this permission notice shall be included in
    11  // all copies or substantial portions of the Software.
    12  //
    13  // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
    14  // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
    15  // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
    16  // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
    17  // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
    18  // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
    19  // THE SOFTWARE.
    20  
    21  package commitlog
    22  
    23  import (
    24  	"io/ioutil"
    25  	"os"
    26  	"strings"
    27  	"testing"
    28  	"time"
    29  
    30  	"github.com/m3db/m3/src/dbnode/persist/fs"
    31  
    32  	"github.com/stretchr/testify/require"
    33  )
    34  
    35  func TestFiles(t *testing.T) {
    36  	dir, err := ioutil.TempDir("", "commitlogs")
    37  	require.NoError(t, err)
    38  	defer os.RemoveAll(dir)
    39  
    40  	createTestCommitLogFiles(t, dir, 10*time.Minute, 5)
    41  
    42  	var (
    43  		minNumBlocks = 5
    44  		opts         = testOpts
    45  	)
    46  	opts = opts.SetFilesystemOptions(
    47  		opts.FilesystemOptions().
    48  			SetFilePathPrefix(dir),
    49  	)
    50  	files, corruptFiles, err := Files(opts)
    51  	require.NoError(t, err)
    52  	require.True(t, len(corruptFiles) == 0)
    53  	require.True(t, len(files) >= minNumBlocks)
    54  
    55  	// Make sure its sorted.
    56  	var lastFileIndex = -1
    57  	for i, file := range files {
    58  		require.Equal(t, int64(i), file.Index)
    59  		require.True(t, strings.Contains(file.FilePath, dir))
    60  		if lastFileIndex == -1 {
    61  			lastFileIndex = int(file.Index)
    62  			continue
    63  		}
    64  
    65  		require.True(t, int(file.Index) > lastFileIndex)
    66  	}
    67  }
    68  
    69  // createTestCommitLogFiles creates at least the specified number of commit log files
    70  // on disk with the appropriate block size. Commit log files will be valid and contain
    71  // readable metadata.
    72  func createTestCommitLogFiles(
    73  	t *testing.T, filePathPrefix string, blockSize time.Duration, minNumBlocks int) {
    74  	require.True(t, minNumBlocks >= 2)
    75  
    76  	var (
    77  		opts = NewOptions().
    78  			SetBlockSize(blockSize).
    79  			SetClockOptions(NewOptions().ClockOptions()).
    80  			SetFilesystemOptions(fs.NewOptions().SetFilePathPrefix(filePathPrefix))
    81  		commitLogsDir = fs.CommitLogsDirPath(filePathPrefix)
    82  	)
    83  
    84  	commitLog, err := NewCommitLog(opts)
    85  	require.NoError(t, err)
    86  	require.NoError(t, commitLog.Open())
    87  
    88  	// Loop until we have enough commit log files.
    89  	for {
    90  		files, err := fs.SortedCommitLogFiles(commitLogsDir)
    91  		require.NoError(t, err)
    92  		if len(files) >= minNumBlocks {
    93  			break
    94  		}
    95  		_, err = commitLog.RotateLogs()
    96  		require.NoError(t, err)
    97  	}
    98  
    99  	require.NoError(t, commitLog.Close())
   100  	files, err := fs.SortedCommitLogFiles(commitLogsDir)
   101  	require.NoError(t, err)
   102  	require.True(t, len(files) >= minNumBlocks)
   103  }