mirror of
https://github.com/featurebasedb/featurebase.git
synced 2026-10-16 15:53:35 +00:00
port mapper gives out ports from 63000-65000 for the tests fix another race http test uses port.MustGetPort rbf: remove :0 port request ocd happy test fix for grpc listener address already in use test/disco allocates BindGRPC port from the port mapper dump stack on each GetPort verify each port is usable right away server/config.go has Config.Validate() now panic if gossip port is 0. validate server.Config fix another gossip port 0 builds quiet, don't dump stack on each port alloc builds happy linter even gossip fallback should not be zero but rather use the port mapper
156 lines
3.3 KiB
Go
156 lines
3.3 KiB
Go
// Copyright 2017 Pilosa Corp.
|
|
//
|
|
// 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.
|
|
|
|
package topology
|
|
|
|
import (
|
|
"fmt"
|
|
"sync"
|
|
|
|
"github.com/pilosa/pilosa/v2/net"
|
|
)
|
|
|
|
// Node represents a node in the cluster.
|
|
type Node struct {
|
|
Mu sync.Mutex
|
|
|
|
ID string `json:"id"`
|
|
URI net.URI `json:"uri"`
|
|
GRPCURI net.URI `json:"grpc-uri"`
|
|
IsCoordinator bool `json:"isCoordinator"`
|
|
State string `json:"state"`
|
|
}
|
|
|
|
func (n *Node) ProtectedClone() *Node {
|
|
n.Mu.Lock()
|
|
defer n.Mu.Unlock()
|
|
return n.Clone()
|
|
}
|
|
|
|
func (n *Node) Clone() *Node {
|
|
if n == nil {
|
|
return nil
|
|
}
|
|
var other Node
|
|
other.ID = n.ID
|
|
other.URI = n.URI
|
|
other.GRPCURI = n.GRPCURI
|
|
other.IsCoordinator = n.IsCoordinator
|
|
other.State = n.State
|
|
return &other
|
|
}
|
|
|
|
func (n *Node) String() string {
|
|
return fmt.Sprintf("Node:%s:%s:%s", n.URI, n.State, n.ID)
|
|
}
|
|
|
|
// Nodes represents a list of nodes.
|
|
type Nodes []*Node
|
|
|
|
// Contains returns true if a node exists in the list.
|
|
func (a Nodes) Contains(n *Node) bool {
|
|
for i := range a {
|
|
if a[i] == n {
|
|
return true
|
|
}
|
|
}
|
|
return false
|
|
}
|
|
|
|
// ContainsID returns true if host matches one of the node's id.
|
|
func (a Nodes) ContainsID(id string) bool {
|
|
for _, n := range a {
|
|
if n.ID == id {
|
|
return true
|
|
}
|
|
}
|
|
return false
|
|
}
|
|
|
|
// NodeByID returns the node for an ID. If the ID is not found,
|
|
// it returns nil.
|
|
func (a Nodes) NodeByID(id string) *Node {
|
|
for _, n := range a {
|
|
if n.ID == id {
|
|
return n
|
|
}
|
|
}
|
|
return nil
|
|
}
|
|
|
|
// Filter returns a new list of nodes with node removed.
|
|
func (a Nodes) Filter(n *Node) []*Node {
|
|
other := make([]*Node, 0, len(a))
|
|
for i := range a {
|
|
if a[i] != n {
|
|
other = append(other, a[i])
|
|
}
|
|
}
|
|
return other
|
|
}
|
|
|
|
// FilterID returns a new list of nodes with ID removed.
|
|
func (a Nodes) FilterID(id string) []*Node {
|
|
other := make([]*Node, 0, len(a))
|
|
for _, node := range a {
|
|
if node.ID != id {
|
|
other = append(other, node)
|
|
}
|
|
}
|
|
return other
|
|
}
|
|
|
|
// FilterURI returns a new list of nodes with URI removed.
|
|
func (a Nodes) FilterURI(uri net.URI) []*Node {
|
|
other := make([]*Node, 0, len(a))
|
|
for _, node := range a {
|
|
if node.URI != uri {
|
|
other = append(other, node)
|
|
}
|
|
}
|
|
return other
|
|
}
|
|
|
|
// IDs returns a list of all node IDs.
|
|
func (a Nodes) IDs() []string {
|
|
ids := make([]string, len(a))
|
|
for i, n := range a {
|
|
ids[i] = n.ID
|
|
}
|
|
return ids
|
|
}
|
|
|
|
// URIs returns a list of all uris.
|
|
func (a Nodes) URIs() []net.URI {
|
|
uris := make([]net.URI, len(a))
|
|
for i, n := range a {
|
|
uris[i] = n.URI
|
|
}
|
|
return uris
|
|
}
|
|
|
|
// Clone returns a shallow copy of nodes.
|
|
func (a Nodes) Clone() []*Node {
|
|
other := make([]*Node, len(a))
|
|
copy(other, a)
|
|
return other
|
|
}
|
|
|
|
// ByID implements sort.Interface for []Node based on
|
|
// the ID field.
|
|
type ByID []*Node
|
|
|
|
func (h ByID) Len() int { return len(h) }
|
|
func (h ByID) Swap(i, j int) { h[i], h[j] = h[j], h[i] }
|
|
func (h ByID) Less(i, j int) bool { return h[i].ID < h[j].ID }
|