Github user mikewalch commented on a diff in the pull request: https://github.com/apache/accumulo-testing/pull/1#discussion_r94629394 --- Diff: twill/src/main/java/org/apache/accumulo/testing/twill/TestRunner.java --- @@ -0,0 +1,166 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.accumulo.testing.twill; + +import com.beust.jcommander.JCommander; +import com.beust.jcommander.Parameter; +import com.google.common.base.Preconditions; +import org.apache.accumulo.testing.core.TestProps; +import org.apache.hadoop.yarn.conf.YarnConfiguration; +import org.apache.twill.api.ResourceSpecification; +import org.apache.twill.api.TwillApplication; +import org.apache.twill.api.TwillController; +import org.apache.twill.api.TwillRunnerService; +import org.apache.twill.api.TwillSpecification; +import org.apache.twill.ext.BundledJarRunnable; +import org.apache.twill.ext.BundledJarRunner; +import org.apache.twill.yarn.YarnTwillRunnerService; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import java.io.File; +import java.io.FileInputStream; +import java.util.ArrayList; +import java.util.List; +import java.util.Properties; +import java.util.concurrent.ExecutionException; +import java.util.concurrent.atomic.AtomicBoolean; + +public class TestRunner { + + private static final Logger LOG = LoggerFactory.getLogger(TestRunner.class); + + private static class TestRunnerApp implements TwillApplication { + + private TestRunnerOpts opts; + private Properties props; + + TestRunnerApp(TestRunnerOpts opts, Properties props) { + this.opts = opts; + this.props = props; + } + + @Override + public TwillSpecification configure() { + + int numCores = Integer.valueOf(props.getProperty(TestProps.CONTAINER_CORES)); + int memory = Integer.valueOf(props.getProperty(TestProps.CONTAINER_MEMORY_MB)); + int instances = Integer.valueOf(props.getProperty(TestProps.CONTAINER_INSTANCES)); + + ResourceSpecification resourceSpec = ResourceSpecification.Builder.with() + .setVirtualCores(numCores).setMemory(memory, ResourceSpecification.SizeUnit.MEGA) + .setInstances(instances).build(); + + File jarFile = new File(opts.jarPath); + File testProps = new File(opts.testProps); + File log4jProps = new File(opts.logProps); + + return TwillSpecification.Builder.with() + .setName(opts.testName) + .withRunnable() + .add("BundledJarRunnable", new BundledJarRunnable(), resourceSpec) + .withLocalFiles() + .add(jarFile.getName(), jarFile.toURI(), false) + .add(testProps.getName(), testProps.toURI()) + .add(log4jProps.getName(), log4jProps.toURI()) + .apply() + .anyOrder() + .build(); + } + } + + private static class TestRunnerOpts { + + @Parameter(names={"--name", "-n"}, required = true, description = "Test name") + String testName; + + @Parameter(names={"--jar", "-j"}, required = true, description = "Bundled jar path") + String jarPath; + + @Parameter(names={"--main", "-m"}, required = true, description = "Main class") + String mainClass; + + @Parameter(names={"--testProps", "-t"}, required = true, description = "Test properties path") + String testProps; + + @Parameter(names={"--logProps", "-l"}, required = true, description = "Log properties path") + String logProps; + + @Parameter(names={"--args", "-a"}, variableArity = true, description = "Main class args") + List<String> mainArgs = new ArrayList<>(); + } + + private static void verifyPath(String path) { + File f = new File(path); + Preconditions.checkState(f.exists()); + Preconditions.checkState(f.canRead()); + } + + public static void main(String[] args) throws Exception { + + TestRunnerOpts opts = new TestRunnerOpts(); + new JCommander(opts, args); + + verifyPath(opts.jarPath); + verifyPath(opts.testProps); + verifyPath(opts.logProps); + + String[] mainArgs = opts.mainArgs.stream().toArray(String[]::new); + BundledJarRunner.Arguments arguments = new BundledJarRunner.Arguments(opts.jarPath, "/lib", + opts.mainClass, mainArgs); + + Properties props = new Properties(); + FileInputStream fis = new FileInputStream(opts.testProps); + props.load(fis); + fis.close(); + String zookeepers = props.getProperty(TestProps.ZOOKEEPERS); + + final TwillRunnerService twillRunner = new YarnTwillRunnerService(new YarnConfiguration(), + zookeepers); + twillRunner.start(); + + final TwillController controller = twillRunner.prepare( + new TestRunnerApp(opts, props)) + .addJVMOptions("-Dlog4j.configuration=file:$PWD/" + new File(opts.logProps).getName()) + .withArguments("BundledJarRunnable", arguments.toArray()) + .start(); + + final AtomicBoolean done = new AtomicBoolean(false); + + Runtime.getRuntime().addShutdownHook(new Thread(() -> { + try { + if (!done.get()) { + controller.kill(); + } + } finally { + twillRunner.stop(); + } + })); + + LOG.info("Waiting for {} to finish in YARN...", opts.testName); + LOG.info("Press ctrl-c to kill {} in YARN", opts.testName); + + try { + controller.awaitTerminated(); + done.set(true); + } catch (ExecutionException e) { + LOG.error("Error", e); --- End diff -- I will change
--- If your project is set up for it, you can reply to this email and have your reply appear on GitHub as well. If your project does not have this feature enabled and wishes so, or if the feature is enabled but not working, please contact infrastructure at infrastruct...@apache.org or file a JIRA ticket with INFRA. ---