Introduce a job scheduler and experiment for sending notifications to idle devices

This commit is contained in:
Jon Chambers
2024-07-29 11:16:53 -04:00
committed by GitHub
parent 4ebad2c473
commit 045ec9689d
13 changed files with 825 additions and 39 deletions

View File

@@ -251,7 +251,12 @@ import org.whispersystems.textsecuregcm.workers.BackupMetricsCommand;
import org.whispersystems.textsecuregcm.workers.CertificateCommand;
import org.whispersystems.textsecuregcm.workers.CheckDynamicConfigurationCommand;
import org.whispersystems.textsecuregcm.workers.DeleteUserCommand;
import org.whispersystems.textsecuregcm.workers.DiscardPushNotificationExperimentSamplesCommand;
import org.whispersystems.textsecuregcm.workers.FinishPushNotificationExperimentCommand;
import org.whispersystems.textsecuregcm.workers.IdleDeviceNotificationSchedulerFactory;
import org.whispersystems.textsecuregcm.workers.MessagePersisterServiceCommand;
import org.whispersystems.textsecuregcm.workers.NotifyIdleDevicesWithoutMessagesExperimentFactory;
import org.whispersystems.textsecuregcm.workers.ProcessScheduledJobsServiceCommand;
import org.whispersystems.textsecuregcm.workers.RemoveExpiredAccountsCommand;
import org.whispersystems.textsecuregcm.workers.RemoveExpiredBackupsCommand;
import org.whispersystems.textsecuregcm.workers.RemoveExpiredLinkedDevicesCommand;
@@ -260,6 +265,7 @@ import org.whispersystems.textsecuregcm.workers.ScheduledApnPushNotificationSend
import org.whispersystems.textsecuregcm.workers.ServerVersionCommand;
import org.whispersystems.textsecuregcm.workers.SetRequestLoggingEnabledTask;
import org.whispersystems.textsecuregcm.workers.SetUserDiscoverabilityCommand;
import org.whispersystems.textsecuregcm.workers.StartPushNotificationExperimentCommand;
import org.whispersystems.textsecuregcm.workers.UnlinkDeviceCommand;
import org.whispersystems.textsecuregcm.workers.ZkParamsCommand;
import org.whispersystems.websocket.WebSocketResourceProviderFactory;
@@ -313,6 +319,24 @@ public class WhisperServerService extends Application<WhisperServerConfiguration
bootstrap.addCommand(new RemoveExpiredBackupsCommand(Clock.systemUTC()));
bootstrap.addCommand(new BackupMetricsCommand(Clock.systemUTC()));
bootstrap.addCommand(new RemoveExpiredLinkedDevicesCommand());
bootstrap.addCommand(new ProcessScheduledJobsServiceCommand("process-idle-device-notification-jobs",
"Processes scheduled jobs to send notifications to idle devices",
new IdleDeviceNotificationSchedulerFactory()));
bootstrap.addCommand(
new StartPushNotificationExperimentCommand<>("start-notify-idle-devices-without-messages-experiment",
"Start an experiment to send push notifications to idle devices with empty message queues",
new NotifyIdleDevicesWithoutMessagesExperimentFactory()));
bootstrap.addCommand(
new FinishPushNotificationExperimentCommand<>("finish-notify-idle-devices-without-messages-experiment",
"Finish an experiment to send push notifications to idle devices with empty message queues",
new NotifyIdleDevicesWithoutMessagesExperimentFactory()));
bootstrap.addCommand(
new DiscardPushNotificationExperimentSamplesCommand("discard-notify-idle-devices-without-messages-samples",
"Discard samples from the \"notify idle devices without messages\" experiment",
new NotifyIdleDevicesWithoutMessagesExperimentFactory()));
}
@Override

View File

