Search in sources :

Example 16 with ParameterException

use of com.beust.jcommander.ParameterException in project pulsar by yahoo.

the class CmdNamespaceIsolationPolicy method createNamespaceIsolationData.

private NamespaceIsolationData createNamespaceIsolationData(List<String> namespaces, List<String> primary, List<String> secondary, String autoFailoverPolicyTypeName, Map<String, String> autoFailoverPolicyParams) {
    // validate
    namespaces = validateList(namespaces);
    if (namespaces.isEmpty()) {
        throw new ParameterException("unable to parse namespaces parameter list: " + namespaces);
    }
    primary = validateList(primary);
    if (primary.isEmpty()) {
        throw new ParameterException("unable to parse primary parameter list: " + namespaces);
    }
    secondary = validateList(secondary);
    // System.out.println("namespaces = " + namespaces);
    // System.out.println("primary = " + primary);
    // System.out.println("secondary = " + secondary);
    // System.out.println("autoFailoverPolicyTypeName = " + autoFailoverPolicyTypeName);
    // System.out.println("autoFailoverPolicyParams = " + autoFailoverPolicyParams);
    NamespaceIsolationData nsIsolationData = new NamespaceIsolationData();
    if (namespaces != null) {
        nsIsolationData.namespaces = namespaces;
    }
    if (primary != null) {
        nsIsolationData.primary = primary;
    }
    if (secondary != null) {
        nsIsolationData.secondary = secondary;
    }
    nsIsolationData.auto_failover_policy = new AutoFailoverPolicyData();
    nsIsolationData.auto_failover_policy.policy_type = AutoFailoverPolicyType.fromString(autoFailoverPolicyTypeName);
    nsIsolationData.auto_failover_policy.parameters = autoFailoverPolicyParams;
    // validation if necessary
    if (nsIsolationData.auto_failover_policy.policy_type == AutoFailoverPolicyType.min_available) {
        // ignore
        boolean error = true;
        String[] expectParamKeys = { "min_limit", "usage_threshold" };
        if (autoFailoverPolicyParams.size() == expectParamKeys.length) {
            for (String paramKey : expectParamKeys) {
                if (!autoFailoverPolicyParams.containsKey(paramKey)) {
                    break;
                }
            }
            error = false;
        }
        if (error) {
            throw new ParameterException("Unknown auto failover policy params specified : " + autoFailoverPolicyParams);
        }
    } else {
        // either we don't handle the new type or user has specified a bad type
        throw new ParameterException("Unknown auto failover policy type specified : " + autoFailoverPolicyTypeName);
    }
    return nsIsolationData;
}
Also used : AutoFailoverPolicyData(com.yahoo.pulsar.common.policies.data.AutoFailoverPolicyData) NamespaceIsolationData(com.yahoo.pulsar.common.policies.data.NamespaceIsolationData) ParameterException(com.beust.jcommander.ParameterException)

Example 17 with ParameterException

use of com.beust.jcommander.ParameterException in project pulsar by yahoo.

the class CmdConsume method run.

/**
     * Run the consume command.
     *
     * @return 0 for success, < 0 otherwise
     */
