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/csi_server.hpp"
61 #include "slave/flags.hpp"
62 #include "slave/gc.hpp"
63 #include "slave/slave.hpp"
65 
68 
69 #include "tests/mock_registrar.hpp"
70 #include "tests/mock_slave.hpp"
71 
72 namespace mesos {
73 namespace internal {
74 namespace tests {
75 namespace cluster {
76 
77 class Master
78 {
79 public:
80  // Factory method that starts a new master with the provided flags and
81  // injections. The destructor of this object will cleanup some, but not
82  // all of its state by calling the injections. Cleanup includes:
83  // * All destructors of each master dependency created in this factory.
84  // * Unsetting the libprocess authenticator.
85  // * Terminating the master process.
87  const master::Flags& flags = master::Flags(),
88  const Option<zookeeper::URL>& zookeeperUrl = None(),
89  const Option<mesos::allocator::Allocator*>& allocator = None(),
90  const Option<Authorizer*>& authorizer = None(),
91  const Option<std::shared_ptr<process::RateLimiter>>&
92  slaveRemovalLimiter = None());
93 
94  ~Master();
95 
96  // Returns a new master detector for this instance of master.
98 
99  // Returns the `MasterInfo` associated with the underlying master process.
100  MasterInfo getMasterInfo();
101 
102  // The underlying master process.
104 
105  // Sets authorization callbacks in libprocess.
106  void setAuthorizationCallbacks(Authorizer* authorizer);
107 
108 private:
110 
111  // Not copyable, not assignable.
112  Master(const Master&) = delete;
113  Master& operator=(const Master&) = delete;
114 
115  Option<zookeeper::URL> zookeeperUrl;
116  Files files;
117 
118  // Dependencies that are created by the factory method. The order in
119  // which these fields are declared should match the order in which
120  // they are initialized by the constructor; this ensures that
121  // dependencies between these fields are handled correctly during
122  // destruction.
123 
125  process::Owned<Authorizer> authorizer;
131 public:
132  // Exposed for testing and mocking purposes. We always use a
133  // `MockRegistrar` in case the test case wants to inspect how the
134  // master interacts with the registrar; by default, the mock
135  // registrar behaves identically to the normal registrar.
137 
138  // The underlying master object.
140 
141 private:
143 
144  // Indicates whether or not authorization callbacks were set when this master
145  // was constructed.
146  bool authorizationCallbacksSet;
147 };
148 
149 
150 class Slave
151 {
152 public:
153  // Factory method that creates a new slave with the provided flags and
154  // injections. The destructor of this object will cleanup some, but not
155  // all of its state by calling the injections. Cleanup includes:
156  // * All destructors of each slave dependency created in this factory.
157  // * If neither `terminate` nor `shutdown` are called, all containers
158  // will be destroyed before termination.
159  // * Terminating the slave process.
160  // * On Linux, we will simulate an OS process exiting.
163  const slave::Flags& flags = slave::Flags(),
164  const Option<std::string>& id = None(),
165  const Option<slave::Containerizer*>& containerizer = None(),
167  const Option<slave::TaskStatusUpdateManager*>& taskStatusUpdateManager =
168  None(),
169  const Option<mesos::slave::ResourceEstimator*>& resourceEstimator =
170  None(),
171  const Option<mesos::slave::QoSController*>& qosController = None(),
172  const Option<mesos::SecretGenerator*>& secretGenerator = None(),
173  const Option<Authorizer*>& authorizer = None(),
174  const Option<PendingFutureTracker*>& futureTracker = None(),
175  const Option<process::Owned<slave::CSIServer>>& csiServer = None(),
176  bool mock = false);
177 
178  ~Slave();
179 
180  // Stops this slave by either dispatching a shutdown call to the underlying
181  // slave process or terminating it. If either of these methods are called,
182  // this wrapper object will not clean up containers during its destruction.
183  // NOTE: Destroying the containerizer does not clean up containers.
184  //
185  // These methods are useful if the test wants to emulate the slave's
186  // shutdown/termination logic. For example, the slave-recovery tests do
187  // not want to destroy all containers when restarting the agent.
188  void shutdown();
189  void terminate();
190 
191  // Returns the pointer to the mock slave object. Returns nullptr if
192  // this is not a mock slave.
193  MockSlave* mock();
194 
195  // Start the mock slave as it is not auto started. This is a no-op
196  // if it is a real slave.
197  void start();
198 
199  // The underlying slave process.
201 
202  // Sets authorization callbacks in libprocess.
203  void setAuthorizationCallbacks(Authorizer* authorizer);
204 
205 private:
206  Slave() : files(slave::READONLY_HTTP_AUTHENTICATION_REALM) {};
207 
208  // Not copyable, not assignable.
209  Slave(const Slave&) = delete;
210  Slave& operator=(const Slave&) = delete;
211 
212  // Helper for `shutdown` and `terminate`.
213  // Waits for the underlying slave process to finish and then
214  // (Linux-only) simulates an OS process exiting.
215  void wait();
216 
218  Files files;
219 
220  // This is set to `false` if either `shutdown()` or `terminate()` are called.
221  // If false, the destructor of this `Slave` will not clean up containers.
222  bool cleanUpContainersInDestructor = true;
223 
224  // Master detector that is not managed by this object.
225  mesos::master::detector::MasterDetector* detector = nullptr;
226 
227  // Containerizer that is either owned outside of this `Slave` object
228  // or by `ownedContainerizer`. We keep a copy of this pointer
229  // because the cleanup logic acts upon the containerizer (regardless
230  // of who created it).
231  slave::Containerizer* containerizer = nullptr;
232 
233  // Pending future tracker must be destroyed last since there may be
234  // pending requests related to the dependant objects declared below.
236 
237  // Dependencies that are created by the factory method.
238  process::Owned<Authorizer> authorizer;
239  process::Owned<slave::Containerizer> ownedContainerizer;
245  process::Owned<slave::TaskStatusUpdateManager> taskStatusUpdateManager;
247 
248  // Indicates whether or not authorization callbacks were set when this agent
249  // was constructed.
250  bool authorizationCallbacksSet;
251 
252  // The underlying slave object.
254 };
255 
256 } // namespace cluster {
257 } // namespace tests {
258 } // namespace internal {
259 } // namespace mesos {
260 
261 #endif // __TESTS_CLUSTER_HPP__
Definition: master.hpp:27
process::PID< master::Master > pid
Definition: cluster.hpp:103
Definition: check.hpp:33
Definition: cluster.hpp:150
process::Owned< MockRegistrar > registrar
Definition: cluster.hpp:136
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:268
Definition: containerizer.hpp:64
process::Owned< mesos::master::detector::MasterDetector > createDetector()
Definition: agent.hpp:25
constexpr char READONLY_HTTP_AUTHENTICATION_REALM[]
Definition: mesos.hpp:111
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:139
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:77
process::PID< slave::Slave > pid
Definition: cluster.hpp:200
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:90
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