@@ -0,0 +1,17 @@
package org.whispersystems.textsecuregcm.experiment;
import javax.annotation.Nullable;
public record DeviceLastSeenState(boolean deviceExists,
long createdAtMillis,
boolean hasPushToken,
long lastSeenMillis,
@Nullable PushTokenType pushTokenType) {
public static DeviceLastSeenState MISSING_DEVICE_STATE = new DeviceLastSeenState(false, 0, false, 0, null);
public enum PushTokenType {
APNS,
FCM
}
}

View File

@@ -0,0 +1,159 @@
package org.whispersystems.textsecuregcm.experiment;
import com.google.common.annotations.VisibleForTesting;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.whispersystems.textsecuregcm.identity.IdentityType;
import org.whispersystems.textsecuregcm.push.IdleDeviceNotificationScheduler;
import org.whispersystems.textsecuregcm.storage.Account;
import org.whispersystems.textsecuregcm.storage.Device;
import org.whispersystems.textsecuregcm.storage.MessagesManager;
import reactor.core.publisher.Flux;
import javax.annotation.Nullable;
import java.util.EnumMap;
import java.util.Map;
import java.time.LocalTime;
import java.util.concurrent.CompletableFuture;
public class NotifyIdleDevicesWithoutMessagesPushNotificationExperiment implements PushNotificationExperiment<DeviceLastSeenState> {
private final MessagesManager messagesManager;
private final IdleDeviceNotificationScheduler idleDeviceNotificationScheduler;
private static final LocalTime PREFERRED_NOTIFICATION_TIME = LocalTime.of(14, 0);
private static final Logger log = LoggerFactory.getLogger(NotifyIdleDevicesWithoutMessagesPushNotificationExperiment.class);
@VisibleForTesting
enum Population {
APNS_CONTROL,
APNS_EXPERIMENT,
FCM_CONTROL,
FCM_EXPERIMENT
}
@VisibleForTesting
enum Outcome {
DELETED,
UNINSTALLED,
REACTIVATED,
UNCHANGED
}
public NotifyIdleDevicesWithoutMessagesPushNotificationExperiment(final MessagesManager messagesManager,
final IdleDeviceNotificationScheduler idleDeviceNotificationScheduler) {
this.messagesManager = messagesManager;
this.idleDeviceNotificationScheduler = idleDeviceNotificationScheduler;
}
@Override
public String getExperimentName() {
return "notify-idle-devices-without-messages";
}
@Override
public CompletableFuture<Boolean> isDeviceEligible(final Account account, final Device device) {
if (!hasPushToken(device)) {
return CompletableFuture.completedFuture(false);
}
if (!idleDeviceNotificationScheduler.isIdle(device)) {
return CompletableFuture.completedFuture(false);
}
return messagesManager.mayHaveMessages(account.getIdentifier(IdentityType.ACI), device)
.thenApply(mayHaveMessages -> !mayHaveMessages);
}
@VisibleForTesting
static boolean hasPushToken(final Device device) {
// Exclude VOIP tokens since they have their own, distinct delivery mechanism
return !StringUtils.isAllBlank(device.getApnId(), device.getGcmId()) && StringUtils.isBlank(device.getVoipApnId());
}
@Override
public DeviceLastSeenState getState(@Nullable final Account account, @Nullable final Device device) {
if (account != null && device != null) {
final DeviceLastSeenState.PushTokenType pushTokenType = StringUtils.isNotBlank(device.getApnId())
? DeviceLastSeenState.PushTokenType.APNS
: DeviceLastSeenState.PushTokenType.FCM;
return new DeviceLastSeenState(true, device.getCreated(), hasPushToken(device), device.getLastSeen(), pushTokenType);
} else {
return DeviceLastSeenState.MISSING_DEVICE_STATE;
}
}
@Override
public CompletableFuture<Void> applyExperimentTreatment(final Account account, final Device device) {
return idleDeviceNotificationScheduler.scheduleNotification(account, device.getId(), PREFERRED_NOTIFICATION_TIME);
}
@Override
public void analyzeResults(final Flux<PushNotificationExperimentSample<DeviceLastSeenState>> samples) {
final Map<Population, Map<Outcome, Integer>> contingencyTable = new EnumMap<>(Population.class);
for (final Population population : Population.values()) {
final Map<Outcome, Integer> countsByOutcome = new EnumMap<>(Outcome.class);
for (final Outcome outcome : Outcome.values()) {
countsByOutcome.put(outcome, 0);
}
contingencyTable.put(population, countsByOutcome);
}
samples.doOnNext(sample -> contingencyTable.get(getPopulation(sample)).merge(getOutcome(sample), 1, Integer::sum))
.then()
.block();
final StringBuilder reportBuilder = new StringBuilder("population,deleted,uninstalled,reactivated,unchanged\n");
for (final Population population : Population.values()) {
final Map<Outcome, Integer> countsByOutcome = contingencyTable.get(population);
reportBuilder.append(population.name());
reportBuilder.append(",");
reportBuilder.append(countsByOutcome.getOrDefault(Outcome.DELETED, 0));
reportBuilder.append(",");
reportBuilder.append(countsByOutcome.getOrDefault(Outcome.UNINSTALLED, 0));
reportBuilder.append(",");
reportBuilder.append(countsByOutcome.getOrDefault(Outcome.REACTIVATED, 0));
reportBuilder.append(",");
reportBuilder.append(countsByOutcome.getOrDefault(Outcome.UNCHANGED, 0));
reportBuilder.append("\n");
}
log.info(reportBuilder.toString());
}
@VisibleForTesting
static Population getPopulation(final PushNotificationExperimentSample<DeviceLastSeenState> sample) {
assert sample.initialState() != null && sample.initialState().pushTokenType() != null;
return switch (sample.initialState().pushTokenType()) {
case APNS -> sample.inExperimentGroup() ? Population.APNS_EXPERIMENT : Population.APNS_CONTROL;
case FCM -> sample.inExperimentGroup() ? Population.FCM_EXPERIMENT : Population.FCM_CONTROL;
};
}
@VisibleForTesting
static Outcome getOutcome(final PushNotificationExperimentSample<DeviceLastSeenState> sample) {
final Outcome outcome;
if (!sample.finalState().deviceExists() || sample.initialState().createdAtMillis() != sample.finalState().createdAtMillis()) {
outcome = Outcome.DELETED;
} else if (!sample.finalState().hasPushToken()) {
outcome = Outcome.UNINSTALLED;
} else if (sample.initialState().lastSeenMillis() != sample.finalState().lastSeenMillis()) {
outcome = Outcome.REACTIVATED;
} else {
outcome = Outcome.UNCHANGED;
}
return outcome;
}
}

