You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@mesos.apache.org by ch...@apache.org on 2019/06/06 19:05:31 UTC

[mesos] branch master updated (c18efb7 -> 4b9c566)

This is an automated email from the ASF dual-hosted git repository.

chhsiao pushed a change to branch master
in repository https://gitbox.apache.org/repos/asf/mesos.git.


    from c18efb7  Improved operation feedback example framework.
     new fe70100  Made SLRP allow changes in volume context.
     new 6273b50  Added MESOS-9395 to the 1.8.1 CHANGELOG.
     new 24c70aa  Used full paths as volume IDs for the test CSI plugin.
     new a7b98f7  Added a unit test to verify if SLRP allows changes in volume context.
     new 343b6d7  Fixed chaining futures infinitely in `UriDiskProfileAdaptor`.
     new 1271584  Added MESOS-9803 to the 1.8.1 CHANNGELOG.
     new 661bac0  Added MESOS-9803 to the 1.7.3 CHANNGELOG.
     new 4c82acb  Garbage-collected disappeared RPs when agent resources remain unchanged.
     new 4b9c566  Added MESOS-9831 to the 1.8.1 CHANGELOG.

The 9 revisions listed above as "new" are entirely new to this
repository and will be described in separate emails.  The revisions
listed as "add" were already present in the repository and have only
been added to this reference.


Summary of changes:
 CHANGELOG                                          |   4 +
 include/mesos/mesos.proto                          |   3 +-
 include/mesos/v1/mesos.proto                       |   3 +-
 src/examples/test_csi_plugin.cpp                   | 282 ++++++++++--------
 src/master/master.cpp                              |   8 +
 src/resource_provider/storage/provider.cpp         | 177 +++++++----
 .../storage/uri_disk_profile_adaptor.cpp           |  55 +++-
 .../storage/uri_disk_profile_adaptor.hpp           |  22 +-
 src/tests/api_tests.cpp                            |  99 +++++++
 .../storage_local_resource_provider_tests.cpp      | 325 +++++++++++----------
 10 files changed, 628 insertions(+), 350 deletions(-)


[mesos] 07/09: Added MESOS-9803 to the 1.7.3 CHANNGELOG.

Posted by ch...@apache.org.
This is an automated email from the ASF dual-hosted git repository.

chhsiao pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/mesos.git

commit 661bac038d16ec590eac8104e5a73f04cc45834d
Author: Chun-Hung Hsiao <ch...@mesosphere.io>
AuthorDate: Wed Jun 5 15:13:26 2019 -0700

    Added MESOS-9803 to the 1.7.3 CHANNGELOG.
---
 CHANGELOG | 1 +
 1 file changed, 1 insertion(+)

diff --git a/CHANGELOG b/CHANGELOG
index a19654f..69a015b 100644
--- a/CHANGELOG
+++ b/CHANGELOG
@@ -447,6 +447,7 @@ Release Notes - Mesos - Version 1.7.3 (WIP)
   * [MESOS-9766] - /__processes__ endpoint can hang.
   * [MESOS-9786] - Race between two REMOVE_QUOTA calls crashes the master.
   * [MESOS-9787] - Log slow SSL (TLS) peer reverse DNS lookup.
+  * [MESOS-9803] - Memory leak caused by an infinite chain of futures in `UriDiskProfileAdaptor`.
 
 ** Improvements
   * [MESOS-8880] - Add minimum capabilities in the master.


[mesos] 02/09: Added MESOS-9395 to the 1.8.1 CHANGELOG.

Posted by ch...@apache.org.
This is an automated email from the ASF dual-hosted git repository.

chhsiao pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/mesos.git

commit 6273b506f2cbd5b140bb8419c90bf22858bd615b
Author: Chun-Hung Hsiao <ch...@mesosphere.io>
AuthorDate: Wed Jun 5 14:41:57 2019 -0700

    Added MESOS-9395 to the 1.8.1 CHANGELOG.
---
 CHANGELOG | 1 +
 1 file changed, 1 insertion(+)

diff --git a/CHANGELOG b/CHANGELOG
index 8b2a613..9d1e557 100644
--- a/CHANGELOG
+++ b/CHANGELOG
@@ -16,6 +16,7 @@ Release Notes - Mesos - Version 1.8.1 (WIP)
 * This is a bug fix release.
 
 ** Bug
+  * [MESOS-9395] - Check failure on `StorageLocalResourceProviderProcess::applyCreateDisk`.
   * [MESOS-9616] - `Filters.refuse_seconds` declines resources not in offers.
   * [MESOS-9695] - Remove the duplicate pid check in Docker containerizer
   * [MESOS-9750] - Agent V1 GET_STATE response may report a complete executor's tasks as non-terminal after a graceful agent shutdown.


[mesos] 05/09: Fixed chaining futures infinitely in `UriDiskProfileAdaptor`.

Posted by ch...@apache.org.
This is an automated email from the ASF dual-hosted git repository.

chhsiao pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/mesos.git

commit 343b6d7f0d4b3024692fa7ed5669c9819658e382
Author: Chun-Hung Hsiao <ch...@mesosphere.io>
AuthorDate: Thu May 30 15:13:44 2019 -0700

    Fixed chaining futures infinitely in `UriDiskProfileAdaptor`.
    
    Previously it is possible to have an infinite chain of futures when
    `UriDiskProfileAdaptor::watch` is called: if the set of profiles remains
    fixed for every poll, each poll would satisfy a promise that triggers
    an asynchronous recursive call to `UriDiskProfileAdaptor::watch` again.
    
    This patch fixes the problem by removing the asynchronous recursion.
    Instead, we maintain a separated promise for each watcher that is never
    associated to another promise. After each poll, we check if the current
    set of profiles differs from the known set for a watcher, and satisfy
    its own promise if so.
    
    Review: https://reviews.apache.org/r/70766
---
 .../storage/uri_disk_profile_adaptor.cpp           | 55 +++++++++++++++-------
 .../storage/uri_disk_profile_adaptor.hpp           | 22 ++++++---
 2 files changed, 55 insertions(+), 22 deletions(-)

diff --git a/src/resource_provider/storage/uri_disk_profile_adaptor.cpp b/src/resource_provider/storage/uri_disk_profile_adaptor.cpp
index 215f7f9..40eae0c 100644
--- a/src/resource_provider/storage/uri_disk_profile_adaptor.cpp
+++ b/src/resource_provider/storage/uri_disk_profile_adaptor.cpp
@@ -16,6 +16,7 @@
 
 #include "resource_provider/storage/uri_disk_profile_adaptor.hpp"
 
+#include <algorithm>
 #include <map>
 #include <string>
 #include <tuple>