public int run() throws PulsarClientException, IOException {
    if (mainOptions.size() != 1)
        throw (new ParameterException("Please provide one and only one topic name."));
    if (this.serviceURL == null || this.serviceURL.isEmpty())
        throw (new ParameterException("Broker URL is not provided."));
    if (this.subscriptionName == null || this.subscriptionName.isEmpty())
        throw (new ParameterException("Subscription name is not provided."));
    if (this.numMessagesToConsume < 0)
        throw (new ParameterException("Number of messages should be zero or positive."));
    String topic = this.mainOptions.get(0);
    int numMessagesConsumed = 0;
    int returnCode = 0;
    try {
        ConsumerConfiguration consumerConf = new ConsumerConfiguration();
        consumerConf.setSubscriptionType(this.subscriptionType);
        PulsarClient client = PulsarClient.create(this.serviceURL, this.clientConfig);
        Consumer consumer = client.subscribe(topic, this.subscriptionName, consumerConf);
        RateLimiter limiter = (this.consumeRate > 0) ? RateLimiter.create(this.consumeRate) : null;
        while (this.numMessagesToConsume == 0 || numMessagesConsumed < this.numMessagesToConsume) {
            if (limiter != null) {
                limiter.acquire();
            }
            Message msg = consumer.receive(5, TimeUnit.SECONDS);
            if (msg == null) {
                LOG.warn("No message to consume after waiting for 20 seconds.");
            } else {
                numMessagesConsumed += 1;
                System.out.println(MESSAGE_BOUNDARY);
                String output = this.interpretMessage(msg, displayHex);
                System.out.println(output);
                consumer.acknowledge(msg);
            }
        }
        client.close();
    } catch (Exception e) {
        LOG.error("Error while consuming messages");
        LOG.error(e.getMessage(), e);
        returnCode = -1;
    } finally {
        LOG.info("{} messages successfully consumed", numMessagesConsumed);
    }
    return returnCode;
}
Also used : Consumer(com.yahoo.pulsar.client.api.Consumer) Message(com.yahoo.pulsar.client.api.Message) ConsumerConfiguration(com.yahoo.pulsar.client.api.ConsumerConfiguration) ParameterException(com.beust.jcommander.ParameterException) PulsarClient(com.yahoo.pulsar.client.api.PulsarClient) RateLimiter(com.google.common.util.concurrent.RateLimiter) ParameterException(com.beust.jcommander.ParameterException) IOException(java.io.IOException) PulsarClientException(com.yahoo.pulsar.client.api.PulsarClientException)

Example 18 with ParameterException

use of com.beust.jcommander.ParameterException in project pulsar by yahoo.

the class CmdProduce method run.

/**
     * Run the producer.
     *
     * @return 0 for success, < 0 otherwise
     * @throws Exception
     */
public int run() throws PulsarClientException {
    if (mainOptions.size() != 1)
        throw (new ParameterException("Please provide one and only one topic name."));
    if (this.serviceURL == null || this.serviceURL.isEmpty())
        throw (new ParameterException("Broker URL is not provided."));
    if (this.numTimesProduce <= 0)
        throw (new ParameterException("Number of times need to be positive number."));
    if (messages.size() == 0 && messageFileNames.size() == 0)
        throw (new ParameterException("Please supply message content with either --messages or --files"));
    int totalMessages = (messages.size() + messageFileNames.size()) * numTimesProduce;
    if (totalMessages > MAX_MESSAGES) {
        String msg = "Attempting to send " + totalMessages + " messages. Please do not send more than " + MAX_MESSAGES + " messages";
        throw new ParameterException(msg);
    }
    String topic = this.mainOptions.get(0);
    int numMessagesSent = 0;
    int returnCode = 0;
    try {
        PulsarClient client = PulsarClient.create(this.serviceURL, this.clientConfig);
        Producer producer = client.createProducer(topic);
        List<byte[]> messageBodies = generateMessageBodies(this.messages, this.messageFileNames);
        RateLimiter limiter = (this.publishRate > 0) ? RateLimiter.create(this.publishRate) : null;
        for (int i = 0; i < this.numTimesProduce; i++) {
            List<Message> messages = generateMessages(messageBodies);
            for (Message msg : messages) {
                if (limiter != null)
                    limiter.acquire();
                producer.send(msg);
                numMessagesSent++;
            }
        }
        client.close();
    } catch (Exception e) {
        LOG.error("Error while producing messages");
        LOG.error(e.getMessage(), e);
        returnCode = -1;
    } finally {
        LOG.info("{} messages successfully produced", numMessagesSent);
    }
    return returnCode;
}
Also used : Producer(com.yahoo.pulsar.client.api.Producer) Message(com.yahoo.pulsar.client.api.Message) ParameterException(com.beust.jcommander.ParameterException) PulsarClient(com.yahoo.pulsar.client.api.PulsarClient) RateLimiter(com.google.common.util.concurrent.RateLimiter) ParameterException(com.beust.jcommander.ParameterException) PulsarClientException(com.yahoo.pulsar.client.api.PulsarClientException)

Example 19 with ParameterException

use of com.beust.jcommander.ParameterException in project pulsar by yahoo.

the class PerformanceConsumer method main.

