Skip to content

Commit

Permalink
ARTEMIS-4349: Replace Guava cache with Caffeine
Browse files Browse the repository at this point in the history
Combination of changes from Alexey Markevich with updates from Robbie Gemmell. See PRs for detail.

This closes #4584.
This closes #4540.
  • Loading branch information
amarkevich authored and gemmellr committed Aug 29, 2023
1 parent 56c8afe commit 50a1824
Show file tree
Hide file tree
Showing 11 changed files with 133 additions and 106 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@
*/
package org.apache.activemq.artemis.cli.commands.tools.xml;

import com.google.common.base.Preconditions;
import java.util.Objects;

import org.apache.activemq.artemis.api.core.ActiveMQBuffer;
import org.apache.activemq.artemis.api.core.Message;
import org.apache.activemq.artemis.api.core.SimpleString;
Expand Down Expand Up @@ -89,7 +90,7 @@ public static String getMessagePrettyType(byte rawType) {
* Base64 encode a ServerMessage body into the proper XML format
*/
public static String encodeMessageBodyBase64(final Message message) throws Exception {
Preconditions.checkNotNull(message, "ServerMessage can not be null");
Objects.requireNonNull(message, "ServerMessage can not be null");

ActiveMQBuffer byteBuffer = message.toCore().getReadOnlyBodyBuffer();
byte[] buffer = new byte[byteBuffer.writerIndex()];
Expand Down
3 changes: 1 addition & 2 deletions artemis-features/src/main/resources/features.xml
Original file line number Diff line number Diff line change
Expand Up @@ -73,8 +73,7 @@
<bundle dependency="true">mvn:org.apache.commons/commons-lang3/${commons.lang.version}</bundle>
<bundle dependency="true">mvn:org.jctools/jctools-core/${jctools.version}</bundle>
<bundle dependency="true">mvn:org.hdrhistogram/HdrHistogram/${hdrhistogram.version}</bundle>
<bundle dependency="true">mvn:com.google.guava/failureaccess/1.0.1</bundle>
<bundle dependency="true">mvn:com.google.guava/guava/${guava.version}</bundle>
<bundle dependency="true">mvn:com.github.ben-manes.caffeine/caffeine/${caffeine.version}</bundle>
<bundle dependency="true">mvn:org.apache.commons/commons-dbcp2/${commons.dbcp2.version}</bundle>
<bundle dependency="true">mvn:org.apache.commons/commons-pool2/${commons.pool2.version}</bundle>
<!-- Micrometer can't be included until it supports OSGi. It is currently an "optional" Maven dependency. -->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
import java.util.ArrayList;
import java.util.List;

import com.google.common.base.CaseFormat;
import io.netty.buffer.ByteBuf;
import io.netty.buffer.ByteBufUtil;
import io.netty.handler.codec.EncoderException;
Expand Down Expand Up @@ -49,6 +48,7 @@
import org.apache.activemq.artemis.core.config.WildcardConfiguration;
import org.apache.activemq.artemis.core.message.impl.CoreMessage;
import org.apache.activemq.artemis.reader.MessageUtil;
import org.apache.commons.text.CaseUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.lang.invoke.MethodHandles;
Expand Down Expand Up @@ -382,7 +382,7 @@ private static String formatByte(byte bite) {
}

private static String formatCase(String string) {
return CaseFormat.UPPER_UNDERSCORE.to(CaseFormat.LOWER_CAMEL, string);
return CaseUtils.toCamelCase(string, false, '_');
}

private static String getPayloadForLogging(MqttPublishMessage message, int maxPayloadLogSize) {
Expand Down
22 changes: 22 additions & 0 deletions artemis-quorum-ri/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,28 @@
<dependency>
<groupId>org.apache.curator</groupId>
<artifactId>curator-client</artifactId>
<exclusions>
<exclusion>
<groupId>com.google.guava</groupId>
<artifactId>listenablefuture</artifactId>
</exclusion>
<exclusion>
<groupId>com.google.code.findbugs</groupId>
<artifactId>jsr305</artifactId>
</exclusion>
<exclusion>
<groupId>org.checkerframework</groupId>
<artifactId>checker-qual</artifactId>
</exclusion>
<exclusion>
<groupId>com.google.errorprone</groupId>
<artifactId>error_prone_annotations</artifactId>
</exclusion>
<exclusion>
<groupId>com.google.j2objc</groupId>
<artifactId>j2objc-annotations</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.apache.zookeeper</groupId>
Expand Down
16 changes: 2 additions & 14 deletions artemis-server/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -51,17 +51,9 @@
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
<groupId>com.github.ben-manes.caffeine</groupId>
<artifactId>caffeine</artifactId>
<exclusions>
<exclusion>
<groupId>com.google.guava</groupId>
<artifactId>listenablefuture</artifactId>
</exclusion>
<exclusion>
<groupId>com.google.code.findbugs</groupId>
<artifactId>jsr305</artifactId>
</exclusion>
<exclusion>
<groupId>org.checkerframework</groupId>
<artifactId>checker-qual</artifactId>
Expand All @@ -70,10 +62,6 @@
<groupId>com.google.errorprone</groupId>
<artifactId>error_prone_annotations</artifactId>
</exclusion>
<exclusion>
<groupId>com.google.j2objc</groupId>
<artifactId>j2objc-annotations</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,6 @@
import java.util.Set;
import java.util.concurrent.TimeUnit;

import com.google.common.cache.Cache;
import com.google.common.cache.CacheBuilder;
import org.apache.activemq.artemis.api.core.Pair;
import org.apache.activemq.artemis.api.core.SimpleString;
import org.apache.activemq.artemis.api.core.management.CoreNotificationType;
Expand Down Expand Up @@ -54,6 +52,10 @@
import org.apache.activemq.artemis.utils.collections.TypedProperties;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.github.benmanes.caffeine.cache.Cache;
import com.github.benmanes.caffeine.cache.Caffeine;

import java.lang.invoke.MethodHandles;

/**
Expand Down Expand Up @@ -101,18 +103,18 @@ public SecurityStoreImpl(final HierarchicalRepository<Set<Role>> securityReposit
if (authenticationCacheSize == 0) {
authenticationCache = null;
} else {
authenticationCache = CacheBuilder.newBuilder()
.maximumSize(authenticationCacheSize)
.expireAfterWrite(invalidationInterval, TimeUnit.MILLISECONDS)
.build();
authenticationCache = Caffeine.newBuilder()
.maximumSize(authenticationCacheSize)
.expireAfterWrite(invalidationInterval, TimeUnit.MILLISECONDS)
.build();
}
if (authorizationCacheSize == 0) {
authorizationCache = null;
} else {
authorizationCache = CacheBuilder.newBuilder()
.maximumSize(authorizationCacheSize)
.expireAfterWrite(invalidationInterval, TimeUnit.MILLISECONDS)
.build();
authorizationCache = Caffeine.newBuilder()
.maximumSize(authorizationCacheSize)
.expireAfterWrite(invalidationInterval, TimeUnit.MILLISECONDS)
.build();
}
this.securityRepository.registerListener(this);
}
Expand Down Expand Up @@ -481,15 +483,15 @@ public long getAuthenticationCacheSize() {
if (authenticationCache == null) {
return 0;
} else {
return authenticationCache.size();
return authenticationCache.estimatedSize();
}
}

public long getAuthorizationCacheSize() {
if (authorizationCache == null) {
return 0;
} else {
return authorizationCache.size();
return authorizationCache.estimatedSize();
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,15 @@
*/
package org.apache.activemq.artemis.core.server.routing.caches;

import com.google.common.cache.CacheBuilder;
import com.google.common.cache.RemovalListener;
import com.google.common.cache.RemovalNotification;
import org.apache.activemq.artemis.core.persistence.StorageManager;
import org.apache.activemq.artemis.core.persistence.config.PersistedKeyValuePair;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.github.benmanes.caffeine.cache.Caffeine;
import com.github.benmanes.caffeine.cache.RemovalCause;
import com.github.benmanes.caffeine.cache.RemovalListener;

import java.lang.invoke.MethodHandles;

import java.util.Map;
Expand All @@ -37,7 +39,7 @@ public class LocalCache implements Cache, RemovalListener<String, String> {
private boolean persisted;
private int timeout;
private StorageManager storageManager;
private com.google.common.cache.Cache<String, String> cache;
private com.github.benmanes.caffeine.cache.Cache<String, String> cache;
private Map<String, PersistedKeyValuePair> persistedCacheEntries;

private volatile boolean running;
Expand All @@ -61,9 +63,9 @@ public LocalCache(String id, boolean persisted, int timeout, StorageManager stor
this.storageManager = storageManager;

if (timeout == 0) {
cache = CacheBuilder.newBuilder().build();
cache = Caffeine.newBuilder().executor(Runnable::run).build();
} else {
cache = CacheBuilder.newBuilder().removalListener(this).expireAfterAccess(timeout, TimeUnit.MILLISECONDS).build();
cache = Caffeine.newBuilder().removalListener(this).expireAfterAccess(timeout, TimeUnit.MILLISECONDS).executor(Runnable::run).build();
}
}

Expand Down Expand Up @@ -123,9 +125,9 @@ public void put(String key, String nodeId) {
}

@Override
public void onRemoval(RemovalNotification<String, String> notification) {
public void onRemoval(String key, String value, RemovalCause cause) {
if (running && persisted) {
PersistedKeyValuePair persistedKeyValuePair = persistedCacheEntries.remove(notification.getKey());
PersistedKeyValuePair persistedKeyValuePair = persistedCacheEntries.remove(key);

if (persistedKeyValuePair != null) {
try {
Expand Down
7 changes: 7 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,7 @@
<commons.io.version>2.10.0</commons.io.version>
<commons.codec.version>1.15</commons.codec.version>
<fuse.mqtt.client.version>1.16</fuse.mqtt.client.version>
<caffeine.version>3.1.6</caffeine.version>
<guava.version>32.0.1-jre</guava.version>
<hawtio.version>2.15.0</hawtio.version>
<jsr305.version>3.0.2</jsr305.version>
Expand Down Expand Up @@ -943,6 +944,12 @@
<version>${guava.version}</version>
<!-- License: Apache 2.0 -->
</dependency>
<dependency>
<groupId>com.github.ben-manes.caffeine</groupId>
<artifactId>caffeine</artifactId>
<version>${caffeine.version}</version>
<!-- License: Apache 2.0 -->
</dependency>
<dependency>
<groupId>com.google.code.findbugs</groupId>
<artifactId>jsr305</artifactId>
Expand Down

0 comments on commit 50a1824

Please sign in to comment.