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.driver.client.JobMessageObserver; 022import org.apache.reef.driver.context.ContextConfiguration; 023import org.apache.reef.driver.evaluator.AllocatedEvaluator; 024import org.apache.reef.driver.evaluator.EvaluatorRequest; 025import org.apache.reef.driver.evaluator.EvaluatorRequestor; 026import org.apache.reef.driver.task.FailedTask; 027import org.apache.reef.driver.task.TaskConfiguration; 028import org.apache.reef.tang.Configuration; 029import org.apache.reef.tang.annotations.Unit; 030import org.apache.reef.tang.exceptions.BindException; 031import org.apache.reef.tests.fail.task.FailTaskCall; 032import org.apache.reef.tests.library.exceptions.SimulatedDriverFailure; 033import org.apache.reef.wake.EventHandler; 034import org.apache.reef.wake.time.event.StartTime; 035 036import javax.inject.Inject; 037import java.util.logging.Level; 038import java.util.logging.Logger; 039 040@Unit 041public final class DriverFailOnFail { 042 043 private static final Logger LOG = Logger.getLogger(DriverFailOnFail.class.getName()); 044 045 private final transient JobMessageObserver client; 046 private final transient EvaluatorRequestor requestor; 047 048 @Inject 049 public DriverFailOnFail(final JobMessageObserver client, final EvaluatorRequestor requestor) { 050 this.client = client; 051 this.requestor = requestor; 052 } 053 054 public final class AllocatedEvaluatorHandler implements EventHandler<AllocatedEvaluator> { 055 @Override 056 public void onNext(final AllocatedEvaluator eval) { 057 058 try { 059 060 LOG.log(Level.INFO, "Submit task: Fail2"); 061 062 final Configuration contextConfig = ContextConfiguration.CONF 063 .set(ContextConfiguration.IDENTIFIER, "Fail2") 064 .build(); 065 066 final Configuration taskConfig = TaskConfiguration.CONF 067 .set(TaskConfiguration.IDENTIFIER, "Fail2") 068 .set(TaskConfiguration.TASK, FailTaskCall.class) 069 .build(); 070 071 eval.submitContextAndTask(contextConfig, taskConfig); 072 073 } catch (final BindException ex) { 074 LOG.log(Level.WARNING, "Configuration error", ex); 075 throw new RuntimeException(ex); 076 } 077 } 078 } 079 080 public final class FailedTaskHandler implements EventHandler<FailedTask> { 081 @Override 082 public void onNext(final FailedTask task) throws SimulatedDriverFailure { 083 final SimulatedDriverFailure error = new SimulatedDriverFailure( 084 "Simulated Failure at DriverFailOnFail :: " + task.getClass().getName(), task.asError()); 085 LOG.log(Level.INFO, "Simulated Failure: {0}", error); 086 throw error; 087 } 088 } 089 090 public final class StartHandler implements EventHandler<StartTime> { 091 @Override 092 public void onNext(final StartTime time) { 093 LOG.log(Level.INFO, "StartTime: {0}", time); 094 DriverFailOnFail.this.requestor.submit(EvaluatorRequest.newBuilder() 095 .setNumber(1).setMemory(128).setNumberOfCores(1).build()); 096 } 097 } 098}