public static void main(String[] args) throws Exception {
    final Arguments arguments = new Arguments();
    JCommander jc = new JCommander(arguments);
    jc.setProgramName("pulsar-perf-consumer");
    try {
        jc.parse(args);
    } catch (ParameterException e) {
        System.out.println(e.getMessage());
        jc.usage();
        System.exit(-1);
    }
    if (arguments.help) {
        jc.usage();
        System.exit(-1);
    }
    if (arguments.topic.size() != 1) {
        System.out.println("Only one destination name is allowed");
        jc.usage();
        System.exit(-1);
    }
    if (arguments.confFile != null) {
        Properties prop = new Properties(System.getProperties());
        prop.load(new FileInputStream(arguments.confFile));
        if (arguments.serviceURL == null) {
            arguments.serviceURL = prop.getProperty("brokerServiceUrl");
        }
        if (arguments.serviceURL == null) {
            arguments.serviceURL = prop.getProperty("webServiceUrl");
        }
        // fallback to previous-version serviceUrl property to maintain backward-compatibility
        if (arguments.serviceURL == null) {
            arguments.serviceURL = prop.getProperty("serviceUrl", "http://localhost:8080/");
        }
        if (arguments.authPluginClassName == null) {
            arguments.authPluginClassName = prop.getProperty("authPlugin", null);
        }
        if (arguments.authParams == null) {
            arguments.authParams = prop.getProperty("authParams", null);
        }
    }
    // Dump config variables
    ObjectMapper m = new ObjectMapper();
    ObjectWriter w = m.writerWithDefaultPrettyPrinter();
    log.info("Starting Pulsar performance consumer with config: {}", w.writeValueAsString(arguments));
    final DestinationName prefixDestinationName = DestinationName.get(arguments.topic.get(0));
    final RateLimiter limiter = arguments.rate > 0 ? RateLimiter.create(arguments.rate) : null;
    MessageListener listener = new MessageListener() {

        public void received(Consumer consumer, Message msg) {
            messagesReceived.increment();
            bytesReceived.add(msg.getData().length);
            if (limiter != null) {
                limiter.acquire();
            }
            consumer.acknowledgeAsync(msg);
        }
    };
    EventLoopGroup eventLoopGroup;
    if (SystemUtils.IS_OS_LINUX) {
        eventLoopGroup = new EpollEventLoopGroup(Runtime.getRuntime().availableProcessors() * 2, new DefaultThreadFactory("pulsar-perf-consumer"));
    } else {
        eventLoopGroup = new NioEventLoopGroup(Runtime.getRuntime().availableProcessors(), new DefaultThreadFactory("pulsar-perf-consumer"));
    }
    ClientConfiguration clientConf = new ClientConfiguration();
    clientConf.setConnectionsPerBroker(arguments.maxConnections);
    clientConf.setStatsInterval(arguments.statsIntervalSeconds, TimeUnit.SECONDS);
    if (isNotBlank(arguments.authPluginClassName)) {
        clientConf.setAuthentication(arguments.authPluginClassName, arguments.authParams);
    }
    PulsarClient pulsarClient = new PulsarClientImpl(arguments.serviceURL, clientConf, eventLoopGroup);
    List<Future<Consumer>> futures = Lists.newArrayList();
    ConsumerConfiguration consumerConfig = new ConsumerConfiguration();
    consumerConfig.setMessageListener(listener);
    consumerConfig.setReceiverQueueSize(arguments.receiverQueueSize);
    for (int i = 0; i < arguments.numDestinations; i++) {
        final DestinationName destinationName = (arguments.numDestinations == 1) ? prefixDestinationName : DestinationName.get(String.format("%s-%d", prefixDestinationName, i));
        log.info("Adding {} consumers on destination {}", arguments.numConsumers, destinationName);
        for (int j = 0; j < arguments.numConsumers; j++) {
            String subscriberName;
            if (arguments.numConsumers > 1) {
                subscriberName = String.format("%s-%d", arguments.subscriberName, j);
            } else {
                subscriberName = arguments.subscriberName;
            }
            futures.add(pulsarClient.subscribeAsync(destinationName.toString(), subscriberName, consumerConfig));
        }
    }
    for (Future<Consumer> future : futures) {
        future.get();
    }
    log.info("Start receiving from {} consumers on {} destinations", arguments.numConsumers, arguments.numDestinations);
    long oldTime = System.nanoTime();
    while (true) {
        try {
            Thread.sleep(10000);
        } catch (InterruptedException e) {
            break;
        }
        long now = System.nanoTime();
        double elapsed = (now - oldTime) / 1e9;
        double rate = messagesReceived.sumThenReset() / elapsed;
        double throughput = bytesReceived.sumThenReset() / elapsed * 8 / 1024 / 1024;
        log.info("Throughput received: {}  msg/s -- {} Mbit/s", dec.format(rate), dec.format(throughput));
        oldTime = now;
    }
    pulsarClient.close();
}
Also used : Message(com.yahoo.pulsar.client.api.Message) MessageListener(com.yahoo.pulsar.client.api.MessageListener) Properties(java.util.Properties) DefaultThreadFactory(io.netty.util.concurrent.DefaultThreadFactory) Consumer(com.yahoo.pulsar.client.api.Consumer) JCommander(com.beust.jcommander.JCommander) DestinationName(com.yahoo.pulsar.common.naming.DestinationName) ConsumerConfiguration(com.yahoo.pulsar.client.api.ConsumerConfiguration) ParameterException(com.beust.jcommander.ParameterException) PulsarClient(com.yahoo.pulsar.client.api.PulsarClient) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) NioEventLoopGroup(io.netty.channel.nio.NioEventLoopGroup) ObjectWriter(com.fasterxml.jackson.databind.ObjectWriter) FileInputStream(java.io.FileInputStream) RateLimiter(com.google.common.util.concurrent.RateLimiter) EpollEventLoopGroup(io.netty.channel.epoll.EpollEventLoopGroup) EventLoopGroup(io.netty.channel.EventLoopGroup) NioEventLoopGroup(io.netty.channel.nio.NioEventLoopGroup) EpollEventLoopGroup(io.netty.channel.epoll.EpollEventLoopGroup) Future(java.util.concurrent.Future) PulsarClientImpl(com.yahoo.pulsar.client.impl.PulsarClientImpl) ClientConfiguration(com.yahoo.pulsar.client.api.ClientConfiguration)

