@Override
 public ListenableFuture<Void> allocate() {
   LOGGER.debug("Allocating space for stage of {}.", getFileAttributes().getPnfsId());
   return register(
       executor.submit(
           () -> {
             descriptor.allocate(descriptor.getFileAttributes().getSize());
             return null;
           }));
 }
    @Override
    public void completed(Set<URI> uris) {
      try {
        descriptor.close();

        FileAttributes fileAttributesForNotification = getFileAttributesForNotification(uris);

        infoMsg.setStorageInfo(fileAttributesForNotification.getStorageInfo());

        PnfsId pnfsId = getFileAttributes().getPnfsId();
        notifyNamespace(pnfsId, fileAttributesForNotification);

        try {
          repository.setState(pnfsId, ReplicaState.CACHED);
        } catch (IllegalTransitionException ignored) {
          /* Apparently the file is no longer precious. Most
           * likely it got deleted, which is fine, since the
           * flush already succeeded.
           */
        }
        done(null);

        LOGGER.info("Flushed {} to nearline storage: {}", pnfsId, Joiner.on(' ').join(uris));
      } catch (Exception e) {
        done(e);
      }
    }
 @Override
 public void failed(Exception cause) {
   descriptor.close();
   /* ListenableFuture#get throws ExecutionException */
   if (cause instanceof ExecutionException) {
     done(cause.getCause());
   } else {
     done(cause);
   }
 }
 public FlushRequestImpl(NearlineStorage nearlineStorage, PnfsId pnfsId)
     throws CacheException, InterruptedException {
   super(nearlineStorage);
   infoMsg = new StorageInfoMessage(cellAddress.toString(), pnfsId, false);
   descriptor = repository.openEntry(pnfsId, NO_FLAGS);
   String path = descriptor.getFileAttributes().getStorageInfo().getKey("path");
   if (path != null) {
     infoMsg.setBillingPath(path);
   }
   LOGGER.debug("Flush request created for {}.", pnfsId);
 }
 @Override
 public void completed(Set<Checksum> checksums) {
   Throwable error = null;
   try {
     if (checksumModule.hasPolicy(ChecksumModule.PolicyFlag.GET_CRC_FROM_HSM)) {
       LOGGER.info(
           "Obtained checksums {} for {} from HSM", checksums, getFileAttributes().getPnfsId());
       descriptor.addChecksums(checksums);
     }
     checksumModule.enforcePostRestorePolicy(descriptor);
     descriptor.commit();
     LOGGER.info("Staged {} from nearline storage.", getFileAttributes().getPnfsId());
   } catch (InterruptedException | CacheException | RuntimeException | Error e) {
     error = e;
   } catch (NoSuchAlgorithmException e) {
     error = new CacheException(1010, "Checksum calculation failed: " + e.getMessage(), e);
   } catch (IOException e) {
     error =
         new DiskErrorCacheException(
             "Checksum calculation failed due to I/O error: " + e.getMessage(), e);
   } finally {
     done(error);
   }
 }
 private void done(Throwable cause) {
   PnfsId pnfsId = getFileAttributes().getPnfsId();
   if (cause != null) {
     if (cause instanceof InterruptedException || cause instanceof CancellationException) {
       cause = new TimeoutCacheException("Stage was cancelled.", cause);
     }
     LOGGER.warn("Stage of {} failed with {}.", pnfsId, cause);
   }
   descriptor.close();
   if (cause instanceof CacheException) {
     infoMsg.setResult(((CacheException) cause).getRc(), cause.getMessage());
   } else if (cause != null) {
     infoMsg.setResult(CacheException.DEFAULT_ERROR_CODE, cause.toString());
   }
   infoMsg.setTransferTime(System.currentTimeMillis() - activatedAt);
   billingStub.notify(infoMsg);
   stageRequests.removeAndCallback(pnfsId, cause);
 }
 private FileAttributes getFileAttributesForNotification(Set<URI> uris) throws CacheException {
   FileAttributes fileAttributes = descriptor.getFileAttributes();
   StorageInfo storageInfo = fileAttributes.getStorageInfo().clone();
   for (URI uri : uris) {
     try {
       HsmLocationExtractorFactory.validate(uri);
       storageInfo.addLocation(uri);
       storageInfo.isSetAddLocation(true);
     } catch (IllegalArgumentException e) {
       throw new CacheException(2, e.getMessage(), e);
     }
   }
   FileAttributes fileAttributesForNotification = new FileAttributes();
   fileAttributesForNotification.setAccessLatency(fileAttributes.getAccessLatency());
   fileAttributesForNotification.setRetentionPolicy(fileAttributes.getRetentionPolicy());
   fileAttributesForNotification.setStorageInfo(storageInfo);
   fileAttributesForNotification.setSize(fileAttributes.getSize());
   return fileAttributesForNotification;
 }
 @Override
 public FileAttributes getFileAttributes() {
   return descriptor.getFileAttributes();
 }
 @Override
 public URI getReplicaUri() {
   return descriptor.getReplicaFile();
 }
 @Override
 public File getFile() {
   return Paths.get(descriptor.getReplicaFile()).toFile();
 }