Simplify registration lock counting by avoiding inactive accounts.

This commit is contained in:
Jon Chambers
2020-07-16 13:55:38 -04:00
committed by Jon Chambers
parent 022dbb606f
commit 7f8f2641f6
2 changed files with 32 additions and 65 deletions

View File

@@ -26,10 +26,8 @@ public class RegistrationLockVersionCounter extends AccountDatabaseCrawlerListen
private final MetricsFactory metricsFactory;
static final String REGLOCK_COUNT_KEY = "ReglockVersionCounter::reglockCount";
static final String NO_REGLOCK_KEY = "none";
static final String PIN_ONLY_KEY = "pinOnly";
static final String REGLOCK_ONLY_KEY = "reglockOnly";
static final String BOTH_KEY = "both";
static final String PIN_KEY = "pin";
static final String REGLOCK_KEY = "reglock";
public RegistrationLockVersionCounter(final FaultTolerantRedisCluster redisCluster, final MetricsFactory metricsFactory) {
this.redisCluster = redisCluster;
@@ -38,52 +36,42 @@ public class RegistrationLockVersionCounter extends AccountDatabaseCrawlerListen
@Override
public void onCrawlStart() {
redisCluster.useWriteCluster(connection -> connection.sync().hset(REGLOCK_COUNT_KEY, Map.of(
NO_REGLOCK_KEY, "0",
PIN_ONLY_KEY, "0",
REGLOCK_ONLY_KEY, "0",
BOTH_KEY, "0")));
redisCluster.useWriteCluster(connection -> connection.sync().hset(REGLOCK_COUNT_KEY, Map.of(PIN_KEY, "0", REGLOCK_KEY, "0")));
}
@Override
protected void onCrawlChunk(final Optional<UUID> fromUuid, final List<Account> chunkAccounts) {
int noReglockCount = 0;
int pinOnlyCount = 0;
int reglockOnlyCount = 0;
int bothCount = 0;
int pinCount = 0;
int reglockCount = 0;
for (final Account account : chunkAccounts) {
final StoredRegistrationLock storedRegistrationLock = account.getRegistrationLock();
if (storedRegistrationLock.hasDeprecatedPin() && storedRegistrationLock.needsFailureCredentials()) {
bothCount++;
} else if (storedRegistrationLock.hasDeprecatedPin()) {
pinOnlyCount++;
} else if (storedRegistrationLock.needsFailureCredentials()) {
reglockOnlyCount++;
} else {
noReglockCount++;
if (storedRegistrationLock.requiresClientRegistrationLock()) {
if (storedRegistrationLock.hasDeprecatedPin()) {
pinCount++;
} else {
reglockCount++;
}
}
}
incrementReglockCounts(noReglockCount, pinOnlyCount, reglockOnlyCount, bothCount);
incrementReglockCounts(pinCount, reglockCount);
}
private void incrementReglockCounts(final int noReglockCount, final int pinOnlyCount, final int reglockOnlyCount, final int bothCount) {
private void incrementReglockCounts(final int pinCount, final int reglockCount) {
redisCluster.useWriteCluster(connection -> {
final RedisAdvancedClusterCommands<String, String> commands = connection.sync();
commands.hincrby(REGLOCK_COUNT_KEY, NO_REGLOCK_KEY, noReglockCount);
commands.hincrby(REGLOCK_COUNT_KEY, PIN_ONLY_KEY, pinOnlyCount);
commands.hincrby(REGLOCK_COUNT_KEY, REGLOCK_ONLY_KEY, reglockOnlyCount);
commands.hincrby(REGLOCK_COUNT_KEY, BOTH_KEY, bothCount);
commands.hincrby(REGLOCK_COUNT_KEY, PIN_KEY, pinCount);
commands.hincrby(REGLOCK_COUNT_KEY, REGLOCK_KEY, reglockCount);
});
}
@Override
public void onCrawlEnd(final Optional<UUID> fromUuid) {
final Map<String, Integer> countsByReglockType =
redisCluster.withReadCluster(connection -> connection.sync().hmget(REGLOCK_COUNT_KEY, NO_REGLOCK_KEY, PIN_ONLY_KEY, REGLOCK_ONLY_KEY, BOTH_KEY))
redisCluster.withReadCluster(connection -> connection.sync().hmget(REGLOCK_COUNT_KEY, PIN_KEY, REGLOCK_KEY))
.stream()
.collect(Collectors.toMap(KeyValue::getKey, keyValue -> keyValue.hasValue() ? keyValue.map(Integer::parseInt).getValue() : 0));