Search in sources :

Example 1 with BufferFileInputStream

use of backtype.storm.utils.BufferFileInputStream in project jstorm by alibaba.

the class StormSubmitter method submitJar.

public static String submitJar(Map conf, String localJar, String uploadLocation, NimbusClient client) {
    if (localJar == null) {
        throw new RuntimeException("Must submit topologies using the 'jstorm' client script so that " + "StormSubmitter knows which jar to upload.");
    }
    try {
        LOG.info("Uploading topology jar " + localJar + " to assigned location: " + uploadLocation);
        int bufferSize = 512 * 1024;
        Object maxBufSizeObject = conf.get(Config.NIMBUS_THRIFT_MAX_BUFFER_SIZE);
        if (maxBufSizeObject != null) {
            bufferSize = Utils.getInt(maxBufSizeObject) / 2;
        }
        BufferFileInputStream is = new BufferFileInputStream(localJar, bufferSize);
        while (true) {
            byte[] toSubmit = is.read();
            if (toSubmit.length == 0)
                break;
            client.getClient().uploadChunk(uploadLocation, ByteBuffer.wrap(toSubmit));
        }
        client.getClient().finishFileUpload(uploadLocation);
        LOG.info("Successfully uploaded topology jar to assigned location: " + uploadLocation);
        return uploadLocation;
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}
Also used : BufferFileInputStream(backtype.storm.utils.BufferFileInputStream) TException(org.apache.thrift.TException)

Example 2 with BufferFileInputStream

use of backtype.storm.utils.BufferFileInputStream in project jstorm by alibaba.

the class ServiceHandler method beginFileDownload.

@Override
public String beginFileDownload(String file) throws TException {
    BufferFileInputStream is;
    String id;
    try {
        int bufferSize = JStormUtils.parseInt(conf.get(Config.NIMBUS_THRIFT_MAX_BUFFER_SIZE), 1024 * 1024) / 2;
        is = new BufferFileInputStream(file, bufferSize);
        id = UUID.randomUUID().toString();
        data.getDownloaders().put(id, is);
    } catch (FileNotFoundException e) {
        LOG.error(e + "file:" + file + " not found");
        throw new TException(e);
    }
    return id;
}
Also used : TException(org.apache.thrift.TException) FileNotFoundException(java.io.FileNotFoundException) BufferFileInputStream(backtype.storm.utils.BufferFileInputStream)

Example 3 with BufferFileInputStream

use of backtype.storm.utils.BufferFileInputStream in project jstorm by alibaba.

the class NimbusData method createFileHandler.

public void createFileHandler() {
    ExpiredCallback<Object, Object> expiredCallback = new ExpiredCallback<Object, Object>() {

        @Override
        public void expire(Object key, Object val) {
            try {
                LOG.info("Close file " + String.valueOf(key));
                if (val != null) {
                    if (val instanceof Channel) {
                        Channel channel = (Channel) val;
                        channel.close();
                    } else if (val instanceof BufferFileInputStream) {
                        BufferFileInputStream is = (BufferFileInputStream) val;
                        is.close();
                    }
                }
            } catch (IOException e) {
                LOG.error(e.getMessage(), e);
            }
        }
    };
    int file_copy_expiration_secs = JStormUtils.parseInt(conf.get(Config.NIMBUS_FILE_COPY_EXPIRATION_SECS), 30);
    uploaders = new TimeCacheMap<>(file_copy_expiration_secs, expiredCallback);
    downloaders = new TimeCacheMap<>(file_copy_expiration_secs, expiredCallback);
}
Also used : ExpiredCallback(com.alibaba.jstorm.utils.ExpiredCallback) Channel(java.nio.channels.Channel) IOException(java.io.IOException) BufferFileInputStream(backtype.storm.utils.BufferFileInputStream)

Example 4 with BufferFileInputStream

use of backtype.storm.utils.BufferFileInputStream in project storm by nathanmarz.

the class StormSubmitter method submitJar.

public static String submitJar(Map conf, String localJar) {
    if (localJar == null) {
        throw new RuntimeException("Must submit topologies using the 'storm' client script so that StormSubmitter knows which jar to upload.");
    }
    NimbusClient client = NimbusClient.getConfiguredClient(conf);
    try {
        String uploadLocation = client.getClient().beginFileUpload();
        LOG.info("Uploading topology jar " + localJar + " to assigned location: " + uploadLocation);
        BufferFileInputStream is = new BufferFileInputStream(localJar);
        while (true) {
            byte[] toSubmit = is.read();
            if (toSubmit.length == 0)
                break;
            client.getClient().uploadChunk(uploadLocation, ByteBuffer.wrap(toSubmit));
        }
        client.getClient().finishFileUpload(uploadLocation);
        LOG.info("Successfully uploaded topology jar to assigned location: " + uploadLocation);
        return uploadLocation;
    } catch (Exception e) {
        throw new RuntimeException(e);
    } finally {
        client.close();
    }
}
Also used : NimbusClient(backtype.storm.utils.NimbusClient) BufferFileInputStream(backtype.storm.utils.BufferFileInputStream) TException(org.apache.thrift7.TException)

Example 5 with BufferFileInputStream

use of backtype.storm.utils.BufferFileInputStream in project jstorm by alibaba.

the class ServiceHandler method downloadChunk.

@Override
public ByteBuffer downloadChunk(String id) throws TException {
    TimeCacheMap<Object, Object> downloaders = data.getDownloaders();
    Object obj = downloaders.get(id);
    if (obj == null) {
        throw new TException("Could not find input stream for that id");
    }
    try {
        if (obj instanceof BufferFileInputStream) {
            BufferFileInputStream is = (BufferFileInputStream) obj;
            byte[] ret = is.read();
            if (ret != null) {
                downloaders.put(id, is);
                return ByteBuffer.wrap(ret);
            }
        } else {
            throw new TException("Object isn't BufferFileInputStream for " + id);
        }
    } catch (IOException e) {
        LOG.error("BufferFileInputStream read failed when downloadChunk ", e);
        throw new TException(e);
    }
    byte[] empty = {};
    return ByteBuffer.wrap(empty);
}
Also used : TException(org.apache.thrift.TException) IOException(java.io.IOException) BufferFileInputStream(backtype.storm.utils.BufferFileInputStream)

Aggregations

BufferFileInputStream (backtype.storm.utils.BufferFileInputStream)5 TException (org.apache.thrift.TException)3 IOException (java.io.IOException)2 NimbusClient (backtype.storm.utils.NimbusClient)1 ExpiredCallback (com.alibaba.jstorm.utils.ExpiredCallback)1 FileNotFoundException (java.io.FileNotFoundException)1 Channel (java.nio.channels.Channel)1 TException (org.apache.thrift7.TException)1