YARN-11433. Router's main() should support generic options. (#6012)

This commit is contained in:
slfan1989 2023-09-13 05:48:40 +08:00 committed by GitHub
parent 475932c524
commit 9c8cdbe28e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 77 additions and 12 deletions

View File

@ -19,6 +19,7 @@
package org.apache.hadoop.yarn.server.globalpolicygenerator; package org.apache.hadoop.yarn.server.globalpolicygenerator;
import java.io.IOException; import java.io.IOException;
import java.io.PrintStream;
import java.net.InetAddress; import java.net.InetAddress;
import java.net.UnknownHostException; import java.net.UnknownHostException;
import java.util.ArrayList; import java.util.ArrayList;
@ -37,6 +38,7 @@
import org.apache.hadoop.security.SecurityUtil; import org.apache.hadoop.security.SecurityUtil;
import org.apache.hadoop.security.UserGroupInformation; import org.apache.hadoop.security.UserGroupInformation;
import org.apache.hadoop.service.CompositeService; import org.apache.hadoop.service.CompositeService;
import org.apache.hadoop.util.GenericOptionsParser;
import org.apache.hadoop.util.JvmPauseMonitor; import org.apache.hadoop.util.JvmPauseMonitor;
import org.apache.hadoop.util.ShutdownHookManager; import org.apache.hadoop.util.ShutdownHookManager;
import org.apache.hadoop.util.StringUtils; import org.apache.hadoop.util.StringUtils;
@ -292,7 +294,19 @@ private String getHostName(Configuration config)
public static void main(String[] argv) { public static void main(String[] argv) {
try { try {
startGPG(argv, new YarnConfiguration()); YarnConfiguration conf = new YarnConfiguration();
GenericOptionsParser hParser = new GenericOptionsParser(conf, argv);
argv = hParser.getRemainingArgs();
if (argv.length > 1) {
if (argv[0].equals("-format-policy-store")) {
// TODO: YARN-11561. [Federation] GPG Supports Format PolicyStateStore.
System.err.println("format-policy-store is not yet supported.");
} else {
printUsage(System.err);
}
} else {
startGPG(argv, conf);
}
} catch (Throwable t) { } catch (Throwable t) {
LOG.error("Error starting global policy generator", t); LOG.error("Error starting global policy generator", t);
System.exit(-1); System.exit(-1);
@ -307,4 +321,8 @@ public static long getGPGStartupTime() {
public WebApp getWebApp() { public WebApp getWebApp() {
return webApp; return webApp;
} }
private static void printUsage(PrintStream out) {
out.println("Usage: yarn gpg [-format-policy-store]");
}
} }

View File

@ -26,9 +26,13 @@
import org.apache.hadoop.yarn.conf.YarnConfiguration; import org.apache.hadoop.yarn.conf.YarnConfiguration;
import org.junit.Test; import org.junit.Test;
import java.io.ByteArrayOutputStream;
import java.io.PrintStream;
import java.util.List; import java.util.List;
import java.util.concurrent.TimeoutException; import java.util.concurrent.TimeoutException;
import static org.junit.Assert.assertTrue;
/** /**
* Unit test for GlobalPolicyGenerator. * Unit test for GlobalPolicyGenerator.
*/ */
@ -58,6 +62,17 @@ public void testGpgWithFederation() throws InterruptedException, TimeoutExceptio
}, 100, 5000); }, 100, 5000);
} }
@Test
public void testGPGCLI() {
ByteArrayOutputStream dataOut = new ByteArrayOutputStream();
ByteArrayOutputStream dataErr = new ByteArrayOutputStream();
System.setOut(new PrintStream(dataOut));
System.setErr(new PrintStream(dataErr));
GlobalPolicyGenerator.main(new String[]{"-help", "-format-policy-store"});
assertTrue(dataErr.toString().contains(
"Usage: yarn gpg [-format-policy-store]"));
}
@Test @Test
public void testUserProvidedUGIConf() throws Exception { public void testUserProvidedUGIConf() throws Exception {
String errMsg = "Invalid attribute value for " + String errMsg = "Invalid attribute value for " +

View File

@ -19,6 +19,7 @@
package org.apache.hadoop.yarn.server.router; package org.apache.hadoop.yarn.server.router;
import java.io.IOException; import java.io.IOException;
import java.io.PrintStream;
import java.net.InetAddress; import java.net.InetAddress;
import java.net.URL; import java.net.URL;
import java.net.InetSocketAddress; import java.net.InetSocketAddress;
@ -36,6 +37,7 @@
import org.apache.hadoop.security.SecurityUtil; import org.apache.hadoop.security.SecurityUtil;
import org.apache.hadoop.security.UserGroupInformation; import org.apache.hadoop.security.UserGroupInformation;
import org.apache.hadoop.service.CompositeService; import org.apache.hadoop.service.CompositeService;
import org.apache.hadoop.util.GenericOptionsParser;
import org.apache.hadoop.util.JvmPauseMonitor; import org.apache.hadoop.util.JvmPauseMonitor;
import org.apache.hadoop.util.ShutdownHookManager; import org.apache.hadoop.util.ShutdownHookManager;
import org.apache.hadoop.util.StringUtils; import org.apache.hadoop.util.StringUtils;
@ -295,18 +297,29 @@ public static void main(String[] argv) {
StringUtils.startupShutdownMessage(Router.class, argv, LOG); StringUtils.startupShutdownMessage(Router.class, argv, LOG);
Router router = new Router(); Router router = new Router();
try { try {
GenericOptionsParser hParser = new GenericOptionsParser(conf, argv);
// Remove the old hook if we are rebooting. argv = hParser.getRemainingArgs();
if (null != routerShutdownHook) { if (argv.length > 1) {
ShutdownHookManager.get().removeShutdownHook(routerShutdownHook); if (argv[0].equals("-format-state-store")) {
// TODO: YARN-11548. [Federation] Router Supports Format FederationStateStore.
System.err.println("format-state-store is not yet supported.");
} else if (argv[0].equals("-remove-application-from-state-store") && argv.length == 2) {
// TODO: YARN-11547. [Federation]
// Router Supports Remove individual application records from FederationStateStore.
System.err.println("remove-application-from-state-store is not yet supported.");
} else {
printUsage(System.err);
}
} else {
// Remove the old hook if we are rebooting.
if (null != routerShutdownHook) {
ShutdownHookManager.get().removeShutdownHook(routerShutdownHook);
}
routerShutdownHook = new CompositeServiceShutdownHook(router);
ShutdownHookManager.get().addShutdownHook(routerShutdownHook, SHUTDOWN_HOOK_PRIORITY);
router.init(conf);
router.start();
} }
routerShutdownHook = new CompositeServiceShutdownHook(router);
ShutdownHookManager.get().addShutdownHook(routerShutdownHook,
SHUTDOWN_HOOK_PRIORITY);
router.init(conf);
router.start();
} catch (Throwable t) { } catch (Throwable t) {
LOG.error("Error starting Router", t); LOG.error("Error starting Router", t);
System.exit(-1); System.exit(-1);
@ -348,4 +361,9 @@ public static long getClusterTimeStamp() {
public FedAppReportFetcher getFetcher() { public FedAppReportFetcher getFetcher() {
return fetcher; return fetcher;
} }
private static void printUsage(PrintStream out) {
out.println("Usage: yarn router [-format-state-store] | " +
"[-remove-application-from-state-store <appId>]");
}
} }

View File

@ -18,6 +18,7 @@
package org.apache.hadoop.yarn.server.router; package org.apache.hadoop.yarn.server.router;
import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail; import static org.junit.Assert.fail;
import org.apache.hadoop.conf.Configuration; import org.apache.hadoop.conf.Configuration;
@ -43,7 +44,9 @@
import javax.servlet.http.Cookie; import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpServletResponse;
import java.io.ByteArrayOutputStream;
import java.io.IOException; import java.io.IOException;
import java.io.PrintStream;
import java.io.PrintWriter; import java.io.PrintWriter;
import java.util.Collection; import java.util.Collection;
import java.util.HashMap; import java.util.HashMap;
@ -390,4 +393,15 @@ public Locale getLocale() {
} }
} }
@Test
public void testRouterCLI() {
ByteArrayOutputStream dataOut = new ByteArrayOutputStream();
ByteArrayOutputStream dataErr = new ByteArrayOutputStream();
System.setOut(new PrintStream(dataOut));
System.setErr(new PrintStream(dataErr));
Router.main(new String[]{"-help", "-format-state-store"});
assertTrue(dataErr.toString().contains(
"Usage: yarn router [-format-state-store] | " +
"[-remove-application-from-state-store <appId>]"));
}
} }