001/* 002 * Licensed to the Apache Software Foundation (ASF) under one 003 * or more contributor license agreements. See the NOTICE file 004 * distributed with this work for additional information 005 * regarding copyright ownership. The ASF licenses this file 006 * to you under the Apache License, Version 2.0 (the 007 * "License"); you may not use this file except in compliance 008 * with the License. You may obtain a copy of the License at 009 * 010 * http://www.apache.org/licenses/LICENSE-2.0 011 * 012 * Unless required by applicable law or agreed to in writing, 013 * software distributed under the License is distributed on an 014 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 015 * KIND, either express or implied. See the License for the 016 * specific language governing permissions and limitations 017 * under the License. 018 */ 019package org.apache.reef.tests.yarn.failure; 020 021import org.apache.reef.client.DriverConfiguration; 022import org.apache.reef.client.DriverLauncher; 023import org.apache.reef.client.LauncherStatus; 024import org.apache.reef.runtime.local.client.LocalRuntimeConfiguration; 025import org.apache.reef.runtime.yarn.client.YarnClientConfiguration; 026import org.apache.reef.tang.Configuration; 027import org.apache.reef.tang.Injector; 028import org.apache.reef.tang.JavaConfigurationBuilder; 029import org.apache.reef.tang.Tang; 030import org.apache.reef.tang.annotations.Name; 031import org.apache.reef.tang.annotations.NamedParameter; 032import org.apache.reef.tang.exceptions.BindException; 033import org.apache.reef.tang.exceptions.InjectionException; 034import org.apache.reef.tang.formats.CommandLine; 035import org.apache.reef.util.EnvironmentUtils; 036 037import java.io.IOException; 038import java.util.logging.Level; 039import java.util.logging.Logger; 040 041/** 042 * Entry point class for REEF failure test. 043 */ 044public final class FailureREEF { 045 /** 046 * The upper limit on the number of Evaluators that the local resourcemanager will hand out concurrently. 047 */ 048 public static final int MAX_NUMBER_OF_EVALUATORS = 16; 049 050 private static final Logger LOG = Logger.getLogger(FailureREEF.class.getName()); 051 052 private static Configuration parseCommandLine(final String[] aArgs) { 053 final JavaConfigurationBuilder cb = Tang.Factory.getTang().newConfigurationBuilder(); 054 try { 055 new CommandLine(cb) 056 .registerShortNameOfClass(Local.class) 057 .registerShortNameOfClass(TimeOut.class) 058 .processCommandLine(aArgs); 059 return cb.build(); 060 } catch (final BindException | IOException ex) { 061 final String msg = "Unable to parse command line"; 062 LOG.log(Level.SEVERE, msg, ex); 063 throw new RuntimeException(msg, ex); 064 } 065 } 066 067 /** 068 * @return (immutable) TANG Configuration object. 069 * @throws BindException if configuration injector fails. 070 * @throws InjectionException if the Local.class parameter is not injected. 071 */ 072 private static Configuration getRunTimeConfiguration(final boolean isLocal) throws BindException { 073 074 final Configuration runtimeConfiguration; 075 076 if (isLocal) { 077 LOG.log(Level.INFO, "Running Failure demo on the local runtime"); 078 runtimeConfiguration = LocalRuntimeConfiguration.CONF 079 .set(LocalRuntimeConfiguration.MAX_NUMBER_OF_EVALUATORS, MAX_NUMBER_OF_EVALUATORS) 080 .build(); 081 } else { 082 LOG.log(Level.INFO, "Running Failure demo on YARN"); 083 runtimeConfiguration = YarnClientConfiguration.CONF.build(); 084 } 085 086 return runtimeConfiguration; 087 } 088 089 public static LauncherStatus runFailureReef( 090 final Configuration runtimeConfig, final int timeout) throws InjectionException { 091 092 final Configuration driverConf = DriverConfiguration.CONF 093 .set(DriverConfiguration.GLOBAL_LIBRARIES, EnvironmentUtils.getClassLocation(FailureDriver.class)) 094 .set(DriverConfiguration.DRIVER_IDENTIFIER, "FailureREEF") 095 .set(DriverConfiguration.ON_DRIVER_STARTED, FailureDriver.StartHandler.class) 096 .set(DriverConfiguration.ON_EVALUATOR_ALLOCATED, FailureDriver.EvaluatorAllocatedHandler.class) 097 .set(DriverConfiguration.ON_EVALUATOR_FAILED, FailureDriver.EvaluatorFailedHandler.class) 098 .build(); 099 100 final LauncherStatus state = DriverLauncher.getLauncher(runtimeConfig).run(driverConf, timeout); 101 LOG.log(Level.INFO, "REEF job completed: {0}", state); 102 return state; 103 } 104 105 public static void main(final String[] args) throws InjectionException { 106 final Configuration commandLineConf = parseCommandLine(args); 107 final Injector injector = Tang.Factory.getTang().newInjector(commandLineConf); 108 final boolean isLocal = injector.getNamedInstance(Local.class); 109 final int jobTimeout = injector.getNamedInstance(TimeOut.class) * 60 * 1000; 110 runFailureReef(getRunTimeConfiguration(isLocal), jobTimeout); 111 } 112 113 /** 114 * Empty private constructor to prohibit instantiation of utility class. 115 */ 116 private FailureREEF() { 117 } 118 119 /** 120 * Command line parameter = true to run locally, or false to run on YARN. 121 */ 122 @NamedParameter(doc = "Whether or not to run on the local runtime", 123 short_name = "local", default_value = "true") 124 public static final class Local implements Name<Boolean> { 125 } 126 127 /** 128 * Number of minutes before timeout. 129 */ 130 @NamedParameter(doc = "Number of minutes before timeout", 131 short_name = "timeout", default_value = "2") 132 public static final class TimeOut implements Name<Integer> { 133 } 134}