You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@mesos.apache.org by ya...@apache.org on 2017/05/23 07:44:29 UTC

[3/4] mesos git commit: Add support for not enforcing XFS quotas.

Add support for not enforcing XFS quotas.

Add XFS disk isolator support for not enforcing disk quotas on
containers. While there is a global filesystem configuration option
to turn off quota enforcement, we should not automatically toggle
that because we don't know why the operator might have changed that
configuration. Instead, we just apply an unlimited (0) quota, which
engages XFS space accounting without enforcing any limit.

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


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

Branch: refs/heads/master
Commit: e86f547e334092de7884db6f32a9cdd396862769
Parents: 80b652c
Author: James Peach <jp...@apache.org>
Authored: Tue May 23 00:34:48 2017 -0700
Committer: Jiang Yan Xu <xu...@apple.com>
Committed: Tue May 23 00:44:23 2017 -0700

----------------------------------------------------------------------
 .../containerizer/mesos/isolators/xfs/disk.cpp  |  54 ++++--
 .../containerizer/mesos/isolators/xfs/disk.hpp  |   2 +
 .../containerizer/mesos/isolators/xfs/utils.hpp |   6 +
 src/tests/containerizer/xfs_quota_tests.cpp     | 167 ++++++++++++++++++-
 4 files changed, 212 insertions(+), 17 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/mesos/blob/e86f547e/src/slave/containerizer/mesos/isolators/xfs/disk.cpp
----------------------------------------------------------------------
diff --git a/src/slave/containerizer/mesos/isolators/xfs/disk.cpp b/src/slave/containerizer/mesos/isolators/xfs/disk.cpp
index 72bef87..2113f86 100644
--- a/src/slave/containerizer/mesos/isolators/xfs/disk.cpp
+++ b/src/slave/containerizer/mesos/isolators/xfs/disk.cpp
@@ -156,15 +156,22 @@ Try<Isolator*> XfsDiskIsolatorProcess::create(const Flags& flags)
     return Error(status->message);
   }
 
+  xfs::QuotaPolicy quotaPolicy =
+    flags.enforce_container_disk_quota ? xfs::QuotaPolicy::ENFORCING
+                                       : xfs::QuotaPolicy::ACCOUNTING;
+
   return new MesosIsolator(Owned<MesosIsolatorProcess>(
-      new XfsDiskIsolatorProcess(flags.work_dir, totalProjectIds.get())));
+      new XfsDiskIsolatorProcess(
+          quotaPolicy, flags.work_dir, totalProjectIds.get())));
 }
 
 
 XfsDiskIsolatorProcess::XfsDiskIsolatorProcess(
+    xfs::QuotaPolicy _quotaPolicy,
     const std::string& _workDir,
     const IntervalSet<prid_t>& projectIds)
   : ProcessBase(process::ID::generate("xfs-disk-isolator")),
+    quotaPolicy(_quotaPolicy),
     workDir(_workDir),
     totalProjectIds(projectIds),
     freeProjectIds(projectIds)
@@ -322,23 +329,37 @@ Future<Nothing> XfsDiskIsolatorProcess::update(
     return Nothing();
   }
 
-  // Only update the disk quota if it has changed.
-  if (needed.get() != info->quota) {
-    Try<Nothing> status =
-      xfs::setProjectQuota(info->directory, info->projectId, needed.get());
+  switch (quotaPolicy) {
+    case xfs::QuotaPolicy::ACCOUNTING: {
+      Try<Nothing> status = xfs::clearProjectQuota(
+          info->directory, info->projectId);
+
+      if (status.isError()) {
+        return Failure("Failed to clear quota for project " +
+                       stringify(info->projectId) + ": " + status.error());
+      }
 
-    if (status.isError()) {
-      return Failure("Failed to update quota for project " +
-                     stringify(info->projectId) + ": " + status.error());
+      break;
     }
 
-    info->quota = needed.get();
+    case xfs::QuotaPolicy::ENFORCING: {
+      Try<Nothing> status = xfs::setProjectQuota(
+          info->directory, info->projectId, needed.get());
+
+      if (status.isError()) {
+        return Failure("Failed to update quota for project " +
+                       stringify(info->projectId) + ": " + status.error());
+      }
 
-    LOG(INFO) << "Set quota on container " << containerId
-              << " for project " << info->projectId
-              << " to " << info->quota;
+      LOG(INFO) << "Set quota on container " << containerId
+                << " for project " << info->projectId
+                << " to " << needed.get();
+
+      break;
+    }
   }
 
+  info->quota = needed.get();
   return Nothing();
 }
 
@@ -361,9 +382,14 @@ Future<ResourceStatistics> XfsDiskIsolatorProcess::usage(
     return Failure(quota.error());
   }
 
+  // If we didn't set the quota (ie. we are in ACCOUNTING mode),
+  // the quota limit will be 0. Since we are already tracking
+  // what the quota ought to be in the Info, we just always
+  // use that.
+  statistics.set_disk_limit_bytes(info->quota.bytes());
+
   if (quota.isSome()) {
-    statistics.set_disk_limit_bytes(quota.get().limit.bytes());
-    statistics.set_disk_used_bytes(quota.get().used.bytes());
+    statistics.set_disk_used_bytes(quota->used.bytes());
   }
 
   return statistics;

http://git-wip-us.apache.org/repos/asf/mesos/blob/e86f547e/src/slave/containerizer/mesos/isolators/xfs/disk.hpp
----------------------------------------------------------------------
diff --git a/src/slave/containerizer/mesos/isolators/xfs/disk.hpp b/src/slave/containerizer/mesos/isolators/xfs/disk.hpp
index 8f008f3..aced6d9 100644
--- a/src/slave/containerizer/mesos/isolators/xfs/disk.hpp
+++ b/src/slave/containerizer/mesos/isolators/xfs/disk.hpp
@@ -72,6 +72,7 @@ public:
 
 private:
   XfsDiskIsolatorProcess(
+      xfs::QuotaPolicy quotaPolicy,
       const std::string& workDir,
       const IntervalSet<prid_t>& projectIds);
 
@@ -91,6 +92,7 @@ private:
     const prid_t projectId;
   };
 
