Skip to content

Commit

Permalink
add new Configuration parameter, fix RLB ping tests
Browse files Browse the repository at this point in the history
  • Loading branch information
chutten committed May 5, 2021
1 parent 78d0be2 commit 08ffed7
Show file tree
Hide file tree
Showing 13 changed files with 59 additions and 20 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

* Rust
* Don't return a result from `submit_ping`. The boolean return value indicates whether a ping was submitted ([#1613](https:/mozilla/glean/pull/1613))
* **Breaking Change**: Glean now schedules "metrics" pings, accepting a new Configuration parameter. ([#1599](https:/mozilla/glean/pull/1599))

# v37.0.0 (2021-04-30)

Expand Down
1 change: 1 addition & 0 deletions glean-core/rlb/examples/prototype.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ fn main() {
channel: None,
server_endpoint: Some("invalid-test-host".into()),
uploader: None,
use_core_mps: true,
};

let client_info = ClientInfoMetrics {
Expand Down
1 change: 1 addition & 0 deletions glean-core/rlb/src/common_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ pub(crate) fn new_glean(
channel: Some("testing".into()),
server_endpoint: Some("invalid-test-host".into()),
uploader: None,
use_core_mps: false,
},
};

Expand Down
2 changes: 2 additions & 0 deletions glean-core/rlb/src/configuration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,4 +30,6 @@ pub struct Configuration {
pub server_endpoint: Option<String>,
/// The instance of the uploader used to send pings.
pub uploader: Option<Box<dyn PingUploader + 'static>>,
/// Whether Glean should schedule "metrics" pings for you.
pub use_core_mps: bool,
}
14 changes: 6 additions & 8 deletions glean-core/rlb/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
//! channel: None,
//! server_endpoint: None,
//! uploader: None,
//! use_core_mps: false,
//! };
//! glean::initialize(cfg, ClientInfoMetrics::unknown());
//!
Expand Down Expand Up @@ -188,7 +189,7 @@ pub fn initialize(cfg: Configuration, client_info: ClientInfoMetrics) {
max_events: cfg.max_events,
delay_ping_lifetime_io: cfg.delay_ping_lifetime_io,
app_build: client_info.app_build.clone(),
use_core_mps: true,
use_core_mps: cfg.use_core_mps,
};

let glean = match Glean::new(core_cfg) {
Expand Down Expand Up @@ -304,7 +305,7 @@ pub fn initialize(cfg: Configuration, client_info: ClientInfoMetrics) {
// Set up information and scheduling for Glean owned pings. Ideally, the "metrics"
// ping startup check should be performed before any other ping, since it relies
// on being dispatched to the API context before any other metric.
// TODO: start the metrics ping scheduler, will happen in bug 1672951.
glean.start_metrics_ping_scheduler();

// Check if the "dirty flag" is set. That means the product was probably
// force-closed. If that's the case, submit a 'baseline' ping with the
Expand Down Expand Up @@ -363,7 +364,7 @@ pub fn shutdown() {
}

crate::launch_with_glean_mut(|glean| {
glean_core::cancel_metrics_ping_scheduler();
glean.cancel_metrics_ping_scheduler();
glean.set_dirty_flag(false);
});

Expand Down Expand Up @@ -464,16 +465,15 @@ pub fn set_upload_enabled(enabled: bool) {
let old_enabled = glean.is_upload_enabled();
glean.set_upload_enabled(enabled);

// TODO: Cancel upload and any outstanding metrics ping scheduler
// task. Will happen on bug 1672951.

if !old_enabled && enabled {
glean.start_metrics_ping_scheduler();
// If uploading is being re-enabled, we have to restore the
// application-lifetime metrics.
initialize_core_metrics(&glean, &state.client_info, state.channel.clone());
}

if old_enabled && !enabled {
glean.cancel_metrics_ping_scheduler();
// If uploading is disabled, we need to send the deletion-request ping:
// note that glean-core takes care of generating it.
state.upload_manager.trigger_upload();
Expand Down Expand Up @@ -684,8 +684,6 @@ pub(crate) fn destroy_glean(clear_stores: bool) {
pub fn test_reset_glean(cfg: Configuration, client_info: ClientInfoMetrics, clear_stores: bool) {
destroy_glean(clear_stores);

// Always log pings for tests
//Glean.setLogPings(true)
initialize(cfg, client_info);
}

Expand Down
19 changes: 19 additions & 0 deletions glean-core/rlb/src/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ fn send_a_ping() {
channel: Some("testing".into()),
server_endpoint: Some("invalid-test-host".into()),
uploader: Some(Box::new(FakeUploader { sender: s })),
use_core_mps: false,
};

let _t = new_glean(Some(cfg), true);
Expand Down Expand Up @@ -155,6 +156,7 @@ fn test_experiments_recording_before_glean_inits() {
channel: Some("testing".into()),
server_endpoint: Some("invalid-test-host".into()),
uploader: None,
use_core_mps: false,
},
ClientInfoMetrics::unknown(),
false,
Expand Down Expand Up @@ -215,6 +217,7 @@ fn sending_of_foreground_background_pings() {
channel: Some("testing".into()),
server_endpoint: Some("invalid-test-host".into()),
uploader: Some(Box::new(FakeUploader { sender: s })),
use_core_mps: false,
};

let _t = new_glean(Some(cfg), true);
Expand Down Expand Up @@ -299,6 +302,7 @@ fn sending_of_startup_baseline_ping() {
channel: Some("testing".into()),
server_endpoint: Some("invalid-test-host".into()),
uploader: Some(Box::new(FakeUploader { sender: s })),
use_core_mps: false,
},
ClientInfoMetrics::unknown(),
false,
Expand Down Expand Up @@ -359,6 +363,7 @@ fn no_dirty_baseline_on_clean_shutdowns() {
channel: Some("testing".into()),
server_endpoint: Some("invalid-test-host".into()),
uploader: Some(Box::new(FakeUploader { sender: s })),
use_core_mps: false,
},
ClientInfoMetrics::unknown(),
false,
Expand Down Expand Up @@ -390,6 +395,7 @@ fn initialize_must_not_crash_if_data_dir_is_messed_up() {
channel: Some("testing".into()),
server_endpoint: Some("invalid-test-host".into()),
uploader: None,
use_core_mps: false,
};

test_reset_glean(cfg, ClientInfoMetrics::unknown(), false);
Expand Down Expand Up @@ -453,6 +459,7 @@ fn initializing_twice_is_a_noop() {
channel: Some("testing".into()),
server_endpoint: Some("invalid-test-host".into()),
uploader: None,
use_core_mps: false,
},
ClientInfoMetrics::unknown(),
true,
Expand All @@ -470,6 +477,7 @@ fn initializing_twice_is_a_noop() {
channel: Some("testing".into()),
server_endpoint: Some("invalid-test-host".into()),
uploader: None,
use_core_mps: false,
},
ClientInfoMetrics::unknown(),
false,
Expand Down Expand Up @@ -508,6 +516,7 @@ fn the_app_channel_must_be_correctly_set_if_requested() {
channel: None,
server_endpoint: Some("invalid-test-host".into()),
uploader: None,
use_core_mps: false,
},
ClientInfoMetrics::unknown(),
true,
Expand Down Expand Up @@ -622,6 +631,7 @@ fn sending_deletion_ping_if_disabled_outside_of_run() {
channel: Some("testing".into()),
server_endpoint: Some("invalid-test-host".into()),
uploader: None,
use_core_mps: false,
};

let _t = new_glean(Some(cfg), true);
Expand All @@ -640,6 +650,7 @@ fn sending_deletion_ping_if_disabled_outside_of_run() {
channel: Some("testing".into()),
server_endpoint: Some("invalid-test-host".into()),
uploader: Some(Box::new(FakeUploader { sender: s })),
use_core_mps: false,
},
ClientInfoMetrics::unknown(),
false,
Expand Down Expand Up @@ -687,6 +698,7 @@ fn no_sending_of_deletion_ping_if_unchanged_outside_of_run() {
channel: Some("testing".into()),
server_endpoint: Some("invalid-test-host".into()),
uploader: None,
use_core_mps: false,
};

let _t = new_glean(Some(cfg), true);
Expand All @@ -705,6 +717,7 @@ fn no_sending_of_deletion_ping_if_unchanged_outside_of_run() {
channel: Some("testing".into()),
server_endpoint: Some("invalid-test-host".into()),
uploader: Some(Box::new(FakeUploader { sender: s })),
use_core_mps: false,
},
ClientInfoMetrics::unknown(),
false,
Expand Down Expand Up @@ -770,6 +783,7 @@ fn setting_debug_view_tag_before_initialization_should_not_crash() {
channel: Some("testing".into()),
server_endpoint: Some("invalid-test-host".into()),
uploader: Some(Box::new(FakeUploader { sender: s })),
use_core_mps: false,
};

let _t = new_glean(Some(cfg), true);
Expand Down Expand Up @@ -829,6 +843,7 @@ fn setting_source_tags_before_initialization_should_not_crash() {
channel: Some("testing".into()),
server_endpoint: Some("invalid-test-host".into()),
uploader: Some(Box::new(FakeUploader { sender: s })),
use_core_mps: false,
};

let _t = new_glean(Some(cfg), true);
Expand Down Expand Up @@ -889,6 +904,7 @@ fn setting_source_tags_after_initialization_should_not_crash() {
channel: Some("testing".into()),
server_endpoint: Some("invalid-test-host".into()),
uploader: Some(Box::new(FakeUploader { sender: s })),
use_core_mps: false,
};

let _t = new_glean(Some(cfg), true);
Expand Down Expand Up @@ -961,6 +977,7 @@ fn flipping_upload_enabled_respects_order_of_events() {
channel: Some("testing".into()),
server_endpoint: Some("invalid-test-host".into()),
uploader: Some(Box::new(FakeUploader { sender: s })),
use_core_mps: false,
};

// We create a ping and a metric before we initialize Glean
Expand Down Expand Up @@ -1031,6 +1048,7 @@ fn registering_pings_before_init_must_work() {
channel: Some("testing".into()),
server_endpoint: Some("invalid-test-host".into()),
uploader: Some(Box::new(FakeUploader { sender: s })),
use_core_mps: false,
};

let _t = new_glean(Some(cfg), true);
Expand Down Expand Up @@ -1081,6 +1099,7 @@ fn test_a_ping_before_submission() {
channel: Some("testing".into()),
server_endpoint: Some("invalid-test-host".into()),
uploader: Some(Box::new(FakeUploader { sender: s })),
use_core_mps: false,
};

let _t = new_glean(Some(cfg), true);
Expand Down
1 change: 1 addition & 0 deletions glean-core/rlb/tests/init_fails.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ fn init_fails() {
channel: Some("testing".into()),
server_endpoint: Some("invalid-test-host".into()),
uploader: None,
use_core_mps: false,
};
common::initialize(cfg);

Expand Down
1 change: 1 addition & 0 deletions glean-core/rlb/tests/no_time_to_init.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ fn init_fails() {
channel: Some("testing".into()),
server_endpoint: Some("invalid-test-host".into()),
uploader: None,
use_core_mps: false,
};
common::initialize(cfg);

Expand Down
1 change: 1 addition & 0 deletions glean-core/rlb/tests/overflowing_preinit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ fn overflowing_the_task_queue_records_telemetry() {
channel: Some("testing".into()),
server_endpoint: Some("invalid-test-host".into()),
uploader: None,
use_core_mps: false,
};

// Insert a bunch of tasks to overflow the queue.
Expand Down
2 changes: 2 additions & 0 deletions glean-core/rlb/tests/schema.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ fn new_glean(configuration: Option<Configuration>) -> tempfile::TempDir {
channel: Some("testing".into()),
server_endpoint: Some("invalid-test-host".into()),
uploader: None,
use_core_mps: false,
},
};

Expand Down Expand Up @@ -86,6 +87,7 @@ fn validate_against_schema() {
channel: Some("testing".into()),
server_endpoint: Some("invalid-test-host".into()),
uploader: Some(Box::new(ValidatingUploader { sender: s })),
use_core_mps: false,
};
let _ = new_glean(Some(cfg));

Expand Down
1 change: 1 addition & 0 deletions glean-core/rlb/tests/simple.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ fn simple_lifecycle() {
channel: Some("testing".into()),
server_endpoint: Some("invalid-test-host".into()),
uploader: None,
use_core_mps: false,
};
common::initialize(cfg);

Expand Down
29 changes: 17 additions & 12 deletions glean-core/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -98,12 +98,7 @@ pub fn setup_glean(glean: Glean) -> Result<()> {
// calling `initialize` on the global singleton and further operations check that it has been
// initialized.
if GLEAN.get().is_none() {
if GLEAN.set(Mutex::new(glean)).is_ok() {
let glean = &GLEAN.get().unwrap().lock().unwrap();
if glean.schedule_metrics_pings {
scheduler::schedule(&glean);
}
} else {
if GLEAN.set(Mutex::new(glean)).is_err() {
log::warn!(
"Global Glean object is initialized already. This probably happened concurrently."
)
Expand Down Expand Up @@ -1017,6 +1012,22 @@ impl Glean {
// We don't care about this failing, maybe the data does just not exist.
let _ = self.event_data_store.clear_all();
}

/// Instructs the Metrics Ping Scheduler's thread to exit cleanly.
/// If Glean was configured with `use_core_mps: false`, this has no effect.
pub fn cancel_metrics_ping_scheduler(&self) {
if self.schedule_metrics_pings {
scheduler::cancel();
}
}

/// Instructs the Metrics Ping Scheduler to being scheduling metrics pings.
/// If Glean wsa configured with `use_core_mps: false`, this has no effect.
pub fn start_metrics_ping_scheduler(&self) {
if self.schedule_metrics_pings {
scheduler::schedule(&self);
}
}
}

/// Returns a timestamp corresponding to "now" with millisecond precision.
Expand All @@ -1025,12 +1036,6 @@ pub fn get_timestamp_ms() -> u64 {
zeitstempel::now() / NANOS_PER_MILLI
}

/// Instructs the Metrics Ping Scheduler's thread to exit cleanly.
/// If Glean was configured with `use_core_mps: false`, this has no effect.
pub fn cancel_metrics_ping_scheduler() {
scheduler::cancel();
}

// Split unit tests to a separate file, to reduce the file of this one.
#[cfg(test)]
#[path = "lib_unit_tests.rs"]
Expand Down
6 changes: 6 additions & 0 deletions glean-core/src/scheduler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,12 @@ impl MetricsPingScheduler for GleanMetricsPingScheduler {
pub fn schedule(glean: &Glean) {
let now = local_now_with_offset().0;

let (cancelled_lock, _condvar) = &**TASK_CONDVAR;
if *cancelled_lock.lock().unwrap() {
log::debug!("Told to schedule, but already cancelled. Are we in a test?");
}
*cancelled_lock.lock().unwrap() = false; // Uncancel the thread.

let submitter = GleanMetricsPingSubmitter {};
let scheduler = GleanMetricsPingScheduler {};

Expand Down

0 comments on commit 08ffed7

Please sign in to comment.