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