+  xfs::QuotaPolicy quotaPolicy;
   const std::string workDir;
   const IntervalSet<prid_t> totalProjectIds;
   IntervalSet<prid_t> freeProjectIds;

http://git-wip-us.apache.org/repos/asf/mesos/blob/e86f547e/src/slave/containerizer/mesos/isolators/xfs/utils.hpp
----------------------------------------------------------------------
diff --git a/src/slave/containerizer/mesos/isolators/xfs/utils.hpp b/src/slave/containerizer/mesos/isolators/xfs/utils.hpp
index e5567c5..e034133 100644
--- a/src/slave/containerizer/mesos/isolators/xfs/utils.hpp
+++ b/src/slave/containerizer/mesos/isolators/xfs/utils.hpp
@@ -69,6 +69,12 @@ private:
 };
 
 
+enum class QuotaPolicy {
+  ENFORCING,
+  ACCOUNTING,
+};
+
+
 inline bool operator==(const QuotaInfo& left, const QuotaInfo& right)
 {
   return left.limit == right.limit && left.used == right.used;

http://git-wip-us.apache.org/repos/asf/mesos/blob/e86f547e/src/tests/containerizer/xfs_quota_tests.cpp
----------------------------------------------------------------------
diff --git a/src/tests/containerizer/xfs_quota_tests.cpp b/src/tests/containerizer/xfs_quota_tests.cpp
index 78a8fce..b33fe8b 100644
--- a/src/tests/containerizer/xfs_quota_tests.cpp
+++ b/src/tests/containerizer/xfs_quota_tests.cpp
@@ -32,8 +32,6 @@
 #include <stout/os.hpp>
 #include <stout/path.hpp>
 
-#include <stout/os/constants.hpp>
-
 #include "linux/fs.hpp"
 
 #include "master/master.hpp"
@@ -170,6 +168,7 @@ public:
     // don't mind that other flags refer to a different temp directory.
     flags.work_dir = mountPoint.get();
     flags.isolation = "disk/xfs";
+    flags.enforce_container_disk_quota = true;
     return flags;
   }
 
@@ -441,6 +440,69 @@ TEST_F(ROOT_XFS_QuotaTest, DiskUsageExceedsQuota)
 }
 
 
+// This is the same logic as DiskUsageExceedsQuota except we turn off disk quota
+// enforcement, so exceeding the quota should be allowed.
+TEST_F(ROOT_XFS_QuotaTest, DiskUsageExceedsQuotaNoEnforce)
+{
+  Try<Owned<cluster::Master>> master = StartMaster();
+  ASSERT_SOME(master);
+
+  Owned<MasterDetector> detector = master.get()->createDetector();
+
+  slave::Flags flags = CreateSlaveFlags();
+  flags.enforce_container_disk_quota = false;
+
+  Try<Owned<cluster::Slave>> slave = StartSlave(detector.get(), flags);
+  ASSERT_SOME(slave);
+
+  MockScheduler sched;
+  MesosSchedulerDriver driver(
+      &sched, DEFAULT_FRAMEWORK_INFO, master.get()->pid, DEFAULT_CREDENTIAL);
+
+  EXPECT_CALL(sched, registered(&driver, _, _));
+
+  Future<vector<Offer>> offers;
+  EXPECT_CALL(sched, resourceOffers(&driver, _))
+    .WillOnce(FutureArg<1>(&offers))
+    .WillRepeatedly(Return()); // Ignore subsequent offers.
+
+  driver.start();
+
+  AWAIT_READY(offers);
+  EXPECT_FALSE(offers->empty());
+
+  const Offer& offer = offers.get()[0];
+
+  // Create a task which requests 1MB disk, but actually uses more
+  // than 2MB disk.
+  TaskInfo task = createTask(
+      offer.slave_id(),
+      Resources::parse("cpus:1;mem:128;disk:1").get(),
+      "dd if=/dev/zero of=file bs=1048576 count=2");
+
+  Future<TaskStatus> status1;
+  Future<TaskStatus> status2;
+  EXPECT_CALL(sched, statusUpdate(&driver, _))
+    .WillOnce(FutureArg<1>(&status1))
+    .WillOnce(FutureArg<1>(&status2));
+
+  driver.launchTasks(offer.id(), {task});
+
+  AWAIT_READY(status1);
+  EXPECT_EQ(task.task_id(), status1->task_id());
+  EXPECT_EQ(TASK_RUNNING, status1->state());
+
+  // We expect the task to succeed even though it exceeded
+  // the disk quota.
+  AWAIT_READY(status2);
+  EXPECT_EQ(task.task_id(), status2->task_id());
+  EXPECT_EQ(TASK_FINISHED, status2->state());
+
+  driver.stop();
+  driver.join();
+}
+
+
 // Verify that we can get accurate resource statistics from the XFS
 // disk isolator.
 TEST_F(ROOT_XFS_QuotaTest, ResourceStatistics)
