Skip to content

Commit

Permalink
Smoothly transition the mute cache across /libertybans restart
Browse files Browse the repository at this point in the history
  • Loading branch information
A248 committed Feb 23, 2024
1 parent b10c847 commit 9cb950b
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 45 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@
import space.arim.libertybans.api.select.PunishmentSelector;
import space.arim.libertybans.core.config.Configs;
import space.arim.libertybans.core.config.InternalFormatter;
import space.arim.libertybans.core.config.SqlConfig;
import space.arim.libertybans.core.env.EnvUserResolver;
import space.arim.libertybans.core.service.Time;
import space.arim.omnibus.util.concurrent.CentralisedFuture;
Expand Down Expand Up @@ -83,15 +82,28 @@ public AlwaysAvailableMuteCache(Configs configs, FactoryOfTheFuture futuresFacto
}

@Override
void installCache(Duration expirationTime, SqlConfig.MuteCaching.ExpirationSemantic expirationSemantic) {
ConcurrentHashMap<MuteCacheKey, Entry> map = new ConcurrentHashMap<>();
Cache cache = new Cache(map, expirationTime);
cache.startPurgeTask();
this.cache = cache;
public void startup() {
installCache((expirationTime, expirationSemantic) -> {
ConcurrentHashMap<MuteCacheKey, Entry> map = new ConcurrentHashMap<>();
Cache cache = new Cache(map, expirationTime);
cache.startPurgeTask();
this.cache = cache;
});
}

@Override
public void restart() {
installCache((expirationTime, expirationSemantic) -> {
Cache oldCache = this.cache;
oldCache.stopPurgeTask();
Cache newCache = new Cache(oldCache.map, expirationTime);
newCache.startPurgeTask();
this.cache = newCache;
});
}

@Override
void uninstallCache() {
public void shutdown() {
cache.stopPurgeTask();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
import java.time.Duration;
import java.util.Optional;
import java.util.UUID;
import java.util.function.BiConsumer;
import java.util.function.Predicate;

abstract class BaseMuteCache implements MuteCache {
Expand All @@ -45,8 +46,7 @@ abstract class BaseMuteCache implements MuteCache {

// Setup

@Override
public final void startup() {
void installCache(BiConsumer<Duration, ExpirationSemantic> settingsConsumer) {
SqlConfig sqlConfig = configs.getSqlConfig();
SqlConfig.MuteCaching muteCaching = sqlConfig.muteCaching();

Expand All @@ -58,24 +58,9 @@ public final void startup() {
} else {
expirationSemantic = muteCaching.expirationSemantic();
}
installCache(expirationTime, expirationSemantic);
settingsConsumer.accept(expirationTime, expirationSemantic);
}

@Override
public final void restart() {
shutdown();
startup();
}

@Override
public final void shutdown() {
uninstallCache();
}

abstract void installCache(Duration expirationTime, ExpirationSemantic expirationSemantic);

abstract void uninstallCache();

// Retrieval

final CentralisedFuture<Optional<Punishment>> queryPunishment(MuteCacheKey key) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,10 @@
import space.arim.libertybans.api.select.PunishmentSelector;
import space.arim.libertybans.core.config.Configs;
import space.arim.libertybans.core.config.InternalFormatter;
import space.arim.libertybans.core.config.SqlConfig;
import space.arim.libertybans.core.service.Time;
import space.arim.omnibus.util.concurrent.CentralisedFuture;
import space.arim.omnibus.util.concurrent.FactoryOfTheFuture;

import java.time.Duration;
import java.util.Optional;
import java.util.UUID;
import java.util.function.Predicate;
Expand Down Expand Up @@ -65,28 +63,36 @@ public OnDemandMuteCache(Configs configs, FactoryOfTheFuture futuresFactory,
}

@Override
void installCache(Duration expirationTime, SqlConfig.MuteCaching.ExpirationSemantic expirationSemantic) {

Caffeine<Object, Object> builder = Caffeine.newBuilder();
builder = switch (expirationSemantic) {
case EXPIRE_AFTER_ACCESS -> builder.expireAfterAccess(expirationTime);
case EXPIRE_AFTER_WRITE -> builder.expireAfterWrite(expirationTime);
};
cache = builder
.scheduler(Scheduler.disabledScheduler())
.ticker(time.toCaffeineTicker())
.buildAsync((key, executor) -> {
return queryPunishment(key).exceptionally((ex) -> {
// If we don't catch these exceptions, Caffeine will
LoggerFactory.getLogger(OnDemandMuteCache.class)
.warn("Exception while computing cached mute", ex);
return Optional.empty();
public void startup() {
installCache((expirationTime, expirationSemantic) -> {
Caffeine<Object, Object> builder = Caffeine.newBuilder();
builder = switch (expirationSemantic) {
case EXPIRE_AFTER_ACCESS -> builder.expireAfterAccess(expirationTime);
case EXPIRE_AFTER_WRITE -> builder.expireAfterWrite(expirationTime);
};
cache = builder
.scheduler(Scheduler.disabledScheduler())
.ticker(time.toCaffeineTicker())
.buildAsync((key, executor) -> {
return queryPunishment(key).exceptionally((ex) -> {
// If we don't catch these exceptions, Caffeine will
LoggerFactory.getLogger(OnDemandMuteCache.class)
.warn("Exception while computing cached mute", ex);
return Optional.empty();
});
});
});
});
}

@Override
public void restart() {
cache.synchronous().invalidateAll();
}

@Override
void uninstallCache() {}
public void shutdown() {

}

@Override
public CentralisedFuture<Optional<Punishment>> getCachedMute(UUID uuid, NetworkAddress address) {
Expand Down

0 comments on commit 9cb950b

Please sign in to comment.