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/4ac4916a Tree: http://git-wip-us.apache.org/repos/asf/mesos/tree/4ac4916a Diff: http://git-wip-us.apache.org/repos/asf/mesos/diff/4ac4916a Branch: refs/heads/1.1.x Commit: 4ac4916a39a6beb81fab0c8d7d72fc6c06e2e650 Parents: 0d74729 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:30:35 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/4ac4916a/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 3dead4f..dd0cc81 100644 --- a/src/webui/master/static/js/controllers.js +++ b/src/webui/master/static/js/controllers.js @@ -241,8 +241,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']; @@ -325,6 +324,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; @@ -384,7 +395,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)); @@ -399,10 +410,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); }