Skip to content

Commit

Permalink
switch caffeine caches to use expireAfterWrite instead of expireAfter…
Browse files Browse the repository at this point in the history
…Access (which doesn't expire under constant load) (#606)

* switch caffeine caches to use expireAfterWrite instead of expireAfterAccess (which doesn't expire under constant load)
fix /accounts REST api to handle accountIds with "."

Signed-off-by: nhartner <[email protected]>

* fix tests that I crossed assertions on

Signed-off-by: nhartner <[email protected]>
  • Loading branch information
nhartner authored Feb 27, 2020
1 parent 32184df commit 7c189bb
Show file tree
Hide file tree
Showing 6 changed files with 35 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ public IlpOverHttpAuthenticationProvider(
.maximumSize(5000)
// Expire after this duration, which will correspond to the last incoming request from the peer.
// TODO: This value should be configurable and match the server-global token expiry.
.expireAfterAccess(30, TimeUnit.MINUTES)
.expireAfterWrite(30, TimeUnit.MINUTES)
.removalListener((RemovalListener<DecisionsCacheKey, AuthenticationDecision>)
(authenticationRequest, authenticationDecision, cause) ->
logger.debug("Removing IlpOverHttp AuthenticationDecision from Cache for Principal: {}",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ public class PathConstants {
public static final String SE_ACCOUNT_ID = "accountId";
public static final String SLASH_ACCOUNTS = "/accounts";
public static final String SLASH_ENCRYPTION = "/encryption";
public static final String SLASH_ACCOUNT_ID = "/{" + ACCOUNT_ID + "}";
public static final String SLASH_SE_ACCOUNT_ID = "/{" + SE_ACCOUNT_ID + "}";
public static final String SLASH_ACCOUNT_ID = "/{" + ACCOUNT_ID + ":.+}";
public static final String SLASH_SE_ACCOUNT_ID = "/{" + SE_ACCOUNT_ID + ":.+}";
public static final String SLASH_SETTLEMENTS = "/settlements";
public static final String SLASH_MESSAGES = "/messages";
public static final String SLASH_ROUTES = "/routes";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -278,10 +278,37 @@ public void testCreateAndAuthAccountWithSimple() throws IOException {
.build();

AccountSettings created = assertPostAccountCreated(settings);

assertThat(((String) created.customSettings().get(IncomingLinkSettings.HTTP_INCOMING_SIMPLE_AUTH_TOKEN)).startsWith("enc:jks:crypto/crypto.p12:secret0:1:aes_gcm"));
assertThat(((String) created.customSettings().get(OutgoingLinkSettings.HTTP_OUTGOING_SIMPLE_AUTH_TOKEN)).startsWith("enc:jks:crypto/crypto.p12:secret0:1:aes_gcm"));
}

@Test
public void testGetDeleteAccountWithDot() throws IOException {
Map<String, Object> customSettings = com.google.common.collect.Maps.newHashMap();
customSettings.put(IncomingLinkSettings.HTTP_INCOMING_AUTH_TYPE, IlpOverHttpLinkSettings.AuthType.SIMPLE.name());
customSettings.put(IncomingLinkSettings.HTTP_INCOMING_SIMPLE_AUTH_TOKEN, INCOMING_SECRET);

final AccountId accountId = AccountId.of(UUID.randomUUID().toString() + ".test");
AccountSettings settings = AccountSettings.builder()
.accountId(accountId)
.accountRelationship(AccountRelationship.CHILD)
.assetCode("FUD")
.assetScale(6)
.linkType(IlpOverHttpLink.LINK_TYPE)
.createdAt(Instant.now())
.customSettings(customSettings)
.build();

AccountSettings created = assertPostAccountCreated(settings);

Optional<AccountSettings> fetched = adminApiTestClient.findAccount(accountId.value());
assertThat(fetched).isPresent().hasValue(created);
adminApiTestClient.deleteAccount(accountId.value());
Optional<AccountSettings> fetchedAgain = adminApiTestClient.findAccount(accountId.value());
assertThat(fetchedAgain).isNotPresent();
}

@Test
public void createWithBadSecretFails() {
String badEncodedSecret = "enc:JKS:crypto.p12:secret0:1:aes_gcm:!!!!!";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ public class AccountSettingsLoadingCache implements AccountSettingsCache {
public AccountSettingsLoadingCache(final AccountSettingsRepository accountSettingsRepository) {
this(accountSettingsRepository, Caffeine.newBuilder()
// No stats recording in the cache because this is only used for testing...
.expireAfterAccess(15, TimeUnit.MINUTES) // Set very high just for testing...
.expireAfterWrite(15, TimeUnit.MINUTES) // Set very high just for testing...
.maximumSize(5000)
// The value stored in the Cache is the AccountSettings converted from the entity so we don't have to convert
// on every ILPv4 packet switch.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ public class FxRateOverridesLoadingCache {
//
// this.repository = repository;
// this.cache = Caffeine.newBuilder()
// .expireAfterAccess(15, TimeUnit.MINUTES) // Set very high just for testing...
// .expireAfterWrite(15, TimeUnit.MINUTES) // Set very high just for testing...
// .maximumSize(5000)
// // The value stored in the Cache is the AccountSettings converted from the entity so we don't have to convert
// // on every ILPv4 packet switch.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ public CacheMetricsCollector cacheMetricsCollector() {
public Cache<AccountId, Optional<AccountSettings>> accountSettingsCache(CacheMetricsCollector cacheMetricsCollector) {
final Cache<AccountId, Optional<AccountSettings>> accountSettingsCache = Caffeine.newBuilder()
.recordStats() // Publish stats to Prometheus.
.expireAfterAccess(15, TimeUnit.MINUTES) // TODO Make this duration configurable
.expireAfterWrite(15, TimeUnit.MINUTES) // TODO Make this duration configurable
.maximumSize(5000) // TODO: Make size configurable.
.build();

Expand Down Expand Up @@ -82,7 +82,7 @@ public Cache<AccountId, Optional<RateLimiter>> rateLimiterCache(CacheMetricsColl
final Cache<AccountId, Optional<RateLimiter>> rateLimiterCache = Caffeine.newBuilder()
.recordStats() // Publish stats to prometheus
//.maximumSize(100) // Not enabled for now in order to support many accounts.
.expireAfterAccess(30, TimeUnit.SECONDS)
.expireAfterWrite(30, TimeUnit.SECONDS)
.build(); // No default loading function.

cacheMetricsCollector.addCache("rateLimiterCache", rateLimiterCache);
Expand All @@ -100,7 +100,7 @@ public Cache<ConversionQuery, ExchangeRate> fxCache(CacheMetricsCollector cacheM
@Value("${interledger.connector.cache.fxTtl:30}") long fxCacheTimeout) {
final Cache<ConversionQuery, ExchangeRate> fxCache = Caffeine.newBuilder()
.recordStats() // Publish stats to prometheus
.expireAfterAccess(fxCacheTimeout, TimeUnit.SECONDS)
.expireAfterWrite(fxCacheTimeout, TimeUnit.SECONDS)
.build(); // No default loading function.

cacheMetricsCollector.addCache("fxCache", fxCache);
Expand Down

0 comments on commit 7c189bb

Please sign in to comment.