github.com/m3db/m3@v1.5.1-0.20231129193456-75a402aa583b/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 // LookupInitializingHostPair returns the initializing host bootstrapping from specific shard from leaving host. 110 LookupInitializingHostPair(leavingHostID string, id uint32) (string, bool) 111 112 // HostsLen returns the length of all hosts in the map 113 HostsLen() int 114 115 // ShardSet returns the shard set for the topology 116 ShardSet() sharding.ShardSet 117 118 // Route will route a given ID to a shard and a set of hosts 119 Route(id ident.ID) (uint32, []Host, error) 120 121 // RouteForEach will route a given ID to a shard then execute a 122 // function for each host in the set of routed hosts 123 RouteForEach(id ident.ID, forEachFn RouteForEachFn) error 124 125 // RouteShard will route a given shard to a set of hosts 126 RouteShard(shard uint32) ([]Host, error) 127 128 // RouteShardForEach will route a given shard and execute 129 // a function for each host in the set of routed hosts 130 RouteShardForEach(shard uint32, forEachFn RouteForEachFn) error 131 132 // Replicas returns the number of replicas in the topology 133 Replicas() int 134 135 // MajorityReplicas returns the number of replicas to establish majority in the topology 136 MajorityReplicas() int 137 } 138 139 // RouteForEachFn is a function to execute for each routed to host 140 type RouteForEachFn func(idx int, shard shard.Shard, host Host) 141 142 // StaticConfiguration is used for standing up M3DB with a static topology 143 type StaticConfiguration struct { 144 Shards int `yaml:"shards"` 145 Replicas int `yaml:"replicas"` 146 Hosts []HostShardConfig `yaml:"hosts"` 147 } 148 149 // HostShardConfig stores host information for fanout 150 type HostShardConfig struct { 151 HostID string `yaml:"hostID"` 152 ListenAddress string `yaml:"listenAddress"` 153 } 154 155 // StaticOptions is a set of options for static topology 156 type StaticOptions interface { 157 // Validate validates the options 158 Validate() error 159 160 // SetShardSet sets the ShardSet 161 SetShardSet(value sharding.ShardSet) StaticOptions 162 163 // ShardSet returns the ShardSet 164 ShardSet() sharding.ShardSet 165 166 // SetReplicas sets the replicas 167 SetReplicas(value int) StaticOptions 168 169 // Replicas returns the replicas 170 Replicas() int 171 172 // SetHostShardSets sets the hostShardSets 173 SetHostShardSets(value []HostShardSet) StaticOptions 174 175 // HostShardSets returns the hostShardSets 176 HostShardSets() []HostShardSet 177 } 178 179 // DynamicOptions is a set of options for dynamic topology 180 type DynamicOptions interface { 181 // Validate validates the options 182 Validate() error 183 184 // SetConfigServiceClient sets the client of ConfigService 185 SetConfigServiceClient(c client.Client) DynamicOptions 186 187 // ConfigServiceClient returns the client of ConfigService 188 ConfigServiceClient() client.Client 189 190 // SetServiceID sets the ServiceID for service discovery 191 SetServiceID(s services.ServiceID) DynamicOptions 192 193 // ServiceID returns the ServiceID for service discovery 194 ServiceID() services.ServiceID 195 196 // SetServicesOverrideOptions sets the override options for service discovery. 197 SetServicesOverrideOptions(opts services.OverrideOptions) DynamicOptions 198 199 // ServicesOverrideOptions returns the override options for service discovery. 200 ServicesOverrideOptions() services.OverrideOptions 201 202 // SetQueryOptions sets the ConfigService query options 203 SetQueryOptions(value services.QueryOptions) DynamicOptions 204 205 // QueryOptions returns the ConfigService query options 206 QueryOptions() services.QueryOptions 207 208 // SetInstrumentOptions sets the instrumentation options 209 SetInstrumentOptions(value instrument.Options) DynamicOptions 210 211 // InstrumentOptions returns the instrumentation options 212 InstrumentOptions() instrument.Options 213 214 // SetHashGen sets the HashGen function 215 SetHashGen(h sharding.HashGen) DynamicOptions 216 217 // HashGen returns HashGen function 218 HashGen() sharding.HashGen 219 } 220 221 // MapProvider is an interface that can provide 222 // a topology map. 223 type MapProvider interface { 224 // TopologyMap returns a topology map. 225 TopologyMap() (Map, error) 226 } 227 228 // StateSnapshot represents a snapshot of the state of the topology at a 229 // given moment. 230 type StateSnapshot struct { 231 Origin Host 232 MajorityReplicas int 233 ShardStates ShardStates 234 } 235 236 // ShardStates maps shard IDs to the state of each of the hosts that own 237 // that shard. 238 type ShardStates map[ShardID]map[HostID]HostShardState 239 240 // HostShardState contains the state of a shard as owned by a given host. 241 type HostShardState struct { 242 Host Host 243 ShardState shard.State 244 } 245 246 // HostID is the string representation of a host ID. 247 type HostID string 248 249 // ShardID is the ID of a shard. 250 type ShardID uint32