Bug 1539166 - Trigger periodic prio ping once a day r=chutten
authorJan-Erik Rediger <jrediger@mozilla.com>
Fri, 05 Apr 2019 16:17:06 +0000
changeset 468194 16819d232d4dd193f91831142723617babe71e68
parent 468193 42c3f90105c16beee971c0e9421f5fb974894a78
child 468195 b4258c7fb49b5120a95d35d600d8ba59cf5a7c1a
push id35822
push usershindli@mozilla.com
push dateFri, 05 Apr 2019 21:47:45 +0000
treeherdermozilla-central@98064c475d2f [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewerschutten
bugs1539166
milestone68.0a1
first release with
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
last release without
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
Bug 1539166 - Trigger periodic prio ping once a day r=chutten Depends on D26152 Differential Revision: https://phabricator.services.mozilla.com/D26153
toolkit/components/telemetry/app/TelemetryScheduler.jsm
toolkit/components/telemetry/app/TelemetryUtils.jsm
toolkit/components/telemetry/docs/data/prio-ping.rst
toolkit/components/telemetry/docs/internals/preferences.rst
toolkit/components/telemetry/pings/PrioPing.jsm
toolkit/components/telemetry/tests/unit/test_PrioPing.js
--- a/toolkit/components/telemetry/app/TelemetryScheduler.jsm
+++ b/toolkit/components/telemetry/app/TelemetryScheduler.jsm
@@ -11,16 +11,17 @@ var EXPORTED_SYMBOLS = [
 const {Log} = ChromeUtils.import("resource://gre/modules/Log.jsm");
 const {Services} = ChromeUtils.import("resource://gre/modules/Services.jsm");
 const {TelemetrySession} = ChromeUtils.import("resource://gre/modules/TelemetrySession.jsm");
 const {TelemetryUtils} = ChromeUtils.import("resource://gre/modules/TelemetryUtils.jsm");
 const {XPCOMUtils} = ChromeUtils.import("resource://gre/modules/XPCOMUtils.jsm", this);
 const {clearTimeout, setTimeout} = ChromeUtils.import("resource://gre/modules/Timer.jsm");
 // Other pings
 const {EcosystemTelemetry} = ChromeUtils.import("resource://gre/modules/EcosystemTelemetry.jsm");
+const {TelemetryPrioPing} = ChromeUtils.import("resource://gre/modules/PrioPing.jsm");
 
 XPCOMUtils.defineLazyServiceGetters(this, {
   idleService: ["@mozilla.org/widget/idleservice;1", "nsIIdleService"],
 });
 
 const MIN_SUBSESSION_LENGTH_MS = Services.prefs.getIntPref("toolkit.telemetry.minSubsessionLength", 5 * 60) * 1000;
 
 const LOGGER_NAME = "Toolkit.Telemetry";
@@ -333,16 +334,17 @@ var TelemetryScheduler = {
     // Check if other regular pings are due.
     const shouldSendPeriodic = this._isPeriodicPingDue(nowDate);
 
     if (shouldSendPeriodic) {
       this._log.trace("_schedulerTickLogic - Periodic ping due.");
       this._lastPeriodicPingTime = now;
       // Send other pings.
       EcosystemTelemetry.periodicPing();
+      TelemetryPrioPing.periodicPing();
     }
 
     if (shouldSendDaily) {
       this._log.trace("_schedulerTickLogic - Daily ping due.");
       this._lastDailyPingTime = now;
       return TelemetrySession.sendDailyPing();
     }
 
--- a/toolkit/components/telemetry/app/TelemetryUtils.jsm
+++ b/toolkit/components/telemetry/app/TelemetryUtils.jsm
@@ -57,17 +57,16 @@ var TelemetryUtils = {
     EventPingMinimumFrequency: "toolkit.telemetry.eventping.minimumFrequency",
     EventPingMaximumFrequency: "toolkit.telemetry.eventping.maximumFrequency",
 
     // Ecosystem Telemetry Preferences
     EcosystemTelemetryEnabled: "toolkit.telemetry.ecosystemtelemetry.enabled",
 
     // Prio Ping Preferences
     PrioPingEnabled: "toolkit.telemetry.prioping.enabled",
-    PrioPingFrequency: "toolkit.telemetry.prioping.frequency",
     PrioPingDataLimit: "toolkit.telemetry.prioping.dataLimit",
 
     // Log Preferences
     LogLevel: "toolkit.telemetry.log.level",
     LogDump: "toolkit.telemetry.log.dump",
 
     // Data reporting Preferences
     AcceptedPolicyDate: "datareporting.policy.dataSubmissionPolicyNotifiedTime",
--- a/toolkit/components/telemetry/docs/data/prio-ping.rst
+++ b/toolkit/components/telemetry/docs/data/prio-ping.rst
@@ -25,17 +25,17 @@ The :doc:`Telemetry Environment <../data
 
 Scheduling
 ----------
 
 The ping is submitted at least once a day for sessions that last longer than 24h.
 The ping is immediately sent if the ``prioData`` array has 10 elements.
 The ping is submitted on shutdown.
 
-Whether the ping is enabled, the 10-element limit, and the once-a-day frequency are controlled by :doc:`preferences <../internals/preferences>`.
+Whether the ping is enabled and the 10-element limit are controlled by :doc:`preferences <../internals/preferences>`.
 
 Field details
 -------------
 
 reason
 ~~~~~~
 The ``reason`` field contains the information about why the "prio" ping was submitted:
 
--- a/toolkit/components/telemetry/docs/internals/preferences.rst
+++ b/toolkit/components/telemetry/docs/internals/preferences.rst
@@ -179,21 +179,16 @@ Preferences
   sending them to the parent process.
   Default is 2000 (milliseconds).
 
 ``toolkit.telemetry.prioping.enabled``
 
   Whether the :doc:`../data/prio-ping` is enabled.
   Defaults to false except on Firefox Nightly where it is true. Change requires restart.
 
-``toolkit.telemetry.prioping.frequency``
-
-  The default frequency at which we send the :doc:`../data/prio-ping`.
-  Default is 24 (hours).
-
 ``toolkit.telemetry.prioping.dataLimit``
 
   The number of encoded prio payloads which triggers an immediate :doc:`../data/prio-ping` with reason "max".
   Default is 10 payloads.
 
 Data-choices notification
 -------------------------
 
--- a/toolkit/components/telemetry/pings/PrioPing.jsm
+++ b/toolkit/components/telemetry/pings/PrioPing.jsm
@@ -19,37 +19,30 @@ XPCOMUtils.defineLazyModuleGetters(this,
   TelemetryController: "resource://gre/modules/TelemetryController.jsm",
   Log: "resource://gre/modules/Log.jsm",
 });
 
 XPCOMUtils.defineLazyServiceGetters(this, {
   Telemetry: ["@mozilla.org/base/telemetry;1", "nsITelemetry"],
 });
 
-const {setTimeout, clearTimeout} = ChromeUtils.import("resource://gre/modules/Timer.jsm");
 const {TelemetryUtils} = ChromeUtils.import("resource://gre/modules/TelemetryUtils.jsm");
 const {Services} = ChromeUtils.import("resource://gre/modules/Services.jsm");
 
 const Utils = TelemetryUtils;
 
-const MILLISECONDS_PER_HOUR = 60 * 60 * 1000;
-
-const DEFAULT_PING_FREQUENCY_HOURS = 24;
-
 const LOGGER_NAME = "Toolkit.Telemetry";
 const LOGGER_PREFIX = "TelemetryPrioPing";
 
 // Triggered from native Origin Telemetry storage.
 const PRIO_LIMIT_REACHED_TOPIC = "origin-telemetry-storage-limit-reached";
 
 const PRIO_PING_VERSION = "1";
 
 var Policy = {
-  setTimeout: (callback, delayMs) => setTimeout(callback, delayMs),
-  clearTimeout: (id) => clearTimeout(id),
   sendPing: (type, payload, options) => TelemetryController.submitExternalPing(type, payload, options),
   getEncodedOriginSnapshot: async (aClear) => Telemetry.getEncodedOriginSnapshot(aClear),
 };
 
 var TelemetryPrioPing = {
   Reason: Object.freeze({
     PERIODIC: "periodic", // Sent the ping containing Origin Telemetry from the past periodic interval (default 24h).
     MAX: "max",           // Sent the ping containing at least the maximum number (default 10) of prioData elements, earlier than the periodic interval.
@@ -65,68 +58,50 @@ var TelemetryPrioPing = {
   startup() {
     if (!this._testing && !Services.prefs.getBoolPref(Utils.Preferences.PrioPingEnabled, false)) {
       this._log.trace("Prio ping disabled.");
       return;
     }
     this._log.trace("Starting up.");
 
     Services.obs.addObserver(this, PRIO_LIMIT_REACHED_TOPIC);
-
-    XPCOMUtils.defineLazyPreferenceGetter(this, "pingFrequency",
-                                          Utils.Preferences.PrioPingFrequency,
-                                          DEFAULT_PING_FREQUENCY_HOURS);
-    this._startTimer();
   },
 
   async shutdown() {
     this._log.trace("Shutting down.");
     // removeObserver may throw, which could interrupt shutdown.
     try {
       Services.obs.removeObserver(this, PRIO_LIMIT_REACHED_TOPIC);
     } catch (ex) {}
 
     await this._submitPing(this.Reason.SHUTDOWN);
-    this._clearTimer();
   },
 
   observe(aSubject, aTopic, aData) {
     switch (aTopic) {
       case PRIO_LIMIT_REACHED_TOPIC:
         this._log.trace("prio limit reached");
         this._submitPing(this.Reason.MAX);
         break;
     }
   },
 
-  _startTimer(delay = this.pingFrequency * MILLISECONDS_PER_HOUR, reason = this.Reason.PERIODIC, discardLeftovers = false) {
-    this._clearTimer();
-    this._timeoutId =
-      Policy.setTimeout(() => TelemetryPrioPing._submitPing(reason), delay);
-  },
-
-  _clearTimer() {
-    if (this._timeoutId) {
-      Policy.clearTimeout(this._timeoutId);
-      this._timeoutId = null;
-    }
+  periodicPing() {
+    this._log.trace("periodic ping triggered");
+    this._submitPing(this.Reason.PERIODIC);
   },
 
   /**
    * Submits an "prio" ping and restarts the timer for the next interval.
    *
    * @param {String} reason The reason we're sending the ping. One of TelemetryPrioPing.Reason.
    */
   async _submitPing(reason) {
     this._log.trace("_submitPing");
 
-    if (reason !== this.Reason.SHUTDOWN) {
-      this._startTimer();
-    }
-
     let snapshot = await Policy.getEncodedOriginSnapshot(true /* clear */);
 
     if (!this._testing) {
       snapshot = snapshot.filter(({encoding}) => !encoding.startsWith("telemetry.test"));
     }
 
     if (snapshot.length === 0) {
       // Don't send a ping if we haven't anything to send
@@ -148,17 +123,16 @@ var TelemetryPrioPing = {
 
     Policy.sendPing(this.PRIO_PING_TYPE, payload, options);
   },
 
   /**
    * Test-only, restore to initial state.
    */
   testReset() {
-    this._clearTimer();
     this._testing = true;
   },
 
   get _log() {
     if (!this._logger) {
       this._logger = Log.repository.getLoggerWithMessagePrefix(LOGGER_NAME, LOGGER_PREFIX + "::");
     }
 
--- a/toolkit/components/telemetry/tests/unit/test_PrioPing.js
+++ b/toolkit/components/telemetry/tests/unit/test_PrioPing.js
@@ -75,32 +75,23 @@ add_task(async function test_limit_reach
   fakePolicy(pass, pass, (type, payload, options) => {
     checkPingStructure(type, payload, options);
     Assert.equal(payload.reason, TelemetryPrioPing.Reason.MAX, "Sent using max reason.");
   }, fakeSnapshot);
   Services.obs.notifyObservers(null, "origin-telemetry-storage-limit-reached");
 });
 
 add_task(async function test_periodic() {
-  // First fake a "max"-reason ping to allow us to control when the timer fires.
-  let fireTimerNow;
-  fakePolicy(callback => fireTimerNow = callback, pass, (type, payload, options) => {
+  fakePolicy(pass, pass, (type, payload, options) => {
     checkPingStructure(type, payload, options);
+    Assert.equal(payload.reason, TelemetryPrioPing.Reason.PERIODIC, "Sent with periodic reason.");
+  }, fakeSnapshot);
 
-    // And now, set up the actual test:
-    fakePolicy(pass, pass, (type2, payload2, options2) => {
-      checkPingStructure(type2, payload2, options2);
-      Assert.equal(payload2.reason, TelemetryPrioPing.Reason.PERIODIC, "Sent with periodic reason.");
-    }, fakeSnapshot);
-
-    // Now fire the timer. This is re-entrant, which is weird, but then
-    // so is this test.
-    fireTimerNow();
-  }, fakeSnapshot);
-  Services.obs.notifyObservers(null, "origin-telemetry-storage-limit-reached");
+  // This is normally triggered by the scheduler once a day
+  TelemetryPrioPing.periodicPing();
 });
 
 add_task(async function test_shutdown() {
   fakePolicy(fail, pass, (type, payload, options) => {
     checkPingStructure(type, payload, options);
     Assert.equal(payload.reason, TelemetryPrioPing.Reason.SHUTDOWN, "Sent with shutdown reason.");
   }, fakeSnapshot);
   await TelemetryPrioPing.shutdown();