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

     1  // +build integration
     2  //
     3  // Copyright (c) 2016 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/namespace"
    30  	"github.com/m3db/m3/src/dbnode/retention"
    31  	"github.com/m3db/m3/src/dbnode/storage/index"
    32  	"github.com/m3db/m3/src/m3ninx/idx"
    33  	xclock "github.com/m3db/m3/src/x/clock"
    34  	xtime "github.com/m3db/m3/src/x/time"
    35  
    36  	"github.com/stretchr/testify/require"
    37  	"go.uber.org/zap"
    38  )
    39  
    40  /*
    41   * This test runs the following situation, Now is 1p, data blockSize is 30m, index blockSize is 1h,
    42   * retention period 2h, buffer past 10mins, and buffer future 5mins. We write & index 50 metrics
    43   * between (12.50p,1p) and 50 metrics between (1p, 1.05p).
    44   *
    45   * Then we query for data for three periods: (12.50,1p), (1p,1.05p) and (12.50,1.05p); and ensure
    46   * the data we're returned is valid.
    47   */
    48  func TestIndexMultipleBlockQuery(t *testing.T) {
    49  	if testing.Short() {
    50  		t.SkipNow() // Just skip if we're doing a short run
    51  	}
    52  
    53  	var (
    54  		numWrites       = 50
    55  		numTags         = 10
    56  		retentionPeriod = 2 * time.Hour
    57  		dataBlockSize   = 30 * time.Minute
    58  		indexBlockSize  = time.Hour
    59  		bufferFuture    = 5 * time.Minute
    60  		bufferPast      = 10 * time.Minute
    61  	)
    62  
    63  	// Test setup
    64  	md, err := namespace.NewMetadata(testNamespaces[0],
    65  		namespace.NewOptions().
    66  			SetRetentionOptions(
    67  				retention.NewOptions().
    68  					SetRetentionPeriod(retentionPeriod).
    69  					SetBufferPast(bufferPast).
    70  					SetBufferFuture(bufferFuture).
    71  					SetBlockSize(dataBlockSize)).
    72  			SetIndexOptions(
    73  				namespace.NewIndexOptions().
    74  					SetBlockSize(indexBlockSize).SetEnabled(true)))
    75  	require.NoError(t, err)
    76  
    77  	testOpts := NewTestOptions(t).
    78  		SetNamespaces([]namespace.Metadata{md}).
    79  		SetWriteNewSeriesAsync(true)
    80  	testSetup, err := NewTestSetup(t, testOpts, nil)
    81  	require.NoError(t, err)
    82  	defer testSetup.Close()
    83  
    84  	t0 := xtime.ToUnixNano(time.Date(2018, time.May, 6, 12, 50, 0, 0, time.UTC))
    85  	t1 := t0.Add(10 * time.Minute)
    86  	t2 := t1.Add(5 * time.Minute)
    87  	testSetup.SetNowFn(t1)
    88  
    89  	writesPeriod0 := GenerateTestIndexWrite(0, numWrites, numTags, t0, t1)
    90  	writesPeriod1 := GenerateTestIndexWrite(1, numWrites, numTags, t1, t2)
    91  
    92  	// Start the server
    93  	log := testSetup.StorageOpts().InstrumentOptions().Logger()
    94  	require.NoError(t, testSetup.StartServer())
    95  
    96  	// Stop the server
    97  	defer func() {
    98  		require.NoError(t, testSetup.StopServer())
    99  		log.Debug("server is now down")
   100  	}()
   101  
   102  	client := testSetup.M3DBClient()
   103  	session, err := client.DefaultSession()
   104  	require.NoError(t, err)
   105  
   106  	log.Info("starting data write")
   107  	start := time.Now()
   108  	writesPeriod0.Write(t, md.ID(), session)
   109  	writesPeriod1.Write(t, md.ID(), session)
   110  	log.Info("test data written", zap.Duration("took", time.Since(start)))
   111  
   112  	log.Info("waiting till data is indexed")
   113  	indexed := xclock.WaitUntil(func() bool {
   114  		indexPeriod0 := writesPeriod0.NumIndexed(t, md.ID(), session)
   115  		indexPeriod1 := writesPeriod1.NumIndexed(t, md.ID(), session)
   116  		return indexPeriod0 == len(writesPeriod0) &&
   117  			indexPeriod1 == len(writesPeriod1)
   118  	}, 5*time.Second)
   119  	require.True(t, indexed)
   120  	log.Info("verified data is indexed", zap.Duration("took", time.Since(start)))
   121  
   122  	// "shared":"shared", is a common tag across all written metrics
   123  	query := index.Query{
   124  		Query: idx.NewTermQuery([]byte("shared"), []byte("shared"))}
   125  
   126  	log.Info("querying period0 results")
   127  	period0Results, _, err := session.FetchTagged(ContextWithDefaultTimeout(),
   128  		md.ID(), query, index.QueryOptions{StartInclusive: t0, EndExclusive: t1})
   129  	require.NoError(t, err)
   130  	writesPeriod0.MatchesSeriesIters(t, period0Results)
   131  	log.Info("found period0 results")
   132  
   133  	log.Info("querying period1 results")
   134  	period1Results, _, err := session.FetchTagged(ContextWithDefaultTimeout(),
   135  		md.ID(), query, index.QueryOptions{StartInclusive: t1, EndExclusive: t2})
   136  	require.NoError(t, err)
   137  	writesPeriod1.MatchesSeriesIters(t, period1Results)
   138  	log.Info("found period1 results")
   139  
   140  	log.Info("querying period 0+1 results")
   141  	period01Results, _, err := session.FetchTagged(ContextWithDefaultTimeout(),
   142  		md.ID(), query, index.QueryOptions{StartInclusive: t0, EndExclusive: t2})
   143  	require.NoError(t, err)
   144  	writes := append(writesPeriod0, writesPeriod1...)
   145  	writes.MatchesSeriesIters(t, period01Results)
   146  	log.Info("found period 0+1 results")
   147  }