github.com/m3db/m3@v1.5.1-0.20231129193456-75a402aa583b/src/dbnode/integration/bootstrap_helpers.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  
    28  	"github.com/m3db/m3/src/dbnode/namespace"
    29  	"github.com/m3db/m3/src/dbnode/persist/fs/commitlog"
    30  	"github.com/m3db/m3/src/dbnode/storage/bootstrap"
    31  	"github.com/m3db/m3/src/dbnode/storage/bootstrap/bootstrapper"
    32  	"github.com/m3db/m3/src/dbnode/storage/bootstrap/result"
    33  	"github.com/m3db/m3/src/x/context"
    34  
    35  	"github.com/stretchr/testify/require"
    36  )
    37  
    38  func newTestBootstrapperSource(
    39  	opts testBootstrapperSourceOptions,
    40  	resultOpts result.Options,
    41  	next bootstrap.Bootstrapper,
    42  ) bootstrap.BootstrapperProvider {
    43  	src := testBootstrapperSource{}
    44  
    45  	if opts.availableData != nil {
    46  		src.availableData = opts.availableData
    47  	} else {
    48  		src.availableData = func(_ namespace.Metadata, shardsTimeRanges result.ShardTimeRanges, _ bootstrap.Cache, _ bootstrap.RunOptions) (result.ShardTimeRanges, error) {
    49  			return shardsTimeRanges, nil
    50  		}
    51  	}
    52  
    53  	if opts.availableIndex != nil {
    54  		src.availableIndex = opts.availableIndex
    55  	} else {
    56  		src.availableIndex = func(_ namespace.Metadata, shardsTimeRanges result.ShardTimeRanges, _ bootstrap.Cache, _ bootstrap.RunOptions) (result.ShardTimeRanges, error) {
    57  			return shardsTimeRanges, nil
    58  		}
    59  	}
    60  
    61  	if opts.read != nil {
    62  		src.read = opts.read
    63  	} else {
    64  		src.read = func(_ context.Context, namespaces bootstrap.Namespaces, _ bootstrap.Cache) (bootstrap.NamespaceResults, error) {
    65  			return bootstrap.NewNamespaceResults(namespaces), nil
    66  		}
    67  	}
    68  
    69  	var (
    70  		b   = &testBootstrapper{}
    71  		err error
    72  	)
    73  	b.Bootstrapper, err = bootstrapper.NewBaseBootstrapper(src.String(), src, resultOpts, next)
    74  	if err != nil {
    75  		panic(err)
    76  	}
    77  	return testBootstrapperProvider{Bootstrapper: b}
    78  }
    79  
    80  var _ bootstrap.BootstrapperProvider = &testBootstrapperProvider{}
    81  
    82  type testBootstrapperProvider struct {
    83  	bootstrap.Bootstrapper
    84  }
    85  
    86  func (p testBootstrapperProvider) String() string {
    87  	return p.Bootstrapper.String()
    88  }
    89  
    90  func (p testBootstrapperProvider) Provide() (bootstrap.Bootstrapper, error) {
    91  	return p.Bootstrapper, nil
    92  }
    93  
    94  type testBootstrapper struct {
    95  	bootstrap.Bootstrapper
    96  }
    97  
    98  type testBootstrapperSourceOptions struct {
    99  	availableData  func(namespace.Metadata, result.ShardTimeRanges, bootstrap.Cache, bootstrap.RunOptions) (result.ShardTimeRanges, error)
   100  	availableIndex func(namespace.Metadata, result.ShardTimeRanges, bootstrap.Cache, bootstrap.RunOptions) (result.ShardTimeRanges, error)
   101  	read           func(ctx context.Context, namespaces bootstrap.Namespaces, cache bootstrap.Cache) (bootstrap.NamespaceResults, error)
   102  }
   103  
   104  var _ bootstrap.Source = &testBootstrapperSource{}
   105  
   106  type testBootstrapperSource struct {
   107  	availableData  func(namespace.Metadata, result.ShardTimeRanges, bootstrap.Cache, bootstrap.RunOptions) (result.ShardTimeRanges, error)
   108  	availableIndex func(namespace.Metadata, result.ShardTimeRanges, bootstrap.Cache, bootstrap.RunOptions) (result.ShardTimeRanges, error)
   109  	read           func(ctx context.Context, namespaces bootstrap.Namespaces, cache bootstrap.Cache) (bootstrap.NamespaceResults, error)
   110  }
   111  
   112  func (t testBootstrapperSource) AvailableData(
   113  	ns namespace.Metadata,
   114  	shardsTimeRanges result.ShardTimeRanges,
   115  	cache bootstrap.Cache,
   116  	runOpts bootstrap.RunOptions,
   117  ) (result.ShardTimeRanges, error) {
   118  	return t.availableData(ns, shardsTimeRanges, cache, runOpts)
   119  }
   120  
   121  func (t testBootstrapperSource) AvailableIndex(
   122  	ns namespace.Metadata,
   123  	shardsTimeRanges result.ShardTimeRanges,
   124  	cache bootstrap.Cache,
   125  	runOpts bootstrap.RunOptions,
   126  ) (result.ShardTimeRanges, error) {
   127  	return t.availableIndex(ns, shardsTimeRanges, cache, runOpts)
   128  }
   129  
   130  func (t testBootstrapperSource) Read(
   131  	ctx context.Context,
   132  	namespaces bootstrap.Namespaces,
   133  	cache bootstrap.Cache,
   134  ) (bootstrap.NamespaceResults, error) {
   135  	return t.read(ctx, namespaces, cache)
   136  }
   137  
   138  func (t testBootstrapperSource) String() string {
   139  	return "test-bootstrapper"
   140  }
   141  
   142  func setupCommitLogBootstrapperWithFSInspection(
   143  	t *testing.T,
   144  	setup TestSetup,
   145  	commitLogOpts commitlog.Options,
   146  ) {
   147  	require.NoError(t, setup.InitializeBootstrappers(InitializeBootstrappersOptions{
   148  		CommitLogOptions: commitLogOpts,
   149  		WithCommitLog:    true,
   150  	}))
   151  }