Apache Mesos
scheduler.hpp
Go to the documentation of this file.
1 // Licensed to the Apache Software Foundation (ASF) under one
2 // or more contributor license agreements. See the NOTICE file
3 // distributed with this work for additional information
4 // regarding copyright ownership. The ASF licenses this file
5 // to you under the Apache License, Version 2.0 (the
6 // "License"); you may not use this file except in compliance
7 // with the License. You may obtain a copy of the License at
8 //
9 // http://www.apache.org/licenses/LICENSE-2.0
10 //
11 // Unless required by applicable law or agreed to in writing, software
12 // distributed under the License is distributed on an "AS IS" BASIS,
13 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 // See the License for the specific language governing permissions and
15 // limitations under the License.
16 
17 #ifndef __MESOS_V1_SCHEDULER_HPP__
18 #define __MESOS_V1_SCHEDULER_HPP__
19 
20 #include <functional>
21 #include <memory>
22 #include <queue>
23 #include <string>
24 
25 #include <mesos/http.hpp>
26 
27 #include <mesos/v1/mesos.hpp>
28 
30 
31 namespace mesos {
32 
33 namespace master {
34 namespace detector {
35 class MasterDetector;
36 } // namespace detector {
37 } // namespace master {
38 
39 namespace v1 {
40 namespace scheduler {
41 
42 class MesosProcess; // Forward declaration.
43 
44 // Abstract interface for connecting a scheduler to Mesos.
45 class MesosBase
46 {
47 public:
48  // Empty virtual destructor (necessary to instantiate subclasses).
49  virtual ~MesosBase() {}
50  virtual void send(const Call& call) = 0;
51  virtual void reconnect() = 0;
52 };
53 
54 
55 // Concrete implementation that connects a scheduler to a Mesos master.
56 // Abstracts master detection (connection and disconnection).
57 //
58 // Expects three callbacks, 'connected', 'disconnected', and
59 // 'received' which will get invoked _serially_ when it's determined
60 // that we've connected (i.e., detected master), disconnected
61 // (i.e, detected no master), or received events from the master.
62 // The library reconnects with the master upon a disconnection.
63 //
64 // NOTE: All calls and events are dropped while disconnected.
65 class Mesos : public MesosBase
66 {
67 public:
68  // The credential will be used for authenticating with the master. Currently,
69  // only HTTP basic authentication is supported.
70  Mesos(const std::string& master,
71  ContentType contentType,
72  const std::function<void()>& connected,
73  const std::function<void()>& disconnected,
74  const std::function<void(const std::queue<Event>&)>& received,
75  const Option<Credential>& credential);
76 
77  // Delete copy constructor.
78  Mesos(const Mesos& other) = delete;
79 
80  // Delete assignment operator.
81  Mesos& operator=(const Mesos& other) = delete;
82 
83  virtual ~Mesos();
84 
85  // Attempts to send a call to the master.
86  //
87  // The scheduler should only invoke this method once it has received the
88  // 'connected' callback. Otherwise, all calls would be dropped while
89  // disconnected.
90  //
91  // Some local validation of calls is performed which may generate
92  // events without ever being sent to the master. This includes when
93  // calls are sent but no master is currently detected (i.e., we're
94  // disconnected).
95  virtual void send(const Call& call) override;
96 
97  // Force a reconnection with the master.
98  //
99  // In the case of a one-way network partition, the connection between the
100  // scheduler and master might not necessarily break. If the scheduler detects
101  // a partition, due to lack of `HEARTBEAT` events (e.g., 5) within a time
102  // window, it can explicitly ask the library to force a reconnection with
103  // the master.
104  //
105  // This call would be ignored if the scheduler is already disconnected with
106  // the master (e.g., no new master has been elected). Otherwise, the scheduler
107  // would get a 'disconnected' callback followed by a 'connected' callback.
108  virtual void reconnect() override;
109 
110 protected:
111  // NOTE: This constructor is used for testing.
112  Mesos(
113  const std::string& master,
114  ContentType contentType,
115  const std::function<void()>& connected,
116  const std::function<void()>& disconnected,
117  const std::function<void(const std::queue<Event>&)>& received,
118  const Option<Credential>& credential,
119  const Option<std::shared_ptr<mesos::master::detector::MasterDetector>>&
120  detector);
121 
122  // Stops the library so that:
123  // - No more calls can be sent to the master.
124  // - No more callbacks can be made to the scheduler. In some cases, there
125  // may be one additional callback if the library was in the middle of
126  // processing an event.
127  //
128  // NOTE: This is used for testing.
129  virtual void stop();
130 
131 private:
132  MesosProcess* process;
133 };
134 
135 } // namespace scheduler {
136 } // namespace v1 {
137 } // namespace mesos {
138 
139 #endif // __MESOS_V1_SCHEDULER_HPP__
ContentType
Definition: http.hpp:43
virtual void send(const Call &call)=0
Mesos(const std::string &master, ContentType contentType, const std::function< void()> &connected, const std::function< void()> &disconnected, const std::function< void(const std::queue< Event > &)> &received, const Option< Credential > &credential)
mesos::v1::scheduler::Call Call
Definition: mesos.hpp:2583
Definition: scheduler.hpp:65
virtual void send(const Call &call) override
virtual void reconnect() override
Definition: scheduler.hpp:45
Mesos & operator=(const Mesos &other)=delete
virtual ~MesosBase()
Definition: scheduler.hpp:49