github.com/m3db/m3@v1.5.0/src/dbnode/integration/truncate_namespace_test.go (about)

     1  // +build integration
     2  
     3  // Copyright (c) 2016 Uber Technologies, Inc.
     4  //
     5  // Permission is hereby granted, free of charge, to any person obtaining a copy
     6  // of this software and associated documentation files (the "Software"), to deal
     7  // in the Software without restriction, including without limitation the rights
     8  // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
     9  // copies of the Software, and to permit persons to whom the Software is
    10  // furnished to do so, subject to the following conditions:
    11  //
    12  // The above copyright notice and this permission notice shall be included in
    13  // all copies or substantial portions of the Software.
    14  //
    15  // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
    16  // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
    17  // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
    18  // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
    19  // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
    20  // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
    21  // THE SOFTWARE.
    22  
    23  package integration
    24  
    25  import (
    26  	"testing"
    27  
    28  	"github.com/m3db/m3/src/dbnode/generated/thrift/rpc"
    29  	"github.com/m3db/m3/src/dbnode/integration/generate"
    30  	"github.com/m3db/m3/src/dbnode/namespace"
    31  	"github.com/m3db/m3/src/x/ident"
    32  	xtime "github.com/m3db/m3/src/x/time"
    33  
    34  	"github.com/stretchr/testify/require"
    35  )
    36  
    37  func TestTruncateNamespace(t *testing.T) {
    38  	if testing.Short() {
    39  		t.SkipNow() // Just skip if we're doing a short run
    40  	}
    41  	// Test setup
    42  	testOpts := NewTestOptions(t)
    43  	testSetup, err := NewTestSetup(t, testOpts, nil)
    44  	require.NoError(t, err)
    45  	defer testSetup.Close()
    46  
    47  	blockSize := namespace.NewOptions().RetentionOptions().BlockSize()
    48  
    49  	// Start the server
    50  	log := testSetup.StorageOpts().InstrumentOptions().Logger()
    51  	log.Debug("truncate namespace test")
    52  	require.NoError(t, testSetup.StartServer())
    53  	log.Debug("server is now up")
    54  
    55  	// Stop the server
    56  	defer func() {
    57  		require.NoError(t, testSetup.StopServer())
    58  		log.Debug("server is now down")
    59  	}()
    60  
    61  	// Write test data
    62  	now := testSetup.NowFn()()
    63  	seriesMaps := make(map[xtime.UnixNano]generate.SeriesBlock)
    64  	inputData := []struct {
    65  		namespace ident.ID
    66  		conf      generate.BlockConfig
    67  	}{
    68  		{testNamespaces[0], generate.BlockConfig{
    69  			IDs: []string{"foo"}, NumPoints: 100, Start: now},
    70  		},
    71  		{testNamespaces[1], generate.BlockConfig{
    72  			IDs: []string{"bar"}, NumPoints: 50, Start: now.Add(blockSize)},
    73  		},
    74  	}
    75  	for _, input := range inputData {
    76  		testSetup.SetNowFn(input.conf.Start)
    77  		testData := generate.Block(input.conf)
    78  		seriesMaps[input.conf.Start] = testData
    79  		require.NoError(t, testSetup.WriteBatch(input.namespace, testData))
    80  	}
    81  	log.Debug("test data is now written")
    82  
    83  	fetchReq := rpc.NewFetchRequest()
    84  	fetchReq.ID = "foo"
    85  	fetchReq.NameSpace = testNamespaces[1].String()
    86  	fetchReq.RangeStart = now.Seconds()
    87  	fetchReq.RangeEnd = now.Add(blockSize).Seconds()
    88  	fetchReq.ResultTimeType = rpc.TimeType_UNIX_SECONDS
    89  
    90  	log.Debug("fetching data from nonexistent namespace")
    91  	fetchReq.NameSpace = "nonexistent"
    92  	_, err = testSetup.Fetch(fetchReq)
    93  	require.Error(t, err)
    94  
    95  	log.Debug("fetching data from wrong namespace")
    96  	fetchReq.NameSpace = testNamespaces[1].String()
    97  	res, err := testSetup.Fetch(fetchReq)
    98  	require.NoError(t, err)
    99  	require.Equal(t, 0, len(res))
   100  
   101  	log.Sugar().Debugf("fetching data from namespace %s", testNamespaces[0])
   102  	fetchReq.NameSpace = testNamespaces[0].String()
   103  	res, err = testSetup.Fetch(fetchReq)
   104  	require.NoError(t, err)
   105  	require.Equal(t, 100, len(res))
   106  
   107  	log.Sugar().Debugf("truncate namespace %s", testNamespaces[0])
   108  	truncateReq := rpc.NewTruncateRequest()
   109  	truncateReq.NameSpace = testNamespaces[0].Bytes()
   110  	truncated, err := testSetup.Truncate(truncateReq)
   111  	require.NoError(t, err)
   112  	require.Equal(t, int64(1), truncated)
   113  
   114  	log.Sugar().Debugf("fetching data from namespace %s again", testNamespaces[0])
   115  	res, err = testSetup.Fetch(fetchReq)
   116  	require.Error(t, err)
   117  
   118  	log.Sugar().Debugf("fetching data from a different namespace %s", testNamespaces[1])
   119  	fetchReq.ID = "bar"
   120  	fetchReq.NameSpace = testNamespaces[1].String()
   121  	fetchReq.RangeStart = now.Add(blockSize).Seconds()
   122  	fetchReq.RangeEnd = now.Add(blockSize * 2).Seconds()
   123  	res, err = testSetup.Fetch(fetchReq)
   124  	require.NoError(t, err)
   125  	require.Equal(t, 50, len(res))
   126  }