github.com/m3db/m3@v1.5.1-0.20231129193456-75a402aa583b/src/dbnode/persist/fs/clone/cloner_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 clone
    22  
    23  import (
    24  	"fmt"
    25  	"io"
    26  	"io/ioutil"
    27  	"os"
    28  	"path"
    29  	"testing"
    30  	"time"
    31  
    32  	"github.com/m3db/m3/src/dbnode/persist"
    33  	"github.com/m3db/m3/src/dbnode/persist/fs"
    34  	"github.com/m3db/m3/src/x/checked"
    35  	"github.com/m3db/m3/src/x/ident"
    36  	xtime "github.com/m3db/m3/src/x/time"
    37  
    38  	"github.com/stretchr/testify/require"
    39  )
    40  
    41  const (
    42  	numTestSeries = 100
    43  )
    44  
    45  var (
    46  	testBytes = checked.NewBytes([]byte("somelongstringofdata"), nil)
    47  )
    48  
    49  func TestCloner(t *testing.T) {
    50  	dir, err := ioutil.TempDir("", "clone")
    51  	require.NoError(t, err)
    52  	defer os.RemoveAll(dir)
    53  	opts := NewOptions()
    54  
    55  	// generate some fake source data
    56  	srcBlockSize := time.Hour
    57  	srcData := path.Join(dir, "src")
    58  	require.NoError(t, os.Mkdir(srcData, opts.DirMode()))
    59  	src := FileSetID{
    60  		PathPrefix: srcData,
    61  		Namespace:  "testns-src",
    62  		Shard:      123,
    63  		Blockstart: xtime.Now().Truncate(srcBlockSize),
    64  	}
    65  	testBytes.IncRef()
    66  	defer testBytes.DecRef()
    67  	writeTestData(t, srcBlockSize, src, opts)
    68  
    69  	// clone it
    70  	destBlockSize := 2 * time.Hour
    71  	clonedData := path.Join(dir, "clone")
    72  	require.NoError(t, os.Mkdir(clonedData, opts.DirMode()))
    73  	dest := FileSetID{
    74  		PathPrefix: clonedData,
    75  		Namespace:  "testns-dest",
    76  		Shard:      321,
    77  		Blockstart: xtime.Now().Add(-1 * 24 * 30 * time.Hour).Truncate(destBlockSize),
    78  	}
    79  	cloner := New(opts)
    80  	require.NoError(t, cloner.Clone(src, dest, destBlockSize))
    81  
    82  	// verify the two are equal
    83  	r1, err := fs.NewReader(opts.BytesPool(), fs.NewOptions().
    84  		SetFilePathPrefix(src.PathPrefix).
    85  		SetDataReaderBufferSize(opts.BufferSize()).
    86  		SetInfoReaderBufferSize(opts.BufferSize()).
    87  		SetDecodingOptions(opts.DecodingOptions()))
    88  	require.NoError(t, err)
    89  	r1OpenOpts := fs.DataReaderOpenOptions{
    90  		Identifier: fs.FileSetFileIdentifier{
    91  			Namespace:  ident.StringID(src.Namespace),
    92  			Shard:      src.Shard,
    93  			BlockStart: src.Blockstart,
    94  		},
    95  	}
    96  	require.NoError(t, r1.Open(r1OpenOpts))
    97  	r2, err := fs.NewReader(opts.BytesPool(), fs.NewOptions().
    98  		SetFilePathPrefix(dest.PathPrefix).
    99  		SetDataReaderBufferSize(opts.BufferSize()).
   100  		SetInfoReaderBufferSize(opts.BufferSize()).
   101  		SetDecodingOptions(opts.DecodingOptions()))
   102  	require.NoError(t, err)
   103  	r2OpenOpts := fs.DataReaderOpenOptions{
   104  		Identifier: fs.FileSetFileIdentifier{
   105  			Namespace:  ident.StringID(dest.Namespace),
   106  			Shard:      dest.Shard,
   107  			BlockStart: dest.Blockstart,
   108  		},
   109  	}
   110  	require.NoError(t, r2.Open(r2OpenOpts))
   111  	for {
   112  		t1, a1, b1, c1, e1 := r1.Read()
   113  		t2, a2, b2, c2, e2 := r2.Read()
   114  		if e1 == e2 && e1 == io.EOF {
   115  			break
   116  		}
   117  
   118  		require.NoError(t, e1)
   119  		require.NoError(t, e2)
   120  
   121  		b1.IncRef()
   122  		b2.IncRef()
   123  		require.Equal(t, t1.String(), t2.String())
   124  		require.True(t, ident.NewTagIterMatcher(a1).Matches(a2))
   125  		require.Equal(t, b1.Bytes(), b2.Bytes())
   126  		require.Equal(t, c1, c2)
   127  		b1.DecRef()
   128  		b2.DecRef()
   129  	}
   130  	require.NoError(t, r1.Close())
   131  	require.NoError(t, r2.Close())
   132  }
   133  
   134  func writeTestData(t *testing.T, bs time.Duration, src FileSetID, opts Options) {
   135  	w, err := fs.NewWriter(fs.NewOptions().
   136  		SetFilePathPrefix(src.PathPrefix).
   137  		SetWriterBufferSize(opts.BufferSize()).
   138  		SetNewFileMode(opts.FileMode()).
   139  		SetNewDirectoryMode(opts.DirMode()))
   140  	require.NoError(t, err)
   141  	writerOpts := fs.DataWriterOpenOptions{
   142  		BlockSize: bs,
   143  		Identifier: fs.FileSetFileIdentifier{
   144  			Namespace:  ident.StringID(src.Namespace),
   145  			Shard:      src.Shard,
   146  			BlockStart: src.Blockstart,
   147  		},
   148  	}
   149  	require.NoError(t, w.Open(writerOpts))
   150  	for i := 0; i < numTestSeries; i++ {
   151  		id := ident.StringID(fmt.Sprintf("test-series.%d", i))
   152  		var tags ident.Tags
   153  		if i%2 == 0 {
   154  			tags = ident.NewTags(
   155  				ident.StringTag("foo", "bar"),
   156  				ident.StringTag("qux", "qaz"),
   157  			)
   158  		}
   159  		metadata := persist.NewMetadataFromIDAndTags(id, tags,
   160  			persist.MetadataOptions{})
   161  		require.NoError(t, w.Write(metadata, testBytes, 1234))
   162  	}
   163  	require.NoError(t, w.Close())
   164  }