forked from cockroachdb/cockroach
-
Notifications
You must be signed in to change notification settings - Fork 0
/
node_set.go
103 lines (90 loc) · 2.95 KB
/
node_set.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
// Copyright 2015 The Cockroach Authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
// implied. See the License for the specific language governing
// permissions and limitations under the License.
//
// Author: Spencer Kimball (spencer.kimball@gmail.com)
package gossip
import (
"github.com/cockroachdb/cockroach/roachpb"
"github.com/cockroachdb/cockroach/util/metric"
)
// A nodeSet keeps a set of nodes and provides simple node-matched
// management functions. nodeSet is not thread safe.
type nodeSet struct {
nodes map[roachpb.NodeID]struct{} // Set of roachpb.NodeID
maxSize int // Maximum size of set
gauge *metric.Gauge // Gauge for the number of nodes in the set.
}
func makeNodeSet(maxSize int, gauge *metric.Gauge) nodeSet {
return nodeSet{
nodes: make(map[roachpb.NodeID]struct{}),
maxSize: maxSize,
gauge: gauge,
}
}
// hasSpace returns whether there are fewer than maxSize nodes
// in the nodes slice.
func (as nodeSet) hasSpace() bool {
return len(as.nodes) < as.maxSize
}
// len returns the number of nodes in the set.
func (as nodeSet) len() int {
return len(as.nodes)
}
// asSlice returns the nodes as a slice.
func (as nodeSet) asSlice() []roachpb.NodeID {
slice := make([]roachpb.NodeID, 0, len(as.nodes))
for node := range as.nodes {
slice = append(slice, node)
}
return slice
}
// filter returns an nodeSet of nodes which return true when passed to the
// supplied filter function filterFn. filterFn should return true to keep an
// node and false to remove an node. The new nodeSet has a separate gauge object
// from the parent.
func (as nodeSet) filter(filterFn func(node roachpb.NodeID) bool) nodeSet {
avail := makeNodeSet(as.maxSize,
metric.NewGauge(metric.Metadata{Name: "TODO(marc)", Help: "TODO(marc)"}))
for node := range as.nodes {
if filterFn(node) {
avail.addNode(node)
}
}
return avail
}
// hasNode verifies that the supplied node matches a node
// in the slice.
func (as nodeSet) hasNode(node roachpb.NodeID) bool {
_, ok := as.nodes[node]
return ok
}
// setMaxSize adjusts the maximum size allowed for the node set.
func (as *nodeSet) setMaxSize(maxSize int) {
if as.maxSize != maxSize {
as.maxSize = maxSize
}
}
// addNode adds the node to the nodes set.
func (as *nodeSet) addNode(node roachpb.NodeID) {
as.nodes[node] = struct{}{}
as.updateGauge()
}
// removeNode removes the node from the nodes set.
func (as *nodeSet) removeNode(node roachpb.NodeID) {
delete(as.nodes, node)
as.updateGauge()
}
func (as *nodeSet) updateGauge() {
as.gauge.Update(int64(len(as.nodes)))
}