Search in sources :

Example 1 with BootException

use of org.commonjava.propulsor.boot.BootException in project indy by Commonjava.

the class Main method loadFromObjectFile.

private void loadFromObjectFile(CacheHandle<Object, Object> cache, MigrationOptions options) throws BootException {
    AtomicReference<Throwable> error = new AtomicReference<>();
    try (ObjectInputStream in = new ObjectInputStream(new GZIPInputStream(new FileInputStream(options.getDataFile())))) {
        cache.executeCache((c) -> {
            try {
                long records = in.readLong();
                for (long i = 0; i < records; i++) {
                    try {
                        Object k = in.readObject();
                        Object v = in.readObject();
                        c.putAsync(k, v);
                    } catch (Exception e) {
                        logger.error("Failed to read entry at index: " + i, e);
                        error.set(e);
                    }
                }
                logger.info("Load {} complete, size: {}", options.getCacheName(), records);
            } catch (IOException e) {
                logger.error("Failed to read data file header.", e);
                error.set(e);
            }
            return true;
        });
    } catch (IOException e) {
        error.set(e);
    }
    if (error.get() != null) {
        throw new BootException("Failed to read data from file: " + options.getDataFile(), error.get());
    }
}
Also used : GZIPInputStream(java.util.zip.GZIPInputStream) AtomicReference(java.util.concurrent.atomic.AtomicReference) IOException(java.io.IOException) BootException(org.commonjava.propulsor.boot.BootException) FileInputStream(java.io.FileInputStream) IndyLifecycleException(org.commonjava.indy.action.IndyLifecycleException) BootException(org.commonjava.propulsor.boot.BootException) IOException(java.io.IOException) InvocationTargetException(java.lang.reflect.InvocationTargetException) ObjectInputStream(java.io.ObjectInputStream)

Example 2 with BootException

use of org.commonjava.propulsor.boot.BootException in project indy by Commonjava.

the class Main method dumpObjectFile.

private void dumpObjectFile(CacheHandle<Object, Object> cache, MigrationOptions options) throws BootException {
    AtomicReference<Throwable> error = new AtomicReference<>();
    try (ObjectOutputStream out = new ObjectOutputStream(new GZIPOutputStream(new FileOutputStream(options.getDataFile())))) {
        cache.executeCache((c) -> {
            try {
                out.writeLong(c.size());
            } catch (IOException e) {
                logger.error("Failed to write data file header.", e);
                error.set(e);
            }
            if (error.get() == null) {
                c.forEach((k, v) -> {
                    if (error.get() == null) {
                        try {
                            out.writeObject(k);
                            out.writeObject(v);
                        } catch (IOException e) {
                            logger.error("Failed to write entry with key: " + k, e);
                            error.set(e);
                        }
                    }
                });
            }
            return true;
        });
    } catch (IOException e) {
        error.set(e);
    }
    if (error.get() != null) {
        throw new BootException("Failed to write data to file: " + options.getDataFile(), error.get());
    }
}
Also used : GZIPOutputStream(java.util.zip.GZIPOutputStream) FileOutputStream(java.io.FileOutputStream) AtomicReference(java.util.concurrent.atomic.AtomicReference) IOException(java.io.IOException) ObjectOutputStream(java.io.ObjectOutputStream) BootException(org.commonjava.propulsor.boot.BootException)

Example 3 with BootException

use of org.commonjava.propulsor.boot.BootException in project indy by Commonjava.

the class Main method dumpZipFiles.

private void dumpZipFiles(CacheHandle<Object, Object> cache, MigrationOptions options) throws BootException {
    AtomicReference<Throwable> error = new AtomicReference<>();
    cache.executeCache((c) -> {
        if (error.get() == null) {
            int count = 0;
            int batchSize = options.getBatchSize() != null ? options.getBatchSize() : 1000;
            Iterable<List<Object>> subLists = Iterables.partition(c.keySet(), batchSize);
            logger.info("Handle the cache in batch size:{}", batchSize);
            for (List<Object> subList : subLists) {
                if (error.get() == null) {
                    Set<TrackedContent> sealed = new HashSet<>();
                    File out = new File(options.getDataFile().getParent() + "/folo-" + count + ".zip");
                    if (error.get() == null) {
                        for (Object key : subList) {
                            sealed.add((TrackedContent) c.get(key));
                        }
                        try {
                            zipTrackedContent(out, sealed);
                        } catch (IOException e) {
                            logger.error("Failed to zip entries : " + subList.toString(), e);
                            error.set(e);
                        }
                    }
                    count++;
                }
            }
        }
        return true;
    });
    if (error.get() != null) {
        throw new BootException("Failed to write data to zip.", error.get());
    }
}
Also used : AtomicReference(java.util.concurrent.atomic.AtomicReference) IOException(java.io.IOException) TrackedContent(org.commonjava.indy.folo.model.TrackedContent) List(java.util.List) BootException(org.commonjava.propulsor.boot.BootException) File(java.io.File) HashSet(java.util.HashSet)

