From 15ed74fa244eacbe7a8eac877ea64d7a53a1bf9c Mon Sep 17 00:00:00 2001 From: Nanda kumar Date: Wed, 19 Sep 2018 19:46:25 +0530 Subject: [PATCH] HDDS-502. Exception in OM startup when running unit tests. Contributed by Arpit Agarwal. --- .../scm/server/StorageContainerManager.java | 55 +++++++++++++++---- .../apache/hadoop/ozone/om/OzoneManager.java | 33 +++++++++-- 2 files changed, 73 insertions(+), 15 deletions(-) diff --git a/hadoop-hdds/server-scm/src/main/java/org/apache/hadoop/hdds/scm/server/StorageContainerManager.java b/hadoop-hdds/server-scm/src/main/java/org/apache/hadoop/hdds/scm/server/StorageContainerManager.java index 21691490ed..86c061b735 100644 --- a/hadoop-hdds/server-scm/src/main/java/org/apache/hadoop/hdds/scm/server/StorageContainerManager.java +++ b/hadoop-hdds/server-scm/src/main/java/org/apache/hadoop/hdds/scm/server/StorageContainerManager.java @@ -374,7 +374,8 @@ public static void main(String[] argv) throws IOException { hParser.printGenericCommandUsage(System.err); System.exit(1); } - StorageContainerManager scm = createSCM(hParser.getRemainingArgs(), conf); + StorageContainerManager scm = createSCM( + hParser.getRemainingArgs(), conf, true); if (scm != null) { scm.start(); scm.join(); @@ -389,9 +390,37 @@ private static void printUsage(PrintStream out) { out.println(USAGE + "\n"); } - public static StorageContainerManager createSCM(String[] args, - OzoneConfiguration conf) - throws IOException { + /** + * Create an SCM instance based on the supplied command-line arguments. + * + * This method is intended for unit tests only. It suppresses the + * startup/shutdown message and skips registering Unix signal + * handlers. + * + * @param args command line arguments. + * @param conf HDDS configuration + * @return SCM instance + * @throws IOException + */ + @VisibleForTesting + public static StorageContainerManager createSCM( + String[] args, OzoneConfiguration conf) throws IOException { + return createSCM(args, conf, false); + } + + /** + * Create an SCM instance based on the supplied command-line arguments. + * + * @param args command-line arguments. + * @param conf HDDS configuration + * @param printBanner if true, then log a verbose startup message. + * @return SCM instance + * @throws IOException + */ + private static StorageContainerManager createSCM( + String[] args, + OzoneConfiguration conf, + boolean printBanner) throws IOException { String[] argv = (args == null) ? new String[0] : args; if (!HddsUtils.isHddsEnabled(conf)) { System.err.println( @@ -407,13 +436,17 @@ public static StorageContainerManager createSCM(String[] args, } switch (startOpt) { case INIT: - StringUtils.startupShutdownMessage(StorageContainerManager.class, argv, - LOG); + if (printBanner) { + StringUtils.startupShutdownMessage(StorageContainerManager.class, argv, + LOG); + } terminate(scmInit(conf) ? 0 : 1); return null; case GENCLUSTERID: - StringUtils.startupShutdownMessage(StorageContainerManager.class, argv, - LOG); + if (printBanner) { + StringUtils.startupShutdownMessage(StorageContainerManager.class, argv, + LOG); + } System.out.println("Generating new cluster id:"); System.out.println(StorageInfo.newClusterID()); terminate(0); @@ -423,8 +456,10 @@ public static StorageContainerManager createSCM(String[] args, terminate(0); return null; default: - StringUtils.startupShutdownMessage(StorageContainerManager.class, argv, - LOG); + if (printBanner) { + StringUtils.startupShutdownMessage(StorageContainerManager.class, argv, + LOG); + } return new StorageContainerManager(conf); } } diff --git a/hadoop-ozone/ozone-manager/src/main/java/org/apache/hadoop/ozone/om/OzoneManager.java b/hadoop-ozone/ozone-manager/src/main/java/org/apache/hadoop/ozone/om/OzoneManager.java index 71cc6bae5f..6ea0fe742a 100644 --- a/hadoop-ozone/ozone-manager/src/main/java/org/apache/hadoop/ozone/om/OzoneManager.java +++ b/hadoop-ozone/ozone-manager/src/main/java/org/apache/hadoop/ozone/om/OzoneManager.java @@ -258,7 +258,7 @@ public static void main(String[] argv) throws IOException { hParser.printGenericCommandUsage(System.err); System.exit(1); } - OzoneManager om = createOm(hParser.getRemainingArgs(), conf); + OzoneManager om = createOm(hParser.getRemainingArgs(), conf, true); if (om != null) { om.start(); om.join(); @@ -276,14 +276,33 @@ private static void printUsage(PrintStream out) { /** * Constructs OM instance based on command line arguments. * + * This method is intended for unit tests only. It suppresses the + * startup/shutdown message and skips registering Unix signal + * handlers. + * * @param argv Command line arguments * @param conf OzoneConfiguration * @return OM instance * @throws IOException in case OM instance creation fails. */ + @VisibleForTesting + public static OzoneManager createOm( + String[] argv, OzoneConfiguration conf) throws IOException { + return createOm(argv, conf, false); + } - public static OzoneManager createOm(String[] argv, - OzoneConfiguration conf) throws IOException { + + /** + * Constructs OM instance based on command line arguments. + * + * @param argv Command line arguments + * @param conf OzoneConfiguration + * @param printBanner if true then log a verbose startup message. + * @return OM instance + * @throws IOException in case OM instance creation fails. + */ + private static OzoneManager createOm(String[] argv, + OzoneConfiguration conf, boolean printBanner) throws IOException { if (!isHddsEnabled(conf)) { System.err.println("OM cannot be started in secure mode or when " + OZONE_ENABLED + " is set to false"); @@ -297,7 +316,9 @@ public static OzoneManager createOm(String[] argv, } switch (startOpt) { case CREATEOBJECTSTORE: - StringUtils.startupShutdownMessage(OzoneManager.class, argv, LOG); + if (printBanner) { + StringUtils.startupShutdownMessage(OzoneManager.class, argv, LOG); + } terminate(omInit(conf) ? 0 : 1); return null; case HELP: @@ -308,7 +329,9 @@ public static OzoneManager createOm(String[] argv, if (argv == null) { argv = new String[]{}; } - StringUtils.startupShutdownMessage(OzoneManager.class, argv, LOG); + if (printBanner) { + StringUtils.startupShutdownMessage(OzoneManager.class, argv, LOG); + } return new OzoneManager(conf); } }