Search in sources :

Example 6 with KeyNotFoundException

use of org.apache.storm.generated.KeyNotFoundException in project storm by apache.

the class Nimbus method beginBlobDownload.

@SuppressWarnings("deprecation")
@Override
public BeginDownloadResult beginBlobDownload(String key) throws AuthorizationException, KeyNotFoundException, TException {
    try {
        InputStreamWithMeta is = blobStore.getBlob(key, getSubject());
        String sessionId = Utils.uuid();
        BeginDownloadResult ret = new BeginDownloadResult(is.getVersion(), sessionId);
        ret.set_data_size(is.getFileLength());
        blobDownloaders.put(sessionId, new BufferInputStream(is, (int) conf.getOrDefault(Config.STORM_BLOBSTORE_INPUTSTREAM_BUFFER_SIZE_BYTES, 65536)));
        LOG.info("Created download session for {}", key);
        return ret;
    } catch (Exception e) {
        LOG.warn("begin blob download exception.", e);
        if (e instanceof TException) {
            throw (TException) e;
        }
        throw new RuntimeException(e);
    }
}
Also used : BufferInputStream(org.apache.storm.utils.BufferInputStream) TException(org.apache.thrift.TException) InputStreamWithMeta(org.apache.storm.blobstore.InputStreamWithMeta) BeginDownloadResult(org.apache.storm.generated.BeginDownloadResult) AuthorizationException(org.apache.storm.generated.AuthorizationException) NotAliveException(org.apache.storm.generated.NotAliveException) InterruptedIOException(java.io.InterruptedIOException) TException(org.apache.thrift.TException) IOException(java.io.IOException) AlreadyAliveException(org.apache.storm.generated.AlreadyAliveException) KeyAlreadyExistsException(org.apache.storm.generated.KeyAlreadyExistsException) KeyNotFoundException(org.apache.storm.generated.KeyNotFoundException) InvalidTopologyException(org.apache.storm.generated.InvalidTopologyException) BindException(java.net.BindException)

Example 7 with KeyNotFoundException

use of org.apache.storm.generated.KeyNotFoundException in project storm by apache.

the class Nimbus method setupBlobstore.

/**
     * Sets up blobstore state for all current keys.
     * @throws KeyNotFoundException 
     * @throws AuthorizationException 
     */
private void setupBlobstore() throws AuthorizationException, KeyNotFoundException {
    IStormClusterState state = stormClusterState;
    BlobStore store = blobStore;
    Set<String> localKeys = new HashSet<>();
    for (Iterator<String> it = store.listKeys(); it.hasNext(); ) {
        localKeys.add(it.next());
    }
    Set<String> activeKeys = new HashSet<>(state.activeKeys());
    Set<String> activeLocalKeys = new HashSet<>(localKeys);
    activeLocalKeys.retainAll(activeKeys);
    Set<String> keysToDelete = new HashSet<>(localKeys);
    keysToDelete.removeAll(activeKeys);
    NimbusInfo nimbusInfo = nimbusHostPortInfo;
    LOG.debug("Deleting keys not on the zookeeper {}", keysToDelete);
    for (String toDelete : keysToDelete) {
        store.deleteBlob(toDelete, NIMBUS_SUBJECT);
    }
    LOG.debug("Creating list of key entries for blobstore inside zookeeper {} local {}", activeKeys, activeLocalKeys);
    for (String key : activeLocalKeys) {
        try {
            state.setupBlobstore(key, nimbusInfo, getVersionForKey(key, nimbusInfo, conf));
        } catch (KeyNotFoundException e) {
            // invalid key, remove it from blobstore
            store.deleteBlob(key, NIMBUS_SUBJECT);
        }
    }
}
Also used : IStormClusterState(org.apache.storm.cluster.IStormClusterState) BlobStore(org.apache.storm.blobstore.BlobStore) LocalFsBlobStore(org.apache.storm.blobstore.LocalFsBlobStore) KeyNotFoundException(org.apache.storm.generated.KeyNotFoundException) HashSet(java.util.HashSet) NimbusInfo(org.apache.storm.nimbus.NimbusInfo)

Example 8 with KeyNotFoundException