View File

@@ -2,6 +2,7 @@ package org.whispersystems.textsecuregcm.experiment;
import org.whispersystems.textsecuregcm.storage.Account;
import org.whispersystems.textsecuregcm.storage.Device;
import reactor.core.publisher.Flux;
import javax.annotation.Nullable;
import java.util.concurrent.CompletableFuture;
@@ -65,4 +66,12 @@ public interface PushNotificationExperiment<T> {
* @return a future that completes when the experimental treatment has been applied for the given device
*/
CompletableFuture<Void> applyExperimentTreatment(Account account, Device device);
/**
* Consumes a stream of finished samples and emits an analysis of the results via an implementation-specific channel
* (e.g. a log message). Implementations must block until all samples have been consumed and analyzed.
*
* @param samples a stream of finished samples from this experiment
*/
void analyzeResults(Flux<PushNotificationExperimentSample<T>> samples);
}

View File

@@ -0,0 +1,99 @@
package org.whispersystems.textsecuregcm.push;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.google.common.annotations.VisibleForTesting;
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;
import javax.annotation.Nullable;
import java.io.IOException;
import java.time.Clock;
import java.time.Duration;
import java.time.Instant;
import java.time.LocalTime;
import java.util.UUID;
import java.util.concurrent.CompletableFuture;
public class IdleDeviceNotificationScheduler extends JobScheduler {
private final AccountsManager accountsManager;
private final PushNotificationManager pushNotificationManager;
private final Clock clock;
@VisibleForTesting
static final Duration MIN_IDLE_DURATION = Duration.ofDays(14);
@VisibleForTesting
record AccountAndDeviceIdentifier(UUID accountIdentifier, byte deviceId) {}
public IdleDeviceNotificationScheduler(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 "IdleDeviceNotification";
}
@Override
protected CompletableFuture<String> processJob(@Nullable final byte[] jobData) {
final AccountAndDeviceIdentifier accountAndDeviceIdentifier;
try {
accountAndDeviceIdentifier = SystemMapper.jsonMapper().readValue(jobData, AccountAndDeviceIdentifier.class);
} catch (final IOException e) {
return CompletableFuture.failedFuture(e);
}
return accountsManager.getByAccountIdentifierAsync(accountAndDeviceIdentifier.accountIdentifier())
.thenCompose(maybeAccount -> maybeAccount.map(account ->
account.getDevice(accountAndDeviceIdentifier.deviceId()).map(device -> {
if (!isIdle(device)) {
return CompletableFuture.completedFuture("deviceSeenRecently");
}
try {
return pushNotificationManager
.sendNewMessageNotification(account, accountAndDeviceIdentifier.deviceId(), true)
.thenApply(ignored -> "sent");
} catch (final NotPushRegisteredException e) {
return CompletableFuture.completedFuture("deviceTokenDeleted");
}
})
.orElse(CompletableFuture.completedFuture("deviceDeleted")))
.orElse(CompletableFuture.completedFuture("accountDeleted")));
}
public boolean isIdle(final Device device) {
final Duration idleDuration = Duration.between(Instant.ofEpochMilli(device.getLastSeen()), clock.instant());
return idleDuration.compareTo(MIN_IDLE_DURATION) >= 0;
}
public CompletableFuture<Void> scheduleNotification(final Account account, final byte deviceId, final LocalTime preferredDeliveryTime) {
final Instant runAt = SchedulingUtil.getNextRecommendedNotificationTime(account, preferredDeliveryTime, clock);
try {
return scheduleJob(runAt, SystemMapper.jsonMapper().writeValueAsBytes(
new AccountAndDeviceIdentifier(account.getIdentifier(IdentityType.ACI), deviceId)));
} catch (final JsonProcessingException e) {
// This should never happen when serializing an `AccountAndDeviceIdentifier`
throw new AssertionError(e);
}
}
}

