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 
37 
38 namespace mesos {
39 namespace internal {
40 namespace tests {
41 
42 // Allocator test helpers.
43 
44 Quota createQuota(const std::string& role, const std::string& resources);
45 
46 
47 WeightInfo createWeightInfo(const std::string& role, double weight);
48 
49 
50 // The following actions make up for the fact that DoDefault
51 // cannot be used inside a DoAll, for example:
52 // EXPECT_CALL(allocator, addFramework(_, _, _, _, _))
53 // .WillOnce(DoAll(InvokeAddFramework(&allocator),
54 // FutureSatisfy(&addFramework)));
55 
56 ACTION_P(InvokeInitialize, allocator)
57 {
58  allocator->real->initialize(arg0, arg1, arg2);
59 }
60 
61 
62 ACTION_P(InvokeRecover, allocator)
63 {
64  allocator->real->recover(arg0, arg1);
65 }
66 
67 
68 ACTION_P(InvokeAddFramework, allocator)
69 {
70  allocator->real->addFramework(arg0, arg1, arg2, arg3, arg4);
71 }
72 
73 
74 ACTION_P(InvokeRemoveFramework, allocator)
75 {
76  allocator->real->removeFramework(arg0);
77 }
78 
79 
80 ACTION_P(InvokeActivateFramework, allocator)
81 {
82  allocator->real->activateFramework(arg0);
83 }
84 
85 
86 ACTION_P(InvokeDeactivateFramework, allocator)
87 {
88  allocator->real->deactivateFramework(arg0);
89 }
90 
91 
92 ACTION_P(InvokeUpdateFramework, allocator)
93 {
94  allocator->real->updateFramework(arg0, arg1, arg2);
95 }
96 
97 
98 ACTION_P(InvokeAddSlave, allocator)
99 {
100  allocator->real->addSlave(arg0, arg1, arg2, arg3, arg4, arg5);
101 }
102 
103 
104 ACTION_P(InvokeRemoveSlave, allocator)
105 {
106  allocator->real->removeSlave(arg0);
107 }
108 
109 
110 ACTION_P(InvokeUpdateSlave, allocator)
111 {
112  allocator->real->updateSlave(arg0, arg1, arg2, arg3);
113 }
114 
115 
116 ACTION_P(InvokeAddResourceProvider, allocator)
117 {
118  allocator->real->addResourceProvider(arg0, arg1, arg2);
119 }
120 
121 
122 ACTION_P(InvokeActivateSlave, allocator)
123 {
124  allocator->real->activateSlave(arg0);
125 }
126 
127 
128 ACTION_P(InvokeDeactivateSlave, allocator)
129 {
130  allocator->real->deactivateSlave(arg0);
131 }
132 
133 
134 ACTION_P(InvokeUpdateWhitelist, allocator)
135 {
136  allocator->real->updateWhitelist(arg0);
137 }
138 
139 
140 ACTION_P(InvokeRequestResources, allocator)
141 {
142  allocator->real->requestResources(arg0, arg1);
143 }
144 
145 
146 ACTION_P(InvokeUpdateAllocation, allocator)
147 {
148  allocator->real->updateAllocation(arg0, arg1, arg2, arg3);
149 }
150 
151 
152 ACTION_P(InvokeUpdateAvailable, allocator)
153 {
154  return allocator->real->updateAvailable(arg0, arg1);
155 }
156 
157 
158 ACTION_P(InvokeUpdateUnavailability, allocator)
159 {
160  return allocator->real->updateUnavailability(arg0, arg1);
161 }
162 
163 
164 ACTION_P(InvokeUpdateInverseOffer, allocator)
165 {
166  return allocator->real->updateInverseOffer(arg0, arg1, arg2, arg3, arg4);
167 }
168 
169 
170 ACTION_P(InvokeGetInverseOfferStatuses, allocator)
171 {
172  return allocator->real->getInverseOfferStatuses();
173 }
174 
175 
176 ACTION_P(InvokeRecoverResources, allocator)
177 {
178  allocator->real->recoverResources(arg0, arg1, arg2, arg3);
179 }
180 
181 
182 ACTION_P2(InvokeRecoverResourcesWithFilters, allocator, timeout)
183 {
184  Filters filters;
185  filters.set_refuse_seconds(timeout);
186 
187  allocator->real->recoverResources(arg0, arg1, arg2, filters);
188 }
189 
190 
191 ACTION_P(InvokeSuppressOffers, allocator)
192 {
193  allocator->real->suppressOffers(arg0, arg1);
194 }
195 
196 
197 ACTION_P(InvokeReviveOffers, allocator)
198 {
199  allocator->real->reviveOffers(arg0, arg1);
200 }
201 
202 
203 ACTION_P(InvokeSetQuota, allocator)
204 {
205  allocator->real->setQuota(arg0, arg1);
206 }
207 
208 
209 ACTION_P(InvokeRemoveQuota, allocator)
210 {
211  allocator->real->removeQuota(arg0);
212 }
213 
214 
215 ACTION_P(InvokeUpdateWeights, allocator)
216 {
217  allocator->real->updateWeights(arg0);
218 }
219 
220 
221 ACTION_P(InvokePause, allocator)
222 {
223  allocator->real->pause();
224 }
225 
226 
227 ACTION_P(InvokeResume, allocator)
228 {
229  allocator->real->resume();
230 }
231 
232 
233 template <typename T = master::allocator::HierarchicalDRFAllocator>
235 {
236  // T represents the allocator type. It can be a default built-in
237  // allocator, or one provided by an allocator module.
239  CHECK_SOME(instance);
240  return CHECK_NOTNULL(instance.get());
241 }
242 
243 template <typename T = master::allocator::HierarchicalDRFAllocator>
245 {
246 public:
247  // Actual allocation is done by an instance of real allocator,
248  // which is specified by the template parameter.
250  {
251  // We use 'ON_CALL' and 'WillByDefault' here to specify the
252  // default actions (call in to the real allocator). This allows
253  // the tests to leverage the 'DoDefault' action.
254  // However, 'ON_CALL' results in a "Uninteresting mock function
255  // call" warning unless each test puts expectations in place.
256  // As a result, we also use 'EXPECT_CALL' and 'WillRepeatedly'
257  // to get the best of both worlds: the ability to use 'DoDefault'
258  // and no warnings when expectations are not explicit.
259 
260  ON_CALL(*this, initialize(_, _, _))
261  .WillByDefault(InvokeInitialize(this));
262  EXPECT_CALL(*this, initialize(_, _, _))
263  .WillRepeatedly(DoDefault());
264 
265  ON_CALL(*this, recover(_, _))
266  .WillByDefault(InvokeRecover(this));
267  EXPECT_CALL(*this, recover(_, _))
268  .WillRepeatedly(DoDefault());
269 
270  ON_CALL(*this, addFramework(_, _, _, _, _))
271  .WillByDefault(InvokeAddFramework(this));
272  EXPECT_CALL(*this, addFramework(_, _, _, _, _))
273  .WillRepeatedly(DoDefault());
274 
275  ON_CALL(*this, removeFramework(_))
276  .WillByDefault(InvokeRemoveFramework(this));
277  EXPECT_CALL(*this, removeFramework(_))
278  .WillRepeatedly(DoDefault());
279 
280  ON_CALL(*this, activateFramework(_))
281  .WillByDefault(InvokeActivateFramework(this));
282  EXPECT_CALL(*this, activateFramework(_))
283  .WillRepeatedly(DoDefault());
284 
285  ON_CALL(*this, deactivateFramework(_))
286  .WillByDefault(InvokeDeactivateFramework(this));
287  EXPECT_CALL(*this, deactivateFramework(_))
288  .WillRepeatedly(DoDefault());
289 
290  ON_CALL(*this, updateFramework(_, _, _))
291  .WillByDefault(InvokeUpdateFramework(this));
292  EXPECT_CALL(*this, updateFramework(_, _, _))
293  .WillRepeatedly(DoDefault());
294 
295  ON_CALL(*this, addSlave(_, _, _, _, _, _))
296  .WillByDefault(InvokeAddSlave(this));
297  EXPECT_CALL(*this, addSlave(_, _, _, _, _, _))
298  .WillRepeatedly(DoDefault());
299 
300  ON_CALL(*this, removeSlave(_))
301  .WillByDefault(InvokeRemoveSlave(this));
302  EXPECT_CALL(*this, removeSlave(_))
303  .WillRepeatedly(DoDefault());
304 
305  ON_CALL(*this, updateSlave(_, _, _, _))
306  .WillByDefault(InvokeUpdateSlave(this));
307  EXPECT_CALL(*this, updateSlave(_, _, _, _))
308  .WillRepeatedly(DoDefault());
309 
310  ON_CALL(*this, addResourceProvider(_, _, _))
311  .WillByDefault(InvokeAddResourceProvider(this));
312  EXPECT_CALL(*this, addResourceProvider(_, _, _))
313  .WillRepeatedly(DoDefault());
314 
315  ON_CALL(*this, activateSlave(_))
316  .WillByDefault(InvokeActivateSlave(this));
317  EXPECT_CALL(*this, activateSlave(_))
318  .WillRepeatedly(DoDefault());
319 
320  ON_CALL(*this, deactivateSlave(_))
321  .WillByDefault(InvokeDeactivateSlave(this));
322  EXPECT_CALL(*this, deactivateSlave(_))
323  .WillRepeatedly(DoDefault());
324 
325  ON_CALL(*this, updateWhitelist(_))
326  .WillByDefault(InvokeUpdateWhitelist(this));
327  EXPECT_CALL(*this, updateWhitelist(_))
328  .WillRepeatedly(DoDefault());
329 
330  ON_CALL(*this, requestResources(_, _))
331  .WillByDefault(InvokeRequestResources(this));
332  EXPECT_CALL(*this, requestResources(_, _))
333  .WillRepeatedly(DoDefault());
334 
335  ON_CALL(*this, updateAllocation(_, _, _, _))
336  .WillByDefault(InvokeUpdateAllocation(this));
337  EXPECT_CALL(*this, updateAllocation(_, _, _, _))
338  .WillRepeatedly(DoDefault());
339 
340  ON_CALL(*this, updateAvailable(_, _))
341  .WillByDefault(InvokeUpdateAvailable(this));
342  EXPECT_CALL(*this, updateAvailable(_, _))
343  .WillRepeatedly(DoDefault());
344 
345  ON_CALL(*this, updateUnavailability(_, _))
346  .WillByDefault(InvokeUpdateUnavailability(this));
347  EXPECT_CALL(*this, updateUnavailability(_, _))
348  .WillRepeatedly(DoDefault());
349 
350  ON_CALL(*this, updateInverseOffer(_, _, _, _, _))
351  .WillByDefault(InvokeUpdateInverseOffer(this));
352  EXPECT_CALL(*this, updateInverseOffer(_, _, _, _, _))
353  .WillRepeatedly(DoDefault());
354 
355  ON_CALL(*this, getInverseOfferStatuses())
356  .WillByDefault(InvokeGetInverseOfferStatuses(this));
357  EXPECT_CALL(*this, getInverseOfferStatuses())
358  .WillRepeatedly(DoDefault());
359 
360  ON_CALL(*this, recoverResources(_, _, _, _))
361  .WillByDefault(InvokeRecoverResources(this));
362  EXPECT_CALL(*this, recoverResources(_, _, _, _))
363  .WillRepeatedly(DoDefault());
364 
365  ON_CALL(*this, suppressOffers(_, _))
366  .WillByDefault(InvokeSuppressOffers(this));
367  EXPECT_CALL(*this, suppressOffers(_, _))
368  .WillRepeatedly(DoDefault());
369 
370  ON_CALL(*this, reviveOffers(_, _))
371  .WillByDefault(InvokeReviveOffers(this));
372  EXPECT_CALL(*this, reviveOffers(_, _))
373  .WillRepeatedly(DoDefault());
374 
375  ON_CALL(*this, setQuota(_, _))
376  .WillByDefault(InvokeSetQuota(this));
377  EXPECT_CALL(*this, setQuota(_, _))
378  .WillRepeatedly(DoDefault());
379 
380  ON_CALL(*this, removeQuota(_))
381  .WillByDefault(InvokeRemoveQuota(this));
382  EXPECT_CALL(*this, removeQuota(_))
383  .WillRepeatedly(DoDefault());
384 
385  ON_CALL(*this, updateWeights(_))
386  .WillByDefault(InvokeUpdateWeights(this));
387  EXPECT_CALL(*this, updateWeights(_))
388  .WillRepeatedly(DoDefault());
389 
390  ON_CALL(*this, pause())
391  .WillByDefault(InvokePause(this));
392  EXPECT_CALL(*this, pause())
393  .WillRepeatedly(DoDefault());
394 
395  ON_CALL(*this, resume())
396  .WillByDefault(InvokeResume(this));
397  EXPECT_CALL(*this, resume())
398  .WillRepeatedly(DoDefault());
399  }
400 
401  ~TestAllocator() override {}
402 
403  MOCK_METHOD3(initialize, void(
404  const Options& options,
405  const lambda::function<
406  void(const FrameworkID&,
407  const hashmap<std::string, hashmap<SlaveID, Resources>>&)>&,
408  const lambda::function<
409  void(const FrameworkID&,
411 
412  MOCK_METHOD2(recover, void(
413  const int expectedAgentCount,
415 
417  const FrameworkID&,
418  const FrameworkInfo&,
420  bool active,
421  const std::set<std::string>&));
422 
424  const FrameworkID&));
425 
427  const FrameworkID&));
428 
430  const FrameworkID&));
431 
433  const FrameworkID&,
434  const FrameworkInfo&,
435  const std::set<std::string>&));
436 
437  MOCK_METHOD6(addSlave, void(
438  const SlaveID&,
439  const SlaveInfo&,
440  const std::vector<SlaveInfo::Capability>&,
441  const Option<Unavailability>&,
442  const Resources&,
444 
446  const SlaveID&));
447 
449  const SlaveID&,
450  const SlaveInfo&,
451  const Option<Resources>&,
452  const Option<std::vector<SlaveInfo::Capability>>&));
453 
455  const SlaveID&,
456  const Resources&,
458 
460  const SlaveID&));
461 
463  const SlaveID&));
464 
466  const Option<hashset<std::string>>&));
467 
469  const FrameworkID&,
470  const std::vector<Request>&));
471 
473  const FrameworkID&,
474  const SlaveID&,
475  const Resources&,
476  const std::vector<ResourceConversion>&));
477 
479  const SlaveID&,
480  const std::vector<Offer::Operation>&));
481 
483  const SlaveID&,
484  const Option<Unavailability>&));
485 
487  const SlaveID&,
488  const FrameworkID&,
491  const Option<Filters>&));
492 
494  hashmap<SlaveID, hashmap<
495  FrameworkID,
496  mesos::allocator::InverseOfferStatus>>>());
497 
499  const FrameworkID&,
500  const SlaveID&,
501  const Resources&,
502  const Option<Filters>& filters));
503 
505  const FrameworkID&,
506  const std::set<std::string>&));
507 
509  const FrameworkID&,
510  const std::set<std::string>&));
511 
512  MOCK_METHOD2(setQuota, void(
513  const std::string&,
514  const Quota&));
515 
517  const std::string&));
518 
520  const std::vector<WeightInfo>&));
521 
522  MOCK_METHOD0(pause, void());
523 
524  MOCK_METHOD0(resume, void());
525 
527 };
528 
529 } // namespace tests {
530 } // namespace internal {
531 } // namespace mesos {
532 
533 #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:249
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.
Pass in configuration to the allocator.
Definition: allocator.hpp:47
mesos::allocator::Allocator * createAllocator()
Definition: allocator.hpp:234
virtual process::Future< hashmap< SlaveID, hashmap< FrameworkID, mesos::allocator::InverseOfferStatus > > > getInverseOfferStatuses()=0
Retrieves the status of all inverse offers maintained by the allocator.
virtual void deactivateFramework(const FrameworkID &frameworkId)=0
Deactivates a framework in the Mesos cluster.
WeightInfo createWeightInfo(const std::string &role, double weight)
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 initialize(const Options &options, 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)=0
Initializes the allocator when the master starts up.
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: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.
Definition: allocator.hpp:244
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:70
Quota createQuota(const std::string &role, const std::string &resources)
virtual void deactivateSlave(const SlaveID &slaveId)=0
Deactivates an agent.
Definition: attributes.hpp:24
~TestAllocator() override
Definition: allocator.hpp:401
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:526
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:182
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:56
MOCK_METHOD2(recover, void(const int expectedAgentCount, const hashmap< std::string, Quota > &))
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.
MOCK_METHOD3(initialize, void(const Options &options, const lambda::function< void(const FrameworkID &, const hashmap< std::string, hashmap< SlaveID, Resources >> &)> &, const lambda::function< void(const FrameworkID &, const hashmap< SlaveID, UnavailableResources > &)> &))