use of org.apache.storm.generated.KeyNotFoundException in project storm by apache.

the class UpdateBlobs method updateBlobsForTopology.

/**
     * Update each blob listed in the topology configuration if the latest version of the blob has not been downloaded.
     * 
     * @param conf
     * @param stormId
     * @param localizer
     * @throws IOException
     */
private void updateBlobsForTopology(Map conf, String stormId, Localizer localizer) throws IOException {
    Map stormConf = ConfigUtils.readSupervisorStormConf(conf, stormId);
    Map<String, Map<String, Object>> blobstoreMap = (Map<String, Map<String, Object>>) stormConf.get(Config.TOPOLOGY_BLOBSTORE_MAP);
    String user = (String) stormConf.get(Config.TOPOLOGY_SUBMITTER_USER);
    List<LocalResource> localresources = SupervisorUtils.blobstoreMapToLocalresources(blobstoreMap);
    try {
        localizer.updateBlobs(localresources, user);
    } catch (AuthorizationException authExp) {
        LOG.error("AuthorizationException error", authExp);
    } catch (KeyNotFoundException knf) {
        LOG.error("KeyNotFoundException error", knf);
    }
}
Also used : AuthorizationException(org.apache.storm.generated.AuthorizationException) Map(java.util.Map) KeyNotFoundException(org.apache.storm.generated.KeyNotFoundException) LocalResource(org.apache.storm.localizer.LocalResource)

Example 9 with KeyNotFoundException

use of org.apache.storm.generated.KeyNotFoundException in project storm by apache.

the class Localizer method getBlobs.

/**
   * This function either returns the blobs in the existing cache or if they don't exist in the
   * cache, it downloads them in parallel (up to SUPERVISOR_BLOBSTORE_DOWNLOAD_THREAD_COUNT)
   * and will block until all of them have been downloaded
   */
public synchronized List<LocalizedResource> getBlobs(List<LocalResource> localResources, String user, String topo, File userFileDir) throws AuthorizationException, KeyNotFoundException, IOException {
    LocalizedResourceSet newSet = new LocalizedResourceSet(user);
    LocalizedResourceSet lrsrcSet = _userRsrc.putIfAbsent(user, newSet);
    if (lrsrcSet == null) {
        lrsrcSet = newSet;
    }
    ArrayList<LocalizedResource> results = new ArrayList<>();
    ArrayList<Callable<LocalizedResource>> downloads = new ArrayList<>();
    ClientBlobStore blobstore = null;
    try {
        blobstore = getClientBlobStore();
        for (LocalResource localResource : localResources) {
            String key = localResource.getBlobName();
            boolean uncompress = localResource.shouldUncompress();
            LocalizedResource lrsrc = lrsrcSet.get(key, localResource.shouldUncompress());
            boolean isUpdate = false;
            if ((lrsrc != null) && (lrsrc.isUncompressed() == localResource.shouldUncompress()) && (isLocalizedResourceDownloaded(lrsrc))) {
                if (isLocalizedResourceUpToDate(lrsrc, blobstore)) {
                    LOG.debug("blob already exists: {}", key);
                    lrsrc.addReference(topo);
                    results.add(lrsrc);
                    continue;
                }
                LOG.debug("blob exists but isn't up to date: {}", key);
                isUpdate = true;
            }
            // go off to blobstore and get it
            // assume dir passed in exists and has correct permission
            LOG.debug("fetching blob: {}", key);
            File downloadDir = getCacheDirForFiles(userFileDir);
            File localFile = new File(downloadDir, key);
            if (uncompress) {
                // for compressed file, download to archives dir
                downloadDir = getCacheDirForArchives(userFileDir);
                localFile = new File(downloadDir, key);
            }
            downloadDir.mkdir();
            downloads.add(new DownloadBlob(this, _conf, key, localFile, user, uncompress, isUpdate));
        }
    } finally {
        if (blobstore != null) {
            blobstore.shutdown();
        }
    }
    try {
        List<Future<LocalizedResource>> futures = _execService.invokeAll(downloads);
        for (Future<LocalizedResource> futureRsrc : futures) {
            LocalizedResource lrsrc = futureRsrc.get();
            lrsrc.addReference(topo);
            lrsrcSet.add(lrsrc.getKey(), lrsrc, lrsrc.isUncompressed());
            results.add(lrsrc);
        }
    } catch (ExecutionException e) {
        if (e.getCause() instanceof AuthorizationException)
            throw (AuthorizationException) e.getCause();
        else if (e.getCause() instanceof KeyNotFoundException) {
            throw (KeyNotFoundException) e.getCause();
        } else {
            throw new IOException("Error getting blobs", e);
        }
    } catch (RejectedExecutionException re) {
        throw new IOException("RejectedExecutionException: ", re);
    } catch (InterruptedException ie) {
        throw new IOException("Interrupted Exception", ie);
    }
    return results;
}
Also used : ClientBlobStore(org.apache.storm.blobstore.ClientBlobStore) AuthorizationException(org.apache.storm.generated.AuthorizationException) ArrayList(java.util.ArrayList) IOException(java.io.IOException) Callable(java.util.concurrent.Callable) RejectedExecutionException(java.util.concurrent.RejectedExecutionException) Future(java.util.concurrent.Future) RejectedExecutionException(java.util.concurrent.RejectedExecutionException) ExecutionException(java.util.concurrent.ExecutionException) File(java.io.File) KeyNotFoundException(org.apache.storm.generated.KeyNotFoundException)

