Add a 2-notification ttl=0 push notification experiment

This commit is contained in:
ravi-signal
2025-02-13 10:25:25 -06:00
committed by GitHub
parent 6032764052
commit 4908a0aa9e
12 changed files with 304 additions and 28 deletions

View File

@@ -14,6 +14,7 @@ import com.google.firebase.FirebaseApp;
import com.google.firebase.FirebaseOptions;
import com.google.firebase.ThreadManager;
import com.google.firebase.messaging.AndroidConfig;
import com.google.firebase.messaging.AndroidNotification;
import com.google.firebase.messaging.FirebaseMessaging;
import com.google.firebase.messaging.FirebaseMessagingException;
import com.google.firebase.messaging.Message;
@@ -24,6 +25,7 @@ import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.util.Optional;
import java.util.UUID;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.ThreadFactory;
@@ -78,11 +80,24 @@ public class FcmSender implements PushNotificationSender {
@Override
public CompletableFuture<SendPushNotificationResult> sendNotification(PushNotification pushNotification) {
final AndroidConfig.Builder androidConfig = AndroidConfig.builder()
.setPriority(pushNotification.urgent() ? AndroidConfig.Priority.HIGH : AndroidConfig.Priority.NORMAL);
// This experiment compares the effect of two standard push notifications to one standard + one with a TTL=0
switch (pushNotification.experimentalNotificationType().orElse(null)) {
case ZERO_TTL -> {
androidConfig.setTtl(0);
androidConfig.setCollapseKey("ttl0");
}
case NON_COLLAPSIBLE ->
// We still want to make sure we don't collapse notifications in the control group
androidConfig.setCollapseKey("ttl0");
case null -> {}
}
Message.Builder builder = Message.builder()
.setToken(pushNotification.deviceToken())
.setAndroidConfig(AndroidConfig.builder()
.setPriority(pushNotification.urgent() ? AndroidConfig.Priority.HIGH : AndroidConfig.Priority.NORMAL)
.build());
.setAndroidConfig(androidConfig.build());
final String key = switch (pushNotification.notificationType()) {
case NOTIFICATION -> "newMessageAlert";

View File

@@ -8,6 +8,7 @@ package org.whispersystems.textsecuregcm.push;
import org.whispersystems.textsecuregcm.storage.Account;
import org.whispersystems.textsecuregcm.storage.Device;
import javax.annotation.Nullable;
import java.util.Optional;
public record PushNotification(String deviceToken,
TokenType tokenType,
@@ -15,7 +16,8 @@ public record PushNotification(String deviceToken,
@Nullable String data,
@Nullable Account destination,
@Nullable Device destinationDevice,
boolean urgent) {
boolean urgent,
Optional<ExperimentalNotificationType> experimentalNotificationType) {
public enum NotificationType {
NOTIFICATION,
@@ -28,4 +30,9 @@ public record PushNotification(String deviceToken,
FCM,
APN
}
public enum ExperimentalNotificationType {
ZERO_TTL,
NON_COLLAPSIBLE
}
}

View File

@@ -51,11 +51,21 @@ public class PushNotificationManager {
final Pair<String, PushNotification.TokenType> tokenAndType = getToken(device);
return sendNotification(new PushNotification(tokenAndType.first(), tokenAndType.second(),
PushNotification.NotificationType.NOTIFICATION, null, destination, device, urgent));
PushNotification.NotificationType.NOTIFICATION, null, destination, device, urgent, Optional.empty()));
}
public CompletableFuture<Optional<SendPushNotificationResult>> sendExperimentMessageNotification(
final Account destination, final byte destinationDeviceId, final boolean urgent, PushNotification.ExperimentalNotificationType experimentalNotificationType) throws NotPushRegisteredException {
final Device device = destination.getDevice(destinationDeviceId).orElseThrow(NotPushRegisteredException::new);
final Pair<String, PushNotification.TokenType> tokenAndType = getToken(device);
return sendNotification(new PushNotification(tokenAndType.first(), tokenAndType.second(),
PushNotification.NotificationType.NOTIFICATION,
null, destination, device, urgent, Optional.of(experimentalNotificationType)));
}
public CompletableFuture<SendPushNotificationResult> sendRegistrationChallengeNotification(final String deviceToken, final PushNotification.TokenType tokenType, final String challengeToken) {
return sendNotification(new PushNotification(deviceToken, tokenType, PushNotification.NotificationType.CHALLENGE, challengeToken, null, null, true))
return sendNotification(new PushNotification(deviceToken, tokenType, PushNotification.NotificationType.CHALLENGE, challengeToken, null, null, true, Optional.empty()))
.thenApply(maybeResponse -> maybeResponse.orElseThrow(() -> new AssertionError("Responses must be present for urgent notifications")));
}
@@ -66,7 +76,7 @@ public class PushNotificationManager {
final Pair<String, PushNotification.TokenType> tokenAndType = getToken(device);
return sendNotification(new PushNotification(tokenAndType.first(), tokenAndType.second(),
PushNotification.NotificationType.RATE_LIMIT_CHALLENGE, challengeToken, destination, device, true))
PushNotification.NotificationType.RATE_LIMIT_CHALLENGE, challengeToken, destination, device, true, Optional.empty()))
.thenApply(maybeResponse -> maybeResponse.orElseThrow(() -> new AssertionError("Responses must be present for urgent notifications")));
}
@@ -76,7 +86,7 @@ public class PushNotificationManager {
return sendNotification(new PushNotification(tokenAndType.first(), tokenAndType.second(),
PushNotification.NotificationType.ATTEMPT_LOGIN_NOTIFICATION_HIGH_PRIORITY,
context, destination, device, true))
context, destination, device, true, Optional.empty()))
.thenApply(maybeResponse -> maybeResponse.orElseThrow(() -> new AssertionError("Responses must be present for urgent notifications")));
}