@@ -100,8 +101,7 @@ Future<hashset<string>> UriDiskProfileAdaptor::watch(
 UriDiskProfileAdaptorProcess::UriDiskProfileAdaptorProcess(
     const UriDiskProfileAdaptor::Flags& _flags)
   : ProcessBase(ID::generate("uri-disk-profile-adaptor")),
-    flags(_flags),
-    watchPromise(new Promise<Nothing>()) {}
+    flags(_flags) {}
 
 
 void UriDiskProfileAdaptorProcess::initialize()
@@ -140,24 +140,24 @@ Future<hashset<string>> UriDiskProfileAdaptorProcess::watch(
     const hashset<string>& knownProfiles,
     const ResourceProviderInfo& resourceProviderInfo)
 {
-  // Calculate the new set of profiles for the resource provider.
-  hashset<string> newProfiles;
-  foreachpair (const string& profile,
-               const ProfileRecord& record,
-               profileMatrix) {
+  // Calculate the current set of profiles for the resource provider.
+  hashset<string> currentProfiles;
+  foreachpair (
+      const string& profile, const ProfileRecord& record, profileMatrix) {
     if (record.active &&
         isSelectedResourceProvider(record.manifest, resourceProviderInfo)) {
-      newProfiles.insert(profile);
+      currentProfiles.insert(profile);
     }
   }
 
-  if (newProfiles != knownProfiles) {
-    return newProfiles;
+  if (currentProfiles != knownProfiles) {
+    return currentProfiles;
   }
 
   // Wait for the next update if there is no change.
-  return watchPromise->future()
-    .then(defer(self(), &Self::watch, knownProfiles, resourceProviderInfo));
+  watchers.emplace_back(knownProfiles, resourceProviderInfo);
+
+  return watchers.back().promise.future();
 }
 
 
@@ -272,12 +272,35 @@ void UriDiskProfileAdaptorProcess::notify(
     profileMatrix.put(entry.first, {entry.second, true});
   }
 
-  // Notify any watchers and then prepare a new promise for the next
-  // iteration of polling.
+  // Notify a watcher if its current set of profiles differs from its known set.
   //
   // TODO(josephw): Delay this based on the `--max_random_wait` option.
-  watchPromise->set(Nothing());
-  watchPromise.reset(new Promise<Nothing>());
+  foreach (WatcherData& watcher, watchers) {
+    hashset<string> current;
+    foreachpair (
+        const string& profile, const ProfileRecord& record, profileMatrix) {
+      if (record.active &&
+          isSelectedResourceProvider(record.manifest, watcher.info)) {
+        current.insert(profile);
+      }
+    }
+
+    if (current != watcher.known) {
+      CHECK(watcher.promise.set(current))
+        << "Promise for watcher '" << watcher.info << "' is already "
+        << watcher.promise.future();
+    }
+  }
+
+  // Remove all notified watchers.
+  watchers.erase(
+      std::remove_if(
+          watchers.begin(),
+          watchers.end(),
+          [](const WatcherData& watcher) {
+            return watcher.promise.future().isReady();
+          }),
+      watchers.end());
 
   LOG(INFO)
     << "Updated disk profile mapping to " << parsed.profile_matrix().size()
diff --git a/src/resource_provider/storage/uri_disk_profile_adaptor.hpp b/src/resource_provider/storage/uri_disk_profile_adaptor.hpp
index a5a34dc..027ceaa 100644
--- a/src/resource_provider/storage/uri_disk_profile_adaptor.hpp
+++ b/src/resource_provider/storage/uri_disk_profile_adaptor.hpp
@@ -17,6 +17,7 @@
 #ifndef __RESOURCE_PROVIDER_URI_DISK_PROFILE_ADAPTOR_HPP__
 #define __RESOURCE_PROVIDER_URI_DISK_PROFILE_ADAPTOR_HPP__
 
+#include <list>
 #include <string>
 #include <tuple>
 
@@ -224,10 +225,9 @@ public:
 
 private:
   // Helper that is called upon successfully polling and parsing the `--uri`.
-  // This method will check the following conditions before updating the state
-  // of the module:
-  //   * All known profiles must be included in the updated set.
-  //   * All properties of known profiles must match those in the updated set.
+  // This method will validate that the capability and parameters of a known
+  // profile must remain the same. Then, any watcher will be notified if its set
+  // of profiles has been changed.
   void notify(const resource_provider::DiskProfileMapping& parsed);
 
   UriDiskProfileAdaptor::Flags flags;
@@ -247,8 +247,18 @@ private:
   // TODO(josephw): Consider persisting this mapping across agent restarts.
   hashmap<std::string, ProfileRecord> profileMatrix;
 
-  // Will be satisfied whenever `profileMatrix` is changed.
-  process::Owned<process::Promise<Nothing>> watchPromise;
+  struct WatcherData
+  {
+    WatcherData(
+        const hashset<std::string>& _known, const ResourceProviderInfo& _info)
+      : known(_known), info(_info) {}
+
+    hashset<std::string> known;
+    ResourceProviderInfo info;
+    process::Promise<hashset<std::string>> promise;
+  };
+
+  std::vector<WatcherData> watchers;
 };
 
 } // namespace storage {


[mesos] 06/09: Added MESOS-9803 to the 1.8.1 CHANNGELOG.

Posted by ch...@apache.org.
This is an automated email from the ASF dual-hosted git repository.

chhsiao pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/mesos.git

commit 1271584060af14bbfc6a5715c9da47cdbd7a1438
Author: Chun-Hung Hsiao <ch...@mesosphere.io>
AuthorDate: Wed Jun 5 15:12:48 2019 -0700

    Added MESOS-9803 to the 1.8.1 CHANNGELOG.
---
 CHANGELOG | 1 +
 1 file changed, 1 insertion(+)

diff --git a/CHANGELOG b/CHANGELOG
index 9d1e557..a19654f 100644
--- a/CHANGELOG
+++ b/CHANGELOG
@@ -25,6 +25,7 @@ Release Notes - Mesos - Version 1.8.1 (WIP)
   * [MESOS-9782] - Random sorter fails to clear removed clients.
   * [MESOS-9786] - Race between two REMOVE_QUOTA calls crashes the master.
   * [MESOS-9787] - Log slow SSL (TLS) peer reverse DNS lookup.
+  * [MESOS-9803] - Memory leak caused by an infinite chain of futures in `UriDiskProfileAdaptor`.
 
 ** Improvement
   * [MESOS-9759] - Log required quota headroom and available quota headroom in the allocator.


[mesos] 09/09: Added MESOS-9831 to the 1.8.1 CHANGELOG.

Posted by ch...@apache.org.
This is an automated email from the ASF dual-hosted git repository.

chhsiao pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/mesos.git

commit 4b9c5667e599fc44ee04f1b524c30b95a2c840f8
Author: Chun-Hung Hsiao <ch...@mesosphere.io>
AuthorDate: Thu Jun 6 11:36:53 2019 -0700

    Added MESOS-9831 to the 1.8.1 CHANGELOG.
---
 CHANGELOG | 1 +
 1 file changed, 1 insertion(+)

diff --git a/CHANGELOG b/CHANGELOG
index 69a015b..197d791 100644
--- a/CHANGELOG
+++ b/CHANGELOG
@@ -26,6 +26,7 @@ Release Notes - Mesos - Version 1.8.1 (WIP)
   * [MESOS-9786] - Race between two REMOVE_QUOTA calls crashes the master.
   * [MESOS-9787] - Log slow SSL (TLS) peer reverse DNS lookup.
   * [MESOS-9803] - Memory leak caused by an infinite chain of futures in `UriDiskProfileAdaptor`.
+  * [MESOS-9831] - Master should not report disconnected resource providers.
 
 ** Improvement
   * [MESOS-9759] - Log required quota headroom and available quota headroom in the allocator.


[mesos] 01/09: Made SLRP allow changes in volume context.

Posted by ch...@apache.org.
This is an automated email from the ASF dual-hosted git repository.

chhsiao pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/mesos.git

commit fe7010099133962fd3d58ffde18d6c8e7472e01a
Author: Chun-Hung Hsiao <ch...@mesosphere.io>
AuthorDate: Thu May 9 17:59:16 2019 -0700

    Made SLRP allow changes in volume context.
    
    To make SLRP more robust against non-conforming CSI plugins that change
    volume contexts, the `getExistVolumes` method returns a list of resource
    conversions consisting of one for converting old volume contexts to new
    volume contexts, and one to remove missing volumes and add new volumes.
    
    To make the interfaces consistent, `getStoragePools` now also returns a
    list of resource conversions consisting of one conversion.
    
    Review: https://reviews.apache.org/r/70620
---
 include/mesos/mesos.proto                  |   3 +-
 include/mesos/v1/mesos.proto               |   3 +-
 src/resource_provider/storage/provider.cpp | 177 +++++++++++++++++++----------
 3 files changed, 121 insertions(+), 62 deletions(-)

diff --git a/include/mesos/mesos.proto b/include/mesos/mesos.proto
index dc6a87f..2b4f350 100644
--- a/include/mesos/mesos.proto
+++ b/include/mesos/mesos.proto
@@ -1510,7 +1510,8 @@ message Resource {
       optional string id = 4; // EXPERIMENTAL.
 
       // Additional metadata for this source. This field maps onto CSI volume
-      // attributes and is not expected to be set by frameworks.
+      // context. Frameworks should neither alter this field, nor expect this
+      // field to remain unchanged.
       optional Labels metadata = 5; // EXPERIMENTAL.
 
       // This field serves as an indirection to a set of storage
diff --git a/include/mesos/v1/mesos.proto b/include/mesos/v1/mesos.proto
index e8086e0..bafc274 100644
--- a/include/mesos/v1/mesos.proto
+++ b/include/mesos/v1/mesos.proto
@@ -1502,7 +1502,8 @@ message Resource {
       optional string id = 4; // EXPERIMENTAL.
 
       // Additional metadata for this source. This field maps onto CSI volume
-      // attributes and is not expected to be set by frameworks.
+      // context. Frameworks should neither alter this field, nor expect this
+      // field to remain unchanged.
       optional Labels metadata = 5; // EXPERIMENTAL.
 
       // This field serves as an indirection to a set of storage
diff --git a/src/resource_provider/storage/provider.cpp b/src/resource_provider/storage/provider.cpp
index 999fe95..6d63260 100644
--- a/src/resource_provider/storage/provider.cpp
+++ b/src/resource_provider/storage/provider.cpp
@@ -57,6 +57,7 @@
 #include <stout/foreach.hpp>
 #include <stout/hashmap.hpp>
 #include <stout/hashset.hpp>
+#include <stout/lambda.hpp>
 #include <stout/linkedhashmap.hpp>
 #include <stout/nothing.hpp>
 #include <stout/os.hpp>
@@ -276,8 +277,14 @@ private:
       const Resources& checkpointed,
       const Resources& discovered);
 
-  Future<Resources> getRawVolumes();
-  Future<Resources> getStoragePools();
+  // Returns a list of resource conversions to updates volume contexts for
+  // existing volumes, remove disappeared unconverted volumes, and add newly
+  // appeared ones.
+  Future<vector<ResourceConversion>> getExistingVolumes();
+
+  // Returns a list of resource conversions to remove disappeared unconverted
+  // storage pools and add newly appeared ones.
+  Future<vector<ResourceConversion>> getStoragePools();
 
   // Spawns a loop to watch for changes in the set of known profiles and update
   // the profile mapping and storage pools accordingly.
@@ -711,21 +718,21 @@ StorageLocalResourceProviderProcess::reconcileResourceProviderState()
 {
   return reconcileOperationStatuses()
     .then(defer(self(), [=] {
-      return collect<Resources>({getRawVolumes(), getStoragePools()})
-        .then(defer(self(), [=](const vector<Resources>& discovered) {
-          ResourceConversion conversion = reconcileResources(
-              totalResources,
-              accumulate(discovered.begin(), discovered.end(), Resources()));
-
-          Try<Resources> result = totalResources.apply(conversion);
-          CHECK_SOME(result);
+      return collect<vector<ResourceConversion>>(
+          {getExistingVolumes(), getStoragePools()})
+        .then(defer(self(), [=](
+            const vector<vector<ResourceConversion>>& collected) {
+          Resources result = totalResources;
+          foreach (const vector<ResourceConversion>& conversions, collected) {
+            result = CHECK_NOTERROR(result.apply(conversions));
+          }
 
-          if (result.get() != totalResources) {
+          if (result != totalResources) {
             LOG(INFO)
-              << "Removing '" << conversion.consumed << "' and adding '"
-              << conversion.converted << "' to the total resources";
+              << "Removing '" << (totalResources - result) << "' and adding '"
+              << (result - totalResources) << "' to the total resources";
 
-            totalResources = result.get();
+            totalResources = result;
             checkpointResourceProviderState();
           }
 
@@ -919,21 +926,15 @@ Future<Nothing> StorageLocalResourceProviderProcess::reconcileStoragePools()
   };
 
   return getStoragePools()
-    .then(defer(self(), [=](const Resources& discovered) {
-      ResourceConversion conversion = reconcileResources(
-          totalResources.filter(
-              [](const Resource& r) { return !r.disk().source().has_id(); }),
-          discovered);
-
-      Try<Resources> result = totalResources.apply(conversion);
-      CHECK_SOME(result);
+    .then(defer(self(), [=](const vector<ResourceConversion>& conversions) {
+      Resources result = CHECK_NOTERROR(totalResources.apply(conversions));
 
-      if (result.get() != totalResources) {
+      if (result != totalResources) {
         LOG(INFO)
-          << "Removing '" << conversion.consumed << "' and adding '"
-          << conversion.converted << "' to the total resources";
+          << "Removing '" << (totalResources - result) << "' and adding '"
+          << (result - totalResources) << "' to the total resources";
 
-        totalResources = result.get();
+        totalResources = result;
         checkpointResourceProviderState();
 
         // NOTE: We always update the resource version before sending
@@ -1007,7 +1008,7 @@ ResourceConversion StorageLocalResourceProviderProcess::reconcileResources(
   Resources toAdd = discovered;
 
   foreach (const Resource& resource, checkpointed) {
-    Resource unconverted = createRawDiskResource(
+    Resources unconverted = createRawDiskResource(
         info,
         Bytes(resource.scalar().value() * Bytes::MEGABYTES),
         resource.disk().source().has_profile()
@@ -1024,7 +1025,7 @@ ResourceConversion StorageLocalResourceProviderProcess::reconcileResources(
       // "unconverted" version of a checkpointed resource, this is not a
       // new resource.
       toAdd -= unconverted;
-    } else if (checkpointed.contains(unconverted)) {
+    } else if (unconverted == resource) {
       // If the remaining of the discovered resources does not contain
       // the "unconverted" version of the checkpointed resource, the
       // resource is missing. However, if it remains unconverted in the
@@ -1041,68 +1042,124 @@ ResourceConversion StorageLocalResourceProviderProcess::reconcileResources(
 }
 
 
-Future<Resources> StorageLocalResourceProviderProcess::getRawVolumes()
+Future<vector<ResourceConversion>>
+StorageLocalResourceProviderProcess::getExistingVolumes()
 {
   CHECK(info.has_id());
 
   return volumeManager->listVolumes()
     .then(defer(self(), [=](const vector<VolumeInfo>& volumeInfos) {
-      Resources resources;
-
-      // Recover disk profiles from the checkpointed state.
-      hashmap<string, string> volumesToProfiles;
+      // Since we only support "exclusive" (MOUNT or BLOCK) disks, there should
+      // be only one checkpointed resource for each volume ID.
+      hashmap<string, Resource> checkpointedMap;
       foreach (const Resource& resource, totalResources) {
-        if (resource.disk().source().has_id() &&
-            resource.disk().source().has_profile()) {
-          volumesToProfiles.put(
-              resource.disk().source().id(),
-              resource.disk().source().profile());
+        if (resource.disk().source().has_id()) {
+          CHECK(!checkpointedMap.contains(resource.disk().source().id()));
+          checkpointedMap.put(resource.disk().source().id(), resource);
         }
       }
 
+      // The "discovered" resources consist of RAW disk resources, one for each
+      // volume reported by the CSI plugin.
+      Resources discovered;
+
+      // If any volume context has been changed by a non-conforming CSI plugin,
+      // we need to construct a resources conversion to reflect the
+      // corresponding metadata changes, so we maintain the resources to be
+      // removed and those to be added here.
+      Resources metadataToRemove;
+      Resources metadataToAdd;
+
       foreach (const VolumeInfo& volumeInfo, volumeInfos) {
-        resources += createRawDiskResource(
+        Option<string> profile;
+        Option<Labels> metadata = volumeInfo.context.empty()
+          ? Option<Labels>::none()
+          : convertStringMapToLabels(volumeInfo.context);
+
+        if (checkpointedMap.contains(volumeInfo.id)) {
+          const Resource& resource = checkpointedMap.at(volumeInfo.id);
+
+          if (resource.disk().source().has_profile()) {
+            profile = resource.disk().source().profile();
+          }
+
+          // If the volume context has been changed by a non-conforming CSI
+          // plugin, the changes will be reflected in a resource conversion.
+          if (resource.disk().source().metadata() !=
+              metadata.getOrElse(Labels())) {
+            metadataToRemove += resource;
+
+            Resource changed = resource;
+            if (metadata.isSome()) {
+              *changed.mutable_disk()->mutable_source()->mutable_metadata() =
+                *metadata;
+            } else {
+              changed.mutable_disk()->mutable_source()->clear_metadata();
+            }
+
+            metadataToAdd += changed;
+          }
+        }
+
+        discovered += createRawDiskResource(
             info,
             volumeInfo.capacity,
-            volumesToProfiles.contains(volumeInfo.id)
-              ? volumesToProfiles.at(volumeInfo.id)
-              : Option<string>::none(),
+            profile,
             vendor,
             volumeInfo.id,
-            volumeInfo.context.empty()
-              ? Option<Labels>::none()
-              : convertStringMapToLabels(volumeInfo.context));
+            metadata);
       }
 
-      return resources;
+      ResourceConversion metadataConversion(
+          std::move(metadataToRemove), std::move(metadataToAdd));
+
+      Resources checkpointed = CHECK_NOTERROR(
+          totalResources.filter([](const Resource& resource) {
+            return resource.disk().source().has_id();
+          }).apply(metadataConversion));
+
+      return vector<ResourceConversion>{
+        std::move(metadataConversion),
+        reconcileResources(std::move(checkpointed), std::move(discovered))};
     }));
 }
 
 
-Future<Resources> StorageLocalResourceProviderProcess::getStoragePools()
+Future<vector<ResourceConversion>>
+StorageLocalResourceProviderProcess::getStoragePools()
 {
   CHECK(info.has_id());
 
-  vector<Future<Resources>> futures;
+  vector<Future<Resource>> futures;
 
   foreachpair (const string& profile,
                const DiskProfileAdaptor::ProfileInfo& profileInfo,
                profileInfos) {
-    futures.push_back(volumeManager->getCapacity(
-        profileInfo.capability, profileInfo.parameters)
-      .then(defer(self(), [=](const Bytes& capacity) -> Resources {
-        if (capacity == 0) {
-          return Resources();
-        }
-
-        return createRawDiskResource(info, capacity, profile, vendor);
-      })));
+    futures.push_back(
+        volumeManager->getCapacity(
+            profileInfo.capability, profileInfo.parameters)
+          .then(std::bind(
+              &createRawDiskResource,
+              info,
+              lambda::_1,
+              profile,
+              vendor,
+              None(),
+              None())));
   }
 
   return collect(futures)
-    .then([](const vector<Resources>& resources) {
-      return accumulate(resources.begin(), resources.end(), Resources());
-    });
+    .then(defer(self(), [=](const vector<Resource>& resources) {
+      Resources discovered(resources); // Zero resources will be ignored.
+      Resources checkpointed =
+        totalResources.filter([](const Resource& resource) {
+          return Resources::isDisk(resource, Resource::DiskInfo::Source::RAW) &&
+            !resource.disk().source().has_id();
+        });
+
+      return vector<ResourceConversion>{
+        reconcileResources(std::move(checkpointed), std::move(discovered))};
+    }));
 }
 
 


[mesos] 04/09: Added a unit test to verify if SLRP allows changes in volume context.

Posted by ch...@apache.org.
This is an automated email from the ASF dual-hosted git repository.

chhsiao pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/mesos.git

commit a7b98f702ddf4e8c9cfd9a7e14e050d802f80084
Author: Chun-Hung Hsiao <ch...@mesosphere.io>
AuthorDate: Mon May 6 11:44:40 2019 -0700

    Added a unit test to verify if SLRP allows changes in volume context.
    
    Review: https://reviews.apache.org/r/70622
---
 src/examples/test_csi_plugin.cpp                   |  43 ++++--
 .../storage_local_resource_provider_tests.cpp      | 155 +++++++++++++++++++--
 2 files changed, 172 insertions(+), 26 deletions(-)

diff --git a/src/examples/test_csi_plugin.cpp b/src/examples/test_csi_plugin.cpp
index 7ff08b8..6202173 100644
--- a/src/examples/test_csi_plugin.cpp
+++ b/src/examples/test_csi_plugin.cpp
@@ -146,6 +146,12 @@ public:
         "specified as a semicolon-delimited list of param=value pairs.\n"
         "(Example: 'param1=value1;param2=value2')");
 
+    add(&Flags::volume_metadata,
+        "volume_metadata",
+        "The static properties to add to the contextual information of each\n"
+        "volume. The metadata are specified as a semicolon-delimited list of\n"
+        "prop=value pairs. (Example: 'prop1=value1;prop2=value2')");
+
     add(&Flags::volumes,
         "volumes",
         "Creates preprovisioned volumes upon start-up. The volumes are\n"
@@ -164,6 +170,7 @@ public:
   string work_dir;
   Bytes available_capacity;
   Option<string> create_parameters;
+  Option<string> volume_metadata;
   Option<string> volumes;
   Option<string> forward;
 };
@@ -184,12 +191,14 @@ public:
       const string& _workDir,
       const Bytes& _availableCapacity,
       const hashmap<string, string>& _createParameters,
+      const hashmap<string, string>& _volumeMetadata,
       const hashmap<string, Bytes>& _volumes)
     : apiVersion(_apiVersion),
       endpoint(_endpoint),
       workDir(_workDir),
       availableCapacity(_availableCapacity),
-      createParameters(_createParameters.begin(), _createParameters.end())
+      createParameters(_createParameters.begin(), _createParameters.end()),
+      volumeMetadata(_volumeMetadata.begin(), _volumeMetadata.end())
   {
     // Construct the default mount volume capability.
     defaultVolumeCapability.mutable_mount();
@@ -221,7 +230,7 @@ public:
       }
 
       VolumeInfo volumeInfo{
-        capacity, getVolumePath(capacity, name), Map<string, string>()};
+        capacity, getVolumePath(capacity, name), volumeMetadata};
 
       Try<Nothing> mkdir = os::mkdir(volumeInfo.id);
       CHECK_SOME(mkdir)
@@ -483,6 +492,7 @@ private:
   Bytes availableCapacity;
   VolumeCapability defaultVolumeCapability;
   Map<string, string> createParameters;
+  Map<string, string> volumeMetadata;
   hashmap<string, VolumeInfo> volumes;
 };
 
@@ -1289,7 +1299,7 @@ Try<VolumeInfo> TestCSIPlugin::parseVolumePath(const string& dir)
     << "Cannot reconstruct volume path '" << dir << "' from volume name '"
     << name.get() << "' and capacity " << capacity.get();
 
-  return VolumeInfo{capacity.get(), dir, Map<string, string>()};
+  return VolumeInfo{capacity.get(), dir, volumeMetadata};
 }
 
 
@@ -1352,7 +1362,7 @@ Try<VolumeInfo, StatusError> TestCSIPlugin::createVolume(
 
     VolumeInfo volumeInfo{min(max(defaultSize, requiredBytes), limitBytes),
                           getVolumePath(volumeInfo.capacity, name),
-                          Map<string, string>()};
+                          volumeMetadata};
 
     Try<Nothing> mkdir = os::mkdir(volumeInfo.id);
     if (mkdir.isError()) {
@@ -1995,19 +2005,27 @@ int main(int argc, char** argv)
     foreachpair (const string& param,
                  const vector<string>& values,
                  strings::pairs(flags.create_parameters.get(), ";", "=")) {
-      Option<Error> error;
-
       if (values.size() != 1) {
-        error = "Parameter keys must be unique";
-      } else {
-        createParameters.put(param, values[0]);
+        cerr << "Parameter key '" << param << "' is not unique" << endl;
+        return EXIT_FAILURE;
       }
 
-      if (error.isSome()) {
-        cerr << "Failed to parse the '--create_parameters' flags: "
-             << error->message << endl;
+      createParameters.put(param, values[0]);
+    }
+  }
+
+  hashmap<string, string> volumeMetadata;
+
+  if (flags.volume_metadata.isSome()) {
+    foreachpair (const string& prop,
+                 const vector<string>& values,
+                 strings::pairs(flags.volume_metadata.get(), ";", "=")) {
+      if (values.size() != 1) {
+        cerr << "Metadata key '" << prop << "' is not unique" << endl;
         return EXIT_FAILURE;
       }
+
+      volumeMetadata.put(prop, values[0]);
     }
   }
 
@@ -2060,6 +2078,7 @@ int main(int argc, char** argv)
         flags.work_dir,
         flags.available_capacity,
         createParameters,
+        volumeMetadata,
         volumes);
 
     plugin.run();
diff --git a/src/tests/storage_local_resource_provider_tests.cpp b/src/tests/storage_local_resource_provider_tests.cpp
index 7dd08be..3823305 100644
--- a/src/tests/storage_local_resource_provider_tests.cpp
+++ b/src/tests/storage_local_resource_provider_tests.cpp
@@ -22,6 +22,8 @@
 #include <tuple>
 #include <vector>
 
+#include <google/protobuf/repeated_field.h>
+
 #include <mesos/csi/v0.hpp>
 #include <mesos/csi/v1.hpp>
 
@@ -79,6 +81,8 @@ using std::shared_ptr;
 using std::string;
 using std::vector;
 
+using google::protobuf::RepeatedPtrField;
+
 using mesos::internal::slave::ContainerDaemonProcess;
 
 using mesos::master::detector::MasterDetector;
@@ -234,8 +238,9 @@ public:
   void setupResourceProviderConfig(
       const Bytes& capacity,
       const Option<string> volumes = None(),
+      const Option<string> forward = None(),
       const Option<string> createParameters = None(),
-      const Option<string> forward = None())
+      const Option<string> volumeMetadata = None())
   {
     const string testCsiPluginPath =
       path::join(tests::flags.build_dir, "src", "test-csi-plugin");
@@ -269,9 +274,10 @@ public:
                       "--api_version=%s",
                       "--work_dir=%s",
                       "--available_capacity=%s",
+                      "--volumes=%s",
                       "%s",
-                      "%s",
-                      "%s"
+                      "--create_parameters=%s",
+                      "--volume_metadata=%s"
                     ]
                   },
                   "resources": [
@@ -305,10 +311,10 @@ public:
         GetParam(),
         testCsiPluginWorkDir.get(),
         stringify(capacity),
-        createParameters.isSome()
-          ? "--create_parameters=" + createParameters.get() : "",
-        volumes.isSome() ? "--volumes=" + volumes.get() : "",
-        forward.isSome() ? "--forward=" + forward.get() : "");
+        volumes.getOrElse(""),
+        forward.isSome() ? "--forward=" + forward.get() : "",
+        createParameters.getOrElse(""),
+        volumeMetadata.getOrElse(""));
 
     ASSERT_SOME(resourceProviderConfig);
 
@@ -1236,6 +1242,127 @@ TEST_P(StorageLocalResourceProviderTest, CreateDestroyDiskWithRecovery)
 }
 
 
+// This test verifies that the storage local resource provider can properly
+// handle changes in volume metadata. This is a regression test for MESOS-9395.
+TEST_P(StorageLocalResourceProviderTest, RecoverDiskWithChangedMetadata)
+{
+  const string profilesPath = path::join(sandbox.get(), "profiles.json");
+
+  ASSERT_SOME(
+      os::write(profilesPath, createDiskProfileMapping({{"test", None()}})));
+
+  loadUriDiskProfileAdaptorModule(profilesPath);
+
+  // Add metadata "label=foo" to each created volume.
+  setupResourceProviderConfig(
+      Gigabytes(4), None(), None(), None(), "label=foo");
+
+  Try<Owned<cluster::Master>> master = StartMaster();
+  ASSERT_SOME(master);
+
+  Owned<MasterDetector> detector = master.get()->createDetector();
+
+  slave::Flags slaveFlags = CreateSlaveFlags();
+  slaveFlags.disk_profile_adaptor = URI_DISK_PROFILE_ADAPTOR_NAME;
+
+  Try<Owned<cluster::Slave>> slave = StartSlave(detector.get(), slaveFlags);
+  ASSERT_SOME(slave);
+
+  // Register a framework to exercise operations.
+  FrameworkInfo framework = DEFAULT_FRAMEWORK_INFO;
+  framework.set_roles(0, "storage/role");
+
+  MockScheduler sched;
+  MesosSchedulerDriver driver(
+      &sched, framework, master.get()->pid, DEFAULT_CREDENTIAL);
+
+  EXPECT_CALL(sched, registered(&driver, _, _));
+
+  // We use the following filter to filter offers that do not have wanted
+  // resources for 365 days (the maximum).
+  Filters declineFilters;
+  declineFilters.set_refuse_seconds(Days(365).secs());
+
+  // Decline unwanted offers. The master can send such offers before the
+  // resource provider receives profile updates.
+  EXPECT_CALL(sched, resourceOffers(&driver, _))
+    .WillRepeatedly(DeclineOffers(declineFilters));
+
+  Future<vector<Offer>> offers;
+  EXPECT_CALL(sched, resourceOffers(&driver, OffersHaveAnyResource(
+      &Resources::hasResourceProvider)))
+    .WillOnce(FutureArg<1>(&offers));
+
+  driver.start();
+
+  AWAIT_READY(offers);
+  ASSERT_EQ(1u, offers->size());
+
+  Offer offer = offers->at(0);
+
+  // Create a MOUNT disk.
+  RepeatedPtrField<Resource> raw =
+    Resources(offer.resources()).filter(&Resources::hasResourceProvider);
+
+  ASSERT_EQ(1, raw.size());
+  ASSERT_TRUE(isStoragePool(raw[0], "test"));
+
+  EXPECT_CALL(sched, resourceOffers(&driver, OffersHaveAnyResource(
+      &Resources::hasResourceProvider)))
+    .WillOnce(FutureArg<1>(&offers));
+
+  driver.acceptOffers(
+      {offer.id()},
+      {CREATE_DISK(raw[0], Resource::DiskInfo::Source::MOUNT)});
+
+  AWAIT_READY(offers);
+  ASSERT_EQ(1u, offers->size());
+
+  offer = offers->at(0);
+
+  RepeatedPtrField<Resource> created =
+    Resources(offer.resources()).filter(&Resources::hasResourceProvider);
+
+  ASSERT_EQ(1, created.size());
+  ASSERT_TRUE(isMountDisk(created[0], "test"));
+  EXPECT_TRUE(created[0].disk().source().has_metadata());
+
+  // Restart the agent.
+  EXPECT_CALL(sched, offerRescinded(_, _));
+
+  slave.get()->terminate();
+
+  // Add metadata "label=bar" to each created volume. This dose not conform to
+  // the CSI specification in terms of backward compatibility, but Mesos should
+  // be robust to handle changes in the metadata.
+  setupResourceProviderConfig(
+      Gigabytes(4), None(), None(), None(), "label=bar");
+
+  EXPECT_CALL(sched, resourceOffers(&driver, OffersHaveAnyResource(
+      Resources::hasResourceProvider)))
+    .WillOnce(FutureArg<1>(&offers));
+
+  slave = StartSlave(detector.get(), slaveFlags);
+  ASSERT_SOME(slave);
+
+  AWAIT_READY(offers);
+  ASSERT_EQ(1u, offers->size());
+
+  offer = offers->at(0);
+
+  RepeatedPtrField<Resource> recovered =
+    Resources(offer.resources()).filter(&Resources::hasResourceProvider);
+
+  ASSERT_EQ(1, recovered.size()) << "To many resources: " << recovered;
+  ASSERT_TRUE(isMountDisk(recovered[0], "test"));
+  ASSERT_TRUE(recovered[0].disk().source().has_metadata());
+  EXPECT_NE(created[0].disk().source().metadata(),
+            recovered[0].disk().source().metadata())
+    << "'" << JSON::protobuf(created[0].disk().source().metadata()) << "' vs "
+       "'" << JSON::protobuf(recovered[0].disk().source().metadata()) << "'";
+}
+
+
 // This test verifies that a framework cannot create a volume during and after
 // the profile disappears, and destroying a volume with a stale profile will
 // recover the freed disk with another appeared profile.
@@ -1644,7 +1771,7 @@ TEST_P(StorageLocalResourceProviderTest, ROOT_AgentRegisteredWithNewId)
 
   loadUriDiskProfileAdaptorModule(profilesPath);
 
-  setupResourceProviderConfig(Gigabytes(5), None(), "label=foo");
+  setupResourceProviderConfig(Gigabytes(5), None(), None(), "label=foo");
 
   Try<Owned<cluster::Master>> master = StartMaster();
   ASSERT_SOME(master);
@@ -1809,7 +1936,7 @@ TEST_P(StorageLocalResourceProviderTest, ROOT_AgentRegisteredWithNewId)
   // allocation for the persistent volume.
   //
   // TODO(chhsiao): Remove this workaround once MESOS-9553 is done.
-  setupResourceProviderConfig(Gigabytes(6), None(), "label=foo");
+  setupResourceProviderConfig(Gigabytes(6), None(), None(), "label=foo");
 
   // NOTE: The order of these expectations is reversed because Google Mock will
   // search the expectations in reverse order.
@@ -2992,7 +3119,7 @@ TEST_P(StorageLocalResourceProviderTest, CreatePersistentBlockVolume)
   MockCSIPlugin plugin;
   ASSERT_SOME(plugin.startup(mockCsiEndpoint));
 
-  setupResourceProviderConfig(Bytes(0), None(), None(), mockCsiEndpoint);
+  setupResourceProviderConfig(Bytes(0), None(), mockCsiEndpoint);
 
   Try<Owned<cluster::Master>> master = StartMaster();
   ASSERT_SOME(master);
@@ -3115,7 +3242,7 @@ TEST_P(StorageLocalResourceProviderTest, DestroyUnpublishedPersistentVolume)
   MockCSIPlugin plugin;
   ASSERT_SOME(plugin.startup(mockCsiEndpoint));
 
-  setupResourceProviderConfig(Bytes(0), None(), None(), mockCsiEndpoint);
+  setupResourceProviderConfig(Bytes(0), None(), mockCsiEndpoint);
 
   Try<Owned<cluster::Master>> master = StartMaster();
   ASSERT_SOME(master);
@@ -3279,7 +3406,7 @@ TEST_P(
   MockCSIPlugin plugin;
   ASSERT_SOME(plugin.startup(mockCsiEndpoint));
 
-  setupResourceProviderConfig(Bytes(0), None(), None(), mockCsiEndpoint);
+  setupResourceProviderConfig(Bytes(0), None(), mockCsiEndpoint);
 
   Try<Owned<cluster::Master>> master = StartMaster();
   ASSERT_SOME(master);
@@ -3495,7 +3622,7 @@ TEST_P(
   MockCSIPlugin plugin;
   ASSERT_SOME(plugin.startup(mockCsiEndpoint));
 
-  setupResourceProviderConfig(Bytes(0), None(), None(), mockCsiEndpoint);
+  setupResourceProviderConfig(Bytes(0), None(), mockCsiEndpoint);
 
   Try<Owned<cluster::Master>> master = StartMaster();
   ASSERT_SOME(master);
@@ -5623,7 +5750,7 @@ TEST_P(StorageLocalResourceProviderTest, RetryRpcWithExponentialBackoff)
   MockCSIPlugin plugin;
   ASSERT_SOME(plugin.startup(mockCsiEndpoint));
 
-  setupResourceProviderConfig(Bytes(0), None(), None(), mockCsiEndpoint);
+  setupResourceProviderConfig(Bytes(0), None(), mockCsiEndpoint);
 
   master::Flags masterFlags = CreateMasterFlags();
 


[mesos] 03/09: Used full paths as volume IDs for the test CSI plugin.

Posted by ch...@apache.org.
This is an automated email from the ASF dual-hosted git repository.

chhsiao pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/mesos.git

commit 24c70aa15c566b6f15a3f3636664507bae3af3b6
Author: Chun-Hung Hsiao <ch...@mesosphere.io>
AuthorDate: Thu Apr 18 12:50:54 2019 -0700

    Used full paths as volume IDs for the test CSI plugin.
    
    The full paths of simulated volumes are now in their ID instead of their
    contextual information. This simplifies SLRP tests, and makes it cleaner
    if we want to customize the contextual information in the future.
    
    Review: https://reviews.apache.org/r/70621
---
 src/examples/test_csi_plugin.cpp                   | 245 ++++++++++++---------
 .../storage_local_resource_provider_tests.cpp      | 170 +++-----------
 2 files changed, 172 insertions(+), 243 deletions(-)

diff --git a/src/examples/test_csi_plugin.cpp b/src/examples/test_csi_plugin.cpp
index 03f782e..7ff08b8 100644
--- a/src/examples/test_csi_plugin.cpp
+++ b/src/examples/test_csi_plugin.cpp
@@ -42,6 +42,7 @@
 #include <stout/bytes.hpp>
 #include <stout/flags.hpp>
 #include <stout/foreach.hpp>
+#include <stout/fs.hpp>
 #include <stout/hashmap.hpp>
 #include <stout/none.hpp>
 #include <stout/option.hpp>
@@ -59,13 +60,14 @@
 
 #include "csi/v0_utils.hpp"
 #include "csi/v1_utils.hpp"
+#include "csi/volume_manager.hpp"
 
 #include "linux/fs.hpp"
 
 #include "logging/logging.hpp"
 
 namespace http = process::http;
-namespace fs = mesos::internal::fs;
+namespace internal = mesos::internal;
 
 using std::cerr;
 using std::cout;
@@ -95,6 +97,8 @@ using grpc::ServerContext;
 using grpc::Status;
 using grpc::WriteOptions;
 
+using mesos::csi::VolumeInfo;
+
 using mesos::csi::types::VolumeCapability;
 
 using process::grpc::StatusError;
@@ -192,41 +196,46 @@ public:
     defaultVolumeCapability.mutable_access_mode()
       ->set_mode(VolumeCapability::AccessMode::SINGLE_NODE_WRITER);
 
-    // Scan for preprovisioned volumes.
+    Bytes usedCapacity(0);
+
+    // Scan for created volumes.
     //
     // TODO(jieyu): Consider not using CHECKs here.
-    Try<list<string>> paths = os::ls(workDir);
-    CHECK_SOME(paths);
-
-    foreach (const string& path, paths.get()) {
-      Try<VolumeInfo> volumeInfo = parseVolumePath(path);
-      CHECK_SOME(volumeInfo);
-
-      CHECK(!volumes.contains(volumeInfo->id));
-      volumes.put(volumeInfo->id, volumeInfo.get());
-
-      if (!_volumes.contains(volumeInfo->id)) {
-        CHECK_GE(availableCapacity, volumeInfo->size);
-        availableCapacity -= volumeInfo->size;
-      }
+    Try<list<string>> paths = fs::list(path::join(workDir, "*-*"));
+    foreach (const string& path, CHECK_NOTERROR(paths)) {
+      volumes.put(path, CHECK_NOTERROR(parseVolumePath(path)));
+      usedCapacity += volumes.at(path).capacity;
     }
 
+    // Create preprovisioned volumes if they have not existed yet.
     foreachpair (const string& name, const Bytes& capacity, _volumes) {
-      if (volumes.contains(name)) {
+      Option<VolumeInfo> found = findVolumeByName(name);
+
+      if (found.isSome()) {
+        CHECK_EQ(found->capacity, capacity)
+          << "Expected preprovisioned volume '" << name << "' to be "
+          << capacity << " but found " << found->capacity << " instead";
+
+        usedCapacity -= found->capacity;
         continue;
       }
 
-      VolumeInfo volumeInfo;
-      volumeInfo.id = name;
-      volumeInfo.size = capacity;
-
-      const string path = getVolumePath(volumeInfo);
+      VolumeInfo volumeInfo{
+        capacity, getVolumePath(capacity, name), Map<string, string>()};
 
-      Try<Nothing> mkdir = os::mkdir(path);
-      CHECK_SOME(mkdir);
+      Try<Nothing> mkdir = os::mkdir(volumeInfo.id);
+      CHECK_SOME(mkdir)
+        << "Failed to create directory for preprovisioned volume '" << name
+        << "': " << mkdir.error();
 
-      volumes.put(volumeInfo.id, volumeInfo);
+      volumes.put(volumeInfo.id, std::move(volumeInfo));
     }
+
+    CHECK_GE(availableCapacity, usedCapacity)
+      << "Insufficient available capacity for volumes, expected to be at least "
+      << usedCapacity;
+
+    availableCapacity -= usedCapacity;
   }
 
   void run();
@@ -406,14 +415,9 @@ public:
       csi::v1::NodeGetInfoResponse* response) override;
 
 private:
-  struct VolumeInfo
-  {
-    string id;
-    Bytes size;
-  };
-
-  string getVolumePath(const VolumeInfo& volumeInfo);
-  Try<VolumeInfo> parseVolumePath(const string& path);
+  string getVolumePath(const Bytes& capacity, const string& name);
+  Try<VolumeInfo> parseVolumePath(const string& dir);
+  Option<VolumeInfo> findVolumeByName(const string& name);
 
   Try<VolumeInfo, StatusError> createVolume(
       const string& name,
@@ -568,9 +572,8 @@ Status TestCSIPlugin::CreateVolume(
   }
 
   response->mutable_volume()->set_id(result->id);
-  response->mutable_volume()->set_capacity_bytes(result->size.bytes());
-  (*response->mutable_volume()->mutable_attributes())["path"] =
-    getVolumePath(result.get());
+  response->mutable_volume()->set_capacity_bytes(result->capacity.bytes());
+  *response->mutable_volume()->mutable_attributes() = result->context;
 
   return Status::OK;
 }
@@ -688,8 +691,8 @@ Status TestCSIPlugin::ListVolumes(
   foreach (const VolumeInfo& volumeInfo, result.get()) {
     csi::v0::Volume* volume = response->add_entries()->mutable_volume();
     volume->set_id(volumeInfo.id);
-    volume->set_capacity_bytes(volumeInfo.size.bytes());
-    (*volume->mutable_attributes())["path"] = getVolumePath(volumeInfo);
+    volume->set_capacity_bytes(volumeInfo.capacity.bytes());
+    *volume->mutable_attributes() = volumeInfo.context;
   }
 
   return Status::OK;
@@ -925,9 +928,8 @@ Status TestCSIPlugin::CreateVolume(
   }
 
   response->mutable_volume()->set_volume_id(result->id);
-  response->mutable_volume()->set_capacity_bytes(result->size.bytes());
-  (*response->mutable_volume()->mutable_volume_context())["path"] =
-    getVolumePath(result.get());
+  response->mutable_volume()->set_capacity_bytes(result->capacity.bytes());
+  *response->mutable_volume()->mutable_volume_context() = result->context;
 
   return Status::OK;
 }
@@ -1050,8 +1052,8 @@ Status TestCSIPlugin::ListVolumes(
   foreach (const VolumeInfo& volumeInfo, result.get()) {
     csi::v1::Volume* volume = response->add_entries()->mutable_volume();
     volume->set_volume_id(volumeInfo.id);
-    volume->set_capacity_bytes(volumeInfo.size.bytes());
-    (*volume->mutable_volume_context())["path"] = getVolumePath(volumeInfo);
+    volume->set_capacity_bytes(volumeInfo.capacity.bytes());
+    *volume->mutable_volume_context() = volumeInfo.context;
   }
 
   return Status::OK;
@@ -1079,11 +1081,9 @@ Status TestCSIPlugin::GetCapacity(
 }
 
 
-string TestCSIPlugin::getVolumePath(const VolumeInfo& volumeInfo)
+string TestCSIPlugin::getVolumePath(const Bytes& capacity, const string& name)
 {
-  return path::join(
-      workDir,
-      strings::join("-", stringify(volumeInfo.size), volumeInfo.id));
+  return path::join(workDir, strings::join("-", capacity, http::encode(name)));
 }
 
 
@@ -1250,31 +1250,62 @@ Status TestCSIPlugin::NodeGetInfo(
 }
 
 
-Try<TestCSIPlugin::VolumeInfo> TestCSIPlugin::parseVolumePath(
-    const string& path)
+Try<VolumeInfo> TestCSIPlugin::parseVolumePath(const string& dir)
 {
-  size_t pos = path.find_first_of("-");
-  if (pos == string::npos) {
-    return Error("Cannot find the delimiter");
+  // TODO(chhsiao): Consider using `<regex>`, which requires GCC 4.9+.
+
+  // Make sure there's a separator at the end of the prefix so that we
+  // don't accidentally slice off part of a directory.
+  const string prefix = path::join(workDir, "");
+
+  if (!strings::startsWith(dir, prefix)) {
+    return Error(
+        "Directory '" + dir + "' does not fall under work directory '" +
+        prefix + "'");
+  }
+
+  const string basename = Path(dir).basename();
+
+  vector<string> tokens = strings::tokenize(basename, "-", 2);
+  if (tokens.size() != 2) {
+    return Error("Cannot find delimiter '-' in '" + basename + "'");
   }
 
-  string bytesString = path.substr(0, path.find_first_of("-"));
-  string id = path.substr(path.find_first_of("-") + 1);
+  Try<Bytes> capacity = Bytes::parse(tokens[0]);
+  if (capacity.isError()) {
+    return Error(
+        "Failed to parse capacity from '" + tokens[0] +
+        "': " + capacity.error());
+  }
 
-  Try<Bytes> bytes = Bytes::parse(bytesString);
-  if (bytes.isError()) {
-    return Error("Failed to parse bytes: " + bytes.error());
+  Try<string> name = http::decode(tokens[1]);
+  if (name.isError()) {
+    return Error(
+        "Failed to decode volume name from '" + tokens[1] +
+        "': " + name.error());
   }
 
-  VolumeInfo volumeInfo;
-  volumeInfo.id = id;
-  volumeInfo.size = bytes.get();
+  CHECK_EQ(dir, getVolumePath(capacity.get(), name.get()))
+    << "Cannot reconstruct volume path '" << dir << "' from volume name '"
+    << name.get() << "' and capacity " << capacity.get();
 
-  return volumeInfo;
+  return VolumeInfo{capacity.get(), dir, Map<string, string>()};
 }
 
 
-Try<TestCSIPlugin::VolumeInfo, StatusError> TestCSIPlugin::createVolume(
+Option<VolumeInfo> TestCSIPlugin::findVolumeByName(const string& name)
+{
+  foreachvalue (const VolumeInfo& volumeInfo, volumes) {
+    if (volumeInfo.id == getVolumePath(volumeInfo.capacity, name)) {
+      return volumeInfo;
+    }
+  }
+
+  return None();
+}
+
+
+Try<VolumeInfo, StatusError> TestCSIPlugin::createVolume(
     const string& name,
     const Bytes& requiredBytes,
     const Bytes& limitBytes,
@@ -1296,43 +1327,42 @@ Try<TestCSIPlugin::VolumeInfo, StatusError> TestCSIPlugin::createVolume(
         grpc::INVALID_ARGUMENT, "Unsupported create parameters"));
   }
 
-  if (volumes.contains(volumeId)) {
-    const VolumeInfo& volumeInfo = volumes.at(volumeId);
+  Option<VolumeInfo> found = findVolumeByName(name);
 
-    if (volumeInfo.size > limitBytes) {
+  if (found.isSome()) {
+    if (found->capacity > limitBytes) {
       return StatusError(Status(
           grpc::ALREADY_EXISTS, "Cannot satisfy limit bytes"));
     }
 
-    if (volumeInfo.size < requiredBytes) {
+    if (found->capacity < requiredBytes) {
       return StatusError(Status(
           grpc::ALREADY_EXISTS, "Cannot satisfy required bytes"));
     }
 
-    return volumeInfo;
+    return *found;
   } else {
     if (availableCapacity < requiredBytes) {
       return StatusError(Status(grpc::OUT_OF_RANGE, "Insufficient capacity"));
     }
 
-    VolumeInfo volumeInfo;
-    volumeInfo.id = volumeId;
 
     // We assume that `requiredBytes <= limitBytes` has been verified.
     const Bytes defaultSize = min(availableCapacity, DEFAULT_VOLUME_CAPACITY);
-    volumeInfo.size = min(max(defaultSize, requiredBytes), limitBytes);
 
-    const string path = getVolumePath(volumeInfo);
+    VolumeInfo volumeInfo{min(max(defaultSize, requiredBytes), limitBytes),
+                          getVolumePath(volumeInfo.capacity, name),
+                          Map<string, string>()};
 
-    Try<Nothing> mkdir = os::mkdir(path);
+    Try<Nothing> mkdir = os::mkdir(volumeInfo.id);
     if (mkdir.isError()) {
       return StatusError(Status(
           grpc::INTERNAL,
           "Failed to create volume '" + volumeInfo.id + "': " + mkdir.error()));
     }
 
-    CHECK_GE(availableCapacity, volumeInfo.size);
-    availableCapacity -= volumeInfo.size;
+    CHECK_GE(availableCapacity, volumeInfo.capacity);
+    availableCapacity -= volumeInfo.capacity;
     volumes.put(volumeInfo.id, volumeInfo);
 
     return volumeInfo;
@@ -1350,16 +1380,15 @@ Try<Nothing, StatusError> TestCSIPlugin::deleteVolume(const string& volumeId)
   }
 
   const VolumeInfo& volumeInfo = volumes.at(volumeId);
-  const string path = getVolumePath(volumeInfo);
 
-  Try<Nothing> rmdir = os::rmdir(path);
+  Try<Nothing> rmdir = os::rmdir(volumeInfo.id);
   if (rmdir.isError()) {
     return StatusError(Status(
         grpc::INTERNAL,
         "Failed to delete volume '" + volumeId + "': " + rmdir.error()));
   }
 
-  availableCapacity += volumeInfo.size;
+  availableCapacity += volumeInfo.capacity;
   volumes.erase(volumeInfo.id);
 
   return Nothing();
@@ -1394,9 +1423,8 @@ Try<Nothing, StatusError> TestCSIPlugin::controllerPublishVolume(
   }
 
   const VolumeInfo& volumeInfo = volumes.at(volumeId);
-  const string path = getVolumePath(volumeInfo);
 
-  if (!volumeContext.count("path") || volumeContext.at("path") != path) {
+  if (volumeContext != volumeInfo.context) {
     return StatusError(Status(
         grpc::INVALID_ARGUMENT, "Invalid volume context"));
   }
@@ -1436,9 +1464,8 @@ Try<Option<Error>, StatusError> TestCSIPlugin::validateVolumeCapabilities(
   }
 
   const VolumeInfo& volumeInfo = volumes.at(volumeId);
-  const string path = getVolumePath(volumeInfo);
 
-  if (!volumeContext.count("path") || volumeContext.at("path") != path) {
+  if (volumeContext != volumeInfo.context) {
     return StatusError(Status(
         grpc::INVALID_ARGUMENT, "Invalid volume context"));
   }
@@ -1457,9 +1484,8 @@ Try<Option<Error>, StatusError> TestCSIPlugin::validateVolumeCapabilities(
 }
 
 
-Try<vector<TestCSIPlugin::VolumeInfo>, StatusError> TestCSIPlugin::listVolumes(
-    const Option<int32_t>& maxEntries,
-    const Option<string>& startingToken)
+Try<vector<VolumeInfo>, StatusError> TestCSIPlugin::listVolumes(
+    const Option<int32_t>& maxEntries, const Option<string>& startingToken)
 {
   // TODO(chhsiao): Support max entries.
   if (maxEntries.isSome()) {
@@ -1527,14 +1553,15 @@ Try<Nothing, StatusError> TestCSIPlugin::nodeStageVolume(
   }
 
   const VolumeInfo& volumeInfo = volumes.at(volumeId);
-  const string path = getVolumePath(volumeInfo);
 
-  if (!volumeContext.count("path") || volumeContext.at("path") != path) {
+  if (volumeContext != volumeInfo.context) {
     return StatusError(Status(
         grpc::INVALID_ARGUMENT, "Invalid volume context"));
   }
 
-  Try<fs::MountInfoTable> table = fs::MountInfoTable::read();
+  Try<internal::fs::MountInfoTable> table =
+    internal::fs::MountInfoTable::read();
+
   if (table.isError()) {
     return StatusError(Status(
         grpc::INTERNAL, "Failed to get mount table: " + table.error()));
@@ -1543,17 +1570,19 @@ Try<Nothing, StatusError> TestCSIPlugin::nodeStageVolume(
   if (std::any_of(
           table->entries.begin(),
           table->entries.end(),
-          [&](const fs::MountInfoTable::Entry& entry) {
+          [&](const internal::fs::MountInfoTable::Entry& entry) {
             return entry.target == stagingPath;
           })) {
     return Nothing();
   }
 
-  Try<Nothing> mount = fs::mount(path, stagingPath, None(), MS_BIND, None());
+  Try<Nothing> mount =
+    internal::fs::mount(volumeInfo.id, stagingPath, None(), MS_BIND, None());
+
   if (mount.isError()) {
     return StatusError(Status(
         grpc::INTERNAL,
-        "Failed to mount from '" + path + "' to '" + stagingPath +
+        "Failed to mount from '" + volumeInfo.id + "' to '" + stagingPath +
           "': " + mount.error()));
   }
 
@@ -1569,7 +1598,9 @@ Try<Nothing, StatusError> TestCSIPlugin::nodeUnstageVolume(
         grpc::NOT_FOUND, "Volume '" + volumeId + "' does not exist"));
   }
 
-  Try<fs::MountInfoTable> table = fs::MountInfoTable::read();
+  Try<internal::fs::MountInfoTable> table =
+    internal::fs::MountInfoTable::read();
+
   if (table.isError()) {
     return StatusError(Status(
         grpc::INTERNAL, "Failed to get mount table: " + table.error()));
@@ -1578,13 +1609,13 @@ Try<Nothing, StatusError> TestCSIPlugin::nodeUnstageVolume(
   if (std::none_of(
           table->entries.begin(),
           table->entries.end(),
-          [&](const fs::MountInfoTable::Entry& entry) {
+          [&](const internal::fs::MountInfoTable::Entry& entry) {
             return entry.target == stagingPath;
           })) {
     return Nothing();
   }
 
-  Try<Nothing> unmount = fs::unmount(stagingPath);
+  Try<Nothing> unmount = internal::fs::unmount(stagingPath);
   if (unmount.isError()) {
     return StatusError(Status(
         grpc::INTERNAL,
@@ -1626,14 +1657,15 @@ Try<Nothing, StatusError> TestCSIPlugin::nodePublishVolume(
   }
 
   const VolumeInfo& volumeInfo = volumes.at(volumeId);
-  const string path = getVolumePath(volumeInfo);
 
-  if (!volumeContext.count("path") || volumeContext.at("path") != path) {
+  if (volumeContext != volumeInfo.context) {
     return StatusError(Status(
         grpc::INVALID_ARGUMENT, "Invalid volume context"));
   }
 
-  Try<fs::MountInfoTable> table = fs::MountInfoTable::read();
+  Try<internal::fs::MountInfoTable> table =
+    internal::fs::MountInfoTable::read();
+
   if (table.isError()) {
     return StatusError(Status(
         grpc::INTERNAL, "Failed to get mount table: " + table.error()));
@@ -1642,7 +1674,7 @@ Try<Nothing, StatusError> TestCSIPlugin::nodePublishVolume(
   if (std::none_of(
           table->entries.begin(),
           table->entries.end(),
-          [&](const fs::MountInfoTable::Entry& entry) {
+          [&](const internal::fs::MountInfoTable::Entry& entry) {
             return entry.target == stagingPath;
           })) {
     return StatusError(Status(
@@ -1653,13 +1685,13 @@ Try<Nothing, StatusError> TestCSIPlugin::nodePublishVolume(
   if (std::any_of(
           table->entries.begin(),
           table->entries.end(),
-          [&](const fs::MountInfoTable::Entry& entry) {
+          [&](const internal::fs::MountInfoTable::Entry& entry) {
             return entry.target == targetPath;
           })) {
     return Nothing();
   }
 
-  Try<Nothing> mount = fs::mount(
+  Try<Nothing> mount = internal::fs::mount(
       stagingPath,
       targetPath,
       None(),
@@ -1685,7 +1717,9 @@ Try<Nothing, StatusError> TestCSIPlugin::nodeUnpublishVolume(
         grpc::NOT_FOUND, "Volume '" + volumeId + "' does not exist"));
   }
 
-  Try<fs::MountInfoTable> table = fs::MountInfoTable::read();
+  Try<internal::fs::MountInfoTable> table =
+    internal::fs::MountInfoTable::read();
+
   if (table.isError()) {
     return StatusError(Status(
         grpc::INTERNAL, "Failed to get mount table: " + table.error()));
@@ -1694,13 +1728,13 @@ Try<Nothing, StatusError> TestCSIPlugin::nodeUnpublishVolume(
   if (std::none_of(
           table->entries.begin(),
           table->entries.end(),
-          [&](const fs::MountInfoTable::Entry& entry) {
+          [&](const internal::fs::MountInfoTable::Entry& entry) {
             return entry.target == targetPath;
           })) {
     return Nothing();
   }
 
-  Try<Nothing> unmount = fs::unmount(targetPath);
+  Try<Nothing> unmount = internal::fs::unmount(targetPath);
   if (unmount.isError()) {
     return StatusError(Status(
         grpc::INTERNAL,
@@ -1985,13 +2019,8 @@ int main(int argc, char** argv)
                  strings::pairs(flags.volumes.get(), ";", ":")) {
       Option<Error> error;
 
-      if (strings::contains(name, stringify(os::PATH_SEPARATOR))) {
-        error =
-          "Volume name cannot contain '" + stringify(os::PATH_SEPARATOR) + "'";
-      } else if (capacities.size() != 1) {
+      if (capacities.size() != 1) {
         error = "Volume name must be unique";
-      } else if (volumes.contains(name)) {
-        error = "Volume '" + name + "' already exists";
       } else {
         Try<Bytes> capacity = Bytes::parse(capacities[0]);
         if (capacity.isError()) {
diff --git a/src/tests/storage_local_resource_provider_tests.cpp b/src/tests/storage_local_resource_provider_tests.cpp
index 609aebc..7dd08be 100644
--- a/src/tests/storage_local_resource_provider_tests.cpp
+++ b/src/tests/storage_local_resource_provider_tests.cpp
@@ -1011,22 +1011,13 @@ TEST_P(StorageLocalResourceProviderTest, CreateDestroyDisk)
     .filter(std::bind(isMountDisk<Resource>, lambda::_1, "test"))
     .begin();
 
-  ASSERT_TRUE(created.disk().source().has_metadata());
   ASSERT_TRUE(created.disk().source().has_mount());
   ASSERT_TRUE(created.disk().source().mount().has_root());
   EXPECT_FALSE(path::absolute(created.disk().source().mount().root()));
 
   // Check if the volume is actually created by the test CSI plugin.
-  Option<string> volumePath;
-  foreach (const Label& label, created.disk().source().metadata().labels()) {
-    if (label.key() == "path") {
-      volumePath = label.value();
-      break;
-    }
-  }
-
-  ASSERT_SOME(volumePath);
-  EXPECT_TRUE(os::exists(volumePath.get()));
+  const string& volumePath = created.disk().source().id();
+  EXPECT_TRUE(os::exists(volumePath));
 
   // Destroy the MOUNT disk with pipelined `CREATE`, `DESTROY` and `UNRESERVE`
   // operations. Note that `UNRESERVE` can come before `DESTROY_DISK`.
@@ -1056,7 +1047,7 @@ TEST_P(StorageLocalResourceProviderTest, CreateDestroyDisk)
   AWAIT_READY(offers);
 
   // Check if the volume is actually deleted by the test CSI plugin.
-  EXPECT_FALSE(os::exists(volumePath.get()));
+  EXPECT_FALSE(os::exists(volumePath));
 }
 
 
@@ -1150,22 +1141,13 @@ TEST_P(StorageLocalResourceProviderTest, CreateDestroyDiskWithRecovery)
     .filter(std::bind(isMountDisk<Resource>, lambda::_1, "test"))
     .begin();
 
-  ASSERT_TRUE(created.disk().source().has_metadata());
   ASSERT_TRUE(created.disk().source().has_mount());
   ASSERT_TRUE(created.disk().source().mount().has_root());
   EXPECT_FALSE(path::absolute(created.disk().source().mount().root()));
 
   // Check if the volume is actually created by the test CSI plugin.
-  Option<string> volumePath;
-  foreach (const Label& label, created.disk().source().metadata().labels()) {
-    if (label.key() == "path") {
-      volumePath = label.value();
-      break;
-    }
-  }
-
-  ASSERT_SOME(volumePath);
-  EXPECT_TRUE(os::exists(volumePath.get()));
+  const string& volumePath = created.disk().source().id();
+  EXPECT_TRUE(os::exists(volumePath));
 
   // Restart the agent.
   EXPECT_CALL(sched, offerRescinded(_, _));
@@ -1250,7 +1232,7 @@ TEST_P(StorageLocalResourceProviderTest, CreateDestroyDiskWithRecovery)
   AWAIT_READY(offers);
 
   // Check if the volume is actually deleted by the test CSI plugin.
-  EXPECT_FALSE(os::exists(volumePath.get()));
+  EXPECT_FALSE(os::exists(volumePath));
 }
 
 
@@ -1882,17 +1864,9 @@ TEST_P(StorageLocalResourceProviderTest, ROOT_AgentRegisteredWithNewId)
     ASSERT_NE(sourceIdToProviderId.at(volume.disk().source().id()),
               volume.provider_id());
 
-    Option<string> volumePath;
-    foreach (const Label& label, volume.disk().source().metadata().labels()) {
-      if (label.key() == "path") {
-        volumePath = label.value();
-        break;
-      }
-    }
-
-    ASSERT_SOME(volumePath);
-    ASSERT_TRUE(os::exists(volumePath.get()));
-    volumePaths.push_back(volumePath.get());
+    const string& volumePath = volume.disk().source().id();
+    ASSERT_TRUE(os::exists(volumePath));
+    volumePaths.push_back(volumePath);
   }
 
   // Apply profiles 'good' and 'bad' to the preprovisioned volumes. The first
@@ -2080,22 +2054,13 @@ TEST_P(
     .filter(std::bind(isMountDisk<Resource>, lambda::_1, "test"))
     .begin();
 
-  ASSERT_TRUE(created.disk().source().has_metadata());
   ASSERT_TRUE(created.disk().source().has_mount());
   ASSERT_TRUE(created.disk().source().mount().has_root());
   EXPECT_FALSE(path::absolute(created.disk().source().mount().root()));
 
   // Check if the CSI volume is actually created.
-  Option<string> volumePath;
-  foreach (const Label& label, created.disk().source().metadata().labels()) {
-    if (label.key() == "path") {
-      volumePath = label.value();
-      break;
-    }
-  }
-
-  ASSERT_SOME(volumePath);
-  EXPECT_TRUE(os::exists(volumePath.get()));
+  const string& volumePath = created.disk().source().id();
+  EXPECT_TRUE(os::exists(volumePath));
 
   // Create a persistent MOUNT volume then launch a task to write a file.
   Resource persistentVolume = created;
@@ -2148,8 +2113,8 @@ TEST_P(
   EXPECT_EQ(OPERATION_FINISHED, updateOperationStatusMessage->status().state());
 
   // Check if the CSI volume still exists but has being cleaned up.
-  EXPECT_TRUE(os::exists(volumePath.get()));
-  EXPECT_FALSE(os::exists(path::join(volumePath.get(), "file")));
+  EXPECT_TRUE(os::exists(volumePath));
+  EXPECT_FALSE(os::exists(path::join(volumePath, "file")));
 
   AWAIT_READY(offers);
   ASSERT_EQ(1u, offers->size());
@@ -2169,7 +2134,7 @@ TEST_P(
   EXPECT_EQ(OPERATION_FINISHED, updateOperationStatusMessage->status().state());
 
   // Check if the CSI volume is actually deleted.
-  EXPECT_FALSE(os::exists(volumePath.get()));
+  EXPECT_FALSE(os::exists(volumePath));
 
   AWAIT_READY(offers);
 }
@@ -2411,18 +2376,9 @@ TEST_P(
   EXPECT_EQ(OPERATION_FINISHED, updateOperationStatusMessage->status().state());
 
   // Check if the CSI volume still exists but has being cleaned up.
-  Option<string> volumePath;
-
-  foreach (const Label& label, created.disk().source().metadata().labels()) {
-    if (label.key() == "path") {
-      volumePath = label.value();
-      break;
-    }
-  }
-
-  ASSERT_SOME(volumePath);
-  EXPECT_TRUE(os::exists(volumePath.get()));
-  EXPECT_FALSE(os::exists(path::join(volumePath.get(), "file")));
+  const string& volumePath = created.disk().source().id();
+  EXPECT_TRUE(os::exists(volumePath));
+  EXPECT_FALSE(os::exists(path::join(volumePath, "file")));
 
   AWAIT_READY(offers);
   ASSERT_EQ(1u, offers->size());
@@ -2442,7 +2398,7 @@ TEST_P(
   EXPECT_EQ(OPERATION_FINISHED, updateOperationStatusMessage->status().state());
 
   // Check if the CSI volume is actually deleted.
-  EXPECT_FALSE(os::exists(volumePath.get()));
+  EXPECT_FALSE(os::exists(volumePath));
 
   AWAIT_READY(offers);
 }
@@ -2728,18 +2684,9 @@ TEST_P(
   EXPECT_EQ(OPERATION_FINISHED, updateOperationStatusMessage->status().state());
 
   // Check if the CSI volume still exists but has being cleaned up.
-  Option<string> volumePath;
-
-  foreach (const Label& label, created.disk().source().metadata().labels()) {
-    if (label.key() == "path") {
-      volumePath = label.value();
-      break;
-    }
-  }
-
-  ASSERT_SOME(volumePath);
-  EXPECT_TRUE(os::exists(volumePath.get()));
-  EXPECT_FALSE(os::exists(path::join(volumePath.get(), "file")));
+  const string& volumePath = created.disk().source().id();
+  EXPECT_TRUE(os::exists(volumePath));
+  EXPECT_FALSE(os::exists(path::join(volumePath, "file")));
 
   AWAIT_READY(offers);
   ASSERT_EQ(1u, offers->size());
@@ -2759,7 +2706,7 @@ TEST_P(
   EXPECT_EQ(OPERATION_FINISHED, updateOperationStatusMessage->status().state());
 
   // Check if the CSI volume is actually deleted.
-  EXPECT_FALSE(os::exists(volumePath.get()));
+  EXPECT_FALSE(os::exists(volumePath));
 
   AWAIT_READY(offers);
 }
@@ -2909,23 +2856,13 @@ TEST_P(
   ASSERT_TRUE(volume->disk().source().has_vendor());
   EXPECT_EQ(TEST_CSI_VENDOR, volume->disk().source().vendor());
   ASSERT_TRUE(volume->disk().source().has_id());
-  ASSERT_TRUE(volume->disk().source().has_metadata());
   ASSERT_TRUE(volume->disk().source().has_mount());
   ASSERT_TRUE(volume->disk().source().mount().has_root());
   EXPECT_FALSE(path::absolute(volume->disk().source().mount().root()));
 
   // Check if the volume is actually created by the test CSI plugin.
-  Option<string> volumePath;
-
-  foreach (const Label& label, volume->disk().source().metadata().labels()) {
-    if (label.key() == "path") {
-      volumePath = label.value();
-      break;
-    }
-  }
-
-  ASSERT_SOME(volumePath);
-  EXPECT_TRUE(os::exists(volumePath.get()));
+  const string& volumePath = volume->disk().source().id();
+  EXPECT_TRUE(os::exists(volumePath));
 
   pluginRestarted =
     FUTURE_DISPATCH(_, &ContainerDaemonProcess::launchContainer);
@@ -2940,7 +2877,7 @@ TEST_P(
   AWAIT_READY(pluginRestarted);
 
   // Put a file into the volume.
-  ASSERT_SOME(os::touch(path::join(volumePath.get(), "file")));
+  ASSERT_SOME(os::touch(path::join(volumePath, "file")));
 
   // Create a persistent volume on the CSI volume, then launch a task to
   // use the persistent volume.
@@ -3011,7 +2948,7 @@ TEST_P(
   ASSERT_FALSE(volumeDestroyedOffers->empty());
 
   // Check if the volume is actually deleted by the test CSI plugin.
-  EXPECT_FALSE(os::exists(volumePath.get()));
+  EXPECT_FALSE(os::exists(volumePath));
 }
 
 
@@ -3908,17 +3845,8 @@ TEST_P(
   offer = offers->at(0);
 
   // Bind-mount the file in the CSI volume to fail persistent volume cleanup.
-  Option<string> volumePath;
-  foreach (const Label& label, created.disk().source().metadata().labels()) {
-    if (label.key() == "path") {
-      volumePath = label.value();
-      break;
-    }
-  }
-
-  ASSERT_SOME(volumePath);
-
-  const string filePath = path::join(volumePath.get(), "file");
+  const string& volumePath = created.disk().source().id();
+  const string filePath = path::join(volumePath, "file");
   ASSERT_TRUE(os::exists(filePath));
   ASSERT_SOME(fs::mount(filePath, filePath, None(), MS_BIND, None()));
 
@@ -4052,17 +3980,9 @@ TEST_P(StorageLocalResourceProviderTest, CreateDestroyPreprovisionedVolume)
   // Get the volume paths of the preprovisioned volumes.
   vector<string> volumePaths;
   foreach (const Resource& volume, preprovisioned) {
-    Option<string> volumePath;
-    foreach (const Label& label, volume.disk().source().metadata().labels()) {
-      if (label.key() == "path") {
-        volumePath = label.value();
-        break;
-      }
-    }
-
-    ASSERT_SOME(volumePath);
-    ASSERT_TRUE(os::exists(volumePath.get()));
-    volumePaths.push_back(volumePath.get());
+    const string& volumePath = volume.disk().source().id();
+    ASSERT_TRUE(os::exists(volumePath));
+    volumePaths.push_back(volumePath);
   }
 
   // Apply profile 'test' and 'unknown' to the preprovisioned volumes. The first
@@ -4872,7 +4792,6 @@ TEST_P(StorageLocalResourceProviderTest, OperationStateMetrics)
   ASSERT_TRUE(volume->disk().source().has_vendor());
   EXPECT_EQ(TEST_CSI_VENDOR, volume->disk().source().vendor());
   ASSERT_TRUE(volume->disk().source().has_id());
-  ASSERT_TRUE(volume->disk().source().has_metadata());
   ASSERT_TRUE(volume->disk().source().has_mount());
   ASSERT_TRUE(volume->disk().source().mount().has_root());
   EXPECT_FALSE(path::absolute(volume->disk().source().mount().root()));
@@ -4893,17 +4812,8 @@ TEST_P(StorageLocalResourceProviderTest, OperationStateMetrics)
       "operations/destroy_disk/dropped")));
 
   // Remove the volume out of band to fail `DESTROY_DISK`.
-  Option<string> volumePath;
-
-  foreach (const Label& label, volume->disk().source().metadata().labels()) {
-    if (label.key() == "path") {
-      volumePath = label.value();
-      break;
-    }
-  }
-
-  ASSERT_SOME(volumePath);
-  ASSERT_SOME(os::rmdir(volumePath.get()));
+  const string& volumePath = volume->disk().source().id();
+  ASSERT_SOME(os::rmdir(volumePath));
 
   // Destroy the created volume, which will fail.
   EXPECT_CALL(sched, resourceOffers(&driver, OffersHaveResource(volume.get())))
@@ -5095,7 +5005,6 @@ TEST_P(StorageLocalResourceProviderTest, CsiPluginRpcMetrics)
   ASSERT_TRUE(volume->disk().source().has_vendor());
   EXPECT_EQ(TEST_CSI_VENDOR, volume->disk().source().vendor());
   ASSERT_TRUE(volume->disk().source().has_id());
-  ASSERT_TRUE(volume->disk().source().has_metadata());
   ASSERT_TRUE(volume->disk().source().has_mount());
   ASSERT_TRUE(volume->disk().source().mount().has_root());
   EXPECT_FALSE(path::absolute(volume->disk().source().mount().root()));
@@ -5106,17 +5015,8 @@ TEST_P(StorageLocalResourceProviderTest, CsiPluginRpcMetrics)
   EXPECT_TRUE(metricEquals(metricName("csi_plugin/rpcs_failed"), 0));
 
   // Remove the volume out of band to fail `DESTROY_DISK`.
-  Option<string> volumePath;
-
-  foreach (const Label& label, volume->disk().source().metadata().labels()) {
-    if (label.key() == "path") {
-      volumePath = label.value();
-      break;
-    }
-  }
-
-  ASSERT_SOME(volumePath);
-  ASSERT_SOME(os::rmdir(volumePath.get()));
+  const string& volumePath = volume->disk().source().id();
+  ASSERT_SOME(os::rmdir(volumePath));
 
   // Destroy the created volume, which will fail.
   EXPECT_CALL(sched, resourceOffers(&driver, OffersHaveResource(volume.get())))


[mesos] 08/09: Garbage-collected disappeared RPs when agent resources remain unchanged.

Posted by ch...@apache.org.
This is an automated email from the ASF dual-hosted git repository.

chhsiao pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/mesos.git

commit 4c82acba45e31d2426adebdb7783b8bd762b6b0d
Author: Chun-Hung Hsiao <ch...@mesosphere.io>
AuthorDate: Wed Jun 5 21:47:07 2019 -0700

    Garbage-collected disappeared RPs when agent resources remain unchanged.
    
    Previously when there is a missing resource provider in an
    `UpdateSlaveMessage` but the agent's total resources remain unchanged,
    the update message will be completely ignored, so the missing resource
    provider will still be cached in the master's state, which is not the
    desired behavior. This patch ensures that the master's state gets
    updated if any resource provider is missing.
    
    Review: https://reviews.apache.org/r/70788
---
 src/master/master.cpp   |  8 ++++
 src/tests/api_tests.cpp | 99 +++++++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 107 insertions(+)

diff --git a/src/master/master.cpp b/src/master/master.cpp
index 4d7c37c..b3c10ab 100644
--- a/src/master/master.cpp
+++ b/src/master/master.cpp
@@ -8185,6 +8185,8 @@ void Master::updateSlave(UpdateSlaveMessage&& message)
 
   // Check if resource provider information changed.
   if (!updated && message.has_resource_providers()) {
+    hashset<ResourceProviderID> receivedResourceProviders;
+
     foreach (
         const UpdateSlaveMessage::ResourceProvider& receivedProvider,
         message.resource_providers().providers()) {
@@ -8194,6 +8196,8 @@ void Master::updateSlave(UpdateSlaveMessage&& message)
       const ResourceProviderID& resourceProviderId =
         receivedProvider.info().id();
 
+      receivedResourceProviders.insert(resourceProviderId);
+
       if (!slave->resourceProviders.contains(resourceProviderId)) {
         updated = true;
         break;
@@ -8224,6 +8228,10 @@ void Master::updateSlave(UpdateSlaveMessage&& message)
         }
       }
     }
+
+    if (slave->resourceProviders.keys() != receivedResourceProviders) {
+      updated = true;
+    }
   }
 
   if (!updated) {
diff --git a/src/tests/api_tests.cpp b/src/tests/api_tests.cpp
index 2220cec..f191a1c 100644
--- a/src/tests/api_tests.cpp
+++ b/src/tests/api_tests.cpp
@@ -275,6 +275,105 @@ TEST_P(MasterAPITest, GetAgents)
 }
 
 
+// This test verifies that if a resource provider becomes disconnected, it will
+// not be reported by `GET_AGENT` calls.
+TEST_P(MasterAPITest, GetAgentsDisconnectedResourceProvider)
+{
+  Clock::pause();
+
+  const ContentType contentType = GetParam();
+
+  master::Flags masterFlags = CreateMasterFlags();
+  Try<Owned<cluster::Master>> master = this->StartMaster(masterFlags);
+  ASSERT_SOME(master);
+
+  Owned<MasterDetector> detector = master.get()->createDetector();
+
+  // Start one agent.
+  Future<UpdateSlaveMessage> updateSlaveMessage =
+    FUTURE_PROTOBUF(UpdateSlaveMessage(), _, _);
+
+  slave::Flags slaveFlags = CreateSlaveFlags();
+  Try<Owned<cluster::Slave>> slave = StartSlave(detector.get(), slaveFlags);
+  ASSERT_SOME(slave);
+
+  Clock::settle();
+  Clock::advance(slaveFlags.registration_backoff_factor);
+
+  AWAIT_READY(updateSlaveMessage);
+  ASSERT_TRUE(updateSlaveMessage->resource_providers().providers().empty());
+
+  // Start a resource provider.
+  mesos::v1::ResourceProviderInfo info;
+  info.set_type("org.apache.mesos.rp.test");
+  info.set_name("test");
+
+  v1::MockResourceProvider resourceProvider(info, v1::Resources());
+
+  // Start and register a resource provider.
+  Owned<EndpointDetector> endpointDetector(
+      resource_provider::createEndpointDetector(slave.get()->pid));
+
+  updateSlaveMessage = FUTURE_PROTOBUF(UpdateSlaveMessage(), _, _);
+
+  resourceProvider.start(std::move(endpointDetector), contentType);
+
+  // Wait until the agent's resources have been updated to include the
+  // resource provider.
+  AWAIT_READY(updateSlaveMessage);
+  ASSERT_FALSE(updateSlaveMessage->resource_providers().providers().empty());
+
+  {
+    v1::master::Call v1Call;
+    v1Call.set_type(v1::master::Call::GET_AGENTS);
+
+    Future<v1::master::Response> v1Response =
+      post(master.get()->pid, v1Call, contentType);
+
+    AWAIT_READY(v1Response);
+    ASSERT_TRUE(v1Response->IsInitialized());
+    ASSERT_EQ(v1::master::Response::GET_AGENTS, v1Response->type());
+    ASSERT_EQ(1, v1Response->get_agents().agents_size());
+    ASSERT_EQ(1, v1Response->get_agents().agents(0).resource_providers_size());
+
+    const mesos::v1::ResourceProviderInfo& responseInfo =
+      v1Response->get_agents()
+        .agents(0)
+        .resource_providers(0)
+        .resource_provider_info();
+
+    EXPECT_EQ(info.type(), responseInfo.type());
+    EXPECT_EQ(info.name(), responseInfo.name());
+    EXPECT_TRUE(responseInfo.has_id());
+  }
+
+  updateSlaveMessage = FUTURE_PROTOBUF(UpdateSlaveMessage(), _, _);
+
+  // Disconnect the resource provider.
+  resourceProvider.stop();
+
+  // Wait until the agent's resources have been updated to exclude the
+  // resource provider.
+  AWAIT_READY(updateSlaveMessage);
+  ASSERT_TRUE(updateSlaveMessage->resource_providers().providers().empty());
+
+  {
+    v1::master::Call v1Call;
+    v1Call.set_type(v1::master::Call::GET_AGENTS);
+
+    Future<v1::master::Response> v1Response =
+      post(master.get()->pid, v1Call, contentType);
+
+    AWAIT_READY(v1Response);
+    ASSERT_TRUE(v1Response->IsInitialized());
+    ASSERT_EQ(v1::master::Response::GET_AGENTS, v1Response->type());
+    ASSERT_EQ(1, v1Response->get_agents().agents_size());
+    EXPECT_TRUE(
+        v1Response->get_agents().agents(0).resource_providers().empty());
+  }
+}
+
+
 TEST_P(MasterAPITest, GetFlags)
 {
   Try<Owned<cluster::Master>> master = this->StartMaster();