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 2016/02/04 20:21:53 UTC

[1/3] mesos git commit: A few style fixes in cgroups net_cls isolator.

Repository: mesos
Updated Branches:
  refs/heads/master cc877e545 -> ebdece373


A few style fixes in cgroups net_cls isolator.


Project: http://git-wip-us.apache.org/repos/asf/mesos/repo
Commit: http://git-wip-us.apache.org/repos/asf/mesos/commit/ebdece37
Tree: http://git-wip-us.apache.org/repos/asf/mesos/tree/ebdece37
Diff: http://git-wip-us.apache.org/repos/asf/mesos/diff/ebdece37

Branch: refs/heads/master
Commit: ebdece3732ec64ff2406135e46e7df3df128c888
Parents: 9c57aa9
Author: Jie Yu <yu...@gmail.com>
Authored: Thu Feb 4 11:05:02 2016 -0800
Committer: Jie Yu <yu...@gmail.com>
Committed: Thu Feb 4 11:21:48 2016 -0800

----------------------------------------------------------------------
 .../mesos/isolators/cgroups/net_cls.cpp         | 38 +++++++--------
 .../mesos/isolators/cgroups/net_cls.hpp         | 49 ++++++++++----------
 2 files changed, 44 insertions(+), 43 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/mesos/blob/ebdece37/src/slave/containerizer/mesos/isolators/cgroups/net_cls.cpp
