-
-
Notifications
You must be signed in to change notification settings - Fork 337
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add basic cache and stats for config
- Loading branch information
Showing
9 changed files
with
330 additions
and
22 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
83 changes: 83 additions & 0 deletions
83
core/src/main/java/org/jdbi/v3/core/config/cache/JdbiConfigCache.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
/* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package org.jdbi.v3.core.config.cache; | ||
|
||
import java.util.concurrent.ConcurrentHashMap; | ||
|
||
import org.inferred.freebuilder.shaded.com.google.common.cache.CacheStats; | ||
|
||
/** | ||
* Caching class for any config related caches. Keeps track of various statistics that are useful for | ||
* performance measurement. | ||
* | ||
* @param <K> key type of the cache | ||
* @param <V> value type of the cache | ||
*/ | ||
public final class JdbiConfigCache<K, V> { | ||
|
||
private final String configName; | ||
private final ConcurrentHashMap<K, V> cache; | ||
|
||
private final JdbiConfigCacheMetrics cacheMetrics = new JdbiConfigCacheMetrics(); | ||
private final JdbiConfigCacheMetrics globalMetrics; | ||
|
||
public JdbiConfigCache(String configName) { | ||
this.configName = configName; | ||
this.cache = new ConcurrentHashMap<>(); | ||
this.globalMetrics = new JdbiConfigCacheMetrics(); | ||
} | ||
|
||
private JdbiConfigCache(JdbiConfigCache<K, V> that) { | ||
this.configName = that.configName; | ||
this.cache = new ConcurrentHashMap<>(that.cache); | ||
this.globalMetrics = that.globalMetrics; | ||
} | ||
|
||
public JdbiConfigCache<K, V> copy() { | ||
return new JdbiConfigCache<>(this); | ||
} | ||
|
||
public void clear() { | ||
this.cache.clear(); | ||
} | ||
|
||
public V put(K key, V value) { | ||
globalMetrics.put(); | ||
cacheMetrics.put(); | ||
return this.cache.put(key, value); | ||
} | ||
|
||
public V putIfAbsent(K key, V value) { | ||
globalMetrics.put(); | ||
cacheMetrics.put(); | ||
return this.cache.putIfAbsent(key, value); | ||
} | ||
|
||
public V get(K key) { | ||
V value = cache.get(key); | ||
if (value != null) { | ||
globalMetrics.hit(); | ||
cacheMetrics.hit(); | ||
} else { | ||
globalMetrics.miss(); | ||
cacheMetrics.miss(); | ||
} | ||
return value; | ||
} | ||
|
||
public JdbiConfigCacheStats getStats() { | ||
return new JdbiConfigCacheStats(configName, cache.size(), cacheMetrics, globalMetrics); | ||
} | ||
|
||
} |
36 changes: 36 additions & 0 deletions
36
core/src/main/java/org/jdbi/v3/core/config/cache/JdbiConfigCacheMetrics.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
package org.jdbi.v3.core.config.cache; | ||
|
||
import java.util.concurrent.atomic.AtomicInteger; | ||
|
||
class JdbiConfigCacheMetrics { | ||
private final AtomicInteger cacheHits = new AtomicInteger(); | ||
private final AtomicInteger cacheMisses = new AtomicInteger(); | ||
private final AtomicInteger cacheWrites = new AtomicInteger(); | ||
|
||
JdbiConfigCacheMetrics() { | ||
} | ||
|
||
void hit() { | ||
cacheHits.incrementAndGet(); | ||
} | ||
|
||
void miss() { | ||
cacheMisses.incrementAndGet(); | ||
} | ||
|
||
void put() { | ||
cacheWrites.incrementAndGet(); | ||
} | ||
|
||
int getCacheWrites() { | ||
return cacheWrites.incrementAndGet(); | ||
} | ||
|
||
int getCacheHits() { | ||
return cacheHits.get(); | ||
} | ||
|
||
int getCacheMisses() { | ||
return cacheMisses.get(); | ||
} | ||
} |
98 changes: 98 additions & 0 deletions
98
core/src/main/java/org/jdbi/v3/core/config/cache/JdbiConfigCacheStats.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
/* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package org.jdbi.v3.core.config.cache; | ||
|
||
import java.util.StringJoiner; | ||
|
||
@SuppressWarnings("PMD.DataClass") | ||
public final class JdbiConfigCacheStats { | ||
|
||
private final String configName; | ||
private final int size; | ||
private final int cacheHits; | ||
private final int cacheMisses; | ||
private final int cacheWrites; | ||
private final int globalHits; | ||
private final int globalMisses; | ||
private final int globalWrites; | ||
|
||
JdbiConfigCacheStats(String configName, int cacheSize, JdbiConfigCacheMetrics cacheMetrics, JdbiConfigCacheMetrics globalMetrics) { | ||
this.configName = configName; | ||
this.size = cacheSize; | ||
this.cacheHits = cacheMetrics.getCacheHits(); | ||
this.cacheMisses = cacheMetrics.getCacheMisses(); | ||
this.cacheWrites = cacheMetrics.getCacheWrites(); | ||
this.globalHits = globalMetrics.getCacheHits(); | ||
this.globalMisses = globalMetrics.getCacheMisses(); | ||
this.globalWrites = globalMetrics.getCacheWrites(); | ||
} | ||
|
||
public String configName() { | ||
return configName; | ||
} | ||
|
||
public String getConfigName() { | ||
return configName; | ||
} | ||
|
||
public int getSize() { | ||
return size; | ||
} | ||
|
||
public int getCacheHits() { | ||
return cacheHits; | ||
} | ||
|
||
public int getCacheMisses() { | ||
return cacheMisses; | ||
} | ||
|
||
public int getCacheAccesses() { | ||
return cacheMisses + cacheHits; | ||
} | ||
|
||
public int getCacheWrites() { | ||
return cacheWrites; | ||
} | ||
|
||
public int getGlobalHits() { | ||
return globalHits; | ||
} | ||
|
||
public int getGlobalMisses() { | ||
return globalMisses; | ||
} | ||
|
||
public int getGlobalAccesses() { | ||
return globalMisses + globalHits; | ||
} | ||
|
||
public int getGlobalWrites() { | ||
return globalWrites; | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
return new StringJoiner(", ", JdbiConfigCacheStats.class.getSimpleName() + "[", "]") | ||
.add("configName='" + configName + "'") | ||
.add("size=" + size) | ||
.add("cacheHits=" + cacheHits) | ||
.add("cacheMisses=" + cacheMisses) | ||
.add("cacheWrites=" + cacheWrites) | ||
.add("globalHits=" + globalHits) | ||
.add("globalMisses=" + globalMisses) | ||
.add("globalWrites=" + globalWrites) | ||
.toString(); | ||
} | ||
} |
50 changes: 50 additions & 0 deletions
50
core/src/main/java/org/jdbi/v3/core/config/cache/JdbiConfigCacheStatsReporter.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
/* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package org.jdbi.v3.core.config.cache; | ||
|
||
import java.util.Set; | ||
|
||
import org.jdbi.v3.core.Handle; | ||
import org.jdbi.v3.core.HandleListener; | ||
import org.jdbi.v3.core.statement.StatementContext; | ||
import org.jdbi.v3.core.statement.StatementContextListener; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
||
import static java.lang.String.format; | ||
|
||
public final class JdbiConfigCacheStatsReporter implements StatementContextListener, HandleListener { | ||
|
||
public static final JdbiConfigCacheStatsReporter INSTANCE = new JdbiConfigCacheStatsReporter(); | ||
|
||
private static final Logger LOG = LoggerFactory.getLogger(JdbiConfigCacheStatsReporter.class); | ||
|
||
@Override | ||
public void handleClosed(Handle handle) { | ||
Set<JdbiConfigCacheStats> cacheStats = handle.getConfig().reportStats(); | ||
report("Handle", cacheStats); | ||
} | ||
|
||
@Override | ||
public void contextCleaned(StatementContext statementContext) { | ||
Set<JdbiConfigCacheStats> cacheStats = statementContext.getConfig().reportStats(); | ||
report("Context", cacheStats); | ||
} | ||
|
||
private void report(String context, Set<JdbiConfigCacheStats> stats) { | ||
LOG.info("------------------------------------------------------------------------"); | ||
LOG.info(format("Cache Stats for %s", context)); | ||
stats.forEach(s -> LOG.info(s.toString())); | ||
} | ||
} |
Oops, something went wrong.