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

     1  // +build integration
     2  
     3  // Copyright (c) 2018 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  	"github.com/m3db/m3/src/dbnode/storage/index"
    33  	"github.com/m3db/m3/src/m3ninx/idx"
    34  	"github.com/m3db/m3/src/x/ident"
    35  
    36  	"github.com/stretchr/testify/require"
    37  )
    38  
    39  func TestCommitLogIndexBootstrap(t *testing.T) {
    40  	if testing.Short() {
    41  		t.SkipNow() // Just skip if we're doing a short run
    42  	}
    43  
    44  	// Test setup
    45  	var (
    46  		rOpts     = retention.NewOptions().SetRetentionPeriod(12 * time.Hour)
    47  		blockSize = rOpts.BlockSize()
    48  	)
    49  
    50  	nsOpts := namespace.NewOptions().
    51  		SetRetentionOptions(rOpts).
    52  		SetIndexOptions(namespace.NewIndexOptions().
    53  			SetEnabled(true).
    54  			SetBlockSize(2 * blockSize),
    55  		).
    56  		SetColdWritesEnabled(true)
    57  	ns1, err := namespace.NewMetadata(testNamespaces[0], nsOpts)
    58  	require.NoError(t, err)
    59  	ns2, err := namespace.NewMetadata(testNamespaces[1], nsOpts)
    60  	require.NoError(t, err)
    61  	opts := NewTestOptions(t).
    62  		SetNamespaces([]namespace.Metadata{ns1, ns2})
    63  
    64  	setup, err := NewTestSetup(t, opts, nil)
    65  	require.NoError(t, err)
    66  	defer setup.Close()
    67  
    68  	commitLogOpts := setup.StorageOpts().CommitLogOptions().
    69  		SetFlushInterval(defaultIntegrationTestFlushInterval)
    70  	setup.SetStorageOpts(setup.StorageOpts().SetCommitLogOptions(commitLogOpts))
    71  
    72  	log := setup.StorageOpts().InstrumentOptions().Logger()
    73  	log.Info("commit log bootstrap test")
    74  
    75  	// Write test data
    76  	log.Info("generating data")
    77  	now := setup.NowFn()()
    78  	fooSeries := generate.Series{
    79  		ID:   ident.StringID("foo"),
    80  		Tags: ident.NewTags(ident.StringTag("city", "new_york"), ident.StringTag("foo", "foo")),
    81  	}
    82  
    83  	barSeries := generate.Series{
    84  		ID:   ident.StringID("bar"),
    85  		Tags: ident.NewTags(ident.StringTag("city", "new_jersey")),
    86  	}
    87  
    88  	bazSeries := generate.Series{
    89  		ID:   ident.StringID("baz"),
    90  		Tags: ident.NewTags(ident.StringTag("city", "seattle")),
    91  	}
    92  
    93  	unindexedSeries := generate.Series{
    94  		ID: ident.StringID("unindexed"),
    95  	}
    96  
    97  	seriesMaps := generate.BlocksByStart([]generate.BlockConfig{
    98  		{
    99  			IDs:       []string{fooSeries.ID.String()},
   100  			Tags:      fooSeries.Tags,
   101  			NumPoints: 100,
   102  			Start:     now.Add(-blockSize),
   103  		},
   104  		{
   105  			IDs:       []string{barSeries.ID.String()},
   106  			Tags:      barSeries.Tags,
   107  			NumPoints: 100,
   108  			Start:     now.Add(-blockSize),
   109  		},
   110  		{
   111  			IDs:       []string{fooSeries.ID.String()},
   112  			Tags:      fooSeries.Tags,
   113  			NumPoints: 50,
   114  			Start:     now,
   115  		},
   116  		{
   117  			IDs:       []string{bazSeries.ID.String()},
   118  			Tags:      bazSeries.Tags,
   119  			NumPoints: 50,
   120  			Start:     now,
   121  		},
   122  		{
   123  			IDs:       []string{unindexedSeries.ID.String()},
   124  			Tags:      ident.Tags{},
   125  			NumPoints: 1,
   126  			Start:     now,
   127  		},
   128  	})
   129  
   130  	log.Info("writing data")
   131  	writeCommitLogData(t, setup, commitLogOpts, seriesMaps, ns1, false)
   132  	writeCommitLogData(t, setup, commitLogOpts, seriesMaps, ns2, false)
   133  	log.Info("finished writing data")
   134  
   135  	// Setup bootstrapper after writing data so filesystem inspection can find it.
   136  	setupCommitLogBootstrapperWithFSInspection(t, setup, commitLogOpts)
   137  
   138  	setup.SetNowFn(now)
   139  	// Start the server with filesystem bootstrapper
   140  	require.NoError(t, setup.StartServer())
   141  	log.Debug("server is now up")
   142  
   143  	// Stop the server
   144  	defer func() {
   145  		require.NoError(t, setup.StopServer())
   146  		log.Debug("server is now down")
   147  	}()
   148  
   149  	// Verify in-memory data match what we expect - all writes from seriesMaps
   150  	// should be present
   151  	verifySeriesMaps(t, setup, testNamespaces[0], seriesMaps)
   152  	verifySeriesMaps(t, setup, testNamespaces[1], seriesMaps)
   153  
   154  	// Issue some index queries
   155  	session, err := setup.M3DBClient().DefaultSession()
   156  	require.NoError(t, err)
   157  
   158  	start := now.Add(-rOpts.RetentionPeriod())
   159  	end := now.Add(blockSize)
   160  	queryOpts := index.QueryOptions{StartInclusive: start, EndExclusive: end}
   161  
   162  	// Match all new_*r*
   163  	regexpQuery, err := idx.NewRegexpQuery([]byte("city"), []byte("new_.*r.*"))
   164  	require.NoError(t, err)
   165  	iter, fetchResponse, err := session.FetchTaggedIDs(ContextWithDefaultTimeout(),
   166  		ns1.ID(), index.Query{Query: regexpQuery}, queryOpts)
   167  	require.NoError(t, err)
   168  	defer iter.Finalize()
   169  
   170  	verifyQueryMetadataResults(t, iter, fetchResponse.Exhaustive, verifyQueryMetadataResultsOptions{
   171  		namespace:  ns1.ID(),
   172  		exhaustive: true,
   173  		expected:   []generate.Series{fooSeries, barSeries},
   174  	})
   175  
   176  	// Match all *e*e*
   177  	regexpQuery, err = idx.NewRegexpQuery([]byte("city"), []byte(".*e.*e.*"))
   178  	require.NoError(t, err)
   179  	iter, fetchResponse, err = session.FetchTaggedIDs(ContextWithDefaultTimeout(),
   180  		ns1.ID(), index.Query{Query: regexpQuery}, queryOpts)
   181  	require.NoError(t, err)
   182  	defer iter.Finalize()
   183  
   184  	verifyQueryMetadataResults(t, iter, fetchResponse.Exhaustive, verifyQueryMetadataResultsOptions{
   185  		namespace:  ns1.ID(),
   186  		exhaustive: true,
   187  		expected:   []generate.Series{barSeries, bazSeries},
   188  	})
   189  }