Search in sources :

Example 96 with OptionParser

use of joptsimple.OptionParser in project voldemort by voldemort.

the class JsonStoreBuilder method main.

/**
     * Main method to run on a input text file
     * 
     * @param args see USAGE for details
     * @throws IOException
     */
public static void main(String[] args) throws IOException {
    OptionParser parser = new OptionParser();
    parser.accepts("help", "print usage information");
    parser.accepts("cluster", "[REQUIRED] path to cluster xml config file").withRequiredArg().describedAs("cluster.xml");
    parser.accepts("stores", "[REQUIRED] path to stores xml config file").withRequiredArg().describedAs("stores.xml");
    parser.accepts("name", "[REQUIRED] store name").withRequiredArg().describedAs("store name");
    parser.accepts("buffer", "[REQUIRED] number of key/value pairs to buffer in memory").withRequiredArg().ofType(Integer.class);
    parser.accepts("input", "[REQUIRED] input file to read from").withRequiredArg().describedAs("input-file");
    parser.accepts("output", "[REQUIRED] directory to output stores to").withRequiredArg().describedAs("output directory");
    parser.accepts("threads", "number of threads").withRequiredArg().ofType(Integer.class);
    parser.accepts("chunks", "number of chunks [per node, per partition, per partition + replica]").withRequiredArg().ofType(Integer.class);
    parser.accepts("io-buffer-size", "size of i/o buffers in bytes").withRequiredArg().ofType(Integer.class);
    parser.accepts("temp-dir", "temporary directory for sorted file pieces").withRequiredArg().describedAs("temp dir");
    parser.accepts("gzip", "compress intermediate chunk files");
    parser.accepts("format", "read-only store format [" + ReadOnlyStorageFormat.READONLY_V0.getCode() + "," + ReadOnlyStorageFormat.READONLY_V1.getCode() + "," + ReadOnlyStorageFormat.READONLY_V2.getCode() + "]").withRequiredArg().ofType(String.class);
    OptionSet options = parser.parse(args);
    if (options.has("help")) {
        parser.printHelpOn(System.out);
        System.exit(0);
    }
    Set<String> missing = CmdUtils.missing(options, "cluster", "stores", "name", "buffer", "input", "output");
    if (missing.size() > 0) {
        System.err.println("Missing required arguments: " + Joiner.on(", ").join(missing));
        parser.printHelpOn(System.err);
        System.exit(1);
    }
    String clusterFile = (String) options.valueOf("cluster");
    String storeDefFile = (String) options.valueOf("stores");
    String storeName = (String) options.valueOf("name");
    int sortBufferSize = (Integer) options.valueOf("buffer");
    String inputFile = (String) options.valueOf("input");
    File outputDir = new File((String) options.valueOf("output"));
    int numThreads = CmdUtils.valueOf(options, "threads", 2);
    int chunks = CmdUtils.valueOf(options, "chunks", 2);
    int ioBufferSize = CmdUtils.valueOf(options, "io-buffer-size", 1000000);
    ReadOnlyStorageFormat storageFormat = ReadOnlyStorageFormat.fromCode(CmdUtils.valueOf(options, "format", ReadOnlyStorageFormat.READONLY_V2.getCode()));
    boolean gzipIntermediate = options.has("gzip");
    File tempDir = new File(CmdUtils.valueOf(options, "temp-dir", System.getProperty("java.io.tmpdir")));
    try {
        JsonReader reader = new JsonReader(new BufferedReader(new FileReader(inputFile), ioBufferSize));
        Cluster cluster = new ClusterMapper().readCluster(new BufferedReader(new FileReader(clusterFile)));
        StoreDefinition storeDef = null;
        List<StoreDefinition> stores = new StoreDefinitionsMapper().readStoreList(new BufferedReader(new FileReader(storeDefFile)));
        for (StoreDefinition def : stores) {
            if (def.getName().equals(storeName))
                storeDef = def;
        }
        if (storeDef == null)
            Utils.croak("No store found with name \"" + storeName + "\"");
        if (!outputDir.exists())
            Utils.croak("Directory \"" + outputDir.getAbsolutePath() + "\" does not exist.");
        RoutingStrategy routingStrategy = new RoutingStrategyFactory().updateRoutingStrategy(storeDef, cluster);
        new JsonStoreBuilder(reader, cluster, storeDef, routingStrategy, outputDir, tempDir, sortBufferSize, numThreads, chunks, ioBufferSize, gzipIntermediate).build(storageFormat);
    } catch (FileNotFoundException e) {
        Utils.croak(e.getMessage());
    }
}
Also used : RoutingStrategyFactory(voldemort.routing.RoutingStrategyFactory) StoreDefinitionsMapper(voldemort.xml.StoreDefinitionsMapper) FileNotFoundException(java.io.FileNotFoundException) Cluster(voldemort.cluster.Cluster) ClusterMapper(voldemort.xml.ClusterMapper) OptionParser(joptsimple.OptionParser) StoreDefinition(voldemort.store.StoreDefinition) BufferedReader(java.io.BufferedReader) RoutingStrategy(voldemort.routing.RoutingStrategy) JsonReader(voldemort.serialization.json.JsonReader) FileReader(java.io.FileReader) OptionSet(joptsimple.OptionSet) File(java.io.File)

