Search in sources :

Example 11 with ArgumentParserException

use of net.sourceforge.argparse4j.inf.ArgumentParserException in project S4T1TM2 by CSUS-CSC-131-Spring2018.

the class YaamServerLauncher method main.

public static void main(String... args) {
    // build argument parser
    ArgumentParser parser = ArgumentParsers.newFor("TaskManager").addHelp(false).build().defaultHelp(true).description("YAAM WebServer");
    // add additional parameters
    ArgumentGroup optionalArguments = parser.addArgumentGroup("Optional Arguments");
    // allow specifying a configuration file by a filepath
    optionalArguments.addArgument("--conf", "--config").dest("configFile").nargs(1).type(File.class).metavar("config.yml").setDefault(new File("yaam-config.yml")).help("Specifies a configuration file by filepath");
    // use no external configuration file, use bundled config
    optionalArguments.addArgument("--noConfig").dest("noConfig").type(boolean.class).help("Uses default bundled config");
    // parse arguments
    Namespace namespace = new Namespace(new HashMap<>());
    try {
        parser.parseArgs(args, namespace.getAttrs());
    } catch (ArgumentParserException exception) {
        System.err.println("error: " + exception.getMessage());
        System.err.println();
        parser.printHelp(new PrintWriter(System.err, true));
        System.exit(-1);
    }
    // load YAAM server configuration and settings into memory
    YaamConfig config = YaamConfig.load(namespace);
    log.info("Loaded configuration");
    // construct WebAPI
    log.info("Launching web api...");
    try {
        WebServerAPI apiServer = new WebServerAPI(config);
        apiServer.launch();
    } catch (Throwable throwable) {
        log.fatal("Failed to launch WebServerAPI", throwable);
        System.exit(-1);
    }
    log.info("Web API launched");
    // construct WebSocket
    log.info("Launching web socket server...");
    try {
        WebSocketServer webSocketServer = new WebSocketServer(config);
        webSocketServer.launch();
    } catch (Throwable throwable) {
        log.fatal("Failed to launch WebSocketServer", throwable);
        System.exit(-1);
    }
    log.info("Web socket server launched");
    log.info("Server running");
}
Also used : WebSocketServer(edu.csus.yaam.server.websocket.WebSocketServer) WebServerAPI(edu.csus.yaam.server.webapi.WebServerAPI) ArgumentParserException(net.sourceforge.argparse4j.inf.ArgumentParserException) ArgumentParser(net.sourceforge.argparse4j.inf.ArgumentParser) File(java.io.File) ArgumentGroup(net.sourceforge.argparse4j.inf.ArgumentGroup) Namespace(net.sourceforge.argparse4j.inf.Namespace) PrintWriter(java.io.PrintWriter)

Example 12 with ArgumentParserException

use of net.sourceforge.argparse4j.inf.ArgumentParserException in project kafka by apache.

the class MirrorMaker method main.

public static void main(String[] args) {
    ArgumentParser parser = ArgumentParsers.newArgumentParser("connect-mirror-maker");
    parser.description("MirrorMaker 2.0 driver");
    parser.addArgument("config").type(Arguments.fileType().verifyCanRead()).metavar("mm2.properties").required(true).help("MM2 configuration file.");
    parser.addArgument("--clusters").nargs("+").metavar("CLUSTER").required(false).help("Target cluster to use for this node.");
    Namespace ns;
    try {
        ns = parser.parseArgs(args);
    } catch (ArgumentParserException e) {
        parser.handleError(e);
        Exit.exit(-1);
        return;
    }
    File configFile = ns.get("config");
    List<String> clusters = ns.getList("clusters");
    try {
        log.info("Kafka MirrorMaker initializing ...");
        Properties props = Utils.loadProps(configFile.getPath());
        Map<String, String> config = Utils.propsToStringMap(props);
        MirrorMaker mirrorMaker = new MirrorMaker(config, clusters, Time.SYSTEM);
        try {
            mirrorMaker.start();
        } catch (Exception e) {
            log.error("Failed to start MirrorMaker", e);
            mirrorMaker.stop();
            Exit.exit(3);
        }
        mirrorMaker.awaitStop();
    } catch (Throwable t) {
        log.error("Stopping due to error", t);
        Exit.exit(2);
    }
}
Also used : ArgumentParserException(net.sourceforge.argparse4j.inf.ArgumentParserException) Properties(java.util.Properties) ArgumentParser(net.sourceforge.argparse4j.inf.ArgumentParser) File(java.io.File) Namespace(net.sourceforge.argparse4j.inf.Namespace) NotLeaderException(org.apache.kafka.connect.runtime.distributed.NotLeaderException) ArgumentParserException(net.sourceforge.argparse4j.inf.ArgumentParserException)

Example 13 with ArgumentParserException

use of net.sourceforge.argparse4j.inf.ArgumentParserException in project kafka by apache.

the class Commands method parseCommand.

