Search in sources :

Example 71 with OptionSet

use of joptsimple.OptionSet in project whirr by apache.

the class ClusterSpecCommandTest method testOverrides.

@Test
public void testOverrides() throws Exception {
    ClusterSpecCommand clusterSpecCommand = new ClusterSpecCommand("name", "description", new ServiceFactory()) {

        @Override
        public int run(InputStream in, PrintStream out, PrintStream err, List<String> args) throws Exception {
            return 0;
        }
    };
    OptionSet optionSet = clusterSpecCommand.parser.parse("--service-name", "overridden-test-service", "--config", "whirr-override-test.properties");
    ClusterSpec clusterSpec = clusterSpecCommand.getClusterSpec(optionSet);
    assertThat(clusterSpec.getServiceName(), is("overridden-test-service"));
    assertThat(clusterSpec.getClusterName(), is("test-cluster"));
}
Also used : PrintStream(java.io.PrintStream) ServiceFactory(org.apache.whirr.service.ServiceFactory) InputStream(java.io.InputStream) List(java.util.List) ClusterSpec(org.apache.whirr.service.ClusterSpec) OptionSet(joptsimple.OptionSet) Test(org.junit.Test)

Example 72 with OptionSet

use of joptsimple.OptionSet in project whirr by apache.

the class DestroyClusterCommand method run.

@Override
public int run(InputStream in, PrintStream out, PrintStream err, List<String> args) throws Exception {
    OptionSet optionSet = parser.parse(args.toArray(new String[0]));
    if (!optionSet.nonOptionArguments().isEmpty()) {
        printUsage(parser, err);
        return -1;
    }
    try {
        ClusterSpec clusterSpec = getClusterSpec(optionSet);
        Service service = factory.create(clusterSpec.getServiceName());
        service.destroyCluster(clusterSpec);
        return 0;
    } catch (IllegalArgumentException e) {
        err.println(e.getMessage());
        printUsage(parser, err);
        return -1;
    }
}
Also used : Service(org.apache.whirr.service.Service) ClusterSpec(org.apache.whirr.service.ClusterSpec) OptionSet(joptsimple.OptionSet)

Example 73 with OptionSet

use of joptsimple.OptionSet in project elasticsearch by elastic.

the class EvilCommandTests method testCommandShutdownHook.

public void testCommandShutdownHook() throws Exception {
    final AtomicBoolean closed = new AtomicBoolean();
    final boolean shouldThrow = randomBoolean();
    final Command command = new Command("test-command-shutdown-hook") {

        @Override
        protected void execute(Terminal terminal, OptionSet options) throws Exception {
        }

        @Override
        public void close() throws IOException {
            closed.set(true);
            if (shouldThrow) {
                throw new IOException("fail");
            }
        }
    };
    final MockTerminal terminal = new MockTerminal();
    command.main(new String[0], terminal);
    assertNotNull(command.shutdownHookThread.get());
    // successful removal here asserts that the runtime hook was installed in Command#main
    assertTrue(Runtime.getRuntime().removeShutdownHook(command.shutdownHookThread.get()));
    command.shutdownHookThread.get().run();
    command.shutdownHookThread.get().join();
    assertTrue(closed.get());
    final String output = terminal.getOutput();
    if (shouldThrow) {
        // ensure that we dump the exception
        assertThat(output, containsString("java.io.IOException: fail"));
        // ensure that we dump the stack trace too
        assertThat(output, containsString("\tat org.elasticsearch.cli.EvilCommandTests$1.close"));
    } else {
        assertThat(output, isEmptyString());
    }
}
Also used : AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) IOException(java.io.IOException) CoreMatchers.containsString(org.hamcrest.CoreMatchers.containsString) Matchers.isEmptyString(org.hamcrest.Matchers.isEmptyString) OptionSet(joptsimple.OptionSet)

Example 74 with OptionSet

use of joptsimple.OptionSet in project elasticsearch by elastic.

the class TruncateTranslogIT method testCorruptTranslogTruncation.