Example 20 with ParameterException

use of com.beust.jcommander.ParameterException in project ignite by apache.

the class AgentLauncher method main.

/**
 * @param args Args.
 */
public static void main(String[] args) throws Exception {
    log.info("Starting Apache Ignite Web Console Agent...");
    final AgentConfiguration cfg = new AgentConfiguration();
    JCommander jCommander = new JCommander(cfg);
    String osName = System.getProperty("os.name").toLowerCase();
    jCommander.setProgramName("ignite-web-agent." + (osName.contains("win") ? "bat" : "sh"));
    try {
        jCommander.parse(args);
    } catch (ParameterException pe) {
        log.error("Failed to parse command line parameters: " + Arrays.toString(args), pe);
        jCommander.usage();
        return;
    }
    String prop = cfg.configPath();
    AgentConfiguration propCfg = new AgentConfiguration();
    try {
        File f = AgentUtils.resolvePath(prop);
        if (f == null)
            log.warn("Failed to find agent property file: {}", prop);
        else
            propCfg.load(f.toURI().toURL());
    } catch (IOException e) {
        if (!AgentConfiguration.DFLT_CFG_PATH.equals(prop))
            log.warn("Failed to load agent property file: " + prop, e);
    }
    cfg.merge(propCfg);
    if (cfg.help()) {
        jCommander.usage();
        return;
    }
    System.out.println();
    System.out.println("Agent configuration:");
    System.out.println(cfg);
    System.out.println();
    if (cfg.tokens() == null) {
        String webHost;
        try {
            webHost = new URI(cfg.serverUri()).getHost();
        } catch (URISyntaxException e) {
            log.error("Failed to parse Ignite Web Console uri", e);
            return;
        }
        System.out.println("Security token is required to establish connection to the web console.");
        System.out.println(String.format("It is available on the Profile page: https://%s/profile", webHost));
        String tokens = String.valueOf(readPassword("Enter security tokens separated by comma: "));
        cfg.tokens(Arrays.asList(tokens.trim().split(",")));
    }
    URI uri = URI.create(cfg.serverUri());
    // Create proxy authenticator using passed properties.
    switch(uri.getScheme()) {
        case "http":
        case "https":
            final String username = System.getProperty(uri.getScheme() + ".proxyUsername");
            final char[] pwd = System.getProperty(uri.getScheme() + ".proxyPassword", "").toCharArray();
            Authenticator.setDefault(new Authenticator() {

                @Override
                protected PasswordAuthentication getPasswordAuthentication() {
                    return new PasswordAuthentication(username, pwd);
                }
            });
            break;
        default:
    }
    IO.Options opts = new IO.Options();
    opts.path = "/agents";
    // Workaround for use self-signed certificate
    if (Boolean.getBoolean("trust.all")) {
        SSLContext ctx = SSLContext.getInstance("TLS");
        // Create an SSLContext that uses our TrustManager
        ctx.init(null, getTrustManagers(), null);
        opts.sslContext = ctx;
    }
    final Socket client = IO.socket(uri, opts);
    final RestExecutor restExecutor = new RestExecutor(cfg.nodeUri());
    try {
        final ClusterListener clusterLsnr = new ClusterListener(client, restExecutor);
        final DemoListener demoHnd = new DemoListener(client, restExecutor);
        Emitter.Listener onConnect = new Emitter.Listener() {

            @Override
            public void call(Object... args) {
                log.info("Connection established.");
                JSONObject authMsg = new JSONObject();
                try {
                    authMsg.put("tokens", toJSON(cfg.tokens()));
                    authMsg.put("disableDemo", cfg.disableDemo());
                    String clsName = AgentLauncher.class.getSimpleName() + ".class";
                    String clsPath = AgentLauncher.class.getResource(clsName).toString();
                    if (clsPath.startsWith("jar")) {
                        String manifestPath = clsPath.substring(0, clsPath.lastIndexOf('!') + 1) + "/META-INF/MANIFEST.MF";
                        Manifest manifest = new Manifest(new URL(manifestPath).openStream());
                        Attributes attr = manifest.getMainAttributes();
                        authMsg.put("ver", attr.getValue("Implementation-Version"));
                        authMsg.put("bt", attr.getValue("Build-Time"));
                    }
                    client.emit("agent:auth", authMsg, new Ack() {

                        @Override
                        public void call(Object... args) {
                            if (args != null) {
                                if (args[0] instanceof String) {
                                    log.error((String) args[0]);
                                    System.exit(1);
                                }
                                if (args[0] == null && args[1] instanceof JSONArray) {
                                    try {
                                        List<String> activeTokens = fromJSON(args[1], List.class);
                                        if (!F.isEmpty(activeTokens)) {
                                            Collection<String> missedTokens = cfg.tokens();
                                            cfg.tokens(activeTokens);
                                            missedTokens.removeAll(activeTokens);
                                            if (!F.isEmpty(missedTokens)) {
                                                String tokens = F.concat(missedTokens, ", ");
                                                log.warn("Failed to authenticate with token(s): {}. " + "Please reload agent archive or check settings", tokens);
                                            }
                                            log.info("Authentication success.");
                                            clusterLsnr.watch();
                                            return;
                                        }
                                    } catch (Exception e) {
                                        log.error("Failed to authenticate agent. Please check agent\'s tokens", e);
                                        System.exit(1);
                                    }
                                }
                            }
                            log.error("Failed to authenticate agent. Please check agent\'s tokens");
                            System.exit(1);
                        }
                    });
                } catch (JSONException | IOException e) {
                    log.error("Failed to construct authentication message", e);
                    client.close();
                }
            }
        };
        DatabaseListener dbHnd = new DatabaseListener(cfg);
        RestListener restHnd = new RestListener(restExecutor);
        final CountDownLatch latch = new CountDownLatch(1);
        log.info("Connecting to: {}", cfg.serverUri());
        client.on(EVENT_CONNECT, onConnect).on(EVENT_CONNECT_ERROR, onError).on(EVENT_ERROR, onError).on(EVENT_DISCONNECT, onDisconnect).on(EVENT_LOG_WARNING, onLogWarning).on(EVENT_CLUSTER_BROADCAST_START, clusterLsnr.start()).on(EVENT_CLUSTER_BROADCAST_STOP, clusterLsnr.stop()).on(EVENT_DEMO_BROADCAST_START, demoHnd.start()).on(EVENT_DEMO_BROADCAST_STOP, demoHnd.stop()).on(EVENT_RESET_TOKENS, new Emitter.Listener() {

            @Override
            public void call(Object... args) {
                String tok = String.valueOf(args[0]);
                log.warn("Security token has been reset: {}", tok);
                cfg.tokens().remove(tok);
                if (cfg.tokens().isEmpty()) {
                    client.off();
                    latch.countDown();
                }
            }
        }).on(EVENT_SCHEMA_IMPORT_DRIVERS, dbHnd.availableDriversListener()).on(EVENT_SCHEMA_IMPORT_SCHEMAS, dbHnd.schemasListener()).on(EVENT_SCHEMA_IMPORT_METADATA, dbHnd.metadataListener()).on(EVENT_NODE_VISOR_TASK, restHnd).on(EVENT_NODE_REST, restHnd);
        client.connect();
        latch.await();
    } finally {
        restExecutor.stop();
        client.close();
    }
}
Also used : Emitter(io.socket.emitter.Emitter) DemoListener(org.apache.ignite.console.agent.handlers.DemoListener) ClusterListener(org.apache.ignite.console.agent.handlers.ClusterListener) RestListener(org.apache.ignite.console.agent.handlers.RestListener) DatabaseListener(org.apache.ignite.console.agent.handlers.DatabaseListener) Attributes(java.util.jar.Attributes) URISyntaxException(java.net.URISyntaxException) URI(java.net.URI) URL(java.net.URL) JCommander(com.beust.jcommander.JCommander) ParameterException(com.beust.jcommander.ParameterException) List(java.util.List) Authenticator(java.net.Authenticator) PasswordAuthentication(java.net.PasswordAuthentication) DemoListener(org.apache.ignite.console.agent.handlers.DemoListener) IO(io.socket.client.IO) RestExecutor(org.apache.ignite.console.agent.rest.RestExecutor) Ack(io.socket.client.Ack) JSONArray(org.json.JSONArray) JSONException(org.json.JSONException) IOException(java.io.IOException) SSLContext(javax.net.ssl.SSLContext) DatabaseListener(org.apache.ignite.console.agent.handlers.DatabaseListener) Manifest(java.util.jar.Manifest) CountDownLatch(java.util.concurrent.CountDownLatch) ParameterException(com.beust.jcommander.ParameterException) URISyntaxException(java.net.URISyntaxException) JSONException(org.json.JSONException) ConnectException(java.net.ConnectException) SSLHandshakeException(javax.net.ssl.SSLHandshakeException) IOException(java.io.IOException) UnknownHostException(java.net.UnknownHostException) RestListener(org.apache.ignite.console.agent.handlers.RestListener) JSONObject(org.json.JSONObject) ClusterListener(org.apache.ignite.console.agent.handlers.ClusterListener) Collection(java.util.Collection) JSONObject(org.json.JSONObject) File(java.io.File) Socket(io.socket.client.Socket)

