Commit e2fad306 authored by aninhacostaribeiro's avatar aninhacostaribeiro
Browse files

Changed the strategy of wakeUpSleepingHosts

parent b8e64149
......@@ -2,9 +2,8 @@ package org.fogbowcloud.green.server.communication;
import java.io.IOException;
import java.util.Properties;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.apache.log4j.Logger;
import org.fogbowcloud.green.server.core.greenStrategy.GreenStrategy;
import org.jamppa.component.XMPPComponent;
import org.xmpp.packet.IQ;
......@@ -12,6 +11,7 @@ import org.xmpp.packet.IQ.Type;
public class ServerCommunicationComponent extends XMPPComponent {
private static final Logger LOGGER = Logger.getLogger(ServerCommunicationComponent.class);
private GreenStrategy gs;
public ServerCommunicationComponent(Properties prop, GreenStrategy gs) {
......@@ -27,14 +27,13 @@ public class ServerCommunicationComponent extends XMPPComponent {
ProcessBuilder pb = new ProcessBuilder("powerwake", macAddress);
pb.start();
} catch (IOException e) {
Logger logger = Logger.getLogger("green.server");
logger.log(Level.WARNING, "It was not possible to wake " + macAddress);
LOGGER.warn("It was not possible to wake " + macAddress + e);
}
}
public void sendIdleHostToBed(String host) {
public void sendIdleHostToBed(String hostJID) {
IQ iq = new IQ(Type.set);
iq.setTo("asdas");
iq.setTo(hostJID);
iq.getElement().addElement("query", "org.fogbowcloud.green.GoToBed");
send(iq);
}
......
package org.fogbowcloud.green.server.communication;
import org.dom4j.Element;
import org.fogbowcloud.green.server.core.greenStrategy.GreenStrategy;
import org.jamppa.component.handler.AbstractQueryHandler;
import org.xmpp.packet.IQ;
......@@ -17,8 +18,9 @@ public class WakeUpRequestHandler extends AbstractQueryHandler {
@Override
public IQ handle(IQ query) {
//expected content format: the minimum CPU and the RAM (in GigaBytes) capacity required (eg. "1 8")
int minCPU = Integer.parseInt(query.getElement().element("query").elementText("cpu"));
int minRAM = Integer.parseInt(query.getElement().element("query").elementText("ram"));
Element queryElement = query.getElement().element("query");
int minCPU = Integer.parseInt(queryElement.elementText("minCPU"));
int minRAM = Integer.parseInt(queryElement.elementText("minRAM"));
gs.wakeUpSleepingHost(minCPU, minRAM);
......
package org.fogbowcloud.green.server.core.greenStrategy;
import java.util.Collections;
import java.util.Date;
import java.util.LinkedList;
import java.util.List;
import java.util.PriorityQueue;
import java.util.Properties;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
......@@ -21,8 +21,10 @@ public class DefaultGreenStrategy implements GreenStrategy {
private CloudInfoPlugin openStackPlugin;
private List<? extends Host> allWakedHosts;
private List<Host> lostHosts = new LinkedList<Host>();
private List<Host> nappingHosts = new LinkedList<Host>();
private List<Host> sleepingHosts = 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*/
private List<Host> hostsInGracePeriod = new LinkedList<Host>();
private PriorityQueue<Host> sleepingHosts = new PriorityQueue<Host>();
private ServerCommunicationComponent scc;
private Date lastUpdatedTime;
......@@ -82,11 +84,11 @@ public class DefaultGreenStrategy implements GreenStrategy {
this.scc = gscc;
}
public List<Host> getNappingHosts() {
return nappingHosts;
public List<Host> getHostsInGracePeriod() {
return hostsInGracePeriod;
}
public List<Host> getSleepingHosts() {
public PriorityQueue<Host> getSleepingHosts() {
return sleepingHosts;
}
......@@ -113,8 +115,8 @@ public class DefaultGreenStrategy implements GreenStrategy {
if (! this.lostHosts.contains(host)) {
this.lostHosts.add(host);
}
if(this.nappingHosts.contains(host)){
this.nappingHosts.remove(host);
if(this.hostsInGracePeriod.contains(host)){
this.hostsInGracePeriod.remove(host);
}
if (this.sleepingHosts.contains(host)){
this.sleepingHosts.remove(host);
......@@ -166,9 +168,9 @@ public class DefaultGreenStrategy implements GreenStrategy {
for (Host host : this.allWakedHosts) {
if (host.isNovaEnable() && host.isNovaRunning()
&& (host.getRunningVM() == 0)) {
if (!this.getNappingHosts().contains(host)) {
if (!this.getHostsInGracePeriod().contains(host)) {
host.setNappingSince(this.lastUpdatedTime.getTime());
this.getNappingHosts().add(host);
this.getHostsInGracePeriod().add(host);
} else {
long nowTime = this.lastUpdatedTime.getTime();
if (!this.getSleepingHosts().contains(host)) {
......@@ -189,8 +191,8 @@ public class DefaultGreenStrategy implements GreenStrategy {
if (this.allWakedHosts.contains(host)){
this.allWakedHosts.remove(host);
}
if (this.nappingHosts.contains(host)) {
this.nappingHosts.remove(host);
if (this.hostsInGracePeriod.contains(host)) {
this.hostsInGracePeriod.remove(host);
}
}
}
......@@ -198,8 +200,8 @@ public class DefaultGreenStrategy implements GreenStrategy {
public void checkHostsLastSeen() {
for (Host host : this.allWakedHosts) {
if (this.lastUpdatedTime.getTime() - host.getLastSeen() > this.lostHostTime) {
if (this.nappingHosts.contains(host)) {
this.nappingHosts.remove(host);
if (this.hostsInGracePeriod.contains(host)) {
this.hostsInGracePeriod.remove(host);
}
this.lostHosts.add(host);
LOGGER.info("Host " + host.getName() + " was found");
......@@ -213,18 +215,17 @@ public class DefaultGreenStrategy implements GreenStrategy {
}
public void wakeUpSleepingHost(int minCPU, int minRAM) {
Collections.sort(this.sleepingHosts);
for (Host host : this.sleepingHosts) {
if (host.getAvailableCPU() >= minCPU) {
if (host.getAvailableRAM() >= minRAM) {
this.scc.wakeUpHost(host.getName());
this.sleepingHosts.remove(host);
return;
}
} else {
return;
}
Host host = this.sleepingHosts.peek();
if (host.getAvailableCPU() < minCPU) {
return;
}
if (host.getAvailableRAM() >= minRAM) {
this.scc.wakeUpHost(host.getName());
this.sleepingHosts.remove(host);
return;
}
}
public void start() {
......
......@@ -9,9 +9,9 @@ public class Host implements Comparable<Host> {
private long cloudUpdatedTime;
private int availableCPU;
private int availableRAM;
private String ip = "";
private String jid = "";
private String macAddress = "";
private String ip = null;
private String jid = null;
private String macAddress = null;
private long lastSeen = 0;
private long nappingSince = 0;
......
......@@ -97,7 +97,7 @@ public class TestDefaultGreenStrategy {
Date date = this.createDateMock(3600001);
dgs.setDate(date);
dgs.sendIdleHostsToBed();
Assert.assertEquals(1, dgs.getNappingHosts().size());
Assert.assertEquals(1, dgs.getHostsInGracePeriod().size());
}
@Test
......@@ -118,7 +118,7 @@ public class TestDefaultGreenStrategy {
h1.setNappingSince(1800000);
dgs.sendIdleHostsToBed();
Assert.assertEquals(1, dgs.getSleepingHosts().size());
Assert.assertEquals(0, dgs.getNappingHosts().size());
Assert.assertEquals(0, dgs.getHostsInGracePeriod().size());
}
@Test
......@@ -225,6 +225,9 @@ public class TestDefaultGreenStrategy {
mustWake.setNappingSince(1800000);
mustWake2.setNappingSince(1800000);
dgs.sendIdleHostsToBed();
Assert.assertEquals("wake", dgs.getSleepingHosts().peek().getName());
dgs.wakeUpSleepingHost(2, 4);
Assert.assertEquals(1, dgs.getSleepingHosts().size());
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment