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/** 040 * Driver for TaskMessagingTest. 041 */ 042@Unit 043public final class TaskMessagingDriver { 044 045 private static final Logger LOG = Logger.getLogger(TaskMessagingDriver.class.getName()); 046 private static final ObjectSerializableCodec<String> CODEC = new ObjectSerializableCodec<>(); 047 private static final byte[] HELLO_STR = CODEC.encode("MESSAGE::HELLO"); 048 private static final int DELAY = 1000; // send message to Task 1 sec. after TaskRuntime 049 050 private final transient JobMessageObserver client; 051 private final transient Clock clock; 052 053 @Inject 054 public TaskMessagingDriver(final JobMessageObserver client, final Clock clock) { 055 this.client = client; 056 this.clock = clock; 057 } 058 059 /** 060 * Handler for AllocatedEvaluator. 061 */ 062 public final class EvaluatorAllocatedHandler implements EventHandler<AllocatedEvaluator> { 063 064 @Override 065 public void onNext(final AllocatedEvaluator eval) { 066 final String taskId = "Task_" + eval.getId(); 067 LOG.log(Level.INFO, "Submit task: {0}", taskId); 068 069 final Configuration taskConfig = TaskConfiguration.CONF 070 .set(TaskConfiguration.IDENTIFIER, taskId) 071 .set(TaskConfiguration.TASK, TaskMessagingTask.class) 072 .set(TaskConfiguration.ON_MESSAGE, TaskMessagingTask.DriverMessageHandler.class) 073 .set(TaskConfiguration.ON_SEND_MESSAGE, TaskMessagingTask.class) 074 .build(); 075 eval.submitTask(taskConfig); 076 } 077 } 078 079 /** 080 * Handler for RunningTask. 081 */ 082 public final class TaskRunningHandler implements EventHandler<RunningTask> { 083 @Override 084 public void onNext(final RunningTask task) { 085 LOG.log(Level.FINE, "TaskRuntime: {0}", task.getId()); 086 clock.scheduleAlarm(DELAY, new EventHandler<Alarm>() { 087 @Override 088 public void onNext(final Alarm alarm) { 089 task.send(HELLO_STR); 090 } 091 }); 092 } 093 } 094 095 /** 096 * Handler for TaskMessage. 097 */ 098 public final class TaskMessageHandler implements EventHandler<TaskMessage> { 099 @Override 100 public void onNext(final TaskMessage msg) { 101 LOG.log(Level.FINE, "TaskMessage: from {0}: {1}", 102 new Object[]{msg.getId(), CODEC.decode(msg.get())}); 103 if (!Arrays.equals(msg.get(), HELLO_STR)) { 104 final RuntimeException ex = new DriverSideFailure("Unexpected message: " + CODEC.decode(msg.get())); 105 LOG.log(Level.SEVERE, "Bad message from " + msg.getId(), ex); 106 throw ex; 107 } 108 } 109 } 110}