Apache Mesos
allocator.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_ALLOCATOR_HPP__
18 #define __TESTS_ALLOCATOR_HPP__
19 
20 #include <gmock/gmock.h>
21 
23 
24 #include <process/future.hpp>
25 #include <process/gmock.hpp>
26 #include <process/owned.hpp>
27 
29 
30 using ::testing::_;
31 using ::testing::An;
32 using ::testing::DoDefault;
33 using ::testing::Invoke;
34 using ::testing::Return;
35 
36 namespace mesos {
37 namespace internal {
38 namespace tests {
39 
40 // The following actions make up for the fact that DoDefault
41 // cannot be used inside a DoAll, for example:
42 // EXPECT_CALL(allocator, addFramework(_, _, _, _, _))
43 // .WillOnce(DoAll(InvokeAddFramework(&allocator),
44 // FutureSatisfy(&addFramework)));
45 
46 ACTION_P(InvokeInitialize, allocator)
47 {
48  allocator->real->initialize(arg0, arg1, arg2, arg3, arg4);
49 }
50 
51 
52 ACTION_P(InvokeRecover, allocator)
53 {
54  allocator->real->recover(arg0, arg1);
55 }
56 
57 
58 ACTION_P(InvokeAddFramework, allocator)
59 {
60  allocator->real->addFramework(arg0, arg1, arg2, arg3, arg4);
61 }
62 
63 
64 ACTION_P(InvokeRemoveFramework, allocator)
65 {
66  allocator->real->removeFramework(arg0);
67 }
68 
69 
70 ACTION_P(InvokeActivateFramework, allocator)
71 {
72  allocator->real->activateFramework(arg0);
73 }
74 
75 
76 ACTION_P(InvokeDeactivateFramework, allocator)
77 {
78  allocator->real->deactivateFramework(arg0);
79 }
80 
81 
82 ACTION_P(InvokeUpdateFramework, allocator)
83 {
84  allocator->real->updateFramework(arg0, arg1, arg2);
85 }
86 
87 
88 ACTION_P(InvokeAddSlave, allocator)
89 {
90  allocator->real->addSlave(arg0, arg1, arg2, arg3, arg4, arg5);
91 }
92 
93 
94 ACTION_P(InvokeRemoveSlave, allocator)
95 {
96  allocator->real->removeSlave(arg0);
97 }
98 
99 
100 ACTION_P(InvokeUpdateSlave, allocator)
101 {
102  allocator->real->updateSlave(arg0, arg1, arg2, arg3);
103 }
104 
105 
106 ACTION_P(InvokeAddResourceProvider, allocator)
107 {
108  allocator->real->addResourceProvider(arg0, arg1, arg2);
109 }
110 
111 
112 ACTION_P(InvokeActivateSlave, allocator)
113 {
114  allocator->real->activateSlave(arg0);
115 }
116 
117 
118 ACTION_P(InvokeDeactivateSlave, allocator)
119 {
120  allocator->real->deactivateSlave(arg0);
121 }
122 
123 
124 ACTION_P(InvokeUpdateWhitelist, allocator)
125 {
126  allocator->real->updateWhitelist(arg0);
127 }
128 
129 
130 ACTION_P(InvokeRequestResources, allocator)
131 {
132  allocator->real->requestResources(arg0, arg1);
133 }
134 
135 
136 ACTION_P(InvokeUpdateAllocation, allocator)
137 {
138  allocator->real->updateAllocation(arg0, arg1, arg2, arg3);
139 }
140 
141 
142 ACTION_P(InvokeUpdateAvailable, allocator)
143 {
144  return allocator->real->updateAvailable(arg0, arg1);
145 }
146 
147 
148 ACTION_P(InvokeUpdateUnavailability, allocator)
149 {
150  return allocator->real->updateUnavailability(arg0, arg1);
151 }
152 
153 
154 ACTION_P(InvokeUpdateInverseOffer, allocator)
155 {
156  return allocator->real->updateInverseOffer(arg0, arg1, arg2, arg3, arg4);
157 }
158 
159 
160 ACTION_P(InvokeGetInverseOfferStatuses, allocator)
161 {
162  return allocator->real->getInverseOfferStatuses();
163 }
164 
165 
166 ACTION_P(InvokeRecoverResources, allocator)
167 {
168  allocator->real->recoverResources(arg0, arg1, arg2, arg3);
169 }
170 
171 
172 ACTION_P2(InvokeRecoverResourcesWithFilters, allocator, timeout)
173 {
174  Filters filters;
175  filters.set_refuse_seconds(timeout);
176 
177  allocator->real->recoverResources(arg0, arg1, arg2, filters);
178 }
179 
180 
181 ACTION_P(InvokeSuppressOffers, allocator)
182 {
183  allocator->real->suppressOffers(arg0, arg1);
184 }
185 
186 
187 ACTION_P(InvokeReviveOffers, allocator)
188 {
189  allocator->real->reviveOffers(arg0, arg1);
190 }
191 
192 
193 ACTION_P(InvokeSetQuota, allocator)
194 {
195  allocator->real->setQuota(arg0, arg1);
196 }
197 
198 
199 ACTION_P(InvokeRemoveQuota, allocator)
200 {
201  allocator->real->removeQuota(arg0);
202 }
203 
204 
205 ACTION_P(InvokeUpdateWeights, allocator)
206 {
207  allocator->real->updateWeights(arg0);
208 }
209 
210 
211 template <typename T = master::allocator::HierarchicalDRFAllocator>
213 {
214  // T represents the allocator type. It can be a default built-in
215  // allocator, or one provided by an allocator module.
217  CHECK_SOME(instance);
218  return CHECK_NOTNULL(instance.get());
219 }
220 
221 template <typename T = master::allocator::HierarchicalDRFAllocator>
223 {
224 public:
225  // Actual allocation is done by an instance of real allocator,
226  // which is specified by the template parameter.
228  {
229  // We use 'ON_CALL' and 'WillByDefault' here to specify the
230  // default actions (call in to the real allocator). This allows
231  // the tests to leverage the 'DoDefault' action.
232  // However, 'ON_CALL' results in a "Uninteresting mock function
233  // call" warning unless each test puts expectations in place.
234  // As a result, we also use 'EXPECT_CALL' and 'WillRepeatedly'
235  // to get the best of both worlds: the ability to use 'DoDefault'
236  // and no warnings when expectations are not explicit.
237 
238  ON_CALL(*this, initialize(_, _, _, _, _, _))
239  .WillByDefault(InvokeInitialize(this));
240  EXPECT_CALL(*this, initialize(_, _, _, _, _, _))
241  .WillRepeatedly(DoDefault());
242 
243  ON_CALL(*this, recover(_, _))
244  .WillByDefault(InvokeRecover(this));
245  EXPECT_CALL(*this, recover(_, _))
246  .WillRepeatedly(DoDefault());
247 
248  ON_CALL(*this, addFramework(_, _, _, _, _))
249  .WillByDefault(InvokeAddFramework(this));
250  EXPECT_CALL(*this, addFramework(_, _, _, _, _))
251  .WillRepeatedly(DoDefault());
252 
253  ON_CALL(*this, removeFramework(_))
254  .WillByDefault(InvokeRemoveFramework(this));
255  EXPECT_CALL(*this, removeFramework(_))
256  .WillRepeatedly(DoDefault());
257 
258  ON_CALL(*this, activateFramework(_))
259  .WillByDefault(InvokeActivateFramework(this));
260  EXPECT_CALL(*this, activateFramework(_))
261  .WillRepeatedly(DoDefault());
262 
263  ON_CALL(*this, deactivateFramework(_))
264  .WillByDefault(InvokeDeactivateFramework(this));
265  EXPECT_CALL(*this, deactivateFramework(_))
266  .WillRepeatedly(DoDefault());
267 
268  ON_CALL(*this, updateFramework(_, _, _))
269  .WillByDefault(InvokeUpdateFramework(this));
270  EXPECT_CALL(*this, updateFramework(_, _, _))
271  .WillRepeatedly(DoDefault());
272 
273  ON_CALL(*this, addSlave(_, _, _, _, _, _))
274  .WillByDefault(InvokeAddSlave(this));
275  EXPECT_CALL(*this, addSlave(_, _, _, _, _, _))
276  .WillRepeatedly(DoDefault());
277 
278  ON_CALL(*this, removeSlave(_))
279  .WillByDefault(InvokeRemoveSlave(this));
280  EXPECT_CALL(*this, removeSlave(_))
281  .WillRepeatedly(DoDefault());
282 
283  ON_CALL(*this, updateSlave(_, _, _, _))
284  .WillByDefault(InvokeUpdateSlave(this));
285  EXPECT_CALL(*this, updateSlave(_, _, _, _))
286  .WillRepeatedly(DoDefault());
287 
288  ON_CALL(*this, addResourceProvider(_, _, _))
289  .WillByDefault(InvokeAddResourceProvider(this));
290  EXPECT_CALL(*this, addResourceProvider(_, _, _))
291  .WillRepeatedly(DoDefault());
292 
293  ON_CALL(*this, activateSlave(_))
294  .WillByDefault(InvokeActivateSlave(this));
295  EXPECT_CALL(*this, activateSlave(_))
296  .WillRepeatedly(DoDefault());
297 
298  ON_CALL(*this, deactivateSlave(_))
299  .WillByDefault(InvokeDeactivateSlave(this));
300  EXPECT_CALL(*this, deactivateSlave(_))
301  .WillRepeatedly(DoDefault());
302 
303  ON_CALL(*this, updateWhitelist(_))
304  .WillByDefault(InvokeUpdateWhitelist(this));
305  EXPECT_CALL(*this, updateWhitelist(_))
306  .WillRepeatedly(DoDefault());
307 
308  ON_CALL(*this, requestResources(_, _))
309  .WillByDefault(InvokeRequestResources(this));
310  EXPECT_CALL(*this, requestResources(_, _))
311  .WillRepeatedly(DoDefault());
312 
313  ON_CALL(*this, updateAllocation(_, _, _, _))
314  .WillByDefault(InvokeUpdateAllocation(this));
315  EXPECT_CALL(*this, updateAllocation(_, _, _, _))
316  .WillRepeatedly(DoDefault());
317 
318  ON_CALL(*this, updateAvailable(_, _))
319  .WillByDefault(InvokeUpdateAvailable(this));
320  EXPECT_CALL(*this, updateAvailable(_, _))
321  .WillRepeatedly(DoDefault());
322 
323  ON_CALL(*this, updateUnavailability(_, _))
324  .WillByDefault(InvokeUpdateUnavailability(this));
325  EXPECT_CALL(*this, updateUnavailability(_, _))
326  .WillRepeatedly(DoDefault());
327 
328  ON_CALL(*this, updateInverseOffer(_, _, _, _, _))
329  .WillByDefault(InvokeUpdateInverseOffer(this));
330  EXPECT_CALL(*this, updateInverseOffer(_, _, _, _, _))
331  .WillRepeatedly(DoDefault());
332 
333  ON_CALL(*this, getInverseOfferStatuses())
334  .WillByDefault(InvokeGetInverseOfferStatuses(this));
335  EXPECT_CALL(*this, getInverseOfferStatuses())
336  .WillRepeatedly(DoDefault());
337 
338  ON_CALL(*this, recoverResources(_, _, _, _))
339  .WillByDefault(InvokeRecoverResources(this));
340  EXPECT_CALL(*this, recoverResources(_, _, _, _))
341  .WillRepeatedly(DoDefault());
342 
343  ON_CALL(*this, suppressOffers(_, _))
344  .WillByDefault(InvokeSuppressOffers(this));
345  EXPECT_CALL(*this, suppressOffers(_, _))
346  .WillRepeatedly(DoDefault());
347 
348  ON_CALL(*this, reviveOffers(_, _))
349  .WillByDefault(InvokeReviveOffers(this));
350  EXPECT_CALL(*this, reviveOffers(_, _))
351  .WillRepeatedly(DoDefault());
352 
353  ON_CALL(*this, setQuota(_, _))
354  .WillByDefault(InvokeSetQuota(this));
355  EXPECT_CALL(*this, setQuota(_, _))
356  .WillRepeatedly(DoDefault());
357 
358  ON_CALL(*this, removeQuota(_))
359  .WillByDefault(InvokeRemoveQuota(this));
360  EXPECT_CALL(*this, removeQuota(_))
361  .WillRepeatedly(DoDefault());
362 
363  ON_CALL(*this, updateWeights(_))
364  .WillByDefault(InvokeUpdateWeights(this));
365  EXPECT_CALL(*this, updateWeights(_))
366  .WillRepeatedly(DoDefault());
367  }
368 
369  virtual ~TestAllocator() {}
370 
371  MOCK_METHOD6(initialize, void(
372  const Duration&,
373  const lambda::function<
374  void(const FrameworkID&,
375  const hashmap<std::string, hashmap<SlaveID, Resources>>&)>&,
376  const lambda::function<
377  void(const FrameworkID&,
379  const Option<std::set<std::string>>&,
380  bool,
381  const Option<DomainInfo>&));
382 
383  MOCK_METHOD2(recover, void(
384  const int expectedAgentCount,
386 
388  const FrameworkID&,
389  const FrameworkInfo&,
391  bool active,
392  const std::set<std::string>&));
393 
395  const FrameworkID&));
396 
398  const FrameworkID&));
399 
401  const FrameworkID&));
402 
404  const FrameworkID&,
405  const FrameworkInfo&,
406  const std::set<std::string>&));
407 
408  MOCK_METHOD6(addSlave, void(
409  const SlaveID&,
410  const SlaveInfo&,
411  const std::vector<SlaveInfo::Capability>&,
412  const Option<Unavailability>&,
413  const Resources&,
415 
417  const SlaveID&));
418 
420  const SlaveID&,
421  const SlaveInfo&,
422  const Option<Resources>&,
423  const Option<std::vector<SlaveInfo::Capability>>&));
424 
426  const SlaveID&,
427  const Resources&,
429 
431  const SlaveID&));
432 
434  const SlaveID&));
435 
437  const Option<hashset<std::string>>&));
438 
440  const FrameworkID&,
441  const std::vector<Request>&));
442 
444  const FrameworkID&,
445  const SlaveID&,
446  const Resources&,
447  const std::vector<ResourceConversion>&));
448 
450  const SlaveID&,
451  const std::vector<Offer::Operation>&));
452 
454  const SlaveID&,
455  const Option<Unavailability>&));
456 
458  const SlaveID&,
459  const FrameworkID&,
462  const Option<Filters>&));
463 
465  hashmap<SlaveID, hashmap<
466  FrameworkID,
467  mesos::allocator::InverseOfferStatus>>>());
468 
470  const FrameworkID&,
471  const SlaveID&,
472  const Resources&,
473  const Option<Filters>& filters));
474 
476  const FrameworkID&,
477  const std::set<std::string>&));
478 
480  const FrameworkID&,
481  const std::set<std::string>&));
482 
483  MOCK_METHOD2(setQuota, void(
484  const std::string&,
485  const Quota&));
486 
488  const std::string&));
489 
491  const std::vector<WeightInfo>&));
492 
494 };
495 
496 } // namespace tests {
497 } // namespace internal {
498 } // namespace mesos {
499 
500 #endif // __TESTS_ALLOCATOR_HPP__
virtual void recoverResources(const FrameworkID &frameworkId, const SlaveID &slaveId, const Resources &resources, const Option< Filters > &filters)=0
Recovers resources.
virtual void removeQuota(const std::string &role)=0
Informs the allocator to remove quota for the given role.
virtual void addResourceProvider(const SlaveID &slave, const Resources &total, const hashmap< FrameworkID, Resources > &used)=0
Add resources from a local resource provider to an agent.
constexpr const char * arg1
Definition: shell.hpp:45
Definition: option.hpp:28
T & get()&
Definition: try.hpp:73
Definition: check.hpp:33
virtual void activateSlave(const SlaveID &slaveId)=0
Activates an agent.
virtual void updateAllocation(const FrameworkID &frameworkId, const SlaveID &slaveId, const Resources &offeredResources, const std::vector< ResourceConversion > &conversions)=0
Updates allocation by applying offer operations.
TestAllocator()
Definition: allocator.hpp:227
virtual void initialize(const Duration &allocationInterval, const lambda::function< void(const FrameworkID &, const hashmap< std::string, hashmap< SlaveID, Resources >> &)> &offerCallback, const lambda::function< void(const FrameworkID &, const hashmap< SlaveID, UnavailableResources > &)> &inverseOfferCallback, const Option< std::set< std::string >> &fairnessExcludeResourceNames=None(), bool filterGpuResources=true, const Option< DomainInfo > &domain=None())=0
Initializes the allocator when the master starts up.
Definition: resources.hpp:79
virtual void reviveOffers(const FrameworkID &frameworkId, const std::set< std::string > &roles)=0
Revives offers to this framework for the specified roles.
mesos::allocator::Allocator * createAllocator()
Definition: allocator.hpp:212
virtual process::Future< hashmap< SlaveID, hashmap< FrameworkID, mesos::allocator::InverseOfferStatus > > > getInverseOfferStatuses()=0
Retrieves the status of all inverse offers maintained by the allocator.
Definition: duration.hpp:32
virtual void deactivateFramework(const FrameworkID &frameworkId)=0
Deactivates a framework in the Mesos cluster.
constexpr const char * arg0
Definition: shell.hpp:44
virtual void addFramework(const FrameworkID &frameworkId, const FrameworkInfo &frameworkInfo, const hashmap< SlaveID, Resources > &used, bool active, const std::set< std::string > &suppressedRoles)=0
Adds a framework to the Mesos cluster.
Definition: hashmap.hpp:38
virtual ~TestAllocator()
Definition: allocator.hpp:369
virtual void updateWeights(const std::vector< WeightInfo > &weightInfos)=0
Updates the weight associated with one or more roles.
#define CHECK_SOME(expression)
Definition: check.hpp:50
virtual void removeSlave(const SlaveID &slaveId)=0
Removes an agent from the Mesos cluster.
Definition: quota.hpp:25
virtual void activateFramework(const FrameworkID &frameworkId)=0
Activates a framework in the Mesos cluster.
Result< std::vector< Filter< Classifier > > > filters(const std::string &_link, const Handle &parent)
Definition: internal.hpp:776
virtual void updateFramework(const FrameworkID &frameworkId, const FrameworkInfo &frameworkInfo, const std::set< std::string > &suppressedRoles)=0
Updates capabilities of a framework in the Mesos cluster.
virtual void setQuota(const std::string &role, const Quota &quota)=0
Informs the allocator to set quota for the given role.
Definition: allocator.hpp:222
MOCK_METHOD4(updateSlave, void(const SlaveID &, const SlaveInfo &, const Option< Resources > &, const Option< std::vector< SlaveInfo::Capability >> &))
Definition: spec.hpp:30
virtual void removeFramework(const FrameworkID &frameworkId)=0
Removes a framework from the Mesos cluster.
MOCK_METHOD5(addFramework, void(const FrameworkID &, const FrameworkInfo &, const hashmap< SlaveID, Resources > &, bool active, const std::set< std::string > &))
virtual void updateUnavailability(const SlaveID &slaveId, const Option< Unavailability > &unavailability)=0
Updates unavailability for an agent.
virtual void addSlave(const SlaveID &slaveId, const SlaveInfo &slaveInfo, const std::vector< SlaveInfo::Capability > &capabilities, const Option< Unavailability > &unavailability, const Resources &total, const hashmap< FrameworkID, Resources > &used)=0
Adds or re-adds an agent to the Mesos cluster.
Basic model of an allocator: resources are allocated to a framework in the form of offers...
Definition: allocator.hpp:55
virtual void deactivateSlave(const SlaveID &slaveId)=0
Deactivates an agent.
Definition: attributes.hpp:24
MOCK_METHOD6(initialize, void(const Duration &, const lambda::function< void(const FrameworkID &, const hashmap< std::string, hashmap< SlaveID, Resources >> &)> &, const lambda::function< void(const FrameworkID &, const hashmap< SlaveID, UnavailableResources > &)> &, const Option< std::set< std::string >> &, bool, const Option< DomainInfo > &))
virtual void updateWhitelist(const Option< hashset< std::string >> &whitelist)=0
Updates the list of trusted agents.
process::Owned< mesos::allocator::Allocator > real
Definition: allocator.hpp:493
virtual void requestResources(const FrameworkID &frameworkId, const std::vector< Request > &requests)=0
Requests resources for a framework.
Try< Nothing > create(const std::string &hierarchy, const std::string &cgroup, bool recursive=false)
ACTION_P2(InvokeRecoverResourcesWithFilters, allocator, timeout)
Definition: allocator.hpp:172
virtual void recover(const int expectedAgentCount, const hashmap< std::string, Quota > &quotas)=0
Informs the allocator of the recovered state from the master.
ACTION_P(InvokeInitialize, allocator)
Definition: allocator.hpp:46
MOCK_METHOD2(recover, void(const int expectedAgentCount, const hashmap< std::string, Quota > &))
MOCK_METHOD3(updateFramework, void(const FrameworkID &, const FrameworkInfo &, const std::set< std::string > &))
virtual void suppressOffers(const FrameworkID &frameworkId, const std::set< std::string > &roles)=0
Suppresses offers.
MOCK_METHOD0(getInverseOfferStatuses, process::Future< hashmap< SlaveID, hashmap< FrameworkID, mesos::allocator::InverseOfferStatus >>>())
virtual process::Future< Nothing > updateAvailable(const SlaveID &slaveId, const std::vector< Offer::Operation > &operations)=0
Updates available resources on an agent based on a sequence of offer operations.
MOCK_METHOD1(removeFramework, void(const FrameworkID &))
virtual void updateInverseOffer(const SlaveID &slaveId, const FrameworkID &frameworkId, const Option< UnavailableResources > &unavailableResources, const Option< InverseOfferStatus > &status, const Option< Filters > &filters=None())=0
Updates inverse offer.
virtual void updateSlave(const SlaveID &slave, const SlaveInfo &slaveInfo, const Option< Resources > &total=None(), const Option< std::vector< SlaveInfo::Capability >> &capabilities=None())=0
Updates an agent.