github.com/m3db/m3@v1.5.0/src/aggregator/sharding/hash_test.go (about)

     1  // Copyright (c) 2017 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 sharding
    22  
    23  import (
    24  	"sync"
    25  	"testing"
    26  
    27  	"github.com/m3db/m3/src/metrics/metric/id"
    28  
    29  	"github.com/stretchr/testify/require"
    30  	yaml "gopkg.in/yaml.v2"
    31  )
    32  
    33  func TestHashTypeUnmarshalYAML(t *testing.T) {
    34  	inputs := []struct {
    35  		str      string
    36  		expected HashType
    37  	}{
    38  		{str: "murmur32", expected: Murmur32Hash},
    39  	}
    40  	for _, input := range inputs {
    41  		var hashType HashType
    42  		require.NoError(t, yaml.Unmarshal([]byte(input.str), &hashType))
    43  		require.Equal(t, input.expected, hashType)
    44  	}
    45  }
    46  
    47  func TestHashTypeUnmarshalYAMLErrors(t *testing.T) {
    48  	inputs := []string{
    49  		"huh",
    50  		"zero",
    51  	}
    52  	for _, input := range inputs {
    53  		var hashType HashType
    54  		err := yaml.Unmarshal([]byte(input), &hashType)
    55  		require.Error(t, err)
    56  		require.Equal(t, "invalid hash type '"+input+"' valid types are: murmur32", err.Error())
    57  	}
    58  }
    59  
    60  func TestMurmur32HashShardFn(t *testing.T) {
    61  	hashType := Murmur32Hash
    62  	numShards := uint32(1024)
    63  	shardFn, err := hashType.ShardFn()
    64  	require.NoError(t, err)
    65  
    66  	inputs := []struct {
    67  		data     []byte
    68  		expected uint32
    69  	}{
    70  		{data: []byte("foo"), expected: 32},
    71  		{data: []byte("bar"), expected: 397},
    72  		{data: []byte("baz"), expected: 234},
    73  	}
    74  	for _, input := range inputs {
    75  		require.Equal(t, input.expected, shardFn(input.data, numShards))
    76  	}
    77  }
    78  
    79  func TestMurmur32HashAggregatedShardFn(t *testing.T) {
    80  	hashType := Murmur32Hash
    81  	numShards := 1024
    82  	aggregatedShardFn, err := hashType.AggregatedShardFn()
    83  	require.NoError(t, err)
    84  
    85  	// Verify the aggregated shard function is thread-safe and the computed
    86  	// shards match expectation.
    87  	var wg sync.WaitGroup
    88  	numWorkers := 100
    89  	inputs := []struct {
    90  		data     id.ChunkedID
    91  		expected uint32
    92  	}{
    93  		{
    94  			data:     id.ChunkedID{Prefix: []byte(""), Data: []byte("bar"), Suffix: []byte("")},
    95  			expected: 397,
    96  		},
    97  		{
    98  			data:     id.ChunkedID{Prefix: []byte("foo"), Data: []byte("bar"), Suffix: []byte("")},
    99  			expected: 189,
   100  		},
   101  		{
   102  			data:     id.ChunkedID{Prefix: []byte(""), Data: []byte("bar"), Suffix: []byte("baz")},
   103  			expected: 350,
   104  		},
   105  		{
   106  			data:     id.ChunkedID{Prefix: []byte("foo"), Data: []byte("bar"), Suffix: []byte("baz")},
   107  			expected: 950,
   108  		},
   109  	}
   110  	for i := 0; i < numWorkers; i++ {
   111  		wg.Add(1)
   112  		go func() {
   113  			defer wg.Done()
   114  
   115  			for _, input := range inputs {
   116  				require.Equal(t, input.expected, aggregatedShardFn(input.data, numShards))
   117  			}
   118  		}()
   119  	}
   120  	wg.Wait()
   121  }
   122  
   123  func TestZeroHashAggregatedShardFn(t *testing.T) {
   124  	hashType := zeroHash
   125  	numShards := 1024
   126  	fn, err := hashType.AggregatedShardFn()
   127  	require.NoError(t, err)
   128  
   129  	inputs := []id.ChunkedID{
   130  		{Prefix: []byte(""), Data: []byte("bar"), Suffix: []byte("")},
   131  		{Prefix: []byte("foo"), Data: []byte("bar"), Suffix: []byte("")},
   132  		{Prefix: []byte(""), Data: []byte("bar"), Suffix: []byte("baz")},
   133  		{Prefix: []byte("foo"), Data: []byte("bar"), Suffix: []byte("baz")},
   134  	}
   135  	for _, input := range inputs {
   136  		require.Equal(t, uint32(0), fn(input, numShards))
   137  	}
   138  }