github.com/zuoyebang/bitalosdb@v1.1.1-0.20240516111551-79a8c4d8ce20/log_recycler_test.go (about)

     1  // Copyright 2021 The Bitalosdb author(hustxrb@163.com) and other contributors.
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package bitalosdb
    16  
    17  import (
    18  	"os"
    19  	"testing"
    20  
    21  	"github.com/stretchr/testify/require"
    22  )
    23  
    24  func (r *logRecycler) logNums() []FileNum {
    25  	r.mu.Lock()
    26  	defer r.mu.Unlock()
    27  	return fileInfoNums(r.mu.logs)
    28  }
    29  
    30  func (r *logRecycler) maxLogNum() FileNum {
    31  	r.mu.Lock()
    32  	defer r.mu.Unlock()
    33  	return r.mu.maxLogNum
    34  }
    35  
    36  func TestLogRecycler(t *testing.T) {
    37  	r := logRecycler{limit: 3, minRecycleLogNum: 4}
    38  
    39  	require.False(t, r.add(fileInfo{1, 0}))
    40  	require.False(t, r.add(fileInfo{2, 0}))
    41  	require.False(t, r.add(fileInfo{3, 0}))
    42  
    43  	require.True(t, r.add(fileInfo{4, 0}))
    44  	require.EqualValues(t, []FileNum{4}, r.logNums())
    45  	require.EqualValues(t, 4, r.maxLogNum())
    46  	fi, ok := r.peek()
    47  	require.True(t, ok)
    48  	require.EqualValues(t, 4, fi.fileNum)
    49  	require.True(t, r.add(fileInfo{5, 0}))
    50  	require.EqualValues(t, []FileNum{4, 5}, r.logNums())
    51  	require.EqualValues(t, 5, r.maxLogNum())
    52  	require.True(t, r.add(fileInfo{6, 0}))
    53  	require.EqualValues(t, []FileNum{4, 5, 6}, r.logNums())
    54  	require.EqualValues(t, 6, r.maxLogNum())
    55  
    56  	require.False(t, r.add(fileInfo{7, 0}))
    57  	require.EqualValues(t, []FileNum{4, 5, 6}, r.logNums())
    58  	require.EqualValues(t, 7, r.maxLogNum())
    59  
    60  	require.True(t, r.add(fileInfo{4, 0}))
    61  	require.EqualValues(t, []FileNum{4, 5, 6}, r.logNums())
    62  	require.EqualValues(t, 7, r.maxLogNum())
    63  
    64  	require.Regexp(t, `invalid 5 vs \[4 5 6\]`, r.pop(5))
    65  
    66  	require.NoError(t, r.pop(4))
    67  	require.EqualValues(t, []FileNum{5, 6}, r.logNums())
    68  
    69  	require.True(t, r.add(fileInfo{7, 0}))
    70  	require.EqualValues(t, []FileNum{5, 6}, r.logNums())
    71  
    72  	require.True(t, r.add(fileInfo{8, 0}))
    73  	require.EqualValues(t, []FileNum{5, 6, 8}, r.logNums())
    74  	require.EqualValues(t, 8, r.maxLogNum())
    75  
    76  	require.NoError(t, r.pop(5))
    77  	require.EqualValues(t, []FileNum{6, 8}, r.logNums())
    78  	require.NoError(t, r.pop(6))
    79  	require.EqualValues(t, []FileNum{8}, r.logNums())
    80  	require.NoError(t, r.pop(8))
    81  	require.EqualValues(t, []FileNum(nil), r.logNums())
    82  
    83  	require.Regexp(t, `empty`, r.pop(9))
    84  }
    85  
    86  func TestRecycleLogs(t *testing.T) {
    87  	defer os.RemoveAll(testDirname)
    88  	os.RemoveAll(testDirname)
    89  
    90  	db := openTestDB(testDirname, nil)
    91  	bitower := db.bitowers[testSlotId]
    92  
    93  	logNum := func() FileNum {
    94  		bitower.mu.Lock()
    95  		defer bitower.mu.Unlock()
    96  		return bitower.mu.log.queue[len(bitower.mu.log.queue)-1].fileNum
    97  	}
    98  
    99  	require.EqualValues(t, []FileNum(nil), bitower.logRecycler.logNums())
   100  	curLog := logNum()
   101  	require.NoError(t, db.Set(makeTestSlotKey([]byte("a")), nil, nil))
   102  	require.NoError(t, bitower.Flush())
   103  	require.EqualValues(t, []FileNum{curLog}, bitower.logRecycler.logNums())
   104  	curLog = logNum()
   105  	require.NoError(t, db.Set(makeTestSlotKey([]byte("b")), nil, nil))
   106  	require.NoError(t, bitower.Flush())
   107  	require.EqualValues(t, []FileNum{curLog}, bitower.logRecycler.logNums())
   108  	require.NoError(t, db.Close())
   109  
   110  	db = openTestDB(testDirname, nil)
   111  	bitower = db.bitowers[testSlotId]
   112  	if recycled := bitower.logRecycler.logNums(); len(recycled) != 0 {
   113  		t.Fatalf("expected no recycled WAL files after recovery, but found %d", recycled)
   114  	}
   115  	require.NoError(t, db.Close())
   116  }