/**
 * Handle the given command.
 *
 * In general this function should not throw exceptions. Instead, it should
 * return ErroneousCommandHandler if the input was invalid.
 *
 * @param arguments     The command line arguments.
 * @return              The command handler.
 */
public Handler parseCommand(List<String> arguments) {
    List<String> trimmedArguments = new ArrayList<>(arguments);
    while (true) {
        if (trimmedArguments.isEmpty()) {
            return new NoOpCommandHandler();
        }
        String last = trimmedArguments.get(trimmedArguments.size() - 1);
        if (!last.isEmpty()) {
            break;
        }
        trimmedArguments.remove(trimmedArguments.size() - 1);
    }
    Namespace namespace;
    try {
        namespace = parser.parseArgs(trimmedArguments.toArray(new String[0]));
    } catch (HelpScreenException e) {
        return new NoOpCommandHandler();
    } catch (ArgumentParserException e) {
        return new ErroneousCommandHandler(e.getMessage());
    }
    String command = namespace.get("command");
    if (!command.equals(trimmedArguments.get(0))) {
        return new ErroneousCommandHandler("invalid choice: '" + trimmedArguments.get(0) + "': did you mean '" + command + "'?");
    }
    Type type = TYPES.get(command);
    if (type == null) {
        return new ErroneousCommandHandler("Unknown command specified: " + command);
    } else {
        return type.createHandler(namespace);
    }
}
Also used : HelpScreenException(net.sourceforge.argparse4j.internal.HelpScreenException) ArrayList(java.util.ArrayList) ArgumentParserException(net.sourceforge.argparse4j.inf.ArgumentParserException) Namespace(net.sourceforge.argparse4j.inf.Namespace)

Example 14 with ArgumentParserException

use of net.sourceforge.argparse4j.inf.ArgumentParserException in project kafka by apache.

the class ProducerPerformance method start.

void start(String[] args) throws IOException {
    ArgumentParser parser = argParser();
    try {
        Namespace res = parser.parseArgs(args);
        /* parse args */
        String topicName = res.getString("topic");
        long numRecords = res.getLong("numRecords");
        Integer recordSize = res.getInt("recordSize");
        int throughput = res.getInt("throughput");
        List<String> producerProps = res.getList("producerConfig");
        String producerConfig = res.getString("producerConfigFile");
        String payloadFilePath = res.getString("payloadFile");
        String transactionalId = res.getString("transactionalId");
        boolean shouldPrintMetrics = res.getBoolean("printMetrics");
        long transactionDurationMs = res.getLong("transactionDurationMs");
        boolean transactionsEnabled = 0 < transactionDurationMs;
        // since default value gets printed with the help text, we are escaping \n there and replacing it with correct value here.
        String payloadDelimiter = res.getString("payloadDelimiter").equals("\\n") ? "\n" : res.getString("payloadDelimiter");
        if (producerProps == null && producerConfig == null) {
            throw new ArgumentParserException("Either --producer-props or --producer.config must be specified.", parser);
        }
        List<byte[]> payloadByteList = readPayloadFile(payloadFilePath, payloadDelimiter);
        Properties props = readProps(producerProps, producerConfig, transactionalId, transactionsEnabled);
        KafkaProducer<byte[], byte[]> producer = createKafkaProducer(props);
        if (transactionsEnabled)
            producer.initTransactions();
        /* setup perf test */
        byte[] payload = null;
        if (recordSize != null) {
            payload = new byte[recordSize];
        }
        Random random = new Random(0);
        ProducerRecord<byte[], byte[]> record;
        Stats stats = new Stats(numRecords, 5000);
        long startMs = System.currentTimeMillis();
        ThroughputThrottler throttler = new ThroughputThrottler(throughput, startMs);
        int currentTransactionSize = 0;
        long transactionStartTime = 0;
        for (long i = 0; i < numRecords; i++) {
            payload = generateRandomPayload(recordSize, payloadByteList, payload, random);
            if (transactionsEnabled && currentTransactionSize == 0) {
                producer.beginTransaction();
                transactionStartTime = System.currentTimeMillis();
            }
            record = new ProducerRecord<>(topicName, payload);
            long sendStartMs = System.currentTimeMillis();
            Callback cb = stats.nextCompletion(sendStartMs, payload.length, stats);
            producer.send(record, cb);
            currentTransactionSize++;
            if (transactionsEnabled && transactionDurationMs <= (sendStartMs - transactionStartTime)) {
                producer.commitTransaction();
                currentTransactionSize = 0;
            }
            if (throttler.shouldThrottle(i, sendStartMs)) {
                throttler.throttle();
            }
        }
        if (transactionsEnabled && currentTransactionSize != 0)
            producer.commitTransaction();
        if (!shouldPrintMetrics) {
            producer.close();
            /* print final results */
            stats.printTotal();
        } else {
            // Make sure all messages are sent before printing out the stats and the metrics
            // We need to do this in a different branch for now since tests/kafkatest/sanity_checks/test_performance_services.py
            // expects this class to work with older versions of the client jar that don't support flush().
            producer.flush();
            /* print final results */
            stats.printTotal();
            /* print out metrics */
            ToolsUtils.printMetrics(producer.metrics());
            producer.close();
        }
    } catch (ArgumentParserException e) {
        if (args.length == 0) {
            parser.printHelp();
            Exit.exit(0);
        } else {
            parser.handleError(e);
            Exit.exit(1);
        }
    }
}
Also used : Properties(java.util.Properties) ArgumentParser(net.sourceforge.argparse4j.inf.ArgumentParser) Namespace(net.sourceforge.argparse4j.inf.Namespace) Callback(org.apache.kafka.clients.producer.Callback) Random(java.util.Random) ArgumentParserException(net.sourceforge.argparse4j.inf.ArgumentParserException)

