/**
  * Called by subclass to remove the request from the container and invoke the callbacks of the
  * request.
  *
  * @param key the key identifying the request to remove
  * @param cause cause of why the request failed, or null if the request was successful
  */
 protected void removeAndCallback(K key, Throwable cause) {
   for (CompletionHandler<Void, K> callback : remove(key)) {
     if (cause != null) {
       callback.failed(cause, key);
     } else {
       callback.completed(null, key);
     }
   }
 }
 public synchronized void transfer(CompletionHandler<Void, Void> completionHandler) {
   try {
     if (_state != QUEUED) {
       completionHandler.failed(new InterruptedException("Transfer cancelled"), null);
     }
     _state = RUNNING;
     _startTime = System.currentTimeMillis();
     _cancellable = _mover.execute(completionHandler);
   } catch (RuntimeException e) {
     completionHandler.failed(e, null);
   }
 }
 /**
  * Stages a file from nearline storage.
  *
  * <p>TODO: Should eventually accept multiple files at once, but the rest of the pool doesn't
  * support that yet.
  *
  * @param file attributes of file to stage
  * @param callback callback notified when file is staged
  */
 public void stage(
     String hsmInstance, FileAttributes file, CompletionHandler<Void, PnfsId> callback) {
   try {
     NearlineStorage nearlineStorage = hsmSet.getNearlineStorageByName(hsmInstance);
     checkArgument(nearlineStorage != null, "No such nearline storage: " + hsmInstance);
     stageRequests.addAll(nearlineStorage, Collections.singleton(file), callback);
   } catch (RuntimeException e) {
     callback.failed(e, file.getPnfsId());
   }
 }
 /**
  * Removes a set of files from nearline storage.
  *
  * @param hsmInstance instance name of nearline storage
  * @param files files to remove
  * @param callback callback notified for every file removed
  */
 public void remove(
     String hsmInstance, Iterable<URI> files, CompletionHandler<Void, URI> callback) {
   try {
     NearlineStorage nearlineStorage = hsmSet.getNearlineStorageByName(hsmInstance);
     checkArgument(nearlineStorage != null, "No such nearline storage: " + hsmInstance);
     removeRequests.addAll(nearlineStorage, files, callback);
   } catch (RuntimeException e) {
     for (URI location : files) {
       callback.failed(e, location);
     }
   }
 }
 /**
  * Flushes a set of files to nearline storage.
  *
  * @param hsmType type of nearline storage
  * @param files files to flush
  * @param callback callback notified for every file flushed
  */
 public void flush(
     String hsmType, Iterable<PnfsId> files, CompletionHandler<Void, PnfsId> callback) {
   try {
     NearlineStorage nearlineStorage = hsmSet.getNearlineStorageByType(hsmType);
     checkArgument(nearlineStorage != null, "No such nearline storage: " + hsmType);
     flushRequests.addAll(nearlineStorage, files, callback);
   } catch (RuntimeException e) {
     for (PnfsId pnfsId : files) {
       callback.failed(e, pnfsId);
     }
   }
 }
    public void addAll(
        NearlineStorage storage, Iterable<F> files, CompletionHandler<Void, K> callback) {
      List<R> newRequests = new ArrayList<>();
      for (F file : files) {
        K key = extractKey(file);
        R request =
            requests.computeIfAbsent(
                key,
                (k) -> {
                  if (!state.increment()) {
                    callback.failed(new CacheException("Nearline storage has been shut down."), k);
                    return null;
                  }
                  try {
                    R newRequest = createRequest(storage, file);
                    newRequests.add(newRequest);
                    return newRequest;
                  } catch (Exception e) {
                    state.decrement();
                    callback.failed(e, k);
                    return null;
                  } catch (Error e) {
                    state.decrement();
                    callback.failed(e, k);
                    throw e;
                  }
                });
        if (request != null) {
          request.addCallback(callback);
        }
      }
      submit(storage, newRequests);

      /* If the container shut down before the requests were added to the map,
       * the shutdown call might have missed them when cancelling requests.
       */
      if (state.isShutdown()) {
        cancelRequests();
      }
    }
Example #7
0
  /*
   * (non-Javadoc)
   *
   * @see org.apache.tomcat.util.net.NioChannel#write(java.nio.ByteBuffer,
   * long, java.util.concurrent.TimeUnit, java.lang.Object,
   * java.nio.channels.CompletionHandler)
   */
  @Override
  public <A> void write(
      final ByteBuffer src,
      long timeout,
      TimeUnit unit,
      final A attachment,
      final CompletionHandler<Integer, ? super A> handler) {

    // The handshake is completed
    checkHandshake();

    try {
      // Prepare the output buffer
      this.netOutBuffer.clear();
      // Wrap the source data into the internal buffer
      final int written = wrap(src, this.netOutBuffer);
      this.netOutBuffer.flip();

      // Write data to the channel
      this.channel.write(
          this.netOutBuffer,
          timeout,
          unit,
          attachment,
          new CompletionHandler<Integer, A>() {

            @Override
            public void completed(Integer nBytes, A attach) {
              if (nBytes < 0) {
                handler.failed(new ClosedChannelException(), attach);
              } else {
                // Call the handler completed method with the
                // consumed bytes number
                handler.completed(written, attach);
              }
            }

            @Override
            public void failed(Throwable exc, A attach) {
              handler.failed(exc, attach);
            }
          });

    } catch (Throwable exp) {
      handler.failed(exp, attachment);
    }
  }
Example #8
0
  /*
   * (non-Javadoc)
   *
   * @see org.apache.tomcat.util.net.NioChannel#write(java.nio.ByteBuffer[],
   * int, int, long, java.util.concurrent.TimeUnit, java.lang.Object,
   * java.nio.channels.CompletionHandler)
   */
  @Override
  public <A> void write(
      final ByteBuffer[] srcs,
      int offset,
      int length,
      long timeout,
      TimeUnit unit,
      A attachment,
      final CompletionHandler<Long, ? super A> handler) {

    // The handshake is completed
    checkHandshake();

    if (handler == null) {
      throw MESSAGES.nullHandler();
    }
    if ((offset < 0) || (length < 0) || (offset > srcs.length - length)) {
      throw new IndexOutOfBoundsException();
    }

    ByteBuffer[] netOutBuffers = new ByteBuffer[length];
    int size = getSSLSession().getPacketBufferSize();
    long written = 0;
    for (int i = 0; i < length; i++) {
      try {
        // Prepare the output buffer
        netOutBuffers[i] = ByteBuffer.allocateDirect(size);
        // Wrap the source data into the internal buffer
        written += wrap(srcs[offset + i], netOutBuffers[i]);
        netOutBuffers[i].flip();
      } catch (Throwable exp) {
        handler.failed(exp, attachment);
        return;
      }
    }

    final long res = written;

    this.channel.write(
        netOutBuffers,
        0,
        length,
        timeout,
        unit,
        attachment,
        new CompletionHandler<Long, A>() {

          @Override
          public void completed(Long nBytes, A attach) {
            if (nBytes < 0) {
              handler.failed(new ClosedChannelException(), attach);
            } else {
              // If everything is OK, so complete
              handler.completed(res, attach);
            }
          }

          @Override
          public void failed(Throwable exc, A attach) {
            handler.failed(exc, attach);
          }
        });
  }