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.fail.driver; 020 021import org.apache.reef.annotations.audience.ClientSide; 022import org.apache.reef.annotations.audience.Private; 023import org.apache.reef.client.DriverConfiguration; 024import org.apache.reef.client.LauncherStatus; 025import org.apache.reef.proto.ReefServiceProtos; 026import org.apache.reef.runtime.common.REEFEnvironment; 027import org.apache.reef.runtime.common.launch.REEFMessageCodec; 028import org.apache.reef.tang.Configuration; 029import org.apache.reef.tang.Tang; 030import org.apache.reef.tang.exceptions.InjectionException; 031import org.apache.reef.tests.TestDriverLauncher; 032import org.apache.reef.util.EnvironmentUtils; 033import org.apache.reef.wake.remote.RemoteConfiguration; 034 035/** 036 * Client for the test REEF job that fails on different stages of execution. 037 */ 038@Private 039@ClientSide 040public final class FailClient { 041 042 private static final Tang TANG = Tang.Factory.getTang(); 043 044 private static Configuration buildDriverConfig(final Class<?> failMsgClass) { 045 046 final Configuration driverConfig = DriverConfiguration.CONF 047 .set(DriverConfiguration.GLOBAL_LIBRARIES, EnvironmentUtils.getClassLocation(FailDriver.class)) 048 .set(DriverConfiguration.DRIVER_IDENTIFIER, "Fail_" + failMsgClass.getSimpleName()) 049 .set(DriverConfiguration.ON_DRIVER_STARTED, FailDriver.StartHandler.class) 050 .set(DriverConfiguration.ON_DRIVER_STOP, FailDriver.StopHandler.class) 051 .set(DriverConfiguration.ON_EVALUATOR_ALLOCATED, FailDriver.AllocatedEvaluatorHandler.class) 052 .set(DriverConfiguration.ON_EVALUATOR_COMPLETED, FailDriver.CompletedEvaluatorHandler.class) 053 .set(DriverConfiguration.ON_EVALUATOR_FAILED, FailDriver.FailedEvaluatorHandler.class) 054 .set(DriverConfiguration.ON_CONTEXT_ACTIVE, FailDriver.ActiveContextHandler.class) 055 .set(DriverConfiguration.ON_CONTEXT_MESSAGE, FailDriver.ContextMessageHandler.class) 056 .set(DriverConfiguration.ON_CONTEXT_CLOSED, FailDriver.ClosedContextHandler.class) 057 .set(DriverConfiguration.ON_CONTEXT_FAILED, FailDriver.FailedContextHandler.class) 058 .set(DriverConfiguration.ON_TASK_RUNNING, FailDriver.RunningTaskHandler.class) 059 .set(DriverConfiguration.ON_TASK_SUSPENDED, FailDriver.SuspendedTaskHandler.class) 060 .set(DriverConfiguration.ON_TASK_MESSAGE, FailDriver.TaskMessageHandler.class) 061 .set(DriverConfiguration.ON_TASK_FAILED, FailDriver.FailedTaskHandler.class) 062 .set(DriverConfiguration.ON_TASK_COMPLETED, FailDriver.CompletedTaskHandler.class) 063 .build(); 064 065 return TANG.newConfigurationBuilder(driverConfig) 066 .bindNamedParameter(FailDriver.FailMsgClassName.class, failMsgClass.getName()) 067 .build(); 068 } 069 070 /** 071 * Run REEF on specified runtime and fail (raise an exception) in a specified class. 072 * @param failMsgClass A class that should fail during the test. 073 * @param runtimeConfig REEF runtime configuration. Can be e.g. Local or YARN. 074 * @param timeOut REEF application timeout. 075 * @return launcher status - usually FAIL. 076 * @throws InjectionException configuration error. 077 */ 078 public static LauncherStatus runClient(final Class<?> failMsgClass, 079 final Configuration runtimeConfig, final int timeOut) throws InjectionException { 080 081 return TestDriverLauncher.getLauncher(runtimeConfig).run(buildDriverConfig(failMsgClass), timeOut); 082 } 083 084 /** 085 * Run REEF in-process using specified runtime and fail (raise an exception) in a specified class. 086 * @param failMsgClass A class that should fail during the test. 087 * @param runtimeConfig REEF runtime configuration. Can be e.g. Local or YARN. 088 * @param timeOut REEF application timeout - not used yet. 089 * @return Final job status. Final status for tests is usually something 090 * with state = FAILED and exception like SimulatedDriverFailure. 091 * @throws InjectionException configuration error. 092 */ 093 public static ReefServiceProtos.JobStatusProto runInProcess(final Class<?> failMsgClass, 094 final Configuration runtimeConfig, final int timeOut) throws InjectionException { 095 096 final Configuration driverConfig = 097 TANG.newConfigurationBuilder(buildDriverConfig(failMsgClass)) 098 .bindNamedParameter(RemoteConfiguration.ManagerName.class, "REEF_FAIL_ENV") 099 .bindNamedParameter(RemoteConfiguration.MessageCodec.class, REEFMessageCodec.class) 100 .build(); 101 102 try (final REEFEnvironment reef = REEFEnvironment.fromConfiguration(runtimeConfig, driverConfig)) { 103 reef.run(); 104 return reef.getLastStatus(); 105 } 106 } 107 108 /** 109 * Empty private constructor to prohibit instantiation of utility class. 110 */ 111 private FailClient() { 112 } 113}