@@ -528,7 +590,106 @@ TEST_F(ROOT_XFS_QuotaTest, ResourceStatistics)
     }
 
     ASSERT_FALSE(timeout.expired());
-    os::sleep(Milliseconds(1));
+    os::sleep(Milliseconds(100));
+  }
+
+  driver.stop();
+  driver.join();
+}
+
+
+// This is the same logic as ResourceStatistics, except the task should
+// be allowed to exceed the disk quota, and usage statistics should report
+// that the quota was exceeded.
+TEST_F(ROOT_XFS_QuotaTest, ResourceStatisticsNoEnforce)
+{
+  Try<Owned<cluster::Master>> master = StartMaster();
+  ASSERT_SOME(master);
+
+  Fetcher fetcher;
+  Owned<MasterDetector> detector = master.get()->createDetector();
+
+  slave::Flags flags = CreateSlaveFlags();
+  flags.enforce_container_disk_quota = false;
+
+  Try<MesosContainerizer*> _containerizer =
+    MesosContainerizer::create(flags, true, &fetcher);
+
+  ASSERT_SOME(_containerizer);
+  Owned<MesosContainerizer> containerizer(_containerizer.get());
+
+  Try<Owned<cluster::Slave>> slave =
+    StartSlave(detector.get(), containerizer.get(), flags);
+  ASSERT_SOME(slave);
+
+  MockScheduler sched;
+
+  MesosSchedulerDriver driver(
+      &sched, DEFAULT_FRAMEWORK_INFO, master.get()->pid, DEFAULT_CREDENTIAL);
+  EXPECT_CALL(sched, registered(_, _, _));
+
+  Future<vector<Offer>> offers;
+  EXPECT_CALL(sched, resourceOffers(_, _))
+    .WillOnce(FutureArg<1>(&offers))
+    .WillRepeatedly(Return()); // Ignore subsequent offers.
+
+  driver.start();
+
+  AWAIT_READY(offers);
+  EXPECT_FALSE(offers->empty());
+
+  Offer offer = offers.get()[0];
+
+  // Create a task that uses 4MB of 3MB disk and fails if it can't
+  // write the full amount.
+  TaskInfo task = createTask(
+      offer.slave_id(),
+      Resources::parse("cpus:1;mem:128;disk:3").get(),
+      "dd if=/dev/zero of=file bs=1048576 count=4 && sleep 1000");
+
+  Future<TaskStatus> status;
+  EXPECT_CALL(sched, statusUpdate(&driver, _))
+    .WillOnce(FutureArg<1>(&status))
+    .WillRepeatedly(Return()); // Ignore subsequent updates.
+
+  driver.launchTasks(offers.get()[0].id(), {task});
+
+  AWAIT_READY(status);
+  EXPECT_EQ(task.task_id(), status->task_id());
+  EXPECT_EQ(TASK_RUNNING, status->state());
+
+  Future<hashset<ContainerID>> containers = containerizer.get()->containers();
+  AWAIT_READY(containers);
+  ASSERT_EQ(1u, containers->size());
+
+  ContainerID containerId = *(containers->begin());
+  Duration diskTimeout = Seconds(5);
+  Timeout timeout = Timeout::in(diskTimeout);
+
+  while (true) {
+    Future<ResourceStatistics> usage = containerizer.get()->usage(containerId);
+    AWAIT_READY(usage);
+
+    ASSERT_TRUE(usage->has_disk_limit_bytes());
+    EXPECT_EQ(Megabytes(3), Bytes(usage->disk_limit_bytes()));
+
+    if (usage->has_disk_used_bytes()) {
+      if (usage->disk_used_bytes() >= Megabytes(4).bytes()) {
+        break;
+      }
+    }
+
+    // The stopping condition for this test is that the isolator is
+    // able to report that we wrote the full amount of data without
+    // being constrained by the task disk limit.
+    EXPECT_LE(usage->disk_used_bytes(), Megabytes(4).bytes());
+
+    ASSERT_FALSE(timeout.expired())
+      << "Used " << Bytes(usage->disk_used_bytes())
+      << " of expected " << Megabytes(4)
+      << " within the " << diskTimeout << " timeout";
+
+    os::sleep(Milliseconds(100));
   }
 
   driver.stop();