Debian Codegraph @

api/v1/lib/extras/scheduler/controller/rules.go

raw symbols SCIP
1 package controller
2
3 import (
4 "context"
5 "fmt"
6 "log"
7 "time"
8
9 . "github.com/mesos/mesos-go/api/v1/lib/extras/scheduler/eventrules"
10 "github.com/mesos/mesos-go/api/v1/lib/extras/store"
11 "github.com/mesos/mesos-go/api/v1/lib/scheduler"
12 "github.com/mesos/mesos-go/api/v1/lib/scheduler/calls"
13 )
14
15 // ErrEvent errors are generated by LiftErrors upon receiving an ERROR event from Mesos.
16 type ErrEvent string
17
18 func (e ErrEvent) Error() string {
19 return string(e)
20 }
21
22 // LiftErrors extract the error message from a scheduler error event and returns it as an ErrEvent
23 // so that downstream rules/handlers may continue processing.
24 func LiftErrors() Rule {
25 return func(ctx context.Context, e *scheduler.Event, err error, chain Chain) (context.Context, *scheduler.Event, error) {
26 if err != nil {
27 return chain(ctx, e, err)
28 }
29 if e.GetType() == scheduler.Event_ERROR {
30 // it's recommended that we abort and re-try subscribing; returning an
31 // error here will cause the event loop to terminate and the connection
32 // will be reset.
33 return chain(ctx, e, ErrEvent(e.GetError().GetMessage()))
34 }
35 return chain(ctx, e, nil)
36 }
37 }
38
39 // StateError is returned when the system encounters an unresolvable state transition error and
40 // should likely exit.
41 type StateError string
42
43 func (err StateError) Error() string { return string(err) }
44
45 func TrackSubscription(frameworkIDStore store.Singleton, failoverTimeout time.Duration) Rule {
46 return func(ctx context.Context, e *scheduler.Event, err error, chain Chain) (context.Context, *scheduler.Event, error) {
47 if err != nil {
48 return chain(ctx, e, err)
49 }
50 if e.GetType() == scheduler.Event_SUBSCRIBED {
51 var (
52 storedFrameworkID, err = frameworkIDStore.Get()
53 frameworkID = e.GetSubscribed().GetFrameworkID().GetValue()
54 )
55 if err != nil && err != store.ErrNotFound {
56 return chain(ctx, e, err)
57 }
58 // order of `if` statements are important: tread carefully w/ respect to future changes
59 if frameworkID == "" {
60 // sanity check, should **never** happen
61 return chain(ctx, e, StateError("mesos sent an empty frameworkID?!"))
62 }
63 if storedFrameworkID != "" && storedFrameworkID != frameworkID && failoverTimeout > 0 {
64 return chain(ctx, e, StateError(fmt.Sprintf(
65 "frameworkID changed unexpectedly; failover exceeded timeout? (%s).", failoverTimeout)))
66 }
67 if storedFrameworkID != frameworkID {
68 frameworkIDStore.Set(frameworkID)
69 }
70 }
71 return chain(ctx, e, nil)
72 }
73 }
74
75 // AckStatusUpdates sends an acknowledgement of a task status update back to mesos and drops the event if
76 // sending the ack fails. If successful, the specified err param (if any) is forwarded. Acknowledgements
77 // are only attempted for task status updates tagged with a UUID.
78 func AckStatusUpdates(caller calls.Caller) Rule {
79 return AckStatusUpdatesF(func() calls.Caller { return caller })
80 }
81
82 // AckStatusUpdatesF is a functional adapter for AckStatusUpdates, useful for cases where the caller may
83 // change over time. An error that occurs while ack'ing the status update is returned as a calls.AckError.
84 func AckStatusUpdatesF(callerLookup func() calls.Caller) Rule {
85 return func(ctx context.Context, e *scheduler.Event, err error, chain Chain) (context.Context, *scheduler.Event, error) {
86 // aggressively attempt to ack updates: even if there's pre-existing error state attempt
87 // to acknowledge all status updates.
88 origErr := err
89 if e.GetType() == scheduler.Event_UPDATE {
90 var (
91 s = e.GetUpdate().GetStatus()
92 uuid = s.GetUUID()
93 )
94 // only ACK non-empty UUID's, as per mesos scheduler spec
95 if len(uuid) > 0 {
96 ack := calls.Acknowledge(
97 s.GetAgentID().GetValue(),
98 s.TaskID.Value,
99 uuid,
100 )
101 err = calls.CallNoData(ctx, callerLookup(), ack)
102 if err != nil {
103 // TODO(jdef): not sure how important this is; if there's an error ack'ing
104 // because we beacame disconnected, then we'll just reconnect later and
105 // Mesos will ask us to ACK anyway -- why pay special attention to these
106 // call failures vs others?
107 err = &calls.AckError{Ack: ack, Cause: err}
108 return ctx, e, Error2(origErr, err) // drop (do not propagate to chain)
109 }
110 }
111 }
112 return chain(ctx, e, origErr)
113 }
114 }
115
116 // DefaultEventLabel is, by default, logged as the first argument by DefaultEventLogger
117 const DefaultEventLabel = "event"
118
119 // DefaultEventLogger logs the event via the `log` package.
120 func DefaultEventLogger(eventLabel string) func(*scheduler.Event) {
121 if eventLabel == "" {
122 return func(e *scheduler.Event) { log.Println(e) }
123 }
124 return func(e *scheduler.Event) { log.Println(eventLabel, e) }
125 }
126
127 // LogEvents returns a rule that logs scheduler events to the EventLogger
128 func LogEvents(f func(*scheduler.Event)) Rule {
129 if f == nil {
130 f = DefaultEventLogger(DefaultEventLabel)
131 }
132 return Rule(func(ctx context.Context, e *scheduler.Event, err error, chain Chain) (context.Context, *scheduler.Event, error) {
133 f(e)
134 return chain(ctx, e, err)
135 })
136 }