-
Notifications
You must be signed in to change notification settings - Fork 146
/
server.go
998 lines (911 loc) · 28.2 KB
/
server.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
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
// Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
// or more contributor license agreements. Licensed under the Elastic License;
// you may not use this file except in compliance with the Elastic License.
package server
import (
"crypto/tls"
"crypto/x509"
"encoding/json"
"fmt"
"io"
"net"
"strings"
"sync"
"time"
"go.elastic.co/apm"
"go.elastic.co/apm/module/apmgrpc"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
"github.com/gofrs/uuid"
protobuf "github.com/golang/protobuf/proto"
"google.golang.org/grpc"
"google.golang.org/grpc/credentials"
"github.com/elastic/elastic-agent-client/v7/pkg/client"
"github.com/elastic/elastic-agent-client/v7/pkg/proto"
"github.com/elastic/elastic-agent/internal/pkg/agent/errors"
"github.com/elastic/elastic-agent/internal/pkg/core/authority"
"github.com/elastic/elastic-agent/pkg/core/logger"
)
const (
// InitialCheckinTimeout is the maximum amount of wait time from initial check-in stream to
// getting the first check-in observed state.
InitialCheckinTimeout = 5 * time.Second
// CheckinMinimumTimeoutGracePeriod is additional time added to the client.CheckinMinimumTimeout
// to ensure the application is checking in correctly.
CheckinMinimumTimeoutGracePeriod = 30 * time.Second
// WatchdogCheckLoop is the amount of time that the watchdog will wait between checking for
// applications that have not checked in the correct amount of time.
WatchdogCheckLoop = 5 * time.Second
)
var (
// ErrApplicationAlreadyRegistered returned when trying to register an application more than once.
ErrApplicationAlreadyRegistered = errors.New("application already registered", errors.TypeApplication)
// ErrApplicationStopping returned when trying to update an application config but it is stopping.
ErrApplicationStopping = errors.New("application stopping", errors.TypeApplication)
// ErrApplicationStopTimedOut returned when calling Stop and the application timed out stopping.
ErrApplicationStopTimedOut = errors.New("application stopping timed out", errors.TypeApplication)
// ErrActionTimedOut returned on PerformAction when the action timed out.
ErrActionTimedOut = errors.New("application action timed out", errors.TypeApplication)
// ErrActionCancelled returned on PerformAction when an action is cancelled, normally due to the application
// being stopped or removed from the server.
ErrActionCancelled = errors.New("application action cancelled", errors.TypeApplication)
)
// ApplicationState represents the applications state according to the server.
type ApplicationState struct {
srv *Server
app interface{}
srvName string
token string
cert *authority.Pair
pendingExpected chan *proto.StateExpected
expected proto.StateExpected_State
expectedConfigIdx uint64
expectedConfig string
status proto.StateObserved_Status
statusMessage string
statusPayload map[string]interface{}
statusPayloadStr string
statusConfigIdx uint64
statusTime time.Time
checkinConn bool
checkinDone chan bool
checkinLock sync.RWMutex
pendingActions chan *pendingAction
sentActions map[string]*sentAction
actionsConn bool
actionsDone chan bool
actionsLock sync.RWMutex
inputTypes map[string]struct{}
}
// Handler is the used by the server to inform of status changes.
type Handler interface {
// OnStatusChange called when a registered application observed status is changed.
OnStatusChange(*ApplicationState, proto.StateObserved_Status, string, map[string]interface{})
}
// Server is the GRPC server that the launched applications connect back to.
type Server struct {
logger *logger.Logger
ca *authority.CertificateAuthority
listenAddr string
handler Handler
tracer *apm.Tracer
listener net.Listener
server *grpc.Server
watchdogDone chan bool
watchdogWG sync.WaitGroup
apps sync.Map
// overridden in tests
watchdogCheckInterval time.Duration
checkInMinTimeout time.Duration
}
// New creates a new GRPC server for clients to connect to.
func New(logger *logger.Logger, listenAddr string, handler Handler, tracer *apm.Tracer) (*Server, error) {
ca, err := authority.NewCA()
if err != nil {
return nil, err
}
return &Server{
logger: logger,
ca: ca,
listenAddr: listenAddr,
handler: handler,
watchdogCheckInterval: WatchdogCheckLoop,
checkInMinTimeout: client.CheckinMinimumTimeout + CheckinMinimumTimeoutGracePeriod,
tracer: tracer,
}, nil
}
// Start starts the GRPC endpoint and accepts new connections.
func (s *Server) Start() error {
if s.server != nil {
// already started
return nil
}
lis, err := net.Listen("tcp", s.listenAddr)
if err != nil {
return err
}
s.listener = lis
certPool := x509.NewCertPool()
if ok := certPool.AppendCertsFromPEM(s.ca.Crt()); !ok {
return errors.New("failed to append root CA", errors.TypeSecurity)
}
creds := credentials.NewTLS(&tls.Config{
ClientAuth: tls.RequireAndVerifyClientCert,
ClientCAs: certPool,
GetCertificate: s.getCertificate,
})
if s.tracer != nil {
apmInterceptor := apmgrpc.NewUnaryServerInterceptor(apmgrpc.WithRecovery(), apmgrpc.WithTracer(s.tracer))
s.server = grpc.NewServer(
grpc.UnaryInterceptor(apmInterceptor),
grpc.Creds(creds),
)
} else {
s.server = grpc.NewServer(grpc.Creds(creds))
}
proto.RegisterElasticAgentServer(s.server, s)
// start serving GRPC connections
go func() {
err := s.server.Serve(lis)
if err != nil {
s.logger.Errorf("error listening for GRPC: %s", err)
}
}()
// start the watchdog
s.watchdogDone = make(chan bool)
s.watchdogWG.Add(1)
go s.watchdog()
return nil
}
// Stop stops the GRPC endpoint.
func (s *Server) Stop() {
if s.server != nil {
close(s.watchdogDone)
s.server.Stop()
s.server = nil
s.listener = nil
s.watchdogWG.Wait()
}
}
// Get returns the application state from the server for the passed application.
func (s *Server) Get(app interface{}) (*ApplicationState, bool) {
var foundState *ApplicationState
s.apps.Range(func(_ interface{}, val interface{}) bool {
as := val.(*ApplicationState)
if as.app == app {
foundState = as
return false
}
return true
})
return foundState, foundState != nil
}
// FindByInputType application by input type
func (s *Server) FindByInputType(inputType string) (*ApplicationState, bool) {
var foundState *ApplicationState
s.apps.Range(func(_ interface{}, val interface{}) bool {
as := val.(*ApplicationState)
if as.inputTypes == nil {
return true
}
if _, ok := as.inputTypes[inputType]; ok {
foundState = as
return false
}
return true
})
return foundState, foundState != nil
}
// Register registers a new application to connect to the server.
func (s *Server) Register(app interface{}, config string) (*ApplicationState, error) {
if _, ok := s.Get(app); ok {
return nil, ErrApplicationAlreadyRegistered
}
id, err := uuid.NewV4()
if err != nil {
return nil, err
}
srvName, err := genServerName()
if err != nil {
return nil, err
}
pair, err := s.ca.GeneratePairWithName(srvName)
if err != nil {
return nil, err
}
appState := &ApplicationState{
srv: s,
app: app,
srvName: srvName,
token: id.String(),
cert: pair,
pendingExpected: make(chan *proto.StateExpected),
expected: proto.StateExpected_RUNNING,
expectedConfigIdx: 1,
expectedConfig: config,
checkinConn: true,
status: proto.StateObserved_STARTING,
statusConfigIdx: client.InitialConfigIdx,
statusTime: time.Now().UTC(),
pendingActions: make(chan *pendingAction, 100),
sentActions: make(map[string]*sentAction),
actionsConn: true,
}
s.apps.Store(appState.token, appState)
return appState, nil
}
// Checkin implements the GRPC bi-direction stream connection for check-ins.
func (s *Server) Checkin(server proto.ElasticAgent_CheckinServer) error {
firstCheckinChan := make(chan *proto.StateObserved)
go func() {
// go func will not be leaked, because when the main function
// returns it will close the connection. that will cause this
// function to return.
observed, err := server.Recv()
if err != nil {
close(firstCheckinChan)
return
}
firstCheckinChan <- observed
}()
var ok bool
var observedConfigStateIdx uint64
var firstCheckin *proto.StateObserved
select {
case firstCheckin, ok = <-firstCheckinChan:
if firstCheckin != nil {
observedConfigStateIdx = firstCheckin.ConfigStateIdx
}
break
case <-time.After(InitialCheckinTimeout):
// close connection
s.logger.Debug("check-in stream never sent initial observed message; closing connection")
return nil
}
if !ok {
// close connection
return nil
}
appState, ok := s.getByToken(firstCheckin.Token)
if !ok {
// no application with token; close connection
s.logger.Debug("check-in stream sent an invalid token; closing connection")
return status.Error(codes.PermissionDenied, "invalid token")
}
appState.checkinLock.Lock()
if appState.checkinDone != nil {
// application is already connected (cannot have multiple); close connection
appState.checkinLock.Unlock()
s.logger.Debug("check-in stream already exists for application; closing connection")
return status.Error(codes.AlreadyExists, "application already connected")
}
if !appState.checkinConn {
// application is being destroyed cannot reconnect; close connection
appState.checkinLock.Unlock()
s.logger.Debug("check-in stream cannot connect, application is being destroyed; closing connection")
return status.Error(codes.Unavailable, "application cannot connect being destroyed")
}
// application is running as a service and counter is already counting
// force config reload
if observedConfigStateIdx > 0 {
appState.expectedConfigIdx = observedConfigStateIdx + 1
}
checkinDone := make(chan bool)
appState.checkinDone = checkinDone
appState.checkinLock.Unlock()
defer func() {
appState.checkinLock.Lock()
appState.checkinDone = nil
appState.checkinLock.Unlock()
}()
// send the config and expected state changes to the applications when
// pushed on the channel
recvDone := make(chan bool)
sendDone := make(chan bool)
go func() {
defer func() {
close(sendDone)
}()
for {
var expected *proto.StateExpected
select {
case <-checkinDone:
return
case <-recvDone:
return
case expected = <-appState.pendingExpected:
}
err := server.Send(expected)
if err != nil {
if reportableErr(err) {
s.logger.Debugf("check-in stream failed to send expected state: %s", err)
}
return
}
}
}()
// update status after the pendingExpected channel has a reader
appState.updateStatus(firstCheckin, true)
// read incoming state observations from the application and act based on
// the servers expected state of the application
go func() {
for {
checkin, err := server.Recv()
if err != nil {
if reportableErr(err) {
s.logger.Debugf("check-in stream failed to receive data: %s", err)
}
close(recvDone)
return
}
appState.updateStatus(checkin, false)
}
}()
<-sendDone
return nil
}
// Actions implements the GRPC bi-direction stream connection for actions.
func (s *Server) Actions(server proto.ElasticAgent_ActionsServer) error {
firstRespChan := make(chan *proto.ActionResponse)
go func() {
// go func will not be leaked, because when the main function
// returns it will close the connection. that will cause this
// function to return.
observed, err := server.Recv()
if err != nil {
close(firstRespChan)
return
}
firstRespChan <- observed
}()
var ok bool
var firstResp *proto.ActionResponse
select {
case firstResp, ok = <-firstRespChan:
break
case <-time.After(InitialCheckinTimeout):
// close connection
s.logger.Debug("actions stream never sent initial response message; closing connection")
return nil
}
if !ok {
// close connection
return nil
}
if firstResp.Id != client.ActionResponseInitID {
// close connection
s.logger.Debug("actions stream first response message must be an init message; closing connection")
return status.Error(codes.InvalidArgument, "initial response must be an init message")
}
appState, ok := s.getByToken(firstResp.Token)
if !ok {
// no application with token; close connection
s.logger.Debug("actions stream sent an invalid token; closing connection")
return status.Error(codes.PermissionDenied, "invalid token")
}
appState.actionsLock.Lock()
if appState.actionsDone != nil {
// application is already connected (cannot have multiple); close connection
appState.actionsLock.Unlock()
s.logger.Debug("actions stream already exists for application; closing connection")
return status.Error(codes.AlreadyExists, "application already connected")
}
if !appState.actionsConn {
// application is being destroyed cannot reconnect; close connection
appState.actionsLock.Unlock()
s.logger.Debug("actions stream cannot connect, application is being destroyed; closing connection")
return status.Error(codes.Unavailable, "application cannot connect being destroyed")
}
actionsDone := make(chan bool)
appState.actionsDone = actionsDone
appState.actionsLock.Unlock()
defer func() {
appState.actionsLock.Lock()
appState.actionsDone = nil
appState.actionsLock.Unlock()
}()
// send the pending actions that need to be performed
recvDone := make(chan bool)
sendDone := make(chan bool)
go func() {
defer func() { close(sendDone) }()
for {
var pending *pendingAction
select {
case <-actionsDone:
return
case <-recvDone:
return
case pending = <-appState.pendingActions:
}
if pending.expiresOn.Sub(time.Now().UTC()) <= 0 {
// to late action already expired
pending.callback(nil, ErrActionTimedOut)
continue
}
appState.actionsLock.Lock()
err := server.Send(&proto.ActionRequest{
Id: pending.id,
Name: pending.name,
Params: pending.params,
})
if err != nil {
// failed to send action; add back to channel to retry on re-connect from the client
appState.actionsLock.Unlock()
appState.pendingActions <- pending
if reportableErr(err) {
s.logger.Debugf("failed to send pending action %s (will retry, after re-connect): %s", pending.id, err)
}
return
}
appState.sentActions[pending.id] = &sentAction{
callback: pending.callback,
expiresOn: pending.expiresOn,
}
appState.actionsLock.Unlock()
}
}()
// receive the finished actions
go func() {
for {
response, err := server.Recv()
if err != nil {
if reportableErr(err) {
s.logger.Debugf("actions stream failed to receive data: %s", err)
}
close(recvDone)
return
}
appState.actionsLock.Lock()
action, ok := appState.sentActions[response.Id]
if !ok {
// nothing to do, unknown action request
s.logger.Debugf("actions stream received an unknown action: %s", response.Id)
appState.actionsLock.Unlock()
continue
}
delete(appState.sentActions, response.Id)
appState.actionsLock.Unlock()
var result map[string]interface{}
err = json.Unmarshal(response.Result, &result)
if err != nil {
action.callback(nil, err)
} else if response.Status == proto.ActionResponse_FAILED {
errStr, ok := result["error"]
if ok {
err = fmt.Errorf("%s", errStr)
} else {
err = fmt.Errorf("unknown error")
}
action.callback(nil, err)
} else {
action.callback(result, nil)
}
}
}()
<-sendDone
return nil
}
// WriteConnInfo writes the connection information for the application into the writer.
//
// Note: If the writer implements io.Closer the writer is also closed.
func (as *ApplicationState) WriteConnInfo(w io.Writer) error {
connInfo := &proto.ConnInfo{
Addr: as.srv.getListenAddr(),
ServerName: as.srvName,
Token: as.token,
CaCert: as.srv.ca.Crt(),
PeerCert: as.cert.Crt,
PeerKey: as.cert.Key,
}
infoBytes, err := protobuf.Marshal(connInfo)
if err != nil {
return errors.New(err, "failed to marshal connection information", errors.TypeApplication)
}
_, err = w.Write(infoBytes)
if err != nil {
return errors.New(err, "failed to write connection information", errors.TypeApplication)
}
closer, ok := w.(io.Closer)
if ok {
_ = closer.Close()
}
return nil
}
// Stop instructs the application to stop gracefully within the timeout.
//
// Once the application is stopped or the timeout is reached the application is destroyed. Even in the case
// the application times out during stop and ErrApplication
func (as *ApplicationState) Stop(timeout time.Duration) error {
as.checkinLock.Lock()
wasConn := as.checkinDone != nil
cfgIdx := as.statusConfigIdx
as.expected = proto.StateExpected_STOPPING
as.checkinLock.Unlock()
// send it to the client if its connected, otherwise it will be sent once it connects.
as.sendExpectedState(&proto.StateExpected{
State: proto.StateExpected_STOPPING,
ConfigStateIdx: cfgIdx,
Config: "",
}, false)
started := time.Now().UTC()
for {
if time.Now().UTC().Sub(started) > timeout {
as.Destroy()
return ErrApplicationStopTimedOut
}
as.checkinLock.RLock()
s := as.status
doneChan := as.checkinDone
as.checkinLock.RUnlock()
if (wasConn && doneChan == nil) || (!wasConn && s == proto.StateObserved_STOPPING && doneChan == nil) {
// either occurred:
// * client was connected then disconnected on stop
// * client was not connected; connected; received stopping; then disconnected
as.Destroy()
return nil
}
<-time.After(500 * time.Millisecond)
}
}
// Destroy completely removes the application from the server without sending any stop command to the application.
//
// The ApplicationState at this point cannot be used.
func (as *ApplicationState) Destroy() {
as.destroyActionsStream()
as.destroyCheckinStream()
as.srv.apps.Delete(as.token)
}
// UpdateConfig pushes an updated configuration to the connected application.
func (as *ApplicationState) UpdateConfig(config string) error {
as.checkinLock.RLock()
expected := as.expected
currentCfg := as.expectedConfig
as.checkinLock.RUnlock()
if expected == proto.StateExpected_STOPPING {
return ErrApplicationStopping
}
if config == currentCfg {
// already at that expected config
return nil
}
as.checkinLock.Lock()
idx := as.expectedConfigIdx + 1
as.expectedConfigIdx = idx
as.expectedConfig = config
as.checkinLock.Unlock()
// send it to the client if its connected, otherwise it will be sent once it connects.
as.sendExpectedState(&proto.StateExpected{
State: expected,
ConfigStateIdx: idx,
Config: config,
}, false)
return nil
}
// PerformAction synchronously performs an action on the application.
func (as *ApplicationState) PerformAction(name string, params map[string]interface{}, timeout time.Duration) (map[string]interface{}, error) {
paramBytes, err := json.Marshal(params)
if err != nil {
return nil, err
}
id, err := uuid.NewV4()
if err != nil {
return nil, err
}
if !as.actionsConn {
// actions stream destroyed, action cancelled
return nil, ErrActionCancelled
}
resChan := make(chan actionResult)
as.pendingActions <- &pendingAction{
id: id.String(),
name: name,
params: paramBytes,
callback: func(m map[string]interface{}, err error) {
resChan <- actionResult{
result: m,
err: err,
}
},
expiresOn: time.Now().UTC().Add(timeout),
}
res := <-resChan
return res.result, res.err
}
// App returns the registered app for the state.
func (as *ApplicationState) App() interface{} {
return as.app
}
// Expected returns the expected state of the process.
func (as *ApplicationState) Expected() proto.StateExpected_State {
as.checkinLock.RLock()
defer as.checkinLock.RUnlock()
return as.expected
}
// Config returns the expected config of the process.
func (as *ApplicationState) Config() string {
as.checkinLock.RLock()
defer as.checkinLock.RUnlock()
return as.expectedConfig
}
// Status returns the current observed status.
func (as *ApplicationState) Status() (proto.StateObserved_Status, string, map[string]interface{}) {
as.checkinLock.RLock()
defer as.checkinLock.RUnlock()
return as.status, as.statusMessage, as.statusPayload
}
// SetStatus allows the status to be overwritten by the agent.
//
// This status will be overwritten by the client if it reconnects and updates it status.
func (as *ApplicationState) SetStatus(status proto.StateObserved_Status, msg string, payload map[string]interface{}) error {
payloadStr, err := json.Marshal(payload)
if err != nil {
return err
}
as.checkinLock.RLock()
as.status = status
as.statusMessage = msg
as.statusPayload = payload
as.statusPayloadStr = string(payloadStr)
as.checkinLock.RUnlock()
return nil
}
// SetInputTypes sets the allowed action input types for this application
func (as *ApplicationState) SetInputTypes(inputTypes []string) {
as.checkinLock.Lock()
as.inputTypes = make(map[string]struct{})
for _, inputType := range inputTypes {
as.inputTypes[inputType] = struct{}{}
}
as.checkinLock.Unlock()
}
// updateStatus updates the current observed status from the application, sends the expected state back to the
// application if the server expects it to be different then its observed state, and alerts the handler on the
// server when the application status has changed.
func (as *ApplicationState) updateStatus(checkin *proto.StateObserved, waitForReader bool) {
// convert payload from string to JSON
var payload map[string]interface{}
if checkin.Payload != "" {
// ignore the error, if client is sending bad JSON, then payload will just be nil
_ = json.Unmarshal([]byte(checkin.Payload), &payload)
}
as.checkinLock.Lock()
expectedStatus := as.expected
expectedConfigIdx := as.expectedConfigIdx
expectedConfig := as.expectedConfig
prevStatus := as.status
prevMessage := as.statusMessage
prevPayloadStr := as.statusPayloadStr
as.status = checkin.Status
as.statusMessage = checkin.Message
as.statusPayloadStr = checkin.Payload
as.statusPayload = payload
as.statusConfigIdx = checkin.ConfigStateIdx
as.statusTime = time.Now().UTC()
as.checkinLock.Unlock()
var expected *proto.StateExpected
if expectedStatus == proto.StateExpected_STOPPING && checkin.Status != proto.StateObserved_STOPPING {
expected = &proto.StateExpected{
State: expectedStatus,
ConfigStateIdx: checkin.ConfigStateIdx, // stopping always inform that the config it has is correct
Config: "",
}
} else if checkin.ConfigStateIdx != expectedConfigIdx {
expected = &proto.StateExpected{
State: expectedStatus,
ConfigStateIdx: expectedConfigIdx,
Config: expectedConfig,
}
}
if expected != nil {
as.sendExpectedState(expected, waitForReader)
}
// alert the service handler that status has changed for the application
if prevStatus != checkin.Status || prevMessage != checkin.Message || prevPayloadStr != checkin.Payload {
as.srv.handler.OnStatusChange(as, checkin.Status, checkin.Message, payload)
}
}
// sendExpectedState sends the expected status over the pendingExpected channel if the other side is
// waiting for a message.
func (as *ApplicationState) sendExpectedState(expected *proto.StateExpected, waitForReader bool) {
if waitForReader {
as.pendingExpected <- expected
return
}
select {
case as.pendingExpected <- expected:
default:
}
}
// destroyActionsStream disconnects the actions stream (prevent reconnect), cancel all pending actions
func (as *ApplicationState) destroyActionsStream() {
as.actionsLock.Lock()
as.actionsConn = false
if as.actionsDone != nil {
close(as.actionsDone)
as.actionsDone = nil
}
as.actionsLock.Unlock()
as.cancelActions()
}
// flushExpiredActions flushes any expired actions from the pending channel or current processing.
func (as *ApplicationState) flushExpiredActions() {
now := time.Now().UTC()
pendingActions := make([]*pendingAction, 0, len(as.pendingActions))
for {
done := false
select {
case pending := <-as.pendingActions:
pendingActions = append(pendingActions, pending)
default:
done = true
}
if done {
break
}
}
for _, pending := range pendingActions {
if pending.expiresOn.Sub(now) <= 0 {
pending.callback(nil, ErrActionTimedOut)
} else {
as.pendingActions <- pending
}
}
as.actionsLock.Lock()
for id, pendingResp := range as.sentActions {
if pendingResp.expiresOn.Sub(now) <= 0 {
delete(as.sentActions, id)
pendingResp.callback(nil, ErrActionTimedOut)
}
}
as.actionsLock.Unlock()
}
// cancelActions cancels all pending or currently processing actions.
func (as *ApplicationState) cancelActions() {
for {
done := false
select {
case pending := <-as.pendingActions:
pending.callback(nil, ErrActionCancelled)
default:
done = true
}
if done {
break
}
}
as.actionsLock.Lock()
for id, pendingResp := range as.sentActions {
delete(as.sentActions, id)
pendingResp.callback(nil, ErrActionCancelled)
}
as.actionsLock.Unlock()
}
// destroyCheckinStream disconnects the check stream (prevent reconnect).
func (as *ApplicationState) destroyCheckinStream() {
as.checkinLock.Lock()
as.checkinConn = false
if as.checkinDone != nil {
close(as.checkinDone)
as.checkinDone = nil
}
as.checkinLock.Unlock()
}
// watchdog ensures that the current applications are checking in during the correct intervals of time.
func (s *Server) watchdog() {
defer s.watchdogWG.Done()
for {
t := time.NewTimer(s.watchdogCheckInterval)
select {
case <-s.watchdogDone:
t.Stop()
return
case <-t.C:
}
now := time.Now().UTC()
s.apps.Range(func(_ interface{}, val interface{}) bool {
serverApp := val.(*ApplicationState)
serverApp.checkinLock.RLock()
statusTime := serverApp.statusTime
serverApp.checkinLock.RUnlock()
if now.Sub(statusTime) > s.checkInMinTimeout {
serverApp.checkinLock.Lock()
prevStatus := serverApp.status
s := prevStatus
prevMessage := serverApp.statusMessage
message := prevMessage
if serverApp.status == proto.StateObserved_DEGRADED {
s = proto.StateObserved_FAILED
message = "Missed two check-ins"
serverApp.status = s
serverApp.statusMessage = message
serverApp.statusPayload = nil
serverApp.statusPayloadStr = ""
serverApp.statusTime = now
} else if serverApp.status != proto.StateObserved_FAILED {
s = proto.StateObserved_DEGRADED
message = "Missed last check-in"
serverApp.status = s
serverApp.statusMessage = message
serverApp.statusPayload = nil
serverApp.statusPayloadStr = ""
serverApp.statusTime = now
}
serverApp.checkinLock.Unlock()
if prevStatus != s || prevMessage != message {
serverApp.srv.handler.OnStatusChange(serverApp, s, message, nil)
}
}
serverApp.flushExpiredActions()
return true
})
}
}
// getByToken returns an application state by its token.
func (s *Server) getByToken(token string) (*ApplicationState, bool) {
val, ok := s.apps.Load(token)
if ok {
return val.(*ApplicationState), true
}
return nil, false
}
// getCertificate returns the TLS certificate based on the clientHello or errors if not found.
func (s *Server) getCertificate(chi *tls.ClientHelloInfo) (*tls.Certificate, error) {
var cert *tls.Certificate
s.apps.Range(func(_ interface{}, val interface{}) bool {
sa := val.(*ApplicationState)
if sa.srvName == chi.ServerName {
cert = sa.cert.Certificate
return false
}
return true
})
if cert != nil {
return cert, nil
}
return nil, errors.New("no supported TLS certificate", errors.TypeSecurity)
}
// getListenAddr returns the listening address of the server.
func (s *Server) getListenAddr() string {
addr := strings.SplitN(s.listenAddr, ":", 2)
if len(addr) == 2 && addr[1] == "0" {
port := s.listener.Addr().(*net.TCPAddr).Port
return fmt.Sprintf("%s:%d", addr[0], port)
}
return s.listenAddr
}
type pendingAction struct {
id string
name string
params []byte
callback func(map[string]interface{}, error)
expiresOn time.Time
}
type sentAction struct {
callback func(map[string]interface{}, error)
expiresOn time.Time
}
type actionResult struct {
result map[string]interface{}
err error
}
func reportableErr(err error) bool {
if errors.Is(err, io.EOF) {
return false
}
s, ok := status.FromError(err)
if !ok {
return true
}
if s.Code() == codes.Canceled {
return false
}
return true
}
func genServerName() (string, error) {
u, err := uuid.NewV4()
if err != nil {
return "", err
}
return strings.Replace(u.String(), "-", "", -1), nil
}