Example 10 with KeyNotFoundException

use of org.apache.storm.generated.KeyNotFoundException in project storm by apache.

the class Localizer method downloadBlob.

private LocalizedResource downloadBlob(Map conf, String key, File localFile, String user, boolean uncompress, boolean isUpdate) throws AuthorizationException, KeyNotFoundException, IOException {
    ClientBlobStore blobstore = null;
    try {
        blobstore = getClientBlobStore();
        long nimbusBlobVersion = Utils.nimbusVersionOfBlob(key, blobstore);
        long oldVersion = Utils.localVersionOfBlob(localFile.toString());
        FileOutputStream out = null;
        PrintWriter writer = null;
        int numTries = 0;
        String localizedPath = localFile.toString();
        String localFileWithVersion = Utils.constructBlobWithVersionFileName(localFile.toString(), nimbusBlobVersion);
        String localVersionFile = Utils.constructVersionFileName(localFile.toString());
        String downloadFile = localFileWithVersion;
        if (uncompress) {
            // we need to download to temp file and then unpack into the one requested
            downloadFile = new File(localFile.getParent(), TO_UNCOMPRESS + localFile.getName()).toString();
        }
        while (numTries < _blobDownloadRetries) {
            out = new FileOutputStream(downloadFile);
            numTries++;
            try {
                if (!Utils.canUserReadBlob(blobstore.getBlobMeta(key), user)) {
                    throw new AuthorizationException(user + " does not have READ access to " + key);
                }
                InputStreamWithMeta in = blobstore.getBlob(key);
                byte[] buffer = new byte[1024];
                int len;
                while ((len = in.read(buffer)) >= 0) {
                    out.write(buffer, 0, len);
                }
                out.close();
                in.close();
                if (uncompress) {
                    Utils.unpack(new File(downloadFile), new File(localFileWithVersion));
                    LOG.debug("uncompressed " + downloadFile + " to: " + localFileWithVersion);
                }
                // Next write the version.
                LOG.info("Blob: " + key + " updated with new Nimbus-provided version: " + nimbusBlobVersion + " local version was: " + oldVersion);
                // The false parameter ensures overwriting the version file, not appending
                writer = new PrintWriter(new BufferedWriter(new FileWriter(localVersionFile, false)));
                writer.println(nimbusBlobVersion);
                writer.close();
                try {
                    setBlobPermissions(conf, user, localFileWithVersion);
                    setBlobPermissions(conf, user, localVersionFile);
                    // Update the key.current symlink. First create tmp symlink and do
                    // move of tmp to current so that the operation is atomic.
                    String tmp_uuid_local = java.util.UUID.randomUUID().toString();
                    LOG.debug("Creating a symlink @" + localFile + "." + tmp_uuid_local + " , " + "linking to: " + localFile + "." + nimbusBlobVersion);
                    File uuid_symlink = new File(localFile + "." + tmp_uuid_local);
                    Files.createSymbolicLink(uuid_symlink.toPath(), Paths.get(Utils.constructBlobWithVersionFileName(localFile.toString(), nimbusBlobVersion)));
                    File current_symlink = new File(Utils.constructBlobCurrentSymlinkName(localFile.toString()));
                    Files.move(uuid_symlink.toPath(), current_symlink.toPath(), ATOMIC_MOVE);
                } catch (IOException e) {
                    // restore the old version to the file
                    try {
                        PrintWriter restoreWriter = new PrintWriter(new BufferedWriter(new FileWriter(localVersionFile, false)));
                        restoreWriter.println(oldVersion);
                        restoreWriter.close();
                    } catch (IOException ignore) {
                    }
                    throw e;
                }
                String oldBlobFile = localFile + "." + oldVersion;
                try {
                    // anyone trying to read it.
                    if ((oldVersion != -1) && (oldVersion != nimbusBlobVersion)) {
                        LOG.info("Removing an old blob file:" + oldBlobFile);
                        Files.delete(Paths.get(oldBlobFile));
                    }
                } catch (IOException e) {
                    // At this point we have downloaded everything and moved symlinks.  If the remove of
                    // old fails just log an error
                    LOG.error("Exception removing old blob version: " + oldBlobFile);
                }
                break;
            } catch (AuthorizationException ae) {
                // we consider this non-retriable exceptions
                if (out != null) {
                    out.close();
                }
                new File(downloadFile).delete();
                throw ae;
            } catch (IOException | KeyNotFoundException e) {
                if (out != null) {
                    out.close();
                }
                if (writer != null) {
                    writer.close();
                }
                new File(downloadFile).delete();
                if (uncompress) {
                    try {
                        FileUtils.deleteDirectory(new File(localFileWithVersion));
                    } catch (IOException ignore) {
                    }
                }
                if (!isUpdate) {
                    // don't want to remove existing version file if its an update
                    new File(localVersionFile).delete();
                }
                if (numTries < _blobDownloadRetries) {
                    LOG.error("Failed to download blob, retrying", e);
                } else {
                    throw e;
                }
            }
        }
        return new LocalizedResource(key, localizedPath, uncompress);
    } finally {
        if (blobstore != null) {
            blobstore.shutdown();
        }
    }
}
Also used : ClientBlobStore(org.apache.storm.blobstore.ClientBlobStore) AuthorizationException(org.apache.storm.generated.AuthorizationException) FileWriter(java.io.FileWriter) IOException(java.io.IOException) BufferedWriter(java.io.BufferedWriter) InputStreamWithMeta(org.apache.storm.blobstore.InputStreamWithMeta) FileOutputStream(java.io.FileOutputStream) File(java.io.File) KeyNotFoundException(org.apache.storm.generated.KeyNotFoundException) PrintWriter(java.io.PrintWriter)

