|
28 | 28 | import java.util.concurrent.TimeUnit; |
29 | 29 | import java.util.concurrent.atomic.AtomicBoolean; |
30 | 30 |
|
| 31 | +import org.apache.commons.cli.CommandLine; |
| 32 | +import org.apache.commons.cli.DefaultParser; |
| 33 | +import org.apache.commons.cli.Option; |
| 34 | +import org.apache.commons.cli.Options; |
| 35 | +import org.apache.commons.cli.ParseException; |
| 36 | +import org.apache.commons.cli.MissingArgumentException; |
31 | 37 | import org.apache.commons.lang.time.DurationFormatUtils; |
32 | 38 | import org.apache.hadoop.classification.InterfaceAudience.Private; |
33 | 39 | import org.apache.hadoop.conf.Configuration; |
|
37 | 43 | import org.apache.hadoop.security.SecurityUtil; |
38 | 44 | import org.apache.hadoop.security.UserGroupInformation; |
39 | 45 | import org.apache.hadoop.service.CompositeService; |
40 | | -import org.apache.hadoop.util.GenericOptionsParser; |
41 | 46 | import org.apache.hadoop.util.JvmPauseMonitor; |
42 | 47 | import org.apache.hadoop.util.ShutdownHookManager; |
43 | 48 | import org.apache.hadoop.util.StringUtils; |
44 | 49 | import org.apache.hadoop.util.VersionInfo; |
| 50 | +import org.apache.hadoop.util.GenericOptionsParser; |
45 | 51 | import org.apache.hadoop.yarn.YarnUncaughtExceptionHandler; |
| 52 | +import org.apache.hadoop.yarn.api.records.ApplicationId; |
46 | 53 | import org.apache.hadoop.yarn.conf.YarnConfiguration; |
47 | 54 | import org.apache.hadoop.yarn.exceptions.YarnRuntimeException; |
| 55 | +import org.apache.hadoop.yarn.server.federation.utils.FederationStateStoreFacade; |
48 | 56 | import org.apache.hadoop.yarn.server.resourcemanager.webapp.RMWebAppUtil; |
49 | 57 | import org.apache.hadoop.yarn.server.router.cleaner.SubClusterCleaner; |
50 | 58 | import org.apache.hadoop.yarn.server.router.clientrm.RouterClientRMService; |
@@ -103,6 +111,9 @@ public class Router extends CompositeService { |
103 | 111 | protected String webAppAddress; |
104 | 112 | private static long clusterTimeStamp = System.currentTimeMillis(); |
105 | 113 | private FedAppReportFetcher fetcher = null; |
| 114 | + private static final String CMD_FORMAT_STATE_STORE = "-format-state-store"; |
| 115 | + private static final String CMD_REMOVE_APPLICATION_FROM_STATE_STORE = |
| 116 | + "-remove-application-from-state-store"; |
106 | 117 |
|
107 | 118 | /** |
108 | 119 | * Priority of the Router shutdown hook. |
@@ -191,7 +202,7 @@ protected void serviceStop() throws Exception { |
191 | 202 | } |
192 | 203 |
|
193 | 204 | protected void shutDown() { |
194 | | - new Thread(() -> Router.this.stop()).start(); |
| 205 | + new Thread(Router.this::stop).start(); |
195 | 206 | } |
196 | 207 |
|
197 | 208 | protected RouterClientRMService createClientRMProxyService() { |
@@ -292,24 +303,14 @@ public static String getProxyHostAndPort(Configuration conf) { |
292 | 303 |
|
293 | 304 | public static void main(String[] argv) { |
294 | 305 | Configuration conf = new YarnConfiguration(); |
295 | | - Thread |
296 | | - .setDefaultUncaughtExceptionHandler(new YarnUncaughtExceptionHandler()); |
| 306 | + Thread.setDefaultUncaughtExceptionHandler(new YarnUncaughtExceptionHandler()); |
297 | 307 | StringUtils.startupShutdownMessage(Router.class, argv, LOG); |
298 | 308 | Router router = new Router(); |
299 | 309 | try { |
300 | 310 | GenericOptionsParser hParser = new GenericOptionsParser(conf, argv); |
301 | 311 | argv = hParser.getRemainingArgs(); |
302 | 312 | 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 | + executeRouterCommand(conf, argv); |
313 | 314 | } else { |
314 | 315 | // Remove the old hook if we are rebooting. |
315 | 316 | if (null != routerShutdownHook) { |
@@ -362,6 +363,73 @@ public FedAppReportFetcher getFetcher() { |
362 | 363 | return fetcher; |
363 | 364 | } |
364 | 365 |
|
| 366 | + @VisibleForTesting |
| 367 | + public static void removeApplication(Configuration conf, String applicationId) |
| 368 | + throws Exception { |
| 369 | + FederationStateStoreFacade facade = FederationStateStoreFacade.getInstance(conf); |
| 370 | + ApplicationId removeAppId = ApplicationId.fromString(applicationId); |
| 371 | + LOG.info("Deleting application {} from state store.", removeAppId); |
| 372 | + facade.deleteApplicationHomeSubCluster(removeAppId); |
| 373 | + LOG.info("Application is deleted from state store"); |
| 374 | + } |
| 375 | + |
| 376 | + private static void handFormatStateStore() { |
| 377 | + // TODO: YARN-11548. [Federation] Router Supports Format FederationStateStore. |
| 378 | + System.err.println("format-state-store is not yet supported."); |
| 379 | + } |
| 380 | + |
| 381 | + private static void handRemoveApplicationFromStateStore(Configuration conf, |
| 382 | + String applicationId) { |
| 383 | + try { |
| 384 | + removeApplication(conf, applicationId); |
| 385 | + System.out.println("Application " + applicationId + " is deleted from state store"); |
| 386 | + } catch (Exception e) { |
| 387 | + System.err.println("Application " + applicationId + " error, exception = " + e); |
| 388 | + } |
| 389 | + } |
| 390 | + |
| 391 | + private static void executeRouterCommand(Configuration conf, String[] args) { |
| 392 | + // Step1. Define Options. |
| 393 | + Options opts = new Options(); |
| 394 | + Option formatStateStoreOpt = new Option("format-state-store", false, |
| 395 | + " Formats the FederationStateStore. " + |
| 396 | + "This will clear the FederationStateStore and " + |
| 397 | + "is useful if past applications are no longer needed. " + |
| 398 | + "This should be run only when the Router is not running."); |
| 399 | + Option removeApplicationFromStateStoreOpt = new Option("remove-application-from-state-store", |
| 400 | + false, " Remove the application from FederationStateStore. " + |
| 401 | + " This should be run only when the Router is not running. "); |
| 402 | + opts.addOption(formatStateStoreOpt); |
| 403 | + opts.addOption(removeApplicationFromStateStoreOpt); |
| 404 | + |
| 405 | + // Step2. Parse Options. |
| 406 | + try { |
| 407 | + String cmd = args[0]; |
| 408 | + |
| 409 | + CommandLine cliParser = new DefaultParser().parse(opts, args); |
| 410 | + |
| 411 | + if (CMD_FORMAT_STATE_STORE.equals(cmd)) { |
| 412 | + handFormatStateStore(); |
| 413 | + } else if (CMD_REMOVE_APPLICATION_FROM_STATE_STORE.equals(cmd)) { |
| 414 | + if (cliParser.hasOption(removeApplicationFromStateStoreOpt)) { |
| 415 | + String applicationId = cliParser.getOptionValue(removeApplicationFromStateStoreOpt); |
| 416 | + handRemoveApplicationFromStateStore(conf, applicationId); |
| 417 | + } else { |
| 418 | + System.err.println("remove-application-from-state-store requires application arg."); |
| 419 | + } |
| 420 | + } else { |
| 421 | + System.out.println("No related commands found."); |
| 422 | + printUsage(System.err); |
| 423 | + } |
| 424 | + } catch (MissingArgumentException ex) { |
| 425 | + System.out.println("Missing argument for options."); |
| 426 | + printUsage(System.err); |
| 427 | + } catch (ParseException e) { |
| 428 | + System.out.println("Parsing of a command-line error."); |
| 429 | + printUsage(System.err); |
| 430 | + } |
| 431 | + } |
| 432 | + |
365 | 433 | private static void printUsage(PrintStream out) { |
366 | 434 | out.println("Usage: yarn router [-format-state-store] | " + |
367 | 435 | "[-remove-application-from-state-store <appId>]"); |
|
0 commit comments