forked from cockroachdb/cockroach
/
node_id.go
75 lines (65 loc) · 2.18 KB
/
node_id.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
// Copyright 2016 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: Radu Berinde (radu@cockroachlabs.com)
package base
import (
"strconv"
"sync/atomic"
"golang.org/x/net/context"
"github.com/cockroachdb/cockroach/pkg/roachpb"
"github.com/cockroachdb/cockroach/pkg/util"
"github.com/cockroachdb/cockroach/pkg/util/log"
)
// NodeIDContainer is used to share a single roachpb.NodeID instance between
// multiple layers. It allows setting and getting the value. Once a value is
// set, the value cannot change.
type NodeIDContainer struct {
noCopy util.NoCopy
// nodeID is atomically updated under the mutex; it can be read atomically
// without the mutex.
nodeID int32
}
// String returns the node ID, or "?" if it is unset.
func (n *NodeIDContainer) String() string {
val := n.Get()
if val == 0 {
return "?"
}
return strconv.Itoa(int(val))
}
// Get returns the current node ID; 0 if it is unset.
func (n *NodeIDContainer) Get() roachpb.NodeID {
return roachpb.NodeID(atomic.LoadInt32(&n.nodeID))
}
// Set sets the current node ID. If it is already set, the value must match.
func (n *NodeIDContainer) Set(ctx context.Context, val roachpb.NodeID) {
if val <= 0 {
log.Fatalf(ctx, "trying to set invalid NodeID: %d", val)
}
oldVal := atomic.SwapInt32(&n.nodeID, int32(val))
if oldVal == 0 {
if log.V(2) {
log.Infof(ctx, "NodeID set to %d", val)
}
} else if oldVal != int32(val) {
log.Fatalf(ctx, "different NodeIDs set: %d, then %d", oldVal, val)
}
}
// Reset changes the NodeID regardless of the old value.
//
// Should only be used in testing code.
func (n *NodeIDContainer) Reset(val roachpb.NodeID) {
atomic.StoreInt32(&n.nodeID, int32(val))
}