use of org.dcache.cells.AbstractMessageCallback in project dcache by dCache.
the class ReservationCaches method buildWriteTokenLookupCache.
/**
* Cache queries to discover if a directory has the "WriteToken" tag set.
*/
public static LoadingCache<FsPath, java.util.Optional<String>> buildWriteTokenLookupCache(PnfsHandler pnfs, Executor executor) {
return CacheBuilder.newBuilder().maximumSize(1000).expireAfterWrite(10, MINUTES).refreshAfterWrite(5, MINUTES).recordStats().build(new CacheLoader<FsPath, java.util.Optional<String>>() {
private java.util.Optional<String> writeToken(FileAttributes attr) {
StorageInfo info = attr.getStorageInfo();
return java.util.Optional.ofNullable(info.getMap().get("writeToken"));
}
@Override
public java.util.Optional<String> load(FsPath path) throws CacheException, NoRouteToCellException, InterruptedException {
return writeToken(pnfs.getFileAttributes(path, EnumSet.of(FileAttribute.STORAGEINFO)));
}
@Override
public ListenableFuture<java.util.Optional<String>> reload(FsPath path, java.util.Optional<String> old) {
PnfsGetFileAttributes message = new PnfsGetFileAttributes(path.toString(), EnumSet.of(FileAttribute.STORAGEINFO));
SettableFuture<java.util.Optional<String>> future = SettableFuture.create();
CellStub.addCallback(pnfs.requestAsync(message), new AbstractMessageCallback<PnfsGetFileAttributes>() {
@Override
public void success(PnfsGetFileAttributes message) {
future.set(writeToken(message.getFileAttributes()));
}
@Override
public void failure(int rc, Object error) {
CacheException exception = CacheExceptionFactory.exceptionOf(rc, Objects.toString(error, null));
future.setException(exception);
}
}, executor);
return future;
}
});
}
use of org.dcache.cells.AbstractMessageCallback in project dcache by dCache.
the class ReservationCaches method buildOwnerDescriptionLookupCache.
/**
* Builds a loading cache for looking up space tokens by owner and description.
*/
public static LoadingCache<GetSpaceTokensKey, long[]> buildOwnerDescriptionLookupCache(CellStub spaceManager, Executor executor) {
return CacheBuilder.newBuilder().maximumSize(1000).expireAfterWrite(30, SECONDS).refreshAfterWrite(10, SECONDS).recordStats().build(new CacheLoader<GetSpaceTokensKey, long[]>() {
@Override
public long[] load(GetSpaceTokensKey key) throws Exception {
try {
return spaceManager.sendAndWait(createRequest(key)).getSpaceTokens();
} catch (TimeoutCacheException e) {
throw new SRMInternalErrorException("Space manager timeout", e);
} catch (InterruptedException e) {
throw new SRMInternalErrorException("Operation interrupted", e);
} catch (CacheException e) {
LOGGER.warn("GetSpaceTokens failed with rc={} error={}", e.getRc(), e.getMessage());
throw new SRMException("GetSpaceTokens failed with rc=" + e.getRc() + " error=" + e.getMessage(), e);
}
}
private GetSpaceTokens createRequest(GetSpaceTokensKey key) {
GetSpaceTokens message = new GetSpaceTokens(key.description);
message.setSubject(new Subject(true, key.principals, Collections.emptySet(), Collections.emptySet()));
return message;
}
@Override
public ListenableFuture<long[]> reload(GetSpaceTokensKey key, long[] oldValue) throws Exception {
final SettableFuture<long[]> future = SettableFuture.create();
CellStub.addCallback(spaceManager.send(createRequest(key)), new AbstractMessageCallback<GetSpaceTokens>() {
@Override
public void success(GetSpaceTokens message) {
future.set(message.getSpaceTokens());
}
@Override
public void failure(int rc, Object error) {
CacheException exception = CacheExceptionFactory.exceptionOf(rc, Objects.toString(error, null));
future.setException(exception);
}
}, executor);
return future;
}
});
}
use of org.dcache.cells.AbstractMessageCallback in project dcache by dCache.
the class ReservationCaches method buildSpaceLookupCache.
/**
* Build a loading cache for looking up space reservations by space token.
*/
public static LoadingCache<String, Optional<Space>> buildSpaceLookupCache(CellStub spaceManager, Executor executor) {
return CacheBuilder.newBuilder().maximumSize(1000).expireAfterWrite(10, MINUTES).refreshAfterWrite(30, SECONDS).recordStats().build(new CacheLoader<String, Optional<Space>>() {
@Override
public Optional<Space> load(String token) throws CacheException, NoRouteToCellException, InterruptedException {
Space space = spaceManager.sendAndWait(new GetSpaceMetaData(token)).getSpaces()[0];
return Optional.ofNullable(space);
}
@Override
public ListenableFuture<Optional<Space>> reload(String token, Optional<Space> oldValue) {
final SettableFuture<Optional<Space>> future = SettableFuture.create();
CellStub.addCallback(spaceManager.send(new GetSpaceMetaData(token)), new AbstractMessageCallback<GetSpaceMetaData>() {
@Override
public void success(GetSpaceMetaData message) {
future.set(Optional.ofNullable(message.getSpaces()[0]));
}
@Override
public void failure(int rc, Object error) {
CacheException exception = CacheExceptionFactory.exceptionOf(rc, Objects.toString(error, null));
future.setException(exception);
}
}, executor);
return future;
}
});
}
Aggregations