Fully delete already-expired accounts

This commit is contained in:
Jon Chambers
2022-03-28 17:40:53 -04:00
committed by Jon Chambers
parent a4ca1ef1a8
commit cf89e2215c
2 changed files with 26 additions and 33 deletions

View File

@@ -4,33 +4,29 @@
*/
package org.whispersystems.textsecuregcm.storage;
import com.codahale.metrics.Histogram;
import com.codahale.metrics.Meter;
import com.codahale.metrics.MetricRegistry;
import com.codahale.metrics.SharedMetricRegistries;
import com.google.common.annotations.VisibleForTesting;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.whispersystems.textsecuregcm.util.Constants;
import org.whispersystems.textsecuregcm.util.Util;
import java.util.List;
import java.util.Optional;
import java.util.UUID;
import java.util.concurrent.TimeUnit;
import io.micrometer.core.instrument.Metrics;
import io.micrometer.core.instrument.Tag;
import io.micrometer.core.instrument.Tags;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.whispersystems.textsecuregcm.util.Util;
import static com.codahale.metrics.MetricRegistry.name;
import static org.whispersystems.textsecuregcm.metrics.MetricsUtil.name;
public class AccountCleaner extends AccountDatabaseCrawlerListener {
private static final Logger log = LoggerFactory.getLogger(AccountCleaner.class);
private static final MetricRegistry metricRegistry = SharedMetricRegistries.getOrCreate(Constants.METRICS_NAME);
private static final Meter expiredAccountsMeter = metricRegistry.meter(name(AccountCleaner.class, "expiredAccounts"));
private static final Histogram deletableAccountHistogram = metricRegistry.histogram(name(AccountCleaner.class, "deletableAccountsPerChunk"));
private static final String DELETED_ACCOUNT_COUNTER_NAME = name(AccountCleaner.class, "deletedAccounts");
private static final String DELETION_REASON_TAG_NAME = "reason";
@VisibleForTesting
static final int MAX_ACCOUNT_UPDATES_PER_CHUNK = 40;
static final int MAX_ACCOUNT_DELETIONS_PER_CHUNK = 40;
private final AccountsManager accountsManager;
@@ -48,29 +44,30 @@ public class AccountCleaner extends AccountDatabaseCrawlerListener {
@Override
protected void onCrawlChunk(Optional<UUID> fromUuid, List<Account> chunkAccounts) {
int accountUpdateCount = 0;
int deletableAccountCount = 0;
int accountUpdateCount = 0;
for (Account account : chunkAccounts) {
if (isExpired(account)) {
deletableAccountCount++;
}
if (isExpired(account) || needsExplicitRemoval(account)) {
final Tag deletionReason;
if (needsExplicitRemoval(account)) {
expiredAccountsMeter.mark();
if (isExpired(account)) {
deletionReason = Tag.of(DELETION_REASON_TAG_NAME, "previouslyExpired");
} else {
deletionReason = Tag.of(DELETION_REASON_TAG_NAME, "newlyExpired");
}
if (accountUpdateCount < MAX_ACCOUNT_UPDATES_PER_CHUNK) {
if (accountUpdateCount < MAX_ACCOUNT_DELETIONS_PER_CHUNK) {
try {
accountsManager.delete(account, AccountsManager.DeletionReason.EXPIRED);
accountUpdateCount++;
Metrics.counter(DELETED_ACCOUNT_COUNTER_NAME, Tags.of(deletionReason)).increment();
} catch (final Exception e) {
log.warn("Failed to delete account {}", account.getUuid(), e);
}
}
}
}
deletableAccountHistogram.update(deletableAccountCount);
}
private boolean needsExplicitRemoval(Account account) {