github.com/m3db/m3@v1.5.0/src/dbnode/integration/index_block_rotation_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 20mins. We write & index 50 metrics
    43   * between (1p, 1.30p).
    44   *
    45   * Then we move Now forward to 4p, and ensure the block is evicted.
    46   */
    47  func TestIndexBlockRotation(t *testing.T) {
    48  	if testing.Short() {
    49  		t.SkipNow() // Just skip if we're doing a short run
    50  	}
    51  
    52  	var (
    53  		numWrites       = 50
    54  		numTags         = 10
    55  		retentionPeriod = 2 * time.Hour
    56  		dataBlockSize   = 30 * time.Minute
    57  		indexBlockSize  = time.Hour
    58  		bufferFuture    = 20 * time.Minute
    59  		bufferPast      = 10 * time.Minute
    60  	)
    61  
    62  	// Test setup
    63  	md, err := namespace.NewMetadata(testNamespaces[0],
    64  		namespace.NewOptions().
    65  			SetRetentionOptions(
    66  				retention.NewOptions().
    67  					SetRetentionPeriod(retentionPeriod).
    68  					SetBufferPast(bufferPast).
    69  					SetBufferFuture(bufferFuture).
    70  					SetBlockSize(dataBlockSize)).
    71  			SetIndexOptions(
    72  				namespace.NewIndexOptions().
    73  					SetBlockSize(indexBlockSize).SetEnabled(true)))
    74  	require.NoError(t, err)
    75  
    76  	testOpts := NewTestOptions(t).
    77  		SetNamespaces([]namespace.Metadata{md}).
    78  		SetWriteNewSeriesAsync(true)
    79  	testSetup, err := NewTestSetup(t, testOpts, nil)
    80  	require.NoError(t, err)
    81  	defer testSetup.Close()
    82  
    83  	t0 := xtime.ToUnixNano(time.Date(2018, time.May, 6, 13, 0, 0, 0, time.UTC))
    84  	t1 := t0.Add(20 * time.Minute)
    85  	t2 := t1.Add(3 * time.Hour)
    86  	testSetup.SetNowFn(t0)
    87  
    88  	writesPeriod0 := GenerateTestIndexWrite(0, numWrites, numTags, t0, t1)
    89  
    90  	// Start the server
    91  	log := testSetup.StorageOpts().InstrumentOptions().Logger()
    92  	require.NoError(t, testSetup.StartServer())
    93  
    94  	// Stop the server
    95  	defer func() {
    96  		require.NoError(t, testSetup.StopServer())
    97  		log.Info("server is now down")
    98  	}()
    99  
   100  	client := testSetup.M3DBClient()
   101  	session, err := client.DefaultSession()
   102  	require.NoError(t, err)
   103  
   104  	log.Info("starting data write")
   105  	start := time.Now()
   106  	writesPeriod0.Write(t, md.ID(), session)
   107  	log.Info("test data written", zap.Duration("took", time.Since(start)))
   108  
   109  	log.Info("waiting till data is indexed")
   110  	indexed := xclock.WaitUntil(func() bool {
   111  		indexPeriod0 := writesPeriod0.NumIndexed(t, md.ID(), session)
   112  		return indexPeriod0 == len(writesPeriod0)
   113  	}, 5*time.Second)
   114  	require.True(t, indexed)
   115  	log.Info("verified data is indexed", zap.Duration("took", time.Since(start)))
   116  
   117  	// "shared":"shared", is a common tag across all written metrics
   118  	query := index.Query{
   119  		Query: idx.NewTermQuery([]byte("shared"), []byte("shared")),
   120  	}
   121  
   122  	// ensure all data is present
   123  	log.Info("querying period0 results")
   124  	period0Results, _, err := session.FetchTagged(ContextWithDefaultTimeout(),
   125  		md.ID(), query, index.QueryOptions{StartInclusive: t0, EndExclusive: t1})
   126  	require.NoError(t, err)
   127  	writesPeriod0.MatchesSeriesIters(t, period0Results)
   128  	log.Info("found period0 results")
   129  
   130  	// move time to 4p
   131  	testSetup.SetNowFn(t2)
   132  
   133  	// ensure all data is absent
   134  	log.Info("querying period0 results after expiry")
   135  	// await for results to be empty.
   136  	// in practice we've seen it take 11s, so make it 30s to be safe.
   137  	time.Sleep(time.Second * 4)
   138  	timeout := time.Second * 30
   139  	noData := xclock.WaitUntil(func() bool {
   140  		period0Results, _, err = session.FetchTagged(ContextWithDefaultTimeout(),
   141  			md.ID(), query, index.QueryOptions{StartInclusive: t0, EndExclusive: t1})
   142  		require.NoError(t, err)
   143  		require.True(t, period0Results.Len() == 50, "results still indexed")
   144  
   145  		for _, i := range period0Results.Iters() {
   146  			if i.Next() {
   147  				return false
   148  			}
   149  		}
   150  		return true
   151  	}, timeout)
   152  	require.True(t, noData, "data still present after %s", timeout)
   153  }