-
Notifications
You must be signed in to change notification settings - Fork 453
/
map.go
159 lines (136 loc) · 4.41 KB
/
map.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
// Copyright (c) 2016 Uber Technologies, Inc.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
package topology
import (
"github.com/m3db/m3/src/dbnode/sharding"
"github.com/m3db/m3/src/x/ident"
xwatch "github.com/m3db/m3/src/x/watch"
)
type staticMap struct {
shardSet sharding.ShardSet
hostShardSets []HostShardSet
hostShardSetsByID map[string]HostShardSet
orderedHosts []Host
hostsByShard [][]Host
orderedHostsByShard [][]orderedHost
replicas int
majority int
}
// NewStaticMap creates a new static topology map
func NewStaticMap(opts StaticOptions) Map {
totalShards := len(opts.ShardSet().AllIDs())
hostShardSets := opts.HostShardSets()
topoMap := staticMap{
shardSet: opts.ShardSet(),
hostShardSets: hostShardSets,
hostShardSetsByID: make(map[string]HostShardSet),
orderedHosts: make([]Host, 0, len(hostShardSets)),
hostsByShard: make([][]Host, totalShards),
orderedHostsByShard: make([][]orderedHost, totalShards),
replicas: opts.Replicas(),
majority: Majority(opts.Replicas()),
}
for idx, hostShardSet := range hostShardSets {
host := hostShardSet.Host()
topoMap.hostShardSetsByID[host.ID()] = hostShardSet
topoMap.orderedHosts = append(topoMap.orderedHosts, host)
for _, shard := range hostShardSet.ShardSet().AllIDs() {
topoMap.hostsByShard[shard] = append(topoMap.hostsByShard[shard], host)
topoMap.orderedHostsByShard[shard] = append(topoMap.orderedHostsByShard[shard], orderedHost{
idx: idx,
host: host,
})
}
}
return &topoMap
}
type orderedHost struct {
idx int
host Host
}
func (t *staticMap) Hosts() []Host {
return t.orderedHosts
}
func (t *staticMap) HostShardSets() []HostShardSet {
return t.hostShardSets
}
func (t *staticMap) LookupHostShardSet(id string) (HostShardSet, bool) {
value, ok := t.hostShardSetsByID[id]
return value, ok
}
func (t *staticMap) HostsLen() int {
return len(t.orderedHosts)
}
func (t *staticMap) ShardSet() sharding.ShardSet {
return t.shardSet
}
func (t *staticMap) Route(id ident.ID) (uint32, []Host, error) {
shard := t.shardSet.Lookup(id)
if int(shard) >= len(t.hostsByShard) {
return shard, nil, errUnownedShard
}
return shard, t.hostsByShard[shard], nil
}
func (t *staticMap) RouteForEach(id ident.ID, forEachFn RouteForEachFn) error {
return t.RouteShardForEach(t.shardSet.Lookup(id), forEachFn)
}
func (t *staticMap) RouteShard(shard uint32) ([]Host, error) {
if int(shard) >= len(t.hostsByShard) {
return nil, errUnownedShard
}
return t.hostsByShard[shard], nil
}
func (t *staticMap) RouteShardForEach(shard uint32, forEachFn RouteForEachFn) error {
if int(shard) >= len(t.orderedHostsByShard) {
return errUnownedShard
}
orderedHosts := t.orderedHostsByShard[shard]
for i := range orderedHosts {
forEachFn(orderedHosts[i].idx, orderedHosts[i].host)
}
return nil
}
func (t *staticMap) Replicas() int {
return t.replicas
}
func (t *staticMap) MajorityReplicas() int {
return t.majority
}
type mapWatch struct {
xwatch.Watch
}
// NewMapWatch creates a new watch on a topology map
// from a generic watch that watches a Map
func NewMapWatch(w xwatch.Watch) MapWatch {
return &mapWatch{w}
}
func (w *mapWatch) C() <-chan struct{} {
return w.Watch.C()
}
func (w *mapWatch) Get() Map {
value := w.Watch.Get()
if value == nil {
return nil
}
return value.(Map)
}
func (w *mapWatch) Close() {
w.Watch.Close()
}