github.com/m3db/m3@v1.5.1-0.20231129193456-75a402aa583b/src/dbnode/topology/host.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 "errors" 25 "fmt" 26 "math" 27 28 "github.com/m3db/m3/src/cluster/services" 29 "github.com/m3db/m3/src/cluster/shard" 30 "github.com/m3db/m3/src/dbnode/sharding" 31 ) 32 33 var errInstanceHasNoShardsAssignment = errors.New("invalid instance with no shards assigned") 34 35 // Majority returns the majority required to successfully return when 36 // querying a majority of a set of replicas 37 func Majority(replicas int) int { 38 return int(math.Ceil(0.5 * float64(replicas+1))) 39 } 40 41 type host struct { 42 id string 43 address string 44 } 45 46 func (h *host) ID() string { 47 return h.id 48 } 49 50 func (h *host) Address() string { 51 return h.address 52 } 53 54 func (h *host) String() string { 55 return fmt.Sprintf("Host<ID=%s, Address=%s>", h.id, h.address) 56 } 57 58 // NewHost creates a new host 59 func NewHost(id, address string) Host { 60 return &host{id: id, address: address} 61 } 62 63 type hostShardSet struct { 64 host Host 65 shardSet sharding.ShardSet 66 } 67 68 // NewHostShardSet creates a new host shard set 69 func NewHostShardSet(host Host, shardSet sharding.ShardSet) HostShardSet { 70 return &hostShardSet{host, shardSet} 71 } 72 73 // NewHostShardSetFromServiceInstance creates a new 74 // host shard set derived from a service instance 75 func NewHostShardSetFromServiceInstance( 76 si services.ServiceInstance, 77 fn sharding.HashFn, 78 ) (HostShardSet, error) { 79 if si.Shards() == nil { 80 return nil, errInstanceHasNoShardsAssignment 81 } 82 all := si.Shards().All() 83 shards := make([]shard.Shard, len(all)) 84 copy(shards, all) 85 shardSet, err := sharding.NewShardSet(shards, fn) 86 if err != nil { 87 return nil, err 88 } 89 return NewHostShardSet(NewHost(si.InstanceID(), si.Endpoint()), shardSet), nil 90 } 91 92 func (h *hostShardSet) Host() Host { 93 return h.host 94 } 95 96 func (h *hostShardSet) ShardSet() sharding.ShardSet { 97 return h.shardSet 98 }