Skip to content

Commit 9c8cdbe

Browse files
authored
YARN-11433. Router's main() should support generic options. (#6012)
1 parent 475932c commit 9c8cdbe

File tree

4 files changed

+77
-12
lines changed
  • hadoop-yarn-project/hadoop-yarn/hadoop-yarn-server

4 files changed

+77
-12
lines changed

hadoop-yarn-project/hadoop-yarn/hadoop-yarn-server/hadoop-yarn-server-globalpolicygenerator/src/main/java/org/apache/hadoop/yarn/server/globalpolicygenerator/GlobalPolicyGenerator.java

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
package org.apache.hadoop.yarn.server.globalpolicygenerator;
2020

2121
import java.io.IOException;
22+
import java.io.PrintStream;
2223
import java.net.InetAddress;
2324
import java.net.UnknownHostException;
2425
import java.util.ArrayList;
@@ -37,6 +38,7 @@
3738
import org.apache.hadoop.security.SecurityUtil;
3839
import org.apache.hadoop.security.UserGroupInformation;
3940
import org.apache.hadoop.service.CompositeService;
41+
import org.apache.hadoop.util.GenericOptionsParser;
4042
import org.apache.hadoop.util.JvmPauseMonitor;
4143
import org.apache.hadoop.util.ShutdownHookManager;
4244
import org.apache.hadoop.util.StringUtils;
@@ -292,7 +294,19 @@ private String getHostName(Configuration config)
292294

293295
public static void main(String[] argv) {
294296
try {
295-
startGPG(argv, new YarnConfiguration());
297+
YarnConfiguration conf = new YarnConfiguration();
298+
GenericOptionsParser hParser = new GenericOptionsParser(conf, argv);
299+
argv = hParser.getRemainingArgs();
300+
if (argv.length > 1) {
301+
if (argv[0].equals("-format-policy-store")) {
302+
// TODO: YARN-11561. [Federation] GPG Supports Format PolicyStateStore.
303+
System.err.println("format-policy-store is not yet supported.");
304+
} else {
305+
printUsage(System.err);
306+
}
307+
} else {
308+
startGPG(argv, conf);
309+
}
296310
} catch (Throwable t) {
297311
LOG.error("Error starting global policy generator", t);
298312
System.exit(-1);
@@ -307,4 +321,8 @@ public static long getGPGStartupTime() {
307321
public WebApp getWebApp() {
308322
return webApp;
309323
}
324+
325+
private static void printUsage(PrintStream out) {
326+
out.println("Usage: yarn gpg [-format-policy-store]");
327+
}
310328
}

hadoop-yarn-project/hadoop-yarn/hadoop-yarn-server/hadoop-yarn-server-globalpolicygenerator/src/test/java/org/apache/hadoop/yarn/server/globalpolicygenerator/TestGlobalPolicyGenerator.java

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,13 @@
2626
import org.apache.hadoop.yarn.conf.YarnConfiguration;
2727
import org.junit.Test;
2828

29+
import java.io.ByteArrayOutputStream;
30+
import java.io.PrintStream;
2931
import java.util.List;
3032
import java.util.concurrent.TimeoutException;
3133

34+
import static org.junit.Assert.assertTrue;
35+
3236
/**
3337
* Unit test for GlobalPolicyGenerator.
3438
*/
@@ -58,6 +62,17 @@ public void testGpgWithFederation() throws InterruptedException, TimeoutExceptio
5862
}, 100, 5000);
5963
}
6064

