Simplify account cleaner
This commit is contained in:
parent
2f21e930e2
commit
a883426402
|
@ -6,8 +6,8 @@ package org.whispersystems.textsecuregcm.storage;
|
|||
|
||||
import static org.whispersystems.textsecuregcm.metrics.MetricsUtil.name;
|
||||
|
||||
import io.micrometer.core.instrument.Counter;
|
||||
import io.micrometer.core.instrument.Metrics;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Optional;
|
||||
import java.util.UUID;
|
||||
|
@ -17,14 +17,12 @@ import java.util.concurrent.Executor;
|
|||
import java.util.concurrent.TimeUnit;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.whispersystems.textsecuregcm.util.Util;
|
||||
|
||||
public class AccountCleaner extends AccountDatabaseCrawlerListener {
|
||||
|
||||
private static final Logger log = LoggerFactory.getLogger(AccountCleaner.class);
|
||||
|
||||
private static final String DELETED_ACCOUNT_COUNTER_NAME = name(AccountCleaner.class, "deletedAccounts");
|
||||
private static final String DELETION_REASON_TAG_NAME = "reason";
|
||||
private static final Counter DELETED_ACCOUNT_COUNTER = Metrics.counter(name(AccountCleaner.class, "deletedAccounts"));
|
||||
|
||||
private final AccountsManager accountsManager;
|
||||
private final Executor deletionExecutor;
|
||||
|
@ -44,13 +42,9 @@ public class AccountCleaner extends AccountDatabaseCrawlerListener {
|
|||
|
||||
@Override
|
||||
protected void onCrawlChunk(Optional<UUID> fromUuid, List<Account> chunkAccounts) {
|
||||
final List<CompletableFuture<Void>> deletionFutures = new ArrayList<>();
|
||||
|
||||
for (Account account : chunkAccounts) {
|
||||
if (isExpired(account) || needsExplicitRemoval(account)) {
|
||||
final String deletionReason = needsExplicitRemoval(account) ? "newlyExpired" : "previouslyExpired";
|
||||
|
||||
deletionFutures.add(CompletableFuture.runAsync(() -> {
|
||||
final List<CompletableFuture<Void>> deletionFutures = chunkAccounts.stream()
|
||||
.filter(AccountCleaner::isExpired)
|
||||
.map(account -> CompletableFuture.runAsync(() -> {
|
||||
try {
|
||||
accountsManager.delete(account, AccountsManager.DeletionReason.EXPIRED);
|
||||
} catch (final InterruptedException e) {
|
||||
|
@ -61,11 +55,10 @@ public class AccountCleaner extends AccountDatabaseCrawlerListener {
|
|||
if (throwable != null) {
|
||||
log.warn("Failed to delete account {}", account.getUuid(), throwable);
|
||||
} else {
|
||||
Metrics.counter(DELETED_ACCOUNT_COUNTER_NAME, DELETION_REASON_TAG_NAME, deletionReason).increment();
|
||||
DELETED_ACCOUNT_COUNTER.increment();
|
||||
}
|
||||
}));
|
||||
}
|
||||
}
|
||||
}))
|
||||
.toList();
|
||||
|
||||
try {
|
||||
CompletableFuture.allOf(deletionFutures.toArray(new CompletableFuture[0])).join();
|
||||
|
@ -74,17 +67,7 @@ public class AccountCleaner extends AccountDatabaseCrawlerListener {
|
|||
}
|
||||
}
|
||||
|
||||
private boolean needsExplicitRemoval(Account account) {
|
||||
return account.getMasterDevice().isPresent() &&
|
||||
hasPushToken(account.getMasterDevice().get()) &&
|
||||
isExpired(account);
|
||||
}
|
||||
|
||||
private boolean hasPushToken(Device device) {
|
||||
return !Util.isEmpty(device.getGcmId()) || !Util.isEmpty(device.getApnId()) || !Util.isEmpty(device.getVoipApnId()) || device.getFetchesMessages();
|
||||
}
|
||||
|
||||
private boolean isExpired(Account account) {
|
||||
private static boolean isExpired(Account account) {
|
||||
return account.getLastSeen() + TimeUnit.DAYS.toMillis(365) < System.currentTimeMillis();
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in New Issue