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, arg5, arg6, arg7);
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 ACTION_P(InvokePause, allocator)
212 {
213  allocator->real->pause();
214 }
215 
216 
217 ACTION_P(InvokeResume, allocator)
218 {
219  allocator->real->resume();
220 }
221 
222 
223 template <typename T = master::allocator::HierarchicalDRFAllocator>
225 {
226  // T represents the allocator type. It can be a default built-in
227  // allocator, or one provided by an allocator module.
229  CHECK_SOME(instance);
230  return CHECK_NOTNULL(instance.get());
231 }
232 
233 template <typename T = master::allocator::HierarchicalDRFAllocator>
235 {
236 public:
237  // Actual allocation is done by an instance of real allocator,
238  // which is specified by the template parameter.
240  {
241  // We use 'ON_CALL' and 'WillByDefault' here to specify the
242  // default actions (call in to the real allocator). This allows
243  // the tests to leverage the 'DoDefault' action.
244  // However, 'ON_CALL' results in a "Uninteresting mock function
245  // call" warning unless each test puts expectations in place.
246  // As a result, we also use 'EXPECT_CALL' and 'WillRepeatedly'
247  // to get the best of both worlds: the ability to use 'DoDefault'
248  // and no warnings when expectations are not explicit.
249 
250  ON_CALL(*this, initialize(_, _, _, _, _, _, _, _))
251  .WillByDefault(InvokeInitialize(this));
252  EXPECT_CALL(*this, initialize(_, _, _, _, _, _, _, _))
253  .WillRepeatedly(DoDefault());
254 
255  ON_CALL(*this, recover(_, _))
256  .WillByDefault(InvokeRecover(this));
257  EXPECT_CALL(*this, recover(_, _))
258  .WillRepeatedly(DoDefault());
259 
260  ON_CALL(*this, addFramework(_, _, _, _, _))
261  .WillByDefault(InvokeAddFramework(this));
262  EXPECT_CALL(*this, addFramework(_, _, _, _, _))
263  .WillRepeatedly(DoDefault());
264 
265  ON_CALL(*this, removeFramework(_))
266  .WillByDefault(InvokeRemoveFramework(this));
267  EXPECT_CALL(*this, removeFramework(_))
268  .WillRepeatedly(DoDefault());
269 
270  ON_CALL(*this, activateFramework(_))
271  .WillByDefault(InvokeActivateFramework(this));
272  EXPECT_CALL(*this, activateFramework(_))
273  .WillRepeatedly(DoDefault());
274 
275  ON_CALL(*this, deactivateFramework(_))
276  .WillByDefault(InvokeDeactivateFramework(this));
277  EXPECT_CALL(*this, deactivateFramework(_))
278  .WillRepeatedly(DoDefault());
279 
280  ON_CALL(*this, updateFramework(_, _, _))
281  .WillByDefault(InvokeUpdateFramework(this));
282  EXPECT_CALL(*this, updateFramework(_, _, _))
283  .WillRepeatedly(DoDefault());
284 
285  ON_CALL(*this, addSlave(_, _, _, _, _, _))
286  .WillByDefault(InvokeAddSlave(this));
287  EXPECT_CALL(*this, addSlave(_, _, _, _, _, _))
288  .WillRepeatedly(DoDefault());
289 
290  ON_CALL(*this, removeSlave(_))
291  .WillByDefault(InvokeRemoveSlave(this));
292  EXPECT_CALL(*this, removeSlave(_))
293  .WillRepeatedly(DoDefault());
294 
295  ON_CALL(*this, updateSlave(_, _, _, _))
296  .WillByDefault(InvokeUpdateSlave(this));
297  EXPECT_CALL(*this, updateSlave(_, _, _, _))
298  .WillRepeatedly(DoDefault());
299 
300  ON_CALL(*this, addResourceProvider(_, _, _))
301  .WillByDefault(InvokeAddResourceProvider(this));
302  EXPECT_CALL(*this, addResourceProvider(_, _, _))
303  .WillRepeatedly(DoDefault());
304 
305  ON_CALL(*this, activateSlave(_))
306  .WillByDefault(InvokeActivateSlave(this));
307  EXPECT_CALL(*this, activateSlave(_))
308  .WillRepeatedly(DoDefault());
309 
310  ON_CALL(*this, deactivateSlave(_))
311  .WillByDefault(InvokeDeactivateSlave(this));
312  EXPECT_CALL(*this, deactivateSlave(_))
313  .WillRepeatedly(DoDefault());
314 
315  ON_CALL(*this, updateWhitelist(_))
316  .WillByDefault(InvokeUpdateWhitelist(this));
317  EXPECT_CALL(*this, updateWhitelist(_))
318  .WillRepeatedly(DoDefault());
319 
320  ON_CALL(*this, requestResources(_, _))
321  .WillByDefault(InvokeRequestResources(this));
322  EXPECT_CALL(*this, requestResources(_, _))
323  .WillRepeatedly(DoDefault());
324 
325  ON_CALL(*this, updateAllocation(_, _, _, _))
326  .WillByDefault(InvokeUpdateAllocation(this));
327  EXPECT_CALL(*this, updateAllocation(_, _, _, _))
328  .WillRepeatedly(DoDefault());
329 
330  ON_CALL(*this, updateAvailable(_, _))
331  .WillByDefault(InvokeUpdateAvailable(this));
332  EXPECT_CALL(*this, updateAvailable(_, _))
333  .WillRepeatedly(DoDefault());
334 
335  ON_CALL(*this, updateUnavailability(_, _))
336  .WillByDefault(InvokeUpdateUnavailability(this));
337  EXPECT_CALL(*this, updateUnavailability(_, _))
338  .WillRepeatedly(DoDefault());
339 
340  ON_CALL(*this, updateInverseOffer(_, _, _, _, _))
341  .WillByDefault(InvokeUpdateInverseOffer(this));
342  EXPECT_CALL(*this, updateInverseOffer(_, _, _, _, _))
343  .WillRepeatedly(DoDefault());
344 
345  ON_CALL(*this, getInverseOfferStatuses())
346  .WillByDefault(InvokeGetInverseOfferStatuses(this));
347  EXPECT_CALL(*this, getInverseOfferStatuses())
348  .WillRepeatedly(DoDefault());
349 
350  ON_CALL(*this, recoverResources(_, _, _, _))
351  .WillByDefault(InvokeRecoverResources(this));
352  EXPECT_CALL(*this, recoverResources(_, _, _, _))
353  .WillRepeatedly(DoDefault());
354 
355  ON_CALL(*this, suppressOffers(_, _))
356  .WillByDefault(InvokeSuppressOffers(this));
357  EXPECT_CALL(*this, suppressOffers(_, _))
358  .WillRepeatedly(DoDefault());
359 
360  ON_CALL(*this, reviveOffers(_, _))
361  .WillByDefault(InvokeReviveOffers(this));
362  EXPECT_CALL(*this, reviveOffers(_, _))
363  .WillRepeatedly(DoDefault());
364 
365  ON_CALL(*this, setQuota(_, _))
366  .WillByDefault(InvokeSetQuota(this));
367  EXPECT_CALL(*this, setQuota(_, _))
368  .WillRepeatedly(DoDefault());
369 
370  ON_CALL(*this, removeQuota(_))
371  .WillByDefault(InvokeRemoveQuota(this));
372  EXPECT_CALL(*this, removeQuota(_))
373  .WillRepeatedly(DoDefault());
374 
375  ON_CALL(*this, updateWeights(_))
376  .WillByDefault(InvokeUpdateWeights(this));
377  EXPECT_CALL(*this, updateWeights(_))
378  .WillRepeatedly(DoDefault());
379 
380  ON_CALL(*this, pause())
381  .WillByDefault(InvokePause(this));
382  EXPECT_CALL(*this, pause())
383  .WillRepeatedly(DoDefault());
384 
385  ON_CALL(*this, resume())
386  .WillByDefault(InvokeResume(this));
387  EXPECT_CALL(*this, resume())
388  .WillRepeatedly(DoDefault());
389  }
390 
391  ~TestAllocator() override {}
392 
393  MOCK_METHOD8(initialize, void(
394  const Duration&,
395  const lambda::function<
396  void(const FrameworkID&,
397  const hashmap<std::string, hashmap<SlaveID, Resources>>&)>&,
398  const lambda::function<
399  void(const FrameworkID&,
401  const Option<std::set<std::string>>&,
402  bool,
403  const Option<DomainInfo>&,
404  const Option<std::vector<Resources>>&,
405  const size_t maxCompletedFrameworks));
406 
407  MOCK_METHOD2(recover, void(
408  const int expectedAgentCount,
410 
412  const FrameworkID&,
413  const FrameworkInfo&,
415  bool active,
416  const std::set<std::string>&));
417 
419  const FrameworkID&));
420 
422  const FrameworkID&));
423 
425  const FrameworkID&));
426 
428  const FrameworkID&,
429  const FrameworkInfo&,
430  const std::set<std::string>&));
431 
432  MOCK_METHOD6(addSlave, void(
433  const SlaveID&,
434  const SlaveInfo&,
435  const std::vector<SlaveInfo::Capability>&,
436  const Option<Unavailability>&,
437  const Resources&,
439 
441  const SlaveID&));
442 
444  const SlaveID&,
445  const SlaveInfo&,
446  const Option<Resources>&,
447  const Option<std::vector<SlaveInfo::Capability>>&));
448 
450  const SlaveID&,
451  const Resources&,
453 
455  const SlaveID&));
456 
458  const SlaveID&));
459 
461  const Option<hashset<std::string>>&));
462 
464  const FrameworkID&,
465  const std::vector<Request>&));
466 
468  const FrameworkID&,
469  const SlaveID&,
470  const Resources&,
471  const std::vector<ResourceConversion>&));
472 
474  const SlaveID&,
475  const std::vector<Offer::Operation>&));
476 
478  const SlaveID&,
479  const Option<Unavailability>&));
480 
482  const SlaveID&,
483  const FrameworkID&,
486  const Option<Filters>&));
487 
489  hashmap<SlaveID, hashmap<
490  FrameworkID,
491  mesos::allocator::InverseOfferStatus>>>());
492 
494  const FrameworkID&,
495  const SlaveID&,
496  const Resources&,
497  const Option<Filters>& filters));
498 
500  const FrameworkID&,
501  const std::set<std::string>&));
502 
504  const FrameworkID&,
505  const std::set<std::string>&));
506 
507  MOCK_METHOD2(setQuota, void(
508  const std::string&,
509  const Quota&));
510 
512  const std::string&));
513 
515  const std::vector<WeightInfo>&));
516 
517  MOCK_METHOD0(pause, void());
518 
519  MOCK_METHOD0(resume, void());
520 
522 };
523 
524 } // namespace tests {
525 } // namespace internal {
526 } // namespace mesos {
527 
528 #endif // __TESTS_ALLOCATOR_HPP__
MOCK_METHOD6(addSlave, void(const SlaveID &, const SlaveInfo &, const std::vector< SlaveInfo::Capability > &, const Option< Unavailability > &, const Resources &, const hashmap< FrameworkID, Resources > &))
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 pause()=0
Idempotent helper to pause allocations.
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:239
Definition: resources.hpp:81
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:224
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 void updateWeights(const std::vector< WeightInfo > &weightInfos)=0
Updates the weight associated with one or more roles.
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(), const Option< std::vector< Resources >> &minAllocatableResources=None(), const size_t maxCompletedFrameworks=0)=0
Initializes the allocator when the master starts up.
#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:769
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.
MOCK_METHOD8(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 > &, const Option< std::vector< Resources >> &, const size_t maxCompletedFrameworks))
Definition: allocator.hpp:234
virtual void resume()=0
Idempotent helper to resume allocations.
MOCK_METHOD4(updateSlave, void(const SlaveID &, const SlaveInfo &, const Option< Resources > &, const Option< std::vector< SlaveInfo::Capability >> &))
Definition: spec.hpp:26
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
~TestAllocator() override
Definition: allocator.hpp:391
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:521
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.