Skip to content

Commit b46704d

Browse files
committed
Update module 'chimera' to use Caffeine.
Signed-off-by: Lukas Mansour <lukas.mansour@desy.de>
1 parent 2a0aa55 commit b46704d

2 files changed

Lines changed: 37 additions & 35 deletions

File tree

modules/chimera/pom.xml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,10 @@
2626
<groupId>com.google.guava</groupId>
2727
<artifactId>guava</artifactId>
2828
</dependency>
29+
<dependency>
30+
<groupId>com.github.ben-manes.caffeine</groupId>
31+
<artifactId>caffeine</artifactId>
32+
</dependency>
2933
<dependency>
3034
<groupId>com.github.spotbugs</groupId>
3135
<artifactId>spotbugs-annotations</artifactId>

modules/chimera/src/main/java/org/dcache/chimera/JdbcFs.java

Lines changed: 33 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -26,11 +26,11 @@
2626
import static org.dcache.util.ByteUnit.EiB;
2727
import static org.dcache.util.SqlHelper.tryToClose;
2828

29+
import com.github.benmanes.caffeine.cache.AsyncLoadingCache;
30+
import com.github.benmanes.caffeine.cache.Cache;
31+
import com.github.benmanes.caffeine.cache.CacheLoader;
32+
import com.github.benmanes.caffeine.cache.Caffeine;
2933
import com.google.common.base.Throwables;
30-
import com.google.common.cache.Cache;
31-
import com.google.common.cache.CacheBuilder;
32-
import com.google.common.cache.CacheLoader;
33-
import com.google.common.cache.LoadingCache;
3434
import com.google.common.io.ByteSource;
3535
import com.google.common.util.concurrent.ThreadFactoryBuilder;
3636
import diskCacheV111.util.RetentionPolicy;
@@ -45,7 +45,8 @@
4545
import java.util.List;
4646
import java.util.Map;
4747
import java.util.Set;
48-
import java.util.concurrent.ExecutionException;
48+
import java.util.concurrent.CompletableFuture;
49+
import java.util.concurrent.CompletionException;
4950
import java.util.concurrent.Executor;
5051
import java.util.concurrent.Executors;
5152
import java.util.concurrent.ScheduledExecutorService;
@@ -127,30 +128,23 @@ public class JdbcFs implements FileSystemProvider, LeaderLatchListener {
127128
.build()
128129
);
129130

130-
private final LoadingCache<Object, FsStat> _fsStatCache
131-
= CacheBuilder.newBuilder()
131+
private final AsyncLoadingCache<Object, FsStat> _fsStatCache
132+
= Caffeine.newBuilder()
132133
.refreshAfterWrite(100, TimeUnit.MILLISECONDS)
133-
.build(
134-
CacheLoader.asyncReloading(new CacheLoader<Object, FsStat>() {
135-
136-
@Override
137-
public FsStat load(Object k) throws Exception {
138-
return JdbcFs.this.getFsStat0();
139-
}
140-
}
141-
, _fsStatUpdateExecutor));
134+
.executor(_fsStatUpdateExecutor)
135+
.buildAsync(key -> JdbcFs.this.getFsStat0());
142136

143137
/* The PNFS ID to inode number mapping will never change while dCache is running.
144138
*/
145139
protected final Cache<String, Long> _inoCache =
146-
CacheBuilder.newBuilder()
140+
Caffeine.newBuilder()
147141
.maximumSize(100000)
148142
.build();
149143

150144
/* The inode number to PNFS ID mapping will never change while dCache is running.
151145
*/
152146
protected final Cache<Long, String> _idCache =
153-
CacheBuilder.newBuilder()
147+
Caffeine.newBuilder()
154148
.maximumSize(100000)
155149
.build();
156150

@@ -725,36 +719,40 @@ public FsInode path2inode(String path, FsInode startFrom) throws ChimeraFsExcept
725719
@Override
726720
public String inode2id(FsInode inode) throws ChimeraFsException {
727721
try {
728-
return _idCache.get(inode.ino(), () -> {
722+
return _idCache.get(inode.ino(), (key) -> {
729723
String id = _sqlDriver.getId(inode);
730724
if (id == null) {
731-
throw FileNotFoundChimeraFsException.of(inode);
725+
throw new RuntimeException(FileNotFoundChimeraFsException.of(inode));
732726
}
733727
return id;
734728
});
735-
} catch (ExecutionException e) {
736-
Throwables.throwIfInstanceOf(e.getCause(), ChimeraFsException.class);
737-
Throwables.throwIfInstanceOf(e.getCause(), DataAccessException.class);
738-
Throwables.throwIfUnchecked(e.getCause());
739-
throw new RuntimeException(e.getCause());
729+
} catch (RuntimeException e) {
730+
if (e.getCause() != null) {
731+
Throwables.throwIfInstanceOf(e.getCause(), ChimeraFsException.class);
732+
Throwables.throwIfInstanceOf(e.getCause(), DataAccessException.class);
733+
Throwables.throwIfUnchecked(e.getCause());
734+
}
735+
throw e;
740736
}
741737
}
742738

743739
@Override
744740
public FsInode id2inode(String id, StatCacheOption option) throws ChimeraFsException {
745741
if (option == NO_STAT) {
746742
try {
747-
return new FsInode(this, _inoCache.get(id, () -> {
743+
return new FsInode(this, _inoCache.get(id, (key) -> {
748744
Long ino = _sqlDriver.getInumber(id);
749745
if (ino == null) {
750-
throw FileNotFoundChimeraFsException.ofPnfsId(id);
746+
throw new RuntimeException(FileNotFoundChimeraFsException.ofPnfsId(id));
751747
}
752748
return ino;
753749
}));
754-
} catch (ExecutionException e) {
755-
Throwables.throwIfInstanceOf(e.getCause(), ChimeraFsException.class);
756-
Throwables.throwIfInstanceOf(e.getCause(), DataAccessException.class);
757-
Throwables.throwIfUnchecked(e.getCause());
750+
} catch (RuntimeException e) {
751+
if (e.getCause() != null) {
752+
Throwables.throwIfInstanceOf(e.getCause(), ChimeraFsException.class);
753+
Throwables.throwIfInstanceOf(e.getCause(), DataAccessException.class);
754+
Throwables.throwIfUnchecked(e.getCause());
755+
}
758756
throw new RuntimeException(e.getCause());
759757
}
760758
} else {
@@ -1403,19 +1401,19 @@ private static void checkNameLength(String name) throws InvalidNameChimeraExcept
14031401
}
14041402

14051403
@Override
1406-
public void updateFsStat() throws ChimeraFsException {
1404+
public void updateFsStat() {
14071405
_sqlDriver.updateFsStat();
14081406
}
14091407

1410-
public FsStat getFsStat0() throws ChimeraFsException {
1408+
public FsStat getFsStat0() {
14111409
return _sqlDriver.getFsStat();
14121410
}
14131411

14141412
@Override
14151413
public FsStat getFsStat() throws ChimeraFsException {
14161414
try {
1417-
return _fsStatCache.get(DUMMY_KEY);
1418-
} catch (ExecutionException e) {
1415+
return _fsStatCache.synchronous().get(DUMMY_KEY);
1416+
} catch (CompletionException e) {
14191417
Throwable t = e.getCause();
14201418
Throwables.propagateIfPossible(t, ChimeraFsException.class);
14211419
throw new ChimeraFsException(t.getMessage(), t);

0 commit comments

Comments
 (0)