github.com/m3db/m3@v1.5.0/src/dbnode/topology/types.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 "github.com/m3db/m3/src/cluster/client" 25 "github.com/m3db/m3/src/cluster/services" 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 "github.com/m3db/m3/src/x/instrument" 30 ) 31 32 // Host is a container of a host in a topology 33 type Host interface { 34 // ID is the identifier of the host 35 ID() string 36 37 // Address returns the address of the host 38 Address() string 39 40 // String returns a string representation of the host 41 String() string 42 } 43 44 // HostShardSet is a container for a host and corresponding shard set 45 type HostShardSet interface { 46 // Host returns the host 47 Host() Host 48 49 // ShardSet returns the shard set owned by the host 50 ShardSet() sharding.ShardSet 51 } 52 53 // Initializer can init new instances of Topology 54 type Initializer interface { 55 // Init will return a new topology 56 Init() (Topology, error) 57 58 // TopologyIsSet returns whether the topology is able to be 59 // initialized immediately or if instead it will blockingly 60 // wait to be set on initialization 61 TopologyIsSet() (bool, error) 62 } 63 64 // Topology is a container of a topology map and disseminates topology map changes 65 type Topology interface { 66 // Get the topology map 67 Get() Map 68 69 // Watch for the topology map 70 Watch() (MapWatch, error) 71 72 // Close will close the topology map 73 Close() 74 } 75 76 // DynamicTopology is a topology that dynamically changes and as such 77 // adds functionality for a clustered database to call back and mark 78 // a shard as available once it completes bootstrapping 79 type DynamicTopology interface { 80 Topology 81 82 // MarkShardsAvailable marks a shard with the state of initializing as available 83 MarkShardsAvailable(instanceID string, shardIDs ...uint32) error 84 } 85 86 // MapWatch is a watch on a topology map 87 type MapWatch interface { 88 // C is the notification channel for when a value becomes available 89 C() <-chan struct{} 90 91 // Get the current topology map 92 Get() Map 93 94 // Close the watch on the topology map 95 Close() 96 } 97 98 // Map describes a topology 99 type Map interface { 100 // Hosts returns all hosts in the map 101 Hosts() []Host 102 103 // HostShardSets returns all HostShardSets in the map 104 HostShardSets() []HostShardSet 105 106 // LookupHostShardSet returns a HostShardSet for a host in the map 107 LookupHostShardSet(hostID string) (HostShardSet, bool) 108 109 // HostsLen returns the length of all hosts in the map 110 HostsLen() int 111 112 // ShardSet returns the shard set for the topology 113 ShardSet() sharding.ShardSet 114 115 // Route will route a given ID to a shard and a set of hosts 116 Route(id ident.ID) (uint32, []Host, error) 117 118 // RouteForEach will route a given ID to a shard then execute a 119 // function for each host in the set of routed hosts 120 RouteForEach(id ident.ID, forEachFn RouteForEachFn) error 121 122 // RouteShard will route a given shard to a set of hosts 123 RouteShard(shard uint32) ([]Host, error) 124 125 // RouteShardForEach will route a given shard and execute 126 // a function for each host in the set of routed hosts 127 RouteShardForEach(shard uint32, forEachFn RouteForEachFn) error 128 129 // Replicas returns the number of replicas in the topology 130 Replicas() int 131 132 // MajorityReplicas returns the number of replicas to establish majority in the topology 133 MajorityReplicas() int 134 } 135 136 // RouteForEachFn is a function to execute for each routed to host 137 type RouteForEachFn func(idx int, shard shard.Shard, host Host) 138 139 // StaticConfiguration is used for standing up M3DB with a static topology 140 type StaticConfiguration struct { 141 Shards int `yaml:"shards"` 142 Replicas int `yaml:"replicas"` 143 Hosts []HostShardConfig `yaml:"hosts"` 144 } 145 146 // HostShardConfig stores host information for fanout 147 type HostShardConfig struct { 148 HostID string `yaml:"hostID"` 149 ListenAddress string `yaml:"listenAddress"` 150 } 151 152 // StaticOptions is a set of options for static topology 153 type StaticOptions interface { 154 // Validate validates the options 155 Validate() error 156 157 // SetShardSet sets the ShardSet 158 SetShardSet(value sharding.ShardSet) StaticOptions 159 160 // ShardSet returns the ShardSet 161 ShardSet() sharding.ShardSet 162 163 // SetReplicas sets the replicas 164 SetReplicas(value int) StaticOptions 165 166 // Replicas returns the replicas 167 Replicas() int 168 169 // SetHostShardSets sets the hostShardSets 170 SetHostShardSets(value []HostShardSet) StaticOptions 171 172 // HostShardSets returns the hostShardSets 173 HostShardSets() []HostShardSet 174 } 175 176 // DynamicOptions is a set of options for dynamic topology 177 type DynamicOptions interface { 178 // Validate validates the options 179 Validate() error 180 181 // SetConfigServiceClient sets the client of ConfigService 182 SetConfigServiceClient(c client.Client) DynamicOptions 183 184 // ConfigServiceClient returns the client of ConfigService 185 ConfigServiceClient() client.Client 186 187 // SetServiceID sets the ServiceID for service discovery 188 SetServiceID(s services.ServiceID) DynamicOptions 189 190 // ServiceID returns the ServiceID for service discovery 191 ServiceID() services.ServiceID 192 193 // SetServicesOverrideOptions sets the override options for service discovery. 194 SetServicesOverrideOptions(opts services.OverrideOptions) DynamicOptions 195 196 // ServicesOverrideOptions returns the override options for service discovery. 197 ServicesOverrideOptions() services.OverrideOptions 198 199 // SetQueryOptions sets the ConfigService query options 200 SetQueryOptions(value services.QueryOptions) DynamicOptions 201 202 // QueryOptions returns the ConfigService query options 203 QueryOptions() services.QueryOptions 204 205 // SetInstrumentOptions sets the instrumentation options 206 SetInstrumentOptions(value instrument.Options) DynamicOptions 207 208 // InstrumentOptions returns the instrumentation options 209 InstrumentOptions() instrument.Options 210 211 // SetHashGen sets the HashGen function 212 SetHashGen(h sharding.HashGen) DynamicOptions 213 214 // HashGen returns HashGen function 215 HashGen() sharding.HashGen 216 } 217 218 // MapProvider is an interface that can provide 219 // a topology map. 220 type MapProvider interface { 221 // TopologyMap returns a topology map. 222 TopologyMap() (Map, error) 223 } 224 225 // StateSnapshot represents a snapshot of the state of the topology at a 226 // given moment. 227 type StateSnapshot struct { 228 Origin Host 229 MajorityReplicas int 230 ShardStates ShardStates 231 } 232 233 // ShardStates maps shard IDs to the state of each of the hosts that own 234 // that shard. 235 type ShardStates map[ShardID]map[HostID]HostShardState 236 237 // HostShardState contains the state of a shard as owned by a given host. 238 type HostShardState struct { 239 Host Host 240 ShardState shard.State 241 } 242 243 // HostID is the string representation of a host ID. 244 type HostID string 245 246 // ShardID is the ID of a shard. 247 type ShardID uint32