Search in sources :

Example 16 with OptionParser

use of joptsimple.OptionParser in project jackrabbit-oak by apache.

the class SegmentPropertyIndexEditorProvider method createSegmentOptions.

private static Options createSegmentOptions(File storePath) throws IOException {
    OptionParser parser = new OptionParser();
    Options opts = new Options().withDisableSystemExit();
    opts.parseAndConfigure(parser, new String[] { storePath.getAbsolutePath() });
    return opts;
}
Also used : Options(org.apache.jackrabbit.oak.run.cli.Options) OptionParser(joptsimple.OptionParser)

Example 17 with OptionParser

use of joptsimple.OptionParser in project jackrabbit-oak by apache.

the class UnlockUpgradeCommand method execute.

@Override
public void execute(String... args) throws Exception {
    OptionParser parser = new OptionParser();
    // RDB specific options
    OptionSpec<String> rdbjdbcuser = parser.accepts("rdbjdbcuser", "RDB JDBC user").withOptionalArg().defaultsTo("");
    OptionSpec<String> rdbjdbcpasswd = parser.accepts("rdbjdbcpasswd", "RDB JDBC password").withOptionalArg().defaultsTo("");
    OptionSpec<String> nonOption = parser.nonOptions("unlockUpgrade {<jdbc-uri> | <mongodb-uri>}");
    OptionSpec help = parser.acceptsAll(asList("h", "?", "help"), "show help").forHelp();
    OptionSet options = parser.parse(args);
    List<String> nonOptions = nonOption.values(options);
    if (options.has(help)) {
        parser.printHelpOn(System.out);
        return;
    }
    if (nonOptions.isEmpty()) {
        parser.printHelpOn(System.err);
        return;
    }
    DocumentStore store = null;
    try {
        String uri = nonOptions.get(0);
        if (uri.startsWith(MONGODB_PREFIX)) {
            MongoClientURI clientURI = new MongoClientURI(uri);
            if (clientURI.getDatabase() == null) {
                System.err.println("Database missing in MongoDB URI: " + clientURI.getURI());
            } else {
                MongoConnection mongo = new MongoConnection(clientURI.getURI());
                store = new MongoDocumentStore(mongo.getMongoClient(), mongo.getDBName(), new MongoDocumentNodeStoreBuilder());
            }
        } else if (uri.startsWith("jdbc")) {
            DataSource ds = RDBDataSourceFactory.forJdbcUrl(uri, rdbjdbcuser.value(options), rdbjdbcpasswd.value(options));
            store = new RDBDocumentStore(ds, new DocumentNodeStoreBuilder());
        } else {
            System.err.println("Unrecognized URI: " + uri);
        }
        if (store != null && VERSION.writeTo(store)) {
            System.out.println("Format version set to " + VERSION);
        }
    } catch (DocumentStoreException e) {
        System.err.println(e.getMessage());
    } finally {
        if (store != null) {
            store.dispose();
        }
    }
}
Also used : OptionSpec(joptsimple.OptionSpec) DocumentStoreException(org.apache.jackrabbit.oak.plugins.document.DocumentStoreException) MongoClientURI(com.mongodb.MongoClientURI) OptionParser(joptsimple.OptionParser) MongoDocumentStore(org.apache.jackrabbit.oak.plugins.document.mongo.MongoDocumentStore) DocumentNodeStoreBuilder(org.apache.jackrabbit.oak.plugins.document.DocumentNodeStoreBuilder) MongoDocumentNodeStoreBuilder(org.apache.jackrabbit.oak.plugins.document.mongo.MongoDocumentNodeStoreBuilder) DataSource(javax.sql.DataSource) MongoDocumentNodeStoreBuilder(org.apache.jackrabbit.oak.plugins.document.mongo.MongoDocumentNodeStoreBuilder) DocumentStore(org.apache.jackrabbit.oak.plugins.document.DocumentStore) MongoDocumentStore(org.apache.jackrabbit.oak.plugins.document.mongo.MongoDocumentStore) RDBDocumentStore(org.apache.jackrabbit.oak.plugins.document.rdb.RDBDocumentStore) RDBDocumentStore(org.apache.jackrabbit.oak.plugins.document.rdb.RDBDocumentStore) OptionSet(joptsimple.OptionSet) MongoConnection(org.apache.jackrabbit.oak.plugins.document.util.MongoConnection)

