github.com/m3db/m3@v1.5.1-0.20231129193456-75a402aa583b/src/dbnode/integration/index_block_flush_test.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 "time" 28 29 "github.com/m3db/m3/src/dbnode/namespace" 30 "github.com/m3db/m3/src/dbnode/persist/fs" 31 "github.com/m3db/m3/src/dbnode/retention" 32 "github.com/m3db/m3/src/dbnode/storage/index" 33 xmetrics "github.com/m3db/m3/src/dbnode/x/metrics" 34 "github.com/m3db/m3/src/m3ninx/idx" 35 xclock "github.com/m3db/m3/src/x/clock" 36 "github.com/m3db/m3/src/x/instrument" 37 xtime "github.com/m3db/m3/src/x/time" 38 39 "github.com/stretchr/testify/assert" 40 "github.com/stretchr/testify/require" 41 "github.com/uber-go/tally" 42 "go.uber.org/zap" 43 ) 44 45 /* 46 * This test runs the following situation, Now is 1p, data blockSize is 30m, index blockSize is 1h, 47 * retention period 2h, buffer past 10mins, and buffer future 20mins. We write & index 50 metrics 48 * between (1p, 1.30p). 49 * 50 * Then we move Now forward to 3p, and ensure the block is flushed. And data is still readable. 51 */ 52 func TestIndexBlockFlush(t *testing.T) { 53 if testing.Short() { 54 t.SkipNow() // Just skip if we're doing a short run 55 } 56 57 var ( 58 numWrites = 50 59 numTags = 10 60 retentionPeriod = 2 * time.Hour 61 dataBlockSize = 30 * time.Minute 62 indexBlockSize = time.Hour 63 bufferFuture = 20 * time.Minute 64 bufferPast = 10 * time.Minute 65 verifyTimeout = 2 * time.Minute 66 ) 67 68 // Test setup 69 md, err := namespace.NewMetadata(testNamespaces[0], 70 namespace.NewOptions(). 71 SetRetentionOptions( 72 retention.NewOptions(). 73 SetRetentionPeriod(retentionPeriod). 74 SetBufferPast(bufferPast). 75 SetBufferFuture(bufferFuture). 76 SetBlockSize(dataBlockSize)). 77 SetIndexOptions( 78 namespace.NewIndexOptions(). 79 SetBlockSize(indexBlockSize).SetEnabled(true))) 80 require.NoError(t, err) 81 82 testOpts := NewTestOptions(t). 83 SetNamespaces([]namespace.Metadata{md}). 84 SetWriteNewSeriesAsync(true) 85 testSetup, err := NewTestSetup(t, testOpts, nil) 86 require.NoError(t, err) 87 defer testSetup.Close() 88 89 reporter := xmetrics.NewTestStatsReporter(xmetrics.NewTestStatsReporterOptions()) 90 scope, closer := tally.NewRootScope( 91 tally.ScopeOptions{Reporter: reporter}, time.Millisecond) 92 defer closer.Close() 93 testSetup.SetStorageOpts(testSetup.StorageOpts().SetInstrumentOptions( 94 instrument.NewOptions().SetMetricsScope(scope))) 95 96 t0 := xtime.ToUnixNano(time.Date(2018, time.May, 6, 13, 0, 0, 0, time.UTC)) 97 assert.True(t, t0.Equal(t0.Truncate(indexBlockSize))) 98 t1 := t0.Add(20 * time.Minute) 99 t2 := t0.Add(2 * time.Hour) 100 testSetup.SetNowFn(t0) 101 102 writesPeriod0 := GenerateTestIndexWrite(0, numWrites, numTags, t0, t1) 103 104 // Start the server 105 log := testSetup.StorageOpts().InstrumentOptions().Logger() 106 require.NoError(t, testSetup.StartServer()) 107 108 // Stop the server 109 defer func() { 110 require.NoError(t, testSetup.StopServer()) 111 log.Debug("server is now down") 112 }() 113 114 client := testSetup.M3DBClient() 115 session, err := client.DefaultSession() 116 require.NoError(t, err) 117 118 log.Info("starting data write") 119 start := time.Now() 120 writesPeriod0.Write(t, md.ID(), session) 121 log.Info("test data written", zap.Duration("took", time.Since(start))) 122 123 log.Info("waiting till data is indexed") 124 indexed := xclock.WaitUntil(func() bool { 125 indexPeriod0 := writesPeriod0.NumIndexed(t, md.ID(), session) 126 return indexPeriod0 == len(writesPeriod0) 127 }, verifyTimeout) 128 require.True(t, indexed) 129 log.Info("verified data is indexed", zap.Duration("took", time.Since(start))) 130 131 // "shared":"shared", is a common tag across all written metrics 132 query := index.Query{ 133 Query: idx.NewTermQuery([]byte("shared"), []byte("shared"))} 134 135 // ensure all data is present 136 log.Info("querying period0 results") 137 period0Results, _, err := session.FetchTagged(ContextWithDefaultTimeout(), 138 md.ID(), query, index.QueryOptions{StartInclusive: t0, EndExclusive: t1}) 139 require.NoError(t, err) 140 writesPeriod0.MatchesSeriesIters(t, period0Results) 141 log.Info("found period0 results") 142 143 // move time to 3p 144 testSetup.SetNowFn(t2) 145 146 // waiting till filesets found on disk 147 log.Info("waiting till filesets found on disk") 148 found := xclock.WaitUntil(func() bool { 149 filesets, err := fs.IndexFileSetsAt(testSetup.FilePathPrefix(), md.ID(), t0) 150 require.NoError(t, err) 151 return len(filesets) == 1 152 }, verifyTimeout) 153 require.True(t, found) 154 log.Info("found filesets found on disk") 155 156 // ensure we've evicted the mutable segments 157 log.Info("waiting till mutable segments are evicted") 158 evicted := xclock.WaitUntil(func() bool { 159 counters := reporter.Counters() 160 counter, ok := counters["dbindex.blocks-evicted-mutable-segments"] 161 return ok && counter > 0 162 }, verifyTimeout) 163 require.True(t, evicted) 164 log.Info("mutable segments are evicted!") 165 166 // ensure all data is still present 167 log.Info("querying period0 results after flush") 168 period0Results, _, err = session.FetchTagged(ContextWithDefaultTimeout(), 169 md.ID(), query, index.QueryOptions{StartInclusive: t0, EndExclusive: t1}) 170 require.NoError(t, err) 171 writesPeriod0.MatchesSeriesIters(t, period0Results) 172 log.Info("found period0 results after flush") 173 }