Skip to content

Commit db99abe

Browse files
authored
VPN-6854: fix data throughput Glean issues (#10296)
* VPN-6854 don't record data transfer metrics too often * android work * linter fix
1 parent 58bf0db commit db99abe

File tree

3 files changed

+41
-43
lines changed

3 files changed

+41
-43
lines changed

android/daemon/src/main/java/org/mozilla/firefox/vpn/daemon/VPNService.kt

Lines changed: 41 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -51,8 +51,8 @@ class VPNService : android.net.VpnService() {
5151
override fun onTick(millisUntilFinished: Long) {
5252
// We want to skip the first onTick, which is called as the timer is started.
5353
// (The Start ping is sent then, so there is no need for a Timer ping.)
54-
var wasTimerJustStarted = (millisUntilFinished == mBackgroundPingTimerMSec)
55-
if (!wasTimerJustStarted && isUsingShortTimerSessionPing && shouldRecordTimerAndEndMetrics) {
54+
var wasTimerJustStarted = (millisUntilFinished >= (mBackgroundPingTimerMSec - 1000))
55+
if (!wasTimerJustStarted && isUsingShortTimerSessionPing && isSuperDooperMetricsActive) {
5656
Log.i(tag, "Sending daemon_timer ping (on short timer debug schedule)")
5757
Pings.daemonsession.submit(
5858
Pings.daemonsessionReasonCodes.daemonTimer,
@@ -61,8 +61,7 @@ class VPNService : android.net.VpnService() {
6161
}
6262
override fun onFinish() {
6363
Log.i(tag, "Sending daemon_timer ping")
64-
if (shouldRecordTimerAndEndMetrics) {
65-
recordDataTransferMetrics()
64+
if (isSuperDooperMetricsActive) {
6665
Pings.daemonsession.submit(
6766
Pings.daemonsessionReasonCodes.daemonTimer,
6867
)
@@ -84,11 +83,6 @@ class VPNService : android.net.VpnService() {
8483
return (this.mConfig?.optInt("reason") ?: 0) == 1
8584
}
8685

87-
private val shouldRecordTimerAndEndMetrics: Boolean
88-
get() {
89-
return isSuperDooperMetricsActive && !isAppChangingServers
90-
}
91-
9286
private val isUsingShortTimerSessionPing: Boolean
9387
get() {
9488
return this.mConfig?.optBoolean("isUsingShortTimerSessionPing", false) ?: false
@@ -325,6 +319,12 @@ class VPNService : android.net.VpnService() {
325319
// now.
326320
if (currentTunnelHandle != -1) {
327321
Log.i(tag, "Currently have a connection, close old handle")
322+
// Record data transfer metrics, as we're switching servers/configs and this data will get reset
323+
// Data metrics must be recorded prior to wgTurnOff, or we can't get data from the config
324+
if (isSuperDooperMetricsActive) {
325+
recordDataTransferMetrics()
326+
}
327+
328328
wgTurnOff(currentTunnelHandle)
329329
}
330330
currentTunnelHandle = wgTurnOn("mvpn0", tun.detachFd(), wgConfig)
@@ -338,12 +338,14 @@ class VPNService : android.net.VpnService() {
338338
protect(wgGetSocketV6(currentTunnelHandle))
339339

340340
mConfig = json
341-
// shouldRecordStartTelemetry must be calculated after mConfig is set (on prior line)
342341
// We don't want to record start metrics in several situations:
343-
// - If Super Dooper feature is not active. (Covered by shouldRecordTimerAndEndMetrics)
344-
// - If this is an app-caused server switch. (Covered by shouldRecordTimerAndEndMetrics)
345-
// - If this is a daemon-caused server switch. (Covered by isDaemonChangingServers)
346-
val shouldRecordStartTelemetry = shouldRecordTimerAndEndMetrics && !isDaemonChangingServers
342+
// - If Super Dooper feature is not active.
343+
// - If this is an app-caused server switch.
344+
// - If this is a daemon-caused server switch.
345+
// This must be calculated after mConfig is set (on line above)
346+
val isChangingServers = isDaemonChangingServers || isAppChangingServers
347+
val shouldSkipStartTelemetry = !isSuperDooperMetricsActive || isChangingServers
348+
Log.i(tag, "Is changing server: " + shouldSkipStartTelemetry)
347349

348350
// Store the config in case the service gets
349351
// asked boot vpn from the OS
@@ -363,13 +365,17 @@ class VPNService : android.net.VpnService() {
363365
} else {
364366
fallbackIpv4 = json.getJSONArray("servers").getJSONObject(0).getString("ipv4AddrIn")
365367
}
366-
mConnectionHealth.start(
367-
jServer.getString("ipv4AddrIn"),
368-
jServer.getString("ipv4Gateway"),
369-
json.getString("dns"),
370-
fallbackIpv4,
371-
shouldRecordStartTelemetry,
372-
)
368+
369+
// If changing server, connection health is still running
370+
if (!isChangingServers) {
371+
mConnectionHealth.start(
372+
jServer.getString("ipv4AddrIn"),
373+
jServer.getString("ipv4Gateway"),
374+
json.getString("dns"),
375+
fallbackIpv4,
376+
isSuperDooperMetricsActive,
377+
)
378+
}
373379

374380
// For `isGleanDebugTagActive` and `isSuperDooperMetricsActive` to work,
375381
// this must be after the mConfig is set to the latest data.
@@ -378,7 +384,8 @@ class VPNService : android.net.VpnService() {
378384
Log.i(tag, "Setting Glean debug tag for daemon.")
379385
Glean.setDebugViewTag(gleanTag)
380386
}
381-
if (shouldRecordStartTelemetry) {
387+
if (!shouldSkipStartTelemetry) {
388+
Log.v(tag, "Recording start telmetry")
382389
val installationIdString = json.getString("installationId")
383390
installationIdString?.let {
384391
try {
@@ -401,7 +408,10 @@ class VPNService : android.net.VpnService() {
401408
Pings.daemonsession.submit(
402409
Pings.daemonsessionReasonCodes.daemonStart,
403410
)
411+
} else {
412+
Log.v(tag, "Skipping recording of start telemetry")
404413
}
414+
mMetricsTimer.cancel() // if this is a server switch, time is already running and must be reset
405415
mMetricsTimer.start()
406416
}
407417

@@ -455,8 +465,10 @@ class VPNService : android.net.VpnService() {
455465

456466
fun turnOff() {
457467
Log.v(tag, "Try to disable tunnel")
458-
if (shouldRecordTimerAndEndMetrics) {
459-
// Data metrics must be recorded prior to wgTurnOff, or we can't get data from the cocnfig
468+
// turnOff is not called when switching locations, doing a silent server switch from app, or doing a silent server switch from daemon...
469+
// Thus we always want to record end-of-session metrics here, and send the ping below.
470+
if (isSuperDooperMetricsActive) {
471+
// Data metrics must be recorded prior to wgTurnOff, or we can't get data from the config
460472
recordDataTransferMetrics()
461473
}
462474

@@ -471,7 +483,8 @@ class VPNService : android.net.VpnService() {
471483
// Clear the notification message, so the content
472484
// is not "disconnected" in case we connect from a non-client.
473485
CannedNotification(mConfig)?.let { mNotificationHandler.hide(it) }
474-
if (shouldRecordTimerAndEndMetrics) {
486+
if (isSuperDooperMetricsActive) {
487+
Log.v(tag, "Sending ending metrics")
475488
Session.daemonSessionEnd.set()
476489
Pings.daemonsession.submit(
477490
Pings.daemonsessionReasonCodes.daemonEnd,
@@ -483,6 +496,8 @@ class VPNService : android.net.VpnService() {
483496
// "flush" reason, which should contain this UUID and no other
484497
// metrics.
485498
Session.daemonSessionId.generateAndSet()
499+
} else {
500+
Log.v(tag, "Skipping ending metrics")
486501
}
487502
mMetricsTimer.cancel()
488503
}
@@ -648,6 +663,7 @@ class VPNService : android.net.VpnService() {
648663
}
649664

650665
private fun recordDataTransferMetrics() {
666+
Log.v(tag, "Recording data metrics")
651667
val rxBytes = getConfigValue("rx_bytes")?.toLong()
652668
val txBytes = getConfigValue("tx_bytes")?.toLong()
653669
if (rxBytes != null && txBytes != null) {

src/connectionhealth.cpp

Lines changed: 0 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -45,23 +45,6 @@ constexpr const char* PING_WELL_KNOWN_ANYCAST_DNS = "194.242.2.2";
4545
ConnectionHealth::ConnectionHealth() : m_dnsPingSender(QHostAddress()) {
4646
MZ_COUNT_CTOR(ConnectionHealth);
4747

48-
// On mobile, these metrics are recorded in the daemon process.
49-
#ifndef MZ_MOBILE
50-
m_metricsTimer.setSingleShot(false);
51-
52-
if (SettingsHolder::instance()->shortTimerSessionPing()) {
53-
m_metricsTimer.setInterval(
54-
std::chrono::duration_cast<std::chrono::minutes>(3min));
55-
} else {
56-
m_metricsTimer.setInterval(
57-
std::chrono::duration_cast<std::chrono::hours>(3h));
58-
}
59-
connect(&m_metricsTimer, &QTimer::timeout, this, []() {
60-
emit MozillaVPN::instance()->controller()->recordDataTransferTelemetry();
61-
});
62-
m_metricsTimer.start();
63-
#endif
64-
6548
m_noSignalTimer.setSingleShot(true);
6649

6750
m_settlingTimer.setSingleShot(true);

src/connectionhealth.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,6 @@ class ConnectionHealth final : public QObject {
7878
QTimer m_settlingTimer;
7979
QTimer m_noSignalTimer;
8080
QTimer m_healthCheckTimer;
81-
QTimer m_metricsTimer;
8281

8382
PingHelper m_pingHelper;
8483

0 commit comments

Comments
 (0)