Example 18 with OptionParser

use of joptsimple.OptionParser in project jackrabbit-oak by apache.

the class Utils method bootstrapDataStore.

@Nullable
public static GarbageCollectableBlobStore bootstrapDataStore(String[] args, Closer closer) throws IOException, RepositoryException {
    OptionParser parser = new OptionParser();
    parser.allowsUnrecognizedOptions();
    ArgumentAcceptingOptionSpec<String> s3dsConfig = parser.accepts("s3ds", "S3DataStore config").withRequiredArg().ofType(String.class);
    ArgumentAcceptingOptionSpec<String> fdsConfig = parser.accepts("fds", "FileDataStore config").withRequiredArg().ofType(String.class);
    ArgumentAcceptingOptionSpec<String> azureBlobDSConfig = parser.accepts("azureblobds", "AzureBlobStorageDataStore config").withRequiredArg().ofType(String.class);
    OptionSpecBuilder nods = parser.accepts("nods", "No DataStore ");
    OptionSet options = parser.parse(args);
    if (!options.has(s3dsConfig) && !options.has(fdsConfig) && !options.has(azureBlobDSConfig) && !options.has(nods)) {
        return null;
    }
    DataStore delegate;
    if (options.has(s3dsConfig)) {
        S3DataStore s3ds = new S3DataStore();
        String cfgPath = s3dsConfig.value(options);
        Properties props = loadAndTransformProps(cfgPath);
        s3ds.setProperties(props);
        File homeDir = Files.createTempDir();
        closer.register(asCloseable(homeDir));
        s3ds.init(homeDir.getAbsolutePath());
        delegate = s3ds;
    } else if (options.has(azureBlobDSConfig)) {
        AzureDataStore azureds = new AzureDataStore();
        String cfgPath = azureBlobDSConfig.value(options);
        Properties props = loadAndTransformProps(cfgPath);
        azureds.setProperties(props);
        File homeDir = Files.createTempDir();
        azureds.init(homeDir.getAbsolutePath());
        closer.register(asCloseable(homeDir));
        delegate = azureds;
    } else if (options.has(nods)) {
        delegate = new DummyDataStore();
        File homeDir = Files.createTempDir();
        delegate.init(homeDir.getAbsolutePath());
        closer.register(asCloseable(homeDir));
    } else {
        delegate = new OakFileDataStore();
        String cfgPath = fdsConfig.value(options);
        Properties props = loadAndTransformProps(cfgPath);
        populate(delegate, asMap(props), true);
        delegate.init(null);
    }
    DataStoreBlobStore blobStore = new DataStoreBlobStore(delegate);
    closer.register(Utils.asCloseable(blobStore));
    return blobStore;
}
Also used : OakFileDataStore(org.apache.jackrabbit.oak.plugins.blob.datastore.OakFileDataStore) DummyDataStore(org.apache.jackrabbit.oak.run.cli.DummyDataStore) AzureDataStore(org.apache.jackrabbit.oak.blob.cloud.azure.blobstorage.AzureDataStore) Properties(java.util.Properties) OptionParser(joptsimple.OptionParser) S3DataStore(org.apache.jackrabbit.oak.blob.cloud.s3.S3DataStore) OptionSpecBuilder(joptsimple.OptionSpecBuilder) S3DataStore(org.apache.jackrabbit.oak.blob.cloud.s3.S3DataStore) DummyDataStore(org.apache.jackrabbit.oak.run.cli.DummyDataStore) DataStore(org.apache.jackrabbit.core.data.DataStore) AzureDataStore(org.apache.jackrabbit.oak.blob.cloud.azure.blobstorage.AzureDataStore) OakFileDataStore(org.apache.jackrabbit.oak.plugins.blob.datastore.OakFileDataStore) OptionSet(joptsimple.OptionSet) File(java.io.File) DataStoreBlobStore(org.apache.jackrabbit.oak.plugins.blob.datastore.DataStoreBlobStore) Nullable(javax.annotation.Nullable)

Example 19 with OptionParser

use of joptsimple.OptionParser in project jackrabbit-oak by apache.

the class ServerCommand method execute.

