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.tang.annotations.Unit; 022import org.apache.reef.task.Task; 023import org.apache.reef.task.TaskMessage; 024import org.apache.reef.task.TaskMessageSource; 025import org.apache.reef.task.events.CloseEvent; 026import org.apache.reef.task.events.DriverMessage; 027import org.apache.reef.task.events.SuspendEvent; 028import org.apache.reef.task.events.TaskStop; 029import org.apache.reef.util.Optional; 030import org.apache.reef.wake.EventHandler; 031import org.apache.reef.wake.remote.impl.ObjectSerializableCodec; 032 033import javax.inject.Inject; 034import java.util.logging.Level; 035import java.util.logging.Logger; 036 037/** 038 * A basic task that quite successfully does nothing. 039 */ 040@Unit 041public final class NoopTask implements Task, TaskMessageSource { 042 043 private static final Logger LOG = Logger.getLogger(NoopTask.class.getName()); 044 private static final ObjectSerializableCodec<String> CODEC = new ObjectSerializableCodec<>(); 045 private static final TaskMessage INIT_MESSAGE = TaskMessage.from("", CODEC.encode("MESSAGE::INIT")); 046 private transient boolean isRunning = true; 047 private transient Optional<TaskMessage> message = Optional.empty(); 048 049 @Inject 050 public NoopTask() { 051 LOG.info("NoopTask created."); 052 } 053 054 @Override 055 public synchronized byte[] call(final byte[] memento) { 056 this.isRunning = true; 057 while (this.isRunning) { 058 try { 059 LOG.info("NoopTask.call(): Waiting for the message."); 060 this.wait(); 061 } catch (final InterruptedException ex) { 062 LOG.log(Level.WARNING, "NoopTask.wait() interrupted.", ex); 063 } 064 } 065 LOG.log(Level.INFO, "NoopTask.call(): Exiting with message {0}", 066 CODEC.decode(this.message.orElse(INIT_MESSAGE).get())); 067 return this.message.orElse(INIT_MESSAGE).get(); 068 } 069 070 @Override 071 public synchronized Optional<TaskMessage> getMessage() { 072 LOG.log(Level.INFO, "NoopTask.getMessage() invoked: {0}", 073 CODEC.decode(this.message.orElse(INIT_MESSAGE).get())); 074 return this.message; 075 } 076 077 private synchronized void stopTask() { 078 LOG.info("NoopTask.stopTask() invoked."); 079 this.isRunning = false; 080 this.notify(); 081 } 082 083 /** 084 * Handler for SuspendEvent. 085 */ 086 public class TaskSuspendHandler implements EventHandler<SuspendEvent> { 087 @Override 088 public void onNext(final SuspendEvent suspendEvent) { 089 LOG.info("NoopTask.TaskSuspendHandler.send() invoked."); 090 NoopTask.this.stopTask(); 091 } 092 } 093 094 /** 095 * Handler for TaskStop. 096 */ 097 public class TaskStopHandler implements EventHandler<TaskStop> { 098 @Override 099 public void onNext(final TaskStop event) { 100 LOG.info("NoopTask.TaskStopHandler.send() invoked."); 101 NoopTask.this.stopTask(); 102 } 103 } 104 105 /** 106 * Handler for CloseEvent. 107 */ 108 public class TaskCloseHandler implements EventHandler<CloseEvent> { 109 @Override 110 public void onNext(final CloseEvent closeEvent) { 111 LOG.info("NoopTask.TaskCloseHandler.send() invoked."); 112 NoopTask.this.stopTask(); 113 } 114 } 115 116 /** 117 * Handler for DriverMessage. 118 */ 119 public class DriverMessageHandler implements EventHandler<DriverMessage> { 120 @Override 121 public void onNext(final DriverMessage driverMessage) { 122 final byte[] msg = driverMessage.get().get(); 123 LOG.log(Level.INFO, "NoopTask.DriverMessageHandler.send() invoked: {0}", CODEC.decode(msg)); 124 synchronized (NoopTask.this) { 125 NoopTask.this.message = Optional.of(TaskMessage.from(NoopTask.this.toString(), msg)); 126 } 127 } 128 } 129}