github.com/m3db/m3@v1.5.1-0.20231129193456-75a402aa583b/src/dbnode/integration/commitlog_bootstrap_with_snapshots_after_restart_test.go (about)

     1  // +build integration
     2  
     3  // Copyright (c) 2020 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/persist"
    32  	"github.com/m3db/m3/src/dbnode/persist/fs"
    33  	"github.com/m3db/m3/src/dbnode/persist/schema"
    34  	"github.com/m3db/m3/src/dbnode/retention"
    35  	xclock "github.com/m3db/m3/src/x/clock"
    36  	"github.com/m3db/m3/src/x/ident"
    37  	xtime "github.com/m3db/m3/src/x/time"
    38  
    39  	"github.com/stretchr/testify/require"
    40  )
    41  
    42  func TestCommitLogBootstrapWithSnapshotsAfterRestart(t *testing.T) {
    43  	if testing.Short() {
    44  		t.SkipNow() // Just skip if we're doing a short run
    45  	}
    46  
    47  	// Test setup
    48  	var (
    49  		ropts     = retention.NewOptions().SetRetentionPeriod(12 * time.Hour)
    50  		blockSize = ropts.BlockSize()
    51  	)
    52  	ns, err := namespace.NewMetadata(testNamespaces[0], namespace.NewOptions().
    53  		SetRetentionOptions(ropts).
    54  		SetColdWritesEnabled(true))
    55  	require.NoError(t, err)
    56  	opts := NewTestOptions(t).
    57  		SetNamespaces([]namespace.Metadata{ns}).
    58  		SetTickMinimumInterval(100 * time.Millisecond)
    59  
    60  	setup, err := NewTestSetup(t, opts, nil)
    61  	require.NoError(t, err)
    62  	defer setup.Close()
    63  
    64  	commitLogOpts := setup.StorageOpts().CommitLogOptions().
    65  		SetFlushInterval(defaultIntegrationTestFlushInterval)
    66  	setup.SetStorageOpts(setup.StorageOpts().
    67  		SetCommitLogOptions(commitLogOpts).
    68  		SetMediatorTickInterval(50 * time.Millisecond))
    69  
    70  	log := setup.StorageOpts().InstrumentOptions().Logger()
    71  	log.Info("commit log bootstrap with snapshots after restart test")
    72  
    73  	// Start the server with filesystem bootstrapper
    74  	require.NoError(t, setup.StartServer())
    75  	log.Debug("server is now up")
    76  
    77  	// Stop the server
    78  	defer func() {
    79  		require.NoError(t, setup.StopServer())
    80  		log.Debug("server is now down")
    81  	}()
    82  
    83  	// Write test data
    84  	log.Info("writing test data")
    85  	now := setup.NowFn()().Truncate(blockSize)
    86  	seriesMaps := make(map[xtime.UnixNano]generate.SeriesBlock)
    87  	inputData := []generate.BlockConfig{
    88  		{IDs: []string{"foo", "bar"}, NumPoints: 50, Start: now.Add(-5 * blockSize)},
    89  		{IDs: []string{"foo", "qux"}, NumPoints: 50, Start: now.Add(-4 * blockSize)},
    90  		{IDs: []string{"qux", "quux"}, NumPoints: 50, Start: now.Add(-3 * blockSize)},
    91  		{IDs: []string{"corge", "porgie"}, NumPoints: 50, Start: now.Add(-2 * blockSize)},
    92  	}
    93  	for _, input := range inputData {
    94  		testData := generate.Block(input)
    95  		seriesMaps[input.Start] = testData
    96  		require.NoError(t, setup.WriteBatch(testNamespaces[0], testData))
    97  	}
    98  
    99  	// Wait until snapshots are on disk.
   100  	fsOpts := commitLogOpts.FilesystemOptions()
   101  	expectedNumSeries := 0
   102  	for _, data := range inputData {
   103  		expectedNumSeries += len(data.IDs)
   104  	}
   105  	xclock.WaitUntil(func() bool {
   106  		var totalNumEntries int
   107  		for _, numEntries := range getNumEntriesPerBlockStart(ns.ID(), opts.NumShards(), fsOpts) {
   108  			totalNumEntries += numEntries
   109  		}
   110  		return totalNumEntries == expectedNumSeries
   111  	}, time.Minute)
   112  
   113  	// Stop and restart server to allow bootstrapping from commit logs.
   114  	require.NoError(t, setup.StopServer())
   115  	// Setup commitlog bootstrapper after writing data so filesystem inspection can find it.
   116  	require.NoError(t, setup.InitializeBootstrappers(InitializeBootstrappersOptions{
   117  		CommitLogOptions: commitLogOpts,
   118  		WithCommitLog:    true,
   119  		// Also setup fs bootstrapper to be ensure correct behaviour on restart w/ fs bootstrapper enabled.
   120  		WithFileSystem: true,
   121  	}))
   122  	require.NoError(t, setup.StartServer())
   123  	log.Debug("server restarted")
   124  
   125  	// Verify that data is what we expect.
   126  	metadatasByShard := testSetupMetadatas(t, setup, testNamespaces[0], now.Add(-5*blockSize), now.Add(-blockSize))
   127  	observedSeriesMaps := testSetupToSeriesMaps(t, setup, ns, metadatasByShard)
   128  	verifySeriesMapsEqual(t, seriesMaps, observedSeriesMaps)
   129  
   130  	// Wait until empty snapshots are on disk.
   131  	xclock.WaitUntil(func() bool {
   132  		var totalNumEntries int
   133  		for _, numEntries := range getNumEntriesPerBlockStart(ns.ID(), opts.NumShards(), fsOpts) {
   134  			totalNumEntries += numEntries
   135  		}
   136  		return totalNumEntries == 0
   137  	}, time.Minute)
   138  
   139  	// Verify that data is still what we expect.
   140  	metadatasByShard = testSetupMetadatas(t, setup, testNamespaces[0], now.Add(-5*blockSize), now.Add(-blockSize))
   141  	observedSeriesMaps = testSetupToSeriesMaps(t, setup, ns, metadatasByShard)
   142  	verifySeriesMapsEqual(t, seriesMaps, observedSeriesMaps)
   143  }
   144  
   145  func getNumEntriesPerBlockStart(
   146  	nsID ident.ID,
   147  	numShards int,
   148  	fsOpts fs.Options,
   149  ) map[xtime.UnixNano]int {
   150  	numEntriesPerBlockStart := make(map[xtime.UnixNano]int)
   151  	for shard := 0; shard < numShards; shard++ {
   152  		infoFiles := fs.ReadInfoFiles(
   153  			fsOpts.FilePathPrefix(),
   154  			nsID,
   155  			uint32(shard),
   156  			fsOpts.InfoReaderBufferSize(),
   157  			fsOpts.DecodingOptions(),
   158  			persist.FileSetSnapshotType,
   159  		)
   160  		// Grab the latest snapshot file for each blockstart.
   161  		latestSnapshotInfoPerBlockStart := make(map[xtime.UnixNano]schema.IndexInfo)
   162  		for _, f := range infoFiles {
   163  			info, ok := latestSnapshotInfoPerBlockStart[xtime.UnixNano(f.Info.BlockStart)]
   164  			if !ok {
   165  				latestSnapshotInfoPerBlockStart[xtime.UnixNano(f.Info.BlockStart)] = f.Info
   166  				continue
   167  			}
   168  
   169  			if f.Info.VolumeIndex > info.VolumeIndex {
   170  				latestSnapshotInfoPerBlockStart[xtime.UnixNano(f.Info.BlockStart)] = f.Info
   171  			}
   172  		}
   173  		for blockStart, info := range latestSnapshotInfoPerBlockStart {
   174  			numEntriesPerBlockStart[blockStart] += int(info.Entries)
   175  		}
   176  	}
   177  	return numEntriesPerBlockStart
   178  }