@Override
public void execute(String... args) throws Exception {
    OptionParser parser = new OptionParser();
    OptionSpec<Integer> cache = parser.accepts("cache", "cache size (MB)").withRequiredArg().ofType(Integer.class).defaultsTo(100);
    // tar/h2 specific option
    OptionSpec<File> base = parser.accepts("base", "Base directory").withRequiredArg().ofType(File.class);
    OptionSpec<Boolean> mmap = parser.accepts("mmap", "TarMK memory mapping").withOptionalArg().ofType(Boolean.class).defaultsTo("64".equals(System.getProperty("sun.arch.data.model")));
    // mongo specific options:
    OptionSpec<String> host = parser.accepts("host", "MongoDB host").withRequiredArg().defaultsTo("127.0.0.1");
    OptionSpec<Integer> port = parser.accepts("port", "MongoDB port").withRequiredArg().ofType(Integer.class).defaultsTo(27017);
    OptionSpec<String> dbName = parser.accepts("db", "MongoDB database").withRequiredArg();
    OptionSpec<Integer> clusterIds = parser.accepts("clusterIds", "Cluster Ids").withOptionalArg().ofType(Integer.class).withValuesSeparatedBy(',');
    // RDB specific options
    OptionSpec<String> rdbjdbcuri = parser.accepts("rdbjdbcuri", "RDB JDBC URI").withOptionalArg().defaultsTo("");
    OptionSpec<String> rdbjdbcuser = parser.accepts("rdbjdbcuser", "RDB JDBC user").withOptionalArg().defaultsTo("");
    OptionSpec<String> rdbjdbcpasswd = parser.accepts("rdbjdbcpasswd", "RDB JDBC password").withOptionalArg().defaultsTo("");
    OptionSpec<String> rdbjdbctableprefix = parser.accepts("rdbjdbctableprefix", "RDB JDBC table prefix").withOptionalArg().defaultsTo("");
    OptionSpec<String> nonOption = parser.nonOptions();
    OptionSpec<?> help = parser.acceptsAll(asList("h", "?", "help"), "show help").forHelp();
    OptionSet options = parser.parse(args);
    if (options.has(help)) {
        parser.printHelpOn(System.out);
        System.exit(0);
    }
    OakFixture oakFixture;
    List<String> arglist = nonOption.values(options);
    String uri = (arglist.isEmpty()) ? DEFAULT_URI : arglist.get(0);
    String fix = (arglist.size() <= 1) ? OakFixture.OAK_MEMORY : arglist.get(1);
    int cacheSize = cache.value(options);
    List<Integer> cIds = Collections.emptyList();
    if (fix.startsWith(OakFixture.OAK_MEMORY)) {
        if (OakFixture.OAK_MEMORY_NS.equals(fix)) {
            oakFixture = OakFixture.getMemoryNS(cacheSize * MB);
        } else {
            oakFixture = OakFixture.getMemory(cacheSize * MB);
        }
    } else if (fix.startsWith(OakFixture.OAK_MONGO)) {
        cIds = clusterIds.values(options);
        String db = dbName.value(options);
        if (db == null) {
            throw new IllegalArgumentException("Required argument db missing");
        }
        if (OakFixture.OAK_MONGO_NS.equals(fix)) {
            oakFixture = OakFixture.getMongoNS(host.value(options), port.value(options), db, false, cacheSize * MB);
        } else {
            oakFixture = OakFixture.getMongo(host.value(options), port.value(options), db, false, cacheSize * MB);
        }
    } else if (fix.equals(OakFixture.OAK_SEGMENT_TAR)) {
        File baseFile = base.value(options);
        if (baseFile == null) {
            throw new IllegalArgumentException("Required argument base missing.");
        }
        oakFixture = OakFixture.getVanillaSegmentTar(baseFile, 256, cacheSize, mmap.value(options));
    } else if (fix.equals(OakFixture.OAK_RDB)) {
        oakFixture = OakFixture.getRDB(OakFixture.OAK_RDB, rdbjdbcuri.value(options), rdbjdbcuser.value(options), rdbjdbcpasswd.value(options), rdbjdbctableprefix.value(options), false, cacheSize, -1);
    } else {
        throw new IllegalArgumentException("Unsupported repository setup " + fix);
    }
    startOakServer(oakFixture, uri, cIds);
}
Also used : OptionParser(joptsimple.OptionParser) OakFixture(org.apache.jackrabbit.oak.fixture.OakFixture) OptionSet(joptsimple.OptionSet) File(java.io.File)

Example 20 with OptionParser

use of joptsimple.OptionParser in project jackrabbit-oak by apache.

the class ThreadDumpCommand method execute.

