Skip to content

Commit 7bde5f9

Browse files
committed
Update module 'dcache-bulk', 'dcache-info', 'dcache-nfs', 'dcache-webdav', 'gplamza-oidc', 'srm-server' and 'dache-qos' to use Caffeine.
Signed-off-by: Lukas Mansour <lukas.mansour@desy.de>
1 parent 4259e18 commit 7bde5f9

9 files changed

Lines changed: 124 additions & 123 deletions

File tree

modules/dcache-bulk/src/main/java/org/dcache/services/bulk/store/jdbc/request/JdbcBulkRequestStore.java

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -72,11 +72,11 @@ LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
7272
import static org.dcache.services.bulk.util.BulkRequestTarget.ROOT_REQUEST_PATH;
7373
import static org.dcache.services.bulk.util.BulkRequestTarget.State.CREATED;
7474

75+
import com.github.benmanes.caffeine.cache.CacheLoader;
76+
import com.github.benmanes.caffeine.cache.Caffeine;
77+
import com.github.benmanes.caffeine.cache.LoadingCache;
7578
import com.google.common.base.Strings;
7679
import com.google.common.base.Throwables;
77-
import com.google.common.cache.CacheBuilder;
78-
import com.google.common.cache.CacheLoader;
79-
import com.google.common.cache.LoadingCache;
8080
import com.google.common.collect.ArrayListMultimap;
8181
import com.google.common.collect.ListMultimap;
8282
import diskCacheV111.util.CacheException;
@@ -91,6 +91,7 @@ LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
9191
import java.util.Map;
9292
import java.util.Optional;
9393
import java.util.Set;
94+
import java.util.concurrent.CompletionException;
9495
import java.util.concurrent.ExecutionException;
9596
import java.util.concurrent.TimeUnit;
9697
import java.util.function.Consumer;
@@ -139,7 +140,7 @@ public final class JdbcBulkRequestStore implements BulkRequestStore {
139140

140141
private static final Integer FETCH_SIZE = 10000;
141142

142-
class RequestLoader extends CacheLoader<String, Optional<BulkRequest>> {
143+
class RequestLoader implements CacheLoader<String, Optional<BulkRequest>> {
143144

144145
@Override
145146
public Optional<BulkRequest> load(String uid) throws Exception {
@@ -170,7 +171,7 @@ public Optional<BulkRequest> load(String uid) throws Exception {
170171
private PnfsHandler pnfsHandler;
171172

172173
public void initialize() {
173-
requestCache = CacheBuilder.newBuilder()
174+
requestCache = Caffeine.newBuilder()
174175
.expireAfterAccess(expiry, expiryUnit)
175176
.maximumSize(capacity)
176177
.build(new RequestLoader());
@@ -232,7 +233,7 @@ public void clear(Subject subject, String uid)
232233
Optional<BulkRequest> stored;
233234
try {
234235
stored = requestCache.get(uid);
235-
} catch (ExecutionException e) {
236+
} catch (CompletionException e) {
236237
throw new BulkStorageException(e.getMessage(), e.getCause());
237238
}
238239

@@ -251,7 +252,7 @@ public void clear(String uid) {
251252

252253
try {
253254
stored = requestCache.get(uid);
254-
} catch (ExecutionException e) {
255+
} catch (CompletionException e) {
255256
Throwable cause = e.getCause();
256257
LOGGER.error("Fatal error trying to clear {}: "
257258
+ "{}.", uid, cause == null ? e.getMessage() : cause.getMessage());
@@ -354,7 +355,7 @@ public Optional<BulkRequest> getRequest(String uid) throws BulkStorageException
354355
Optional<BulkRequest> stored = Optional.empty();
355356
try {
356357
stored = requestCache.get(uid);
357-
} catch (ExecutionException e) {
358+
} catch (CompletionException e) {
358359
Throwable cause = e.getCause();
359360
LOGGER.error("Fatal error trying to get request {}: "
360361
+ "{}.", uid, cause == null ? e.getMessage() : cause.getMessage());
@@ -542,7 +543,7 @@ public void reset(String uid) throws BulkStorageException {
542543
status.setLastModified(System.currentTimeMillis());
543544
status.setCompletedAt(null);
544545
});
545-
} catch (ExecutionException e) {
546+
} catch (CompletionException e) {
546547
throw new BulkStorageException(e.getMessage(), e.getCause());
547548
}
548549
}
@@ -712,7 +713,7 @@ public boolean update(String uid, BulkRequestStatus status)
712713
default:
713714
}
714715
});
715-
} catch (ExecutionException e) {
716+
} catch (CompletionException e) {
716717
throw new BulkStorageException(e.getMessage(), e.getCause());
717718
}
718719

@@ -805,7 +806,7 @@ private void clear(BulkRequest request) {
805806
private BulkRequest get(String uid) throws BulkStorageException {
806807
try {
807808
return requestCache.get(uid).orElse(null);
808-
} catch (ExecutionException e) {
809+
} catch (CompletionException e) {
809810
Throwable cause = e.getCause();
810811
throw new BulkStorageException(
811812
cause == null ? e.getMessage() : cause.getMessage());

modules/dcache-info/src/main/java/org/dcache/services/info/InfoHttpEngine.java

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,10 @@
88
import static java.util.Arrays.asList;
99
import static java.util.concurrent.TimeUnit.MILLISECONDS;
1010

11+
import com.github.benmanes.caffeine.cache.CacheLoader;
12+
import com.github.benmanes.caffeine.cache.Caffeine;
13+
import com.github.benmanes.caffeine.cache.LoadingCache;
1114
import com.google.common.base.Splitter;
12-
import com.google.common.cache.CacheBuilder;
13-
import com.google.common.cache.CacheLoader;
14-
import com.google.common.cache.LoadingCache;
1515
import com.google.common.collect.ImmutableMap;
1616
import diskCacheV111.util.CacheException;
1717
import diskCacheV111.util.TimeoutCacheException;
@@ -28,6 +28,7 @@
2828
import java.util.Arrays;
2929
import java.util.List;
3030
import java.util.Map;
31+
import java.util.concurrent.CompletionException;
3132
import java.util.concurrent.ExecutionException;
3233
import java.util.concurrent.TimeUnit;
3334
import org.dcache.cells.CellStub;
@@ -93,10 +94,10 @@ private class SerialisationHandler {
9394
private final String _name;
9495
private final String _mimeType;
9596

96-
LoadingCache<List<String>, String> resultCache = CacheBuilder.newBuilder()
97+
LoadingCache<List<String>, String> resultCache = Caffeine.newBuilder()
9798
.maximumSize(10)
9899
.expireAfterWrite(1, TimeUnit.SECONDS)
99-
.build(new CacheLoader<List<String>, String>() {
100+
.build(new CacheLoader<>() {
100101
@Override
101102
public String load(List<String> path)
102103
throws InterruptedException, CacheException, NoRouteToCellException {
@@ -125,7 +126,7 @@ public void handleRequest(HttpRequest request) throws HttpException {
125126
request.printHttpHeader(raw.length);
126127
request.setContentType(this._mimeType);
127128
out.write(raw);
128-
} catch (ExecutionException e) {
129+
} catch (CompletionException e) {
129130
Throwable cause = e.getCause();
130131
if (cause instanceof TimeoutCacheException) {
131132
throw new HttpException(503, "The info cell took too " +

modules/dcache-nfs/src/main/java/org/dcache/chimera/nfsv41/door/DoorOperationFactory.java

Lines changed: 14 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -20,13 +20,14 @@
2020
import static com.google.common.base.Throwables.getRootCause;
2121
import static java.nio.charset.StandardCharsets.UTF_8;
2222

23-
import com.google.common.cache.CacheBuilder;
24-
import com.google.common.cache.CacheLoader;
25-
import com.google.common.cache.LoadingCache;
23+
import com.github.benmanes.caffeine.cache.CacheLoader;
24+
import com.github.benmanes.caffeine.cache.Caffeine;
25+
import com.github.benmanes.caffeine.cache.LoadingCache;
2626
import java.io.IOException;
2727
import java.security.Principal;
2828
import java.security.PrivilegedAction;
2929
import java.util.Optional;
30+
import java.util.concurrent.CompletionException;
3031
import java.util.concurrent.ExecutionException;
3132
import java.util.concurrent.TimeUnit;
3233
import java.util.function.Function;
@@ -110,7 +111,7 @@ public DoorOperationFactory(ProxyIoFactory proxyIoFactory, ChimeraVfs fs,
110111
_proxyIoFactory = proxyIoFactory;
111112
_vfs = fs;
112113
_jdbcFs = jdbcFs;
113-
_pathCache = CacheBuilder.newBuilder()
114+
_pathCache = Caffeine.newBuilder()
114115
.maximumSize(512)
115116
.expireAfterWrite(30, TimeUnit.SECONDS)
116117
.softValues()
@@ -137,7 +138,7 @@ public DoorOperationFactory(ProxyIoFactory proxyIoFactory, ChimeraVfs fs,
137138
_inode2path = i -> {
138139
try {
139140
return _pathCache.get(i);
140-
} catch (ExecutionException e) {
141+
} catch (CompletionException e) {
141142
Throwable t = getRootCause(e);
142143
LOG.error("Failed to get inode path {} : {}", i, t.getMessage());
143144
return "inode:" + i;
@@ -157,19 +158,14 @@ public DoorOperationFactory(ProxyIoFactory proxyIoFactory, ChimeraVfs fs,
157158
}
158159

159160
if (subjectMapper.isPresent()) {
160-
CacheLoader<Principal, Subject> loader = new CacheLoader<Principal, Subject>() {
161-
@Override
162-
public Subject load(Principal key) throws Exception {
163-
Subject in = new Subject();
164-
in.getPrincipals().add(key);
165-
return subjectMapper.get().login(in);
166-
}
167-
};
168-
169-
_subjectCache = Optional.of(CacheBuilder.newBuilder()
161+
_subjectCache = Optional.of(Caffeine.newBuilder()
170162
.maximumSize(2048)
171163
.expireAfterWrite(10, TimeUnit.MINUTES)
172-
.build(loader));
164+
.build(key -> {
165+
Subject in = new Subject();
166+
in.getPrincipals().add(key);
167+
return subjectMapper.get().login(in);
168+
}));
173169
} else {
174170
_subjectCache = Optional.empty();
175171
}
@@ -223,7 +219,7 @@ public void process(CompoundContext context, nfs_resop4 result)
223219
if (gids.length >= 16) {
224220
long uid = UnixSubjects.getUid(subject);
225221
UidPrincipal uidPrincipal = new UidPrincipal(uid);
226-
subject = _subjectCache.get().getUnchecked(uidPrincipal);
222+
subject = _subjectCache.get().get(uidPrincipal);
227223
context.getSubject().getPrincipals()
228224
.addAll(subject.getPrincipals());
229225
}
@@ -459,7 +455,7 @@ public void process(CompoundContext context, nfs_resop4 result)
459455
}
460456
}
461457

462-
private class ParentPathLoader extends CacheLoader<FsInode, String> {
458+
private class ParentPathLoader implements CacheLoader<FsInode, String> {
463459

464460
@Override
465461
public String load(FsInode inode) throws Exception {

modules/dcache-nfs/src/main/java/org/dcache/chimera/nfsv41/door/proxy/NfsProxyIoFactory.java

Lines changed: 34 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,14 @@
22

33
import static org.dcache.chimera.nfsv41.door.ExceptionUtils.asNfsException;
44

5+
import com.github.benmanes.caffeine.cache.Cache;
6+
import com.github.benmanes.caffeine.cache.Caffeine;
57
import com.google.common.base.Stopwatch;
6-
import com.google.common.cache.Cache;
7-
import com.google.common.cache.CacheBuilder;
88
import com.google.common.net.HostAndPort;
99
import java.io.IOException;
1010
import java.net.InetAddress;
1111
import java.net.InetSocketAddress;
1212
import java.util.Arrays;
13-
import java.util.concurrent.ExecutionException;
1413
import java.util.concurrent.TimeUnit;
1514
import java.util.function.Consumer;
1615
import java.util.stream.Collectors;
@@ -59,7 +58,7 @@ public class NfsProxyIoFactory implements ProxyIoFactory {
5958
private static final Logger _log = LoggerFactory.getLogger(NfsProxyIoFactory.class);
6059

6160
private final Cache<stateid4, ProxyIoAdapter> _proxyIO
62-
= CacheBuilder.newBuilder()
61+
= Caffeine.newBuilder()
6362
.build();
6463

6564
private final NFSv41DeviceManager deviceManager;
@@ -78,29 +77,36 @@ public ProxyIoAdapter getOrCreateProxy(Inode inode, stateid4 stateid, CompoundCo
7877
boolean isWrite) throws IOException {
7978
try {
8079
return _proxyIO.get(stateid,
81-
() -> {
82-
final NFS4Client nfsClient;
83-
if (context.getMinorversion() == 1) {
84-
nfsClient = context.getSession().getClient();
85-
} else {
86-
nfsClient = context.getStateHandler().getClientIdByStateId(stateid);
87-
}
88-
89-
final NFS4State state = nfsClient.state(stateid);
90-
final ProxyIoAdapter adapter = createIoAdapter(inode, stateid, context,
91-
isWrite);
92-
93-
state.addDisposeListener(s -> {
94-
tryToClose(adapter);
95-
_proxyIO.invalidate(s.stateid());
96-
});
97-
98-
return adapter;
99-
});
100-
} catch (ExecutionException e) {
101-
Throwable t = e.getCause();
102-
_log.debug("failed to create IO adapter: {}", t.getMessage());
103-
throw asNfsException(t, NfsIoException.class);
80+
(key) -> {
81+
try {
82+
final NFS4Client nfsClient;
83+
if (context.getMinorversion() == 1) {
84+
nfsClient = context.getSession().getClient();
85+
} else {
86+
nfsClient = context.getStateHandler().getClientIdByStateId(stateid);
87+
}
88+
89+
final NFS4State state = nfsClient.state(stateid);
90+
final ProxyIoAdapter adapter = createIoAdapter(inode, stateid, context,
91+
isWrite);
92+
93+
state.addDisposeListener(s -> {
94+
tryToClose(adapter);
95+
_proxyIO.invalidate(s.stateid());
96+
});
97+
98+
return adapter;
99+
} catch (IOException e) {
100+
throw new RuntimeException(e);
101+
}});
102+
} catch (RuntimeException e) {
103+
if (e.getCause() != null) {
104+
Throwable t = e.getCause();
105+
_log.debug("failed to create IO adapter: {}", t.getMessage());
106+
throw asNfsException(t, NfsIoException.class);
107+
} else {
108+
throw e;
109+
}
104110
}
105111
}
106112

@@ -233,7 +239,7 @@ public void forEach(Consumer<ProxyIoAdapter> action) {
233239

234240
@Override
235241
public int getCount() {
236-
return (int) _proxyIO.size();
242+
return (int) _proxyIO.estimatedSize();
237243
}
238244

239245
public static nfsv4_1_file_layout4 decodeLayoutId(byte[] data) throws IOException {

modules/dcache-qos/src/main/java/org/dcache/qos/services/engine/util/QoSPolicyCache.java

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -59,16 +59,16 @@ LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
5959
*/
6060
package org.dcache.qos.services.engine.util;
6161

62+
import com.github.benmanes.caffeine.cache.CacheLoader;
63+
import com.github.benmanes.caffeine.cache.Caffeine;
64+
import com.github.benmanes.caffeine.cache.LoadingCache;
6265
import com.google.common.base.Throwables;
63-
import com.google.common.cache.CacheBuilder;
64-
import com.google.common.cache.CacheLoader;
65-
import com.google.common.cache.LoadingCache;
6666
import diskCacheV111.util.CacheException;
6767
import diskCacheV111.util.PermissionDeniedCacheException;
6868
import dmg.cells.nucleus.CellMessageReceiver;
6969
import dmg.cells.nucleus.NoRouteToCellException;
7070
import java.util.Optional;
71-
import java.util.concurrent.ExecutionException;
71+
import java.util.concurrent.CompletionException;
7272
import java.util.concurrent.TimeUnit;
7373
import javax.security.auth.Subject;
7474
import org.dcache.auth.Subjects;
@@ -95,7 +95,7 @@ public class QoSPolicyCache implements CellMessageReceiver{
9595

9696
private static final Logger LOGGER = LoggerFactory.getLogger(QoSPolicyCache.class);
9797

98-
class PolicyLoader extends CacheLoader<String, QoSPolicy> {
98+
class PolicyLoader implements CacheLoader<String, QoSPolicy> {
9999

100100
@Override
101101
public QoSPolicy load(String name) throws Exception {
@@ -137,7 +137,7 @@ public void setCapacity(long capacity) {
137137
}
138138

139139
public void initialize() {
140-
policyCache = CacheBuilder.newBuilder()
140+
policyCache = Caffeine.newBuilder()
141141
.expireAfterAccess(expiry, expiryUnit)
142142
.maximumSize(capacity)
143143
.build(new PolicyLoader());
@@ -210,7 +210,7 @@ public PnfsManagerListQoSPoliciesMessage messageArrived(PnfsManagerListQoSPolici
210210
public Optional<QoSPolicy> getPolicy(String name) {
211211
try {
212212
return Optional.ofNullable(policyCache.get(name));
213-
} catch (ExecutionException e) {
213+
} catch (CompletionException e) {
214214
LOGGER.error("problem getting {}: {}.", name, String.valueOf(Throwables.getRootCause(e)));
215215
return Optional.empty();
216216
}

0 commit comments

Comments
 (0)