Skip to content

Commit 79eef2e

Browse files
committed
YARN-11433. Router's main() should support generic options.
1 parent bacb4df commit 79eef2e

File tree

4 files changed

+68
-15
lines changed
  • hadoop-yarn-project/hadoop-yarn/hadoop-yarn-server

4 files changed

+68
-15
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: 16 additions & 2 deletions
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;
@@ -292,8 +293,17 @@ private String getHostName(Configuration config)
292293
public static void main(String[] argv) {
293294
try {
294295
YarnConfiguration conf = new YarnConfiguration();
295-
new GenericOptionsParser(conf, argv);
296-
startGPG(argv, conf);
296+
GenericOptionsParser hParser = new GenericOptionsParser(conf, argv);
297+
argv = hParser.getRemainingArgs();
298+
if (argv.length > 1) {
299+
if (argv[0].equals("-format-policy-store")) {
300+
// TODO: YARN-11561. [Federation] GPG Supports Format PolicyStateStore.
301+
} else {
302+
printUsage(System.err);
303+
}
304+
} else {
305+
startGPG(argv, conf);
306+
}
297307
} catch (Throwable t) {
298308
LOG.error("Error starting global policy generator", t);
299309
System.exit(-1);
@@ -308,4 +318,8 @@ public static long getGPGStartupTime() {
308318
public WebApp getWebApp() {
309319
return webApp;
310320
}
321+
322+
private static void printUsage(PrintStream out) {
323+
out.println("Usage: yarn gpg [-format-policy-store]");
324+
}
311325
}

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
@@ -24,9 +24,13 @@
2424
import org.apache.hadoop.yarn.conf.YarnConfiguration;
2525
import org.junit.Test;
2626

27+
import java.io.ByteArrayOutputStream;
28+
import java.io.PrintStream;
2729
import java.util.List;
2830
import java.util.concurrent.TimeoutException;
2931

32+
import static org.junit.Assert.assertTrue;
33+
3034
/**
3135
* Unit test for GlobalPolicyGenerator.
3236
*/
@@ -55,4 +59,15 @@ public void testGpgWithFederation() throws InterruptedException, TimeoutExceptio
5559
return (services.size() == 1 && gpg.getWebApp() != null);
5660
}, 100, 5000);
5761
}
62+
63+
@Test
64+
public void testGPGCLI() {
65+
ByteArrayOutputStream dataOut = new ByteArrayOutputStream();
66+
ByteArrayOutputStream dataErr = new ByteArrayOutputStream();
67+
System.setOut(new PrintStream(dataOut));
68+
System.setErr(new PrintStream(dataErr));
69+
GlobalPolicyGenerator.main(new String[]{"-help", "-format-policy-store"});
70+
assertTrue(dataErr.toString().contains(
71+
"Usage: yarn gpg [-format-policy-store]"));
72+
}
5873
}

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: 24 additions & 13 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.InetSocketAddress;
2425
import java.net.UnknownHostException;
@@ -246,20 +247,26 @@ public static void main(String[] argv) {
246247
StringUtils.startupShutdownMessage(Router.class, argv, LOG);
247248
Router router = new Router();
248249
try {
249-
250-
new GenericOptionsParser(conf, argv);
251-
252-
// Remove the old hook if we are rebooting.
253-
if (null != routerShutdownHook) {
254-
ShutdownHookManager.get().removeShutdownHook(routerShutdownHook);
250+
GenericOptionsParser hParser = new GenericOptionsParser(conf, argv);
251+
argv = hParser.getRemainingArgs();
252+
if (argv.length > 1) {
253+
if (argv[0].equals("-format-state-store")) {
254+
// TODO: YARN-11548. [Federation] Router Supports Format FederationStateStore.
255+
} else if (argv[0].equals("-remove-application-from-state-store") && argv.length == 2) {
256+
// TODO: YARN-11547. [Federation] Router Supports Remove individual application records from FederationStateStore.
257+
} else {
258+
printUsage(System.err);
259+
}
260+
} else {
261+
// Remove the old hook if we are rebooting.
262+
if (null != routerShutdownHook) {
263+
ShutdownHookManager.get().removeShutdownHook(routerShutdownHook);
264+
}
265+
routerShutdownHook = new CompositeServiceShutdownHook(router);
266+
ShutdownHookManager.get().addShutdownHook(routerShutdownHook, SHUTDOWN_HOOK_PRIORITY);
267+
router.init(conf);
268+
router.start();
255269
}
256-
257-
routerShutdownHook = new CompositeServiceShutdownHook(router);
258-
ShutdownHookManager.get().addShutdownHook(routerShutdownHook,
259-
SHUTDOWN_HOOK_PRIORITY);
260-
261-
router.init(conf);
262-
router.start();
263270
} catch (Throwable t) {
264271
LOG.error("Error starting Router", t);
265272
System.exit(-1);
@@ -301,4 +308,8 @@ public static long getClusterTimeStamp() {
301308
public FedAppReportFetcher getFetcher() {
302309
return fetcher;
303310
}
311+
312+
private static void printUsage(PrintStream out) {
313+
out.println("Usage: yarn router [-format-state-store] | [-remove-application-from-state-store <appId>]");
314+
}
304315
}

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: 13 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;
@@ -42,7 +43,9 @@
4243
import javax.servlet.http.Cookie;
4344
import javax.servlet.http.HttpServletRequest;
4445
import javax.servlet.http.HttpServletResponse;
46+
import java.io.ByteArrayOutputStream;
4547
import java.io.IOException;
48+
import java.io.PrintStream;
4649
import java.io.PrintWriter;
4750
import java.util.Collection;
4851
import java.util.HashMap;
@@ -377,4 +380,14 @@ public Locale getLocale() {
377380
}
378381
}
379382

383+
@Test
384+
public void testRouterCLI() {
385+
ByteArrayOutputStream dataOut = new ByteArrayOutputStream();
386+
ByteArrayOutputStream dataErr = new ByteArrayOutputStream();
387+
System.setOut(new PrintStream(dataOut));
388+
System.setErr(new PrintStream(dataErr));
389+
Router.main(new String[]{"-help", "-format-state-store"});
390+
assertTrue(dataErr.toString().contains(
391+
"Usage: yarn router [-format-state-store] | [-remove-application-from-state-store <appId>]"));
392+
}
380393
}

0 commit comments

Comments
 (0)