github.com/m3db/m3@v1.5.0/src/cluster/placement/algo/non_sharded_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 algo 22 23 import ( 24 "testing" 25 26 "github.com/m3db/m3/src/cluster/placement" 27 28 "github.com/stretchr/testify/assert" 29 ) 30 31 func TestNonShardedAlgo(t *testing.T) { 32 a := newNonShardedAlgorithm() 33 34 i1 := placement.NewInstance().SetID("i1").SetEndpoint("e1") 35 i2 := placement.NewInstance().SetID("i2").SetEndpoint("e2") 36 i3 := placement.NewInstance().SetID("i3").SetEndpoint("e3") 37 i4 := placement.NewInstance().SetID("i4").SetEndpoint("e4") 38 _, err := a.InitialPlacement([]placement.Instance{i1, i2}, []uint32{1, 2}, 1) 39 assert.Error(t, err) 40 41 p, err := a.InitialPlacement([]placement.Instance{i1, i2}, []uint32{}, 1) 42 assert.NoError(t, err) 43 assert.NoError(t, placement.Validate(p)) 44 assert.Equal(t, 2, p.NumInstances()) 45 for _, instance := range p.Instances() { 46 assert.Equal(t, 0, instance.Shards().NumShards()) 47 } 48 assert.Equal(t, 0, p.NumShards()) 49 assert.Equal(t, 1, p.ReplicaFactor()) 50 assert.False(t, p.IsSharded()) 51 52 p, err = a.AddReplica(p) 53 assert.NoError(t, err) 54 assert.NoError(t, placement.Validate(p)) 55 assert.Equal(t, 2, p.NumInstances()) 56 for _, instance := range p.Instances() { 57 assert.Equal(t, 0, instance.Shards().NumShards()) 58 } 59 assert.Equal(t, 0, p.NumShards()) 60 assert.Equal(t, 2, p.ReplicaFactor()) 61 assert.False(t, p.IsSharded()) 62 63 p, err = a.AddInstances(p, []placement.Instance{i3}) 64 assert.NoError(t, err) 65 assert.NoError(t, placement.Validate(p)) 66 assert.Equal(t, 3, p.NumInstances()) 67 for _, instance := range p.Instances() { 68 assert.Equal(t, 0, instance.Shards().NumShards()) 69 } 70 assert.Equal(t, 0, p.NumShards()) 71 assert.Equal(t, 2, p.ReplicaFactor()) 72 assert.False(t, p.IsSharded()) 73 74 _, err = a.AddInstances(p, []placement.Instance{i3}) 75 assert.Error(t, err) 76 77 p, err = a.RemoveInstances(p, []string{"i1"}) 78 assert.NoError(t, err) 79 assert.NoError(t, placement.Validate(p)) 80 assert.Equal(t, 2, p.NumInstances()) 81 for _, instance := range p.Instances() { 82 assert.Equal(t, 0, instance.Shards().NumShards()) 83 } 84 assert.Equal(t, 0, p.NumShards()) 85 assert.Equal(t, 2, p.ReplicaFactor()) 86 assert.False(t, p.IsSharded()) 87 88 _, err = a.RemoveInstances(p, []string{"i1"}) 89 assert.Error(t, err) 90 91 p, err = a.ReplaceInstances(p, []string{"i2"}, []placement.Instance{i1, i4}) 92 assert.NoError(t, err) 93 assert.NoError(t, placement.Validate(p)) 94 assert.Equal(t, 3, p.NumInstances()) 95 for _, instance := range p.Instances() { 96 assert.Equal(t, 0, instance.Shards().NumShards()) 97 } 98 assert.Equal(t, 0, p.NumShards()) 99 assert.Equal(t, 2, p.ReplicaFactor()) 100 assert.False(t, p.IsSharded()) 101 102 balancedPlacement, err := a.BalanceShards(p) 103 assert.NoError(t, err) 104 assert.NoError(t, placement.Validate(balancedPlacement)) 105 } 106 107 func TestIncompatibleWithNonShardedAlgo(t *testing.T) { 108 i1 := placement.NewInstance().SetID("i1").SetEndpoint("e1").SetWeight(1) 109 i2 := placement.NewInstance().SetID("i2").SetEndpoint("e2").SetWeight(1) 110 i3 := placement.NewInstance().SetID("i3").SetEndpoint("e3").SetWeight(1) 111 i4 := placement.NewInstance().SetID("i4").SetEndpoint("e4").SetWeight(1) 112 p, err := newShardedAlgorithm(placement.NewOptions()).InitialPlacement([]placement.Instance{i1, i2}, []uint32{1, 2}, 1) 113 assert.NoError(t, err) 114 115 a := newNonShardedAlgorithm() 116 117 _, err = a.AddReplica(p) 118 assert.Error(t, err) 119 assert.Equal(t, errInCompatibleWithNonShardedAlgo, err) 120 121 _, err = a.AddInstances(p, []placement.Instance{i3}) 122 assert.Error(t, err) 123 assert.Equal(t, errInCompatibleWithNonShardedAlgo, err) 124 125 _, err = a.RemoveInstances(p, []string{"i1"}) 126 assert.Error(t, err) 127 assert.Equal(t, errInCompatibleWithNonShardedAlgo, err) 128 129 _, err = a.ReplaceInstances(p, []string{"i1"}, []placement.Instance{i3, i4}) 130 assert.Error(t, err) 131 assert.Equal(t, errInCompatibleWithNonShardedAlgo, err) 132 133 _, err = a.BalanceShards(p) 134 assert.Error(t, err) 135 assert.Equal(t, errInCompatibleWithNonShardedAlgo, err) 136 }