This project has retired. For details please refer to its Attic page.
Source code
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.poison.context;
020
021import org.apache.reef.evaluator.context.events.ContextStart;
022import org.apache.reef.poison.PoisonException;
023import org.apache.reef.poison.PoisonedAlarmHandler;
024import org.apache.reef.poison.params.CrashProbability;
025import org.apache.reef.poison.params.CrashTimeout;
026import org.apache.reef.tang.annotations.Parameter;
027import org.apache.reef.wake.EventHandler;
028import org.apache.reef.wake.time.Clock;
029
030import javax.inject.Inject;
031import java.util.Random;
032import java.util.logging.Level;
033import java.util.logging.Logger;
034
035public final class PoisonedContextStartHandler implements EventHandler<ContextStart> {
036
037  private static final Logger LOG = Logger.getLogger(PoisonedContextStartHandler.class.getName());
038
039  private final Random random = new Random();
040
041  private final double crashProbability;
042  private final int timeOut;
043  private final Clock clock;
044
045  @Inject
046  public PoisonedContextStartHandler(
047      final @Parameter(CrashProbability.class) double crashProbability,
048      final @Parameter(CrashTimeout.class) int timeOut,
049      final Clock clock) {
050
051    this.crashProbability = crashProbability;
052    this.timeOut = timeOut;
053    this.clock = clock;
054  }
055
056  @Override
057  public void onNext(final ContextStart contextStart) {
058
059    LOG.log(Level.INFO, "Starting Context poison injector with prescribed dose: {0} units",
060        this.crashProbability);
061
062    if (this.random.nextDouble() <= this.crashProbability) {
063
064      final int timeToCrash = this.random.nextInt(this.timeOut) * 1000;
065      LOG.log(Level.INFO, "Dosage lethal! Crashing in {0} msec.", timeToCrash);
066
067      if (timeToCrash == 0) {
068        throw new PoisonException("Crashed at: " + System.currentTimeMillis());
069      } else {
070        this.clock.scheduleAlarm(timeToCrash, new PoisonedAlarmHandler());
071      }
072
073    } else {
074      LOG.info("Dosage not lethal");
075    }
076  }
077}