View File

@@ -286,7 +286,7 @@ public class PushNotificationScheduler implements Managed {
return pushSchedulingCluster.withCluster(connection -> connection.async().set(
getLastBackgroundApnsNotificationTimestampKey(account, device),
String.valueOf(clock.millis()), new SetArgs().ex(BACKGROUND_NOTIFICATION_PERIOD)))
.thenCompose(ignored -> apnSender.sendNotification(new PushNotification(device.getApnId(), PushNotification.TokenType.APN, PushNotification.NotificationType.NOTIFICATION, null, account, device, false)))
.thenCompose(ignored -> apnSender.sendNotification(new PushNotification(device.getApnId(), PushNotification.TokenType.APN, PushNotification.NotificationType.NOTIFICATION, null, account, device, false, Optional.empty())))
.thenAccept(response -> Metrics.counter(BACKGROUND_NOTIFICATION_SENT_COUNTER_NAME,
ACCEPTED_TAG, String.valueOf(response.accepted()))
.increment())
@@ -308,7 +308,8 @@ public class PushNotificationScheduler implements Managed {
null,
account,
device,
true);
true,
Optional.empty());
final PushNotificationSender pushNotificationSender = isApnsDevice ? apnSender : fcmSender;

View File

@@ -0,0 +1,103 @@
package org.whispersystems.textsecuregcm.push;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.google.common.annotations.VisibleForTesting;
import java.io.IOException;
import java.time.Clock;
import java.time.Duration;
import java.time.Instant;
import java.time.LocalTime;
import java.util.Optional;
import java.util.UUID;
import java.util.concurrent.CompletableFuture;
import javax.annotation.Nullable;
import org.whispersystems.textsecuregcm.identity.IdentityType;
import org.whispersystems.textsecuregcm.scheduler.JobScheduler;
import org.whispersystems.textsecuregcm.scheduler.SchedulingUtil;
import org.whispersystems.textsecuregcm.storage.Account;
import org.whispersystems.textsecuregcm.storage.AccountsManager;
import org.whispersystems.textsecuregcm.storage.Device;
import org.whispersystems.textsecuregcm.util.SystemMapper;
import software.amazon.awssdk.services.dynamodb.DynamoDbAsyncClient;
public class ZeroTtlNotificationScheduler extends JobScheduler {
private final AccountsManager accountsManager;
private final PushNotificationManager pushNotificationManager;
private final Clock clock;
@VisibleForTesting
record JobDescriptor(UUID accountIdentifier, byte deviceId, long lastSeen, boolean zeroTtl) {}
public ZeroTtlNotificationScheduler(
final AccountsManager accountsManager,
final PushNotificationManager pushNotificationManager,
final DynamoDbAsyncClient dynamoDbAsyncClient,
final String tableName,
final Duration jobExpiration,
final Clock clock) {
super(dynamoDbAsyncClient, tableName, jobExpiration, clock);
this.accountsManager = accountsManager;
this.pushNotificationManager = pushNotificationManager;
this.clock = clock;
}
@Override
public String getSchedulerName() {
return "ZeroTtlNotification";
}
@Override
protected CompletableFuture<String> processJob(@Nullable final byte[] jobData) {
final JobDescriptor jobDescriptor;
try {
jobDescriptor = SystemMapper.jsonMapper().readValue(jobData, JobDescriptor.class);
} catch (final IOException e) {
return CompletableFuture.failedFuture(e);
}
return accountsManager.getByAccountIdentifierAsync(jobDescriptor.accountIdentifier())
.thenCompose(maybeAccount -> maybeAccount.map(account ->
account.getDevice(jobDescriptor.deviceId()).map(device -> {
if (jobDescriptor.lastSeen() != device.getLastSeen()) {
return CompletableFuture.completedFuture("deviceSeenRecently");
}
try {
return sendNotification(account, jobDescriptor).thenApply(ignored -> "sent");
} catch (final NotPushRegisteredException e) {
return CompletableFuture.completedFuture("deviceTokenDeleted");
}
})
.orElse(CompletableFuture.completedFuture("deviceDeleted")))
.orElse(CompletableFuture.completedFuture("accountDeleted")));
}
private CompletableFuture<Void> sendNotification(final Account account,
final JobDescriptor jobDescriptor) throws NotPushRegisteredException {
final CompletableFuture<Optional<SendPushNotificationResult>> standardNotification = pushNotificationManager.sendNewMessageNotification(
account, jobDescriptor.deviceId(), true);
final CompletableFuture<Optional<SendPushNotificationResult>> experimentNotification = pushNotificationManager.sendExperimentMessageNotification(
account, jobDescriptor.deviceId(), true, jobDescriptor.zeroTtl()
? PushNotification.ExperimentalNotificationType.ZERO_TTL
: PushNotification.ExperimentalNotificationType.NON_COLLAPSIBLE);
return standardNotification.thenCombine(experimentNotification, (ignored1, ignored2) -> null);
}
public CompletableFuture<Void> scheduleNotification(final Account account, final Device device,
final LocalTime preferredDeliveryTime, boolean zeroTtl) {
final Instant runAt = SchedulingUtil.getNextRecommendedNotificationTime(account, preferredDeliveryTime, clock);
try {
return scheduleJob(runAt, SystemMapper.jsonMapper().writeValueAsBytes(
new JobDescriptor(account.getIdentifier(IdentityType.ACI), device.getId(), device.getLastSeen(), zeroTtl)));
} catch (final JsonProcessingException e) {
// This should never happen when serializing an `AccountAndDeviceIdentifier`
throw new AssertionError(e);
}
}
}