Example 97 with OptionParser

use of joptsimple.OptionParser in project voldemort by voldemort.

the class AdminStoreSwapper method main.

public static void main(String[] args) throws Exception {
    OptionParser parser = new OptionParser();
    parser.accepts("help", "print usage information");
    parser.accepts("cluster", "[REQUIRED] the voldemort cluster.xml file ").withRequiredArg().describedAs("cluster.xml");
    parser.accepts("name", "[REQUIRED] the name of the store to swap").withRequiredArg().describedAs("store-name");
    parser.accepts("file", "[REQUIRED] uri of a directory containing the new store files").withRequiredArg().describedAs("uri");
    parser.accepts("timeout", "http timeout for the fetch in ms").withRequiredArg().describedAs("timeout ms").ofType(Integer.class);
    parser.accepts("rollback", "Rollback store to older version");
    parser.accepts("push-version", "[REQUIRED] Version of push to fetch / rollback-to").withRequiredArg().ofType(Long.class);
    OptionSet options = parser.parse(args);
    if (options.has("help")) {
        parser.printHelpOn(System.out);
        System.exit(0);
    }
    Set<String> missing = CmdUtils.missing(options, "cluster", "name", "file", "push-version");
    if (missing.size() > 0) {
        if (!(missing.equals(ImmutableSet.of("file")) && (options.has("rollback")))) {
            System.err.println("Missing required arguments: " + Joiner.on(", ").join(missing));
            parser.printHelpOn(System.err);
            System.exit(1);
        }
    }
    String clusterXml = (String) options.valueOf("cluster");
    String storeName = (String) options.valueOf("name");
    String filePath = (String) options.valueOf("file");
    int timeoutMs = CmdUtils.valueOf(options, "timeout", (int) (3 * Time.SECONDS_PER_HOUR * Time.MS_PER_SECOND));
    boolean rollbackStore = options.has("rollback");
    Long pushVersion = (Long) options.valueOf("push-version");
    String clusterStr = FileUtils.readFileToString(new File(clusterXml));
    Cluster cluster = new ClusterMapper().readCluster(new StringReader(clusterStr));
    ExecutorService executor = Executors.newFixedThreadPool(cluster.getNumberOfNodes());
    AdminClient adminClient = new AdminClient(cluster);
    AdminStoreSwapper swapper = new AdminStoreSwapper(executor, adminClient, timeoutMs);
    try {
        long start = System.currentTimeMillis();
        if (rollbackStore) {
            swapper.invokeRollback(storeName, pushVersion.longValue());
        } else {
            swapper.fetchAndSwapStoreData(storeName, filePath, pushVersion.longValue());
        }
        long end = System.currentTimeMillis();
        swapper.logger.info("Succeeded on all nodes in " + ((end - start) / Time.MS_PER_SECOND) + " seconds.");
    } finally {
        if (adminClient != null)
            adminClient.close();
        executor.shutdownNow();
        executor.awaitTermination(1, TimeUnit.SECONDS);
    }
    System.exit(0);
}
Also used : Cluster(voldemort.cluster.Cluster) ClusterMapper(voldemort.xml.ClusterMapper) OptionParser(joptsimple.OptionParser) StringReader(java.io.StringReader) ExecutorService(java.util.concurrent.ExecutorService) OptionSet(joptsimple.OptionSet) File(java.io.File) AdminClient(voldemort.client.protocol.admin.AdminClient)

