-
Notifications
You must be signed in to change notification settings - Fork 453
/
node.go
481 lines (413 loc) · 11.9 KB
/
node.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
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
// Copyright (c) 2017 Uber Technologies, Inc.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
package node
import (
"context"
"fmt"
"io"
"os"
"path/filepath"
"sync"
"github.com/m3db/m3/src/cluster/placement"
"github.com/m3db/m3/src/m3em/build"
"github.com/m3db/m3/src/m3em/generated/proto/m3em"
"github.com/m3db/m3/src/m3em/os/fs"
xclock "github.com/m3db/m3x/clock"
xerrors "github.com/m3db/m3x/errors"
xlog "github.com/m3db/m3x/log"
uuid "github.com/satori/go.uuid"
"google.golang.org/grpc"
)
var (
errUnableToSetupInitializedNode = fmt.Errorf("unable to setup node, must be either setup/uninitialized")
errUnableToTeardownNode = fmt.Errorf("unable to teardown node, must be either setup/running")
errUnableToStartNode = fmt.Errorf("unable to start node, it must be setup")
errUnableToStopNode = fmt.Errorf("unable to stop node, it must be running")
errUnableToTransferFile = fmt.Errorf("unable to transfer file. node must be setup/running")
)
type svcNode struct {
sync.Mutex
placement.Instance
logger xlog.Logger
opts Options
status Status
currentBuild build.ServiceBuild
currentConf build.ServiceConfiguration
clientConn *grpc.ClientConn
client m3em.OperatorClient
listeners *listenerGroup
heartbeater *opHeartbeatServer
operatorUUID string
heartbeatEndpoint string
}
// New returns a new ServiceNode.
func New(
node placement.Instance,
opts Options,
) (ServiceNode, error) {
if err := opts.Validate(); err != nil {
return nil, err
}
clientConn, client, err := opts.OperatorClientFn()()
if err != nil {
return nil, err
}
uuid := uuid.NewV4().Bytes()
var (
retNode = &svcNode{
logger: opts.InstrumentOptions().Logger(),
opts: opts,
Instance: node,
status: StatusUninitialized,
}
listeners = newListenerGroup(retNode)
hbUUID = string(uuid[:])
heartbeater *opHeartbeatServer
routerEndpoint string
)
if opts.HeartbeatOptions().Enabled() {
router := opts.HeartbeatOptions().HeartbeatRouter()
routerEndpoint = router.Endpoint()
heartbeater = newHeartbeater(listeners, opts.HeartbeatOptions(), opts.InstrumentOptions())
if err := router.Register(hbUUID, heartbeater); err != nil {
return nil, fmt.Errorf("unable to register heartbeat server with router: %v", err)
}
}
retNode.listeners = listeners
retNode.client = client
retNode.clientConn = clientConn
retNode.heartbeater = heartbeater
retNode.heartbeatEndpoint = routerEndpoint
retNode.operatorUUID = hbUUID
return retNode, nil
}
func (i *svcNode) String() string {
i.Lock()
defer i.Unlock()
return fmt.Sprintf("ServiceNode %s", i.Instance.String())
}
func (i *svcNode) heartbeatReceived() bool {
return !i.heartbeater.lastHeartbeatTime().IsZero()
}
func (i *svcNode) Setup(
bld build.ServiceBuild,
conf build.ServiceConfiguration,
token string,
force bool,
) error {
i.Lock()
defer i.Unlock()
if i.status != StatusUninitialized &&
i.status != StatusSetup {
return errUnableToSetupInitializedNode
}
i.currentConf = conf
i.currentBuild = bld
freq := uint32(i.opts.HeartbeatOptions().Interval().Seconds())
err := i.opts.Retrier().Attempt(func() error {
ctx := context.Background()
_, err := i.client.Setup(ctx, &m3em.SetupRequest{
OperatorUuid: i.operatorUUID,
SessionToken: token,
Force: force,
HeartbeatEnabled: i.opts.HeartbeatOptions().Enabled(),
HeartbeatEndpoint: i.heartbeatEndpoint,
HeartbeatFrequencySecs: freq,
})
return err
})
if err != nil {
return fmt.Errorf("unable to setup: %v", err)
}
// TODO(prateek): make heartbeat pickup existing agent state
// Wait till we receive our first heartbeat
if i.opts.HeartbeatOptions().Enabled() {
i.logger.Infof("waiting until initial heartbeat is received")
received := xclock.WaitUntil(i.heartbeatReceived, i.opts.HeartbeatOptions().Timeout())
if !received {
return fmt.Errorf("did not receive heartbeat response from remote agent within timeout")
}
i.logger.Infof("initial heartbeat received")
// start hb monitoring
if err := i.heartbeater.start(); err != nil {
return fmt.Errorf("unable to start heartbeat monitor loop: %v", err)
}
}
// transfer build
if err := i.opts.Retrier().Attempt(func() error {
iter, err := bld.Iter(i.opts.TransferBufferSize())
if err != nil {
return err
}
return i.transferFile(transferOpts{
targets: []string{bld.ID()},
fileType: m3em.PushFileType_PUSH_FILE_TYPE_SERVICE_BINARY,
overwrite: force,
iter: iter,
})
}); err != nil {
return fmt.Errorf("unable to transfer build: %v", err)
}
if err := i.opts.Retrier().Attempt(func() error {
iter, err := conf.Iter(i.opts.TransferBufferSize())
if err != nil {
return err
}
return i.transferFile(transferOpts{
targets: []string{conf.ID()},
fileType: m3em.PushFileType_PUSH_FILE_TYPE_SERVICE_CONFIG,
overwrite: force,
iter: iter,
})
}); err != nil {
return fmt.Errorf("unable to transfer config: %v", err)
}
i.status = StatusSetup
return nil
}
// nolint: maligned
type transferOpts struct {
targets []string
fileType m3em.PushFileType
iter fs.FileReaderIter
overwrite bool
}
func (i *svcNode) transferFile(
t transferOpts,
) error {
defer t.iter.Close()
ctx := context.Background()
stream, err := i.client.PushFile(ctx)
if err != nil {
return err
}
chunkIdx := 0
for ; t.iter.Next(); chunkIdx++ {
bytes := t.iter.Current()
request := &m3em.PushFileRequest{
Type: t.fileType,
TargetPaths: t.targets,
Overwrite: t.overwrite,
Data: &m3em.DataChunk{
Bytes: bytes,
Idx: int32(chunkIdx),
},
}
err := stream.Send(request)
if err != nil {
stream.CloseSend()
return err
}
}
if err := t.iter.Err(); err != nil {
stream.CloseSend()
return err
}
response, err := stream.CloseAndRecv()
if err != nil {
return err
}
if int(response.NumChunksRecvd) != chunkIdx {
return fmt.Errorf("sent %d chunks, server only received %d of them", chunkIdx, response.NumChunksRecvd)
}
if t.iter.Checksum() != response.FileChecksum {
return fmt.Errorf("expected file checksum: %d, received: %d", t.iter.Checksum(), response.FileChecksum)
}
return nil
}
func (i *svcNode) TransferLocalFile(
srcPath string,
destPaths []string,
overwrite bool,
) error {
i.Lock()
defer i.Unlock()
if i.status != StatusSetup && i.status != StatusRunning {
return errUnableToTransferFile
}
if err := i.opts.Retrier().Attempt(func() error {
iter, err := fs.NewSizedFileReaderIter(srcPath, i.opts.TransferBufferSize())
if err != nil {
return err
}
return i.transferFile(transferOpts{
targets: destPaths,
fileType: m3em.PushFileType_PUSH_FILE_TYPE_DATA_FILE,
overwrite: overwrite,
iter: iter,
})
}); err != nil {
return fmt.Errorf("unable to transfer file: %v", err)
}
return nil
}
func (i *svcNode) pullRemoteFile(t m3em.PullFileType, fd *os.File) (bool, error) {
ctx := context.Background()
// resetting file in case this a retry
if err := fd.Truncate(0); err != nil {
return false, err
}
// create streaming client
client, err := i.client.PullFile(ctx, &m3em.PullFileRequest{
ChunkSize: int64(i.opts.TransferBufferSize()),
MaxSize: i.opts.MaxPullSize(),
FileType: t,
})
if err != nil {
return false, err
}
// iterate through responses
truncated := false
for {
response, err := client.Recv()
switch err {
case nil: // this Recv was successful, and we have more to read
truncated = response.Truncated
if _, writeErr := fd.Write(response.Data.Bytes); writeErr != nil {
return truncated, writeErr
}
case io.EOF: // no more to read, indicate success
return truncated, nil
default: // unexpected error, indicate failure
return truncated, err
}
}
}
func toM3EMPullType(t RemoteOutputType) (m3em.PullFileType, error) {
switch t {
case RemoteProcessStderr:
return m3em.PullFileType_PULL_FILE_TYPE_SERVICE_STDERR, nil
case RemoteProcessStdout:
return m3em.PullFileType_PULL_FILE_TYPE_SERVICE_STDOUT, nil
default:
return m3em.PullFileType_PULL_FILE_TYPE_UNKNOWN, fmt.Errorf("unknown output type: %v", t)
}
}
func (i *svcNode) GetRemoteOutput(
t RemoteOutputType,
localDest string,
) (bool, error) {
i.Lock()
defer i.Unlock()
if i.status != StatusSetup && i.status != StatusRunning {
return false, errUnableToTransferFile
}
mType, err := toM3EMPullType(t)
if err != nil {
return false, err
}
// create base directory for specified remote path if it doesn't exist
base := filepath.Dir(localDest)
if err := os.MkdirAll(base, os.FileMode(0755)|os.ModeDir); err != nil {
return false, err
}
fd, err := os.OpenFile(localDest, os.O_CREATE|os.O_WRONLY, os.FileMode(0666))
if err != nil {
return false, err
}
truncated := false
if retryErr := i.opts.Retrier().Attempt(func() error {
truncated, err = i.pullRemoteFile(mType, fd)
return err
}); retryErr != nil {
return truncated, fmt.Errorf("unable to get remote output: %v", retryErr)
}
return truncated, fd.Close()
}
func (i *svcNode) Teardown() error {
i.Lock()
defer i.Unlock()
if status := i.status; status != StatusRunning &&
status != StatusSetup &&
status != StatusError {
return errUnableToTeardownNode
}
// clear any listeners
i.listeners.clear()
if err := i.opts.Retrier().Attempt(func() error {
ctx := context.Background()
_, err := i.client.Teardown(ctx, &m3em.TeardownRequest{})
return err
}); err != nil {
return err
}
if err := i.Close(); err != nil {
return err
}
i.status = StatusUninitialized
return nil
}
func (i *svcNode) Close() error {
var err xerrors.MultiError
if conn := i.clientConn; conn != nil {
err = err.Add(conn.Close())
i.clientConn = nil
}
if hbServer := i.heartbeater; hbServer != nil {
hbServer.stop()
err = err.Add(i.opts.HeartbeatOptions().HeartbeatRouter().Deregister(i.operatorUUID))
i.heartbeater = nil
i.operatorUUID = ""
}
return err.FinalError()
}
func (i *svcNode) Start() error {
i.Lock()
defer i.Unlock()
if i.status != StatusSetup {
return errUnableToStartNode
}
if err := i.opts.Retrier().Attempt(func() error {
ctx := context.Background()
_, err := i.client.Start(ctx, &m3em.StartRequest{})
return err
}); err != nil {
return err
}
i.status = StatusRunning
return nil
}
func (i *svcNode) Stop() error {
i.Lock()
defer i.Unlock()
if i.status != StatusRunning {
return errUnableToStopNode
}
if err := i.opts.Retrier().Attempt(func() error {
ctx := context.Background()
_, err := i.client.Stop(ctx, &m3em.StopRequest{})
return err
}); err != nil {
return err
}
i.status = StatusSetup
return nil
}
func (i *svcNode) Status() Status {
i.Lock()
defer i.Unlock()
return i.status
}
func (i *svcNode) RegisterListener(l Listener) ListenerID {
return ListenerID(i.listeners.add(l))
}
func (i *svcNode) DeregisterListener(token ListenerID) {
i.listeners.remove(int(token))
}