Commit ae7ecbf8 authored by aninhacostaribeiro's avatar aninhacostaribeiro
Browse files

Updated

parent 26a87c08
......@@ -10,6 +10,5 @@ openstack.username = fogbow
openstack.password = itIsASecret
openstack.tenant = fogbow-project
greenstrategy.sleeptime = 1800000
greenstrategy.gracetime = 1800000
greenstrategy.lostAgentTime = 1500000
greenstrategy.gracetime = 1800
greenstrategy.expirationtime = 300
......@@ -10,16 +10,18 @@ import org.dom4j.tree.DefaultElement;
import org.jamppa.client.XMPPClient;
import org.jamppa.client.plugin.xep0077.XEP0077;
import org.jivesoftware.smack.PacketListener;
import org.jivesoftware.smack.XMPPException;
import org.jivesoftware.smack.filter.PacketFilter;
import org.xmpp.packet.IQ;
import org.xmpp.packet.JID;
import org.xmpp.packet.Packet;
import org.xmpp.packet.IQ.Type;
public class AgentCommunicationComponent {
private static final String NAMESPACE = "org.fogbowcloud.green.IAmAlive";
private static final Logger LOGGER = Logger.getLogger(AgentCommunicationComponent.class);
private static final Logger LOGGER = Logger
.getLogger(AgentCommunicationComponent.class);
private ScheduledExecutorService executor = Executors
.newScheduledThreadPool(1);
......@@ -28,15 +30,16 @@ public class AgentCommunicationComponent {
private XMPPClient client;
public AgentCommunicationComponent(Properties prop) {
try{
this.prop = prop;
this.client = new XMPPClient(this.prop.getProperty("xmpp.jid"),
this.prop.getProperty("xmpp.password"),
this.prop.getProperty("xmpp.host"), Integer.parseInt(this.prop
.getProperty("xmpp.port")));
this.sleepingTime = Long.parseLong(this.prop.getProperty("green.sleepingTime"));
}catch (Exception e){
LOGGER.fatal("The configuration file is not correct" + e);
try {
this.prop = prop;
this.client = new XMPPClient(this.prop.getProperty("xmpp.jid"),
this.prop.getProperty("xmpp.password"),
this.prop.getProperty("xmpp.host"),
Integer.parseInt(this.prop.getProperty("xmpp.port")));
this.sleepingTime = Long.parseLong(this.prop
.getProperty("green.sleepingTime"));
} catch (Exception e) {
LOGGER.fatal("The configuration file is not correct", e);
}
}
......@@ -45,16 +48,24 @@ public class AgentCommunicationComponent {
try {
this.client.registerPlugin(register);
this.client.connect();
} catch (Exception e) {
LOGGER.fatal("It was not possible to connect to server", e);
return false;
}
try {
register.createAccount(this.prop.getProperty("xmpp.jid"),
this.prop.getProperty("xmpp.password"));
} catch (XMPPException e) {
LOGGER.fatal("It was not possible to create the account", e);
}
try {
this.client.login();
this.client.process(false);
LOGGER.info("connected to the server");
} catch (Exception e) {
LOGGER.fatal("It was not possible to connect to server " + e);
} catch (XMPPException e) {
LOGGER.fatal("It was not possible to login", e);
return false;
}
this.client.process(false);
LOGGER.info("connected to the server");
client.getConnection().addPacketListener(new PacketListener() {
@Override
public void processPacket(Packet packet) {
......@@ -63,15 +74,21 @@ public class AgentCommunicationComponent {
}, new PacketFilter() {
@Override
public boolean accept(Packet packet) {
if (!packet.getFrom().toString()
.equals(prop.getProperty("xmpp.component"))) {
JID from = packet.getFrom();
if (from == null) {
return false;
}
String ns = packet.getElement().element("query")
.getNamespaceURI();
if (!ns.equals("org.fogbowcloud.green.GoToBed")) {
if (!from.toString().equals(prop.getProperty("xmpp.component"))) {
return false;
}
try {
String ns = packet.getElement().element("query")
.getNamespaceURI();
if (!ns.equals("org.fogbowcloud.green.GoToBed")) {
return false;
}
} catch (Exception e) {
LOGGER.info("There not a query element in the packet", e);
return false;
}
......@@ -81,7 +98,7 @@ public class AgentCommunicationComponent {
return true;
}
public void sendIamAliveSignal() {
public void sendIamAliveSignal() {
IQ iq = new IQ(Type.get);
iq.setTo(this.prop.getProperty("xmpp.component"));
iq.getElement().addElement("query", NAMESPACE);
......@@ -93,8 +110,9 @@ public class AgentCommunicationComponent {
this.prop.getProperty("host.macAddress"));
query.addElement("hostName")
.setText(this.prop.getProperty("host.name"));
LOGGER.info("Sent I am alive signal");
}
public void start() {
executor.scheduleWithFixedDelay(new Runnable() {
@Override
......
package org.fogbowcloud.green.server.core.greenStrategy;
import java.io.IOException;
import java.util.Date;
import java.util.LinkedList;
import java.util.List;
import java.util.PriorityQueue;
......@@ -21,8 +20,7 @@ public class DefaultGreenStrategy implements GreenStrategy {
.getLogger(DefaultGreenStrategy.class);
private CloudInfoPlugin openStackPlugin;
private List<? extends Host> allWakedHosts;
private List<Host> lostHosts = new LinkedList<Host>();
private List<Host> hostsAwake = new LinkedList<Host>();
/*
* The List hosts in grace period is used to keep those hosts which are not
* been used by the system but not for too long
......@@ -31,15 +29,12 @@ public class DefaultGreenStrategy implements GreenStrategy {
private PriorityQueue<Host> sleepingHosts = new PriorityQueue<Host>();
private ServerCommunicationComponent scc;
private Date lastUpdatedTime;
private DateWrapper dateWrapper;
private long graceTime;
private long sleepingTime;
private long lostHostTime;
private long expirationTime;
private ScheduledExecutorService executorSendIdleHostsToBed = Executors
.newScheduledThreadPool(1);
private ScheduledExecutorService executorVerifyLastTimeSeen = Executors
private ScheduledExecutorService executorService = Executors
.newScheduledThreadPool(1);
public DefaultGreenStrategy(Properties greenProperties) {
......@@ -48,14 +43,11 @@ public class DefaultGreenStrategy implements GreenStrategy {
.getProperty("openstack.username").toString(), greenProperties
.get("openstack.password").toString(), greenProperties
.getProperty("openstack.tenant").toString());
this.lastUpdatedTime = new Date();
this.sleepingTime = Long.parseLong(greenProperties
.getProperty("greenstrategy.sleeptime"));
this.dateWrapper = new DateWrapper();
this.graceTime = Long.parseLong(greenProperties
.getProperty("greenstrategy.gracetime"));
this.lostHostTime = Long.parseLong(greenProperties
.getProperty("greenstrategy.lostAgentTime"));
this.allWakedHosts = this.openStackPlugin.getHostInformation();
.getProperty("greenstrategy.gracetime")) * 1000;
this.expirationTime = Long.parseLong(greenProperties
.getProperty("greenstrategy.expirationtime")) * 1000;
}
/*
......@@ -65,23 +57,18 @@ public class DefaultGreenStrategy implements GreenStrategy {
long graceTime) {
this.openStackPlugin = openStackPlugin;
this.graceTime = graceTime;
this.allWakedHosts = this.openStackPlugin.getHostInformation();
}
protected void setLostHostTime(long lostHostTime) {
this.lostHostTime = lostHostTime;
protected void setExpirationTime(long lostHostTime) {
this.expirationTime = lostHostTime;
}
protected void setAllHosts(List<Host> hosts) {
this.allWakedHosts = hosts;
}
protected void setLostHosts(List<Host> hosts) {
this.lostHosts = hosts;
this.hostsAwake = hosts;
}
protected void setDate(Date date) {
this.lastUpdatedTime = date;
protected void setDateWrapper(DateWrapper dateWrapper) {
this.dateWrapper = dateWrapper;
}
public void setCommunicationComponent(ServerCommunicationComponent gscc) {
......@@ -96,86 +83,69 @@ public class DefaultGreenStrategy implements GreenStrategy {
return sleepingHosts;
}
public List<? extends Host> getAllWakedHosts() {
return allWakedHosts;
}
public List<Host> getLostHosts() {
return lostHosts;
public List<? extends Host> getHostsAwake() {
return hostsAwake;
}
protected void updateAllHosts() {
List<Host> nowHosts = new LinkedList<Host>();
nowHosts.addAll(this.allWakedHosts);
this.allWakedHosts = this.openStackPlugin.getHostInformation();
LOGGER.info("Updating host info at the local cloud...");
List<? extends Host> cloudInfo = this.openStackPlugin.getHostInformation();
/*
* Solution for eliminating hosts that don't send an "I am alive signal"
* but still are in the cloud information
* Solution for not loosing data when it is updated
*/
for (Host host : this.allWakedHosts) {
if (!nowHosts.contains(host)) {
this.allWakedHosts.remove(host);
if (!this.lostHosts.contains(host)) {
this.lostHosts.add(host);
}
if (this.hostsInGracePeriod.contains(host)) {
this.hostsInGracePeriod.remove(host);
}
if (this.sleepingHosts.contains(host)) {
this.sleepingHosts.remove(host);
for (Host hostCloudInfo : cloudInfo) {
for (Host host : this.hostsAwake) {
if (host.getName().equals(hostCloudInfo.getName())) {
host.setAvailableCPU(hostCloudInfo.getAvailableCPU());
host.setAvailableRAM(hostCloudInfo.getAvailableRAM());
host.setRunningVM(hostCloudInfo.getRunningVM());
host.setNovaRunning(hostCloudInfo.isNovaRunning());
host.setNovaEnable(hostCloudInfo.isNovaEnable());
host.setCloudUpdatedTime(dateWrapper.getTime());
}
}
}
/*
* Solution for not loosing data when it is updated
*/
for (Host host : this.allWakedHosts) {
Host fullHost = nowHosts.get(nowHosts.indexOf(host));
host.setIp(fullHost.getIp());
host.setJid(fullHost.getJid());
host.setMacAddress(fullHost.getMacAddress());
host.setNappingSince(fullHost.getNappingSince());
host.setLastSeen(fullHost.getLastSeen());
}
}
public void receiveIamAliveInfo(String hostName, String jid, String ip,
String macAddress) {
for (Host host : this.lostHosts) {
if (this.lostHosts.contains(host)) {
this.lostHosts.remove(host);
LinkedList<Host> aux = new LinkedList<Host>();
aux.addAll(this.allWakedHosts);
aux.add(host);
this.allWakedHosts = aux;
LOGGER.info("Host " + host.getName() + " was found");
}
}
for (Host host : this.allWakedHosts) {
LOGGER.info("Received IAmAlive from " + hostName);
Host hostToUpdate = null;
for (Host host : this.hostsAwake) {
if (host.getName().equals(hostName)) {
host.setJid(jid);
host.setIp(ip);
host.setMacAddress(macAddress);
host.setLastSeen(lastUpdatedTime.getTime());
hostToUpdate = host;
}
}
if (hostToUpdate == null) {
hostToUpdate = new Host(hostName);
this.hostsAwake.add(hostToUpdate);
}
hostToUpdate.setJid(jid);
hostToUpdate.setIp(ip);
hostToUpdate.setMacAddress(macAddress);
hostToUpdate.setLastSeen(dateWrapper.getTime());
}
public void sendIdleHostsToBed() {
this.updateAllHosts();
for (Host host : this.allWakedHosts) {
LOGGER.info("Will send idle hosts to bed. Hosts' status: "
+ this.hostsAwake);
for (Host host : this.hostsAwake) {
if (host.isNovaEnable() && host.isNovaRunning()
&& (host.getRunningVM() == 0)) {
if (!this.getHostsInGracePeriod().contains(host)) {
host.setNappingSince(this.lastUpdatedTime.getTime());
this.getHostsInGracePeriod().add(host);
host.setNappingSince(this.dateWrapper.getTime());
LOGGER.info("Host " + host.getName() + " in grace period");
this.hostsInGracePeriod.add(host);
} else {
long nowTime = this.lastUpdatedTime.getTime();
long nowTime = this.dateWrapper.getTime();
if (!this.getSleepingHosts().contains(host)) {
/*
* if there is more than a half hour that the host is
......@@ -183,38 +153,31 @@ public class DefaultGreenStrategy implements GreenStrategy {
*/
if (nowTime - host.getNappingSince() > this.graceTime) {
scc.sendIdleHostToBed(host.getMacAddress());
LOGGER.info("Host " + host.getName()
+ " was sent to bed");
LOGGER.info("Host " + host.getName() + " was sent to bed");
this.sleepingHosts.add(host);
}
}
}
}
}
for (Host host : sleepingHosts) {
if (this.allWakedHosts.contains(host)) {
this.allWakedHosts.remove(host);
}
if (this.hostsInGracePeriod.contains(host)) {
this.hostsInGracePeriod.remove(host);
}
this.hostsAwake.remove(host);
this.hostsInGracePeriod.remove(host);
}
}
public void checkHostsLastSeen() {
for (Host host : this.allWakedHosts) {
if (this.lastUpdatedTime.getTime() - host.getLastSeen() > this.lostHostTime) {
if (this.hostsInGracePeriod.contains(host)) {
this.hostsInGracePeriod.remove(host);
}
this.lostHosts.add(host);
LOGGER.info("Host " + host.getName() + " was found");
public void checkExpiredHosts() {
List<Host> hostsToRemove = new LinkedList<Host>();
for (Host host : this.hostsAwake) {
if (this.dateWrapper.getTime() - host.getLastSeen() > this.expirationTime) {
hostsToRemove.add(host);
}
}
for (Host host : this.lostHosts) {
if (this.allWakedHosts.contains(host)) {
this.allWakedHosts.remove(host);
}
for (Host hostToRemove : hostsToRemove) {
LOGGER.info(hostToRemove.getJid() + " has expired.");
this.hostsAwake.remove(hostToRemove);
this.hostsInGracePeriod.remove(hostToRemove);
}
}
......@@ -238,22 +201,16 @@ public class DefaultGreenStrategy implements GreenStrategy {
}
public void start() {
executorVerifyLastTimeSeen.scheduleWithFixedDelay(new Runnable() {
@Override
public void run() {
checkHostsLastSeen();
LOGGER.info("Checked when hosts were seen"
+ " Lost Hosts now");
}
}, 0, lostHostTime, TimeUnit.MILLISECONDS);
executorSendIdleHostsToBed.scheduleWithFixedDelay(new Runnable() {
executorService.scheduleWithFixedDelay(new Runnable() {
@Override
public void run() {
sendIdleHostsToBed();
LOGGER.info("Sent idle hosts to bed: "
+ "Sleeping Hosts now "+sleepingHosts);
try {
checkExpiredHosts();
sendIdleHostsToBed();
} catch (Exception e) {
LOGGER.warn("Exception thrown at the main thread", e);
}
}
}, 0, sleepingTime, TimeUnit.MILLISECONDS);
}, 0, 1, TimeUnit.MINUTES);
}
}
\ No newline at end of file
......@@ -3,12 +3,16 @@ package org.fogbowcloud.green.server.core.greenStrategy;
public class Host implements Comparable<Host> {
private String name;
// Cloud related attributes
private int runningVM;
private boolean novaRunning;
private boolean novaEnable;
private long cloudUpdatedTime;
private int availableCPU;
private int availableRAM;
// Green agent related attributes
private String ip = null;
private String jid = null;
private String macAddress = null;
......@@ -27,6 +31,10 @@ public class Host implements Comparable<Host> {
this.availableRAM = availableRAM;
}
public Host(String name) {
this.name = name;
}
public int getAvailableCPU() {
return availableCPU;
}
......@@ -50,6 +58,10 @@ public class Host implements Comparable<Host> {
public boolean isNovaEnable() {
return novaEnable;
}
public void setNovaEnable(boolean novaEnable) {
this.novaEnable = novaEnable;
}
public boolean isNovaRunning() {
return novaRunning;
......
package org.fogbowcloud.green.server.core.greenStrategy;
import java.io.IOException;
import java.util.Date;
import java.util.LinkedList;
import java.util.List;
import org.fogbowcloud.green.server.communication.ServerCommunicationComponent;
import org.fogbowcloud.green.server.core.greenStrategy.DefaultGreenStrategy;
import org.fogbowcloud.green.server.core.greenStrategy.Host;
import org.fogbowcloud.green.server.core.plugins.openstack.OpenStackInfoPlugin;
import org.junit.Assert;
import org.junit.Test;
......@@ -21,8 +18,8 @@ public class TestDefaultGreenStrategy {
return osip;
}
private Date createDateMock(long Time) {
Date date = Mockito.mock(Date.class);
private DateWrapper createDateMock(long Time) {
DateWrapper date = Mockito.mock(DateWrapper.class);
Mockito.when(date.getTime()).thenReturn(Time);
return date;
}
......@@ -32,17 +29,22 @@ public class TestDefaultGreenStrategy {
Host lost = new Host("lost", 0, true, true, 0, 0, 0);
lost.setLastSeen(0);
Host still = new Host("still", 0, true, true, 0, 0, 0);
still.setLastSeen(1500000);
List<Host> hosts = new LinkedList<Host>();
hosts.add(lost);
hosts.add(still);
OpenStackInfoPlugin osip = this.createOpenStackInfoPluginMock(hosts);
DefaultGreenStrategy dgs = new DefaultGreenStrategy(osip, 1800000);
Date date = this.createDateMock(1500001);
dgs.setDate(date);
dgs.setLostHostTime(1500000);
dgs.checkHostsLastSeen();
Assert.assertEquals(1, dgs.getAllWakedHosts().size());
DateWrapper date = this.createDateMock(0);
dgs.setDateWrapper(date);
dgs.receiveIamAliveInfo("lost", "lost@test.com", "123.456.789.10", "A1:B2:C3:D4:E5:67");
dgs.receiveIamAliveInfo("still", "still@test.com", "123.456.789.10", "A1:B2:C3:D4:E5:67");
dgs.setExpirationTime(1500000);
dgs.checkExpiredHosts();
DateWrapper newDate = this.createDateMock(1500001);
dgs.setDateWrapper(newDate);
dgs.receiveIamAliveInfo("still", "still@test.com", "123.456.789.10", "A1:B2:C3:D4:E5:67");
dgs.checkExpiredHosts();
Assert.assertEquals(1, dgs.getHostsAwake().size());
}
@Test
......@@ -52,14 +54,12 @@ public class TestDefaultGreenStrategy {
hosts.add(toBeFound);
OpenStackInfoPlugin osip = this.createOpenStackInfoPluginMock(hosts);
DefaultGreenStrategy dgs = new DefaultGreenStrategy(osip, 1800000);
Date date = this.createDateMock(1500001);
dgs.setDate(date);
dgs.setLostHosts(hosts);
dgs.receiveIamAliveInfo("found", "test@test.com", "123.456.789",
" A1:B2:C3:D4:E5:67");
Assert.assertEquals(0, dgs.getLostHosts().size());
Assert.assertEquals(1, dgs.getAllWakedHosts().size());
Assert.assertEquals("test@test.com", dgs.getAllWakedHosts().get(0)
DateWrapper date = this.createDateMock(1500001);
dgs.setDateWrapper(date);
dgs.receiveIamAliveInfo("found", "test@test.com", "123.456.789.10",
"A1:B2:C3:D4:E5:67");
Assert.assertEquals(1, dgs.getHostsAwake().size());
Assert.assertEquals("test@test.com", dgs.getHostsAwake().get(0)
.getJid());
}
......@@ -81,10 +81,12 @@ public class TestDefaultGreenStrategy {
updatedHosts.add(upHost1);
updatedHosts.add(upHost2);
dgs.setAllHosts(updatedHosts);
DateWrapper newDate = this.createDateMock(1500001);
dgs.setDateWrapper(newDate);
dgs.updateAllHosts();
Assert.assertEquals("test2@test.com", dgs.getAllWakedHosts().get(0)
Assert.assertEquals("test2@test.com", dgs.getHostsAwake().get(1)
.getJid());
Assert.assertEquals("test1@test.com", dgs.getAllWakedHosts().get(1)
Assert.assertEquals("test1@test.com", dgs.getHostsAwake().get(0)
.getJid());
}
......@@ -95,8 +97,8 @@ public class TestDefaultGreenStrategy {
hosts.add(napping);
OpenStackInfoPlugin osip = this.createOpenStackInfoPluginMock(hosts);
DefaultGreenStrategy dgs = new DefaultGreenStrategy(osip, 1800000);
Date date = this.createDateMock(3600001);
dgs.setDate(date);
DateWrapper date = this.createDateMock(3600001);
dgs.setDateWrapper(date);
dgs.sendIdleHostsToBed();
Assert.assertEquals(1, dgs.getHostsInGracePeriod().size());
}
......@@ -107,18 +109,17 @@ public class TestDefaultGreenStrategy {
h1.setMacAddress("mac");
List<Host> hosts = new LinkedList<Host>();
hosts.add(h1);
Date date = this.createDateMock(3600001);
DateWrapper date = this.createDateMock(3600001);
ServerCommunicationComponent gscc = Mockito
.mock(ServerCommunicationComponent.class);
try {
Mockito.doNothing().when(gscc).wakeUpHost(h1.getMacAddress());