Skip to content

Commit 4259e18

Browse files
committed
Update module 'dcache-core', 'dcache-ftp', 'dcache-srm' and 'srm-client' to use Caffeine.
Signed-off-by: Lukas Mansour <lukas.mansour@desy.de>
1 parent e0bbe91 commit 4259e18

16 files changed

Lines changed: 197 additions & 183 deletions

File tree

modules/dcache-ftp/src/main/java/org/dcache/ftp/door/AbstractFtpDoorV1.java

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@ LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
7171
import static java.lang.annotation.ElementType.METHOD;
7272
import static java.lang.annotation.RetentionPolicy.RUNTIME;
7373
import static java.nio.charset.StandardCharsets.UTF_8;
74+
import static java.util.Collections.synchronizedList;
7475
import static java.util.Objects.requireNonNull;
7576
import static org.dcache.acl.enums.AccessType.ACCESS_ALLOWED;
7677
import static org.dcache.auth.attributes.Activity.DELETE;
@@ -102,11 +103,11 @@ LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
102103
import static org.dcache.util.TransferRetryPolicy.maximumTries;
103104
import static org.dcache.util.TransferRetryPolicy.tryOnce;
104105

106+
import com.github.benmanes.caffeine.cache.LoadingCache;
105107
import com.google.common.annotations.VisibleForTesting;
106108
import com.google.common.base.Splitter;
107109
import com.google.common.base.Strings;
108110
import com.google.common.base.Throwables;
109-
import com.google.common.cache.LoadingCache;
110111
import com.google.common.collect.ImmutableMap;
111112
import com.google.common.collect.Lists;
112113
import com.google.common.collect.Ordering;
@@ -200,7 +201,7 @@ LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
200201
import java.util.TimeZone;
201202
import java.util.Timer;
202203
import java.util.TimerTask;
203-
import java.util.concurrent.ExecutionException;
204+
import java.util.concurrent.CompletionException;
204205
import java.util.concurrent.Executor;
205206
import java.util.concurrent.TimeUnit;
206207
import java.util.function.Supplier;
@@ -267,8 +268,6 @@ LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
267268
import org.slf4j.Logger;
268269
import org.slf4j.LoggerFactory;
269270

270-
import static java.util.Collections.synchronizedList;
271-
272271
@Inherited
273272
@Retention(RUNTIME)
274273
@Target(METHOD)
@@ -1522,11 +1521,13 @@ public void setExecutor(Executor executor) {
15221521
_executor = new CDCExecutorDecorator<>(executor);
15231522
}
15241523