65+
@Test
66+
public void testGPGCLI() {
67+
ByteArrayOutputStream dataOut = new ByteArrayOutputStream();
68+
ByteArrayOutputStream dataErr = new ByteArrayOutputStream();
69+
System.setOut(new PrintStream(dataOut));
70+
System.setErr(new PrintStream(dataErr));
71+
GlobalPolicyGenerator.main(new String[]{"-help", "-format-policy-store"});
72+
assertTrue(dataErr.toString().contains(
73+
"Usage: yarn gpg [-format-policy-store]"));
74+
}
75+
6176
@Test
6277
public void testUserProvidedUGIConf() throws Exception {
6378
String errMsg = "Invalid attribute value for " +

hadoop-yarn-project/hadoop-yarn/hadoop-yarn-server/hadoop-yarn-server-router/src/main/java/org/apache/hadoop/yarn/server/router/Router.java

Lines changed: 29 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
package org.apache.hadoop.yarn.server.router;
2020

2121
import java.io.IOException;
22+
import java.io.PrintStream;
2223
import java.net.InetAddress;
2324
import java.net.URL;
2425
import java.net.InetSocketAddress;
@@ -36,6 +37,7 @@
3637
import org.apache.hadoop.security.SecurityUtil;
3738
import org.apache.hadoop.security.UserGroupInformation;
3839
import org.apache.hadoop.service.CompositeService;
40+
import org.apache.hadoop.util.GenericOptionsParser;
3941
import org.apache.hadoop.util.JvmPauseMonitor;
4042
import org.apache.hadoop.util.ShutdownHookManager;
4143
import org.apache.hadoop.util.StringUtils;
@@ -295,18 +297,29 @@ public static void main(String[] argv) {
295297
StringUtils.startupShutdownMessage(Router.class, argv, LOG);
296298
Router router = new Router();
297299
try {
298-
299-
// Remove the old hook if we are rebooting.
300-
if (null != routerShutdownHook) {
301-
ShutdownHookManager.get().removeShutdownHook(routerShutdownHook);
300+
GenericOptionsParser hParser = new GenericOptionsParser(conf, argv);
301+
argv = hParser.getRemainingArgs();
302+
if (argv.length > 1) {
303+
if (argv[0].equals("-format-state-store")) {
304+
// TODO: YARN-11548. [Federation] Router Supports Format FederationStateStore.
305+
System.err.println("format-state-store is not yet supported.");
306+
} else if (argv[0].equals("-remove-application-from-state-store") && argv.length == 2) {
307+
// TODO: YARN-11547. [Federation]
308+
// Router Supports Remove individual application records from FederationStateStore.
309+
System.err.println("remove-application-from-state-store is not yet supported.");
310+
} else {
311+
printUsage(System.err);
312+
}
313+
} else {
314+
// Remove the old hook if we are rebooting.
315+
if (null != routerShutdownHook) {
316+
ShutdownHookManager.get().removeShutdownHook(routerShutdownHook);
317+
}
318+
routerShutdownHook = new CompositeServiceShutdownHook(router);
319+
ShutdownHookManager.get().addShutdownHook(routerShutdownHook, SHUTDOWN_HOOK_PRIORITY);
320+
router.init(conf);
321+
router.start();
302322
}
303-
304-
routerShutdownHook = new CompositeServiceShutdownHook(router);
305-
ShutdownHookManager.get().addShutdownHook(routerShutdownHook,
306-
SHUTDOWN_HOOK_PRIORITY);
307-
308-
router.init(conf);
309-
router.start();
310323
} catch (Throwable t) {
311324
LOG.error("Error starting Router", t);
312325
System.exit(-1);
@@ -348,4 +361,9 @@ public static long getClusterTimeStamp() {
348361
public FedAppReportFetcher getFetcher() {
349362
return fetcher;
350363
}
364+
365+
private static void printUsage(PrintStream out) {
366+
out.println("Usage: yarn router [-format-state-store] | " +
367+
"[-remove-application-from-state-store <appId>]");
368+
}
351369
}

hadoop-yarn-project/hadoop-yarn/hadoop-yarn-server/hadoop-yarn-server-router/src/test/java/org/apache/hadoop/yarn/server/router/TestRouter.java

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
package org.apache.hadoop.yarn.server.router;
1919

2020
import static org.junit.Assert.assertEquals;
21+
import static org.junit.Assert.assertTrue;
2122
import static org.junit.Assert.fail;
2223

2324
import org.apache.hadoop.conf.Configuration;
@@ -43,7 +44,9 @@
4344
import javax.servlet.http.Cookie;
4445
import javax.servlet.http.HttpServletRequest;
4546
import javax.servlet.http.HttpServletResponse;
47+
import java.io.ByteArrayOutputStream;
4648
import java.io.IOException;
49+
import java.io.PrintStream;
4750
import java.io.PrintWriter;
4851
import java.util.Collection;
4952
import java.util.HashMap;
@@ -390,4 +393,15 @@ public Locale getLocale() {
390393
}
391394
}
392395

396+
@Test
397+
public void testRouterCLI() {
398+
ByteArrayOutputStream dataOut = new ByteArrayOutputStream();
399+
ByteArrayOutputStream dataErr = new ByteArrayOutputStream();
400+
System.setOut(new PrintStream(dataOut));
401+
System.setErr(new PrintStream(dataErr));
402+
Router.main(new String[]{"-help", "-format-state-store"});
403+
assertTrue(dataErr.toString().contains(
404+
"Usage: yarn router [-format-state-store] | " +
405+
"[-remove-application-from-state-store <appId>]"));
406+
}
393407
}

0 commit comments

Comments
 (0)