Skip to content

Commit

Permalink
Adding "instances-idle" and "instances-inuse" in the exchanges of
Browse files Browse the repository at this point in the history
message.
  • Loading branch information
fgan1 committed May 8, 2015
1 parent 167870a commit ae14c4e
Show file tree
Hide file tree
Showing 12 changed files with 102 additions and 114 deletions.
36 changes: 33 additions & 3 deletions src/main/java/org/fogbowcloud/rendezvous/core/ResourcesInfo.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,18 +11,48 @@ public class ResourcesInfo {
private String cpuInUse;
private String memIdle;
private String memInUse;
private String instanceInUse;
private String instanceIdle;
private List<Flavor> flavours;
private String cert;

public ResourcesInfo(String id, String cpuIdle, String cpuInUse,
String memIdle, String memInUse, List<Flavor> flavours, String cert) {
this.setCert(cert);


public ResourcesInfo(String id, String cpuIdle, String cpuInUse, String memIdle,
String memInUse, String instanceIdle, String instanceInUse, List<Flavor> flavours,
String cert) {
super();
setId(id);
setCpuIdle(cpuIdle);
setCpuInUse(cpuInUse);
setMemIdle(memIdle);
setMemInUse(memInUse);
setInstanceIdle(instanceIdle);
setInstanceInUse(instanceInUse);
this.setFlavours(flavours);
this.setCert(cert);
}

public String getInstanceInUse() {
return instanceInUse;
}

public void setInstanceInUse(String instanceInUse) {
if (instanceInUse == null || instanceInUse.isEmpty()) {
throw new IllegalArgumentException("ResourceInfo instances-inuse is invalid.");
}
this.instanceInUse = instanceInUse;
}

public String getInstanceIdle() {
return instanceIdle;
}

public void setInstanceIdle(String instanceIdle) {
if (instanceIdle == null || instanceIdle.isEmpty()) {
throw new IllegalArgumentException("ResourceInfo instances-idle is invalid.");
}
this.instanceIdle = instanceIdle;
}

public String getId() {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package org.fogbowcloud.rendezvous.xmpp;

import java.text.ParseException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
Expand Down Expand Up @@ -55,7 +56,6 @@ private static RendezvousResponseItem convertWhoIsAliveSyncResponse(IQ iq)
return rendezvousItem;
}

@SuppressWarnings("unchecked")
public static RendezvousItem getWhoIsAliveResponseItem(Element itemEl) throws ParseException {
Attribute id = itemEl.attribute("id");
Element statusEl = itemEl.element("status");
Expand All @@ -65,23 +65,12 @@ public static RendezvousItem getWhoIsAliveResponseItem(Element itemEl) throws Pa
String cpuInUse = statusEl.element("cpu-inuse").getText();
String memIdle = statusEl.element("mem-idle").getText();
String memInUse = statusEl.element("mem-inuse").getText();
String updated = statusEl.element("updated").getText();
String instanceIdle = statusEl.element("instances-idle").getText();
String instanceInUse = statusEl.element("instances-inuse").getText();
long quietFor = Long.parseLong(statusEl.element("quiet-for").getText());

List<Flavor> flavoursList = new LinkedList<Flavor>();
Iterator<Element> flavourIterator = statusEl.elementIterator("flavor");
while (flavourIterator.hasNext()) {
Element flavour = (Element) flavourIterator.next();
String name = flavour.element("name").getText();
String cpu = flavour.element("cpu").getText();
String mem = flavour.element("mem").getText();
int capacity = Integer.parseInt(flavour.element("capacity").getText());
Flavor flavor = new Flavor(name, cpu, mem, capacity);
flavoursList.add(flavor);
}

ResourcesInfo resources = new ResourcesInfo(id.getValue(), cpuIdle, cpuInUse, memIdle,
memInUse, flavoursList, cert);
memInUse, instanceIdle, instanceInUse, new ArrayList<Flavor>(), cert);
RendezvousItem item = new RendezvousItem(resources);
item.setLastTime(System.currentTimeMillis() - quietFor);
return item;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
package org.fogbowcloud.rendezvous.xmpp.handler;

import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.ArrayList;

import org.dom4j.Element;
import org.fogbowcloud.rendezvous.core.Rendezvous;
Expand All @@ -21,7 +19,6 @@ public IAmAliveHandler(Rendezvous rendezvous) {
this.rendezvous = rendezvous;
}

@SuppressWarnings("unchecked")
public IQ handle(IQ iq) {
String id = iq.getFrom().toBareJID();

Expand All @@ -37,22 +34,11 @@ public IQ handle(IQ iq) {
String cpuInUse = statusElement.element("cpu-inuse").getText();
String memIdle = statusElement.element("mem-idle").getText();
String memInUse = statusElement.element("mem-inuse").getText();
String instanceIdle = statusElement.element("instances-idle").getText();
String instanceInUse = statusElement.element("instances-inuse").getText();

List<Flavor> flavoursList = new LinkedList<Flavor>();
Iterator<Element> flavourIterator = statusElement
.elementIterator("flavor");
while(flavourIterator.hasNext()) {
Element flavour = (Element) flavourIterator.next();
String name = flavour.element("name").getText();
String cpu = flavour.element("cpu").getText();
String mem = flavour.element("mem").getText();
int capacity = Integer.parseInt(flavour.element("capacity")
.getText());
Flavor flavor = new Flavor(name, cpu, mem, capacity);
flavoursList.add(flavor);
}
ResourcesInfo resources = new ResourcesInfo(id ,cpuIdle, cpuInUse, memIdle,
memInUse, flavoursList, cert);
ResourcesInfo resources = new ResourcesInfo(id, cpuIdle, cpuInUse, memIdle,
memInUse, instanceIdle, instanceInUse, new ArrayList<Flavor>(), cert);
//TODO handle certificate?
rendezvous.iAmAlive(resources);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,17 +68,11 @@ private IQ createResponse(IQ iq, RSM rsm, List<RendezvousItem> aliveIds) {
rendezvousItem.getResourcesInfo().getMemIdle());
statusEl.addElement("mem-inuse").setText(
rendezvousItem.getResourcesInfo().getMemInUse());
statusEl.addElement("instances-idle").setText(
rendezvousItem.getResourcesInfo().getInstanceIdle());
statusEl.addElement("instances-inuse").setText(
rendezvousItem.getResourcesInfo().getInstanceInUse());

List<Flavor> flavours = rendezvousItem.getResourcesInfo()
.getFlavours();
for (Flavor f : flavours) {
Element flavorElement = statusEl.addElement("flavor");
flavorElement.addElement("name").setText(f.getName());
flavorElement.addElement("cpu").setText(f.getCpu());
flavorElement.addElement("mem").setText(f.getMem());
flavorElement.addElement("capacity").setText(
f.getCapacity().toString());
}
statusEl.addElement("updated").setText(
String.valueOf(rendezvousItem.getFormattedTime()));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,8 @@
import org.fogbowcloud.rendezvous.core.Rendezvous;
import org.fogbowcloud.rendezvous.core.RendezvousImpl;
import org.fogbowcloud.rendezvous.core.RendezvousItem;
import org.fogbowcloud.rendezvous.core.model.Flavor;
import org.fogbowcloud.rendezvous.xmpp.util.RSM;
import org.fogbowcloud.rendezvous.xmpp.util.FederationMember;
import org.fogbowcloud.rendezvous.xmpp.util.RSM;
import org.jamppa.component.handler.AbstractQueryHandler;
import org.xmpp.packet.IQ;
import org.xmpp.packet.PacketError;
Expand Down Expand Up @@ -109,14 +108,11 @@ private IQ createResponse(IQ iq, RendezvousImpl rendezvousImpl,
item.getResourcesInfo().getMemIdle());
statusEl.addElement("mem-inuse").setText(
item.getResourcesInfo().getMemInUse());
for (Flavor flavor : item.getResourcesInfo().getFlavours()) {
Element flavorElement = statusEl.addElement("flavor");
flavorElement.addElement("name").setText(flavor.getName());
flavorElement.addElement("cpu").setText(flavor.getCpu());
flavorElement.addElement("mem").setText(flavor.getMem());
flavorElement.addElement("capacity").setText(
flavor.getCapacity().toString());
}
statusEl.addElement("instances-idle").setText(
item.getResourcesInfo().getInstanceIdle());
statusEl.addElement("instances-inuse").setText(
item.getResourcesInfo().getInstanceInUse());

statusEl.addElement("updated").setText(item.getFormattedTime());

statusEl.addElement("quiet-for").setText(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public void testLastTime() throws InterruptedException {
List<Flavor> flavors = new LinkedList<Flavor>();
flavors.add(new Flavor("small", "cpu", "mem", 2));
RendezvousItem item = new RendezvousItem(new ResourcesInfo("id",
"value1", "value2", "value3", "value4", flavors, "cert"));
"value1", "value2", "value3", "value4", "value5", "value6", flavors, "cert"));
Thread.sleep(1);
Date after = new Date(System.currentTimeMillis());

Expand All @@ -39,7 +39,7 @@ public void testFormattedDateNotNull() {
List<Flavor> flavors = new LinkedList<Flavor>();
flavors.add(new Flavor("small", "cpu", "mem", 2));
RendezvousItem item = new RendezvousItem(new ResourcesInfo("id",
"value1", "value2", "value3", "value4", flavors, "cert"));
"value1", "value2", "value3", "value4", "value5", "value6", flavors, "cert"));

Assert.assertNotNull(item.getFormattedTime());
}
Expand Down
22 changes: 11 additions & 11 deletions src/test/java/org/fogbowcloud/rendezvous/core/RendezvousTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public void set() {
flavors = new LinkedList<Flavor>();
flavors.add(new Flavor("small", "cpu", "mem", 2));
resources = new ResourcesInfo("abc", "value1", "value2", "value3",
"value4", flavors, "cert");
"value4", "value5", "value6", flavors, "cert");
neighbors = new String[] {};
executor = Mockito.mock(ScheduledExecutorService.class);
properties = Mockito.mock(Properties.class);
Expand Down Expand Up @@ -77,7 +77,7 @@ public void testImAliveManyElements() {
for (int i = 0; i < 10; i++) {
String s = "Element " + i;
resources = new ResourcesInfo(s, "value1", "value2", "value3",
"value4", flavors, "cert");
"value4", "value5", "value6", flavors, "cert");
rendezvous.iAmAlive(resources);
List<RendezvousItem> elementList = rendezvous.whoIsAlive();
Assert.assertTrue(containsId(elementList, s));
Expand Down Expand Up @@ -105,14 +105,14 @@ public void testContainsSameIDs() {
@Test(expected = IllegalArgumentException.class)
public void testImAliveNullParameter() {
ResourcesInfo resources = new ResourcesInfo(null, "value1", "value2",
"value3", "value4", flavors, "cert");
"value3", "value4", "value5", "value6", flavors, "cert");
rendezvous.iAmAlive(resources);
}

@Test(expected = IllegalArgumentException.class)
public void testImAliveEmptyParameter() {
ResourcesInfo resources = new ResourcesInfo("", "value1", "value2",
"value3", "value4", flavors, "cert");
"value3", "value4", "value5", "value6", flavors, "cert");
rendezvous.iAmAlive(resources);
}

Expand All @@ -131,7 +131,7 @@ public void testWhoIsAliveEmpty() {
@Test
public void testWhoIsAliveSingleElement() {
ResourcesInfo resources = new ResourcesInfo("OnlyElement", "value1",
"value2", "value3", "value4", flavors, "cert");
"value2", "value3", "value4", "value5", "value6", flavors, "cert");
rendezvous.iAmAlive(resources);
Assert.assertEquals(1, rendezvous.whoIsAlive().size());
Assert.assertTrue(containsId(rendezvous.whoIsAlive(), "OnlyElement"));
Expand All @@ -140,7 +140,7 @@ public void testWhoIsAliveSingleElement() {
@Test
public void testWhoIsAliveElementValues() throws InterruptedException {
ResourcesInfo resources = new ResourcesInfo("id", "value1", "value2",
"value3", "value4", flavors, "cert");
"value3", "value4", "value5", "value6", flavors, "cert");

Date beforeMessage = new Date(System.currentTimeMillis());
Thread.sleep(1);
Expand Down Expand Up @@ -179,7 +179,7 @@ public void testWhoIsAliveElementUpdatedValueNotNull() {
public void testWhoIsAliveManyElements() {
for (int i = 0; i < 10; i++) {
rendezvous.iAmAlive(new ResourcesInfo("Element" + (i + 1),
"value1", "value2", "value3", "value4", flavors, "cert"));
"value1", "value2", "value3", "value4", "value5", "value6", flavors, "cert"));
}

for (int i = 0; i < 10; i++) {
Expand Down Expand Up @@ -238,7 +238,7 @@ public void testWhoIsAliveAfterTimeManyElements()

// IAmAlive of new id
ResourcesInfo resources2 = new ResourcesInfo("id2", "value1", "value2",
"value3", "value4", flavors, "cert");
"value3", "value4", "value5", "value6", flavors, "cert");
rendezvous.iAmAlive(resources2);
rendezvous.setLastTime("id2", firstPassageOfTime + 3);

Expand Down Expand Up @@ -272,7 +272,7 @@ public void testConcurrentIAmAlive() throws InterruptedException {
// IAmAlive of new id
String id = "Element" + i;
rendezvous.iAmAlive(new ResourcesInfo(id, "value1", "value2",
"value3", "value4", flavors, "cert"));
"value3", "value4", "value5", "value6", flavors, "cert"));
rendezvous.setLastTime(id, currentTime);

// mocking date to check expired
Expand All @@ -295,7 +295,7 @@ public void testConcurrentIAmAlive() throws InterruptedException {
// IAmAlive client from already existing client
String id = "Element" + i;
rendezvous.iAmAlive(new ResourcesInfo(id, "value1", "value2",
"value3", "value4", flavors, "cert"));
"value3", "value4", "value5", "value6", flavors, "cert"));
((RendezvousImpl) rendezvous).setLastTime(id, currentTime);

// mocking date to check expired
Expand Down Expand Up @@ -338,7 +338,7 @@ public void testConcourrency() throws InterruptedException {

for (int i = 0; i < 1000000; i++) {
rendezvous.iAmAlive(new ResourcesInfo("Element" + i, "value1",
"value2", "value3", "value4", flavors, "cert"));
"value2", "value3", "value4", "value5", "value6", flavors, "cert"));
}

Assert.assertFalse(rendezvous.getInError());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -228,6 +228,8 @@ public static IQ createIAmAliveIQ() {
statusEl.addElement("cpu-inuse").setText("valor2");
statusEl.addElement("mem-idle").setText("valor3");
statusEl.addElement("mem-inuse").setText("valor4");
statusEl.addElement("instances-idle").setText("valor5");
statusEl.addElement("instances-inuse").setText("valor6");
return iq;
}

Expand Down Expand Up @@ -390,23 +392,12 @@ public static RendezvousItem getWhoIsAliveResponseItem(Element itemEl)
String cpuInUse = statusEl.element("cpu-inuse").getText();
String memIdle = statusEl.element("mem-idle").getText();
String memInUse = statusEl.element("mem-inuse").getText();
String instancesIdle = statusEl.element("instances-idle").getText();
String instancesInUse = statusEl.element("instances-inuse").getText();
String updated = statusEl.element("updated").getText();

List<Flavor> flavoursList = new LinkedList<Flavor>();
Iterator<Element> flavourIterator = statusEl.elementIterator("flavor");
while (flavourIterator.hasNext()) {
Element flavour = (Element) flavourIterator.next();
String name = flavour.element("name").getText();
String cpu = flavour.element("cpu").getText();
String mem = flavour.element("mem").getText();
int capacity = Integer.parseInt(flavour.element("capacity")
.getText());
Flavor flavor = new Flavor(name, cpu, mem, capacity);
flavoursList.add(flavor);
}

ResourcesInfo resources = new ResourcesInfo(id.getValue(), cpuIdle,
cpuInUse, memIdle, memInUse, flavoursList, cert);
cpuInUse, memIdle, memInUse, instancesIdle, instancesInUse, new ArrayList<Flavor>(), cert);
RendezvousItem item = new RendezvousItem(resources);
item.setLastTime(RendezvousItem.ISO_8601_DATE_FORMAT.parse(updated)
.getTime());
Expand All @@ -415,8 +406,8 @@ public static RendezvousItem getWhoIsAliveResponseItem(Element itemEl)

public ResourcesInfo getResources() {
List<Flavor> flavours = new LinkedList<Flavor>();
ResourcesInfo resources = new ResourcesInfo("id", "cpuIdle",
"cpuInUse", "memIdle", "memInUse", flavours, "cert");
ResourcesInfo resources = new ResourcesInfo("id", "cpuIdle", "cpuInUse", "memIdle",
"memInUse", "instanceIdle", "instanceInUSe", flavours, "cert");
return resources;
}

Expand All @@ -438,6 +429,8 @@ public IQ createWhoIsAliveSyncResponse(IQ iq) {
statusEl.addElement("cpu-inuse").setText("cpu-inuse");
statusEl.addElement("mem-idle").setText("mem-idle");
statusEl.addElement("mem-inuse").setText("mem-inuse");
statusEl.addElement("instances-idle").setText("instances-idle");
statusEl.addElement("instances-inuse").setText("instances-inuse");

Element flavorElement = statusEl.addElement("flavor");
flavorElement.addElement("name").setText("flavor");
Expand Down
Loading

0 comments on commit ae14c4e

Please sign in to comment.