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.tests.messaging.task;
020
021import org.apache.reef.driver.client.JobMessageObserver;
022import org.apache.reef.driver.evaluator.AllocatedEvaluator;
023import org.apache.reef.driver.task.RunningTask;
024import org.apache.reef.driver.task.TaskConfiguration;
025import org.apache.reef.driver.task.TaskMessage;
026import org.apache.reef.tang.Configuration;
027import org.apache.reef.tang.annotations.Unit;
028import org.apache.reef.tests.library.exceptions.DriverSideFailure;
029import org.apache.reef.wake.EventHandler;
030import org.apache.reef.wake.remote.impl.ObjectSerializableCodec;
031import org.apache.reef.wake.time.Clock;
032import org.apache.reef.wake.time.event.Alarm;
033
034import javax.inject.Inject;
035import java.util.Arrays;
036import java.util.logging.Level;
037import java.util.logging.Logger;
038
039@Unit
040public final class TaskMessagingDriver {
041
042  private static final Logger LOG = Logger.getLogger(TaskMessagingDriver.class.getName());
043  private static final ObjectSerializableCodec<String> CODEC = new ObjectSerializableCodec<>();
044  private static final byte[] HELLO_STR = CODEC.encode("MESSAGE::HELLO");
045  private static final int DELAY = 1000; // send message to Task 1 sec. after TaskRuntime
046
047  private final transient JobMessageObserver client;
048  private final transient Clock clock;
049
050  @Inject
051  public TaskMessagingDriver(final JobMessageObserver client, final Clock clock) {
052    this.client = client;
053    this.clock = clock;
054  }
055
056  public final class EvaluatorAllocatedHandler implements EventHandler<AllocatedEvaluator> {
057
058    @Override
059    public void onNext(final AllocatedEvaluator eval) {
060      final String taskId = "Task_" + eval.getId();
061      LOG.log(Level.INFO, "Submit task: {0}", taskId);
062
063      final Configuration taskConfig = TaskConfiguration.CONF
064          .set(TaskConfiguration.IDENTIFIER, taskId)
065          .set(TaskConfiguration.TASK, TaskMessagingTask.class)
066          .set(TaskConfiguration.ON_MESSAGE, TaskMessagingTask.DriverMessageHandler.class)
067          .set(TaskConfiguration.ON_SEND_MESSAGE, TaskMessagingTask.class)
068          .build();
069      eval.submitTask(taskConfig);
070    }
071  }
072
073  public final class TaskRunningHandler implements EventHandler<RunningTask> {
074    @Override
075    public void onNext(final RunningTask task) {
076      LOG.log(Level.FINE, "TaskRuntime: {0}", task.getId());
077      clock.scheduleAlarm(DELAY, new EventHandler<Alarm>() {
078        @Override
079        public void onNext(final Alarm alarm) {
080          task.send(HELLO_STR);
081        }
082      });
083    }
084  }
085
086  public final class TaskMessageHandler implements EventHandler<TaskMessage> {
087    @Override
088    public void onNext(final TaskMessage msg) {
089      LOG.log(Level.FINE, "TaskMessage: from {0}: {1}",
090          new Object[]{msg.getId(), CODEC.decode(msg.get())});
091      if (!Arrays.equals(msg.get(), HELLO_STR)) {
092        final RuntimeException ex = new DriverSideFailure("Unexpected message: " + CODEC.decode(msg.get()));
093        LOG.log(Level.SEVERE, "Bad message from " + msg.getId(), ex);
094        throw ex;
095      }
096    }
097  }
098}