Apache Mesos
resources.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 __MESOS_V1_RESOURCES_HPP__
18 #define __MESOS_V1_RESOURCES_HPP__
19 
20 #include <map>
21 #include <iosfwd>
22 #include <set>
23 #include <string>
24 #include <vector>
25 
26 #include <boost/container/small_vector.hpp>
27 #include <boost/iterator/indirect_iterator.hpp>
28 
29 #include <google/protobuf/repeated_field.h>
30 
32 
33 #include <mesos/v1/mesos.hpp>
34 #include <mesos/v1/values.hpp>
35 
36 #include <stout/bytes.hpp>
37 #include <stout/check.hpp>
38 #include <stout/error.hpp>
39 #include <stout/foreach.hpp>
40 #include <stout/hashmap.hpp>
41 #include <stout/json.hpp>
42 #include <stout/lambda.hpp>
43 #include <stout/nothing.hpp>
44 #include <stout/option.hpp>
45 #include <stout/try.hpp>
46 
47 
48 // Resources come in three types: scalar, ranges, and sets. These are
49 // represented using protocol buffers. To make manipulation of
50 // resources easier within the Mesos core and for scheduler writers,
51 // we provide generic overloaded operators (see below) as well as a
52 // general Resources class that encapsulates a collection of protocol
53 // buffer Resource objects. The Resources class also provides a few
54 // static routines to allow parsing resources (e.g., from the command
55 // line), as well as determining whether or not a Resource object is
56 // valid. Note that many of these operations have not been optimized
57 // but instead just written for correct semantics.
58 
59 namespace mesos {
60 namespace v1 {
61 
62 // Forward declaration.
63 class ResourceConversion;
64 
65 
66 // Helper functions.
67 bool operator==(
68  const Resource::ReservationInfo& left,
69  const Resource::ReservationInfo& right);
70 
71 
72 bool operator!=(
73  const Resource::ReservationInfo& left,
74  const Resource::ReservationInfo& right);
75 
76 
77 // NOTE: Resource objects stored in the class are always valid, are in
78 // the "post-reservation-refinement" format, and kept combined if possible.
79 // It is the caller's responsibility to validate any Resource object or
80 // repeated Resource protobufs before constructing a Resources object.
81 // Otherwise, invalid Resource objects will be silently stripped.
82 // Invalid Resource objects will also be silently ignored when used in
83 // arithmetic operations (e.g., +=, -=, etc.).
84 class Resources
85 {
86 private:
87  // An internal abstraction to facilitate managing shared resources.
88  // It allows 'Resources' to group identical shared resource objects
89  // together into a single 'Resource_' object and tracked by its internal
90  // counter. Non-shared resource objects are not grouped.
91  //
92  // The rest of the private section is below the public section. We
93  // need to define Resource_ first because the public typedefs below
94  // depend on it.
95  class Resource_
96  {
97  public:
98  /*implicit*/ Resource_(const Resource& _resource)
99  : resource(_resource),
100  sharedCount(None())
101  {
102  // Setting the counter to 1 to denote "one copy" of the shared resource.
103  if (resource.has_shared()) {
104  sharedCount = 1;
105  }
106  }
107 
108  /*implicit*/ Resource_(Resource&& _resource)
109  : resource(std::move(_resource)), sharedCount(None())
110  {
111  // Setting the counter to 1 to denote "one copy" of the shared resource.
112  if (resource.has_shared()) {
113  sharedCount = 1;
114  }
115  }
116 
117  Resource_(const Resource_& resource_) = default;
118  Resource_(Resource_&& resource_) = default;
119 
120  Resource_& operator=(const Resource_&) = default;
121  Resource_& operator=(Resource_&&) = default;
122 
123  // By implicitly converting to Resource we are able to keep Resource_
124  // logic internal and expose only the protobuf object.
125  operator const Resource&() const { return resource; }
126 
127  // Check whether this Resource_ object corresponds to a shared resource.
128  bool isShared() const { return sharedCount.isSome(); }
129 
130  // Validates this Resource_ object.
131  Option<Error> validate() const;
132 
133  // Check whether this Resource_ object is empty.
134  bool isEmpty() const;
135 
136  // The `Resource_` arithmetic, comparison operators and `contains()`
137  // method require the wrapped `resource` protobuf to have the same
138  // sharedness.
139  //
140  // For shared resources, the `resource` protobuf needs to be equal,
141  // and only the shared counters are adjusted or compared.
142  // For non-shared resources, the shared counters are none and the
143  // semantics of the Resource_ object's operators/contains() method
144  // are the same as those of the Resource objects.
145 
146  // Checks if this Resource_ is a superset of the given Resource_.
147  bool contains(const Resource_& that) const;
148 
149  // The arithmetic operators, viz. += and -= assume that the corresponding
150  // Resource objects are addable or subtractable already.
151  Resource_& operator+=(const Resource_& that);
152  Resource_& operator-=(const Resource_& that);
153 
154  bool operator==(const Resource_& that) const;
155  bool operator!=(const Resource_& that) const;
156 
157  // Friend classes and functions for access to private members.
158  friend class Resources;
159  friend std::ostream& operator<<(
160  std::ostream& stream, const Resource_& resource_);
161 
162  private:
163  // The protobuf Resource that is being managed.
164  Resource resource;
165 
166  // The counter for grouping shared 'resource' objects, None if the
167  // 'resource' is non-shared. This is an int so as to support arithmetic
168  // operations involving subtraction.
169  Option<int> sharedCount;
170  };
171 
172 public:
173  // We rename the type here to alert people about the fact that with
174  // `shared_ptr`, no mutation should be made without obtaining exclusive
175  // ownership. See `resourcesNoMutationWithoutExclusiveOwnership`.
176  using Resource_Unsafe = std::shared_ptr<Resource_>;
177 
190  static Try<Resource> parse(
191  const std::string& name,
192  const std::string& value,
193  const std::string& role);
194 
209  static Try<Resources> parse(
210  const std::string& text,
211  const std::string& defaultRole = "*");
212 
233  const JSON::Array& resourcesJSON,
234  const std::string& defaultRole = "*");
235 
254  const std::string& text,
255  const std::string& defaultRole = "*");
256 
270  const std::string& text,
271  const std::string& defaultRole = "*");
272 
285  static Option<Error> validate(const Resource& resource);
286 
304  static Option<Error> validate(
305  const google::protobuf::RepeatedPtrField<Resource>& resources);
306 
307  // NOTE: The following predicate functions assume that the given resource is
308  // validated, and is in the "post-reservation-refinement" format. That is,
309  // the reservation state is represented by `Resource.reservations` field,
310  // and `Resource.role` and `Resource.reservation` fields are not set.
311  //
312  // See 'Resource Format' section in `mesos.proto` for more details.
313 
314  // Tests if the given Resource object is empty.
315  static bool isEmpty(const Resource& resource);
316 
317  // Tests if the given Resource object is a persistent volume.
318  static bool isPersistentVolume(const Resource& resource);
319 
320  // Tests if the given Resource object is a disk of the specified type.
321  static bool isDisk(
322  const Resource& resource,
324 
325  // Tests if the given Resource object is reserved. If the role is
326  // specified, tests that it's reserved for the given role.
327  static bool isReserved(
328  const Resource& resource,
329  const Option<std::string>& role = None());
330 
331  // Tests if the given Resource object is allocatable to the given role.
332  // A resource object is allocatable to 'role' if:
333  // * it is reserved to an ancestor of that role in the hierarchy, OR
334  // * it is reserved to 'role' itself, OR
335  // * it is unreserved.
336  static bool isAllocatableTo(
337  const Resource& resource,
338  const std::string& role);
339 
340  // Tests if the given Resource object is unreserved.
341  static bool isUnreserved(const Resource& resource);
342 
343  // Tests if the given Resource object is dynamically reserved.
344  static bool isDynamicallyReserved(const Resource& resource);
345 
346  // Tests if the given Resource object is revocable.
347  static bool isRevocable(const Resource& resource);
348 
349  // Tests if the given Resource object is shared.
350  static bool isShared(const Resource& resource);
351 
352  // Returns true if the resource is allocated to the role subtree
353  // (i.e. either to the role itself or to its decedents).
354  static bool isAllocatedToRoleSubtree(
355  const Resource& resource, const std::string& role);
356 
357  // Returns true if the resource is reserved to the role subtree
358  // (i.e. either to the role itself or to its decedents).
359  static bool isReservedToRoleSubtree(
360  const Resource& resource, const std::string& role);
361 
362  // Tests if the given Resource object has refined reservations.
363  static bool hasRefinedReservations(const Resource& resource);
364 
365  // Tests if the given Resource object is provided by a resource provider.
366  static bool hasResourceProvider(const Resource& resource);
367 
368  // Returns the role to which the given Resource object is reserved for.
369  // This must be called only when the resource is reserved!
370  static const std::string& reservationRole(const Resource& resource);
371 
372  // Shrinks a scalar type `resource` to the target size.
373  // Returns true if the resource was shrunk to the target size,
374  // or the resource is already within the target size.
375  // Returns false otherwise (i.e. the resource is indivisible.
376  // E.g. MOUNT volume).
377  static bool shrink(Resource* resource, const Value::Scalar& target);
378 
379  // Returns the summed up Resources given a hashmap<Key, Resources>.
380  //
381  // NOTE: While scalar resources such as "cpus" sum correctly,
382  // non-scalar resources such as "ports" do not.
383  // e.g. "cpus:2" + "cpus:1" = "cpus:3"
384  // "ports:[0-100]" + "ports:[0-100]" = "ports:[0-100]"
385  //
386  // TODO(mpark): Deprecate this function once we introduce the
387  // concept of "cluster-wide" resources which provides correct
388  // semantics for summation over all types of resources. (e.g.
389  // non-scalar)
390  template <typename Key>
391  static Resources sum(const hashmap<Key, Resources>& _resources)
392  {
393  Resources result;
394 
395  foreachvalue (const Resources& resources, _resources) {
396  result += resources;
397  }
398 
399  return result;
400  }
401 
403 
404  // TODO(jieyu): Consider using C++11 initializer list.
405  /*implicit*/ Resources(const Resource& resource);
406  /*implicit*/ Resources(Resource&& resource);
407 
408  /*implicit*/
409  Resources(const std::vector<Resource>& _resources);
410  Resources(std::vector<Resource>&& _resources);
411 
412  /*implicit*/
413  Resources(const google::protobuf::RepeatedPtrField<Resource>& _resources);
414  Resources(google::protobuf::RepeatedPtrField<Resource>&& _resources);
415 
416  Resources(const Resources& that) = default;
417  Resources(Resources&& that) = default;
418 
420  {
421  if (this != &that) {
422  resourcesNoMutationWithoutExclusiveOwnership =
423  that.resourcesNoMutationWithoutExclusiveOwnership;
424  }
425  return *this;
426  }
427 
429  {
430  if (this != &that) {
431  resourcesNoMutationWithoutExclusiveOwnership =
432  std::move(that.resourcesNoMutationWithoutExclusiveOwnership);
433  }
434  return *this;
435  }
436 
437  bool empty() const
438  {
439  return resourcesNoMutationWithoutExclusiveOwnership.size() == 0;
440  }
441 
442  size_t size() const
443  {
444  return resourcesNoMutationWithoutExclusiveOwnership.size();
445  }
446 
447  // Checks if this Resources is a superset of the given Resources.
448  bool contains(const Resources& that) const;
449 
450  // Checks if this Resources contains the given Resource.
451  bool contains(const Resource& that) const;
452 
453  // Checks if the quantities of this `Resources` is a superset of the
454  // given `ResourceQuantities`. If a `Resource` object is `SCALAR` type,
455  // its quantity is its scalar value. For `RANGES` and `SET` type, their
456  // quantities are the number of different instances in the range or set.
457  // For example, "range:[1-5]" has a quantity of 5 and "set:{a,b}" has a
458  // quantity of 2.
459  bool contains(const ResourceQuantities& quantities) const;
460 
461  // Count the Resource objects that match the specified value.
462  //
463  // NOTE:
464  // - For a non-shared resource the count can be at most 1 because all
465  // non-shared Resource objects in Resources are unique.
466  // - For a shared resource the count can be greater than 1.
467  // - If the resource is not in the Resources object, the count is 0.
468  size_t count(const Resource& that) const;
469 
470  // Allocates the resources to the given role (by setting the
471  // `AllocationInfo.role`). Any existing allocation will be
472  // over-written.
473  void allocate(const std::string& role);
474 
475  // Unallocates the resources.
476  void unallocate();
477 
478  // Filter resources based on the given predicate.
480  const lambda::function<bool(const Resource&)>& predicate) const;
481 
482  // Returns the reserved resources, by role.
484 
485  // Returns the reserved resources for the role, if specified.
486  // Note that the "*" role represents unreserved resources,
487  // and will be ignored.
488  Resources reserved(const Option<std::string>& role = None()) const;
489 
490  // Returns resources allocatable to role. See `isAllocatableTo` for the
491  // definition of 'allocatableTo'.
492  Resources allocatableTo(const std::string& role) const;
493 
494  // Returns resources that are allocated to the role subtree
495  // (i.e. either to the role itself or to its decedents).
496  Resources allocatedToRoleSubtree(const std::string& role) const;
497 
498  // Returns resources that are reserved to the role subtree
499  // (i.e. either to the role itself or to its decedents).
500  Resources reservedToRoleSubtree(const std::string& role) const;
501 
502  // Returns the unreserved resources.
503  Resources unreserved() const;
504 
505  // Returns the persistent volumes.
507 
508  // Returns the revocable resources.
509  Resources revocable() const;
510 
511  // Returns the non-revocable resources, effectively !revocable().
512  Resources nonRevocable() const;
513 
514  // Returns the shared resources.
515  Resources shared() const;
516 
517  // Returns the non-shared resources.
518  Resources nonShared() const;
519 
520  // Returns the per-role allocations within these resource objects.
521  // This must be called only when the resources are allocated!
523 
524  // Returns a `Resources` object with the new reservation added to the back.
525  // The new reservation must be a valid refinement of the current reservation.
526  Resources pushReservation(const Resource::ReservationInfo& reservation) const;
527 
528  // Returns a `Resources` object with the last reservation removed.
529  // Every resource in `Resources` must have `resource.reservations_size() > 0`.
530  Resources popReservation() const;
531 
532  // Returns a `Resources` object with all of the reservations removed.
533  Resources toUnreserved() const;
534 
535  // Returns a Resources object that contains all the scalar resources
536  // but with all the meta-data fields, such as AllocationInfo,
537  // ReservationInfo and etc. cleared. Only scalar resources' name,
538  // type (SCALAR) and value are preserved.
539  //
540  // This is intended for code that would like to aggregate together
541  // Resource values without regard for metadata like whether the
542  // resource is reserved or the particular volume ID in use. For
543  // example, when calculating the total resources in a cluster,
544  // preserving such information has a major performance cost.
546 
547  // Finds a Resources object with the same amount of each resource
548  // type as "targets" from these Resources. The roles specified in
549  // "targets" set the preference order. For each resource type,
550  // resources are first taken from the specified role, then from '*',
551  // then from any other role.
552  // TODO(jieyu): 'find' contains some allocation logic for scalars and
553  // fixed set / range elements. However, this is not sufficient for
554  // schedulers that want, say, any N available ports. We should
555  // consider moving this to an internal "allocation" library for our
556  // example frameworks to leverage.
557  Option<Resources> find(const Resources& targets) const;
558 
559  // Applies a resource conversion by taking out the `consumed`
560  // resources and adding back the `converted` resources. Returns an
561  // Error if the conversion cannot be applied.
562  Try<Resources> apply(const ResourceConversion& conversion) const;
563 
564  // Obtains the conversion from the given operation and applies the
565  // conversion. This method serves a syntax sugar for applying a
566  // resource conversion.
567  // TODO(jieyu): Consider remove this method once we updated all the
568  // call sites.
569  Try<Resources> apply(const Offer::Operation& operation) const;
570 
571  template <typename Iterable>
572  Try<Resources> apply(const Iterable& iterable) const
573  {
574  Resources result = *this;
575 
576  foreach (const auto& t, iterable) {
577  Try<Resources> converted = result.apply(t);
578  if (converted.isError()) {
579  return Error(converted.error());
580  }
581 
582  result = converted.get();
583  }
584 
585  return result;
586  }
587 
588  // Helpers to get resource values. We consider all roles here.
589  template <typename T>
590  Option<T> get(const std::string& name) const;
591 
592  // Get resources of the given name.
593  Resources get(const std::string& name) const;
594 
595  // Get all the resources that are scalars.
596  Resources scalars() const;
597 
598  // Get the set of unique resource names.
599  std::set<std::string> names() const;
600 
601  // Get the types of resources associated with each resource name.
602  // NOTE: Resources of the same name must have the same type, as
603  // enforced by Resources::parse().
604  std::map<std::string, Value_Type> types() const;
605 
606  // Helpers to get known resource types.
607  // TODO(vinod): Fix this when we make these types as first class
608  // protobufs.
609  Option<double> cpus() const;
610  Option<double> gpus() const;
611  Option<Bytes> mem() const;
612  Option<Bytes> disk() const;
613 
614  // TODO(vinod): Provide a Ranges abstraction.
616 
617  // TODO(jieyu): Consider returning an EphemeralPorts abstraction
618  // which holds the ephemeral ports allocation logic.
620 
621  // We use `boost::indirect_iterator` to expose `Resource` (implicitly
622  // converted from `Resource_`) iteration, while actually storing
623  // `Resource_Unsafe`.
624  //
625  // NOTE: Non-const `begin()` and `end()` intentionally return const
626  // iterators to prevent mutable access to the `Resource` objects.
627 
628  typedef boost::indirect_iterator<
629  boost::container::small_vector_base<Resource_Unsafe>::const_iterator>
631 
633  {
634  const auto& self = *this;
635  return self.begin();
636  }
637 
639  {
640  const auto& self = *this;
641  return self.end();
642  }
643 
645  {
646  return resourcesNoMutationWithoutExclusiveOwnership.begin();
647  }
648 
650  {
651  return resourcesNoMutationWithoutExclusiveOwnership.end();
652  }
653 
654  // Using this operator makes it easy to copy a resources object into
655  // a protocol buffer field.
656  // Note that the google::protobuf::RepeatedPtrField<Resource> is
657  // generated at runtime.
658  operator google::protobuf::RepeatedPtrField<Resource>() const;
659 
660  bool operator==(const Resources& that) const;
661  bool operator!=(const Resources& that) const;
662 
663  // NOTE: If any error occurs (e.g., input Resource is not valid or
664  // the first operand is not a superset of the second operand while
665  // doing subtraction), the semantics is as though the second operand
666  // was actually just an empty resource (as though you didn't do the
667  // operation at all).
668  Resources operator+(const Resource& that) const &;
669  Resources operator+(const Resource& that) &&;
670 
671  Resources operator+(Resource&& that) const &;
672  Resources operator+(Resource&& that) &&;
673 
674  Resources& operator+=(const Resource& that);
675  Resources& operator+=(Resource&& that);
676 
677  Resources operator+(const Resources& that) const &;
678  Resources operator+(const Resources& that) &&;
679 
680  Resources operator+(Resources&& that) const &;
681  Resources operator+(Resources&& that) &&;
682 
683  Resources& operator+=(const Resources& that);
684  Resources& operator+=(Resources&& that);
685 
686  Resources operator-(const Resource& that) const;
687  Resources operator-(const Resources& that) const;
688  Resources& operator-=(const Resource& that);
689  Resources& operator-=(const Resources& that);
690 
691  friend std::ostream& operator<<(
692  std::ostream& stream, const Resource_& resource_);
693 
694 private:
695  // Similar to 'contains(const Resource&)' but skips the validity
696  // check. This can be used to avoid the performance overhead of
697  // calling 'contains(const Resource&)' when the resource can be
698  // assumed valid (e.g. it's inside a Resources).
699  //
700  // TODO(jieyu): Measure performance overhead of validity check to
701  // ensure this is warranted.
702  bool _contains(const Resource_& that) const;
703 
704  // Similar to the public 'find', but only for a single Resource
705  // object. The target resource may span multiple roles, so this
706  // returns Resources.
707  Option<Resources> find(const Resource& target) const;
708 
709  // Validation-free versions of += and -= `Resource_` operators.
710  // These can be used when `r` is already validated.
711  //
712  // NOTE: `Resource` objects are implicitly converted to `Resource_`
713  // objects, so here the API can also accept a `Resource` object.
714  void add(const Resource_& r);
715  void add(Resource_&& r);
716 
717  // TODO(mzhu): Add move support.
718  void add(const Resource_Unsafe& that);
719 
720  void subtract(const Resource_& r);
721 
722  Resources& operator+=(const Resource_& that);
723  Resources& operator+=(Resource_&& that);
724 
725  Resources& operator-=(const Resource_& that);
726 
727  // Resources are stored using copy-on-write:
728  //
729  // (1) Copies are done by copying the `shared_ptr`. This
730  // makes read-only filtering (e.g. `unreserved()`)
731  // inexpensive as we do not have to perform copies
732  // of the resource objects.
733  //
734  // (2) When a write occurs:
735  // (a) If there's a single reference to the resource
736  // object, we mutate directly.
737  // (b) If there's more than a single reference to the
738  // resource object, we copy first, then mutate the copy.
739  //
740  // We name the `vector` field `resourcesNoMutationWithoutExclusiveOwnership`
741  // and typedef its item type to `Resource_Unsafe` to alert people
742  // regarding (2).
743  //
744  // TODO(mzhu): While naming the vector and its item type may help, this is
745  // still brittle and certainly not ideal. Explore more robust designs such as
746  // introducing a customized copy-on-write abstraction that hides direct
747  // setters and only allow mutations in a controlled fashion.
748  //
749  // TODO(mzhu): Consider using `boost::intrusive_ptr` for
750  // possibly better performance.
751  //
752  // We chose a size of 15 based on the fact that we have five first class
753  // resources (cpu, mem, disk, gpu and port). And 15 would allow one set of
754  // unreserved resources and two sets of reservations.
755  boost::container::small_vector<Resource_Unsafe, 15>
756  resourcesNoMutationWithoutExclusiveOwnership;
757 };
758 
759 
760 std::ostream& operator<<(
761  std::ostream& stream,
762  const Resources::Resource_& resource);
763 
764 
765 std::ostream& operator<<(std::ostream& stream, const Resource& resource);
766 
767 
768 std::ostream& operator<<(std::ostream& stream, const Resources& resources);
769 
770 
771 std::ostream& operator<<(
772  std::ostream& stream,
773  const google::protobuf::RepeatedPtrField<Resource>& resources);
774 
775 
777  const google::protobuf::RepeatedPtrField<Resource>& left,
778  const Resources& right)
779 {
780  return Resources(left) + right;
781 }
782 
783 
785  const google::protobuf::RepeatedPtrField<Resource>& left,
786  const Resources& right)
787 {
788  return Resources(left) - right;
789 }
790 
791 
792 inline bool operator==(
793  const google::protobuf::RepeatedPtrField<Resource>& left,
794  const Resources& right)
795 {
796  return Resources(left) == right;
797 }
798 
799 
800 template <typename Key>
803  const hashmap<Key, Resources>& right)
804 {
805  foreachpair (const Key& key, const Resources& resources, right) {
806  left[key] += resources;
807  }
808  return left;
809 }
810 
811 
812 template <typename Key>
814  const hashmap<Key, Resources>& left,
815  const hashmap<Key, Resources>& right)
816 {
817  hashmap<Key, Resources> result = left;
818  result += right;
819  return result;
820 }
821 
822 
823 // Tests if `right` is contained in `left`, note that most
824 // callers should just make use of `Resources::contains(...)`.
825 // However, if dealing only with singular `Resource` objects,
826 // this has lower overhead.
827 //
828 // NOTE: `left` and `right` must be valid resource objects.
829 bool contains(const Resource& left, const Resource& right);
830 
831 
837 {
838 public:
839  typedef lambda::function<Try<Nothing>(const Resources&)> PostValidation;
840 
842  const Resources& _consumed,
843  const Resources& _converted,
844  const Option<PostValidation>& _postValidation = None())
845  : consumed(_consumed),
846  converted(_converted),
847  postValidation(_postValidation) {}
848 
849  Try<Resources> apply(const Resources& resources) const;
850 
854 };
855 
856 } // namespace v1 {
857 } // namespace mesos {
858 
859 #endif // __MESOS_V1_RESOURCES_HPP__
Resources toUnreserved() const
Resources revocable() const
Resources reserved(const Option< std::string > &role=None()) const
Resources & operator=(const Resources &that)
Definition: resources.hpp:419
static Option< Error > validate(const Resource &resource)
Validates a Resource object.
Definition: errorbase.hpp:36
Resources unreserved() const
T & get()&
Definition: try.hpp:80
const_iterator end()
Definition: resources.hpp:638
static bool isPersistentVolume(const Resource &resource)
Definition: check.hpp:33
Option< Bytes > disk() const
Definition: resource_quantities.hpp:63
Resources nonRevocable() const
Option< double > cpus() const
Try< Resources > apply(const Iterable &iterable) const
Definition: resources.hpp:572
static Try< std::vector< Resource > > fromJSON(const JSON::Array &resourcesJSON, const std::string &defaultRole="*")
Parses an input JSON array into a vector of Resource objects.
Resources popReservation() const
Resources persistentVolumes() const
static Try< std::vector< Resource > > fromSimpleString(const std::string &text, const std::string &defaultRole="*")
Parses an input text string into a vector of Resource objects.
bool empty() const
Definition: resources.hpp:437
static bool isAllocatableTo(const Resource &resource, const std::string &role)
boost::indirect_iterator< boost::container::small_vector_base< Resource_Unsafe >::const_iterator > const_iterator
Definition: resources.hpp:630
Resources reservedToRoleSubtree(const std::string &role) const
bool operator==(const Resources &that) const
static const std::string & reservationRole(const Resource &resource)
Definition: json.hpp:198
Resources filter(const lambda::function< bool(const Resource &)> &predicate) const
static bool hasResourceProvider(const Resource &resource)
Operation
Definition: cgroups.hpp:444
Future< Nothing > add(const T &metric)
Definition: metrics.hpp:95
Resources & operator-=(const Resource &that)
static bool isDynamicallyReserved(const Resource &resource)
std::map< std::string, Value_Type > types() const
Resources operator+(const Resource &that) const &
static Try< Resource > parse(const std::string &name, const std::string &value, const std::string &role)
Returns a Resource with the given name, value, and role.
static bool hasRefinedReservations(const Resource &resource)
static bool isReserved(const Resource &resource, const Option< std::string > &role=None())
hashmap< std::string, Resources > allocations() const
Definition: hashmap.hpp:38
Resources pushReservation(const Resource::ReservationInfo &reservation) const
Represents a resource conversion, usually as a result of an offer operation.
Definition: resources.hpp:836
static bool isReservedToRoleSubtree(const Resource &resource, const std::string &role)
const_iterator begin() const
Definition: resources.hpp:644
Resources createStrippedScalarQuantity() const
size_t size() const
Definition: resources.hpp:442
bool contains(const Resources &that) const
hashmap< std::string, Resources > reservations() const
Resources nonShared() const
static bool isAllocatedToRoleSubtree(const Resource &resource, const std::string &role)
static bool isShared(const Resource &resource)
Resources operator-(const Resource &that) const
Resources consumed
Definition: resources.hpp:851
ResourceConversion(const Resources &_consumed, const Resources &_converted, const Option< PostValidation > &_postValidation=None())
Definition: resources.hpp:841
Definition: agent.hpp:25
void allocate(const std::string &role)
const_iterator end() const
Definition: resources.hpp:649
Definition: resources.hpp:84
#define foreachpair(KEY, VALUE, ELEMS)
Definition: foreach.hpp:51
bool operator==(const CommandInfo &left, const CommandInfo &right)
#define foreachvalue(VALUE, ELEMS)
Definition: foreach.hpp:77
Option< Value::Ranges > ports() const
Resources()
Definition: resources.hpp:402
friend std::ostream & operator<<(std::ostream &stream, const Resource_ &resource_)
static Try error(const E &e)
Definition: try.hpp:43
Resources & operator=(Resources &&that)
Definition: resources.hpp:428
Resources allocatedToRoleSubtree(const std::string &role) const
lambda::function< Try< Nothing >const Resources &)> PostValidation
Definition: resources.hpp:839
const_iterator begin()
Definition: resources.hpp:632
size_t count(const Resource &that) const
Definition: none.hpp:27
bool isError() const
Definition: try.hpp:78
Option< Bytes > mem() const
bool operator!=(const Resources &that) const
static bool isRevocable(const Resource &resource)
Type
Definition: capabilities.hpp:82
Resources scalars() const
Resources converted
Definition: resources.hpp:852
Resources allocatableTo(const std::string &role) const
static bool isUnreserved(const Resource &resource)
Try< uint32_t > type(const std::string &path)
Option< Value::Ranges > ephemeral_ports() const
static Try< std::vector< Resource > > fromString(const std::string &text, const std::string &defaultRole="*")
Parse an input string into a vector of Resource objects.
bool operator!=(const Labels &left, const Labels &right)
Try< Resources > apply(const ResourceConversion &conversion) const
static bool isDisk(const Resource &resource, const Resource::DiskInfo::Source::Type &type)
std::shared_ptr< Resource_ > Resource_Unsafe
Definition: resources.hpp:176
std::set< std::string > names() const
static bool shrink(Resource *resource, const Value::Scalar &target)
static Resources sum(const hashmap< Key, Resources > &_resources)
Definition: resources.hpp:391
constexpr const char * name
Definition: shell.hpp:41
Option< Resources > find(const Resources &targets) const
Resources shared() const
Resources & operator+=(const Resource &that)
Option< double > gpus() const
Option< PostValidation > postValidation
Definition: resources.hpp:853
static bool isEmpty(const Resource &resource)