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; 020 021import org.apache.reef.annotations.Provided; 022import org.apache.reef.annotations.audience.ClientSide; 023import org.apache.reef.annotations.audience.Public; 024import org.apache.reef.client.*; 025import org.apache.reef.tang.Configuration; 026import org.apache.reef.tang.Tang; 027import org.apache.reef.tang.annotations.Unit; 028import org.apache.reef.tang.exceptions.BindException; 029import org.apache.reef.tang.exceptions.InjectionException; 030import org.apache.reef.util.Optional; 031import org.apache.reef.wake.EventHandler; 032 033import javax.inject.Inject; 034import java.util.logging.Level; 035import java.util.logging.Logger; 036 037/** 038 * A launcher for REEF Drivers. It behaves exactly like the original DriverLauncher, 039 * but does not dump the exception stack trace to the log. (it writes only a short INFO message). 040 * It is used in TestFailDriver etc. unit tests. 041 */ 042@Public 043@Provided 044@ClientSide 045@Unit 046public final class TestDriverLauncher { 047 048 private static final Logger LOG = Logger.getLogger(TestDriverLauncher.class.getName()); 049 050 private final DriverLauncher launcher; 051 052 @Inject 053 private TestDriverLauncher(final DriverLauncher launcher) { 054 this.launcher = launcher; 055 } 056 057 /** 058 * Instantiate a launcher for the given Configuration. 059 * 060 * @param runtimeConfiguration the resourcemanager configuration to be used 061 * @return a DriverLauncher based on the given resourcemanager configuration 062 * @throws org.apache.reef.tang.exceptions.BindException on configuration errors 063 * @throws org.apache.reef.tang.exceptions.InjectionException on configuration errors 064 */ 065 public static TestDriverLauncher getLauncher( 066 final Configuration runtimeConfiguration) throws BindException, InjectionException { 067 068 final Configuration clientConfiguration = ClientConfiguration.CONF 069 .set(ClientConfiguration.ON_JOB_RUNNING, DriverLauncher.RunningJobHandler.class) 070 .set(ClientConfiguration.ON_JOB_COMPLETED, DriverLauncher.CompletedJobHandler.class) 071 .set(ClientConfiguration.ON_RUNTIME_ERROR, SilentRuntimeErrorHandler.class) 072 .set(ClientConfiguration.ON_JOB_FAILED, SilentFailedTestJobHandler.class) 073 .build(); 074 075 return Tang.Factory.getTang() 076 .newInjector(runtimeConfiguration, clientConfiguration) 077 .getInstance(TestDriverLauncher.class); 078 } 079 080 public void close() { 081 this.launcher.close(); 082 } 083 084 /** 085 * Run a job. Waits indefinitely for the job to complete. 086 * 087 * @param driverConfig the configuration for the driver. See DriverConfiguration for details. 088 * @return the state of the job after execution. 089 */ 090 public LauncherStatus run(final Configuration driverConfig) { 091 return this.launcher.run(driverConfig); 092 } 093 094 /** 095 * Run a job with a waiting timeout after which it will be killed, if it did not complete yet. 096 * 097 * @param driverConfig the configuration for the driver. See DriverConfiguration for details. 098 * @param timeOut timeout on the job. 099 * @return the state of the job after execution. 100 */ 101 public LauncherStatus run(final Configuration driverConfig, final long timeOut) { 102 return this.launcher.run(driverConfig, timeOut); 103 } 104 105 @Override 106 public String toString() { 107 return this.launcher.toString(); 108 } 109 110 /** 111 * Handler an error in the job driver. 112 */ 113 protected final class SilentRuntimeErrorHandler implements EventHandler<FailedRuntime> { 114 @Override 115 public void onNext(final FailedRuntime error) { 116 LOG.log(Level.INFO, "Received a runtime error: {0}", error); 117 launcher.setStatusAndNotify(LauncherStatus.failed(error.getReason())); 118 } 119 } 120 121 /** 122 * Job driver notifies us that the job had failed. 123 */ 124 protected final class SilentFailedTestJobHandler implements EventHandler<FailedJob> { 125 @Override 126 public void onNext(final FailedJob job) { 127 final Optional<Throwable> ex = job.getReason(); 128 LOG.log(Level.INFO, "Received an error for job {0}: {1}", new Object[]{job.getId(), ex}); 129 launcher.setStatusAndNotify(LauncherStatus.failed(ex)); 130 } 131 } 132}