github.com/m3db/m3@v1.5.1-0.20231129193456-75a402aa583b/src/dbnode/client/aggregate_results_accumulator_merge_test.go (about)

     1  // Copyright (c) 2019 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 client
    22  
    23  import (
    24  	"testing"
    25  
    26  	"github.com/m3db/m3/src/cluster/shard"
    27  	"github.com/m3db/m3/src/dbnode/topology"
    28  	"github.com/m3db/m3/src/dbnode/topology/testutil"
    29  	xtime "github.com/m3db/m3/src/x/time"
    30  
    31  	"github.com/stretchr/testify/require"
    32  )
    33  
    34  var (
    35  	testAggregateTimeUnit = xtime.Millisecond
    36  )
    37  
    38  func TestAggregateResultsAccumulatorIdsMerge(t *testing.T) {
    39  	// rf=3, 30 shards total; 10 shards shared between each pair
    40  	topoMap := testutil.MustNewTopologyMap(3, map[string][]shard.Shard{
    41  		"testhost0": testutil.ShardsRange(0, 19, shard.Available),
    42  		"testhost1": testutil.ShardsRange(10, 29, shard.Available),
    43  		"testhost2": append(testutil.ShardsRange(0, 9, shard.Available),
    44  			testutil.ShardsRange(20, 29, shard.Available)...),
    45  	})
    46  
    47  	th := newTestFetchTaggedHelper(t)
    48  	s := testSerieses{
    49  		newTestSeriesWithInstance("sphere/m3-cluster-short-rep0-2"),
    50  		newTestSeriesWithInstance("sphere/m3-cluster-short-rep0-3"),
    51  		newTestSeriesWithInstance("sphere/m3-cluster-short-rep1-9"),
    52  		newTestSeriesWithInstance("sphere/m3-cluster-short-rep0-11"),
    53  		newTestSeriesWithInstance("sphere/m3-cluster-short-rep1-2"),
    54  		newTestSeriesWithInstance("sphere/m3-cluster-short-rep2-15"),
    55  	}
    56  	workflow := testFetchStateWorkflow{
    57  		t:         t,
    58  		topoMap:   topoMap,
    59  		level:     topology.ReadConsistencyLevelAll,
    60  		startTime: testStartTime,
    61  		endTime:   testEndTime,
    62  
    63  		steps: []testFetchStateWorklowStep{
    64  			{
    65  				hostname:        "testhost0",
    66  				aggregateResult: testSerieses{s[0], s[2], s[4]}.toRPCAggResult(true),
    67  			},
    68  			{
    69  				hostname:        "testhost1",
    70  				aggregateResult: testSerieses{s[1], s[3]}.toRPCAggResult(true),
    71  			},
    72  			{
    73  				hostname:        "testhost2",
    74  				aggregateResult: testSerieses{s[5]}.toRPCAggResult(true),
    75  				expectedDone:    true,
    76  			},
    77  		},
    78  	}
    79  
    80  	accum := workflow.run()
    81  
    82  	// not really restricting, ensuring we don't have extra results
    83  	resultsIter, resultsMetadata, err := accum.AsAggregatedTagsIterator(1000, th.pools)
    84  	require.NoError(t, err)
    85  	require.True(t, resultsMetadata.Exhaustive)
    86  	s.assertMatchesAggregatedTagsIter(t, resultsIter)
    87  }
    88  
    89  func TestAggregateResultsAccumulatorIdsMergeUnstrictMajority(t *testing.T) {
    90  	// rf=3, 3 identical hosts, with same shards
    91  	topoMap := testutil.MustNewTopologyMap(3, map[string][]shard.Shard{
    92  		"testhost0": testutil.ShardsRange(0, 29, shard.Available),
    93  		"testhost1": testutil.ShardsRange(0, 29, shard.Available),
    94  		"testhost2": testutil.ShardsRange(0, 29, shard.Available),
    95  	})
    96  
    97  	th := newTestFetchTaggedHelper(t)
    98  	workflow := testFetchStateWorkflow{
    99  		t:         t,
   100  		topoMap:   topoMap,
   101  		level:     topology.ReadConsistencyLevelUnstrictMajority,
   102  		startTime: testStartTime,
   103  		endTime:   testEndTime,
   104  		steps: []testFetchStateWorklowStep{
   105  			{
   106  				hostname:        "testhost0",
   107  				aggregateResult: newTestSerieses(1, 10).toRPCAggResult(true),
   108  			},
   109  			{
   110  				hostname:        "testhost1",
   111  				aggregateResult: newTestSerieses(5, 15).toRPCAggResult(true),
   112  				expectedDone:    true,
   113  			},
   114  		},
   115  	}
   116  	accum := workflow.run()
   117  
   118  	resultsIter, resultsMetadata, err := accum.AsAggregatedTagsIterator(10, th.pools)
   119  	require.NoError(t, err)
   120  	require.False(t, resultsMetadata.Exhaustive)
   121  	newTestSerieses(1, 15).assertMatchesLimitedAggregatedTagsIter(t, 10, resultsIter)
   122  }
   123  
   124  func TestAggregateResultsAccumulatorIdsMergeReportsExhaustiveCorrectly(t *testing.T) {
   125  	// rf=3, 3 identical hosts, with same shards
   126  	topoMap := testutil.MustNewTopologyMap(3, map[string][]shard.Shard{
   127  		"testhost0": testutil.ShardsRange(0, 29, shard.Available),
   128  		"testhost1": testutil.ShardsRange(0, 29, shard.Available),
   129  		"testhost2": testutil.ShardsRange(0, 29, shard.Available),
   130  	})
   131  
   132  	th := newTestFetchTaggedHelper(t)
   133  	workflow := testFetchStateWorkflow{
   134  		t:         t,
   135  		topoMap:   topoMap,
   136  		level:     topology.ReadConsistencyLevelUnstrictMajority,
   137  		startTime: testStartTime,
   138  		endTime:   testEndTime,
   139  		steps: []testFetchStateWorklowStep{
   140  			{
   141  				hostname:        "testhost0",
   142  				aggregateResult: newTestSerieses(1, 10).toRPCAggResult(false),
   143  			},
   144  			{
   145  				hostname:        "testhost1",
   146  				aggregateResult: newTestSerieses(5, 15).toRPCAggResult(true),
   147  				expectedDone:    true,
   148  			},
   149  		},
   150  	}
   151  	accum := workflow.run()
   152  
   153  	resultsIter, resultsMetadata, err := accum.AsAggregatedTagsIterator(100, th.pools)
   154  	require.NoError(t, err)
   155  	require.False(t, resultsMetadata.Exhaustive)
   156  	newTestSerieses(1, 15).assertMatchesAggregatedTagsIter(t, resultsIter)
   157  }