Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add iaas compute calls from mmonitor. #1

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 2 additions & 4 deletions iaas/src/main/java/com/alterjoc/caliper/iaas/Compute.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,15 @@

package com.alterjoc.caliper.iaas;

import org.jclouds.compute.RunNodesException;

/**
* @author Matej Lazar
*/
public interface Compute {
String createInstance() throws RunNodesException;

String scaleUp() throws RunNodesException;
String scaleUp();

void scaleDown() throws Exception;
void scaleDown();

/**
* Destroys all instances
Expand Down
41 changes: 33 additions & 8 deletions iaas/src/main/java/com/alterjoc/caliper/iaas/ComputeImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import java.util.NoSuchElementException;
import java.util.Properties;
import java.util.Set;
import java.util.logging.Logger;

import org.jclouds.compute.ComputeService;
import org.jclouds.compute.ComputeServiceContext;
Expand All @@ -22,6 +23,8 @@
*/
public class ComputeImpl implements Compute {

private static final Logger log = Logger.getLogger(SimpleMonitorManager.class.getName());

private IaasProperties iaasProperties;
private ComputeService service;
private List<NodeMetadata> runningInstances = new ArrayList<NodeMetadata>();
Expand All @@ -43,6 +46,11 @@ ImmutableSet.<Module> of(),
}

public String createInstance() throws RunNodesException {
if (iaasProperties == null) {
logSkipping();
return "";
}

String keyPairName = iaasProperties.getKeyPairName();

String imageId = iaasProperties.getImageId();
Expand All @@ -64,13 +72,20 @@ public String createInstance() throws RunNodesException {
return node.getId();
}

public String scaleUp() throws RunNodesException {
return createInstance();
public String scaleUp() {
try {
return createInstance();
} catch (RunNodesException e) {
log.warning("Cannot create instance.");
}
return null;
}

public void scaleDown() throws Exception {
public void scaleDown() {
NodeMetadata instance = getLastInstance();
destroy(instance);
if (instance != null) {
destroy(instance);
}
}

public void terminate() {
Expand All @@ -80,16 +95,26 @@ public void terminate() {
}

private void destroy(NodeMetadata instance) {
if (iaasProperties == null) {
logSkipping();
return;
}
service.destroyNode(instance.getId());
runningInstances.remove(instance);
}

private NodeMetadata getLastInstance() throws Exception {
private NodeMetadata getLastInstance() {
int lastIndex = runningInstances.size() - 1;
if (lastIndex < 0) {
throw new Exception("No more instances.");
if (lastIndex > 0) {
return runningInstances.get(lastIndex );
} else {
return null;
}
return runningInstances.get(lastIndex );

}

private void logSkipping() {
log.info("Skipping iaas operation because of incomplete properties.");
}

}
85 changes: 64 additions & 21 deletions iaas/src/main/java/com/alterjoc/caliper/iaas/IaasProperties.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
package com.alterjoc.caliper.iaas;

import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;
import java.util.logging.Logger;

/**
* @author Matej Lazar
*/
Expand All @@ -9,29 +14,37 @@ public class IaasProperties {
private String imageId;
private String userName;
private String password;
private String keyPairName = "caliperKey";
private String[] securityGroups = new String[]{"default"};
private String group = "caliper";

public IaasProperties(String provider, String providerUrl, String imageId, String userName, String password) {
super();
this.provider = provider;
this.providerUrl = providerUrl;
this.imageId = imageId;
this.userName = userName;
this.password = password;
private String keyPairName;
private String[] securityGroups;
private String group;

private static final Logger log = Logger.getLogger(IaasProperties.class.getName());

private IaasProperties() throws IOException {

Properties prop = new Properties();
InputStream in = null;
try {
in = getClass().getResourceAsStream("iaas.properties");
prop.load(in);
} finally {
if (in != null) {
in.close();
}
}
setProperties(prop);
}

public IaasProperties(String provider, String providerUrl, String imageId, String userName, String password, String keyPairName, String[] securityGroups, String group) {
super();
this.provider = provider;
this.providerUrl = providerUrl;
this.imageId = imageId;
this.userName = userName;
this.password = password;
this.keyPairName = keyPairName;
this.securityGroups = securityGroups;
this.group = group;
private void setProperties(Properties prop) {
this.provider = prop.getProperty("provider");
this.providerUrl = prop.getProperty("provider-url");
this.imageId = prop.getProperty("image-id");
this.userName = prop.getProperty("username");
this.password = prop.getProperty("password");

this.keyPairName = prop.getProperty("key-pair-name");
this.securityGroups = prop.getProperty("security-groups").split(":");
this.group = prop.getProperty("group");
}

public String getProvider() {
Expand Down Expand Up @@ -65,4 +78,34 @@ public String[] getSecurityGroups() {
public String getGroup() {
return group ;
}

public static IaasProperties getInstance() {
IaasProperties iaasProp;
try {
iaasProp = new IaasProperties();
if (iaasProp.isValid()) {
return iaasProp;
} else {
log.warning("Iaas properties are not complete.");
}
} catch (IOException e) {
log.warning("Cannot read iaas properties.");
}
return null;
}

private boolean isValid() {
if (provider == null || provider.equals("")) {
return false;
} else if (providerUrl == null || providerUrl.equals("")) {
return false;
} else if (imageId == null || imageId.equals("")) {
return false;
} else if (userName == null || userName.equals("")) {
return false;
} else if (password == null || password.equals("")) {
return false;
}
return true;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,10 @@
import java.util.Set;
import java.util.logging.Logger;

import org.kohsuke.MetaInfServices;

import com.alterjoc.caliper.agent.monitor.AbstractMonitorManager;
import com.alterjoc.caliper.agent.monitor.MonitorManager;
import org.kohsuke.MetaInfServices;

/**
* @author <a href="mailto:[email protected]">Ales Justin</a>
Expand All @@ -26,6 +27,11 @@ public class SimpleMonitorManager extends AbstractMonitorManager {
private static final Logger log = Logger.getLogger(SimpleMonitorManager.class.getName());

private Set<String> apps = new HashSet<String>();
private Compute compute;

public SimpleMonitorManager() {
compute = new ComputeImpl(IaasProperties.getInstance());
}

private static boolean isCreateApp(Class clazz, String name) {
return APP_FACTORY.equals(clazz.getName()) && CREATE_APP.equals(name);
Expand All @@ -52,10 +58,12 @@ public void postMonitorStatic(Class clazz, String name, Class[] types, Object[]
if (size == Counter.LIMIT_SIZE && (diff < 1000 * 60)) {
// less than 1min?
log.info("Scale up -- " + apps + " !!!");
String instanceId = compute.scaleUp();
counter.reset();
} else if (diff > 1000 * 60 * 5) {
// more than 5min
log.info("Scale down -- " + apps + " !!!");
compute.scaleDown();
counter.reset();
}
}
Expand Down
21 changes: 21 additions & 0 deletions iaas/src/main/resources/iaas.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
#iaas provider name
provider=

#url of iaas provider REST API
provider-url=

#ID of image to instantiate
image-id=

username=

password=

#ssh kex name
key-pair-name=caliperKey

#iaas securtiy group name
security-groups=default

#iaas instance group
group=caliper