-
Notifications
You must be signed in to change notification settings - Fork 3.9k
/
Copy pathfanout_clients.go
313 lines (282 loc) · 10.3 KB
/
fanout_clients.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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
// Copyright 2022 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 server
import (
"context"
"strconv"
"github.com/cockroachdb/cockroach/pkg/base"
"github.com/cockroachdb/cockroach/pkg/gossip"
"github.com/cockroachdb/cockroach/pkg/kv"
"github.com/cockroachdb/cockroach/pkg/kv/kvserver/liveness"
"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/server/status/statuspb"
"github.com/cockroachdb/cockroach/pkg/settings/cluster"
"github.com/cockroachdb/cockroach/pkg/util"
"github.com/cockroachdb/cockroach/pkg/util/hlc"
"github.com/cockroachdb/cockroach/pkg/util/log"
"github.com/cockroachdb/cockroach/pkg/util/stop"
"github.com/cockroachdb/errors"
"google.golang.org/grpc"
)
// serverID is a type that is either a `roachpb.NodeID`
// or `base.SQLInstanceID` depending on the context.
// Different implementations of `ServerIterator` interpret
// values of `serverID` differently depending on context.
type serverID int32
// ServerIterator is an interface that provides an abstraction
// around fanning out requests to different "servers" or "nodes".
// It is meant to be implemented in two contexts: one for KV
// servers to fan out to cluster nodes, and another for SQL
// servers to fan out to SQL instances. The implementation of
// this interface is used by gRPC services to collect data from
// all cluster nodes or all tenants to return to the user.
//
// TODO(davidh): unify the `getAllNodes` and `nodesList` responses.
// They contain similar data and are confusing to use.
type ServerIterator interface {
// dialNode provides a gRPC connection to the node or
// SQL instance identified by serverID.
dialNode(
ctx context.Context, serverID serverID,
) (*grpc.ClientConn, error)
// getAllNodes returns a map of all nodes in the cluster
// or instances in the tenant with their liveness status.
getAllNodes(
ctx context.Context,
) (map[serverID]livenesspb.NodeLivenessStatus, error)
// nodes returns a list of nodes or instances for the
// cluster or tenant suitable for returning as the Nodes
// RPC response.
nodes(ctx context.Context) (*serverpb.NodesResponse, error)
// nodesList returns a list of nodes or instances for the
// cluster or tenant suitable for returning as the NodesList
// RPC response.
nodesList(ctx context.Context) (*serverpb.NodesListResponse, error)
// parseServerID parses the given string as either a node
// or instance ID and returns a bool that is true if the
// ID corresponds to the local node or instance that the
// call was executed on.
parseServerID(serverIDParam string) (serverID, bool, error)
// getID returns the current node or SQL instance ID on the
// local server.
getID() serverID
// getServerIDAddress returns a gRPC address for the given node
// or SQL instance.
getServerIDAddress(context.Context, serverID) (*util.UnresolvedAddr, error)
// getServerIDSQLAddress returns a SQL address for the given node
// or SQL instance.
getServerIDSQLAddress(context.Context, serverID) (*util.UnresolvedAddr, error)
}
type tenantFanoutClient struct {
sqlServer *SQLServer
rpcCtx *rpc.Context
stopper *stop.Stopper
}
func (t *tenantFanoutClient) nodes(ctx context.Context) (*serverpb.NodesResponse, error) {
instances, err := t.sqlServer.sqlInstanceReader.GetAllInstances(ctx)
if err != nil {
return nil, err
}
var resp serverpb.NodesResponse
for _, instance := range instances {
resp.Nodes = append(resp.Nodes, statuspb.NodeStatus{
Desc: roachpb.NodeDescriptor{
NodeID: roachpb.NodeID(instance.InstanceID),
Locality: instance.Locality,
},
})
}
return &resp, err
}
func (t *tenantFanoutClient) nodesList(ctx context.Context) (*serverpb.NodesListResponse, error) {
instances, err := t.sqlServer.sqlInstanceReader.GetAllInstances(ctx)
if err != nil {
return nil, err
}
var resp serverpb.NodesListResponse
for _, instance := range instances {
// For SQL only servers, the (RPC) Address and SQL address is the same.
// TODO(#76175): We should split the instance address into SQL and RPC addresses.
nodeDetails := serverpb.NodeDetails{
NodeID: int32(instance.InstanceID),
Address: util.MakeUnresolvedAddr("tcp", instance.InstanceAddr),
SQLAddress: util.MakeUnresolvedAddr("tcp", instance.InstanceAddr),
}
resp.Nodes = append(resp.Nodes, nodeDetails)
}
return &resp, err
}
var _ ServerIterator = &tenantFanoutClient{}
func (t *tenantFanoutClient) getServerIDAddress(
ctx context.Context, serverID serverID,
) (*util.UnresolvedAddr, error) {
id := base.SQLInstanceID(serverID)
instance, err := t.sqlServer.sqlInstanceReader.GetInstance(ctx, id)
if err != nil {
return nil, err
}
return &util.UnresolvedAddr{
NetworkField: "tcp",
AddressField: instance.InstanceAddr,
}, nil
}
// TODO(davidh): This implementation is incorrect and should be fixed
// see https://github.com/cockroachdb/cockroach/issues/76175
func (t *tenantFanoutClient) getServerIDSQLAddress(
ctx context.Context, id serverID,
) (*util.UnresolvedAddr, error) {
return t.getServerIDAddress(ctx, id)
}
func (t *tenantFanoutClient) getID() serverID {
return serverID(t.sqlServer.SQLInstanceID())
}
func (t *tenantFanoutClient) dialNode(
ctx context.Context, serverID serverID,
) (*grpc.ClientConn, error) {
id := base.SQLInstanceID(serverID)
instance, err := t.sqlServer.sqlInstanceReader.GetInstance(ctx, id)
if err != nil {
return nil, err
}
return t.rpcCtx.GRPCDialPod(instance.InstanceAddr, id, rpc.DefaultClass).Connect(ctx)
}
func (t *tenantFanoutClient) getAllNodes(
ctx context.Context,
) (map[serverID]livenesspb.NodeLivenessStatus, error) {
liveTenantInstances, err := t.sqlServer.sqlInstanceReader.GetAllInstances(ctx)
if err != nil {
return nil, err
}
statuses := make(map[serverID]livenesspb.NodeLivenessStatus, len(liveTenantInstances))
for _, i := range liveTenantInstances {
statuses[serverID(i.InstanceID)] = livenesspb.NodeLivenessStatus_LIVE
}
return statuses, nil
}
func (t *tenantFanoutClient) parseServerID(serverIDParam string) (serverID, bool, error) {
// No parameter provided or set to local.
if len(serverIDParam) == 0 || localRE.MatchString(serverIDParam) {
return serverID(t.sqlServer.SQLInstanceID()), true /* isLocal */, nil /* err */
}
id, err := strconv.ParseInt(serverIDParam, 0, 32)
if err != nil {
return 0 /* instanceID */, false /* isLocal */, errors.Wrap(err, "instance ID could not be parsed")
}
instanceID := base.SQLInstanceID(id)
return serverID(instanceID), instanceID == t.sqlServer.SQLInstanceID() /* isLocal */, nil
}
type kvFanoutClient struct {
gossip *gossip.Gossip
rpcCtx *rpc.Context
db *kv.DB
nodeLiveness *liveness.NodeLiveness
clock *hlc.Clock
st *cluster.Settings
ambientCtx log.AmbientContext
}
func (k kvFanoutClient) nodes(_ context.Context) (*serverpb.NodesResponse, error) {
// This is a no-op because the systemStatusServer provides its own Nodes
// implementation rather than using the one from its embedded statusServer.
return &serverpb.NodesResponse{}, nil
}
func (k kvFanoutClient) nodesList(ctx context.Context) (*serverpb.NodesListResponse, error) {
statuses, _, err := getNodeStatuses(ctx, k.db, 0 /* limit */, 0 /* offset */)
if err != nil {
return nil, serverError(ctx, err)
}
resp := &serverpb.NodesListResponse{
Nodes: make([]serverpb.NodeDetails, len(statuses)),
}
for i, status := range statuses {
resp.Nodes[i].NodeID = int32(status.Desc.NodeID)
resp.Nodes[i].Address = status.Desc.Address
resp.Nodes[i].SQLAddress = status.Desc.SQLAddress
}
return resp, nil
}
var _ ServerIterator = &kvFanoutClient{}
func (k kvFanoutClient) getServerIDAddress(
_ context.Context, id serverID,
) (*util.UnresolvedAddr, error) {
return k.gossip.GetNodeIDAddress(roachpb.NodeID(id))
}
func (k kvFanoutClient) getServerIDSQLAddress(
_ context.Context, id serverID,
) (*util.UnresolvedAddr, error) {
return k.gossip.GetNodeIDSQLAddress(roachpb.NodeID(id))
}
func (k kvFanoutClient) getID() serverID {
return serverID(k.gossip.NodeID.Get())
}
func (k kvFanoutClient) dialNode(ctx context.Context, serverID serverID) (*grpc.ClientConn, error) {
id := roachpb.NodeID(serverID)
addr, err := k.gossip.GetNodeIDAddress(id)
if err != nil {
return nil, err
}
return k.rpcCtx.GRPCDialNode(addr.String(), id, rpc.DefaultClass).Connect(ctx)
}
func (k kvFanoutClient) listNodes(ctx context.Context) (*serverpb.NodesResponse, error) {
ctx = propagateGatewayMetadata(ctx)
ctx = k.ambientCtx.AnnotateCtx(ctx)
statuses, _, err := getNodeStatuses(ctx, k.db, 0, 0)
if err != nil {
return nil, err
}
resp := serverpb.NodesResponse{
Nodes: statuses,
}
clock := k.clock
resp.LivenessByNodeID, err = getLivenessStatusMap(ctx, k.nodeLiveness, clock.Now().GoTime(), k.st)
if err != nil {
return nil, err
}
return &resp, nil
}
func (k kvFanoutClient) getAllNodes(
ctx context.Context,
) (map[serverID]livenesspb.NodeLivenessStatus, error) {
nodes, err := k.listNodes(ctx)
if err != nil {
return nil, err
}
ret := make(map[serverID]livenesspb.NodeLivenessStatus)
for _, node := range nodes.Nodes {
nodeID := node.Desc.NodeID
livenessStatus := nodes.LivenessByNodeID[nodeID]
if livenessStatus == livenesspb.NodeLivenessStatus_DECOMMISSIONED {
// Skip over removed nodes.
continue
}
ret[serverID(nodeID)] = livenessStatus
}
return ret, nil
}
func (k kvFanoutClient) parseServerID(serverIDParam string) (serverID, bool, error) {
n, isLocal, err := parseNodeID(k.gossip, serverIDParam)
return serverID(n), isLocal, err
}
func parseNodeID(
gossip *gossip.Gossip, nodeIDParam string,
) (nodeID roachpb.NodeID, isLocal bool, err error) {
// No parameter provided or set to local.
if len(nodeIDParam) == 0 || localRE.MatchString(nodeIDParam) {
return gossip.NodeID.Get(), true, nil
}
id, err := strconv.ParseInt(nodeIDParam, 0, 32)
if err != nil {
return 0, false, errors.Wrap(err, "node ID could not be parsed")
}
nodeID = roachpb.NodeID(id)
return nodeID, nodeID == gossip.NodeID.Get(), nil
}