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 private:
139 
140  // Indicates whether or not authorization callbacks were set when this master
141  // was constructed.
142  bool authorizationCallbacksSet;
143 
144  // The underlying master object.
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  bool mock = false);
174 
175  ~Slave();
176 
177  // Stops this slave by either dispatching a shutdown call to the underlying
178  // slave process or terminating it. If either of these methods are called,
179  // this wrapper object will not clean up containers during its destruction.
180  // NOTE: Destroying the containerizer does not clean up containers.
181  //
182  // These methods are useful if the test wants to emulate the slave's
183  // shutdown/termination logic. For example, the slave-recovery tests do
184  // not want to destroy all containers when restarting the agent.
185  void shutdown();
186  void terminate();
187 
188  // Returns the pointer to the mock slave object. Returns nullptr if
189  // this is not a mock slave.
190  MockSlave* mock();
191 
192  // Start the mock slave as it is not auto started. This is a no-op
193  // if it is a real slave.
194  void start();
195 
196  // The underlying slave process.
198 
199  // Sets authorization callbacks in libprocess.
200  void setAuthorizationCallbacks(Authorizer* authorizer);
201 
202 private:
203  Slave() : files(slave::READONLY_HTTP_AUTHENTICATION_REALM) {};
204 
205  // Not copyable, not assignable.
206  Slave(const Slave&) = delete;
207  Slave& operator=(const Slave&) = delete;
208 
209  // Helper for `shutdown` and `terminate`.
210  // Waits for the underlying slave process to finish and then
211  // (Linux-only) simulates an OS process exiting.
212  void wait();
213 
215  Files files;
216 
217  // This is set to `false` if either `shutdown()` or `terminate()` are called.
218  // If false, the destructor of this `Slave` will not clean up containers.
219  bool cleanUpContainersInDestructor = true;
220 
221  // Master detector that is not managed by this object.
222  mesos::master::detector::MasterDetector* detector = nullptr;
223 
224  // Containerizer that is either owned outside of this `Slave` object
225  // or by `ownedContainerizer`. We keep a copy of this pointer
226  // because the cleanup logic acts upon the containerizer (regardless
227  // of who created it).
228  slave::Containerizer* containerizer = nullptr;
229 
230  // Dependencies that are created by the factory method.
231  process::Owned<Authorizer> authorizer;
232  process::Owned<slave::Containerizer> ownedContainerizer;
238  process::Owned<slave::TaskStatusUpdateManager> taskStatusUpdateManager;
239 
240  // Indicates whether or not authorization callbacks were set when this agent
241  // was constructed.
242  bool authorizationCallbacksSet;
243 
244  // The underlying slave object.
246 };
247 
248 } // namespace cluster {
249 } // namespace tests {
250 } // namespace internal {
251 } // namespace mesos {
252 
253 #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:59
process::Owned< mesos::master::detector::MasterDetector > createDetector()
Definition: spec.hpp:26
constexpr char READONLY_HTTP_AUTHENTICATION_REALM[]
Definition: mesos.hpp:108
bool wait(const UPID &pid, const Duration &duration=Seconds(-1))
Wait for the process to exit for no more than the specified seconds.
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:197
A "process identifier" used to uniquely identify a process when dispatching messages.
Definition: pid.hpp:279
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