Apache Mesos
cluster.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 __TESTS_CLUSTER_HPP__
18 #define __TESTS_CLUSTER_HPP__
19 
20 #include <memory>
21 #include <string>
22 
23 #include <mesos/mesos.hpp>
24 
26 
28 
29 #include <mesos/log/log.hpp>
30 
34 
36 
38 #include <mesos/state/log.hpp>
39 #include <mesos/state/state.hpp>
40 #include <mesos/state/storage.hpp>
41 
42 #include <mesos/zookeeper/url.hpp>
43 
44 #include <process/owned.hpp>
45 #include <process/pid.hpp>
46 
47 #include <stout/none.hpp>
48 #include <stout/option.hpp>
49 #include <stout/try.hpp>
50 
52 
53 #include "files/files.hpp"
54 
55 #include "master/constants.hpp"
56 #include "master/flags.hpp"
57 #include "master/master.hpp"
58 
59 #include "slave/constants.hpp"
60 #include "slave/flags.hpp"
61 #include "slave/gc.hpp"
62 #include "slave/slave.hpp"
64 
67 
68 #include "tests/mock_registrar.hpp"
69 #include "tests/mock_slave.hpp"
70 
71 namespace mesos {
72 namespace internal {
73 namespace tests {
74 namespace cluster {
75 
76 class Master
77 {
78 public:
79  // Factory method that starts a new master with the provided flags and
80  // injections. The destructor of this object will cleanup some, but not
81  // all of its state by calling the injections. Cleanup includes:
82  // * All destructors of each master dependency created in this factory.
83  // * Unsetting the libprocess authenticator.
84  // * Terminating the master process.
86  const master::Flags& flags = master::Flags(),
87  const Option<zookeeper::URL>& zookeeperUrl = None(),
88  const Option<mesos::allocator::Allocator*>& allocator = None(),
89  const Option<Authorizer*>& authorizer = None(),
90  const Option<std::shared_ptr<process::RateLimiter>>&
91  slaveRemovalLimiter = None());
92 
93  ~Master();
94 
95  // Returns a new master detector for this instance of master.
97 
98  // Returns the `MasterInfo` associated with the underlying master process.
99  MasterInfo getMasterInfo();
100 
101  // The underlying master process.
103 
104  // Sets authorization callbacks in libprocess.
105  void setAuthorizationCallbacks(Authorizer* authorizer);
106 
107 private:
109 
110  // Not copyable, not assignable.
111  Master(const Master&) = delete;
112  Master& operator=(const Master&) = delete;
113 
114  Option<zookeeper::URL> zookeeperUrl;
115  Files files;
116 
117  // Dependencies that are created by the factory method. The order in
118  // which these fields are declared should match the order in which
119  // they are initialized by the constructor; this ensures that
120  // dependencies between these fields are handled correctly during
121  // destruction.
122 
124  process::Owned<Authorizer> authorizer;
130 public:
131  // Exposed for testing and mocking purposes. We always use a
132  // `MockRegistrar` in case the test case wants to inspect how the
133  // master interacts with the registrar; by default, the mock
134  // registrar behaves identically to the normal registrar.
136 
137  // The underlying master object.
139 
140 private:
142 
143  // Indicates whether or not authorization callbacks were set when this master
144  // was constructed.
145  bool authorizationCallbacksSet;
146 };
147 
148 
149 class Slave
150 {
151 public:
152  // Factory method that creates a new slave with the provided flags and
153  // injections. The destructor of this object will cleanup some, but not
154  // all of its state by calling the injections. Cleanup includes:
155  // * All destructors of each slave dependency created in this factory.
156  // * If neither `terminate` nor `shutdown` are called, all containers
157  // will be destroyed before termination.
158  // * Terminating the slave process.
159  // * On Linux, we will simulate an OS process exiting.
162  const slave::Flags& flags = slave::Flags(),
163  const Option<std::string>& id = None(),
164  const Option<slave::Containerizer*>& containerizer = None(),
166  const Option<slave::TaskStatusUpdateManager*>& taskStatusUpdateManager =
167  None(),
168  const Option<mesos::slave::ResourceEstimator*>& resourceEstimator =
169  None(),
170  const Option<mesos::slave::QoSController*>& qosController = None(),
171  const Option<mesos::SecretGenerator*>& secretGenerator = None(),
172  const Option<Authorizer*>& authorizer = None(),
173  const Option<PendingFutureTracker*>& futureTracker = None(),
174  bool mock = false);
175 
176  ~Slave();
177 
178  // Stops this slave by either dispatching a shutdown call to the underlying
179  // slave process or terminating it. If either of these methods are called,
180  // this wrapper object will not clean up containers during its destruction.
181  // NOTE: Destroying the containerizer does not clean up containers.
182  //
183  // These methods are useful if the test wants to emulate the slave's
184  // shutdown/termination logic. For example, the slave-recovery tests do
185  // not want to destroy all containers when restarting the agent.
186  void shutdown();
187  void terminate();
188 
189  // Returns the pointer to the mock slave object. Returns nullptr if
190  // this is not a mock slave.
191  MockSlave* mock();
192 
193  // Start the mock slave as it is not auto started. This is a no-op
194  // if it is a real slave.
195  void start();
196 
197  // The underlying slave process.
199 
200  // Sets authorization callbacks in libprocess.
201  void setAuthorizationCallbacks(Authorizer* authorizer);
202 
203 private:
204  Slave() : files(slave::READONLY_HTTP_AUTHENTICATION_REALM) {};
205 
206  // Not copyable, not assignable.
207  Slave(const Slave&) = delete;
208  Slave& operator=(const Slave&) = delete;
209 
210  // Helper for `shutdown` and `terminate`.
211  // Waits for the underlying slave process to finish and then
212  // (Linux-only) simulates an OS process exiting.
213  void wait();
214 
216  Files files;
217 
218  // This is set to `false` if either `shutdown()` or `terminate()` are called.
219  // If false, the destructor of this `Slave` will not clean up containers.
220  bool cleanUpContainersInDestructor = true;
221 
222  // Master detector that is not managed by this object.
223  mesos::master::detector::MasterDetector* detector = nullptr;
224 
225  // Containerizer that is either owned outside of this `Slave` object
226  // or by `ownedContainerizer`. We keep a copy of this pointer
227  // because the cleanup logic acts upon the containerizer (regardless
228  // of who created it).
229  slave::Containerizer* containerizer = nullptr;
230 
231  // Pending future tracker must be destroyed last since there may be
232  // pending requests related to the dependant objects declared below.
234 
235  // Dependencies that are created by the factory method.
236  process::Owned<Authorizer> authorizer;
237  process::Owned<slave::Containerizer> ownedContainerizer;
243  process::Owned<slave::TaskStatusUpdateManager> taskStatusUpdateManager;
244 
245  // Indicates whether or not authorization callbacks were set when this agent
246  // was constructed.
247  bool authorizationCallbacksSet;
248 
249  // The underlying slave object.
251 };
252 
253 } // namespace cluster {
254 } // namespace tests {
255 } // namespace internal {
256 } // namespace mesos {
257 
258 #endif // __TESTS_CLUSTER_HPP__
Definition: master.hpp:27
process::PID< master::Master > pid
Definition: cluster.hpp:102
Definition: check.hpp:33
Definition: cluster.hpp:149
process::Owned< MockRegistrar > registrar
Definition: cluster.hpp:135
Definition: files.hpp:73
Definition: flags.hpp:39
void terminate(const UPID &pid, bool inject=true)
Sends a TerminateEvent to the given process.
This interface is used to enable an identity service or any other back end to check authorization pol...
Definition: authorizer.hpp:243
Definition: containerizer.hpp:63
process::Owned< mesos::master::detector::MasterDetector > createDetector()
Definition: agent.hpp:25
constexpr char READONLY_HTTP_AUTHENTICATION_REALM[]
Definition: mesos.hpp:109
bool wait(const UPID &pid, const Duration &duration=Seconds(-1))
Wait for the process to exit for no more than the specified seconds.
process::Owned< master::Master > master
Definition: cluster.hpp:138
static Try< process::Owned< Master > > start(const master::Flags &flags=master::Flags(), const Option< zookeeper::URL > &zookeeperUrl=None(), const Option< mesos::allocator::Allocator * > &allocator=None(), const Option< Authorizer * > &authorizer=None(), const Option< std::shared_ptr< process::RateLimiter >> &slaveRemovalLimiter=None())
Definition: cluster.hpp:76
process::PID< slave::Slave > pid
Definition: cluster.hpp:198
A "process identifier" used to uniquely identify a process when dispatching messages.
Definition: pid.hpp:289
Definition: none.hpp:27
Definition: attributes.hpp:24
Definition: mock_slave.hpp:88
An abstraction of a Master detector which can be used to detect the leading master from a group...
Definition: detector.hpp:38
void setAuthorizationCallbacks(Authorizer *authorizer)
Try< Nothing > create(const std::string &hierarchy, const std::string &cgroup, bool recursive=false)
Definition: parse.hpp:33