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