Browse Source

AMBARI-4664. Replace using local time with real server time. (xiwang via yusaku)

Yusaku Sako 11 năm trước cách đây
mục cha
commit
c6e459251e
26 tập tin đã thay đổi với 95 bổ sung37 xóa
  1. 3 1
      ambari-web/app/app.js
  2. 3 3
      ambari-web/app/controllers/global/background_operations_controller.js
  3. 30 0
      ambari-web/app/controllers/global/cluster_controller.js
  4. 1 1
      ambari-web/app/controllers/main/admin/highAvailability/progress_controller.js
  5. 1 1
      ambari-web/app/controllers/main/admin/security/security_progress_controller.js
  6. 1 1
      ambari-web/app/controllers/main/apps_controller.js
  7. 1 1
      ambari-web/app/controllers/wizard.js
  8. 1 1
      ambari-web/app/controllers/wizard/stack_upgrade/step3_controller.js
  9. 3 3
      ambari-web/app/controllers/wizard/step3_controller.js
  10. 1 1
      ambari-web/app/controllers/wizard/step9_controller.js
  11. 1 1
      ambari-web/app/models/alert.js
  12. 1 1
      ambari-web/app/models/user.js
  13. 3 1
      ambari-web/app/routes/main.js
  14. 3 0
      ambari-web/app/utils/ajax.js
  15. 9 5
      ambari-web/app/utils/date.js
  16. 9 0
      ambari-web/app/utils/helper.js
  17. 1 1
      ambari-web/app/utils/http_client.js
  18. 4 4
      ambari-web/app/views/common/chart/linear_time.js
  19. 3 3
      ambari-web/app/views/common/configs/services_config.js
  20. 4 1
      ambari-web/app/views/common/filter_view.js
  21. 2 2
      ambari-web/app/views/main/dashboard/service/hbase.js
  22. 1 1
      ambari-web/app/views/main/dashboard/service/hdfs.js
  23. 1 1
      ambari-web/app/views/main/dashboard/service/mapreduce.js
  24. 1 1
      ambari-web/app/views/main/dashboard/service/yarn.js
  25. 1 1
      ambari-web/app/views/main/dashboard/widgets/uptime_text_widget.js
  26. 6 1
      ambari-web/vendor/scripts/jquery.timeago.js

+ 3 - 1
ambari-web/app/app.js

