github.com/m3db/m3@v1.5.0/src/dbnode/topology/map_test.go (about) 1 // Copyright (c) 2016 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 topology 22 23 import ( 24 "testing" 25 26 "github.com/m3db/m3/src/cluster/shard" 27 "github.com/m3db/m3/src/dbnode/sharding" 28 "github.com/m3db/m3/src/x/ident" 29 30 "github.com/stretchr/testify/assert" 31 "github.com/stretchr/testify/require" 32 ) 33 34 func newTestShardSet( 35 t *testing.T, 36 shards []testShard, 37 hashFn sharding.HashFn, 38 ) sharding.ShardSet { 39 var values []shard.Shard 40 for _, elem := range shards { 41 value := shard.NewShard(elem.id).SetState(elem.state) 42 values = append(values, value) 43 } 44 shardSet, err := sharding.NewShardSet(values, hashFn) 45 require.NoError(t, err) 46 return shardSet 47 } 48 49 type testShard struct { 50 id uint32 51 state shard.State 52 } 53 54 type testShards []testShard 55 56 func (s testShards) IDs() []uint32 { 57 var ids []uint32 58 for _, elem := range s { 59 ids = append(ids, elem.id) 60 } 61 return ids 62 } 63 64 func TestStaticMap(t *testing.T) { 65 hashFn := func(id ident.ID) uint32 { 66 switch id.String() { 67 case "foo": 68 return 0 69 case "bar": 70 return 1 71 case "unowned": 72 return 999 73 default: 74 return 2 75 } 76 } 77 78 hosts := []struct { 79 id string 80 addr string 81 shards testShards 82 }{ 83 {"h1", "h1:9000", []testShard{{id: 0, state: shard.Available}}}, 84 {"h2", "h2:9000", []testShard{{id: 1, state: shard.Available}}}, 85 {"h3", "h3:9000", []testShard{{id: 0, state: shard.Available}}}, 86 {"h4", "h4:9000", []testShard{{id: 1, state: shard.Initializing}}}, 87 } 88 89 var hostShardSets []HostShardSet 90 for _, h := range hosts { 91 hostShardSets = append(hostShardSets, 92 NewHostShardSet( 93 NewHost(h.id, h.addr), 94 newTestShardSet(t, h.shards, hashFn))) 95 } 96 97 seedShardSet := newTestShardSet(t, []testShard{ 98 {id: 0, state: shard.Available}, 99 {id: 1, state: shard.Available}, 100 }, hashFn) 101 opts := NewStaticOptions(). 102 SetShardSet(seedShardSet). 103 SetReplicas(2). 104 SetHostShardSets(hostShardSets) 105 106 m := NewStaticMap(opts) 107 108 require.Equal(t, 4, len(m.Hosts())) 109 require.Equal(t, 4, m.HostsLen()) 110 for i, h := range hosts { 111 assert.Equal(t, h.id, m.Hosts()[i].ID()) 112 assert.Equal(t, h.addr, m.Hosts()[i].Address()) 113 } 114 115 require.Equal(t, 4, len(m.HostShardSets())) 116 for i, h := range hosts { 117 assert.Equal(t, h.id, m.HostShardSets()[i].Host().ID()) 118 assert.Equal(t, h.addr, m.HostShardSets()[i].Host().Address()) 119 assert.Equal(t, h.shards.IDs(), m.HostShardSets()[i].ShardSet().AllIDs()) 120 } 121 122 targetShard, targetHosts, err := m.Route(ident.StringID("foo")) 123 require.NoError(t, err) 124 assert.Equal(t, uint32(0), targetShard) 125 require.Equal(t, 2, len(targetHosts)) 126 assert.Equal(t, "h1", targetHosts[0].ID()) 127 assert.Equal(t, "h3", targetHosts[1].ID()) 128 129 _, _, err = m.Route(ident.StringID("unowned")) 130 require.Error(t, err) 131 assert.Equal(t, errUnownedShard, err) 132 133 targetHosts, err = m.RouteShard(1) 134 require.NoError(t, err) 135 require.Equal(t, 2, len(targetHosts)) 136 assert.Equal(t, "h2", targetHosts[0].ID()) 137 assert.Equal(t, "h4", targetHosts[1].ID()) 138 139 _, err = m.RouteShard(999) 140 require.Error(t, err) 141 assert.Equal(t, errUnownedShard, err) 142 143 err = m.RouteForEach(ident.StringID("bar"), func( 144 idx int, 145 s shard.Shard, 146 h Host, 147 ) { 148 switch idx { 149 case 1: 150 assert.Equal(t, "h2", h.ID()) 151 assert.Equal(t, shard.Available, s.State()) 152 case 3: 153 assert.Equal(t, "h4", h.ID()) 154 assert.Equal(t, shard.Initializing, s.State()) 155 default: 156 assert.Fail(t, "routed to wrong host") 157 } 158 }) 159 assert.NoError(t, err) 160 161 err = m.RouteForEach(ident.StringID("unowned"), func(_ int, _ shard.Shard, _ Host) {}) 162 require.Error(t, err) 163 assert.Equal(t, errUnownedShard, err) 164 165 assert.Equal(t, 2, m.Replicas()) 166 assert.Equal(t, 2, m.MajorityReplicas()) 167 }