Aggregations

ParameterException (com.beust.jcommander.ParameterException)20 JCommander (com.beust.jcommander.JCommander)15 RateLimiter (com.google.common.util.concurrent.RateLimiter)4 PulsarClient (com.yahoo.pulsar.client.api.PulsarClient)4 FileInputStream (java.io.FileInputStream)4 Message (com.yahoo.pulsar.client.api.Message)3 File (java.io.File)3 IOException (java.io.IOException)3 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)2 ObjectWriter (com.fasterxml.jackson.databind.ObjectWriter)2 ClientConfiguration (com.yahoo.pulsar.client.api.ClientConfiguration)2 Consumer (com.yahoo.pulsar.client.api.Consumer)2 ConsumerConfiguration (com.yahoo.pulsar.client.api.ConsumerConfiguration)2 Producer (com.yahoo.pulsar.client.api.Producer)2 PulsarClientException (com.yahoo.pulsar.client.api.PulsarClientException)2 PulsarClientImpl (com.yahoo.pulsar.client.impl.PulsarClientImpl)2 DestinationName (com.yahoo.pulsar.common.naming.DestinationName)2 EventLoopGroup (io.netty.channel.EventLoopGroup)2 EpollEventLoopGroup (io.netty.channel.epoll.EpollEventLoopGroup)2 NioEventLoopGroup (io.netty.channel.nio.NioEventLoopGroup)2