View File

@@ -81,7 +81,8 @@ record CommandDependencies(
FaultTolerantRedisCluster pushSchedulerCluster,
ClientResources.Builder redisClusterClientResourcesBuilder,
BackupManager backupManager,
DynamicConfigurationManager<DynamicConfiguration> dynamicConfigurationManager) {
DynamicConfigurationManager<DynamicConfiguration> dynamicConfigurationManager,
DynamoDbAsyncClient dynamoDbAsyncClient) {
static CommandDependencies build(
final String name,
@@ -271,7 +272,8 @@ record CommandDependencies(
pushSchedulerCluster,
redisClientResourcesBuilder,
backupManager,
dynamicConfigurationManager
dynamicConfigurationManager,
dynamoDbAsyncClient
);
}

View File

@@ -10,10 +10,12 @@ import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.whispersystems.textsecuregcm.WhisperServerConfiguration;
import org.whispersystems.textsecuregcm.experiment.PushNotificationExperiment;
import org.whispersystems.textsecuregcm.experiment.PushNotificationExperimentSample;
import org.whispersystems.textsecuregcm.experiment.PushNotificationExperimentSamples;
import org.whispersystems.textsecuregcm.storage.Account;
import org.whispersystems.textsecuregcm.storage.AccountsManager;
import org.whispersystems.textsecuregcm.storage.Device;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;
import reactor.util.function.Tuples;
import reactor.util.retry.Retry;
@@ -71,47 +73,50 @@ public class FinishPushNotificationExperimentCommand<T> extends AbstractCommandW
final AccountsManager accountsManager = commandDependencies.accountsManager();
final PushNotificationExperimentSamples pushNotificationExperimentSamples = commandDependencies.pushNotificationExperimentSamples();
pushNotificationExperimentSamples.getDevicesPendingFinalState(experiment.getExperimentName())
.flatMap(accountIdentifierAndDeviceId ->
Mono.fromFuture(() -> accountsManager.getByAccountIdentifierAsync(accountIdentifierAndDeviceId.getT1()))
.retryWhen(Retry.backoff(3, Duration.ofSeconds(1)))
.map(maybeAccount -> Tuples.of(accountIdentifierAndDeviceId.getT1(), accountIdentifierAndDeviceId.getT2(), maybeAccount)), maxConcurrency)
.map(accountIdentifierAndDeviceIdAndMaybeAccount -> {
final UUID accountIdentifier = accountIdentifierAndDeviceIdAndMaybeAccount.getT1();
final byte deviceId = accountIdentifierAndDeviceIdAndMaybeAccount.getT2();
final Flux<PushNotificationExperimentSample<T>> finishedSamples =
pushNotificationExperimentSamples.getDevicesPendingFinalState(experiment.getExperimentName())
.flatMap(accountIdentifierAndDeviceId ->
Mono.fromFuture(() -> accountsManager.getByAccountIdentifierAsync(accountIdentifierAndDeviceId.getT1()))
.retryWhen(Retry.backoff(3, Duration.ofSeconds(1)))
.map(maybeAccount -> Tuples.of(accountIdentifierAndDeviceId.getT1(),
accountIdentifierAndDeviceId.getT2(), maybeAccount)), maxConcurrency)
.map(accountIdentifierAndDeviceIdAndMaybeAccount -> {
final UUID accountIdentifier = accountIdentifierAndDeviceIdAndMaybeAccount.getT1();
final byte deviceId = accountIdentifierAndDeviceIdAndMaybeAccount.getT2();
@Nullable final Account account = accountIdentifierAndDeviceIdAndMaybeAccount.getT3()
.orElse(null);
@Nullable final Account account = accountIdentifierAndDeviceIdAndMaybeAccount.getT3()
.orElse(null);
@Nullable final Device device = accountIdentifierAndDeviceIdAndMaybeAccount.getT3()
.flatMap(a -> a.getDevice(deviceId))
.orElse(null);
@Nullable final Device device = accountIdentifierAndDeviceIdAndMaybeAccount.getT3()
.flatMap(a -> a.getDevice(deviceId))
.orElse(null);
return Tuples.of(accountIdentifier, deviceId, experiment.getState(account, device));
})
.flatMap(accountIdentifierAndDeviceIdAndFinalState -> {
final UUID accountIdentifier = accountIdentifierAndDeviceIdAndFinalState.getT1();
final byte deviceId = accountIdentifierAndDeviceIdAndFinalState.getT2();
final T finalState = accountIdentifierAndDeviceIdAndFinalState.getT3();
return Tuples.of(accountIdentifier, deviceId, experiment.getState(account, device));
})
.flatMap(accountIdentifierAndDeviceIdAndFinalState -> {
final UUID accountIdentifier = accountIdentifierAndDeviceIdAndFinalState.getT1();
final byte deviceId = accountIdentifierAndDeviceIdAndFinalState.getT2();
final T finalState = accountIdentifierAndDeviceIdAndFinalState.getT3();
return Mono.fromFuture(() -> {
try {
return pushNotificationExperimentSamples.recordFinalState(accountIdentifier, deviceId,
experiment.getExperimentName(), finalState);
} catch (final JsonProcessingException e) {
throw new RuntimeException(e);
}
})
.onErrorResume(ConditionalCheckFailedException.class, throwable -> Mono.empty())
.retryWhen(Retry.backoff(3, Duration.ofSeconds(1)))
.onErrorResume(throwable -> {
log.warn("Failed to record final state for {}:{} in experiment {}",
accountIdentifier, deviceId, experiment.getExperimentName(), throwable);
return Mono.fromFuture(() -> {
try {
return pushNotificationExperimentSamples.recordFinalState(accountIdentifier, deviceId,
experiment.getExperimentName(), finalState);
} catch (final JsonProcessingException e) {
throw new RuntimeException(e);
}
})
.onErrorResume(ConditionalCheckFailedException.class, throwable -> Mono.empty())
.retryWhen(Retry.backoff(3, Duration.ofSeconds(1)))
.onErrorResume(throwable -> {
log.warn("Failed to record final state for {}:{} in experiment {}",
accountIdentifier, deviceId, experiment.getExperimentName(), throwable);
return Mono.empty();
});
}, maxConcurrency)
.then()
.block();
return Mono.empty();
});
}, maxConcurrency)
.flatMap(Mono::justOrEmpty);
experiment.analyzeResults(finishedSamples);
}
}