Example 98 with OptionParser

use of joptsimple.OptionParser in project voldemort by voldemort.

the class VoldemortClientShell method main.

@SuppressWarnings("unchecked")
public static void main(String[] args) throws Exception {
    OptionParser parser = new OptionParser();
    parser.accepts("client-zone-id", "Client zone id for zone routing").withRequiredArg().describedAs("zone-id").ofType(Integer.class);
    parser.accepts("config-file", "A properties file that contains client config properties").withRequiredArg().describedAs("file");
    parser.accepts("help", "Print this help message").isForHelp();
    parser.accepts("voldemort-shell", "Suffix of shell script; used to format help output." + " Examples of script suffixes: sh, bat, app").withRequiredArg().describedAs("script_suffix");
    OptionSet options = parser.parse(args);
    List<String> nonOptions = (List<String>) options.nonOptionArguments();
    if (nonOptions.size() < 2 || nonOptions.size() > 3 || options.has("help")) {
        if (options.has("voldemort-shell")) {
            System.err.println("Usage: voldemort-shell." + options.valueOf("voldemort-shell") + " store_name bootstrap_url [command_file] [options]");
        } else {
            System.err.println("Usage: java VoldemortClientShell store_name bootstrap_url [command_file] [options]");
        }
        parser.printHelpOn(System.err);
        System.exit(-1);
    }
    String storeName = nonOptions.get(0);
    String bootstrapUrl = nonOptions.get(1);
    String configFile = (String) options.valueOf("config-file");
    ClientConfig clientConfig = null;
    BufferedReader inputReader = null;
    boolean fileInput = false;
    try {
        if (nonOptions.size() == 3) {
            inputReader = new BufferedReader(new FileReader(nonOptions.get(2)));
            fileInput = true;
        } else {
            inputReader = new BufferedReader(new InputStreamReader(System.in));
        }
    } catch (IOException e) {
        Utils.croak("Failure to open input stream: " + e.getMessage());
    }
    if (configFile != null) {
        clientConfig = new ClientConfig(new File(configFile));
    } else {
        clientConfig = new ClientConfig();
    }
    clientConfig.setBootstrapUrls(bootstrapUrl).setEnableLazy(false).setRequestFormatType(RequestFormatType.VOLDEMORT_V3);
    if (options.has("client-zone-id")) {
        clientConfig.setClientZoneId((Integer) options.valueOf("client-zone-id"));
    }
    VoldemortClientShell shell = new VoldemortClientShell(clientConfig, storeName, inputReader, System.out, System.err);
    shell.process(fileInput);
}
Also used : InputStreamReader(java.io.InputStreamReader) IOException(java.io.IOException) OptionParser(joptsimple.OptionParser) BufferedReader(java.io.BufferedReader) List(java.util.List) ArrayList(java.util.ArrayList) FileReader(java.io.FileReader) OptionSet(joptsimple.OptionSet) ClientConfig(voldemort.client.ClientConfig) File(java.io.File)

Example 99 with OptionParser

use of joptsimple.OptionParser in project aic-praise by aic-sri-international.

the class RandomConditionalPotentialExpressionGenerator method getArgs.