Aggregations

KeyNotFoundException (org.apache.storm.generated.KeyNotFoundException)21 IOException (java.io.IOException)11 AuthorizationException (org.apache.storm.generated.AuthorizationException)10 File (java.io.File)8 KeyAlreadyExistsException (org.apache.storm.generated.KeyAlreadyExistsException)5 NimbusInfo (org.apache.storm.nimbus.NimbusInfo)5 KeeperException (org.apache.zookeeper.KeeperException)4 ArrayList (java.util.ArrayList)3 HashSet (java.util.HashSet)3 ClientBlobStore (org.apache.storm.blobstore.ClientBlobStore)3 InputStreamWithMeta (org.apache.storm.blobstore.InputStreamWithMeta)3 ReadableBlobMeta (org.apache.storm.generated.ReadableBlobMeta)3 TTransportException (org.apache.thrift.transport.TTransportException)3 NoNodeException (org.apache.zookeeper.KeeperException.NoNodeException)3 Test (org.junit.Test)3 Matchers.anyString (org.mockito.Matchers.anyString)3 ByteArrayOutputStream (java.io.ByteArrayOutputStream)2 FileNotFoundException (java.io.FileNotFoundException)2 InputStream (java.io.InputStream)2 LinkedHashMap (java.util.LinkedHashMap)2