Debian Codegraph @

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

raw symbols SCIP
1 package controller
2
3 import (
4 "context"
5
6 "github.com/mesos/mesos-go/api/v1/lib"
7 "github.com/mesos/mesos-go/api/v1/lib/encoding"
8 "github.com/mesos/mesos-go/api/v1/lib/scheduler"
9 "github.com/mesos/mesos-go/api/v1/lib/scheduler/calls"
10 "github.com/mesos/mesos-go/api/v1/lib/scheduler/events"
11 )
12
13 type (
14 // Option modifies a Config, returns an Option that acts as an "undo"
15 Option func(*Config) Option
16
17 // Config is an opaque controller configuration. Properties are configured by applying Option funcs.
18 Config struct {
19 frameworkIDFunc func() string
20 handler events.Handler
21 registrationTokens <-chan struct{}
22 subscriptionTerminated func(error)
23 }
24 )
25
26 // WithEventHandler sets the consumer of scheduler events. The controller's internal event processing
27 // loop is aborted if a Handler returns a non-nil error, after which the controller may attempt
28 // to re-register (subscribe) with Mesos.
29 func WithEventHandler(handler events.Handler) Option {
30 return func(c *Config) Option {
31 old := c.handler
32 c.handler = handler
33 return WithEventHandler(old)
34 }
35 }
36
37 // WithFrameworkID sets a fetcher for the current Mesos-assigned framework ID. Frameworks are expected to
38 // track this ID (that comes from Mesos, in a SUBSCRIBED event).
39 // frameworkIDFunc is optional; nil tells the controller to always register as a new framework
40 // for each subscription attempt.
41 func WithFrameworkID(frameworkIDFunc func() string) Option {
42 return func(c *Config) Option {
43 old := c.frameworkIDFunc
44 c.frameworkIDFunc = frameworkIDFunc
45 return WithFrameworkID(old)
46 }
47 }
48
49 // WithSubscriptionTerminated sets a handler that is invoked at the end of every subscription cycle; the
50 // given error may be nil if no error occurred. subscriptionTerminated is optional; if nil then errors are
51 // swallowed.
52 func WithSubscriptionTerminated(handler func(error)) Option {
53 return func(c *Config) Option {
54 old := c.subscriptionTerminated
55 c.subscriptionTerminated = handler
56 return WithSubscriptionTerminated(old)
57 }
58 }
59
60 // WithRegistrationTokens limits the rate at which a framework (re)registers with Mesos.
61 // A non-nil chan should yield a struct{} in order to allow the framework registration process to continue.
62 // When nil, there is no backoff delay between re-subscription attempts.
63 // A closed chan disables re-registration and terminates the Run control loop.
64 func WithRegistrationTokens(registrationTokens <-chan struct{}) Option {
65 return func(c *Config) Option {
66 old := c.registrationTokens
67 c.registrationTokens = registrationTokens
68 return WithRegistrationTokens(old)
69 }
70 }
71
72 func (c *Config) tryFrameworkID() (result string) {
73 if c.frameworkIDFunc != nil {
74 result = c.frameworkIDFunc()
75 }
76 return
77 }
78
79 func isDone(ctx context.Context) (result bool) {
80 select {
81 case <-ctx.Done():
82 return true
83 default:
84 return false
85 }
86 }
87
88 // Run executes a control loop that registers a framework with Mesos and processes the scheduler events
89 // that flow through the subscription. Upon disconnection, if the current configuration reports "not done"
90 // then the controller will attempt to re-register the framework and continue processing events.
91 func Run(ctx context.Context, framework *mesos.FrameworkInfo, caller calls.Caller, options ...Option) (lastErr error) {
92 var config Config
93 for _, opt := range options {
94 if opt != nil {
95 opt(&config)
96 }
97 }
98 if config.handler == nil {
99 config.handler = DefaultHandler
100 }
101 subscribe := calls.Subscribe(framework)
102 for !isDone(ctx) {
103 frameworkID := config.tryFrameworkID()
104 if framework.GetFailoverTimeout() > 0 && frameworkID != "" {
105 subscribe.With(calls.SubscribeTo(frameworkID))
106 }
107 if config.registrationTokens != nil {
108 select {
109 case _, ok := <-config.registrationTokens:
110 if !ok {
111 // re-registration canceled, exit Run loop
112 return
113 }
114 case <-ctx.Done():
115 return ctx.Err()
116 }
117 }
118 resp, err := caller.Call(ctx, subscribe)
119 lastErr = processSubscription(ctx, config, resp, err)
120 if config.subscriptionTerminated != nil {
121 config.subscriptionTerminated(lastErr)
122 }
123 }
124 return
125 }
126
127 func processSubscription(ctx context.Context, config Config, resp mesos.Response, err error) error {
128 if resp != nil {
129 defer resp.Close()
130 }
131 if err == nil {
132 err = eventLoop(ctx, config, resp)
133 }
134 return err
135 }
136
137 // eventLoop returns the framework ID received by mesos (if any); callers should check for a
138 // framework ID regardless of whether error != nil.
139 func eventLoop(ctx context.Context, config Config, eventDecoder encoding.Decoder) (err error) {
140 for err == nil && !isDone(ctx) {
141 var e scheduler.Event
142 if err = eventDecoder.Decode(&e); err == nil {
143 err = config.handler.HandleEvent(ctx, &e)
144 }
145 }
146 return err
147 }
148
149 // DefaultHandler is invoked when no other handlers have been defined for the controller.
150 // The current implementation does nothing.
151 // TODO(jdef) a smarter default impl would decline all offers so as to avoid resource hoarding.
152 const DefaultHandler = events.NoopHandler