private static GeneratorArgs getArgs(String[] args) throws UnsupportedEncodingException, FileNotFoundException, IOException {
    GeneratorArgs result = new GeneratorArgs();
    OptionParser parser = new OptionParser();
    // Optional
    OptionSpec<Integer> randomSeed = parser.accepts("r", "random seed.").withRequiredArg().ofType(Integer.class);
    OptionSpec<File> outputFile = parser.accepts("o", "output file name (defaults to stdout).").withRequiredArg().ofType(File.class);
    // Required
    OptionSpec<Integer> numPotentials = parser.accepts("n", "# potentials to generate.").withRequiredArg().required().ofType(Integer.class);
    OptionSpec<Integer> depth = parser.accepts("d", "the depth of the generated formulas (all their sub-expressions will have depth equal to depth - 1.").withRequiredArg().required().ofType(Integer.class);
    // At least one instance of one of the following required
    OptionSpec<String> propositionalTheoryTypes = parser.accepts("p", "propositional theory type args ('v#' - v)variable #number).").withRequiredArg().ofType(String.class);
    OptionSpec<String> equalityTheoryTypes = parser.accepts("e", "equality theory type args ('v#c##u' - v)ariable #number, c)ategorical size #number, u)niquely named constants listed in category #number).").withRequiredArg().ofType(String.class);
    OptionSpec<String> inequalityTheoryTypes = parser.accepts("i", "difference arithmetic theory type args ('v#s#e#' - v)ariable #number, s)tart integer interval inclusive #number, e)nd integer interval inclusive #number).").withRequiredArg().ofType(String.class);
    //
    parser.accepts("help").forHelp();
    OptionSet options = parser.parse(args);
    if (options.has("help")) {
        parser.printHelpOn(System.out);
        System.exit(0);
    }
    // Handle optional args
    if (options.has(randomSeed)) {
        result.random = new Random(options.valueOf(randomSeed).longValue());
    } else {
        result.random = new Random();
    }
    if (options.has(outputFile)) {
        result.out = new PrintStream(options.valueOf(outputFile), FILE_CHARSET.name());
    }
    result.numberPotentials = options.valueOf(numPotentials);
    result.depth = options.valueOf(depth);
    List<String> propositionalTheoryTypeArgs = options.valuesOf(propositionalTheoryTypes);
    result.propositionalTheories = new TheoryTypePropositionalArgs[propositionalTheoryTypeArgs.size()];
    for (int i = 0; i < result.propositionalTheories.length; i++) {
        result.propositionalTheories[i] = new TheoryTypePropositionalArgs(propositionalTheoryTypeArgs.get(i));
    }
    List<String> equalityTheoryTypeArgs = options.valuesOf(equalityTheoryTypes);
    result.equalityTheories = new TheoryTypeEqualityArgs[equalityTheoryTypeArgs.size()];
    for (int i = 0; i < result.equalityTheories.length; i++) {
        result.equalityTheories[i] = new TheoryTypeEqualityArgs(equalityTheoryTypeArgs.get(i));
    }
    List<String> inequalityTheoryTypeArgs = options.valuesOf(inequalityTheoryTypes);
    result.inequalityTheories = new TheoryTypeInequalityArgs[inequalityTheoryTypeArgs.size()];
    for (int i = 0; i < result.inequalityTheories.length; i++) {
        result.inequalityTheories[i] = new TheoryTypeInequalityArgs(inequalityTheoryTypeArgs.get(i));
    }
    if (result.propositionalTheories.length == 0 && result.equalityTheories.length == 0 && result.inequalityTheories.length == 0) {
        throw new IllegalArgumentException("At least one set of propositional, equality, or difference arithmetic theory type args must be provided.");
    }
    result.potentialExpressionGenerator = new RandomConditionalPotentialExpressionGenerator(result.random, result.propositionalTheories, result.equalityTheories, result.inequalityTheories, result.depth);
    return result;
}
Also used : PrintStream(java.io.PrintStream) OptionParser(joptsimple.OptionParser) Random(java.util.Random) OptionSet(joptsimple.OptionSet) File(java.io.File)

Example 100 with OptionParser

use of joptsimple.OptionParser in project aic-praise by aic-sri-international.

the class PRAiSE method getArgs.

