github.com/m3db/m3@v1.5.1-0.20231129193456-75a402aa583b/src/dbnode/storage/bootstrap/bootstrapper/persist_test.go (about)

     1  // Copyright (c) 2021 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 bootstrapper
    22  
    23  import (
    24  	"testing"
    25  	"time"
    26  
    27  	"github.com/golang/mock/gomock"
    28  	"github.com/stretchr/testify/require"
    29  
    30  	"github.com/m3db/m3/src/dbnode/namespace"
    31  	"github.com/m3db/m3/src/dbnode/persist/fs"
    32  	"github.com/m3db/m3/src/dbnode/retention"
    33  	"github.com/m3db/m3/src/dbnode/storage/bootstrap/result"
    34  	"github.com/m3db/m3/src/m3ninx/doc"
    35  	"github.com/m3db/m3/src/m3ninx/index/segment"
    36  	xtime "github.com/m3db/m3/src/x/time"
    37  )
    38  
    39  func TestBootstrapIndexSegmentOutOfRetention(t *testing.T) {
    40  	ctrl := gomock.NewController(t)
    41  	defer ctrl.Finish()
    42  	blockSize := 2 * time.Hour
    43  	retentionPeriod := 4 * time.Hour
    44  	now := xtime.Now().Add(-retentionPeriod)
    45  	start := now.Add(-retentionPeriod).Truncate(blockSize)
    46  	end := start.Add(retentionPeriod)
    47  	shardTimeRanges := result.NewShardTimeRanges().Set(
    48  		0,
    49  		xtime.NewRanges(xtime.Range{
    50  			Start: start,
    51  			End:   end,
    52  		}),
    53  	)
    54  
    55  	rOpts := retention.NewOptions().
    56  		SetBlockSize(blockSize).
    57  		SetRetentionPeriod(retentionPeriod)
    58  
    59  	nsOptions := namespace.NewOptions().SetRetentionOptions(rOpts)
    60  
    61  	mockMetadata := namespace.NewMockMetadata(ctrl)
    62  	mockMetadata.EXPECT().Options().Return(nsOptions).AnyTimes()
    63  
    64  	builder := segment.NewMockCloseableDocumentsBuilder(ctrl)
    65  	metadata := make([]doc.Metadata, 10)
    66  	builder.EXPECT().Docs().Return(metadata).AnyTimes()
    67  
    68  	resultOptions := result.NewOptions()
    69  	resultOptions.ClockOptions().SetNowFn(func() time.Time {
    70  		return now.ToTime()
    71  	})
    72  
    73  	blockStart := start
    74  	blockEnd := end // blockEnd equals earliest retention time
    75  
    76  	_, err := PersistBootstrapIndexSegment(
    77  		mockMetadata,
    78  		shardTimeRanges,
    79  		builder,
    80  		nil,
    81  		nil,
    82  		resultOptions,
    83  		shardTimeRanges,
    84  		blockStart,
    85  		blockEnd)
    86  
    87  	require.Equal(t, fs.ErrIndexOutOfRetention, err)
    88  
    89  	_, err = BuildBootstrapIndexSegment(
    90  		mockMetadata,
    91  		shardTimeRanges,
    92  		builder,
    93  		nil,
    94  		resultOptions,
    95  		nil,
    96  		blockStart,
    97  		blockEnd)
    98  	require.Equal(t, fs.ErrIndexOutOfRetention, err)
    99  
   100  	blockEnd = end.Add(-1 * time.Hour) // blockEnd less than earliest retention time
   101  
   102  	_, err = PersistBootstrapIndexSegment(
   103  		mockMetadata,
   104  		shardTimeRanges,
   105  		builder,
   106  		nil,
   107  		nil,
   108  		resultOptions,
   109  		shardTimeRanges,
   110  		blockStart,
   111  		blockEnd)
   112  
   113  	require.Equal(t, fs.ErrIndexOutOfRetention, err)
   114  
   115  	_, err = BuildBootstrapIndexSegment(
   116  		mockMetadata,
   117  		shardTimeRanges,
   118  		builder,
   119  		nil,
   120  		resultOptions,
   121  		nil,
   122  		blockStart,
   123  		blockEnd)
   124  	require.Equal(t, fs.ErrIndexOutOfRetention, err)
   125  }