Fixed the broken metrics information of master in WebUI.

After we introduced redirection on `/master/state` endpoint to the
leading master in `c9153336`, the metrics information in the WebUI
was broken when the current master is not the leading master.

In this patch, we retrieve the leading master from `/master/state`
endpoint and ensure that requests to `/metrics/snapshot` and `/state`
endpoints are always sent to the leading master.

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


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

Branch: refs/heads/1.0.x
Commit: 1ca4db714fd0acc6095a7a0e14c373a3775df528
Parents: ef90134
Author: haosdent huang <haosd...@gmail.com>
Authored: Thu Oct 27 10:22:18 2016 -0700
Committer: Vinod Kone <vinodk...@gmail.com>
Committed: Thu Oct 27 13:22:54 2016 -0700

----------------------------------------------------------------------
 src/webui/master/static/js/controllers.js | 24 +++++++++++++++++-------
 1 file changed, 17 insertions(+), 7 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/mesos/blob/1ca4db71/src/webui/master/static/js/controllers.js
----------------------------------------------------------------------
diff --git a/src/webui/master/static/js/controllers.js 
b/src/webui/master/static/js/controllers.js
index cf8c2a4..b30794c 100644
--- a/src/webui/master/static/js/controllers.js
+++ b/src/webui/master/static/js/controllers.js
@@ -233,8 +233,7 @@
   });
 
   // Update the outermost scope with the metrics/snapshot endpoint.
-  function updateMetrics($scope, $timeout, data) {
-    var metrics = JSON.parse(data);
+  function updateMetrics($scope, $timeout, metrics) {
     $scope.staging_tasks = metrics['master/tasks_staging'];
     $scope.starting_tasks = metrics['master/tasks_starting'];
     $scope.running_tasks = metrics['master/tasks_running'];
@@ -317,6 +316,18 @@
       if (!matched) $scope.navbarActiveTab = null;
     });
 
+    var leadingMasterURL = function(path) {
+      // Use current location as address in case we could not find the
+      // leading master.
+      var address = location.hostname + ':' + location.port;
+      if ($scope.state && $scope.state.leader_info) {
+          address = $scope.state.leader_info.hostname + ':' +
+                    $scope.state.leader_info.port;
+      }
+
+      return '//' + address + path;
+    }
+
     var popupErrorModal = function() {
       if ($scope.delay >= 128000) {
         $scope.delay = 2000;
@@ -376,7 +387,7 @@
       // the leading master automatically. This would cause a CORS error if we
       // use XMLHttpRequest here. To avoid the CORS error, we use JSONP as a
       // workaround. Please refer to MESOS-5911 for further details.
-      $http.jsonp('master/state?jsonp=JSON_CALLBACK')
+      $http.jsonp(leadingMasterURL('/master/state?jsonp=JSON_CALLBACK'))
         .success(function(response) {
           if (updateState($scope, $timeout, response)) {
             $scope.delay = updateInterval(_.size($scope.agents));
@@ -391,10 +402,9 @@
     };
 
     var pollMetrics = function() {
-      $http.get('metrics/snapshot',
-                {transformResponse: function(data) { return data; }})
-        .success(function(data) {
-          if (updateMetrics($scope, $timeout, data)) {
+      $http.jsonp(leadingMasterURL('/metrics/snapshot?jsonp=JSON_CALLBACK'))
+        .success(function(response) {
+          if (updateMetrics($scope, $timeout, response)) {
             $scope.delay = updateInterval(_.size($scope.agents));
             $timeout(pollMetrics, $scope.delay);
           }

Reply via email to