public static SGSolverArgs getArgs(String[] args) throws UnsupportedEncodingException, FileNotFoundException, IOException {
    SGSolverArgs result = new SGSolverArgs();
    OptionParser parser = new OptionParser();
    // Optional
    OptionSpec<String> language = parser.accepts("language", "input model language (code), allowed values are " + getLegalModelLanguageCodesDescription()).withRequiredArg().ofType(String.class);
    OptionSpec<String> query = parser.accepts("query", "query to run over all input models").withRequiredArg().ofType(String.class);
    OptionSpec<File> outputFile = parser.accepts("output", "output file name (defaults to stdout).").withRequiredArg().ofType(File.class);
    // Help
    OptionSpec<Void> help = parser.accepts("help", "command line options help").forHelp();
    //
    String usage = "java " + PRAiSE.class.getName() + " [--help] [--language language_code] [--query global_query_string] [--output output_file_name] inputModelFile ..." + "\n\n" + "This command reads a set of models from input files and executes a set of queries on each of them.\n\n" + "The models are obtained in the following manner:\n" + "- input files may be one or more; they can be .praise files (saved from the PRAiSE editor and solver) or plain text files.\n" + "- each .praise input file contains possibly multiple pages, each with a model and a set of queries for it (see PRAiSE editor and solver).\n" + "- multiple plain text input files are combined into a single model (not combined with .praise models).\n\n" + "The queries are obtained in the following manner:\n" + "- each page in each .praise file contains a list of queries for its specific model\n" + "- queries specified with --query option will apply to all models from all .praise files and to the model from combined plain text input files.\n\n" + "Evidence can be encoded as deterministic statements (see examples in PRAiSE editor and solver).\n";
    List<String> errors = new ArrayList<>();
    List<String> warnings = new ArrayList<>();
    try {
        OptionSet options = parser.parse(args);
        if (options.has(help)) {
            System.out.println(usage);
            parser.printHelpOn(System.out);
            System.exit(0);
        }
        for (Object inputFileName : options.nonOptionArguments()) {
            File inputFile = new File(inputFileName.toString());
            if (inputFile.exists()) {
                result.inputFiles.add(inputFile);
            } else {
                errors.add("Input file [" + inputFileName + "] is not a file or does not exist.");
            }
        }
        if (result.inputFiles.size() == 0) {
            errors.add("No input files specified");
        }
        if (options.has(query)) {
            for (String commandLineQuery : options.valuesOf(query)) {
                result.globalQueries.add(commandLineQuery);
            }
        }
        if (options.has(language)) {
            String languageCode = options.valueOf(language);
            result.inputLanguage = findLanguageModel(languageCode);
            if (result.inputLanguage == null) {
                errors.add("Input language code, " + languageCode + ", is not legal. Legal values are " + getLegalModelLanguageCodesDescription() + ".");
            }
        } else {
            // Guess the language from the input file names
            result.inputLanguage = guessLanguageModel(result.inputFiles);
            if (result.inputLanguage == null) {
                errors.add("Unable to guess the input language from the given input file name extensions. Legal input language codes are " + getLegalModelLanguageCodesDescription() + ".");
            }
        }
        if (options.has(outputFile)) {
            File outFile = options.valueOf(outputFile);
            if (outFile.exists()) {
                if (outFile.isDirectory()) {
                    errors.add("Output file specified [" + outFile.getAbsolutePath() + "] is a directory, must be a legal file name.");
                } else {
                    warnings.add("Warning output file [" + outFile.getAbsolutePath() + "] already exists, will be overwritten.");
                }
            }
            // Only setup file output if there are no errors.
            if (errors.size() == 0) {
                result.out = new PrintStream(outFile, FILE_CHARSET.name());
            }
        }
    } catch (OptionException oe) {
        errors.add(oe.getMessage());
    }
    if (warnings.size() > 0) {
        warnings.forEach(warning -> System.err.println("WARNING: " + warning));
    }
    if (errors.size() > 0) {
        errors.forEach(error -> System.err.println("ERROR: " + error));
        System.err.println(usage);
        parser.printHelpOn(System.err);
        System.exit(1);
    }
    return result;
}
Also used : PrintStream(java.io.PrintStream) ArrayList(java.util.ArrayList) OptionException(joptsimple.OptionException) OptionParser(joptsimple.OptionParser) OptionSet(joptsimple.OptionSet) File(java.io.File)

Aggregations

OptionParser (joptsimple.OptionParser)118 OptionSet (joptsimple.OptionSet)91 File (java.io.File)35 IOException (java.io.IOException)15 OptionException (joptsimple.OptionException)14 List (java.util.List)13 Test (org.junit.Test)13 ArrayList (java.util.ArrayList)11 Cluster (voldemort.cluster.Cluster)8 FileNotFoundException (java.io.FileNotFoundException)6 StoreDefinition (voldemort.store.StoreDefinition)6 Closer (com.google.common.io.Closer)5 BufferedReader (java.io.BufferedReader)5 Properties (java.util.Properties)5 OptionSpec (joptsimple.OptionSpec)5 Node (voldemort.cluster.Node)5 ByteArray (voldemort.utils.ByteArray)5 ClusterMapper (voldemort.xml.ClusterMapper)5 MongoClientURI (com.mongodb.MongoClientURI)4 FileReader (java.io.FileReader)4