-
Notifications
You must be signed in to change notification settings - Fork 453
/
agent.go
695 lines (586 loc) · 18.4 KB
/
agent.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
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
// 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 agent
import (
"bufio"
"fmt"
"io"
"io/ioutil"
"os"
"path"
"strings"
"sync"
"sync/atomic"
"time"
"github.com/m3db/m3/src/m3em/checksum"
"github.com/m3db/m3/src/m3em/generated/proto/m3em"
"github.com/m3db/m3/src/m3em/os/exec"
"github.com/m3db/m3/src/m3em/os/fs"
xerrors "github.com/m3db/m3/src/x/errors"
"github.com/uber-go/tally"
"go.uber.org/zap"
context "golang.org/x/net/context"
"google.golang.org/grpc"
"google.golang.org/grpc/codes"
)
const (
defaultReportInterval = 5 * time.Second
defaultTestCanaryPrefix = "test-canary-file"
reasonTeardownHeartbeat = "remote agent received Teardown(), turning off heartbeating"
reasonSetupInitializeHostResources = "unable to initialize host resources, turning off heartbeating"
)
var (
errProcessMonitorNotDefined = fmt.Errorf("process monitor not defined")
errNoValidTargetsSpecified = fmt.Errorf("no valid target destinations specified")
errOnlyDataFileMultiTarget = fmt.Errorf("multiple targets are only supported for data files")
)
type opAgent struct {
sync.RWMutex
token string
executablePath string
configPath string
newProcessMonitorFn newProcessMonitorFn
processMonitor exec.ProcessMonitor
heartbeater *heatbeater
running int32
stopping int32
heartbeatTimeoutCh chan struct{}
opts Options
logger *zap.Logger
metrics *opAgentMetrics
doneCh chan struct{}
closeCh chan struct{}
}
type newProcessMonitorFn func(exec.Cmd, exec.ProcessListener) (exec.ProcessMonitor, error)
// New creates and returns a new Operator Agent
func New(
opts Options,
) (Agent, error) {
if err := opts.Validate(); err != nil {
return nil, err
}
if err := canaryWriteTest(opts.WorkingDirectory()); err != nil {
return nil, err
}
agent := &opAgent{
opts: opts,
logger: opts.InstrumentOptions().Logger(),
metrics: newAgentMetrics(opts.InstrumentOptions().MetricsScope()),
newProcessMonitorFn: exec.NewProcessMonitor,
doneCh: make(chan struct{}, 1),
closeCh: make(chan struct{}, 1),
}
go agent.reportMetrics()
return agent, nil
}
func (o *opAgent) Close() error {
o.closeCh <- struct{}{}
<-o.doneCh
return nil
}
func canaryWriteTest(dir string) error {
fi, err := os.Stat(dir)
if err != nil {
return fmt.Errorf("unable to stat directory, [ err = %v ]", err)
}
if !fi.IsDir() {
return fmt.Errorf("path is not a directory")
}
fd, err := ioutil.TempFile(dir, defaultTestCanaryPrefix)
if err != nil {
return fmt.Errorf("unable to create canary file, [ err = %v ]", err)
}
os.Remove(fd.Name())
return nil
}
func updateBoolGauge(b bool, m tally.Gauge) {
if b {
m.Update(1)
} else {
m.Update(0)
}
}
func (o *opAgent) reportMetrics() {
reportTicker := time.NewTicker(defaultReportInterval)
for {
select {
case <-reportTicker.C:
state := o.state()
updateBoolGauge(state.running, o.metrics.running)
updateBoolGauge(state.executablePath != "", o.metrics.execTransferred)
updateBoolGauge(state.configPath != "", o.metrics.confTransferred)
case <-o.closeCh:
reportTicker.Stop()
o.doneCh <- struct{}{}
return
}
}
}
func (o *opAgent) Running() bool {
return atomic.LoadInt32(&o.running) == 1
}
type opAgentState struct {
running bool
executablePath string
configPath string
}
func (o *opAgent) state() opAgentState {
o.RLock()
defer o.RUnlock()
return opAgentState{
running: o.Running(),
executablePath: o.executablePath,
configPath: o.configPath,
}
}
func (o *opAgent) Start(ctx context.Context, request *m3em.StartRequest) (*m3em.StartResponse, error) {
o.logger.Info("received Start()")
o.Lock()
defer o.Unlock()
if o.Running() {
return nil, grpc.Errorf(codes.FailedPrecondition, "already running")
}
if o.executablePath == "" {
return nil, grpc.Errorf(codes.FailedPrecondition, "agent missing build")
}
if o.configPath == "" {
return nil, grpc.Errorf(codes.FailedPrecondition, "agent missing config")
}
if err := o.startWithLock(); err != nil {
return nil, grpc.Errorf(codes.Internal, "unable to start: %v", err)
}
return &m3em.StartResponse{}, nil
}
func (o *opAgent) onProcessTerminate(err error) {
if err == nil {
err = fmt.Errorf("test process terminated without error")
} else {
err = fmt.Errorf("test process terminated with error: %v", err)
}
o.logger.Warn(err.Error())
if stopping := atomic.LoadInt32(&o.stopping); stopping == 0 && o.heartbeater != nil {
o.heartbeater.notifyProcessTermination(err.Error())
}
atomic.StoreInt32(&o.running, 0)
}
func (o *opAgent) newProcessListener() exec.ProcessListener {
return exec.NewProcessListener(func() {
o.onProcessTerminate(nil)
}, func(err error) {
o.onProcessTerminate(err)
})
}
func (o *opAgent) startWithLock() error {
var (
path, args = o.opts.ExecGenFn()(o.executablePath, o.configPath)
osArgs = append([]string{path}, args...)
cmd = exec.Cmd{
Path: path,
Args: osArgs,
OutputDir: o.opts.WorkingDirectory(),
Env: o.opts.EnvMap(),
}
listener = o.newProcessListener()
)
pm, err := o.newProcessMonitorFn(cmd, listener)
if err != nil {
return err
}
o.logger.Info("executing command", zap.Any("command", cmd))
if err := pm.Start(); err != nil {
return err
}
atomic.StoreInt32(&o.running, 1)
o.processMonitor = pm
return nil
}
func (o *opAgent) Stop(ctx context.Context, request *m3em.StopRequest) (*m3em.StopResponse, error) {
o.logger.Info("received Stop()")
o.Lock()
defer o.Unlock()
if !o.Running() {
return nil, grpc.Errorf(codes.FailedPrecondition, "not running")
}
atomic.StoreInt32(&o.stopping, 1)
if err := o.stopWithLock(); err != nil {
return nil, grpc.Errorf(codes.Internal, "unable to stop: %v", err)
}
atomic.StoreInt32(&o.stopping, 0)
return &m3em.StopResponse{}, nil
}
func (o *opAgent) stopWithLock() error {
if o.processMonitor == nil {
return errProcessMonitorNotDefined
}
if err := o.processMonitor.Stop(); err != nil {
return err
}
o.processMonitor = nil
atomic.StoreInt32(&o.running, 0)
return nil
}
func (o *opAgent) resetWithLock(reason string) error {
var multiErr xerrors.MultiError
if o.heartbeater != nil {
o.logger.Info("stopping heartbeating")
if reason != "" {
o.heartbeater.notifyOverwrite(reason)
}
multiErr = multiErr.Add(o.heartbeater.close())
o.heartbeater = nil
}
if o.heartbeatTimeoutCh != nil {
close(o.heartbeatTimeoutCh)
o.heartbeatTimeoutCh = nil
}
if o.Running() {
o.logger.Info("process running, stopping")
if err := o.stopWithLock(); err != nil {
o.logger.Warn("unable to stop", zap.Error(err))
multiErr = multiErr.Add(err)
}
}
o.logger.Info("releasing host resources")
if err := o.opts.ReleaseHostResourcesFn()(); err != nil {
o.logger.Info("unable to release host resources", zap.Error(err))
multiErr = multiErr.Add(err)
}
o.token = ""
o.executablePath = ""
o.configPath = ""
atomic.StoreInt32(&o.running, 0)
return multiErr.FinalError()
}
func (o *opAgent) Teardown(ctx context.Context, request *m3em.TeardownRequest) (*m3em.TeardownResponse, error) {
o.logger.Info("received Teardown()")
o.Lock()
defer o.Unlock()
if err := o.resetWithLock(reasonTeardownHeartbeat); err != nil {
return nil, grpc.Errorf(codes.Internal, "unable to teardown: %v", err)
}
return &m3em.TeardownResponse{}, nil
}
func (o *opAgent) isSetup() bool {
o.RLock()
defer o.RUnlock()
return o.isSetupWithLock()
}
func (o *opAgent) isSetupWithLock() bool {
return o.token != ""
}
func (o *opAgent) Setup(ctx context.Context, request *m3em.SetupRequest) (*m3em.SetupResponse, error) {
o.logger.Info("received Setup()")
// nil check
if request == nil || request.SessionToken == "" {
return nil, grpc.Errorf(codes.InvalidArgument, "nil request")
}
o.Lock()
defer o.Unlock()
if o.token != "" && o.token != request.SessionToken && !request.Force {
return nil, grpc.Errorf(codes.AlreadyExists, "agent already initialized with token: %s", o.token)
}
if o.isSetupWithLock() {
// reset agent
msg := fmt.Sprintf("heartbeating being overwritten by new setup request: %+v", *request)
if err := o.resetWithLock(msg); err != nil {
return nil, grpc.Errorf(codes.Aborted, "unable to reset: %v", err)
}
}
// remove any files stored in the working directory
wd := o.opts.WorkingDirectory()
o.logger.Info("removing contents from working directory", zap.String("dir", wd))
if err := fs.RemoveContents(wd); err != nil {
return nil, grpc.Errorf(codes.Internal, "unable to clear working directory: %v", err)
}
// initialize any resources needed on the host
o.logger.Info("initializing host resources")
if err := o.opts.InitHostResourcesFn()(); err != nil {
o.resetWithLock(reasonSetupInitializeHostResources) // release any resources
return nil, grpc.Errorf(codes.Internal, "unable to initialize host resources: %v", err)
}
// setup new heartbeating
if request.HeartbeatEnabled {
opts := heartbeatOpts{
operatorUUID: request.OperatorUuid,
endpoint: request.HeartbeatEndpoint,
nowFn: o.opts.NowFn(),
timeout: o.opts.HeartbeatTimeout(),
timeoutFn: o.heartbeatingTimeout,
errorFn: o.heartbeatInternalError,
}
beater, err := newHeartbeater(o, opts, o.opts.InstrumentOptions())
if err != nil {
o.resetWithLock(reasonSetupInitializeHostResources) // release any resources
return nil, grpc.Errorf(codes.Aborted, "unable to start heartbeating process: %v", err)
}
o.heartbeater = beater
o.heartbeater.start(time.Second * time.Duration(request.HeartbeatFrequencySecs))
}
o.token = request.SessionToken
return &m3em.SetupResponse{}, nil
}
func (o *opAgent) heartbeatingTimeout(lastHb time.Time) {
o.logger.Warn("heartbeat sending timed out, resetting agent")
o.Lock()
err := o.resetWithLock("") // "" indicates we don't want to send a heartbeat
o.Unlock()
if err == nil {
o.logger.Info("successfully reset agent")
} else {
o.logger.Warn("error while resetting agent", zap.Error(err))
}
}
func (o *opAgent) heartbeatInternalError(err error) {
o.logger.Warn("received unknown error whilst heartbeat", zap.Error(err))
o.logger.Warn("resetting agent")
o.Lock()
err = o.resetWithLock(err.Error())
o.Unlock()
if err == nil {
o.logger.Info("successfully reset agent")
} else {
o.logger.Warn("error while resetting agent", zap.Error(err))
}
}
func (o *opAgent) pathsRelativeToWorkingDir(
targets []string,
) ([]string, error) {
files := make([]string, 0, len(targets))
for _, t := range targets {
if strings.Contains(t, "..") { // i.e. relative path
return nil, fmt.Errorf("relative paths not allowed: %v", t)
}
f := path.Join(o.opts.WorkingDirectory(), t)
files = append(files, f)
}
return files, nil
}
func (o *opAgent) initFile(
fileType m3em.PushFileType,
targets []string,
overwrite bool,
) (*multiWriter, error) {
if len(targets) < 1 {
return nil, errNoValidTargetsSpecified
}
if len(targets) > 1 && fileType != m3em.PushFileType_PUSH_FILE_TYPE_DATA_FILE {
return nil, errOnlyDataFileMultiTarget
}
paths, err := o.pathsRelativeToWorkingDir(targets)
if err != nil {
return nil, err
}
flags := os.O_CREATE | os.O_WRONLY
if overwrite {
flags = flags | os.O_TRUNC
}
fileMode := o.opts.NewFileMode()
if fileType == m3em.PushFileType_PUSH_FILE_TYPE_SERVICE_BINARY {
fileMode = os.FileMode(0755)
}
dirMode := o.opts.NewDirectoryMode()
return newMultiWriter(paths, flags, fileMode, dirMode)
}
func (o *opAgent) markFileDone(
fileType m3em.PushFileType,
mw *multiWriter,
) error {
if len(mw.fds) != 1 && (fileType == m3em.PushFileType_PUSH_FILE_TYPE_SERVICE_BINARY || fileType == m3em.PushFileType_PUSH_FILE_TYPE_SERVICE_CONFIG) {
// should never happen
return fmt.Errorf("internal error: multiple targets for binary/config")
}
for _, fd := range mw.fds {
o.logger.Info("file transferred",
zap.Stringer("type", fileType),
zap.String("path", fd.Name()))
}
o.Lock()
defer o.Unlock()
if fileType == m3em.PushFileType_PUSH_FILE_TYPE_SERVICE_BINARY {
o.executablePath = mw.fds[0].Name()
}
if fileType == m3em.PushFileType_PUSH_FILE_TYPE_SERVICE_CONFIG {
o.configPath = mw.fds[0].Name()
}
return nil
}
// PullFile receives a file from the caller to be stored locally on the agent
func (o *opAgent) PushFile(stream m3em.Operator_PushFileServer) error {
o.logger.Info("received PushFile()")
var (
checksum = checksum.NewAccumulator()
numChunks = 0
lastChunkIdx = int32(0)
fileHandle *multiWriter
fileType = m3em.PushFileType_PUSH_FILE_TYPE_UNKNOWN
err error
)
for {
request, streamErr := stream.Recv()
if streamErr != nil && streamErr != io.EOF {
return streamErr
}
if request == nil {
break
}
if numChunks == 0 {
// first request with any data in it, log it for visibilty
o.logger.Info("file transfer initiated",
zap.Strings("targets", request.GetTargetPaths()),
zap.Stringer("fileType", request.GetType()),
zap.Bool("overwrite", request.GetOverwrite()))
fileType = request.GetType()
fileHandle, err = o.initFile(fileType, request.GetTargetPaths(), request.GetOverwrite())
if err != nil {
return err
}
lastChunkIdx = request.GetData().GetIdx() - 1
}
chunkIdx := request.GetData().GetIdx()
if chunkIdx != 1+lastChunkIdx {
return fmt.Errorf("received chunkIdx: %d after %d", chunkIdx, lastChunkIdx)
}
lastChunkIdx = chunkIdx
numChunks++
bytes := request.GetData().GetBytes()
checksum.Update(bytes)
numWritten, err := fileHandle.write(bytes)
if err != nil {
return err
}
if numWritten != len(bytes) {
return fmt.Errorf("unable to write bytes, expected: %d, observed: %d", len(bytes), numWritten)
}
if streamErr == io.EOF {
break
}
}
if fileHandle == nil {
return fmt.Errorf("multiwriter has not been initialized")
}
var me xerrors.MultiError
me = me.Add(fileHandle.Close())
me = me.Add(o.markFileDone(fileType, fileHandle))
if err := me.FinalError(); err != nil {
return err
}
return stream.SendAndClose(&m3em.PushFileResponse{
FileChecksum: checksum.Current(),
NumChunksRecvd: int32(numChunks),
})
}
func validatePullFileRequest(request *m3em.PullFileRequest) error {
if request == nil {
return grpc.Errorf(codes.InvalidArgument, "nil request")
}
if request.ChunkSize <= 0 {
return grpc.Errorf(codes.InvalidArgument, "chunkSize must be a positive integer")
}
if request.MaxSize < 0 {
return grpc.Errorf(codes.InvalidArgument, "maxSize must be a non-negative integer")
}
return nil
}
// PullFile sends a local agent file to the caller
func (o *opAgent) PullFile(request *m3em.PullFileRequest, stream m3em.Operator_PullFileServer) error {
if err := validatePullFileRequest(request); err != nil {
return err
}
o.logger.Info("received PullFile()", zap.Any("request", *request))
o.RLock()
defer o.RUnlock()
if !o.isSetupWithLock() {
return grpc.Errorf(codes.InvalidArgument, "agent has not been setup, unable to transfer file")
}
pm := o.processMonitor
if pm == nil {
return grpc.Errorf(codes.InvalidArgument, "no process running, unable to transfer file")
}
switch fileType := request.GetFileType(); fileType {
case m3em.PullFileType_PULL_FILE_TYPE_SERVICE_STDERR:
return o.sendLocalFileWithRLock(pm.StderrPath(), request.ChunkSize, request.MaxSize, stream)
case m3em.PullFileType_PULL_FILE_TYPE_SERVICE_STDOUT:
return o.sendLocalFileWithRLock(pm.StdoutPath(), request.ChunkSize, request.MaxSize, stream)
default:
return grpc.Errorf(codes.InvalidArgument, "received unknown pull file: %v", fileType)
}
}
func (o *opAgent) sendLocalFileWithRLock(localPath string, chunkSize int64, maxBytes int64, stream m3em.Operator_PullFileServer) error {
fi, err := os.Stat(localPath)
if err != nil {
return grpc.Errorf(codes.InvalidArgument, "unable to find file: %v", err)
}
fd, err := os.Open(localPath)
if err != nil {
return grpc.Errorf(codes.InvalidArgument, "unable to open file: %v", err)
}
var (
reader = bufio.NewReaderSize(fd, int(chunkSize))
buf = make([]byte, chunkSize)
chunkIdx = 1
truncated = false
)
// check if we need to seek ahead or if we are sending all the bytes
if maxBytes > 0 && fi.Size() > maxBytes {
offset := fi.Size() - maxBytes
if _, err := fd.Seek(offset, 0 /* relative to start of file */); err != nil {
return grpc.Errorf(codes.Internal, "unable to seek file: %v", err)
}
truncated = true
}
for {
n, err := reader.Read(buf)
switch err {
case io.EOF:
// i.e. streamed through the file, we can indicate we're done
return nil
case nil:
// i.e. this read succeeded, send it and continue as we can read more data
if streamErr := stream.Send(&m3em.PullFileResponse{
Data: &m3em.DataChunk{
Bytes: buf[:n],
Idx: int32(chunkIdx),
},
Truncated: truncated,
}); streamErr != nil {
return grpc.Errorf(codes.Internal, "unable to send chunk: %v", streamErr.Error())
}
default:
// i.e. something broke
return grpc.Errorf(codes.Unavailable, "unable to read file: %v", err.Error())
}
// increment idx
chunkIdx++
}
}
type opAgentMetrics struct {
// TODO(prateek): process monitor opts, metric for process uptime
running tally.Gauge
execTransferred tally.Gauge
confTransferred tally.Gauge
}
func newAgentMetrics(scope tally.Scope) *opAgentMetrics {
subscope := scope.SubScope("agent")
return &opAgentMetrics{
running: subscope.Gauge("running"),
execTransferred: subscope.Gauge("exec_transferred"),
confTransferred: subscope.Gauge("conf_transferred"),
}
}