Commit c14ec070 authored by Shirley Crompton's avatar Shirley Crompton
Browse files

Found that we can't overwrite models in CDO, so updated mapper code and

related JUnits, and the main metasolver call to Mapper.  Also tidied up
POM to remove those features/jars that we can get via inheritance,
transitive dependencies and those that are duplicate dependencies.
parent d94673e3
This diff is collapsed.
package eu.paasage.upperware.metasolver;
//import com.fasterxml.jackson.core.JsonGenerationException;
//import com.fasterxml.jackson.databind.JsonMappingException;
//import com.fasterxml.jackson.databind.ObjectMapper;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import eu.paasage.upperware.metasolver.exception.MetricMapperException;
import eu.paasage.upperware.metasolver.metrics.Mapper;
import org.apache.commons.daemon.Daemon;
import org.apache.commons.daemon.DaemonContext;
import org.apache.commons.daemon.DaemonInitException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class App implements Daemon {
static Logger LOGGER = LoggerFactory.getLogger(App.class.getSimpleName());
private Thread myThread;
private static boolean stopped = false;
//add method here to run metasolver with zeroMQ
public static void main (String args[]){
String arg = null;
System.out.println("in main");
if(args.length == 0)
{
arg = "nothing";
System.out.println(" args empty");
}
else if (args[3].contains("daemon"))
{
System.out.println("in daemon");
LOGGER.info("MetaSolver main method called !!!");
LOGGER.info("PAASAGE_CONFIG_DIR: {}", System.getenv("PAASAGE_CONFIG_DIR"));
metasolver mslv = new metasolver();
try {
mslv.startSolving(args[0],args[1]);
} catch (MetricMapperException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
else {
try{
System.out.println("in normal");
String modID= args[1];
Mapper map = new Mapper();
long mapResult = map.mapMetricVariables(modID);
metasolver mslv = new metasolver();
mslv.runMILPSolver(modID, mapResult);
}
catch(Exception e){
System.out.println("error starting metasolver " + e);
}
}
}
public void init(DaemonContext daemonContext) throws DaemonInitException {
LOGGER.info("metasolver init method called !!!");
myThread = new Thread() {
@Override
public synchronized void start() {
App.stopped = false;
super.start();
}
@Override
public void run() {
String[] dam = new String[2];
dam[0]="daemon";
dam[1]="daemon";
main(dam);
}
};
}
@Override
public void start() throws Exception {
LOGGER.info("metasolver start method called !!!");
myThread.start();
}
@Override
public void stop() throws Exception {
LOGGER.info("metasolver stop method called !!!");
App.stopped = true;
try {
myThread.join(1000);
} catch (InterruptedException e) {
System.err.println(e.getMessage());
throw e;
}
}
@Override
public void destroy() {
LOGGER.info("destroy method called !!!");
myThread = null;
}
public static void runMILPSolver(String input, long timestamp){
try{
Process p1 = Runtime.getRuntime().exec("java -jar milp-solver-assembly.jar " + input + ' ' + timestamp);
// you can pass the system command or a script to exec command. here i used uname -a system command
BufferedReader stdInput = new BufferedReader(new
InputStreamReader(p1.getInputStream()));
BufferedReader stdError = new BufferedReader(new
InputStreamReader(p1.getErrorStream()));
// read the output from the command
String s1="";
StringBuilder sb = new StringBuilder();
while ((s1 = stdInput.readLine()) != null) {
sb.append(s1);
sb.append("\n");
}
while ((s1 = stdError.readLine()) != null) {
sb.append(s1);
sb.append("\n");
}
s1= sb.toString();
System.out.println(" output = " + s1);
if (s1.length() > 1 || Integer.parseInt(s1) > 0){
// Process p2 = Runtime.getRuntime().exec("./LAStart " + args[1]);
String s2 = "";
while ((s2 = stdInput.readLine()) != null) {
System.out.println("Std OUT: "+s2);
}
while ((s2 = stdError.readLine()) != null) {
System.out.println("Std ERROR : "+s2);
}
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
package eu.paasage.upperware.metasolver;
//import com.fasterxml.jackson.core.JsonGenerationException;
//import com.fasterxml.jackson.databind.JsonMappingException;
//import com.fasterxml.jackson.databind.ObjectMapper;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import eu.paasage.upperware.metasolver.exception.MetricMapperException;
import eu.paasage.upperware.metasolver.metrics.Mapper;
import org.apache.commons.daemon.Daemon;
import org.apache.commons.daemon.DaemonContext;
import org.apache.commons.daemon.DaemonInitException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.eclipsesource.json.JsonObject;
public class App implements Daemon {
static Logger LOGGER = LoggerFactory.getLogger(App.class.getSimpleName());
private Thread myThread;
private static boolean stopped = false;
//add method here to run metasolver with zeroMQ
public static void main (String args[]){
String arg = null;
System.out.println("in main");
if(args.length == 0)
{
arg = "nothing";
System.out.println(" args empty");
}
else if (args[3].contains("daemon"))
{
System.out.println("in daemon");
LOGGER.info("MetaSolver main method called !!!");
LOGGER.info("PAASAGE_CONFIG_DIR: {}", System.getenv("PAASAGE_CONFIG_DIR"));
metasolver mslv = new metasolver();
try {
mslv.startSolving(args[0],args[1]);
} catch (MetricMapperException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
else {
try{
System.out.println("in normal");
String modID= args[1];
Mapper map = new Mapper();
/* syc17
26Nov15 aligned with updated mapper
long mapResult = map.mapMetricVariables(modID);
*/
metasolver mslv = new metasolver();
JsonObject jObj = map.mapMetricVariables(modID);
mslv.runMILPSolver(jObj.get("id").asString(), jObj.get("solution_timp").asLong());
}
catch(Exception e){
System.out.println("error starting metasolver " + e);
}
}
}
public void init(DaemonContext daemonContext) throws DaemonInitException {
LOGGER.info("metasolver init method called !!!");
myThread = new Thread() {
@Override
public synchronized void start() {
App.stopped = false;
super.start();
}
@Override
public void run() {
String[] dam = new String[2];
dam[0]="daemon";
dam[1]="daemon";
main(dam);
}
};
}
@Override
public void start() throws Exception {
LOGGER.info("metasolver start method called !!!");
myThread.start();
}
@Override
public void stop() throws Exception {
LOGGER.info("metasolver stop method called !!!");
App.stopped = true;
try {
myThread.join(1000);
} catch (InterruptedException e) {
System.err.println(e.getMessage());
throw e;
}
}
@Override
public void destroy() {
LOGGER.info("destroy method called !!!");
myThread = null;
}
public static void runMILPSolver(String input, long timestamp){
try{
Process p1 = Runtime.getRuntime().exec("java -jar milp-solver-assembly.jar " + input + ' ' + timestamp);
// you can pass the system command or a script to exec command. here i used uname -a system command
BufferedReader stdInput = new BufferedReader(new
InputStreamReader(p1.getInputStream()));
BufferedReader stdError = new BufferedReader(new
InputStreamReader(p1.getErrorStream()));
// read the output from the command
String s1="";
StringBuilder sb = new StringBuilder();
while ((s1 = stdInput.readLine()) != null) {
sb.append(s1);
sb.append("\n");
}
while ((s1 = stdError.readLine()) != null) {
sb.append(s1);
sb.append("\n");
}
s1= sb.toString();
System.out.println(" output = " + s1);
if (s1.length() > 1 || Integer.parseInt(s1) > 0){
// Process p2 = Runtime.getRuntime().exec("./LAStart " + args[1]);
String s2 = "";
while ((s2 = stdInput.readLine()) != null) {
System.out.println("Std OUT: "+s2);
}
while ((s2 = stdError.readLine()) != null) {
System.out.println("Std ERROR : "+s2);
}
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
......@@ -12,6 +12,7 @@ package eu.paasage.upperware.metasolver;
import org.zeromq.ZMQ;
import org.zeromq.ZMQ.Context;
import org.zeromq.ZMQ.Socket;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStream;
......@@ -30,6 +31,8 @@ import org.eclipse.emf.cdo.view.CDOView;
import org.eclipse.emf.common.util.EList;
import org.eclipse.emf.ecore.EObject;
import com.eclipsesource.json.JsonObject;
//The metasolver is responsible for calling the different solvers in PaaSage.
public class metasolver{
......@@ -41,11 +44,14 @@ public class metasolver{
String CAMELmodID= args[0];
String CPmodID= args[1];
Mapper map = new Mapper();
/* syc17 26Nov15 aligned with updated mapper
long mapResult = map.mapMetricVariables(CPmodID);
runMILPSolver(CPmodID, mapResult);
*/
JsonObject jObj = map.mapMetricVariables(CPmodID);
runMILPSolver(jObj.get("id").asString(), jObj.get("solution_tmp").asLong());
//now invoke S2D
runS2D(CAMELmodID, CPmodID, mapResult);
//runS2D(CAMELmodID, CPmodID, mapResult);
runS2D(CAMELmodID, jObj.get("id").asString(), jObj.get("solution_tmp").asLong());
}
catch(Exception e){
System.out.println("error running metasolver " + e);
......@@ -56,8 +62,12 @@ public class metasolver{
try{
// String modID= args[1];
Mapper map = new Mapper();
/* syc17 26Nov15 aligned with updated mapper
long mapResult = map.mapMetricVariables(modID);
runMILPSolver("modID", mapResult);
*/
JsonObject jObj = map.mapMetricVariables(modID);
//runMILPSolver("modID", mapResult);
runMILPSolver(jObj.get("id").asString(), jObj.get("solution_tmp").asLong());
}
catch(Exception e){
System.out.println("error starting metasolver " + e);
......@@ -72,11 +82,13 @@ public class metasolver{
}
public void go(String CAMELmodel, String CPmodel) throws MetricMapperException{
Mapper map = new Mapper();
long mapResult = map.mapMetricVariables(CPmodel);
/* 26Nov15 syc17 aigned with uppdated Mapper code
long mapResult = map.mapMetricVariables(CPmodel); */
JsonObject jObj = map.mapMetricVariables(CPmodel);
RPListener rpl = new RPListener();
metricsListener ml = new metricsListener("metricID");
solutionListener sl = new solutionListener(CAMELmodel, CPmodel, mapResult);
//solutionListener sl = new solutionListener(CAMELmodel, CPmodel, mapResult);
solutionListener sl = new solutionListener(CAMELmodel, jObj.get("id").asString(), jObj.get("solution_tmp").asLong());
//Currently we only have one solver and this is invoked taking in the ResourceID from the masterscript
......
......@@ -14,16 +14,19 @@ import java.util.Set;
import org.apache.log4j.Logger;
import org.eclipse.emf.ecore.EObject;
import com.eclipsesource.json.JsonObject;
//import eu.paasage.upperware.cp.cloner.CDOClientExtended;
//import eu.paasage.upperware.cp.cloner.CPCloner;
import eu.paasage.upperware.metamodel.cp.ConstraintProblem;
import eu.paasage.upperware.metamodel.cp.MetricVariable;
import eu.paasage.upperware.metamodel.cp.MetricVariableValue;
import eu.paasage.upperware.metamodel.cp.Solution;
import eu.paasage.upperware.profiler.cp.generator.model.tools.CPModelTool;
import eu.paasage.upperware.metasolver.exception.MetricMapperException;
import eu.paasage.upperware.metasolver.util.CdoTool;
import eu.paasage.upperware.metasolver.util.CpModelTool;
import eu.paasage.upperware.profiler.cp.generator.model.tools.CPModelTool;
/**
* The metrics Mapper prepares a CP model for submission to the solver. It
......@@ -42,12 +45,11 @@ public class Mapper {
protected static Logger log = Logger.getLogger(Mapper.class);
/** the CP Model resource id */
private String cpModelId;
/** CPCloner for cloning existing CDO resources */
//private CPCloner cpCloner;
/** the extended CDO client provided by CPCloner */
//private CDOClientExtended xCdoClient; // currently no credentials. If
// require credentials, need to
// extend it
/** CPCloner for cloning existing CDO resources
private CPCloner cpCloner;
/** the extended CDO client provided by CPCloner
private CDOClientExtended xCdoClient; // currently no credentials. If require credentials, need to extend it
*/
/** CDO utilties */
private CdoTool utils;
......@@ -56,117 +58,138 @@ public class Mapper {
*/
public Mapper() {
}
/**
* Construct an instance with the target CP Model resource id
*/
public Mapper(String resId) {
// I think this may change at runtime as CP_generate clone the cp_model
// rather than
// updating it. The new version will have a new resource id
// I think this may change at runtime as rule processor clones the cp_model
// rather than updating it. The new version will have a new resource id
// Also, we may need to update the CP model expressions too, this requires creating a new version to avoid
// overwriting previous state
this.cpModelId = resId;
}
}*/
/**
* Construct an instance with the target CP Model resource id
public Mapper(String resId) {
// I think this may change at runtime as rule processor clones the cp_model
// rather than updating it. The new version will have a new resource id
// Also, we may need to update the CP model expressions too, this requires creating a new version to avoid
// overwriting previous state
this.cpModelId = resId;
}*/
/**
* This method is used for preparing a CP model for first deployment. There
* are no running metrics and a default constant value is assigned to each
* metric variable listed in the model. If there are no metric variables in
* the CPModel, the return value will be 0.
* the CPModel, the solution timestamp value will be 0.
* <p>
*
* @param resId
* the target CP Model resource id
* @return long the solution timestamp to use when calling the solver or 0
* @return a {@link com.eclipsesource.json.JsonObject <em>JsonObject</em>} containing the
* new resource ID and the solution timestamp
* @throws MetricMapperException
* on processing error
*/
public long mapMetricVariables(String resId) throws MetricMapperException {
//
long timestamp = 0;
public JsonObject mapMetricVariables(String resId) throws MetricMapperException {
JsonObject jObj = new JsonObject(); //keys are "id" (String) and "solution_tmp" (long)
boolean updateCP = false; // commit to CDO flag
if (this.utils == null) {
this.utils = CdoTool.getInstance();
}
List<EObject> model_contents = null;
// start the cdo-client
this.utils.openCDOSession();
// get the cp model from a copy of the resource
List<EObject> model_contents = this.utils.cloneModel(resId); // cloner
// may
// return
// an
// empty
// list
ConstraintProblem cp = CpModelTool.getCPModel(model_contents);
if (cp == null) {
throw new MetricMapperException(
"failed to extract ConstraintModel from the resource("
+ resId + ")");
}
// go ahead
List<MetricVariable> mvs = cp.getMetricVariables();
//
if (mvs.isEmpty()) {
log.debug("CP model in " + resId
+ " has no Metric Variable entities...");
} else {
log.debug(mvs.size()
+ " metric variables retrived from CP model in " + resId
+ "...");
// now get the solution
Solution solution = CPModelTool
.searchLastSolution(cp.getSolution());
if (solution == null) {
// no solution in model, create one now
try{
this.utils.openCDOSession();
// clone the resource in memory and get the cp model
model_contents = this.utils.cloneModel(resId); // cloner may return an empty list
ConstraintProblem cp = CpModelTool.getCPModel(model_contents);
if (cp == null) {
throw new MetricMapperException(
"failed to extract ConstraintModel from the resource("
+ resId + ")");
}
//16Nov15 - Daniel R. only supports 3 types of metrics (cost, availability and response time). He should have created the empty solution
//to hold the metricVariableValues
// go ahead
List<MetricVariable> mvs = cp.getMetricVariables();
//
if (mvs.isEmpty()) {
log.debug("CP model in " + resId
+ " has no Solution entities...");
updateCP = true;
solution = CPModelTool.createSolution(cp);
// now add the metricVariable with constant value
for (MetricVariable mv : mvs) {
//
solution = CpModelTool.setConstantValue(mv, solution);
}
} else {// there is a last solution
// match the metricVariables
for (MetricVariable mv : mvs) {
if (CPModelTool.searchMetricValue(solution, mv) == null) {
// create it
+ " has no Metric Variable entities...");
} else {
log.debug(mvs.size()
+ " metric variables retrived from CP model in " + resId
+ "...");
// now get the solution
Solution solution = CPModelTool
.searchLastSolution(cp.getSolution());
if (solution == null) {
// no solution in model, create one now
log.debug("CP model in " + resId
+ " has no Solution entities...");
updateCP = true;
solution = CPModelTool.createSolution(cp);
// now add the metricVariable with constant value
for (MetricVariable mv : mvs) {
//
solution