1525-
public void setSpaceDescriptionCache(LoadingCache<GetSpaceTokensKey, long[]> cache) {
1524+
public void setSpaceDescriptionCache(
1525+
LoadingCache<GetSpaceTokensKey, long[]> cache) {
15261526
_spaceDescriptionCache = cache;
15271527
}
15281528

1529-
public void setSpaceLookupCache(LoadingCache<String, Optional<Space>> cache) {
1529+
public void setSpaceLookupCache(
1530+
LoadingCache<String, Optional<Space>> cache) {
15301531
_spaceLookupCache = cache;
15311532
}
15321533

@@ -3233,7 +3234,7 @@ public void doUsage(String arg, boolean hasToken) throws FTPCommandException {
32333234
default:
32343235
throw new FTPCommandException(451, "Operation failed: " + e.getMessage(), e);
32353236
}
3236-
} catch (ExecutionException e) {
3237+
} catch (CompletionException e) {
32373238
Throwable cause = e.getCause();
32383239
Throwables.throwIfUnchecked(cause);
32393240
throw new FTPCommandException(451,

modules/dcache-ftp/src/main/java/org/dcache/ftp/door/FtpInterpreterFactory.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
*/
1818
package org.dcache.ftp.door;
1919

20-
import com.google.common.cache.LoadingCache;
20+
import com.github.benmanes.caffeine.cache.LoadingCache;
2121
import diskCacheV111.doors.LineBasedInterpreter;
2222
import diskCacheV111.doors.NettyLineBasedInterpreterFactory;
2323
import diskCacheV111.services.space.Space;

modules/dcache-srm/src/main/java/diskCacheV111/srm/SrmHandler.java

Lines changed: 13 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -51,11 +51,11 @@
5151
import static org.dcache.srm.v2_2.TStatusCode.SRM_SUCCESS;
5252
import static org.dcache.srm.v2_2.TStatusCode.SRM_TOO_MANY_RESULTS;
5353

54+
import com.github.benmanes.caffeine.cache.CacheLoader;
55+
import com.github.benmanes.caffeine.cache.Caffeine;
56+
import com.github.benmanes.caffeine.cache.LoadingCache;
5457
import com.google.common.base.Strings;
5558
import com.google.common.base.Throwables;
56-
import com.google.common.cache.CacheBuilder;
57-
import com.google.common.cache.CacheLoader;
58-
import com.google.common.cache.LoadingCache;
5959
import com.google.common.collect.ImmutableMap;
6060
import com.google.common.collect.ImmutableSet;
6161
import com.google.common.net.InetAddresses;
@@ -257,17 +257,14 @@ public class SrmHandler implements CellInfoProvider, CuratorFrameworkAware {
257257

258258
private final CertificateFactory cf = CertificateFactories.newX509CertificateFactory();
259259

260-
private final LoadingCache<Class, Optional<Field>> requestTokenFieldCache = CacheBuilder.newBuilder()
261-
.build(new CacheLoader<Class, Optional<Field>>() {
262-
@Override
263-
public Optional<Field> load(Class clazz) {
264-
try {
265-
Field field = clazz.getDeclaredField("requestToken");
266-
field.setAccessible(true);
267-
return Optional.of(field);
268-
} catch (NoSuchFieldException e) {
269-
return Optional.empty();
270-
}
260+
private final LoadingCache<Class<?>, Optional<Field>> requestTokenFieldCache = Caffeine.newBuilder()
261+
.build(clazz -> {
262+
try {
263+
Field field = clazz.getDeclaredField("requestToken");
264+
field.setAccessible(true);
265+
return Optional.of(field);
266+
} catch (NoSuchFieldException e) {
267+
return Optional.empty();
271268
}
272269
});
273270

@@ -727,7 +724,7 @@ public void close() {
727724
}
728725

729726
private MappedRequest mapRequest(Object request) throws SRMInternalErrorException {
730-
Optional<Field> field = requestTokenFieldCache.getUnchecked(request.getClass());
727+
Optional<Field> field = requestTokenFieldCache.get(request.getClass());
731728
if (field.isPresent()) {
732729
try {
733730
Field f = field.get();
@@ -803,7 +800,7 @@ private SrmReleaseFilesResponse mapReleaseFilesResponse(SrmReleaseFilesRequest r
803800

804801
private Object mapResponse(SrmResponse response) {
805802
Object o = response.getResponse();
806-
Optional<Field> field = requestTokenFieldCache.getUnchecked(o.getClass());
803+
Optional<Field> field = requestTokenFieldCache.get(o.getClass());
807804
field.ifPresent(f -> {
808805
try {
809806
f.set(o, prefix(response.getId(), (String) f.get(o)));

modules/dcache-srm/src/main/java/diskCacheV111/srm/dcache/CanonicalizingByteArrayStore.java

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,15 +19,16 @@
1919

2020
import static org.dcache.util.ByteUnit.KiB;
2121

22+
import com.github.benmanes.caffeine.cache.Cache;
23+
import com.github.benmanes.caffeine.cache.Caffeine;
2224
import com.google.common.base.Throwables;
23-
import com.google.common.cache.Cache;
24-
import com.google.common.cache.CacheBuilder;
2525
import com.google.common.hash.HashCode;
2626
import com.google.common.hash.Hashing;
2727
import com.google.common.util.concurrent.Striped;
2828
import com.google.common.util.concurrent.UncheckedExecutionException;
2929
import java.util.Arrays;
3030
import java.util.List;
31+
import java.util.concurrent.CompletionException;
3132
import java.util.concurrent.ExecutionException;
3233
import java.util.concurrent.TimeUnit;
3334
import java.util.concurrent.locks.Lock;
@@ -108,7 +109,7 @@ public int hashCode() {
108109
* Cache to reduce frequent reloads from the database.
109110
*/
110111
private final Cache<Long, byte[]> cache =
111-
CacheBuilder.newBuilder().expireAfterAccess(10, TimeUnit.MINUTES).maximumSize(1000)
112+
Caffeine.newBuilder().expireAfterAccess(10, TimeUnit.MINUTES).maximumSize(1000)
112113
.build();
113114

114115
/**
@@ -117,7 +118,7 @@ public int hashCode() {
117118
* <p>
118119
* Class invariant: Any Token has a corresponding byte array in the database.
119120
*/
120-
private final Cache<Long, Token> canonicalizationCache = CacheBuilder.newBuilder().weakValues()
121+
private final Cache<Long, Token> canonicalizationCache = Caffeine.newBuilder().weakValues()
121122
.build();
122123

123124
/**
@@ -217,8 +218,8 @@ public byte[] readBytes(Token token) {
217218
*/
218219
private Token makeToken(long id) {
219220
try {
220-
return canonicalizationCache.get(id, () -> new Token(id));
221-
} catch (UncheckedExecutionException | ExecutionException e) {
221+
return canonicalizationCache.get(id, (key) -> new Token(id));
222+
} catch (UncheckedExecutionException | CompletionException e) {
222223
Throwable cause = e.getCause();
223224
Throwables.throwIfUnchecked(cause);
224225
throw new RuntimeException(cause);

modules/dcache-srm/src/main/java/diskCacheV111/srm/dcache/Storage.java

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -81,14 +81,13 @@ LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
8181
import static org.dcache.srm.SRMInvalidPathException.checkValidPath;
8282
import static org.dcache.util.NetworkUtils.isInetAddress;
8383

84+
import com.github.benmanes.caffeine.cache.CacheLoader;
85+
import com.github.benmanes.caffeine.cache.Caffeine;
86+
import com.github.benmanes.caffeine.cache.LoadingCache;
8487
import com.google.common.base.CharMatcher;
85-
import com.google.common.base.Function;
8688
import com.google.common.base.Joiner;
8789
import com.google.common.base.Predicate;
8890
import com.google.common.base.Throwables;
89-
import com.google.common.cache.CacheBuilder;
90-
import com.google.common.cache.CacheLoader;
91-
import com.google.common.cache.LoadingCache;
9291
import com.google.common.collect.ImmutableMap;
9392
import com.google.common.collect.Maps;
9493
import com.google.common.collect.Ordering;
@@ -161,9 +160,8 @@ LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
161160
import java.util.Optional;
162161
import java.util.Random;
163162
import java.util.Set;
164-
import java.util.concurrent.CancellationException;
163+
import java.util.concurrent.CompletionException;
165164
import java.util.concurrent.ConcurrentHashMap;
166-
import java.util.concurrent.ExecutionException;
167165
import java.util.concurrent.Executor;
168166
import java.util.concurrent.Semaphore;
169167
import java.util.concurrent.atomic.AtomicLong;
@@ -201,7 +199,6 @@ LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
201199
import org.dcache.srm.CopyCallbacks;
202200
import org.dcache.srm.FileMetaData;
203201
import org.dcache.srm.RemoveFileCallback;
204-
import org.dcache.srm.SRMAbortedException;
205202
import org.dcache.srm.SRMAuthorizationException;
206203
import org.dcache.srm.SRMDuplicationException;
207204
import org.dcache.srm.SRMExceedAllocationException;
@@ -256,7 +253,7 @@ public final class Storage
256253
private static final Set<String> FTP_URL_SCHEMATA = Set.of("ftp", "gsiftp", "gkftp");
257254

258255
private static final LoadingCache<InetAddress, String> GET_HOST_BY_ADDR_CACHE =
259-
CacheBuilder.newBuilder()
256+
Caffeine.newBuilder()
260257
.expireAfterWrite(10, MINUTES)
261258
.recordStats()
262259
.build(new GetHostByAddressCacheLoader());
@@ -940,7 +937,7 @@ private String selectHostName(LoginBrokerInfo door, InetAddressScope scope,
940937
resolvedHost = GET_HOST_BY_ADDR_CACHE.get(address);
941938
}
942939
return resolvedHost;
943-
} catch (ExecutionException e) {
940+
} catch (CompletionException e) {
944941
Throwable cause = e.getCause();
945942
throw new SRMInternalErrorException("Failed to resolve door: " + cause, cause);
946943
}
@@ -1032,7 +1029,7 @@ public ListenableFuture<String> prepareToPut(
10321029
"Space associated with the space token " + spaceToken
10331030
+ " is not enough to hold SURL."));
10341031
}
1035-
} catch (ExecutionException e) {
1032+
} catch (CompletionException e) {
10361033
return immediateFailedFuture(new SRMException(
10371034
"Failure while querying space reservation: " + e.getCause()
10381035
.getMessage()));
@@ -2231,7 +2228,7 @@ public String[] srmGetSpaceTokens(SRMUser user, String description)
22312228
_log.trace("srmGetSpaceTokens returns: {}", Arrays.toString(tokens));
22322229
}
22332230
return Arrays.stream(tokens).mapToObj(Long::toString).toArray(String[]::new);
2234-
} catch (ExecutionException e) {
2231+
} catch (CompletionException e) {
22352232
Throwable cause = e.getCause();
22362233
Throwables.throwIfInstanceOf(cause, SRMException.class);
22372234
Throwables.throwIfUnchecked(cause);
@@ -2340,7 +2337,7 @@ private static DcacheUser asDcacheUser(SRMUser user) throws SRMAuthorizationExce
23402337
return dcacheUser;
23412338
}
23422339

2343-
private static class GetHostByAddressCacheLoader extends CacheLoader<InetAddress, String> {
2340+
private static class GetHostByAddressCacheLoader implements CacheLoader<InetAddress, String> {
23442341

23452342
@Override
23462343
public String load(InetAddress address) throws Exception {

modules/dcache/pom.xml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,10 @@
4646
<groupId>com.google.guava</groupId>
4747
<artifactId>guava</artifactId>
4848
</dependency>
49+
<dependency>
50+
<groupId>com.github.ben-manes.caffeine</groupId>
51+
<artifactId>caffeine</artifactId>
52+
</dependency>
4953

5054
<dependency>
5155
<groupId>org.dcache</groupId>

modules/dcache/src/main/java/diskCacheV111/doors/NettyLineBasedDoor.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
import static java.nio.charset.StandardCharsets.UTF_8;
2323
import static org.dcache.util.ByteUnit.KiB;
2424

25-
import com.google.common.cache.LoadingCache;
25+
import com.github.benmanes.caffeine.cache.LoadingCache;
2626
import com.google.common.net.InetAddresses;
2727
import diskCacheV111.services.space.Space;
2828
import dmg.cells.nucleus.CDC;

modules/dcache/src/main/java/diskCacheV111/doors/NettyLineBasedDoorFactory.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919

2020
package diskCacheV111.doors;
2121

22-
import com.google.common.cache.LoadingCache;
22+
import com.github.benmanes.caffeine.cache.LoadingCache;
2323
import com.google.common.util.concurrent.AbstractService;
2424
import com.google.common.util.concurrent.ThreadFactoryBuilder;
2525
import diskCacheV111.services.space.Space;
@@ -138,8 +138,8 @@ protected void doStart() {
138138

139139
CellStub spaceManager = new CellStub(parentEndpoint, spaceManagerPath, 30_000);
140140
spaceDescriptionCache = ReservationCaches.buildOwnerDescriptionLookupCache(spaceManager,
141-
executor);
142-
spaceLookupCache = ReservationCaches.buildSpaceLookupCache(spaceManager, executor);
141+
executor).synchronous();
142+
spaceLookupCache = ReservationCaches.buildSpaceLookupCache(spaceManager, executor).synchronous();
143143

144144
LoginStrategy loginStrategy = new RemoteLoginStrategy(
145145
new CellStub(parentEndpoint, gPlazma, 30_000));

modules/dcache/src/main/java/diskCacheV111/doors/NettyLineBasedInterpreterFactory.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
*/
1919
package diskCacheV111.doors;
2020

21-
import com.google.common.cache.LoadingCache;
21+
import com.github.benmanes.caffeine.cache.LoadingCache;
2222
import diskCacheV111.services.space.Space;
2323
import diskCacheV111.util.ConfigurationException;
2424
import dmg.cells.nucleus.CellAddressCore;

modules/dcache/src/main/java/diskCacheV111/poolManager/PoolSelectionUnitV2.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,13 @@
1010
import static org.dcache.namespace.FileAttribute.HSM;
1111
import static org.dcache.namespace.FileAttribute.STORAGECLASS;
1212

13+
import com.github.benmanes.caffeine.cache.Cache;
14+
import com.github.benmanes.caffeine.cache.Caffeine;
1315
import com.google.common.base.Joiner;
1416
import com.google.common.base.Preconditions;
1517
import com.google.common.base.Predicates;
1618
import com.google.common.base.Splitter;
1719
import com.google.common.base.Strings;
18-
import com.google.common.cache.Cache;
19-
import com.google.common.cache.CacheBuilder;
2020
import com.google.common.collect.ImmutableList;
2121
import com.google.common.collect.Lists;
2222
import com.google.common.collect.Maps;
@@ -97,7 +97,7 @@ public String getVersion() {
9797
private final Map<String, UGroup> _uGroups = new HashMap<>();
9898
private final Map<String, Unit> _units = new HashMap<>();
9999
private final Cache<String, PoolPreferenceLevel[]> cachedMatchValue =
100-
CacheBuilder.newBuilder()
100+
Caffeine.newBuilder()
101101
.maximumSize(100000)
102102
.build();
103103
private boolean _useRegex;

0 commit comments

Comments
 (0)