use of org.apache.commons.cli.CommandLine in project databus by linkedin.
the class BootstrapAvroRecordDumper method parseArgs.
@SuppressWarnings("static-access")
public static void parseArgs(String[] args) throws IOException {
CommandLineParser cliParser = new GnuParser();
Option outputDirOption = OptionBuilder.withLongOpt(OUTPUT_DIR_OPT_LONG_NAME).withDescription("Help screen").create(OUTPUT_DIR_OPT_CHAR);
Options options = new Options();
options.addOption(outputDirOption);
CommandLine cmd = null;
try {
cmd = cliParser.parse(options, args);
} catch (ParseException pe) {
LOG.fatal("Bootstrap Avro Record Dumper: failed to parse command-line options.", pe);
throw new RuntimeException("Bootstrap Avro Record Dumper: failed to parse command-line options.", pe);
}
if (cmd.hasOption(OUTPUT_DIR_OPT_CHAR)) {
outputDir = cmd.getOptionValue(OUTPUT_DIR_OPT_CHAR);
}
}
use of org.apache.commons.cli.CommandLine in project databus by linkedin.
the class BootstrapMetadata method main.
/**
*/
public static void main(String[] args) {
int exitStatus = 0;
GnuParser cmdLineParser = new GnuParser();
Options options = new Options();
options.addOption("d", "dbname", true, "database name : [bootstrap]").addOption("s", "dbhost", true, "database hostname: [localhost]").addOption("p", "dbpassword", true, "database password: [bootstrap]").addOption("u", "dbuser", true, "database user: [bootstrap] ").addOption("S", "sources", true, " <srcid,..>|<srcName substring,..,> [all]").addOption("m", "minScnType", true, "type of minScn algorithm: [catchup-forever] catchup-forever|catchup-purged|snapshot. " + "'snapshot': use when snapshot table has been purged and the size of snapshot is not large.Times-out in 5 minutes if query cannot complete. " + "'catchup-purged': use when snapshot table has been purged and a conservative value of minScn is acceptable or when 'snapshot' isn't feasible. " + "'catchup-forever': use when snapshot table has *never* been purged . " + "Note: If a source has been seeded, regardless of the minScnType, the value returned is 0.").addOption("h", "help", false, "help");
// add descriptions used in usage() 'functionName' , 'description-pairs'
String[] functionNames = { "getSources", "get sourceName, sourceId, sourceStatus from bootstrap db", "createMinScnTable", "create metadata required for minScn feature in bootstrap db if none exists", "createAllTables", "create all metadata in bootstrap db if they don't exist", "getMinScn", "[--sources <>] return minScn as seen by bootstrap service for specified sources", "computeMinScn", "[--sources <>] [--minScnType <>] return minScn using one of the algorithms specified. " + "INIT could mean failure to find a minSCN ", "computeAndSetMinScn", "[--sources <>] [--minScnType<>] set minScn using one of the algorithms specified. ", "setMinScn", "[--sources <>] INFINITY|INIT|<scn> set minScn to a desired value. " };
try {
CommandLine cmdLineArgs = cmdLineParser.parse(options, args, false);
if (cmdLineArgs.hasOption('h')) {
usage(options, functionNames);
System.exit(0);
}
String[] fns = cmdLineArgs.getArgs();
if (fns.length < 1) {
usage(options, functionNames);
throw new Exception("Missing argument");
}
String function = fns[0];
String arg1 = (fns.length > 1) ? fns[1] : null;
if (!legitFunction(functionNames, function)) {
usage(options, functionNames);
throw new Exception("Unknown function");
}
String bootstrapHost = cmdLineArgs.getOptionValue("dbhost", DEFAULT_BOOTSTRAP_HOST);
String bootstrapDbName = cmdLineArgs.getOptionValue("dbname", DEFAULT_BOOTSTRAP_DATABASE);
String bootstrapPassword = cmdLineArgs.getOptionValue("dbpassword", DEFAULT_BOOTSTRAP_PASSWORD);
String bootstrapUser = cmdLineArgs.getOptionValue("dbuser", DEFAULT_BOOTSTRAP_USER);
_bsConn = new BootstrapConn();
_bsConn.initBootstrapConn(false, bootstrapUser, bootstrapPassword, bootstrapHost, bootstrapDbName);
_dbao = new BootstrapDBMetaDataDAO(_bsConn, bootstrapHost, bootstrapUser, bootstrapPassword, bootstrapDbName, false);
String minScnType = cmdLineArgs.getOptionValue("minScnType", DEFAULT_MINSCN_TYPE);
if (function.equals("createMinScnTable")) {
createBootstrapMinScn();
return;
} else if (function.equals("createAllTables")) {
_dbao.setupDB();
return;
}
List<SourceStatusInfo> sourceInfoList = _dbao.getSrcStatusInfoFromDB();
if (function.equalsIgnoreCase("getSources")) {
for (SourceStatusInfo s : sourceInfoList) {
StringBuilder sb = new StringBuilder();
sb.append(s.getSrcName()).append('\t').append(s.getSrcId()).append('\t').append(s.getStatus());
System.out.println(sb.toString());
}
} else {
// not getSources()
// expect srcId list; for brevity;
String mySources = cmdLineArgs.getOptionValue("sources");
String[] mySrcList = null;
if (mySources != null) {
mySrcList = mySources.split(",");
}
List<SourceStatusInfo> finalSrcIdList = (mySrcList != null) ? getFinalSrcIdList(sourceInfoList, mySrcList) : sourceInfoList;
if (function.equalsIgnoreCase("getMinScn")) {
// read minScn from minscn table;
createBootstrapMinScn();
for (SourceStatusInfo sInfo : finalSrcIdList) {
long minScn = _dbao.getMinScnOfSnapshots(sInfo.getSrcId());
DBHelper.commit(_bsConn.getDBConn());
if (minScn == Long.MIN_VALUE) {
System.err.println("Error: Cannot get minScn for " + sInfo.getSrcId());
} else {
printScn(minScn, sInfo, '\t');
}
}
} else if (function.equalsIgnoreCase("computeMinScn")) {
// database not affected
for (SourceStatusInfo sInfo : finalSrcIdList) {
long minScn = computeMinScn(minScnType, sInfo.getSrcId());
// commit select;
DBHelper.commit(_bsConn.getDBConn());
if (minScn == Long.MIN_VALUE) {
System.err.println("Error: Cannot get minScn for " + sInfo.getSrcId());
} else {
printScn(minScn, sInfo, '\t');
}
}
} else if (function.equalsIgnoreCase("setMinScn")) {
if (arg1 == null) {
throw new Exception("Missing argument for setMinScn: <scn>, INFINITY or INIT");
}
// setMinScn <scn>
// override existing scn; never fail unless update fails ; show
// warning that you should know what you are doing
createBootstrapMinScn();
long scn;
if (arg1.equalsIgnoreCase("INFINITY")) {
scn = Long.MAX_VALUE;
} else if (arg1.equalsIgnoreCase("INIT")) {
scn = BootstrapDBMetaDataDAO.DEFAULT_WINDOWSCN;
} else {
scn = Long.parseLong(arg1);
}
for (SourceStatusInfo sInfo : finalSrcIdList) {
// calls commit
_dbao.updateMinScnOfSnapshot(sInfo.getSrcId(), scn);
}
} else if (function.equalsIgnoreCase("computeAndSetMinScn")) {
// set scn iff minScn has been initialized; ensure minScn is updated
// safely according to --type
// if seeded; --type is ignored;
createBootstrapMinScn();
for (SourceStatusInfo sInfo : finalSrcIdList) {
long minScn = computeMinScn(minScnType, sInfo.getSrcId());
if (minScn != Long.MIN_VALUE) {
_dbao.updateMinScnOfSnapshot(sInfo.getSrcId(), minScn);
}
DBHelper.commit(_bsConn.getDBConn());
}
}
}
} catch (ParseException e) {
usage(options, functionNames);
exitStatus = 1;
} catch (Exception e) {
System.err.println("Error: " + e);
exitStatus = 1;
} finally {
if (_dbao != null)
_dbao.close();
System.exit(exitStatus);
}
}
use of org.apache.commons.cli.CommandLine in project databus by linkedin.
the class BootstrapTableReader method parseArgs.
@SuppressWarnings("static-access")
public static void parseArgs(String[] args) throws IOException {
CommandLineParser cliParser = new GnuParser();
Option helpOption = OptionBuilder.withLongOpt(HELP_OPT_LONG_NAME).withDescription("Help screen").create(HELP_OPT_CHAR);
Option sourcesOption = OptionBuilder.withLongOpt(QUERY_CONFIG_OPT_LONG_NAME).withDescription("Query Config").hasArg().withArgName("property_file").create(QUERY_CONFIG_OPT_CHAR);
Option dbOption = OptionBuilder.withLongOpt(BOOTSTRAP_DB_PROPS_OPT_LONG_NAME).withDescription("Bootstrap DB properties to use").hasArg().withArgName("property_file").create(BOOTSTRAP_DB_PROP_OPT_CHAR);
Option log4jPropsOption = OptionBuilder.withLongOpt(LOG4J_PROPS_OPT_LONG_NAME).withDescription("Log4j properties to use").hasArg().withArgName("property_file").create(LOG4J_PROPS_OPT_CHAR);
Options options = new Options();
options.addOption(helpOption);
options.addOption(sourcesOption);
options.addOption(dbOption);
options.addOption(log4jPropsOption);
CommandLine cmd = null;
try {
cmd = cliParser.parse(options, args);
} catch (ParseException pe) {
LOG.fatal("Bootstrap Physical Config: failed to parse command-line options.", pe);
throw new RuntimeException("Bootstrap Physical Config: failed to parse command-line options.", pe);
}
if (cmd.hasOption(LOG4J_PROPS_OPT_CHAR)) {
String log4jPropFile = cmd.getOptionValue(LOG4J_PROPS_OPT_CHAR);
PropertyConfigurator.configure(log4jPropFile);
LOG.info("Using custom logging settings from file " + log4jPropFile);
} else {
PatternLayout defaultLayout = new PatternLayout("%d{ISO8601} +%r [%t] (%p) {%c} %m%n");
ConsoleAppender defaultAppender = new ConsoleAppender(defaultLayout);
Logger.getRootLogger().removeAllAppenders();
Logger.getRootLogger().addAppender(defaultAppender);
LOG.info("Using default logging settings");
}
if (cmd.hasOption(HELP_OPT_CHAR)) {
printCliHelp(options);
System.exit(0);
}
if (!cmd.hasOption(QUERY_CONFIG_OPT_CHAR)) {
throw new RuntimeException("Query Config is not provided; use --help for usage");
}
if (!cmd.hasOption(BOOTSTRAP_DB_PROP_OPT_CHAR)) {
throw new RuntimeException("Bootstrap config is not provided; use --help for usage");
}
String propFile1 = cmd.getOptionValue(QUERY_CONFIG_OPT_CHAR);
String propFile2 = cmd.getOptionValue(BOOTSTRAP_DB_PROP_OPT_CHAR);
LOG.info("Loading bootstrap DB config from properties file " + propFile2);
_sQueryConfigProps = new Properties();
FileInputStream f1 = new FileInputStream(propFile1);
try {
_sQueryConfigProps.load(f1);
} finally {
f1.close();
}
_sBootstrapConfigProps = new Properties();
FileInputStream f2 = new FileInputStream(propFile2);
try {
_sBootstrapConfigProps.load(f2);
} finally {
f2.close();
}
}
use of org.apache.commons.cli.CommandLine in project databus by linkedin.
the class ClusterFileLoggingClient method processLocalArgs.
protected static String[] processLocalArgs(String[] cliArgs) throws IOException, ParseException {
CommandLineParser cliParser = new GnuParser();
Options cliOptions = constructCommandLineOptions();
CommandLine cmd = cliParser.parse(cliOptions, cliArgs, true);
// Options here has to be up front
if (cmd.hasOption(CLUSTER_NAME_OPT_NAME)) {
String cluster = cmd.getOptionValue(CLUSTER_NAME_OPT_NAME);
String[] clusters = cluster.split(",");
_clusters = Arrays.asList(clusters);
LOG.info("Cluster Name = " + _clusters);
}
if (cmd.hasOption(RELAY_HOST_OPT_NAME)) {
_relayHost = cmd.getOptionValue(RELAY_HOST_OPT_NAME);
LOG.info("Relay Host = " + _relayHost);
}
if (cmd.hasOption(RELAY_PORT_OPT_NAME)) {
_relayPort = cmd.getOptionValue(RELAY_PORT_OPT_NAME);
LOG.info("Relay Port = " + _relayPort);
}
if (cmd.hasOption(EVENT_DUMP_FILE_OPT_NAME)) {
_eventDumpFile = cmd.getOptionValue(EVENT_DUMP_FILE_OPT_NAME);
LOG.info("Saving event dump to file: " + _eventDumpFile);
}
if (cmd.hasOption(VALUE_DUMP_FILE_OPT_NAME)) {
_valueDumpFile = cmd.getOptionValue(VALUE_DUMP_FILE_OPT_NAME);
LOG.info("Saving event value dump to file: " + _valueDumpFile);
}
if (cmd.hasOption(HTTP_PORT_OPT_NAME)) {
_httpPort = cmd.getOptionValue(HTTP_PORT_OPT_NAME);
LOG.info("Consumer http port = " + _httpPort);
}
if (cmd.hasOption(JMX_SERVICE_PORT_OPT_NAME)) {
_jmxServicePort = cmd.getOptionValue(JMX_SERVICE_PORT_OPT_NAME);
LOG.info("Consumer JMX Service port = " + _jmxServicePort);
}
if (cmd.hasOption(BOOTSTRAP_HOST_OPT_NAME)) {
_bootstrapHost = cmd.getOptionValue(BOOTSTRAP_HOST_OPT_NAME);
LOG.info("Bootstrap Server = " + _bootstrapHost);
}
if (cmd.hasOption(BOOTSTRAP_PORT_OPT_NAME)) {
_bootstrapPort = cmd.getOptionValue(BOOTSTRAP_PORT_OPT_NAME);
LOG.info("Bootstrap Server Port = " + _bootstrapPort);
}
if (cmd.hasOption(EVENT_PATTERN_OPT_NAME)) {
_eventPattern = cmd.getOptionValue(EVENT_PATTERN_OPT_NAME);
LOG.info("Event pattern = " + _eventPattern);
}
if (_bootstrapHost != null || _bootstrapPort != null) {
_enableBootStrap = true;
}
// return what left over args
return cmd.getArgs();
}
use of org.apache.commons.cli.CommandLine in project databus by linkedin.
the class CheckpointSerializerMain method parseArgs.
private static void parseArgs(String[] args) throws Exception {
CommandLineParser cliParser = new GnuParser();
Options options = createOptions();
CommandLine cmd = null;
try {
cmd = cliParser.parse(options, args);
} catch (ParseException pe) {
throw new RuntimeException("failed to parse command-line options.", pe);
}
if (cmd.hasOption(HELP_OPT_CHAR) || 0 == cmd.getOptions().length) {
printCliHelp(options);
System.exit(0);
}
try {
_action = Action.valueOf(cmd.getOptionValue(ACTION_OPT_CHAR).toUpperCase());
} catch (Exception e) {
throw new RuntimeException("invalid action: " + cmd.getOptionValue(ACTION_OPT_CHAR), e);
}
if (!cmd.hasOption(SOURCES_OPT_CHAR)) {
throw new RuntimeException("expected sources list; see --help for more info");
}
String sourcesListStr = cmd.getOptionValue(SOURCES_OPT_CHAR);
_sources = sourcesListStr.split(",");
if (null == _sources || 0 == _sources.length) {
throw new RuntimeException("empty sources list");
}
for (int i = 0; i < _sources.length; ++i) _sources[i] = _sources[i].trim();
if (Action.PRINT != _action && !cmd.hasOption(CLIENT_PROPS_FILE_OPT_CHAR) && !cmd.hasOption(CP3_PROPS_FILE_OPT_CHAR)) {
throw new RuntimeException("expected client or CP3 configuration; see --help for more info");
}
String defaultPropPrefix = null;
if (cmd.hasOption(CLIENT_PROPS_FILE_OPT_CHAR)) {
try {
_clientProps = loadProperties(cmd.getOptionValue(CLIENT_PROPS_FILE_OPT_CHAR));
defaultPropPrefix = "databus2.client";
} catch (Exception e) {
throw new RuntimeException("unable to load client properties", e);
}
} else if (cmd.hasOption(CP3_PROPS_FILE_OPT_CHAR)) {
try {
_cp3Props = loadProperties(cmd.getOptionValue(CP3_PROPS_FILE_OPT_CHAR));
defaultPropPrefix = "databus2.client.checkpointPersistence";
} catch (Exception e) {
throw new RuntimeException("unable to load CP3 properties", e);
}
}
_propPrefix = cmd.hasOption(PROPS_PREFIX_OPT_CHAR) ? cmd.getOptionValue(PROPS_PREFIX_OPT_CHAR) : defaultPropPrefix;
if (null != _propPrefix && !_propPrefix.endsWith(".")) {
_propPrefix = _propPrefix + ".";
}
if (!cmd.hasOption(ACTION_OPT_CHAR)) {
throw new RuntimeException("action expected; see --help for more info");
}
_scn = parseLongOption(cmd, SCN_OPT_CHAR, "sequence number");
_sinceScn = parseLongOption(cmd, SINCE_SCN_OPT_CHAR, "last sequence number");
_startScn = parseLongOption(cmd, START_SCN_OPT_CHAR, "start sequence number");
_targetScn = parseLongOption(cmd, TARGET_SCN_OPT_CHAR, "target sequence number");
if (cmd.hasOption(TYPE_OPT_CHAR)) {
try {
_cpType = DbusClientMode.valueOf(cmd.getOptionValue(TYPE_OPT_CHAR).toUpperCase());
} catch (Exception e) {
throw new RuntimeException("invalid checkpoint type:" + cmd.getOptionValue(TYPE_OPT_CHAR), e);
}
}
if (cmd.hasOption(BOOTSTRAP_SOURCE_OPT_CHAR)) {
_bootstrapSource = cmd.getOptionValue(BOOTSTRAP_SOURCE_OPT_CHAR);
}
}
Aggregations