com.eduworks.mapreduce.MapReduceManager Maven / Gradle / Ivy
package com.eduworks.mapreduce;
import java.rmi.AlreadyBoundException;
import java.rmi.NotBoundException;
import java.rmi.RemoteException;
import java.rmi.registry.LocateRegistry;
import java.rmi.registry.Registry;
import java.rmi.server.ExportException;
import java.rmi.server.UnicastRemoteObject;
import java.util.Collection;
import java.util.Collections;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map.Entry;
import org.apache.log4j.Logger;
import com.eduworks.lang.EwList;
import com.eduworks.lang.threading.EwThreading;
import com.eduworks.lang.threading.EwThreading.MyRunnable;
import com.eduworks.util.Tuple;
public class MapReduceManager
{
public Logger log = Logger.getLogger(MapReduceManager.class);
public List peers = Collections.synchronizedList(new EwList());
public MapReduceTransport shipping;
public boolean isChecking = false;
public boolean debug = true;
public long lastServiceMs;
private Short port;
private String name;
public void cleanup()
{
try
{
Registry registry = LocateRegistry.getRegistry(port);
try
{
registry.unbind(name);
}
catch (NotBoundException e)
{
e.printStackTrace();
}
UnicastRemoteObject.unexportObject(shipping, true);
}
catch (Exception e)
{
}
}
public MapReduceManager(String name, Short port, List> hosts, MapReduceListener myListener)
throws RemoteException, AlreadyBoundException
{
peers.clear();
this.port = port;
this.name = name;
try
{
myListener = (MapReduceListener) UnicastRemoteObject.exportObject(myListener, 0);
}
catch (Exception e)
{
}
this.shipping = myListener;
try
{
if (debug)
log.info("Binding to port " + port);
Registry registry = LocateRegistry.createRegistry(port);
registry.rebind(name, myListener);
if (debug)
log.info("Binding complete.");
}
catch (ExportException ex)
{
log.info("Already bound.");
}
catch (Exception e)
{
e.printStackTrace();
}
int i = 0;
for (Tuple host : hosts)
{
MapReduceStatus status = new MapReduceStatus();
status.setHost(host.getFirst(), host.getSecond());
status.setState(MapReduceStatus.STATE.IN_QUESTION);
status.setI(i++);
status.setServiceName(name);
peers.add(status);
if (debug)
log.info("Established Peer: " + status);
}
}
private void invokeCheckup()
{
// if (isChecking == true)
// return;
// isChecking = true;
// EwThreading.invokeLater(new Runnable()
// {
// public void run()
// {
checkup();
// isChecking = false;
// }
// });
}
public void checkup()
{
synchronized (peers)
{
for (MapReduceStatus s : peers)
{
if (!s.notOK())
continue;
try
{
s.getInterface().ping();
s.setState(MapReduceStatus.STATE.OK);
if (debug)
log.info("Established connection to: " + s);
}
catch (RemoteException e)
{
s.setState(MapReduceStatus.STATE.FAILED);
if (debug)
log.info("Lost connection with: " + s);
}
catch (NotBoundException e)
{
s.setState(MapReduceStatus.STATE.FAILED);
if (debug)
log.info("Lost connection with: " + s);
}
}
}
}
public List