github.com/m3db/m3@v1.5.0/src/dbnode/storage/index/write_batch_test.go (about)

     1  // Copyright (c) 2020 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 index
    22  
    23  import (
    24  	"fmt"
    25  	"testing"
    26  	"time"
    27  
    28  	"github.com/stretchr/testify/require"
    29  
    30  	"github.com/m3db/m3/src/m3ninx/doc"
    31  	xtest "github.com/m3db/m3/src/x/test"
    32  	xtime "github.com/m3db/m3/src/x/time"
    33  )
    34  
    35  func TestWriteBatchSortByUnmarkedAndIndexBlockStart(t *testing.T) {
    36  	ctrl := xtest.NewController(t)
    37  	defer ctrl.Finish()
    38  
    39  	blockSize := time.Hour
    40  
    41  	now := xtime.Now()
    42  	blockStart := now.Truncate(blockSize)
    43  
    44  	nowNotBlockStartAligned := now.
    45  		Truncate(blockSize).
    46  		Add(time.Minute)
    47  
    48  	h1 := doc.NewMockOnIndexSeries(ctrl)
    49  	h1.EXPECT().OnIndexFinalize(blockStart)
    50  	h1.EXPECT().OnIndexSuccess(blockStart)
    51  
    52  	h2 := doc.NewMockOnIndexSeries(ctrl)
    53  	h2.EXPECT().OnIndexFinalize(blockStart)
    54  
    55  	h3 := doc.NewMockOnIndexSeries(ctrl)
    56  	h3.EXPECT().OnIndexFinalize(blockStart)
    57  	h3.EXPECT().OnIndexSuccess(blockStart)
    58  
    59  	batch := NewWriteBatch(WriteBatchOptions{
    60  		IndexBlockSize: blockSize,
    61  	})
    62  	batch.Append(WriteBatchEntry{
    63  		Timestamp:     nowNotBlockStartAligned,
    64  		OnIndexSeries: h1,
    65  	}, testDoc1())
    66  	batch.Append(WriteBatchEntry{
    67  		Timestamp:     nowNotBlockStartAligned,
    68  		OnIndexSeries: h2,
    69  	}, testDoc2())
    70  	batch.Append(WriteBatchEntry{
    71  		Timestamp:     nowNotBlockStartAligned,
    72  		OnIndexSeries: h3,
    73  	}, testDoc3())
    74  
    75  	// Mark entry in middle as failure
    76  	batch.MarkUnmarkedEntryError(fmt.Errorf("an error"), 1)
    77  
    78  	// Now sort by unmarked and block start.
    79  	batch.SortByUnmarkedAndIndexBlockStart()
    80  
    81  	// Make sure two remaining.
    82  	require.Equal(t, 2, len(batch.PendingDocs()))
    83  
    84  	// Make sure marks two done.
    85  	batch.MarkUnmarkedEntriesSuccess()
    86  
    87  	// Make sure none remaining.
    88  	require.Equal(t, 0, len(batch.PendingDocs()))
    89  }