@@ -53,6 +53,7 @@ module.exports = Em.Application.create({
     return '/stacks2/HDP/versions/' + stackVersion.replace(/HDP-/g, '');
   }.property('currentStackVersion'),
   clusterName: null,
+  clockDistance:null, // server clock - client clock
   currentStackVersion: '',
   currentStackVersionNumber: function(){
     return this.get('currentStackVersion').replace(/HDP(Local)?-/, '');
@@ -250,6 +251,7 @@ Em.View.reopen({
  * only in seconds whereas Javascript's Date needs
  * milliseconds representation.
  */
+var App = require('app');
 DS.attr.transforms.date = {
   from: function (serialized) {
     var type = typeof serialized;
@@ -261,7 +263,7 @@ DS.attr.transforms.date = {
       // The number could be seconds or milliseconds.
       // If seconds, then multiplying with 1000 should still
       // keep it below the current time.
-      if (serialized * 1000 < new Date().getTime()) {
+      if (serialized * 1000 < App.dateTime()) {
         serialized = serialized * 1000;
       }
       return new Date(serialized);

+ 3 - 3
ambari-web/app/controllers/global/background_operations_controller.js

@@ -131,7 +131,7 @@ App.BackgroundOperationsController = Em.Controller.extend({
     }, this);
     request.set('previousTaskStatusMap', currentTaskStatusMap);
     request.set('hostsMap', hostsMap);
-    this.set('serviceTimestamp', new Date().getTime());
+    this.set('serviceTimestamp', App.dateTime());
   },
   /**
    * Update task, with uploading two additional properties: stdout and stderr
@@ -146,7 +146,7 @@ App.BackgroundOperationsController = Em.Controller.extend({
     task.Tasks.status = data.Tasks.status;
     task.Tasks.stdout = data.Tasks.stdout;
     task.Tasks.stderr = data.Tasks.stderr;
-    this.set('serviceTimestamp', new Date().getTime());
+    this.set('serviceTimestamp', App.dateTime());
   },
 
   /**
@@ -199,7 +199,7 @@ App.BackgroundOperationsController = Em.Controller.extend({
       }
     });
     self.set("allOperationsCount", runningServices);
-    self.set('serviceTimestamp', new Date().getTime());
+    self.set('serviceTimestamp', App.dateTime());
   },
 
   /**

+ 30 - 0
ambari-web/app/controllers/global/cluster_controller.js

@@ -93,6 +93,36 @@ App.ClusterController = Em.Controller.extend({
     this.set('isLoaded', true);
   },
 
+  /**
+   * load current server clock in milli-seconds
+   */
+  loadClientServerClockDistance: function () {
+    var dfd = $.Deferred();
+    this.getServerClock().done(function () {
+      dfd.resolve();
+    });
+    return dfd.promise();
+  },
+
+  getServerClock: function(){
+    return App.ajax.send({
+      name: 'ambari.service.load_server_clock',
+      sender: this,
+      success: 'getServerClockSuccessCallback',
+      error: 'getServerClockErrorCallback'
+    });
+  },
+  getServerClockSuccessCallback: function (data) {
+    var clientClock = new Date().getTime();
+    var serverClock = (data.RootServiceComponents.server_clock).toString();
+    serverClock = serverClock.length < 13? serverClock+ '000': serverClock;
+    App.set('clockDistance', serverClock - clientClock );
+    console.log('loading ambari server clock distance');
+  },
+  getServerClockErrorCallback: function () {
+    console.log('Cannot load ambari server clock');
+  },
+
   getUrl:function (testUrl, url) {
     return (App.testMode) ? testUrl : App.apiPrefix + '/clusters/' + this.get('clusterName') + url;
   },

+ 1 - 1
ambari-web/app/controllers/main/admin/highAvailability/progress_controller.js

@@ -124,7 +124,7 @@ App.HighAvailabilityProgressPageController = App.HighAvailabilityWizardControlle
       );
     });
     this.get('tasks').findProperty('id', taskId).set('hosts', hosts);
-    this.set('serviceTimestamp', new Date().getTime());
+    this.set('serviceTimestamp', App.dateTime());
   },
 
   retryTask: function () {

+ 1 - 1
ambari-web/app/controllers/main/admin/security/security_progress_controller.js

@@ -77,7 +77,7 @@ App.MainAdminSecurityProgressController = Em.Controller.extend({
         services.push(newService);
       }
     });
-    this.set('serviceTimestamp', new Date().getTime());
+    this.set('serviceTimestamp', App.dateTime());
   }.observes('stages.@each.polledData'),
 
   loadStages: function () {

+ 1 - 1
ambari-web/app/controllers/main/apps_controller.js

@@ -220,7 +220,7 @@ App.MainAppsController = Em.ArrayController.extend({
         min:"",
         max:""
       };
-      var nowTime = new Date().getTime();
+      var nowTime = App.dateTime();
 
       switch (value){
         case 'Any':

+ 1 - 1
ambari-web/app/controllers/wizard.js

@@ -302,7 +302,7 @@ App.WizardController = Em.Controller.extend({
   },
 
   installServicesSuccessCallback: function (jsonData) {
-    var installStartTime = new Date().getTime();
+    var installStartTime = App.dateTime();
     console.log("TRACE: In success function for the installService call");
     if (jsonData) {
       var requestId = jsonData.Requests.id;

+ 1 - 1
ambari-web/app/controllers/wizard/stack_upgrade/step3_controller.js

@@ -357,7 +357,7 @@ App.StackUpgradeStep3Controller = Em.Controller.extend({
         }
       }, this);
     }
-    this.set('serviceTimestamp', new Date().getTime());
+    this.set('serviceTimestamp', App.dateTime());
     return continuePolling;
   },
   /**

+ 3 - 3
ambari-web/app/controllers/wizard/step3_controller.js

@@ -382,7 +382,7 @@ App.WizardStep3Controller = Em.Controller.extend({
 
   startRegistration: function () {
     if (this.get('registrationStartedAt') == null) {
-      this.set('registrationStartedAt', new Date().getTime());
+      this.set('registrationStartedAt', App.dateTime());
       console.log('registration started at ' + this.get('registrationStartedAt'));
       this.isHostsRegistered();
     }
@@ -439,7 +439,7 @@ App.WizardStep3Controller = Em.Controller.extend({
           _host.set('bootStatus', 'REGISTERING');
           _host.set('bootLog', (_host.get('bootLog') != null ? _host.get('bootLog') : '') + Em.I18n.t('installer.step3.hosts.bootLog.registering'));
           // update registration timestamp so that the timeout is computed from the last host that finished bootstrapping
-          this.set('registrationStartedAt', new Date().getTime());
+          this.set('registrationStartedAt', App.dateTime());
           stopPolling = false;
           break;
         case 'REGISTERING':
@@ -462,7 +462,7 @@ App.WizardStep3Controller = Em.Controller.extend({
 
     if (stopPolling) {
       this.getHostInfo();
-    } else if (hosts.someProperty('bootStatus', 'RUNNING') || new Date().getTime() - this.get('registrationStartedAt') < this.get('registrationTimeoutSecs') * 1000) {
+    } else if (hosts.someProperty('bootStatus', 'RUNNING') || App.dateTime() - this.get('registrationStartedAt') < this.get('registrationTimeoutSecs') * 1000) {
       // we want to keep polling for registration status if any of the hosts are still bootstrapping (so we check for RUNNING).
       var self = this;
       window.setTimeout(function () {

+ 1 - 1
ambari-web/app/controllers/wizard/step9_controller.js

@@ -602,7 +602,7 @@ App.WizardStep9Controller = Em.Controller.extend({
       };
       if (this.isSuccess(polledData)) {
         clusterStatus.status = 'STARTED';
-        var serviceStartTime = new Date().getTime();
+        var serviceStartTime = App.dateTime();
         clusterStatus.installTime = ((parseInt(serviceStartTime) - parseInt(this.get('content.cluster.installStartTime'))) / 60000).toFixed(2);
       } else {
         clusterStatus.status = 'START FAILED'; // 'START FAILED' implies to step10 that installation was successful but start failed

+ 1 - 1
ambari-web/app/models/alert.js

@@ -100,7 +100,7 @@ App.Alert = Em.Object.extend({
   }.property('date', 'status'),
   
   makeTimeAtleastMinuteAgo: function(d){
-    var diff = new Date().getTime() - d.getTime();
+    var diff = App.dateTime() - d.getTime();
     if (diff < 60000) {
       diff = 60000 - diff;
       return new Date(d.getTime() - diff );

+ 1 - 1
ambari-web/app/models/user.js

@@ -162,7 +162,7 @@ App.CreateUserForm = App.Form.extend({
     });
 
     if (this.get('className')) {
-      App.store.load(this.get('className'), (new Date()).getTime(), formValues);
+      App.store.load(this.get('className'), App.dateTime(), formValues);
     }
     else {
       console.log("Please define class name for your form " + this.constructor);

+ 3 - 1
ambari-web/app/routes/main.js

@@ -26,7 +26,9 @@ module.exports = Em.Route.extend({
     console.log('in /main:enter');
     if (router.getAuthenticated()) {
       App.router.get('clusterController').loadClusterName(false);
-      router.get('mainController').initialize();
+      App.router.get('clusterController').loadClientServerClockDistance().done(function() {
+        router.get('mainController').initialize();
+      });
       // TODO: redirect to last known state
     } else {
       Ember.run.next(function () {

+ 3 - 0
ambari-web/app/utils/ajax.js

@@ -1321,6 +1321,9 @@ var urls = {
     'real': '/services/AMBARI/components/AMBARI_SERVER',
     'mock': ''
   },
+  'ambari.service.load_server_clock': {
+    'real': '/services/AMBARI/components/AMBARI_SERVER?fields=RootServiceComponents/server_clock'
+  },
   'dashboard.get.user_pref': {
     'real': '/persist/{key}',
     'mock': '',

+ 9 - 5
ambari-web/app/utils/date.js

@@ -17,6 +17,7 @@
  */
 
 var validator = require('utils/validator');
+var App = require('app');
 
 module.exports = {
   dateMonths:['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'],
@@ -53,7 +54,7 @@ module.exports = {
     return date.toDateString();
   },
   /**
-   * Convert starTimestamp to 'DAY_OF_THE_WEEK, MONTH DAY, YEAR HOURS:MINUTES, lasted for DURATION', except for the case: year equals 1969
+   * Convert starTimestamp to 'DAY_OF_THE_WEEK, MONTH DAY, YEAR HOURS:MINUTES', except for the case: year equals 1969
    * @param startTimestamp
    * @return string startTimeSummary
    */
@@ -88,13 +89,16 @@ module.exports = {
     var durationSummary = '';
     var startDate = new Date(startTimestamp);
     var endDate = new Date(endTimestamp);
+    var self = this;
     if (startDate.getFullYear() == 1969 || startTimestamp < 1) {
       return '';
     }
     if (endDate.getFullYear() != 1969 && endTimestamp > 0) {
-      durationSummary = '' + this.timingFormat(endTimestamp - startTimestamp, 1); //lasted for xx secs
+      return '' + this.timingFormat(endTimestamp - startTimestamp, 1); //lasted for xx secs
     } else {
-      durationSummary = '' + this.timingFormat(new Date().getTime() - startTimestamp, 1);
+      // still running, duration till now
+      var time =  (App.dateTime() - startTimestamp) < 0? 0 : (App.dateTime() - startTimestamp) ;
+      durationSummary = '' + this.timingFormat( time , 1);
     }
     return durationSummary;
   },
@@ -117,7 +121,7 @@ module.exports = {
    */
   timingFormat:function (time, /* optional */ zeroValid) {
     var intTime  = parseInt(time);
-    if (zeroValid && intTime == 0) return 0 + '';
+    if (zeroValid && intTime == 0) return 0 + ' secs';
     if (!intTime) return null;
     var timeStr = intTime.toString();
     var lengthOfNumber = timeStr.length;
@@ -154,7 +158,7 @@ module.exports = {
     var duration = 0;
     if (startTime && startTime > 0) {
       if (!endTime || endTime < 1) {
-        endTime = new Date().getTime();
+        endTime = App.dateTime();
       }
       duration = endTime - startTime;
     }

+ 9 - 0
ambari-web/app/utils/helper.js

@@ -296,6 +296,15 @@ App.tooltip = function(self, options) {
   });
 };
 
+/**
+ * wrapper to Date().getTime()
+ * fix issue when client clock and server clock not sync
+ * @return timeStamp of current server clock
+ */
+App.dateTime = function() {
+  return new Date().getTime() + App.clockDistance;
+};
+
 /*
  * Helper function for bound property helper registration
  * @params name {String} - name of helper

+ 1 - 1
ambari-web/app/utils/http_client.js

@@ -58,7 +58,7 @@ App.HttpClient = Em.Object.create({
     }
 
     var xhr = new XMLHttpRequest();
-    var curTime = new Date().getTime();
+    var curTime = App.dateTime();
 
     xhr.open('GET', url + (url.indexOf('?') >= 0 ? '&_=' : '?_=') + curTime, true);
     xhr.send(null);

+ 4 - 4
ambari-web/app/views/common/chart/linear_time.js

@@ -162,7 +162,7 @@ App.ChartLinearTimeView = Ember.View.extend({
   },
 
   getDataForAjaxRequest: function() {
-    var toSeconds = Math.round(new Date().getTime() / 1000);
+    var toSeconds = Math.round(App.dateTime() / 1000);
     var hostName = (this.get('content')) ? this.get('content.hostName') : "";
 
     var HDFSService = App.HDFSService.find().objectAt(0);
@@ -262,8 +262,8 @@ App.ChartLinearTimeView = Ember.View.extend({
         // Same number applies to all time.
         var number = seriesData;
         seriesData = [];
-        seriesData.push([number, new Date().getTime()-(60*60)]);
-        seriesData.push([number, new Date().getTime()]);
+        seriesData.push([number, App.dateTime()-(60*60)]);
+        seriesData.push([number, App.dateTime()]);
       }
       // We have valid data
       var series = {};
@@ -416,7 +416,7 @@ App.ChartLinearTimeView = Ember.View.extend({
    * @param {Array} data
    */
   dataShiftFix: function(data) {
-    var nowTime = Math.round(new Date().getTime() / 1000);
+    var nowTime = Math.round(App.dateTime() / 1000);
     data.forEach(function(series){
       var l = series.data.length;
       var shiftDiff = nowTime - series.data[l - 1].x;

+ 3 - 3
ambari-web/app/views/common/configs/services_config.js

@@ -918,7 +918,7 @@ App.ServiceConfigCapacityScheduler = App.ServiceConfigsByCategoryView.extend({
     }, this);
     adminConfig.set('value', admin.join(' '));
     submitConfig.set('value', submit.join(' '));
-    this.set('queueObserver', new Date().getTime());
+    this.set('queueObserver', App.dateTime());
   },
   /**
    * delete queue
@@ -935,7 +935,7 @@ App.ServiceConfigCapacityScheduler = App.ServiceConfigsByCategoryView.extend({
         i--;
       }
     }
-    this.set('queueObserver', new Date().getTime());
+    this.set('queueObserver', App.dateTime());
   },
   /**
    * save changes that was made to queue
@@ -966,7 +966,7 @@ App.ServiceConfigCapacityScheduler = App.ServiceConfigsByCategoryView.extend({
         _config.set('name', configName.replace(queueNamePrefix + queue.name, queueNamePrefix + queue.configs.findProperty('name', 'queueName').get('value')));
       }
     }, this);
-    this.set('queueObserver', new Date().getTime());
+    this.set('queueObserver', App.dateTime());
   },
   pieChart: App.ChartPieView.extend({
     w: 200,

+ 4 - 1
ambari-web/app/views/common/filter_view.js

@@ -25,6 +25,9 @@
  * All inner views implemented below this view.
  * @type {*}
  */
+
+var App = require('app');
+
 var wrapperView = Ember.View.extend({
   classNames: ['view-wrapper'],
   layout: Ember.Handlebars.compile('<a href="#" {{action "clearFilter" target="view"}} class="ui-icon ui-icon-circle-close"></a> {{yield}}'),
@@ -368,7 +371,7 @@ module.exports = {
       case 'date':
         return function (rowValue, rangeExp) {
           var match = false;
-          var timePassed = new Date().getTime() - rowValue;
+          var timePassed = App.dateTime() - rowValue;
           switch (rangeExp) {
             case 'Past 1 hour':
               match = timePassed <= 3600000;

+ 2 - 2
ambari-web/app/views/main/dashboard/service/hbase.js

@@ -115,7 +115,7 @@ App.MainDashboardServiceHbaseView = App.MainDashboardServiceView.extend({
   masterStartedTime: function () {
     var uptime = this.get('service').get('masterStartTime');
     if (uptime && uptime > 0) {
-      var diff = (new Date()).getTime() - uptime;
+      var diff = App.dateTime() - uptime;
       if (diff < 0) {
         diff = 0;
       }
@@ -128,7 +128,7 @@ App.MainDashboardServiceHbaseView = App.MainDashboardServiceView.extend({
   masterActivatedTime: function () {
     var uptime = this.get('service').get('masterActiveTime');
     if (uptime && uptime > 0) {
-      var diff = (new Date()).getTime() - uptime;
+      var diff = App.dateTime() - uptime;
       if (diff < 0) {
         diff = 0;
       }

+ 1 - 1
ambari-web/app/views/main/dashboard/service/hdfs.js

@@ -101,7 +101,7 @@ App.MainDashboardServiceHdfsView = App.MainDashboardServiceView.extend({
   nodeUptime: function () {
     var uptime = this.get('service').get('nameNodeStartTime');
     if (uptime && uptime > 0){
-      var diff = (new Date()).getTime() - uptime;
+      var diff = App.dateTime() - uptime;
       if (diff < 0) {
         diff = 0;
       }

+ 1 - 1
ambari-web/app/views/main/dashboard/service/mapreduce.js

@@ -35,7 +35,7 @@ App.MainDashboardServiceMapreduceView = App.MainDashboardServiceView.extend({
   jobTrackerUptime: function () {
     var uptime = this.get('service').get('jobTrackerStartTime');
     if (uptime && uptime > 0){
-      var diff = (new Date()).getTime() - uptime;
+      var diff = App.dateTime() - uptime;
       if (diff < 0) {
         diff = 0;
       }

+ 1 - 1
ambari-web/app/views/main/dashboard/service/yarn.js

@@ -55,7 +55,7 @@ App.MainDashboardServiceYARNView = App.MainDashboardServiceView.extend({
   nodeUptime: function () {
     var uptime = this.get('service').get('resourceManagerStartTime');
     if (uptime && uptime > 0){
-      var diff = (new Date()).getTime() - uptime;
+      var diff = App.dateTime() - uptime;
       if (diff < 0) {
         diff = 0;
       }

+ 1 - 1
ambari-web/app/views/main/dashboard/widgets/uptime_text_widget.js

@@ -96,7 +96,7 @@ App.UptimeTextDashboardWidgetView = App.TextDashboardWidgetView.extend({
 
   uptimeProcessing: function(uptime) {
     var uptimeString = this.timeConverter(uptime);
-    var diff = (new Date()).getTime() - uptime;
+    var diff = App.dateTime() - uptime;
     if (diff < 0) {
       diff = 0;
     }

+ 6 - 1
ambari-web/vendor/scripts/jquery.timeago.js

@@ -139,13 +139,18 @@
   }
 
   function inWords(date) {
-    return $t.inWords(distance(date));
+    return $t.inWords(distanceToServerClock(date));
   }
 
   function distance(date) {
     return (new Date().getTime() - date.getTime());
   }
 
+  function distanceToServerClock(date) {
+    var App = require('app');
+    return (App.dateTime() - date.getTime());
+  }
+
   // fix for IE6 suckage
   document.createElement("abbr");
   document.createElement("time");