github.com/m3db/m3@v1.5.1-0.20231129193456-75a402aa583b/src/metrics/rules/match_result_test.go (about)

     1  // Copyright (c) 2020 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 rules
    22  
    23  import (
    24  	"strconv"
    25  	"testing"
    26  	"time"
    27  
    28  	"github.com/m3db/m3/src/metrics/aggregation"
    29  	"github.com/m3db/m3/src/metrics/metadata"
    30  	"github.com/m3db/m3/src/metrics/policy"
    31  	xtime "github.com/m3db/m3/src/x/time"
    32  
    33  	"github.com/stretchr/testify/require"
    34  )
    35  
    36  func TestMatchResultProperties(t *testing.T) {
    37  	cases := []struct {
    38  		version      int
    39  		expires      int64
    40  		keepOriginal bool
    41  	}{
    42  		{
    43  			version:      1,
    44  			expires:      1000,
    45  			keepOriginal: false,
    46  		},
    47  		{
    48  			version:      2,
    49  			expires:      2000,
    50  			keepOriginal: true,
    51  		},
    52  	}
    53  
    54  	for i, tt := range cases {
    55  		t.Run(strconv.Itoa(i), func(t *testing.T) {
    56  			r := NewMatchResult(
    57  				tt.version,
    58  				tt.expires,
    59  				nil,
    60  				nil,
    61  				tt.keepOriginal,
    62  			)
    63  
    64  			require.Equal(t, tt.version, r.Version())
    65  			require.Equal(t, tt.expires, r.ExpireAtNanos())
    66  			require.False(t, r.HasExpired(tt.expires-1))
    67  			require.True(t, r.HasExpired(tt.expires))
    68  			require.Equal(t, tt.keepOriginal, r.KeepOriginal())
    69  		})
    70  	}
    71  }
    72  
    73  func TestMatchResult(t *testing.T) {
    74  	var (
    75  		testExpireAtNanos = int64(67890)
    76  		testForExistingID = metadata.StagedMetadatas{
    77  			{
    78  				CutoverNanos: 12345,
    79  				Tombstoned:   false,
    80  				Metadata: metadata.Metadata{
    81  					Pipelines: []metadata.PipelineMetadata{
    82  						{
    83  							AggregationID: aggregation.DefaultID,
    84  							StoragePolicies: policy.StoragePolicies{
    85  								policy.NewStoragePolicy(10*time.Second, xtime.Second, 12*time.Hour),
    86  								policy.NewStoragePolicy(time.Minute, xtime.Minute, 24*time.Hour),
    87  								policy.NewStoragePolicy(5*time.Minute, xtime.Minute, 48*time.Hour),
    88  							},
    89  						},
    90  					},
    91  				},
    92  			},
    93  			{
    94  				CutoverNanos: 23456,
    95  				Tombstoned:   true,
    96  				Metadata: metadata.Metadata{
    97  					Pipelines: []metadata.PipelineMetadata{
    98  						{
    99  							AggregationID: aggregation.DefaultID,
   100  							StoragePolicies: policy.StoragePolicies{
   101  								policy.NewStoragePolicy(30*time.Second, xtime.Second, 10*time.Hour),
   102  								policy.NewStoragePolicy(2*time.Minute, xtime.Minute, 48*time.Hour),
   103  							},
   104  						},
   105  					},
   106  				},
   107  			},
   108  		}
   109  		testForNewRollupIDs = []IDWithMetadatas{
   110  			{
   111  				ID: b("rName1|rtagName1=rtagValue1,rtagName2=rtagValue2"),
   112  				Metadatas: metadata.StagedMetadatas{
   113  					{
   114  						CutoverNanos: 12345,
   115  						Tombstoned:   false,
   116  						Metadata: metadata.Metadata{
   117  							Pipelines: []metadata.PipelineMetadata{
   118  								{
   119  									AggregationID: aggregation.DefaultID,
   120  									StoragePolicies: policy.StoragePolicies{
   121  										policy.NewStoragePolicy(10*time.Second, xtime.Second, 12*time.Hour),
   122  										policy.NewStoragePolicy(time.Minute, xtime.Minute, 24*time.Hour),
   123  										policy.NewStoragePolicy(5*time.Minute, xtime.Minute, 48*time.Hour),
   124  									},
   125  								},
   126  							},
   127  						},
   128  					},
   129  					{
   130  						CutoverNanos: 23456,
   131  						Tombstoned:   false,
   132  						Metadata: metadata.Metadata{
   133  							Pipelines: []metadata.PipelineMetadata{
   134  								{
   135  									AggregationID: aggregation.DefaultID,
   136  									StoragePolicies: policy.StoragePolicies{
   137  										policy.NewStoragePolicy(30*time.Second, xtime.Second, 10*time.Hour),
   138  										policy.NewStoragePolicy(2*time.Minute, xtime.Minute, 48*time.Hour),
   139  									},
   140  								},
   141  							},
   142  						},
   143  					},
   144  				},
   145  			},
   146  			{
   147  				ID: b("rName2|rtagName1=rtagValue1"),
   148  				Metadatas: metadata.StagedMetadatas{
   149  					{
   150  						CutoverNanos: 12345,
   151  						Tombstoned:   false,
   152  						Metadata: metadata.Metadata{
   153  							Pipelines: []metadata.PipelineMetadata{
   154  								metadata.DefaultPipelineMetadata,
   155  							},
   156  						},
   157  					},
   158  				},
   159  			},
   160  			{
   161  				ID: b("rName3|rtagName1=rtagValue2"),
   162  				Metadatas: metadata.StagedMetadatas{
   163  					{
   164  						CutoverNanos: 12345,
   165  						Tombstoned:   false,
   166  						Metadata: metadata.Metadata{
   167  							Pipelines: []metadata.PipelineMetadata{
   168  								metadata.DefaultPipelineMetadata,
   169  							},
   170  						},
   171  					},
   172  					{
   173  						CutoverNanos: 27000,
   174  						Tombstoned:   true,
   175  						Metadata: metadata.Metadata{
   176  							Pipelines: []metadata.PipelineMetadata{
   177  								metadata.DefaultPipelineMetadata,
   178  							},
   179  						},
   180  					},
   181  				},
   182  			},
   183  		}
   184  	)
   185  
   186  	inputs := []struct {
   187  		matchAtNanos            int64
   188  		expectedForExistingID   metadata.StagedMetadatas
   189  		expectedForNewRollupIDs []IDWithMetadatas
   190  	}{
   191  		{
   192  			matchAtNanos:            0,
   193  			expectedForExistingID:   testForExistingID,
   194  			expectedForNewRollupIDs: testForNewRollupIDs,
   195  		},
   196  		{
   197  			matchAtNanos:            23455,
   198  			expectedForExistingID:   testForExistingID,
   199  			expectedForNewRollupIDs: testForNewRollupIDs,
   200  		},
   201  		{
   202  			matchAtNanos:          23456,
   203  			expectedForExistingID: testForExistingID[1:],
   204  			expectedForNewRollupIDs: []IDWithMetadatas{
   205  				{
   206  					ID:        testForNewRollupIDs[0].ID,
   207  					Metadatas: testForNewRollupIDs[0].Metadatas[1:],
   208  				},
   209  				testForNewRollupIDs[1],
   210  				testForNewRollupIDs[2],
   211  			},
   212  		},
   213  		{
   214  			matchAtNanos:          30000,
   215  			expectedForExistingID: testForExistingID[1:],
   216  			expectedForNewRollupIDs: []IDWithMetadatas{
   217  				{
   218  					ID:        testForNewRollupIDs[0].ID,
   219  					Metadatas: testForNewRollupIDs[0].Metadatas[1:],
   220  				},
   221  				testForNewRollupIDs[1],
   222  				{
   223  					ID:        testForNewRollupIDs[2].ID,
   224  					Metadatas: testForNewRollupIDs[2].Metadatas[1:],
   225  				},
   226  			},
   227  		},
   228  	}
   229  
   230  	res := NewMatchResult(0, testExpireAtNanos, testForExistingID, testForNewRollupIDs, false)
   231  	for _, input := range inputs {
   232  		require.Equal(t, input.expectedForExistingID, res.ForExistingIDAt(input.matchAtNanos))
   233  		require.Equal(t, len(input.expectedForNewRollupIDs), res.NumNewRollupIDs())
   234  		for i := 0; i < len(input.expectedForNewRollupIDs); i++ {
   235  			forNewRollupID := res.ForNewRollupIDsAt(i, input.matchAtNanos)
   236  			require.Equal(t, input.expectedForNewRollupIDs[i], forNewRollupID)
   237  		}
   238  	}
   239  }