Search in sources :

Example 6 with InputStreamWithMeta

use of org.apache.storm.blobstore.InputStreamWithMeta in project storm by apache.

the class LocallyCachedBlob method fetch.

/**
 * Helper function to download blob from blob store.
 * @param store Blob store to fetch blobs from
 * @param key Key to retrieve blobs
 * @param pathSupplier A function that supplies the download destination of a blob. It guarantees the validity
 *                     of path or throws {@link IOException}
 * @param outStreamSupplier A function that supplies the {@link OutputStream} object
 * @return The metadata of the download session, including blob's version and download destination
 * @throws KeyNotFoundException Thrown if key to retrieve blob is invalid
 * @throws AuthorizationException Thrown if the retrieval is not under security authorization
 * @throws IOException Thrown if any IO error occurs
 */
protected DownloadMeta fetch(ClientBlobStore store, String key, IOFunction<Long, Path> pathSupplier, IOFunction<File, OutputStream> outStreamSupplier) throws KeyNotFoundException, AuthorizationException, IOException {
    try (InputStreamWithMeta in = store.getBlob(key)) {
        long newVersion = in.getVersion();
        long currentVersion = getLocalVersion();
        if (newVersion == currentVersion) {
            LOG.warn("The version did not change, but going to download again {} {}", currentVersion, key);
        }
        // Make sure the parent directory is there and ready to go
        Path downloadPath = pathSupplier.apply(newVersion);
        LOG.debug("Downloading {} to {}", key, downloadPath);
        long duration;
        long totalRead = 0;
        try (OutputStream out = outStreamSupplier.apply(downloadPath.toFile())) {
            long startTime = Time.nanoTime();
            byte[] buffer = new byte[4096];
            int read;
            while ((read = in.read(buffer)) >= 0) {
                out.write(buffer, 0, read);
                totalRead += read;
            }
            duration = Time.nanoTime() - startTime;
        }
        long expectedSize = in.getFileLength();
        if (totalRead != expectedSize) {
            throw new IOException("We expected to download " + expectedSize + " bytes but found we got " + totalRead);
        } else {
            double downloadRate = ((double) totalRead * 1e3) / duration;
            fetchingRate.update(Math.round(downloadRate));
        }
        return new DownloadMeta(downloadPath, newVersion);
    }
}
Also used : Path(java.nio.file.Path) InputStreamWithMeta(org.apache.storm.blobstore.InputStreamWithMeta) OutputStream(java.io.OutputStream) IOException(java.io.IOException)

Aggregations

IOException (java.io.IOException)6 InputStreamWithMeta (org.apache.storm.blobstore.InputStreamWithMeta)6 AuthorizationException (org.apache.storm.generated.AuthorizationException)4 KeyNotFoundException (org.apache.storm.generated.KeyNotFoundException)4 FileOutputStream (java.io.FileOutputStream)2 Subject (javax.security.auth.Subject)2 StormTopology (org.apache.storm.generated.StormTopology)2 Sets (com.google.common.collect.Sets)1 BufferedWriter (java.io.BufferedWriter)1 File (java.io.File)1 FileWriter (java.io.FileWriter)1 InterruptedIOException (java.io.InterruptedIOException)1 OutputStream (java.io.OutputStream)1 PrintWriter (java.io.PrintWriter)1 BindException (java.net.BindException)1 Path (java.nio.file.Path)1 TreeSet (java.util.TreeSet)1 LeaderLatchListener (org.apache.curator.framework.recipes.leader.LeaderLatchListener)1 ClientBlobStore (org.apache.storm.blobstore.ClientBlobStore)1 AlreadyAliveException (org.apache.storm.generated.AlreadyAliveException)1