You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@mesos.apache.org by ji...@apache.org on 2014/12/16 23:41:27 UTC
mesos git commit: Added abstraction for resources transformation.
Repository: mesos
Updated Branches:
refs/heads/master 0b543658c -> bff02b0c1
Added abstraction for resources transformation.
Review: https://reviews.apache.org/r/29018
Project: http://git-wip-us.apache.org/repos/asf/mesos/repo
Commit: http://git-wip-us.apache.org/repos/asf/mesos/commit/bff02b0c
Tree: http://git-wip-us.apache.org/repos/asf/mesos/tree/bff02b0c
Diff: http://git-wip-us.apache.org/repos/asf/mesos/diff/bff02b0c
Branch: refs/heads/master
Commit: bff02b0c15ef5307aad651046379e49f43ecfa4f
Parents: 0b54365
Author: Jie Yu <yu...@gmail.com>
Authored: Fri Dec 12 21:54:42 2014 -0800
Committer: Jie Yu <yu...@gmail.com>
Committed: Tue Dec 16 14:40:20 2014 -0800
----------------------------------------------------------------------
include/mesos/resources.hpp | 49 ++++++++++++++++++++++++++++++++++++++++
src/common/resources.cpp | 49 ++++++++++++++++++++++++++++++++++++++++
2 files changed, 98 insertions(+)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/mesos/blob/bff02b0c/include/mesos/resources.hpp
----------------------------------------------------------------------
diff --git a/include/mesos/resources.hpp b/include/mesos/resources.hpp
index 296553a..a502459 100644
--- a/include/mesos/resources.hpp
+++ b/include/mesos/resources.hpp
@@ -21,11 +21,13 @@
#include <iostream>
#include <string>
+#include <vector>
#include <mesos/mesos.hpp>
#include <mesos/values.hpp>
#include <stout/bytes.hpp>
+#include <stout/foreach.hpp>
#include <stout/option.hpp>
#include <stout/try.hpp>
@@ -186,6 +188,53 @@ public:
Resources& operator -= (const Resource& that);
Resources& operator -= (const Resources& that);
+ // This is an abstraction for describing a transformation that can
+ // be applied to Resources. Transformations cannot not alter the
+ // quantity, or the static role of the resources.
+ class Transformation
+ {
+ public:
+ virtual ~Transformation() {}
+
+ // Returns the result of the transformation, applied to the given
+ // 'resources'. Returns an Error if the transformation cannot be
+ // applied, or the transformation invariants do not hold.
+ Try<Resources> operator () (const Resources& resources) const;
+
+ protected:
+ virtual Try<Resources> apply(const Resources& resources) const = 0;
+ };
+
+ // Represents a sequence of transformations, the transformations are
+ // applied in an all-or-nothing manner. We follow the composite
+ // pattern here.
+ class CompositeTransformation : public Transformation
+ {
+ public:
+ CompositeTransformation() {}
+
+ ~CompositeTransformation()
+ {
+ foreach (Transformation* transformation, transformations) {
+ delete transformation;
+ }
+ }
+
+ // TODO(jieyu): Consider using unique_ptr here once we finialize
+ // our style guide for unique_ptr.
+ template <typename T>
+ void add(const T& t)
+ {
+ transformations.push_back(new T(t));
+ }
+
+ protected:
+ virtual Try<Resources> apply(const Resources& resources) const;
+
+ private:
+ std::vector<Transformation*> transformations;
+ };
+
private:
bool contains(const Resource& that) const;
http://git-wip-us.apache.org/repos/asf/mesos/blob/bff02b0c/src/common/resources.cpp
----------------------------------------------------------------------
diff --git a/src/common/resources.cpp b/src/common/resources.cpp
index 9bf7ae9..1cd885f 100644
--- a/src/common/resources.cpp
+++ b/src/common/resources.cpp
@@ -854,4 +854,53 @@ ostream& operator << (ostream& stream, const Resources& resources)
return stream;
}
+
+/////////////////////////////////////////////////
+// Resources transformations.
+/////////////////////////////////////////////////
+
+
+Try<Resources> Resources::Transformation::operator () (
+ const Resources& resources) const
+{
+ Try<Resources> applied = apply(resources);
+
+ if (applied.isSome()) {
+ // Additional checks.
+
+ // Ensure the amount of each type of resource does not change.
+ // TODO(jieyu): Currently, we only checks known resource types
+ // like cpus, mem, disk, ports, etc. We should generalize this.
+ if (resources.cpus() != applied.get().cpus() ||
+ resources.mem() != applied.get().mem() ||
+ resources.disk() != applied.get().disk() ||
+ resources.ports() != applied.get().ports()) {
+ return Error("Resource amount does not match");
+ }
+
+ // TODO(jieyu): Ensure that static role does not change.
+ }
+
+ return applied;
+}
+
+
+Try<Resources> Resources::CompositeTransformation::apply(
+ const Resources& resources) const
+{
+ Resources result = resources;
+
+ foreach (Transformation* transformation, transformations) {
+ Try<Resources> transformed = (*transformation)(result);
+
+ if (transformed.isError()) {
+ return Error(transformed.error());
+ }
+
+ result = transformed.get();
+ }
+
+ return result;
+}
+
} // namespace mesos {