-
Notifications
You must be signed in to change notification settings - Fork 3.9k
/
Copy pathcluster.go
192 lines (166 loc) · 5.8 KB
/
cluster.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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
// Copyright 2020 The Cockroach Authors.
//
// Use of this software is governed by the Business Source License
// included in the file licenses/BSL.txt.
//
// As of the Change Date specified in that file, in accordance with
// the Business Source License, use of this software will be governed
// by the Apache License, Version 2.0, included in the file
// licenses/APL.txt.
// Package upgradecluster provides implementations of upgrade.Cluster.
package upgradecluster
import (
"context"
"github.com/cockroachdb/cockroach/pkg/keys"
"github.com/cockroachdb/cockroach/pkg/kv"
"github.com/cockroachdb/cockroach/pkg/kv/kvserver/liveness/livenesspb"
"github.com/cockroachdb/cockroach/pkg/roachpb"
"github.com/cockroachdb/cockroach/pkg/rpc"
"github.com/cockroachdb/cockroach/pkg/server/serverpb"
"github.com/cockroachdb/cockroach/pkg/util/ctxgroup"
"github.com/cockroachdb/cockroach/pkg/util/log"
"github.com/cockroachdb/cockroach/pkg/util/quotapool"
"github.com/cockroachdb/errors"
"github.com/cockroachdb/redact"
"google.golang.org/grpc"
)
// Cluster mediates interacting with a cockroach cluster.
type Cluster struct {
c ClusterConfig
}
// ClusterConfig configures a Cluster.
type ClusterConfig struct {
// NodeLiveness is used to determine the set of nodes in the cluster.
NodeLiveness NodeLiveness
// Dialer constructs connections to other nodes.
Dialer NodeDialer
// DB provides access the kv.DB instance backing the cluster.
//
// TODO(irfansharif): We could hide the kv.DB instance behind an interface
// to expose only relevant, vetted bits of kv.DB. It'll make our tests less
// "integration-ey".
DB *kv.DB
}
// NodeDialer abstracts connecting to other nodes in the cluster.
type NodeDialer interface {
// Dial returns a grpc connection to the given node.
Dial(context.Context, roachpb.NodeID, rpc.ConnectionClass) (*grpc.ClientConn, error)
}
// NodeLiveness is the subset of the interface satisfied by CRDB's node liveness
// component that the upgrade manager relies upon.
type NodeLiveness interface {
GetLivenessesFromKV(context.Context) ([]livenesspb.Liveness, error)
IsLive(roachpb.NodeID) (bool, error)
}
// New constructs a new Cluster with the provided dependencies.
func New(cfg ClusterConfig) *Cluster {
return &Cluster{c: cfg}
}
// UntilClusterStable is part of the upgrade.Cluster interface.
func (c *Cluster) UntilClusterStable(ctx context.Context, fn func() error) error {
ns, err := NodesFromNodeLiveness(ctx, c.c.NodeLiveness)
if err != nil {
return err
}
for {
if err := fn(); err != nil {
return err
}
curNodes, err := NodesFromNodeLiveness(ctx, c.c.NodeLiveness)
if err != nil {
return err
}
if ok, diffs := ns.Identical(curNodes); !ok {
log.Infof(ctx, "%s, retrying", diffs)
ns = curNodes
continue
}
break
}
return nil
}
// NumNodes is part of the upgrade.Cluster interface.
func (c *Cluster) NumNodes(ctx context.Context) (int, error) {
ns, err := NodesFromNodeLiveness(ctx, c.c.NodeLiveness)
if err != nil {
return 0, err
}
return len(ns), nil
}
// ForEveryNode is part of the upgrade.Cluster interface.
func (c *Cluster) ForEveryNode(
ctx context.Context, op string, fn func(context.Context, serverpb.MigrationClient) error,
) error {
ns, err := NodesFromNodeLiveness(ctx, c.c.NodeLiveness)
if err != nil {
return err
}
// We'll want to rate limit outgoing RPCs (limit pulled out of thin air).
qp := quotapool.NewIntPool("every-node", 25)
log.Infof(ctx, "executing %s on nodes %s", redact.Safe(op), ns)
grp := ctxgroup.WithContext(ctx)
for _, node := range ns {
id := node.ID // copy out of the loop variable
alloc, err := qp.Acquire(ctx, 1)
if err != nil {
return err
}
grp.GoCtx(func(ctx context.Context) error {
defer alloc.Release()
conn, err := c.c.Dialer.Dial(ctx, id, rpc.DefaultClass)
if err != nil {
return err
}
client := serverpb.NewMigrationClient(conn)
return fn(ctx, client)
})
}
return grp.Wait()
}
// IterateRangeDescriptors is part of the upgrade.Cluster interface.
func (c *Cluster) IterateRangeDescriptors(
ctx context.Context, blockSize int, init func(), fn func(...roachpb.RangeDescriptor) error,
) error {
return c.c.DB.Txn(ctx, func(ctx context.Context, txn *kv.Txn) error {
// Inform the caller that we're starting a fresh attempt to page in
// range descriptors.
init()
// Iterate through meta{1,2} to pull out all the range descriptors.
var lastRangeIDInMeta1 roachpb.RangeID
return txn.Iterate(ctx, keys.MetaMin, keys.MetaMax, blockSize,
func(rows []kv.KeyValue) error {
descriptors := make([]roachpb.RangeDescriptor, 0, len(rows))
var desc roachpb.RangeDescriptor
for _, row := range rows {
err := row.ValueProto(&desc)
if err != nil {
return errors.Wrapf(err, "unable to unmarshal range descriptor from %s", row.Key)
}
// In small enough clusters it's possible for the same range
// descriptor to be stored in both meta1 and meta2. This
// happens when some range spans both the meta and the user
// keyspace. Consider when r1 is [/Min,
// /System/NodeLiveness); we'll store the range descriptor
// in both /Meta2/<r1.EndKey> and in /Meta1/KeyMax[1].
//
// As part of iterator we'll de-duplicate this descriptor
// away by checking whether we've seen it before in meta1.
// Since we're scanning over the meta range in sorted
// order, it's enough to check against the last range
// descriptor we've seen in meta1.
//
// [1]: See kvserver.rangeAddressing.
if desc.RangeID == lastRangeIDInMeta1 {
continue
}
descriptors = append(descriptors, desc)
if keys.InMeta1(keys.RangeMetaKey(desc.StartKey)) {
lastRangeIDInMeta1 = desc.RangeID
}
}
// Invoke fn with the current chunk (of size ~blockSize) of
// range descriptors.
return fn(descriptors...)
})
})
}