Example 15 with ArgumentParserException

use of net.sourceforge.argparse4j.inf.ArgumentParserException in project kafka by apache.

the class VerifiableConsumer method createFromArgs.

public static VerifiableConsumer createFromArgs(ArgumentParser parser, String[] args) throws ArgumentParserException {
    Namespace res = parser.parseArgs(args);
    boolean useAutoCommit = res.getBoolean("useAutoCommit");
    String configFile = res.getString("consumer.config");
    String brokerHostandPort = null;
    Properties consumerProps = new Properties();
    if (configFile != null) {
        try {
            consumerProps.putAll(Utils.loadProps(configFile));
        } catch (IOException e) {
            throw new ArgumentParserException(e.getMessage(), parser);
        }
    }
    consumerProps.put(ConsumerConfig.GROUP_ID_CONFIG, res.getString("groupId"));
    String groupInstanceId = res.getString("groupInstanceId");
    if (groupInstanceId != null) {
        consumerProps.put(ConsumerConfig.GROUP_INSTANCE_ID_CONFIG, groupInstanceId);
    }
    if (res.get("bootstrapServer") != null) {
        brokerHostandPort = res.getString("bootstrapServer");
    } else if (res.getString("brokerList") != null) {
        brokerHostandPort = res.getString("brokerList");
    } else {
        parser.printHelp();
        // Can't use `Exit.exit` here because it didn't exist until 0.11.0.0.
        System.exit(0);
    }
    consumerProps.put(ConsumerConfig.BOOTSTRAP_SERVERS_CONFIG, brokerHostandPort);
    consumerProps.put(ConsumerConfig.ENABLE_AUTO_COMMIT_CONFIG, useAutoCommit);
    consumerProps.put(ConsumerConfig.AUTO_OFFSET_RESET_CONFIG, res.getString("resetPolicy"));
    consumerProps.put(ConsumerConfig.SESSION_TIMEOUT_MS_CONFIG, Integer.toString(res.getInt("sessionTimeout")));
    consumerProps.put(ConsumerConfig.PARTITION_ASSIGNMENT_STRATEGY_CONFIG, res.getString("assignmentStrategy"));
    StringDeserializer deserializer = new StringDeserializer();
    KafkaConsumer<String, String> consumer = new KafkaConsumer<>(consumerProps, deserializer, deserializer);
    String topic = res.getString("topic");
    int maxMessages = res.getInt("maxMessages");
    boolean verbose = res.getBoolean("verbose");
    return new VerifiableConsumer(consumer, System.out, topic, maxMessages, useAutoCommit, false, verbose);
}
Also used : StringDeserializer(org.apache.kafka.common.serialization.StringDeserializer) KafkaConsumer(org.apache.kafka.clients.consumer.KafkaConsumer) IOException(java.io.IOException) ArgumentParserException(net.sourceforge.argparse4j.inf.ArgumentParserException) Properties(java.util.Properties) Namespace(net.sourceforge.argparse4j.inf.Namespace)

Aggregations

ArgumentParserException (net.sourceforge.argparse4j.inf.ArgumentParserException)42 Namespace (net.sourceforge.argparse4j.inf.Namespace)34 ArgumentParser (net.sourceforge.argparse4j.inf.ArgumentParser)30 Properties (java.util.Properties)13 IOException (java.io.IOException)10 ArrayList (java.util.ArrayList)6 MutuallyExclusiveGroup (net.sourceforge.argparse4j.inf.MutuallyExclusiveGroup)6 ArgumentGroup (net.sourceforge.argparse4j.inf.ArgumentGroup)4 Platform (org.apache.kafka.trogdor.common.Platform)4 JsonRestServer (org.apache.kafka.trogdor.rest.JsonRestServer)4 AnnotatorService (edu.illinois.cs.cogcomp.annotation.AnnotatorService)3 File (java.io.File)3 Path (java.nio.file.Path)3 List (java.util.List)3 Random (java.util.Random)3 HelpScreenException (net.sourceforge.argparse4j.internal.HelpScreenException)3 KafkaProducer (org.apache.kafka.clients.producer.KafkaProducer)3 Arrays (java.util.Arrays)2 Collections (java.util.Collections)2 HashSet (java.util.HashSet)2