Search in sources :

Example 96 with OptionSet

use of joptsimple.OptionSet in project voldemort by voldemort.

the class ZoneClipperCLI method main.

public static void main(String[] args) throws Exception {
    setupParser();
    OptionSet options = getValidOptions(args);
    int dropZoneId = CmdUtils.valueOf(options, "drop-zoneid", Zone.UNSET_ZONE_ID);
    String outputDir = null;
    if (options.has("output-dir")) {
        outputDir = (String) options.valueOf("output-dir");
    }
    /*
         * A. Generate the clipped cluster.xml
         */
    String initialClusterXML = (String) options.valueOf("current-cluster");
    Cluster initialCluster = new ClusterMapper().readCluster(new File(initialClusterXML));
    // Create a list of current partition ids. We will use this set to
    // compare partitions ids in final cluster
    Set<Integer> originalPartitions = new HashSet<Integer>();
    for (Integer zoneId : initialCluster.getZoneIds()) {
        originalPartitions.addAll(initialCluster.getPartitionIdsInZone(zoneId));
    }
    // Get an intermediate cluster where partitions that belong to the zone
    // that is being dropped have been moved to the existing zones
    Cluster intermediateCluster = RebalanceUtils.vacateZone(initialCluster, dropZoneId);
    Cluster finalCluster = RebalanceUtils.dropZone(intermediateCluster, dropZoneId);
    // Make sure everything is fine
    if (initialCluster.getNumberOfPartitions() != finalCluster.getNumberOfPartitions()) {
        logger.error("The number of partitions in the initial and the final cluster is not equal \n");
    }
    Set<Integer> finalPartitions = new HashSet<Integer>();
    for (Integer zoneId : finalCluster.getZoneIds()) {
        finalPartitions.addAll(finalCluster.getPartitionIdsInZone(zoneId));
    }
    // Compare to original partition ids list
    if (!originalPartitions.equals(finalPartitions)) {
        logger.error("The list of partition ids in the initial and the final cluster doesn't match \n ");
    }
    // Finally write the final cluster to a xml file
    RebalanceUtils.dumpClusterToFile(outputDir, RebalanceUtils.finalClusterFileName, finalCluster);
    /*
         * B. Generate the clipped stores.xml
         */
    logger.info("Generating the adjusted stores.xml..");
    String initialStoresXML = (String) options.valueOf("current-stores");
    List<StoreDefinition> initialStoreDefs = new StoreDefinitionsMapper().readStoreList(new File(initialStoresXML));
    List<StoreDefinition> finalStoreDefs = RebalanceUtils.dropZone(initialStoreDefs, dropZoneId);
    RebalanceUtils.dumpStoreDefsToFile(outputDir, RebalanceUtils.finalStoresFileName, finalStoreDefs);
}
Also used : StoreDefinition(voldemort.store.StoreDefinition) StoreDefinitionsMapper(voldemort.xml.StoreDefinitionsMapper) Cluster(voldemort.cluster.Cluster) ClusterMapper(voldemort.xml.ClusterMapper) OptionSet(joptsimple.OptionSet) File(java.io.File) HashSet(java.util.HashSet)

Example 97 with OptionSet

use of joptsimple.OptionSet 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 98 with OptionSet

use of joptsimple.OptionSet 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 99 with OptionSet

use of joptsimple.OptionSet 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 100 with OptionSet

use of joptsimple.OptionSet 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)

Aggregations

OptionSet (joptsimple.OptionSet)121 OptionParser (joptsimple.OptionParser)93 File (java.io.File)40 OptionException (joptsimple.OptionException)22 IOException (java.io.IOException)20 List (java.util.List)16 Cluster (voldemort.cluster.Cluster)13 ArrayList (java.util.ArrayList)12 Test (org.junit.Test)12 StoreDefinition (voldemort.store.StoreDefinition)12 ClusterMapper (voldemort.xml.ClusterMapper)10 StoreDefinitionsMapper (voldemort.xml.StoreDefinitionsMapper)9 FileNotFoundException (java.io.FileNotFoundException)6 VoldemortException (voldemort.VoldemortException)6 BufferedReader (java.io.BufferedReader)5 Properties (java.util.Properties)5 OptionSpec (joptsimple.OptionSpec)5 Node (voldemort.cluster.Node)5 ByteArray (voldemort.utils.ByteArray)5 Closer (com.google.common.io.Closer)4