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/**
041 * Driver which fails on FailedTask event.
042 */
043@Unit
044public final class DriverFailOnFail {
045
046  private static final Logger LOG = Logger.getLogger(DriverFailOnFail.class.getName());
047
048  private final transient JobMessageObserver client;
049  private final transient EvaluatorRequestor requestor;
050
051  @Inject
052  public DriverFailOnFail(final JobMessageObserver client, final EvaluatorRequestor requestor) {
053    this.client = client;
054    this.requestor = requestor;
055  }
056
057  /**
058   * Handler for AllocatedEvaluator.
059   */
060  public final class AllocatedEvaluatorHandler implements EventHandler<AllocatedEvaluator> {
061    @Override
062    public void onNext(final AllocatedEvaluator eval) {
063
064      try {
065
066        LOG.log(Level.INFO, "Submit task: Fail2");
067
068        final Configuration contextConfig = ContextConfiguration.CONF
069            .set(ContextConfiguration.IDENTIFIER, "Fail2")
070            .build();
071
072        final Configuration taskConfig = TaskConfiguration.CONF
073            .set(TaskConfiguration.IDENTIFIER, "Fail2")
074            .set(TaskConfiguration.TASK, FailTaskCall.class)
075            .build();
076
077        eval.submitContextAndTask(contextConfig, taskConfig);
078
079      } catch (final BindException ex) {
080        LOG.log(Level.WARNING, "Configuration error", ex);
081        throw new RuntimeException(ex);
082      }
083    }
084  }
085
086  /**
087   * Handler for FailedTask.
088   */
089  public final class FailedTaskHandler implements EventHandler<FailedTask> {
090    @Override
091    public void onNext(final FailedTask task) throws SimulatedDriverFailure {
092      final SimulatedDriverFailure error = new SimulatedDriverFailure(
093          "Simulated Failure at DriverFailOnFail :: " + task.getClass().getName(), task.asError());
094      LOG.log(Level.INFO, "Simulated Failure: {0}", error);
095      throw error;
096    }
097  }
098
099  /**
100   * Handler for StartTime.
101   */
102  public final class StartHandler implements EventHandler<StartTime> {
103    @Override
104    public void onNext(final StartTime time) {
105      LOG.log(Level.INFO, "StartTime: {0}", time);
106      DriverFailOnFail.this.requestor.submit(EvaluatorRequest.newBuilder()
107          .setNumber(1).setMemory(128).setNumberOfCores(1).build());
108    }
109  }
110}