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