github.com/m3db/m3@v1.5.0/src/query/test/m3/test_storage.go (about)

     1  // Copyright (c) 2018 Uber Technologies, Inc.
     2  //
     3  // Permission is hereby granted, free of charge, to any person obtaining a copy
     4  // of this software and associated documentation files (the "Software"), to deal
     5  // in the Software without restriction, including without limitation the rights
     6  // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
     7  // copies of the Software, and to permit persons to whom the Software is
     8  // furnished to do so, subject to the following conditions:
     9  //
    10  // The above copyright notice and this permission notice shall be included in
    11  // all copies or substantial portions of the Software.
    12  //
    13  // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
    14  // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
    15  // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
    16  // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
    17  // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
    18  // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
    19  // THE SOFTWARE.
    20  
    21  package m3
    22  
    23  import (
    24  	"testing"
    25  	"time"
    26  
    27  	"github.com/golang/mock/gomock"
    28  	"github.com/stretchr/testify/require"
    29  
    30  	"github.com/m3db/m3/src/dbnode/client"
    31  	"github.com/m3db/m3/src/dbnode/encoding"
    32  	"github.com/m3db/m3/src/query/models"
    33  	"github.com/m3db/m3/src/query/storage"
    34  	"github.com/m3db/m3/src/query/storage/m3"
    35  	"github.com/m3db/m3/src/x/ident"
    36  	"github.com/m3db/m3/src/x/instrument"
    37  	"github.com/m3db/m3/src/x/sync"
    38  )
    39  
    40  const (
    41  	// TestNamespaceID is the namespace of the test unaggregated namespace
    42  	// used by local storage.
    43  	TestNamespaceID = "metrics"
    44  	// TestRetention is the retention of the test unaggregated namespace
    45  	// used by local storage.
    46  	TestRetention = 30 * 24 * time.Hour
    47  )
    48  
    49  var (
    50  	defaultLookbackDuration = time.Minute
    51  )
    52  
    53  // NewStorageAndSession generates a new m3 storage and mock session
    54  func NewStorageAndSession(
    55  	t *testing.T,
    56  	ctrl *gomock.Controller,
    57  ) (storage.Storage, *client.MockSession) {
    58  	session := client.NewMockSession(ctrl)
    59  	clusters, err := m3.NewClusters(m3.UnaggregatedClusterNamespaceDefinition{
    60  		NamespaceID: ident.StringID(TestNamespaceID),
    61  		Session:     session,
    62  		Retention:   TestRetention,
    63  	})
    64  	require.NoError(t, err)
    65  	writePool, err := sync.NewPooledWorkerPool(10,
    66  		sync.NewPooledWorkerPoolOptions())
    67  	require.NoError(t, err)
    68  	writePool.Init()
    69  	tagOptions := models.NewTagOptions().SetMetricName([]byte("name"))
    70  	opts := m3.NewOptions(encoding.NewOptions()).
    71  		SetWriteWorkerPool(writePool).
    72  		SetTagOptions(tagOptions).
    73  		SetLookbackDuration(defaultLookbackDuration)
    74  
    75  	storage, err := m3.NewStorage(clusters, opts, instrument.NewOptions())
    76  	require.NoError(t, err)
    77  	return storage, session
    78  }
    79  
    80  // NewStorageAndSessionWithAggregatedNamespaces generates a new m3 storage and mock session
    81  // with the specified aggregated cluster namespace definitions.
    82  func NewStorageAndSessionWithAggregatedNamespaces(
    83  	t *testing.T,
    84  	ctrl *gomock.Controller,
    85  	aggregatedNamespaces []m3.AggregatedClusterNamespaceDefinition,
    86  ) (storage.Storage, *client.MockSession) {
    87  	session := client.NewMockSession(ctrl)
    88  	for i := range aggregatedNamespaces {
    89  		aggregatedNamespaces[i].Session = session
    90  	}
    91  
    92  	clusters, err := m3.NewClusters(m3.UnaggregatedClusterNamespaceDefinition{
    93  		NamespaceID: ident.StringID(TestNamespaceID),
    94  		Session:     session,
    95  		Retention:   TestRetention,
    96  	}, aggregatedNamespaces...)
    97  	require.NoError(t, err)
    98  
    99  	writePool, err := sync.NewPooledWorkerPool(10,
   100  		sync.NewPooledWorkerPoolOptions())
   101  	require.NoError(t, err)
   102  	writePool.Init()
   103  	tagOptions := models.NewTagOptions().SetMetricName([]byte("name"))
   104  	opts := m3.NewOptions(encoding.NewOptions()).
   105  		SetWriteWorkerPool(writePool).
   106  		SetTagOptions(tagOptions).
   107  		SetLookbackDuration(defaultLookbackDuration)
   108  
   109  	storage, err := m3.NewStorage(clusters, opts, instrument.NewOptions())
   110  	require.NoError(t, err)
   111  	return storage, session
   112  }