Skip to content

Commit

Permalink
FISH-7426 variable naming clarification -- milliseconds
Browse files Browse the repository at this point in the history
FISH-7426 another variable naming clarification -- milliseconds

FISH-7426 fixing config provider resolver to return seconds from config cache

The value cacheDurationSeconds returned milliseconds by mistake
(misleading method name).

FISH-7426 update copyright years
  • Loading branch information
aubi committed Jun 5, 2023
1 parent 39fd53a commit 5518045
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 19 deletions.
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/*
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
*
* Copyright (c) [2017-2021] Payara Foundation and/or its affiliates. All rights reserved.
* Copyright (c) [2017-2023] Payara Foundation and/or its affiliates. All rights reserved.
*
* The contents of this file are subject to the terms of either the GNU
* General Public License Version 2 only ("GPL") or the Common Development
Expand Down Expand Up @@ -203,7 +203,7 @@ public MicroprofileConfigConfiguration getMPConfig() {

long getCacheDurationSeconds() {
if (serverLevelConfig != null) {
return serverLevelConfig.getCacheDurationSeconds();
return serverLevelConfig.getCacheDurationMilliSeconds() / 1_000;
}
return Integer.parseInt(getMPConfig().getCacheDurationSeconds());
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/*
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
*
* Copyright (c) 2017-2022 Payara Foundation and/or its affiliates. All rights reserved.
* Copyright (c) 2017-2023 Payara Foundation and/or its affiliates. All rights reserved.
*
* The contents of this file are subject to the terms of either the GNU
* General Public License Version 2 only ("GPL") or the Common Development
Expand Down Expand Up @@ -98,49 +98,49 @@ private static final class CacheEntry {

private final Map<String, CacheEntry> cachedValuesByProperty = new ConcurrentHashMap<>();

private volatile long configuredCacheDurationSeconds;
private volatile long configuredCacheDurationSecondsExpires = 0; // force value reload at start
private volatile long configuredCacheDurationMilliSeconds;
private volatile long configuredCacheDurationMilliSecondsExpires = 0; // force value reload at start
private final Object configuredCacheValueLock = new Object();

private final String profile;

public PayaraConfig(List<ConfigSource> sources, Map<Class<?>, Converter<?>> converters, long defaultCacheDurationSeconds) {
public PayaraConfig(List<ConfigSource> sources, Map<Class<?>, Converter<?>> converters, long defaultCacheDurationMilliSeconds) {
this.sources = sources;
this.converters = new ConcurrentHashMap<>(converters);
this.defaultCacheDurationMilliSeconds = defaultCacheDurationSeconds;
this.defaultCacheDurationMilliSeconds = defaultCacheDurationMilliSeconds;
Collections.sort(sources, new ConfigSourceComparator());

profile = getConfigValue(MP_CONFIG_PROFILE_NAME_STRING).getValue();
}

@SuppressWarnings("unchecked")
public long getCacheDurationSeconds() {
public long getCacheDurationMilliSeconds() {
final Optional<Converter<Long>> converter = Optional.ofNullable((Converter<Long>) converters.get(Long.class));
if (converter.isPresent()) {
long currentTimeMillis = currentTimeMillis();
// If the value has been found and it hasn't expired
if (currentTimeMillis < configuredCacheDurationSecondsExpires) {
return configuredCacheDurationSeconds;
if (currentTimeMillis < configuredCacheDurationMilliSecondsExpires) {
return configuredCacheDurationMilliSeconds;
} else {
// Atomic block to modify the cached value
synchronized (configuredCacheValueLock) {
// double check if situation didn't change
if (currentTimeMillis < configuredCacheDurationSecondsExpires) {
return configuredCacheDurationSeconds;
if (currentTimeMillis < configuredCacheDurationMilliSecondsExpires) {
return configuredCacheDurationMilliSeconds;
}
// Fetch the value from config
final ConfigValue value = searchConfigSources(MP_CONFIG_CACHE_DURATION, null);
if (value.getValue() != null) {
// If it's found, cache it
configuredCacheDurationSeconds = convertValue(value, null, converter);
configuredCacheDurationMilliSeconds = convertValue(value, null, converter);
} else {
// Cache the default value (usually that's from the server config)
configuredCacheDurationSeconds = defaultCacheDurationMilliSeconds;
configuredCacheDurationMilliSeconds = defaultCacheDurationMilliSeconds;
}
configuredCacheDurationSecondsExpires = currentTimeMillis + configuredCacheDurationSeconds;
configuredCacheDurationMilliSecondsExpires = currentTimeMillis + configuredCacheDurationMilliSeconds;
long endTimeMillis = currentTimeMillis();
log.log(Level.FINER, () -> "getCacheDurationSeconds took about " + (endTimeMillis - currentTimeMillis) + " ms");
return configuredCacheDurationSeconds;
return configuredCacheDurationMilliSeconds;
}
}
}
Expand Down Expand Up @@ -230,7 +230,7 @@ protected ConfigValueImpl getConfigValue(String propertyName, String cacheKey, L
}

protected ConfigValueImpl getConfigValue(String propertyName, String cacheKey, Long ttl, String defaultValue, ConfigValueType type) {
long entryTTL = ttl != null ? ttl : getCacheDurationSeconds();
long entryTTL = ttl != null ? ttl : getCacheDurationMilliSeconds();

if (entryTTL <= 0) {
return searchConfigSources(propertyName, defaultValue);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/*
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
*
* Copyright (c) 2020 Payara Foundation and/or its affiliates. All rights reserved.
* Copyright (c) 2020-2023 Payara Foundation and/or its affiliates. All rights reserved.
*
* The contents of this file are subject to the terms of either the GNU
* General Public License Version 2 only ("GPL") or the Common Development
Expand Down Expand Up @@ -272,7 +272,7 @@ public void illegalArrayElementFailsOverallArrayConversion() {
@Test
public void ttlParameterIsRespected() {
final long ttl = 60 * 1000L;
assertEquals(ttl, new PayaraConfig(emptyList(), emptyMap(), ttl).getCacheDurationSeconds());
assertEquals(ttl, new PayaraConfig(emptyList(), emptyMap(), ttl).getCacheDurationMilliSeconds());
}

private <T> void assertCachedValue(ConfigSource source, String key, Class<T> propertyType, T expectedValue1,
Expand Down

0 comments on commit 5518045

Please sign in to comment.