View File

@@ -0,0 +1,21 @@
package org.whispersystems.textsecuregcm.workers;
import org.whispersystems.textsecuregcm.WhisperServerConfiguration;
import org.whispersystems.textsecuregcm.push.IdleDeviceNotificationScheduler;
import org.whispersystems.textsecuregcm.scheduler.JobScheduler;
import java.time.Clock;
public class IdleDeviceNotificationSchedulerFactory implements JobSchedulerFactory {
@Override
public JobScheduler buildJobScheduler(final CommandDependencies commandDependencies,
final WhisperServerConfiguration configuration) {
return new IdleDeviceNotificationScheduler(commandDependencies.accountsManager(),
commandDependencies.pushNotificationManager(),
commandDependencies.dynamoDbAsyncClient(),
configuration.getDynamoDbTables().getScheduledJobs().getTableName(),
configuration.getDynamoDbTables().getScheduledJobs().getExpiration(),
Clock.systemUTC());
}
}

View File

@@ -0,0 +1,28 @@
package org.whispersystems.textsecuregcm.workers;
import org.whispersystems.textsecuregcm.WhisperServerConfiguration;
import org.whispersystems.textsecuregcm.configuration.DynamoDbTables;
import org.whispersystems.textsecuregcm.experiment.DeviceLastSeenState;
import org.whispersystems.textsecuregcm.experiment.NotifyIdleDevicesWithoutMessagesPushNotificationExperiment;
import org.whispersystems.textsecuregcm.experiment.PushNotificationExperiment;
import org.whispersystems.textsecuregcm.push.IdleDeviceNotificationScheduler;
import java.time.Clock;
public class NotifyIdleDevicesWithoutMessagesExperimentFactory implements PushNotificationExperimentFactory<DeviceLastSeenState> {
@Override
public PushNotificationExperiment<DeviceLastSeenState> buildExperiment(final CommandDependencies commandDependencies,
final WhisperServerConfiguration configuration) {
final DynamoDbTables.TableWithExpiration tableConfiguration = configuration.getDynamoDbTables().getScheduledJobs();
return new NotifyIdleDevicesWithoutMessagesPushNotificationExperiment(commandDependencies.messagesManager(),
new IdleDeviceNotificationScheduler(
commandDependencies.accountsManager(),
commandDependencies.pushNotificationManager(),
commandDependencies.dynamoDbAsyncClient(),
tableConfiguration.getTableName(),
tableConfiguration.getExpiration(),
Clock.systemUTC()));
}
}