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

     1  // +build integration
     2  
     3  // Copyright (c) 2017 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  	"time"
    28  
    29  	"github.com/m3db/m3/src/dbnode/integration/generate"
    30  	"github.com/m3db/m3/src/dbnode/namespace"
    31  	"github.com/m3db/m3/src/dbnode/retention"
    32  
    33  	"github.com/stretchr/testify/require"
    34  	"github.com/uber-go/tally"
    35  )
    36  
    37  func TestCommitLogBootstrap(t *testing.T) {
    38  	testCommitLogBootstrap(t, nil, nil)
    39  }
    40  
    41  func TestProtoCommitLogBootstrap(t *testing.T) {
    42  	testCommitLogBootstrap(t, setProtoTestOptions, setProtoTestInputConfig)
    43  }
    44  
    45  func testCommitLogBootstrap(t *testing.T, setTestOpts setTestOptions, updateInputConfig generate.UpdateBlockConfig) {
    46  	if testing.Short() {
    47  		t.SkipNow() // Just skip if we're doing a short run
    48  	}
    49  
    50  	// Test setup
    51  	var (
    52  		ropts     = retention.NewOptions().SetRetentionPeriod(12 * time.Hour)
    53  		blockSize = ropts.BlockSize()
    54  	)
    55  	ns1, err := namespace.NewMetadata(testNamespaces[0], namespace.NewOptions().SetRetentionOptions(ropts))
    56  	require.NoError(t, err)
    57  	ns2, err := namespace.NewMetadata(testNamespaces[1], namespace.NewOptions().SetRetentionOptions(ropts))
    58  	require.NoError(t, err)
    59  	opts := NewTestOptions(t).
    60  		SetNamespaces([]namespace.Metadata{ns1, ns2})
    61  	if setTestOpts != nil {
    62  		opts = setTestOpts(t, opts)
    63  		ns1 = opts.Namespaces()[0]
    64  		ns2 = opts.Namespaces()[1]
    65  	}
    66  
    67  	setup, err := NewTestSetup(t, opts, nil)
    68  	require.NoError(t, err)
    69  	defer setup.Close()
    70  
    71  	commitLogOpts := setup.StorageOpts().CommitLogOptions().
    72  		SetFlushInterval(defaultIntegrationTestFlushInterval)
    73  	setup.SetStorageOpts(setup.StorageOpts().SetCommitLogOptions(commitLogOpts))
    74  
    75  	log := setup.StorageOpts().InstrumentOptions().Logger()
    76  	log.Info("commit log bootstrap test")
    77  
    78  	// Write test data
    79  	log.Info("generating data")
    80  	now := setup.NowFn()()
    81  	seriesMaps := generateSeriesMaps(30, updateInputConfig, now.Add(-2*blockSize), now.Add(-blockSize))
    82  	log.Info("writing data")
    83  	writes := writeCommitLogData(t, setup, commitLogOpts, seriesMaps, ns1, false)
    84  	log.Info("finished writing data")
    85  
    86  	// Setup bootstrapper after writing data so filesystem inspection can find it.
    87  	setupCommitLogBootstrapperWithFSInspection(t, setup, commitLogOpts)
    88  
    89  	setup.SetNowFn(now)
    90  	// Start the server with filesystem bootstrapper
    91  	require.NoError(t, setup.StartServer())
    92  	log.Debug("server is now up")
    93  
    94  	// Stop the server
    95  	defer func() {
    96  		require.NoError(t, setup.StopServer())
    97  		log.Debug("server is now down")
    98  	}()
    99  
   100  	// Verify in-memory data match what we expect - all writes from seriesMaps
   101  	// should be present
   102  	metadatasByShard := testSetupMetadatas(t, setup, testNamespaces[0], now.Add(-2*blockSize), now)
   103  	observedSeriesMaps := testSetupToSeriesMaps(t, setup, ns1, metadatasByShard)
   104  	verifySeriesMapsEqual(t, seriesMaps, observedSeriesMaps)
   105  
   106  	// Verify in-memory data match what we expect - no writes should be present
   107  	// because we didn't issue any writes for this namespace.
   108  	emptySeriesMaps := make(generate.SeriesBlocksByStart)
   109  	metadatasByShard2 := testSetupMetadatas(t, setup, testNamespaces[1], now.Add(-2*blockSize), now)
   110  	observedSeriesMaps2 := testSetupToSeriesMaps(t, setup, ns2, metadatasByShard2)
   111  	verifySeriesMapsEqual(t, emptySeriesMaps, observedSeriesMaps2)
   112  
   113  	counters := commitLogOpts.InstrumentOptions().MetricsScope().(tally.TestScope).Snapshot().Counters()
   114  	require.Equal(t, writes, int(counters["bootstrapper-commitlog.commitlog.entries-read+"].Value()))
   115  }