Skip to content

Commit 5183342

Browse files
committed
Bug 1848200 - add migration flag to init
Conditionally migrate IDB data only if it already exists. If the project has never used an older version of Glean, then we want to avoid migration because of a potential race condition when submitting pings on first page load.
1 parent cecfb8c commit 5183342

File tree

4 files changed

+25
-9
lines changed

4 files changed

+25
-9
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
[Full changelog](https://github.com/mozilla/glean.js/compare/v2.0.0...main)
44

5+
* [#1751](https://github.com/mozilla/glean.js/pull/1751): Add a migration flag to initialize. If not explicitly set in the `config` object the migration from IndexedDB to LocalStorage will not occur. **The only projects that should ever set this flag are those that have used Glean.js in production with a version <v2.0.0 and have upgraded.**
6+
57
# v2.0.0 (2023-08-03)
68

79
[Full changelog](https://github.com/mozilla/glean.js/compare/v1.4.0...v2.0.0)

glean/src/core/config.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,9 @@ export interface ConfigurationInterface {
5656
readonly osVersion?: string,
5757
// The build date, provided by glean_parser
5858
readonly buildDate?: Date,
59+
// Migrate from legacy storage (IndexedDB) to the new one (LocalStorage).
60+
// This should only be true for older projects that have existing data in IndexedDB.
61+
readonly migrateFromLegacyStorage?: boolean,
5962
}
6063

6164
// Important: the `Configuration` should only be used internally by the Glean singleton.
@@ -68,6 +71,7 @@ export class Configuration implements ConfigurationInterface {
6871
readonly osVersion?: string;
6972
readonly buildDate?: Date;
7073
readonly maxEvents: number;
74+
readonly migrateFromLegacyStorage?: boolean;
7175

7276
// Debug configuration.
7377
debug: DebugOptions;
@@ -82,6 +86,7 @@ export class Configuration implements ConfigurationInterface {
8286
this.osVersion = config?.osVersion;
8387
this.buildDate = config?.buildDate;
8488
this.maxEvents = config?.maxEvents || DEFAULT_MAX_EVENTS;
89+
this.migrateFromLegacyStorage = config?.migrateFromLegacyStorage;
8590

8691
this.debug = {};
8792

glean/src/core/glean/sync.ts

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,10 @@
22
* License, v. 2.0. If a copy of the MPL was not distributed with this
33
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
44

5-
import { CLIENT_INFO_STORAGE, KNOWN_CLIENT_ID } from "../constants.js";
65
import type { ConfigurationInterface } from "../config.js";
6+
import type PlatformSync from "../../platform/sync.js";
7+
8+
import { CLIENT_INFO_STORAGE, KNOWN_CLIENT_ID } from "../constants.js";
79
import { Configuration } from "../config.js";
810
import PingUploadManager from "../upload/manager/sync.js";
911
import { isBoolean, isString, sanitizeApplicationId } from "../utils.js";
@@ -13,7 +15,6 @@ import { DatetimeMetric } from "../metrics/types/datetime.js";
1315
import CorePings from "../internal_pings.js";
1416
import { registerPluginToEvent } from "../events/utils/sync.js";
1517
import ErrorManagerSync from "../error/sync.js";
16-
import type PlatformSync from "../../platform/sync.js";
1718
import { Lifetime } from "../metrics/lifetime.js";
1819
import { Context } from "../context.js";
1920
import log, { LoggingLevel } from "../log.js";
@@ -42,7 +43,15 @@ namespace Glean {
4243
*/
4344
function onUploadEnabled(): void {
4445
Context.uploadEnabled = true;
45-
(Context.coreMetrics as CoreMetricsSync).initialize();
46+
}
47+
48+
/**
49+
* Initialize core metrics: client_id, first_run_date, os, etc.
50+
*
51+
* @param migrateFromLegacy Whether or not to migrate data from legacy storage.
52+
*/
53+
function initializeCoreMetrics(migrateFromLegacy?: boolean): void {
54+
(Context.coreMetrics as CoreMetricsSync).initialize(migrateFromLegacy);
4655
}
4756

4857
/**
@@ -246,6 +255,7 @@ namespace Glean {
246255
// If upload is enabled,
247256
// just follow the normal code path to instantiate the core metrics.
248257
onUploadEnabled();
258+
initializeCoreMetrics(config?.migrateFromLegacyStorage);
249259
} else {
250260
// If upload is disabled, and we've never run before, only set the
251261
// client_id to KNOWN_CLIENT_ID, but do not send a deletion request
@@ -317,6 +327,7 @@ namespace Glean {
317327
if (Context.uploadEnabled !== flag) {
318328
if (flag) {
319329
onUploadEnabled();
330+
initializeCoreMetrics(Context.config.migrateFromLegacyStorage);
320331
} else {
321332
onUploadDisabled(false);
322333
}

glean/src/core/internal_metrics/sync.ts

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,7 @@ export class CoreMetricsSync {
125125
);
126126
}
127127

128-
initialize(): void {
128+
initialize(migrateFromLegacyStorage?: boolean): void {
129129
// The "sync" version of Glean.js is only meant to be used in the browser.
130130
// If we cannot access the window object, then we are unable to store
131131
// any of the metric data in `localStorage`.
@@ -140,11 +140,9 @@ export class CoreMetricsSync {
140140
// Currently we are interested in migrating two things
141141
// 1. The client_id - consistent clientId across all sessions.
142142
// 2. The first_run_date - the date when the client was first run.
143-
144-
// The migration is done only once per client. The flag is set in
145-
// LocalStorage to indicate that the migration has been completed.
146-
const migrationFlag = localStorage.getItem("GLEAN_MIGRATION_FLAG");
147-
if (migrationFlag !== "1") {
143+
if (!!migrateFromLegacyStorage && localStorage.getItem("GLEAN_MIGRATION_FLAG") !== "1") {
144+
// The migration is done only once per client. The flag is set in
145+
// LocalStorage to indicate that the migration has been completed.
148146
this.migrateCoreMetricsFromIdb();
149147
localStorage.setItem("GLEAN_MIGRATION_FLAG", "1");
150148
} else {

0 commit comments

Comments
 (0)