You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@mesos.apache.org by bm...@apache.org on 2014/05/13 00:48:43 UTC

git commit: Allowed Gauge to take synchronous functions.

Repository: mesos
Updated Branches:
  refs/heads/master 4faf21555 -> 249011b3d


Allowed Gauge to take synchronous functions.

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


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

Branch: refs/heads/master
Commit: 249011b3d71fcc1776104132391c2011335788d5
Parents: 4faf215
Author: Dominic Hamon <dh...@twopensource.com>
Authored: Mon May 12 15:47:28 2014 -0700
Committer: Benjamin Mahler <bm...@twitter.com>
Committed: Mon May 12 15:48:11 2014 -0700

----------------------------------------------------------------------
 .../include/process/metrics/gauge.hpp           | 21 ++++++++++++++---
 3rdparty/libprocess/src/tests/metrics_tests.cpp | 24 ++++++++++++++------
 2 files changed, 35 insertions(+), 10 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/mesos/blob/249011b3/3rdparty/libprocess/include/process/metrics/gauge.hpp
----------------------------------------------------------------------
diff --git a/3rdparty/libprocess/include/process/metrics/gauge.hpp b/3rdparty/libprocess/include/process/metrics/gauge.hpp
index af06534..560e273 100644
--- a/3rdparty/libprocess/include/process/metrics/gauge.hpp
+++ b/3rdparty/libprocess/include/process/metrics/gauge.hpp
@@ -3,11 +3,14 @@
 
 #include <string>
 
+#include <process/async.hpp>
 #include <process/defer.hpp>
 
 #include <process/metrics/metric.hpp>
 
+#include <stout/lambda.hpp>
 #include <stout/memory.hpp>
+#include <stout/none.hpp>
 
 namespace process {
 namespace metrics {
@@ -19,9 +22,14 @@ class Gauge : public Metric
 public:
   // 'name' is the unique name for the instance of Gauge being constructed.
   // It will be the key exposed in the JSON endpoint.
+  // 'f' is the function called asynchronously when the Metric value is
+  // requested.
+  Gauge(const std::string& name, const lambda::function<double(void)>& f)
+    : Metric(name, None()),
+      data(new Data(f)) {}
+
   // 'f' is the deferred object called when the Metric value is requested.
-  Gauge(const std::string& name,
-        const Deferred<Future<double> (void)>& f)
+  Gauge(const std::string& name, const Deferred<Future<double>(void)>& f)
     : Metric(name, None()),
       data(new Data(f)) {}
 
@@ -32,10 +40,17 @@ public:
 private:
   struct Data
   {
+    // Wrap the function in an async call. The extra NULL parameter is required
+    // as async takes a third parameter that defaults to NULL.
+    explicit Data(const lambda::function<double (void)>& _f)
+      : f(lambda::bind(&async<lambda::function<double (void)> >,
+                       _f,
+                       static_cast<void*>(NULL))) {}
+
     explicit Data(const Deferred<Future<double> (void)>& _f)
       : f(_f) {}
 
-    const Deferred<Future<double> (void)> f;
+    const lambda::function<Future<double> (void)> f;
   };
 
   memory::shared_ptr<Data> data;

http://git-wip-us.apache.org/repos/asf/mesos/blob/249011b3/3rdparty/libprocess/src/tests/metrics_tests.cpp
----------------------------------------------------------------------
diff --git a/3rdparty/libprocess/src/tests/metrics_tests.cpp b/3rdparty/libprocess/src/tests/metrics_tests.cpp
index 8a13121..c455269 100644
--- a/3rdparty/libprocess/src/tests/metrics_tests.cpp
+++ b/3rdparty/libprocess/src/tests/metrics_tests.cpp
@@ -4,6 +4,7 @@
 #include <stout/gtest.hpp>
 
 #include <process/clock.hpp>
+#include <process/defer.hpp>
 #include <process/future.hpp>
 #include <process/gtest.hpp>
 #include <process/http.hpp>
@@ -27,10 +28,16 @@ using process::metrics::Gauge;
 using process::metrics::Timer;
 
 
+double one()
+{
+  return 1.0;
+}
+
+
 class GaugeProcess : public Process<GaugeProcess>
 {
 public:
-  double get()
+  Future<double> get()
   {
     return 42.0;
   }
@@ -77,26 +84,29 @@ TEST(Metrics, Gauge)
 {
   ASSERT_TRUE(GTEST_IS_THREADSAFE);
 
+  // Simple synchronous gauge.
+  Gauge gauge("test/function_gauge", one);
+
+  AWAIT_READY(metrics::add(gauge));
+  AWAIT_EXPECT_EQ(1.0, gauge.value());
+  AWAIT_READY(metrics::remove(gauge));
+
   GaugeProcess process;
   PID<GaugeProcess> pid = spawn(&process);
   ASSERT_TRUE(pid);
 
   // Gauge with a value.
-  Gauge gauge("test/gauge", defer(pid, &GaugeProcess::get));
+  gauge = Gauge("test/deferred_gauge", defer(pid, &GaugeProcess::get));
 
   AWAIT_READY(metrics::add(gauge));
-
   AWAIT_EXPECT_EQ(42.0, gauge.value());
-
   AWAIT_READY(metrics::remove(gauge));
 
   // Failing gauge.
-  gauge = Gauge("test/failedgauge", defer(pid, &GaugeProcess::fail));
+  gauge = Gauge("test/failed_gauge", defer(pid, &GaugeProcess::fail));
 
   AWAIT_READY(metrics::add(gauge));
-
   AWAIT_EXPECT_FAILED(gauge.value());
-
   AWAIT_READY(metrics::remove(gauge));
 
   terminate(process);