public void testCorruptTranslogTruncation() throws Exception {
    internalCluster().startNodes(1, Settings.EMPTY);
    assertAcked(prepareCreate("test").setSettings(Settings.builder().put("index.number_of_shards", 1).put("index.number_of_replicas", 0).put("index.refresh_interval", "-1").put(MockEngineSupport.DISABLE_FLUSH_ON_CLOSE.getKey(), // never flush - always recover from translog
    true)));
    ensureYellow();
    // Index some documents
    int numDocs = scaledRandomIntBetween(100, 1000);
    IndexRequestBuilder[] builders = new IndexRequestBuilder[numDocs];
    for (int i = 0; i < builders.length; i++) {
        builders[i] = client().prepareIndex("test", "type").setSource("foo", "bar");
    }
    disableTranslogFlush("test");
    indexRandom(false, false, false, Arrays.asList(builders));
    Set<Path> translogDirs = getTranslogDirs("test");
    TruncateTranslogCommand ttc = new TruncateTranslogCommand();
    MockTerminal t = new MockTerminal();
    OptionParser parser = ttc.getParser();
    for (Path translogDir : translogDirs) {
        OptionSet options = parser.parse("-d", translogDir.toAbsolutePath().toString(), "-b");
        // Try running it before the shard is closed, it should flip out because it can't acquire the lock
        try {
            logger.info("--> running truncate while index is open on [{}]", translogDir.toAbsolutePath());
            ttc.execute(t, options, null);
            fail("expected the truncate command to fail not being able to acquire the lock");
        } catch (Exception e) {
            assertThat(e.getMessage(), containsString("Failed to lock shard's directory"));
        }
    }
    // Corrupt the translog file(s)
    logger.info("--> corrupting translog");
    corruptRandomTranslogFiles("test");
    // Restart the single node
    logger.info("--> restarting node");
    internalCluster().fullRestart();
    client().admin().cluster().prepareHealth().setWaitForYellowStatus().setTimeout(new TimeValue(1000, TimeUnit.MILLISECONDS)).setWaitForEvents(Priority.LANGUID).get();
    try {
        client().prepareSearch("test").setQuery(matchAllQuery()).get();
        fail("all shards should be failed due to a corrupted translog");
    } catch (SearchPhaseExecutionException e) {
    // Good, all shards should be failed because there is only a
    // single shard and its translog is corrupt
    }
    // Close the index so we can actually truncate the translog
    logger.info("--> closing 'test' index");
    client().admin().indices().prepareClose("test").get();
    for (Path translogDir : translogDirs) {
        final Path idxLocation = translogDir.getParent().resolve("index");
        assertBusy(() -> {
            logger.info("--> checking that lock has been released for {}", idxLocation);
            try (Directory dir = FSDirectory.open(idxLocation, NativeFSLockFactory.INSTANCE);
                Lock writeLock = dir.obtainLock(IndexWriter.WRITE_LOCK_NAME)) {
            // Great, do nothing, we just wanted to obtain the lock
            } catch (LockObtainFailedException lofe) {
                logger.info("--> failed acquiring lock for {}", idxLocation);
                fail("still waiting for lock release at [" + idxLocation + "]");
            } catch (IOException ioe) {
                fail("Got an IOException: " + ioe);
            }
        });
        OptionSet options = parser.parse("-d", translogDir.toAbsolutePath().toString(), "-b");
        logger.info("--> running truncate translog command for [{}]", translogDir.toAbsolutePath());
        ttc.execute(t, options, null);
        logger.info("--> output:\n{}", t.getOutput());
    }
    // Re-open index
    logger.info("--> opening 'test' index");
    client().admin().indices().prepareOpen("test").get();
    ensureYellow("test");
    // Run a search and make sure it succeeds
    SearchResponse resp = client().prepareSearch("test").setQuery(matchAllQuery()).get();
    ElasticsearchAssertions.assertNoFailures(resp);
}
Also used : Path(java.nio.file.Path) SearchPhaseExecutionException(org.elasticsearch.action.search.SearchPhaseExecutionException) IOException(java.io.IOException) MockTerminal(org.elasticsearch.cli.MockTerminal) OptionParser(joptsimple.OptionParser) SearchPhaseExecutionException(org.elasticsearch.action.search.SearchPhaseExecutionException) LockObtainFailedException(org.apache.lucene.store.LockObtainFailedException) IOException(java.io.IOException) Lock(org.apache.lucene.store.Lock) SearchResponse(org.elasticsearch.action.search.SearchResponse) IndexRequestBuilder(org.elasticsearch.action.index.IndexRequestBuilder) LockObtainFailedException(org.apache.lucene.store.LockObtainFailedException) OptionSet(joptsimple.OptionSet) TimeValue(org.elasticsearch.common.unit.TimeValue) Directory(org.apache.lucene.store.Directory) FSDirectory(org.apache.lucene.store.FSDirectory)

Example 75 with OptionSet

use of joptsimple.OptionSet in project PayFile by mikehearn.

the class Main method main.

public static void main(String[] args) throws Exception {
    // allow client to choose another network for testing by passing through an argument.
    OptionParser parser = new OptionParser();
    parser.accepts("network").withRequiredArg().withValuesConvertedBy(regex("(mainnet)|(testnet)|(regtest)")).defaultsTo("mainnet");
    parser.accepts("help").forHelp();
    parser.formatHelpWith(new BuiltinHelpFormatter(120, 10));
    OptionSet options;
    try {
        options = parser.parse(args);
    } catch (OptionException e) {
        System.err.println(e.getMessage());
        System.err.println("");
        parser.printHelpOn(System.err);
        return;
    }
    if (options.has("help")) {
        parser.printHelpOn(System.out);
        return;
    }
    if (options.valueOf("network").equals(("testnet"))) {
        params = TestNet3Params.get();
        filePrefix = "testnet-";
    } else if (options.valueOf("network").equals(("mainnet"))) {
        params = MainNetParams.get();
        filePrefix = "";
    } else if (options.valueOf("network").equals(("regtest"))) {
        params = RegTestParams.get();
        filePrefix = "regtest-";
    }
    launch(args);
}
Also used : BuiltinHelpFormatter(joptsimple.BuiltinHelpFormatter) OptionException(joptsimple.OptionException) OptionSet(joptsimple.OptionSet) OptionParser(joptsimple.OptionParser)

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