@SuppressWarnings("unchecked")
@Override
public void execute(String... args) throws Exception {
    OptionParser parser = new OptionParser();
    OptionSpec<Void> convertSpec = parser.accepts("convert", "convert the thread dumps to the standard format");
    OptionSpec<Void> filterSpec = parser.accepts("filter", "filter the thread dumps, only keep working (running), interesting threads " + "(for example, threads that read from sockets are ignored, " + "as they are typically waiting for input; " + "system threads such as GC are also ignored)");
    OptionSpec<Void> threadNamesSpec = parser.accepts("threadNames", "create a summary of thread names");
    OptionSpec<Void> profileSpec = parser.accepts("profile", "profile the thread dumps");
    OptionSpec<Void> profileClassesSpec = parser.accepts("profileClasses", "profile classes");
    OptionSpec<Void> profileMethodsSpec = parser.accepts("profileMethods", "profile methods");
    OptionSpec<Void> profilePackagesSpec = parser.accepts("profilePackages", "profile packages");
    OptionSpec<?> helpSpec = parser.acceptsAll(asList("h", "?", "help"), "show help").forHelp();
    OptionSet options = parser.parse(args);
    parser.nonOptions("file or directory containing thread dumps " + "(ensure it does not contain other files, such as binaries)").ofType(File.class);
    if (options.has(helpSpec) || options.nonOptionArguments().isEmpty()) {
        System.out.println("Mode: " + THREADDUMP);
        System.out.println();
        parser.printHelpOn(System.out);
        return;
    }
    boolean convert = options.has(convertSpec);
    boolean filter = options.has(filterSpec);
    boolean threadNames = options.has(threadNamesSpec);
    boolean profile = options.has(profileSpec);
    boolean profileClasses = options.has(profileClassesSpec);
    boolean profileMethods = options.has(profileMethodsSpec);
    boolean profilePackages = options.has(profilePackagesSpec);
    for (String fileName : ((List<String>) options.nonOptionArguments())) {
        File file = new File(fileName);
        if (file.isDirectory() || file.getName().endsWith(".gz")) {
            file = combineAndExpandFiles(file);
            System.out.println("Combined into " + file.getAbsolutePath());
        }
        if (convert) {
            file = ThreadDumpConverter.process(file);
            System.out.println("Converted to " + file.getAbsolutePath());
        }
        if (threadNames) {
            File f = ThreadDumpThreadNames.process(file);
            System.out.println("Thread names written to " + f.getAbsolutePath());
        }
        if (filter) {
            file = ThreadDumpCleaner.process(file);
            System.out.println("Filtered into " + file.getAbsolutePath());
        }
        if (threadNames) {
            File f = ThreadDumpThreadNames.process(file);
            System.out.println("Thread names written to " + f.getAbsolutePath());
        }
        if (profile) {
            ArrayList<String> list = new ArrayList<String>();
            if (profileClasses) {
                list.add("-classes");
            }
            if (profileMethods) {
                list.add("-methods");
            }
            if (profilePackages) {
                list.add("-packages");
            }
            list.add(file.getAbsolutePath());
            Profiler.main(list.toArray(new String[0]));
        }
    }
}
Also used : ArrayList(java.util.ArrayList) OptionSet(joptsimple.OptionSet) OptionParser(joptsimple.OptionParser) File(java.io.File)

Aggregations

OptionParser (joptsimple.OptionParser)199 OptionSet (joptsimple.OptionSet)151 File (java.io.File)58 IOException (java.io.IOException)29 Test (org.junit.Test)28 OptionException (joptsimple.OptionException)25 ArrayList (java.util.ArrayList)21 Properties (java.util.Properties)16 List (java.util.List)15 OptionSpec (joptsimple.OptionSpec)14 Test (org.junit.jupiter.api.Test)12 VerifiableProperties (com.github.ambry.config.VerifiableProperties)11 FileNotFoundException (java.io.FileNotFoundException)9 BufferedReader (java.io.BufferedReader)8 ArgumentAcceptingOptionSpec (joptsimple.ArgumentAcceptingOptionSpec)8 FileReader (java.io.FileReader)7 OptionSpecBuilder (joptsimple.OptionSpecBuilder)7 Closer (com.google.common.io.Closer)6 Cluster (voldemort.cluster.Cluster)6 StoreDefinition (voldemort.store.StoreDefinition)6