----------------------------------------------------------------------
diff --git a/src/slave/containerizer/mesos/isolators/cgroups/net_cls.cpp b/src/slave/containerizer/mesos/isolators/cgroups/net_cls.cpp
index 740b1b4..bf6c883 100644
--- a/src/slave/containerizer/mesos/isolators/cgroups/net_cls.cpp
+++ b/src/slave/containerizer/mesos/isolators/cgroups/net_cls.cpp
@@ -54,13 +54,14 @@ namespace mesos {
 namespace internal {
 namespace slave {
 
-string NetClsHandleManager::hexify(uint32_t handle)
+static string hexify(uint32_t handle)
 {
   stringstream stream;
   stream << std::hex << handle;
   return "0x" + stream.str();
 };
 
+
 // For each primary handle, we maintain a bitmap to keep track of
 // allocated and free secondary handles. To find a free secondary
 // handle we scan the bitmap from the first element till we find a
@@ -77,7 +78,7 @@ Try<NetClsHandle> NetClsHandleManager::alloc(uint16_t primary)
 {
   if (!primaries.contains(primary)) {
     return Error(
-        "Primary handle "+ hexify(primary) +
+        "Primary handle " + hexify(primary) +
         " not present in primary handle range");
   }
 
@@ -90,14 +91,12 @@ Try<NetClsHandle> NetClsHandleManager::alloc(uint16_t primary)
         hexify(primary));
   }
 
-  // There is at least one secondary handle free for this primary handle.
+  // At least one secondary handle is free for this primary handle.
   for (size_t count = 1; count < used[primary].size(); count++) {
     if (!used[primary].test(count)) {
       used[primary].set(count);
 
-      NetClsHandle handle(primary, count);
-
-      return handle;
+      return NetClsHandle(primary, count);
     }
   }
 
@@ -135,12 +134,11 @@ Try<Nothing> NetClsHandleManager::reserve(const NetClsHandle& handle)
 }
 
 
-
 Try<Nothing> NetClsHandleManager::free(const NetClsHandle& handle)
 {
   if (!primaries.contains(handle.primary)) {
     return Error(
-        "Primary handle "+ hexify(handle.primary) +
+        "Primary handle " + hexify(handle.primary) +
         " not present in primary handle range");
   }
 
@@ -157,7 +155,8 @@ Try<Nothing> NetClsHandleManager::free(const NetClsHandle& handle)
   if (!used[handle.primary].test(handle.secondary)) {
     return Error(
         "Secondary handle " + hexify(handle.secondary) +
-        " is not allocated for primary handle " + hexify(handle.primary));
+        " is not allocated for primary handle " +
+        hexify(handle.primary));
   }
 
   used[handle.primary].reset(handle.secondary);
@@ -282,10 +281,11 @@ Future<Nothing> CgroupsNetClsIsolatorProcess::recover(
 }
 
 
-// TODO(asridharan): Currently we haven't decided on the entity who will
-// allocate the net_cls handles, or the interfaces through which the net_cls
-// handles will be exposed to network isolators and frameworks. Once the
-// management entity is decided we might need to revisit this implementation.
+// TODO(asridharan): Currently we haven't decided on the entity who
+// will allocate the net_cls handles, or the interfaces through which
+// the net_cls handles will be exposed to network isolators and
+// frameworks. Once the management entity is decided we might need to
+// revisit this implementation.
 Future<Nothing> CgroupsNetClsIsolatorProcess::update(
     const ContainerID& containerId,
     const Resources& resources)
@@ -294,9 +294,9 @@ Future<Nothing> CgroupsNetClsIsolatorProcess::update(
 }
 
 
-// The net_cls handles aren't treated as resources. Further, they have fixed
-// values and hence don't have a notion of usage. We are therefore returning an
-// empty 'ResourceStatistics' object.
+// The net_cls handles aren't treated as resources. Further, they have
+// fixed values and hence don't have a notion of usage. We are
+// therefore returning an empty 'ResourceStatistics' object.
 Future<ResourceStatistics> CgroupsNetClsIsolatorProcess::usage(
     const ContainerID& containerId)
 {
@@ -380,9 +380,9 @@ Future<Nothing> CgroupsNetClsIsolatorProcess::isolate(
 }
 
 
-// The net_cls handles are labels and hence there are no limitations associated
-// with them . This function would therefore always return a pending future
-// since the limitation is never reached.
+// The net_cls handles are labels and hence there are no limitations
+// associated with them. This function would therefore always return
+// a pending future since the limitation is never reached.
 Future<ContainerLimitation> CgroupsNetClsIsolatorProcess::watch(
     const ContainerID& containerId)
 {

http://git-wip-us.apache.org/repos/asf/mesos/blob/ebdece37/src/slave/containerizer/mesos/isolators/cgroups/net_cls.hpp
----------------------------------------------------------------------
diff --git a/src/slave/containerizer/mesos/isolators/cgroups/net_cls.hpp b/src/slave/containerizer/mesos/isolators/cgroups/net_cls.hpp
index 8e3259e..85004ae 100644
--- a/src/slave/containerizer/mesos/isolators/cgroups/net_cls.hpp
+++ b/src/slave/containerizer/mesos/isolators/cgroups/net_cls.hpp
@@ -35,14 +35,15 @@ namespace mesos {
 namespace internal {
 namespace slave {
 
-// This defines the net_cls handle. The handle is composed of two parts, a
-// 16-bit primary handle and a 16-bit secondary handle.
+// This defines the net_cls handle. The handle is composed of two
+// parts, a 16-bit primary handle and a 16-bit secondary handle.
 //
-// TODO(asridharan): Currently we need to define the net_cls handle here, since
-// we cannot use the definitions in `src/linux/routing/handle.hpp` due to its
-// dependency on `libnl`, which is under GPL. Once we have been able to resolve
-// these issues we should remove this definition and use the definition
-// presented in `src/linux/routing/handle.hpp`.
+// TODO(asridharan): Currently we need to define the net_cls handle
+// here, since we cannot use the definitions in
+// `src/linux/routing/handle.hpp` due to its dependency on `libnl`,
+// which is under GPL. Once we have been able to resolve these issues
+// we should remove this definition and use the definition presented
+// in `src/linux/routing/handle.hpp`.
 struct NetClsHandle
 {
   NetClsHandle(uint16_t _primary, uint16_t _secondary)
@@ -51,7 +52,7 @@ struct NetClsHandle
   explicit NetClsHandle(uint32_t handle)
   {
     primary = handle >> 16;
-    secondary = handle & 0xFFFF;
+    secondary = handle & 0xffff;
   };
 
   uint16_t primary;
@@ -59,25 +60,24 @@ struct NetClsHandle
 };
 
 
-// This manages the net_cls handles for the `cgroup/net_cls` isolator.  The
-// isolator can use this with a range of primary handles, which will be managed
-// by this class.  For each primary handle there are 64K possible secondary
-// handles.  For a given primary handle the isolator can get a secondary handle
-// by calling `alloc` and release an allocated handle by calling `free` on the
-// secondary handle. For a given primary handle, the isolator can also
-// explicitly reserve a secondary handle by calling `reserve`.
+// This manages the net_cls handles for the `cgroup/net_cls` isolator.
+// The isolator can use this with a range of primary handles, which
+// will be managed by this class. For each primary handle there are
+// 64K possible secondary handles. For a given primary handle the
+// isolator can get a secondary handle by calling `alloc` and release
+// an allocated handle by calling `free` on the secondary handle. For
+// a given primary handle, the isolator can also explicitly reserve a
+// secondary handle by calling `reserve`.
 class NetClsHandleManager
 {
 public:
-  static std::string hexify(uint32_t handle);
-
   NetClsHandleManager(const IntervalSet<uint16_t>& _primaries)
     : primaries(_primaries) {};
 
   ~NetClsHandleManager() {};
 
   // Allocates a primary handle from the given interval set.
-  Try<uint16_t> allocPrimary() { return Error("Not Implemented");};
+  Try<uint16_t> allocPrimary() { return Error("Not Implemented"); }
   Try<NetClsHandle> alloc(uint16_t primary);
 
   Try<Nothing> reserve(const NetClsHandle& handle);
@@ -92,11 +92,12 @@ private:
 
 
 // Uses the Linux net_cls subsystem for allocating network handles to
-// containers. The network handles of a net_cls cgroup will be used for tagging
-// packets originating from containers belonging to that cgroup. The tags on the
-// packets can then be used by applications, such as traffic-controllers (tc)
-// and firewalls (iptables), to provide network performance isolation. A more
-// detailed explanation can be found at:
+// containers. The network handles of a net_cls cgroup will be used
+// for tagging packets originating from containers belonging to that
+// cgroup. The tags on the packets can then be used by applications,
+// such as traffic-controllers (tc) and firewalls (iptables), to
+// provide network performance isolation. A more detailed explanation
+// can be found at:
 // https://www.kernel.org/doc/Documentation/cgroups/net_cls.txt
 class CgroupsNetClsIsolatorProcess : public MesosIsolatorProcess
 {
@@ -144,11 +145,11 @@ private:
   };
 
   const Flags flags;
-
   const std::string hierarchy;
 
   hashmap<ContainerID, Info> infos;
 };
+
 } // namespace slave {
 } // namespace internal {
 } // namespace mesos {


[3/3] mesos git commit: Defined the NetClsHandleManager class.

Posted by ji...@apache.org.
Defined the NetClsHandleManager class.

This class will be responsible for keeping track of the free and
allocated net_cls handles.

Review: https://reviews.apache.org/r/42586/


Project: http://git-wip-us.apache.org/repos/asf/mesos/repo
Commit: http://git-wip-us.apache.org/repos/asf/mesos/commit/5ff85e1d
Tree: http://git-wip-us.apache.org/repos/asf/mesos/tree/5ff85e1d
Diff: http://git-wip-us.apache.org/repos/asf/mesos/diff/5ff85e1d

Branch: refs/heads/master
Commit: 5ff85e1d1be69fb261ab373bc9ea2fa9e4902f82
Parents: cc877e5
Author: Avinash sridharan <av...@mesosphere.io>
Authored: Thu Feb 4 10:46:09 2016 -0800
Committer: Jie Yu <yu...@gmail.com>
Committed: Thu Feb 4 11:21:48 2016 -0800

----------------------------------------------------------------------
 .../mesos/isolators/cgroups/net_cls.cpp         | 18 ++++++
 .../mesos/isolators/cgroups/net_cls.hpp         | 63 ++++++++++++++++++++
 2 files changed, 81 insertions(+)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/mesos/blob/5ff85e1d/src/slave/containerizer/mesos/isolators/cgroups/net_cls.cpp
----------------------------------------------------------------------
diff --git a/src/slave/containerizer/mesos/isolators/cgroups/net_cls.cpp b/src/slave/containerizer/mesos/isolators/cgroups/net_cls.cpp
index 8821ccd..52a1109 100644
--- a/src/slave/containerizer/mesos/isolators/cgroups/net_cls.cpp
+++ b/src/slave/containerizer/mesos/isolators/cgroups/net_cls.cpp
@@ -51,6 +51,24 @@ namespace mesos {
 namespace internal {
 namespace slave {
 
+Try<NetClsHandle> NetClsHandleManager::alloc(uint16_t primary)
+{
+  return Error("Not Implemented");
+}
+
+
+Try<Nothing> NetClsHandleManager::reserve(const NetClsHandle& handle)
+{
+  return Error("Not Implemented");
+}
+
+
+Try<Nothing> NetClsHandleManager::free(const NetClsHandle& handle)
+{
+  return Error("Not Implemented");
+}
+
+
 CgroupsNetClsIsolatorProcess::CgroupsNetClsIsolatorProcess(
     const Flags& _flags,
     const string& _hierarchy)

http://git-wip-us.apache.org/repos/asf/mesos/blob/5ff85e1d/src/slave/containerizer/mesos/isolators/cgroups/net_cls.hpp
----------------------------------------------------------------------
diff --git a/src/slave/containerizer/mesos/isolators/cgroups/net_cls.hpp b/src/slave/containerizer/mesos/isolators/cgroups/net_cls.hpp
index b4bc521..6de2056 100644
--- a/src/slave/containerizer/mesos/isolators/cgroups/net_cls.hpp
+++ b/src/slave/containerizer/mesos/isolators/cgroups/net_cls.hpp
@@ -17,9 +17,18 @@
 #ifndef __CGROUPS_NET_CLS_ISOLATOR_HPP__
 #define __CGROUPS_NET_CLS_ISOLATOR_HPP__
 
+#include <stdint.h>
+
+#include <bitset>
+#include <iostream>
+#include <sstream>
 #include <string>
 
 #include <stout/hashmap.hpp>
+#include <stout/interval.hpp>
+#include <stout/nothing.hpp>
+#include <stout/result.hpp>
+#include <stout/try.hpp>
 
 #include "slave/containerizer/mesos/isolator.hpp"
 
@@ -27,6 +36,60 @@ namespace mesos {
 namespace internal {
 namespace slave {
 
+// This defines the net_cls handle. The handle is composed of two parts, a
+// 16-bit primary handle and a 16-bit secondary handle.
+//
+// TODO(asridharan): Currently we need to define the net_cls handle here, since
+// we cannot use the definitions in `src/linux/routing/handle.hpp` due to its
+// dependency on `libnl`, which is under GPL. Once we have been able to resolve
+// these issues we should remove this definition and use the definition
+// presented in `src/linux/routing/handle.hpp`.
+struct NetClsHandle
+{
+  NetClsHandle(uint16_t _primary, uint16_t _secondary)
+    : primary(_primary), secondary(_secondary) {};
+
+  explicit NetClsHandle(uint32_t handle)
+  {
+    primary = handle >> 16;
+    secondary = handle & 0xFFFF;
+  };
+
+  uint16_t primary;
+  uint16_t secondary;
+};
+
+
+// This manages the net_cls handles for the `cgroup/net_cls` isolator.  The
+// isolator can use this with a range of primary handles, which will be managed
+// by this class.  For each primary handle there are 64K possible secondary
+// handles.  For a given primary handle the isolator can get a secondary handle
+// by calling `alloc` and release an allocated handle by calling `free` on the
+// secondary handle. For a given primary handle, the isolator can also
+// explicitly reserve a secondary handle by calling `reserve`.
+class NetClsHandleManager
+{
+public:
+  NetClsHandleManager(const IntervalSet<uint16_t>& _primaries)
+    : primaries(_primaries) {};
+
+  ~NetClsHandleManager() {};
+
+  // Allocates a primary handle from the given interval set.
+  Try<uint16_t> allocPrimary() { return Error("Not Implemented");};
+  Try<NetClsHandle> alloc(uint16_t primary);
+
+  Try<Nothing> reserve(const NetClsHandle& handle);
+  Try<Nothing> free(const NetClsHandle& handle);
+
+private:
+  // The key to this hashmap is the 16-bit primary handle.
+  hashmap<uint16_t, std::bitset<0x10000>> used;
+
+  IntervalSet<uint16_t> primaries;
+};
+
+
 // Uses the Linux net_cls subsystem for allocating network handles to
 // containers. The network handles of a net_cls cgroup will be used for tagging
 // packets originating from containers belonging to that cgroup. The tags on the


[2/3] mesos git commit: Implemented the NetClsHandleManager class.

Posted by ji...@apache.org.
Implemented the NetClsHandleManager class.

Review: https://reviews.apache.org/r/42587/


Project: http://git-wip-us.apache.org/repos/asf/mesos/repo
Commit: http://git-wip-us.apache.org/repos/asf/mesos/commit/9c57aa97
Tree: http://git-wip-us.apache.org/repos/asf/mesos/tree/9c57aa97
Diff: http://git-wip-us.apache.org/repos/asf/mesos/diff/9c57aa97

Branch: refs/heads/master
Commit: 9c57aa97cbae07f5c7c0c4b002d952b371babb0c
Parents: 5ff85e1
Author: Avinash sridharan <av...@mesosphere.io>
Authored: Thu Feb 4 10:46:21 2016 -0800
Committer: Jie Yu <yu...@gmail.com>
Committed: Thu Feb 4 11:21:48 2016 -0800

----------------------------------------------------------------------
 .../mesos/isolators/cgroups/net_cls.cpp         | 103 ++++++++++++++++++-
 .../mesos/isolators/cgroups/net_cls.hpp         |   3 +-
 2 files changed, 102 insertions(+), 4 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/mesos/blob/9c57aa97/src/slave/containerizer/mesos/isolators/cgroups/net_cls.cpp
----------------------------------------------------------------------
diff --git a/src/slave/containerizer/mesos/isolators/cgroups/net_cls.cpp b/src/slave/containerizer/mesos/isolators/cgroups/net_cls.cpp
index 52a1109..740b1b4 100644
--- a/src/slave/containerizer/mesos/isolators/cgroups/net_cls.cpp
+++ b/src/slave/containerizer/mesos/isolators/cgroups/net_cls.cpp
@@ -15,6 +15,7 @@
 // limitations under the License.
 
 #include <list>
+#include <sstream>
 #include <vector>
 
 #include <process/defer.hpp>
@@ -31,9 +32,11 @@
 
 #include "slave/containerizer/mesos/isolators/cgroups/net_cls.hpp"
 
+using std::bitset;
 using std::list;
 using std::set;
 using std::string;
+using std::stringstream;
 using std::vector;
 
 using process::Failure;
@@ -51,21 +54,115 @@ namespace mesos {
 namespace internal {
 namespace slave {
 
+string NetClsHandleManager::hexify(uint32_t handle)
+{
+  stringstream stream;
+  stream << std::hex << handle;
+  return "0x" + stream.str();
+};
+
+// For each primary handle, we maintain a bitmap to keep track of
+// allocated and free secondary handles. To find a free secondary
+// handle we scan the bitmap from the first element till we find a
+// free handle.
+//
+// TODO(asridharan): Currently the bitmap search is naive, since the
+// assumption is that the number of containers running on an agent
+// will be O(100). If we start facing any performance issues, we might
+// want to revisit this logic and make the search for a free secondary
+// handle more efficient. One idea for making it more efficient would
+// be to store the last allocated handle and start the search at this
+// position and performing a circular search on the bitmap.
 Try<NetClsHandle> NetClsHandleManager::alloc(uint16_t primary)
 {
-  return Error("Not Implemented");
+  if (!primaries.contains(primary)) {
+    return Error(
+        "Primary handle "+ hexify(primary) +
+        " not present in primary handle range");
+  }
+
+  if (!used.contains(primary)) {
+    // NOTE: We never use 0 as a secondary handle, so mark it used.
+    used[primary].set(0);
+  } else if (used[primary].all()) {
+    return Error(
+        "No free handles remaining for primary handle " +
+        hexify(primary));
+  }
+
+  // There is at least one secondary handle free for this primary handle.
+  for (size_t count = 1; count < used[primary].size(); count++) {
+    if (!used[primary].test(count)) {
+      used[primary].set(count);
+
+      NetClsHandle handle(primary, count);
+
+      return handle;
+    }
+  }
+
+  UNREACHABLE();
 }
 
 
 Try<Nothing> NetClsHandleManager::reserve(const NetClsHandle& handle)
 {
-  return Error("Not Implemented");
+  if (!primaries.contains(handle.primary)) {
+    return Error(
+        "Primary handle " + hexify(handle.primary) +
+        " not present in primary handle range");
+  }
+
+  if (handle.secondary == 0) {
+    return Error("0 is an invalid secondary handle");
+  }
+
+  if (!used.contains(handle.primary)) {
+    // NOTE: We never use 0 as a secondary handle, so mark it used.
+    used[handle.primary].set(0);
+  }
+
+  if (used[handle.primary].test(handle.secondary)) {
+    return Error(
+        "The secondary handle " + hexify(handle.secondary) +
+        ", for the primary handle " + hexify(handle.primary) +
+        " has already been allocated");
+  }
+
+  used[handle.primary].set(handle.secondary);
+
+  return Nothing();
 }
 
 
+
 Try<Nothing> NetClsHandleManager::free(const NetClsHandle& handle)
 {
-  return Error("Not Implemented");
+  if (!primaries.contains(handle.primary)) {
+    return Error(
+        "Primary handle "+ hexify(handle.primary) +
+        " not present in primary handle range");
+  }
+
+  if (handle.secondary == 0) {
+    return Error("0 is an invalid secondary handle");
+  }
+
+  if (!used.contains(handle.primary)) {
+    return Error(
+        "No secondary handles have been allocated from this primary handle " +
+        hexify(handle.primary));
+  }
+
+  if (!used[handle.primary].test(handle.secondary)) {
+    return Error(
+        "Secondary handle " + hexify(handle.secondary) +
+        " is not allocated for primary handle " + hexify(handle.primary));
+  }
+
+  used[handle.primary].reset(handle.secondary);
+
+  return Nothing();
 }
 
 

http://git-wip-us.apache.org/repos/asf/mesos/blob/9c57aa97/src/slave/containerizer/mesos/isolators/cgroups/net_cls.hpp
----------------------------------------------------------------------
diff --git a/src/slave/containerizer/mesos/isolators/cgroups/net_cls.hpp b/src/slave/containerizer/mesos/isolators/cgroups/net_cls.hpp
index 6de2056..8e3259e 100644
--- a/src/slave/containerizer/mesos/isolators/cgroups/net_cls.hpp
+++ b/src/slave/containerizer/mesos/isolators/cgroups/net_cls.hpp
@@ -21,7 +21,6 @@
 
 #include <bitset>
 #include <iostream>
-#include <sstream>
 #include <string>
 
 #include <stout/hashmap.hpp>
@@ -70,6 +69,8 @@ struct NetClsHandle
 class NetClsHandleManager
 {
 public:
+  static std::string hexify(uint32_t handle);
+
   NetClsHandleManager(const IntervalSet<uint16_t>& _primaries)
     : primaries(_primaries) {};