Example 4 with BootException

use of org.commonjava.propulsor.boot.BootException in project indy by Commonjava.

the class Main method run.

private int run(final MigrationOptions options) throws BootException {
    try {
        File inXml = options.getInfinispanXml();
        if (inXml != null) {
            File outXmlDir = new File(System.getProperty("java.io.tmpdir", "/tmp"), "infinispan-config-" + System.currentTimeMillis());
            if (!outXmlDir.isDirectory() && !outXmlDir.mkdirs()) {
                throw new BootException("Failed to create temporary direcory for infinispan configuration loading");
            }
            File outXml = new File(outXmlDir, "infinispan.xml");
            FileUtils.copyFile(inXml, outXml);
            Properties props = System.getProperties();
            props.setProperty("indy.config.dir", outXmlDir.getAbsolutePath());
            System.setProperties(props);
        }
        producer = new SimpleCacheProducer();
        objectMapper = new IndyObjectMapper(true);
        objectMapper.disable(SerializationFeature.INDENT_OUTPUT);
        CacheHandle<Object, Object> cache = producer.getCache(options.getCacheName());
        if (MigrationCommand.dump == options.getMigrationCommand()) {
            if (DataType.json == options.getDataType()) {
                dumpJsonFile(cache, options);
            } else if (DataType.object == options.getDataType()) {
                dumpObjectFile(cache, options);
            } else {
                dumpZipFiles(cache, options);
            }
        } else {
            if (DataType.json == options.getDataType()) {
                loadFromJsonFile(cache, options);
            } else {
                loadFromObjectFile(cache, options);
            }
        }
    } catch (final Throwable e) {
        if (e instanceof BootException)
            throw (BootException) e;
        logger.error("Failed to initialize Booter: " + e.getMessage(), e);
        return ERR_INIT;
    } finally {
        try {
            producer.stop();
        } catch (final IndyLifecycleException e) {
            logger.error("Failed to stop cache subsystem: " + e.getMessage(), e);
        }
    }
    return 0;
}
Also used : IndyObjectMapper(org.commonjava.indy.model.core.io.IndyObjectMapper) BootException(org.commonjava.propulsor.boot.BootException) Properties(java.util.Properties) IndyLifecycleException(org.commonjava.indy.action.IndyLifecycleException) File(java.io.File)

Example 5 with BootException

use of org.commonjava.propulsor.boot.BootException in project indy by Commonjava.

the class MigrationOptions method parseArgs.

public boolean parseArgs(final String[] args) throws BootException {
    final CmdLineParser parser = new CmdLineParser(this);
    boolean canStart = true;
    try {
        parser.parseArgument(args);
    } catch (final CmdLineException e) {
        throw new BootException("Failed to parse command-line args: %s", e, e.getMessage());
    }
    if (isHelp()) {
        printUsage(parser, null);
        canStart = false;
    } else if (getMigrationCommand() == null || getDataFile() == null || getCacheName() == null) {
        System.err.println("You must provide 'action', 'cache-name', and 'data-file' arguments!");
        printUsage(parser, null);
        canStart = false;
    }
    return canStart;
}
Also used : CmdLineParser(org.kohsuke.args4j.CmdLineParser) BootException(org.commonjava.propulsor.boot.BootException) CmdLineException(org.kohsuke.args4j.CmdLineException)

Aggregations

BootException (org.commonjava.propulsor.boot.BootException)9 IOException (java.io.IOException)5 AtomicReference (java.util.concurrent.atomic.AtomicReference)5 InvocationTargetException (java.lang.reflect.InvocationTargetException)3 IndyLifecycleException (org.commonjava.indy.action.IndyLifecycleException)3 File (java.io.File)2 FileInputStream (java.io.FileInputStream)2 FileOutputStream (java.io.FileOutputStream)2 BufferedOutputStream (java.io.BufferedOutputStream)1 BufferedReader (java.io.BufferedReader)1 InputStreamReader (java.io.InputStreamReader)1 ObjectInputStream (java.io.ObjectInputStream)1 ObjectOutputStream (java.io.ObjectOutputStream)1 HashSet (java.util.HashSet)1 List (java.util.List)1 Properties (java.util.Properties)1 GZIPInputStream (java.util.zip.GZIPInputStream)1 GZIPOutputStream (java.util.zip.GZIPOutputStream)1 TrackedContent (org.commonjava.indy.folo.model.TrackedContent)1 IndyObjectMapper (org.commonjava.indy.model.core.io.IndyObjectMapper)1