Search in sources :

Example 1 with AbstractMessageCallback

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;
        }
    });
}
Also used : SettableFuture(com.google.common.util.concurrent.SettableFuture) Optional(java.util.Optional) CacheException(diskCacheV111.util.CacheException) TimeoutCacheException(diskCacheV111.util.TimeoutCacheException) NoRouteToCellException(dmg.cells.nucleus.NoRouteToCellException) StorageInfo(diskCacheV111.vehicles.StorageInfo) PnfsGetFileAttributes(org.dcache.vehicles.PnfsGetFileAttributes) ListenableFuture(com.google.common.util.concurrent.ListenableFuture) FileAttributes(org.dcache.vehicles.FileAttributes) PnfsGetFileAttributes(org.dcache.vehicles.PnfsGetFileAttributes) AbstractMessageCallback(org.dcache.cells.AbstractMessageCallback) FsPath(diskCacheV111.util.FsPath)

Example 2 with AbstractMessageCallback

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;
        }
    });
}
Also used : SettableFuture(com.google.common.util.concurrent.SettableFuture) CacheException(diskCacheV111.util.CacheException) TimeoutCacheException(diskCacheV111.util.TimeoutCacheException) SRMInternalErrorException(org.dcache.srm.SRMInternalErrorException) CacheException(diskCacheV111.util.CacheException) SRMException(org.dcache.srm.SRMException) TimeoutCacheException(diskCacheV111.util.TimeoutCacheException) NoRouteToCellException(dmg.cells.nucleus.NoRouteToCellException) Subject(javax.security.auth.Subject) SRMInternalErrorException(org.dcache.srm.SRMInternalErrorException) GetSpaceTokens(diskCacheV111.services.space.message.GetSpaceTokens) SRMException(org.dcache.srm.SRMException) ListenableFuture(com.google.common.util.concurrent.ListenableFuture) AbstractMessageCallback(org.dcache.cells.AbstractMessageCallback) TimeoutCacheException(diskCacheV111.util.TimeoutCacheException)

Example 3 with AbstractMessageCallback

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;
        }
    });
}
Also used : Space(diskCacheV111.services.space.Space) GetSpaceMetaData(diskCacheV111.services.space.message.GetSpaceMetaData) SettableFuture(com.google.common.util.concurrent.SettableFuture) Optional(java.util.Optional) CacheException(diskCacheV111.util.CacheException) TimeoutCacheException(diskCacheV111.util.TimeoutCacheException) NoRouteToCellException(dmg.cells.nucleus.NoRouteToCellException) ListenableFuture(com.google.common.util.concurrent.ListenableFuture) AbstractMessageCallback(org.dcache.cells.AbstractMessageCallback)

Aggregations

ListenableFuture (com.google.common.util.concurrent.ListenableFuture)3 SettableFuture (com.google.common.util.concurrent.SettableFuture)3 CacheException (diskCacheV111.util.CacheException)3 TimeoutCacheException (diskCacheV111.util.TimeoutCacheException)3 NoRouteToCellException (dmg.cells.nucleus.NoRouteToCellException)3 AbstractMessageCallback (org.dcache.cells.AbstractMessageCallback)3 Optional (java.util.Optional)2 Space (diskCacheV111.services.space.Space)1 GetSpaceMetaData (diskCacheV111.services.space.message.GetSpaceMetaData)1 GetSpaceTokens (diskCacheV111.services.space.message.GetSpaceTokens)1 FsPath (diskCacheV111.util.FsPath)1 StorageInfo (diskCacheV111.vehicles.StorageInfo)1 Subject (javax.security.auth.Subject)1 SRMException (org.dcache.srm.SRMException)1 SRMInternalErrorException (org.dcache.srm.SRMInternalErrorException)1 FileAttributes (org.dcache.vehicles.FileAttributes)1 PnfsGetFileAttributes (org.dcache.vehicles.PnfsGetFileAttributes)1