forked from daos-stack/daos
-
Notifications
You must be signed in to change notification settings - Fork 0
/
mgmt_client.go
280 lines (229 loc) · 8.07 KB
/
mgmt_client.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
//
// (C) Copyright 2019 Intel Corporation.
//
// 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.
//
// GOVERNMENT LICENSE RIGHTS-OPEN SOURCE SOFTWARE
// The Government's rights to use, modify, reproduce, release, perform, display,
// or disclose this software are subject to the terms of the Apache License as
// provided in Contract No. 8F-30005.
// Any reproduction of computer software, computer software documentation, or
// portions thereof marked with this legend must also reproduce the markings.
//
package server
import (
"fmt"
"net"
"time"
"github.com/pkg/errors"
"golang.org/x/net/context"
"google.golang.org/grpc"
mgmtpb "github.com/daos-stack/daos/src/control/common/proto/mgmt"
"github.com/daos-stack/daos/src/control/logging"
"github.com/daos-stack/daos/src/control/security"
)
const (
retryDelay = 3 * time.Second
)
type (
mgmtSvcClientCfg struct {
AccessPoints []string
ControlAddr *net.TCPAddr
TransportConfig *security.TransportConfig
}
mgmtSvcClient struct {
log logging.Logger
cfg mgmtSvcClientCfg
}
)
func newMgmtSvcClient(ctx context.Context, log logging.Logger, cfg mgmtSvcClientCfg) *mgmtSvcClient {
return &mgmtSvcClient{
log: log,
cfg: cfg,
}
}
// delayRetry delays next retry.
func (msc *mgmtSvcClient) delayRetry(ctx context.Context) {
select {
case <-ctx.Done(): // break early if the parent context is canceled
case <-time.After(retryDelay): // otherwise, block until after the delay duration
}
}
func (msc *mgmtSvcClient) withConnection(ctx context.Context, ap string,
fn func(context.Context, mgmtpb.MgmtSvcClient) error, extraDialOpts ...grpc.DialOption) error {
var opts []grpc.DialOption
authDialOption, err := security.DialOptionForTransportConfig(msc.cfg.TransportConfig)
if err != nil {
return errors.Wrap(err, "Failed to determine dial option from TransportConfig")
}
// Setup Dial Options that will always be included.
opts = append(opts, grpc.WithBlock(), authDialOption)
conn, err := grpc.DialContext(ctx, ap, append(opts, extraDialOpts...)...)
if err != nil {
return err
}
defer conn.Close()
return fn(ctx, mgmtpb.NewMgmtSvcClient(conn))
}
func (msc *mgmtSvcClient) withConnectionRetry(ctx context.Context, ap string,
fn func(context.Context, mgmtpb.MgmtSvcClient) error) error {
return msc.withConnection(ctx, ap, fn, grpc.WithBackoffMaxDelay(retryDelay),
grpc.WithDefaultCallOptions(grpc.FailFast(false)))
}
func (msc *mgmtSvcClient) withConnectionFailOnBadDial(ctx context.Context, ap string,
fn func(context.Context, mgmtpb.MgmtSvcClient) error) error {
return msc.withConnection(ctx, ap, fn, grpc.FailOnNonTempDialError(true))
}
func (msc *mgmtSvcClient) LeaderAddress() (string, error) {
if len(msc.cfg.AccessPoints) == 0 {
return "", errors.New("no access points defined")
}
// TODO: Develop algorithm for determining current leader.
// For now, just choose the first address.
return msc.cfg.AccessPoints[0], nil
}
func (msc *mgmtSvcClient) retryOnErr(err error, ctx context.Context, prefix string) bool {
if err != nil {
msc.log.Debugf("%s: %v", prefix, err)
msc.delayRetry(ctx)
return true
}
return false
}
func (msc *mgmtSvcClient) retryOnStatus(status int32, ctx context.Context, prefix string) bool {
if status != 0 {
msc.log.Debugf("%s: %d", prefix, status)
msc.delayRetry(ctx)
return true
}
return false
}
func (msc *mgmtSvcClient) Join(ctx context.Context, req *mgmtpb.JoinReq) (resp *mgmtpb.JoinResp, joinErr error) {
ap, err := msc.LeaderAddress()
if err != nil {
return nil, err
}
joinErr = msc.withConnectionRetry(ctx, ap,
func(ctx context.Context, pbClient mgmtpb.MgmtSvcClient) error {
if req.Addr == "" {
req.Addr = msc.cfg.ControlAddr.String()
}
prefix := fmt.Sprintf("join(%s, %+v)", ap, *req)
msc.log.Debugf(prefix + " begin")
defer msc.log.Debugf(prefix + " end")
for {
var err error
select {
case <-ctx.Done():
return errors.Wrap(ctx.Err(), prefix)
default:
}
resp, err = pbClient.Join(ctx, req)
if msc.retryOnErr(err, ctx, prefix) {
continue
}
if resp == nil {
return errors.New("unexpected nil response status")
}
// TODO: Stop retrying upon certain errors (e.g., "not
// MS", "rank unavailable", and "excluded").
if msc.retryOnStatus(resp.Status, ctx, prefix) {
continue
}
return nil
}
})
return
}
// PrepShutdown calls function remotely over gRPC on server listening at destAddr.
//
// Shipped function propose ranks for shutdown by sending requests over dRPC
// to each rank.
func (msc *mgmtSvcClient) PrepShutdown(ctx context.Context, destAddr string, req mgmtpb.RanksReq) (resp *mgmtpb.RanksResp, psErr error) {
psErr = msc.withConnectionFailOnBadDial(ctx, destAddr,
func(ctx context.Context, pbClient mgmtpb.MgmtSvcClient) (err error) {
msc.log.Debugf("prep shutdown(%s, %+v)", destAddr, req)
resp, err = pbClient.PrepShutdownRanks(ctx, &req)
return
})
return
}
// Stop calls function remotely over gRPC on server listening at destAddr.
//
// Shipped function terminates ranks directly from the harness at the listening
// address without requesting over dRPC.
func (msc *mgmtSvcClient) Stop(ctx context.Context, destAddr string, req mgmtpb.RanksReq) (resp *mgmtpb.RanksResp, stopErr error) {
stopErr = msc.withConnectionFailOnBadDial(ctx, destAddr,
func(ctx context.Context, pbClient mgmtpb.MgmtSvcClient) error {
prefix := fmt.Sprintf("stop(%s, %+v)", destAddr, req)
msc.log.Debugf(prefix + " begin")
defer msc.log.Debugf(prefix + " end")
for {
var err error
select {
case <-ctx.Done():
return errors.Wrap(ctx.Err(), prefix)
default:
}
// returns on time out or when all instances are stopped
// error returned if any instance is still running so that
// we retry until all are terminated on host
resp, err = pbClient.StopRanks(ctx, &req)
if msc.retryOnErr(err, ctx, prefix) {
continue
}
if resp == nil {
return errors.New("unexpected nil response status")
}
// TODO: Stop retrying upon certain errors.
return nil
}
})
return
}
// Start calls function remotely over gRPC on server listening at destAddr.
//
// Shipped function issues StartRanks requests over dRPC to start each
// rank managed by the harness listening at the destination address.
//
// StartRanks will return results for any instances started by the harness.
func (msc *mgmtSvcClient) Start(ctx context.Context, destAddr string, req mgmtpb.RanksReq) (resp *mgmtpb.RanksResp, startErr error) {
startErr = msc.withConnectionFailOnBadDial(ctx, destAddr,
func(ctx context.Context, pbClient mgmtpb.MgmtSvcClient) (err error) {
prefix := fmt.Sprintf("start(%s, %+v)", destAddr, req)
msc.log.Debugf(prefix + " begin")
defer msc.log.Debugf(prefix + " end")
// returns on time out or when all instances are running
// don't retry
resp, err = pbClient.StartRanks(ctx, &req)
return
})
return
}
// Status calls function remotely over gRPC on server listening at destAddr.
//
// Shipped function issues PingRank dRPC requests to query each rank to verify
// activity.
//
// PingRanks should return ping results for any instances managed by the harness.
func (msc *mgmtSvcClient) Status(ctx context.Context, destAddr string, req mgmtpb.RanksReq) (resp *mgmtpb.RanksResp, statusErr error) {
statusErr = msc.withConnectionFailOnBadDial(ctx, destAddr,
func(ctx context.Context, pbClient mgmtpb.MgmtSvcClient) (err error) {
prefix := fmt.Sprintf("status(%s, %+v)", destAddr, req)
msc.log.Debugf(prefix + " begin")
defer msc.log.Debugf(prefix + " end")
resp, err = pbClient.PingRanks(ctx, &req)
return
})
return
}