Commit 576c29ba authored by aninhacostaribeiro's avatar aninhacostaribeiro
Browse files

Fixed bugs

parent 56464432
,
xmpp.jid = agent@example.com
xmpp.password = tellNoOne
xmpp.host = localhost
......
package org.fogbowcloud.green.agent.core;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.apache.log4j.Logger;
public class TurnOff {
private static final Logger LOGGER = Logger.getLogger(TurnOff.class);
private static final String DEFAULT_SUSPEND_COMMAND = "pm-suspend";
public void suspend(String command) {
String suspendCommand = "pm-suspend";
if (command != ""){
suspendCommand = command;
if (command == null || command.isEmpty()){
command = DEFAULT_SUSPEND_COMMAND;
}
ProcessBuilder pb = new ProcessBuilder("sudo", "-S", suspendCommand);
ProcessBuilder pb = new ProcessBuilder("sudo", "-S", command);
try {
pb.start();
} catch (IOException e) {
Logger logger = Logger.getLogger("green.agent");
logger.log(Level.WARNING,
"It was not possible to turn down this host");
LOGGER.warn("It was not possible to turn down this host", e);
}
}
......
......@@ -22,7 +22,7 @@ public class IAmAliveHandler extends AbstractQueryHandler {
String jid = query.getFrom().toString();
String macAddress = query.getElement().element("query").elementText("macAddress");
gs.setAgentAddress(hostName, jid, ip, macAddress);
gs.receiveIamAliveInfo(hostName, jid, ip, macAddress);
IQ resultIQ = IQ.createResultIQ(query);
......
......@@ -25,9 +25,12 @@ public class DefaultGreenStrategy implements GreenStrategy {
private long graceTime;
private long sleepingTime;
private long lostHostTime;
private ScheduledExecutorService executorSendIdleHostsToBed = Executors
.newScheduledThreadPool(1);
private ScheduledExecutorService executorVerifyLastTimeSeen = Executors
.newScheduledThreadPool(1);
public DefaultGreenStrategy(Properties greenProperties) {
this.openStackPlugin = new OpenStackInfoPlugin(greenProperties
......@@ -38,19 +41,31 @@ public class DefaultGreenStrategy implements GreenStrategy {
this.lastUpdatedTime = new Date();
this.sleepingTime = Long.parseLong(greenProperties
.getProperty("greenstrategy.sleeptime"));
this.graceTime = Long.parseLong(greenProperties.get(
"greenstrategy.gracetime").toString());
this.graceTime = Long.parseLong(greenProperties.getProperty(
"greenstrategy.gracetime"));
this.lostHostTime = Long.parseLong(
greenProperties.getProperty("greenstrategy.lostAgentTime"));
}
public DefaultGreenStrategy(CloudInfoPlugin openStackPlugin, long graceTime) {
protected DefaultGreenStrategy(CloudInfoPlugin openStackPlugin, long graceTime) {
this.openStackPlugin = openStackPlugin;
this.graceTime = graceTime;
}
private void setAllHosts() {
protected void setAllHosts() {
//must write tests and implement a solution for not loosing host information while updated
this.allHosts = this.openStackPlugin.getHostInformation();
}
protected void setLostHostTime(long lostHostTime) {
this.lostHostTime = lostHostTime;
}
public void setCommunicationComponent(ServerCommunicationComponent gscc) {
this.scc = gscc;
}
protected void setDate(Date date) {
this.lastUpdatedTime = date;
}
......@@ -62,6 +77,15 @@ public class DefaultGreenStrategy implements GreenStrategy {
public List<Host> getSleepingHosts() {
return sleepingHosts;
}
public void receiveIamAliveInfo(String hostName, String jid, String ip, String macAddress) {
for (Host host : this.allHosts){
if (host.getName() == hostName){
host.setJid(jid);
host.setIp(ip);
host.setMacAddress(macAddress);
}
}
}
public void sendIdleHostsToBed() {
this.setAllHosts();
......@@ -70,16 +94,17 @@ public class DefaultGreenStrategy implements GreenStrategy {
if (host.isNovaEnable() && host.isNovaRunning()
&& (host.getRunningVM() == 0)) {
if (!this.getNappingHosts().contains(host)) {
host.setNappingSince(this.lastUpdatedTime.getTime());
this.getNappingHosts().add(host);
} else {
long nowTime = lastUpdatedTime.getTime();
long nowTime = this.lastUpdatedTime.getTime();
if (!this.getSleepingHosts().contains(host)) {
/*
* if there is more than a half hour that the host is
* napping than put it in sleeping host list
*/
if (nowTime - host.getClodUpdatedTime() > this.graceTime) {
scc.sendIdleHostToBed(host.getName());
if (nowTime - host.getNappingSince() > this.graceTime) {
scc.sendIdleHostToBed(host.getMacAddress());
this.getSleepingHosts().add(host);
this.getNappingHosts().remove(host);
}
......@@ -90,23 +115,23 @@ public class DefaultGreenStrategy implements GreenStrategy {
}
public void setCommunicationComponent(ServerCommunicationComponent gscc) {
this.scc = gscc;
}
public void setAgentAddress(String hostName, String jid, String ip, String macAddress) {
public void checkHostsLastSeen() {
for (Host host : this.allHosts){
if (host.getName() == hostName){
host.setJid(jid);
host.setIp(ip);
host.setMacAddress(macAddress);
if (host.getLastSeen() - this.lastUpdatedTime.getTime() > this.lostHostTime){
allHosts.remove(host);
if (this.sleepingHosts.contains(host)){
this.sleepingHosts.remove(host);
}
if (this.nappingHosts.contains(host)){
this.nappingHosts.remove(host);
}
}
}
}
public void wakeUpSleepingHost(int minCPU, int minRAM) {
Collections.sort(this.sleepingHosts);
for (Host host : this.getSleepingHosts()) {
for (Host host : this.sleepingHosts) {
if (host.getAvailableCPU() >= minCPU) {
if (host.getAvailableRAM() >= minRAM) {
this.scc.wakeUpHost(host.getName());
......@@ -120,6 +145,13 @@ public class DefaultGreenStrategy implements GreenStrategy {
}
public void start() {
executorVerifyLastTimeSeen.scheduleWithFixedDelay(new Runnable() {
@Override
public void run() {
checkHostsLastSeen();
}
}, 0, lostHostTime, TimeUnit.MILLISECONDS);
executorSendIdleHostsToBed.scheduleWithFixedDelay(new Runnable() {
@Override
public void run() {
......
......@@ -8,7 +8,7 @@ public interface GreenStrategy {
public void wakeUpSleepingHost(int minCPU, int minRAM);
public void setAgentAddress(String Name, String JID, String IP, String macAddress);
public void receiveIamAliveInfo(String Name, String JID, String IP, String macAddress);
public void setCommunicationComponent(ServerCommunicationComponent gscc);
......
......@@ -13,6 +13,7 @@ public class Host implements Comparable<Host> {
private String jid;
private String macAddress;
private long lastSeen;
private long nappingSince;
public Host(String name, int runningVM, boolean novaRunning,
boolean novaEnable, long updateTime, int availableCPU,
......@@ -105,6 +106,14 @@ public class Host implements Comparable<Host> {
public void setLastSeen(long lastSeen) {
this.lastSeen = lastSeen;
}
public long getNappingSince() {
return nappingSince;
}
public void setNappingSince(long nappingSince) {
this.nappingSince = nappingSince;
}
@Override
public int hashCode() {
final int prime = 31;
......
......@@ -32,7 +32,7 @@ public class TestIamAliverHandler {
.addElement("content").setText(IP);
DefaultGreenStrategy gs = Mockito.mock(DefaultGreenStrategy.class);
Mockito.doNothing().when(gs).setAgentAddress("nothing",JID, IP, null);
Mockito.doNothing().when(gs).receiveIamAliveInfo("nothing",JID, IP, null);
IAmAliveHandler iah = new IAmAliveHandler(gs);
IQ result = iah.handle(iq);
......
......@@ -25,7 +25,7 @@ public class TestDefaultGreenStrategy {
Mockito.when(date.getTime()).thenReturn(Time);
return date;
}
@Test
public void testOneHostNapping(){
Host napping = new Host ("host1", 0, true, true, 0, 0, 0);
......@@ -33,6 +33,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);
dgs.sendIdleHostsToBed();
Assert.assertEquals(1, dgs.getNappingHosts().size());
}
......@@ -40,18 +42,18 @@ public class TestDefaultGreenStrategy {
@Test
public void testOneHostSleeping(){
Host h1 = new Host ("host1", 0, true, true, 1800000,0,0);
h1.setMacAddress("mac");
List <Host> hosts = new LinkedList <Host> ();
hosts.add(h1);
Date date = this.createDateMock(3600001);
ServerCommunicationComponent gscc = Mockito.mock(ServerCommunicationComponent.class);
Mockito.doNothing().when(gscc).wakeUpHost("host1");
Mockito.doNothing().when(gscc).wakeUpHost(h1.getMacAddress());
OpenStackInfoPlugin osip = this.createOpenStackInfoPluginMock(hosts);
DefaultGreenStrategy dgs = new DefaultGreenStrategy(osip, 1800000);
dgs.setDate(date);
dgs.setCommunicationComponent(gscc);
dgs.sendIdleHostsToBed();
dgs.setDate(date);
h1.setNappingSince(1800000);
dgs.sendIdleHostsToBed();
Assert.assertEquals(1, dgs.getSleepingHosts().size());
Assert.assertEquals(0, dgs.getNappingHosts().size());
......@@ -83,12 +85,19 @@ public class TestDefaultGreenStrategy {
Date date = this.createDateMock(3600001);
OpenStackInfoPlugin osip = this.createOpenStackInfoPluginMock(hosts);
DefaultGreenStrategy dgs = new DefaultGreenStrategy(osip, 1800000);
dgs.setDate(date);
ServerCommunicationComponent gscc = Mockito.mock(ServerCommunicationComponent.class);
Mockito.doNothing().when(gscc).wakeUpHost("wake");
Mockito.doNothing().when(gscc).wakeUpHost(mustWake.getMacAddress());
Mockito.doNothing().when(gscc).wakeUpHost(stilSleep.getMacAddress());
Mockito.doNothing().when(gscc).wakeUpHost(stilSleep2.getMacAddress());
dgs.setCommunicationComponent(gscc);
dgs.sendIdleHostsToBed();
dgs.setDate(date);
mustWake.setNappingSince(1800000);
stilSleep.setNappingSince(1800000);
stilSleep2.setNappingSince(1800000);
dgs.sendIdleHostsToBed();
dgs.wakeUpSleepingHost(2, 4);
......@@ -114,8 +123,7 @@ public class TestDefaultGreenStrategy {
@Test
public void testMultipleWakableHosts(){
Host mustWake = new Host ("wake", 0, true, true, 1800000, 3, 8);
Host mustWake2 = new Host ("stil",0,true, true, 1800000, 3, 5);
Host mustWake2 = new Host ("stil", 0, true, true, 1800000, 3, 5);
List <Host> hosts = new LinkedList <Host> ();
hosts.add(mustWake);
......@@ -127,9 +135,11 @@ public class TestDefaultGreenStrategy {
OpenStackInfoPlugin osip = this.createOpenStackInfoPluginMock(hosts);
DefaultGreenStrategy dgs = new DefaultGreenStrategy(osip, 1800000);
dgs.setDate(date);
dgs.setCommunicationComponent(gscc);
dgs.sendIdleHostsToBed();
dgs.setDate(date);
mustWake.setNappingSince(1800000);
mustWake2.setNappingSince(1800000);
dgs.sendIdleHostsToBed();
dgs.wakeUpSleepingHost(2, 4);
......
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