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.examples.group.broadcast; 020 021import org.apache.reef.examples.group.bgd.operatornames.ControlMessageBroadcaster; 022import org.apache.reef.examples.group.bgd.parameters.AllCommunicationGroup; 023import org.apache.reef.examples.group.broadcast.parameters.ModelBroadcaster; 024import org.apache.reef.examples.group.broadcast.parameters.ModelReceiveAckReducer; 025import org.apache.reef.examples.group.utils.math.Vector; 026import org.apache.reef.io.network.group.api.operators.Broadcast; 027import org.apache.reef.io.network.group.api.operators.Reduce; 028import org.apache.reef.io.network.group.api.task.CommunicationGroupClient; 029import org.apache.reef.io.network.group.api.task.GroupCommClient; 030import org.apache.reef.task.Task; 031 032import javax.inject.Inject; 033 034/** 035 * Slave task for broadcast example. 036 */ 037public class SlaveTask implements Task { 038 private final CommunicationGroupClient communicationGroupClient; 039 private final Broadcast.Receiver<ControlMessages> controlMessageBroadcaster; 040 private final Broadcast.Receiver<Vector> modelBroadcaster; 041 private final Reduce.Sender<Boolean> modelReceiveAckReducer; 042 043 @Inject 044 public SlaveTask( 045 final GroupCommClient groupCommClient) { 046 this.communicationGroupClient = groupCommClient.getCommunicationGroup(AllCommunicationGroup.class); 047 this.controlMessageBroadcaster = communicationGroupClient.getBroadcastReceiver(ControlMessageBroadcaster.class); 048 this.modelBroadcaster = communicationGroupClient.getBroadcastReceiver(ModelBroadcaster.class); 049 this.modelReceiveAckReducer = communicationGroupClient.getReduceSender(ModelReceiveAckReducer.class); 050 } 051 052 @Override 053 public byte[] call(final byte[] memento) throws Exception { 054 boolean stop = false; 055 while (!stop) { 056 final ControlMessages controlMessage = controlMessageBroadcaster.receive(); 057 switch (controlMessage) { 058 case Stop: 059 stop = true; 060 break; 061 062 case ReceiveModel: 063 modelBroadcaster.receive(); 064 if (Math.random() < 0.1) { 065 throw new RuntimeException("Simulated Failure"); 066 } 067 modelReceiveAckReducer.send(true); 068 break; 069 070 default: 071 break; 072 } 073 } 074 return null; 075 } 076}