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

     1  // Copyright (c) 2021  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 simple contains code simple integration tests that writes and reads.
    22  package simple
    23  
    24  import (
    25  	"fmt"
    26  	"testing"
    27  	"time"
    28  
    29  	"github.com/stretchr/testify/require"
    30  	"go.uber.org/zap"
    31  
    32  	"github.com/m3db/m3/src/dbnode/generated/thrift/rpc"
    33  	"github.com/m3db/m3/src/integration/resources"
    34  	"github.com/m3db/m3/src/m3ninx/idx"
    35  	"github.com/m3db/m3/src/x/ident"
    36  	"github.com/m3db/m3/src/x/pool"
    37  	"github.com/m3db/m3/src/x/serialize"
    38  )
    39  
    40  const (
    41  	// TestSimpleDBNodeConfig is the test config for the dbnode.
    42  	TestSimpleDBNodeConfig = `
    43  db: {}
    44  `
    45  
    46  	// TestSimpleCoordinatorConfig is the test config for the coordinator.
    47  	TestSimpleCoordinatorConfig = `
    48  clusters:
    49    - namespaces:
    50        - namespace: default
    51          type: unaggregated
    52          retention: 48h
    53  `
    54  )
    55  
    56  // RunTest contains the logic for running the simple test.
    57  func RunTest(t *testing.T, m3 resources.M3Resources) {
    58  	var (
    59  		id  = "foo"
    60  		val = 42.123456789
    61  		ts  = time.Now()
    62  	)
    63  
    64  	encoderPool := newTagEncoderPool()
    65  	encodedTags := encodeTags(t, encoderPool,
    66  		ident.StringTag("city", "new_york"),
    67  		ident.StringTag("endpoint", "/request"))
    68  
    69  	// Write data point.
    70  	req := &rpc.WriteTaggedBatchRawRequest{
    71  		NameSpace: []byte(resources.UnaggName),
    72  		Elements: []*rpc.WriteTaggedBatchRawRequestElement{
    73  			{
    74  				ID:          []byte(id),
    75  				EncodedTags: encodedTags,
    76  				Datapoint: &rpc.Datapoint{
    77  					Timestamp:         ts.UnixNano(),
    78  					TimestampTimeType: rpc.TimeType_UNIX_NANOSECONDS,
    79  					Value:             val,
    80  				},
    81  			},
    82  		},
    83  	}
    84  	dbnode := m3.Nodes()[0]
    85  
    86  	require.NoError(t, dbnode.WriteTaggedBatchRaw(req))
    87  
    88  	// Fetch tagged data point.
    89  	query, err := idx.NewRegexpQuery([]byte("city"), []byte(".*"))
    90  	require.NoError(t, err)
    91  
    92  	encoded, err := idx.Marshal(query)
    93  	require.NoError(t, err)
    94  
    95  	freq := &rpc.FetchTaggedRequest{
    96  		RangeStart:    0,
    97  		RangeEnd:      ts.UnixNano(),
    98  		NameSpace:     []byte(resources.UnaggName),
    99  		RangeTimeType: rpc.TimeType_UNIX_NANOSECONDS,
   100  		FetchData:     true,
   101  		Query:         encoded,
   102  	}
   103  
   104  	logger, err := resources.NewLogger()
   105  	require.NoError(t, err)
   106  	err = resources.Retry(func() error {
   107  		res, err := dbnode.FetchTagged(freq)
   108  		if err != nil {
   109  			return err
   110  		}
   111  
   112  		if len(res.Elements) != 1 {
   113  			err = fmt.Errorf("expected datapoint not present")
   114  			logger.Error("number of elements not equal to 1", zap.Error(err))
   115  			return err
   116  		}
   117  
   118  		logger.Info("datapoint successfully fetched")
   119  		return nil
   120  	})
   121  	require.NoError(t, err)
   122  
   123  	coord := m3.Coordinator()
   124  	require.NoError(t, coord.DeleteAllPlacements(resources.PlacementRequestOptions{
   125  		Service: resources.ServiceTypeM3DB,
   126  	}))
   127  	require.NoError(t, coord.DeleteNamespace(resources.UnaggName))
   128  }
   129  
   130  func newTagEncoderPool() serialize.TagEncoderPool {
   131  	encoderPool := serialize.
   132  		NewTagEncoderPool(serialize.NewTagEncoderOptions(),
   133  			pool.NewObjectPoolOptions().SetSize(1))
   134  	encoderPool.Init()
   135  	return encoderPool
   136  }
   137  
   138  func encodeTags(
   139  	t *testing.T,
   140  	pool serialize.TagEncoderPool,
   141  	tags ...ident.Tag,
   142  ) []byte {
   143  	encoder := pool.Get()
   144  
   145  	seriesTags := ident.NewTags(tags...)
   146  	err := encoder.Encode(ident.NewTagsIterator(seriesTags))
   147  	require.NoError(t, err)
   148  
   149  	data, ok := encoder.Data()
   150  	require.True(t, ok)
   151  
   152  	return data.Bytes()
   153  }