Skip to content

Commit

Permalink
Merge branch 'hackathon' of https://github.com/hms-dbmi/IRCT into hac…
Browse files Browse the repository at this point in the history
…kathon
  • Loading branch information
Jason Stedman committed Sep 8, 2017
2 parents 53f19e4 + efd6994 commit b3eefea
Show file tree
Hide file tree
Showing 93 changed files with 1,091 additions and 14,430 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,6 @@
import java.util.List;
import java.util.Map;
import java.util.Properties;
import java.util.logging.Level;
import java.util.logging.Logger;

import javax.annotation.PostConstruct;
import javax.ejb.Singleton;
Expand All @@ -24,6 +22,8 @@
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Root;

import org.apache.log4j.Logger;

import edu.harvard.hms.dbmi.bd2k.irct.dataconverter.ResultDataConverter;
import edu.harvard.hms.dbmi.bd2k.irct.event.EventConverterImplementation;
import edu.harvard.hms.dbmi.bd2k.irct.event.IRCTEventListener;
Expand Down Expand Up @@ -52,8 +52,7 @@ public class IRCTApplication {
private Map<String, IRCTJoin> supportedJoinTypes;
private Map<ResultDataType, List<DataConverterImplementation>> resultDataConverters;

@Inject
Logger log;
Logger logger = Logger.getLogger(this.getClass());

@Inject
private EntityManagerFactory objectEntityManager;
Expand All @@ -70,27 +69,27 @@ public class IRCTApplication {
*/
@PostConstruct
public void init() {
log.info("Starting IRCT Application");
logger.info("Starting IRCT Application");
this.oem = objectEntityManager.createEntityManager();
this.oem.setFlushMode(FlushModeType.COMMIT);

log.info("Loading Data Converters");
logger.info("Loading Data Converters");
loadDataConverters();
log.info("Finished Data Converters");
logger.info("Finished Data Converters");

log.info("Loading Event Listeners");
logger.info("Loading Event Listeners");
loadIRCTEventListeners();
log.info("Finished Loading Event Listeners");
logger.info("Finished Loading Event Listeners");

log.info("Loading Join Types");
logger.info("Loading Join Types");
loadJoins();
log.info("Finished Loading Join Types");
logger.info("Finished Loading Join Types");

log.info("Loading Resources");
logger.info("Loading Resources");
loadResources();
log.info("Finished Loading Resources");
logger.info("Finished Loading Resources");

log.info("Finished Starting IRCT Application");
logger.info("Finished Starting IRCT Application");
}

public String getVersion() {
Expand All @@ -109,14 +108,13 @@ public String getVersion() {
version = p.getProperty("version", "");
}
} catch (Exception e) {
log.log(Level.INFO, "getVersion() ERROR:" + e.getMessage());
logger.error("getVersion() ERROR:" + e.getMessage());
}

if (version == null) {
// we could not compute the version so use a blank
version = "N/A";
}

return version;
}

Expand All @@ -136,7 +134,7 @@ private void loadIRCTEventListeners() {
irctEventListener.registerListener(irctEvent);
}

log.info("Loaded " + allEventListeners.size() + " IRCT Event listeners");
logger.info("Loaded " + allEventListeners.size() + " IRCT Event listeners");
}

/**
Expand All @@ -161,7 +159,7 @@ private void loadDataConverters() {

}

log.info("Loaded " + allDCI.size() + " result data converters");
logger.info("Loaded " + allDCI.size() + " result data converters");
}

/**
Expand All @@ -178,7 +176,7 @@ private void loadJoins() {
for (IRCTJoin jt : oem.createQuery(criteria).getResultList()) {
this.supportedJoinTypes.put(jt.getName(), jt);
}
log.info("Loaded " + this.supportedJoinTypes.size() + " joins");
logger.info("Loaded " + this.supportedJoinTypes.size() + " joins");
}

/**
Expand All @@ -194,19 +192,19 @@ private void loadResources() {
CriteriaQuery<Resource> criteria = cb.createQuery(Resource.class);
Root<Resource> load = criteria.from(Resource.class);
criteria.select(load);
log.info("loadResources() "+criteria.toString());
logger.info("loadResources() "+criteria.toString());
for (Resource resource : oem.createQuery(criteria).getResultList()) {
try {
log.info("loadResources() Setting up resource:"+resource.toString()+" "+resource.getId()+" "+resource.getClass().toString());
logger.info("loadResources() Setting up resource:"+resource.toString()+" "+resource.getId()+" "+resource.getClass().toString());
resource.setup();
log.info("loadResources() resource ```"+resource.getName()+"``` has been loaded");
logger.info("loadResources() resource ```"+resource.getName()+"``` has been loaded");
this.resources.put(resource.getName(), resource);
} catch (ResourceInterfaceException e) {
log.warning("loadResources() Exception: "+e.getMessage());
logger.error("loadResources() Exception: "+e.getMessage());
e.printStackTrace();
}
}
log.info("loadResources() Loaded " + this.resources.size() + " resources");
logger.info("loadResources() Loaded " + this.resources.size() + " resources");
}

/**
Expand Down Expand Up @@ -255,6 +253,12 @@ public boolean doesResourceExist(String name) {
* @return Resources
*/
public Map<String, Resource> getResources() {
logger.debug("getResources()");

//Map<String, Resource> resources
for (Map.Entry<String, Resource> entry : resources.entrySet()) {
logger.debug("getResources() resource key: " + entry.getKey() + " value:" + entry.getValue().getName());
}
return resources;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@

import javax.naming.NamingException;

import org.apache.log4j.Logger;

import edu.harvard.hms.dbmi.bd2k.irct.event.IRCTEventListener;
import edu.harvard.hms.dbmi.bd2k.irct.exception.ResourceInterfaceException;
import edu.harvard.hms.dbmi.bd2k.irct.model.query.ClauseAbstract;
import edu.harvard.hms.dbmi.bd2k.irct.model.query.Query;
import edu.harvard.hms.dbmi.bd2k.irct.model.query.WhereClause;
Expand All @@ -18,8 +22,6 @@
import edu.harvard.hms.dbmi.bd2k.irct.model.result.ResultStatus;
import edu.harvard.hms.dbmi.bd2k.irct.model.security.SecureSession;
import edu.harvard.hms.dbmi.bd2k.irct.util.Utilities;
import edu.harvard.hms.dbmi.bd2k.irct.event.IRCTEventListener;
import edu.harvard.hms.dbmi.bd2k.irct.exception.ResourceInterfaceException;

/**
* Implements the Action interface to run a query on a specific instance
Expand All @@ -35,7 +37,8 @@ public class QueryAction implements Action {
private Result result;

private IRCTEventListener irctEventListener;

private Logger logger = Logger.getLogger(this.getClass());

/**
* Sets up the action to run a given query on a resource
*
Expand All @@ -45,14 +48,22 @@ public class QueryAction implements Action {
* Run the query
*/
public void setup(Resource resource, Query query) {
logger.debug("setup() Starting...");

if (resource == null) {
logger.warn("setup() resource is null");
}
this.query = query;
this.resource = resource;
this.status = ActionStatus.CREATED;
this.irctEventListener = Utilities.getIRCTEventListener();
logger.debug("setup() Finished.");
}

@Override
public void updateActionParams(Map<String, Result> updatedParams) {
logger.debug("updateActionParams() Starting...");

for (String key : updatedParams.keySet()) {
Long clauseId = Long.valueOf(key.split(".")[0]);
String parameterId = key.split(".")[1];
Expand All @@ -64,33 +75,50 @@ public void updateActionParams(Map<String, Result> updatedParams) {
updatedParams.get(key).getId().toString());
}
}
logger.debug("updateActionParams() Finished.");
}

@Override
public void run(SecureSession session) {
logger.debug("run() Starting...");

logger.debug("run() calling `beforeQuery`");
irctEventListener.beforeQuery(session, resource, query);
logger.debug("run() set status to "+ActionStatus.RUNNING);
this.status = ActionStatus.RUNNING;
try {
logger.debug("run() getting `QueryResourceImplementationInterface` object");
QueryResourceImplementationInterface queryInterface = (QueryResourceImplementationInterface) resource
.getImplementingInterface();

if (queryInterface == null) {
logger.error("run() `queryInterface` is not found for "+resource.getName()+" resource");
throw new RuntimeException("Unknown implementing interface for resource `"+resource.getName()+"`");
}

this.result = ActionUtilities.createResult(queryInterface
.getQueryDataType(query));
logger.debug("run() creating `result` field.");
this.result = ActionUtilities.createResult(queryInterface.getQueryDataType(query));

if (session != null) {
logger.debug("run() setting `user` field of the `result`.");
this.result.setUser(session.getUser());
}



logger.debug("run() calling runQuery() on the interface.");
this.result = queryInterface.runQuery(session, query, result);

// Update the result in the database
logger.debug("run() calling ActionUtilities.mergeResult(). This would persist the result in the database.");
ActionUtilities.mergeResult(this.result);
} catch (Exception e) {
this.result.setResultStatus(ResultStatus.ERROR);
this.result.setMessage(e.getMessage());
logger.error("run() Exception: "+e.getMessage());
this.status = ActionStatus.ERROR;
if (this.result == null) {
throw new RuntimeException(e);
} else {
this.result.setResultStatus(ResultStatus.ERROR);
this.result.setMessage(e.getMessage());
}
}
irctEventListener.afterQuery(session, resource, query);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@

import java.util.Date;
import java.util.concurrent.Callable;
import java.util.logging.Logger;

import javax.annotation.Resource;
import javax.ejb.Asynchronous;
Expand All @@ -18,6 +17,8 @@
import javax.persistence.PersistenceContext;
import javax.transaction.UserTransaction;

import org.apache.log4j.Logger;

import edu.harvard.hms.dbmi.bd2k.irct.action.JoinAction;
import edu.harvard.hms.dbmi.bd2k.irct.action.ProcessAction;
import edu.harvard.hms.dbmi.bd2k.irct.action.QueryAction;
Expand All @@ -44,8 +45,7 @@
@Stateless
public class ExecutionController {

@Inject
Logger log;
Logger logger = Logger.getLogger(this.getClass());

@PersistenceContext(unitName = "primary")
EntityManager entityManager;
Expand Down Expand Up @@ -103,31 +103,49 @@ public Long runProcess(IRCTProcess process, SecureSession secureSession)
*/
public Long runQuery(Query query, SecureSession secureSession)
throws PersistableException {
logger.debug("runQuery() Starting...");

Result newResult = new Result();
newResult.setJobType("EXECUTION");
logger.debug("runQuery() Set `newResult` as type:EXECUTION");

// Add the current user to the query.
newResult.setUser(secureSession.getUser());

logger.debug("runQuery() Set `newResult` as user:"+newResult.getUser().getUserId());

newResult.setResultStatus(ResultStatus.RUNNING);
logger.debug("runQuery() Set `newResult` as type:EXECUTION");

entityManager.persist(newResult);
logger.debug("runQuery() persited `newResult`");

QueryAction qa = new QueryAction();

edu.harvard.hms.dbmi.bd2k.irct.model.resource.Resource resource = (edu.harvard.hms.dbmi.bd2k.irct.model.resource.Resource) query.getResources().toArray()[0];
logger.debug("runQuery() `QueryAction` and `Resource` are set.");

if(!resource.isSetup()) {
logger.debug("runQuery() Resource `"+resource.getName()+"` is not set up, yet.");
resource = rc.getResource(resource.getName());
} else {
logger.debug("runQuery() Resource is already set up. "+resource.getName());
}
logger.debug("runQuery() Resource `"+resource.getName()+"` is now should be all set.");

logger.debug("runQuery() Setting up queryaction with resource and query.");
qa.setup(resource, query);
logger.debug("runQuery() `QueryAction` setup is now complete.");

ExecutableLeafNode eln = new ExecutableLeafNode();
eln.setAction(qa);
logger.debug("runQuery() Added `QueryAction` to `ExecutableLeafNode`.");

ExecutionPlan exp = new ExecutionPlan();
exp.setup(eln, secureSession);

logger.debug("runQuery() `ExecutionPlan` is all set up. Try runExecutionPlan() now");

runExecutionPlan(exp, newResult);

logger.debug("runQuery() returning `newResult.id` as "+newResult.getId());
return newResult.getId();
}

Expand Down Expand Up @@ -178,12 +196,16 @@ public Long runJoin(Join join, SecureSession secureSession)
@Asynchronous
public void runExecutionPlan(final ExecutionPlan executionPlan,
final Result result) throws PersistableException {


logger.debug("runExectionPlan() Starting...");
Callable<Result> runPlan = new Callable<Result>() {
@Override
public Result call() {
logger.debug("Callable.call() starting in inner class");

try {
result.setStartTime(new Date());
logger.debug("Callable.call() Calling run() on `executionPlan` "+executionPlan.getClass().getName());
executionPlan.run();

Result finalResult = executionPlan.getResults();
Expand All @@ -206,25 +228,37 @@ public Result call() {
}

result.setEndTime(new Date());

UserTransaction userTransaction = lookup();

userTransaction.begin();
entityManager.merge(result);
userTransaction.commit();

} catch (PersistableException e) {
logger.error("Callable.call() PersistableException:"+e.getMessage());

result.setResultStatus(ResultStatus.ERROR);
result.setMessage(e.getMessage());
} catch (Exception e) {
e.printStackTrace();
log.info(e.getMessage());
logger.error("Callable.call() Exception:"+e.getMessage());
result.setResultStatus(ResultStatus.ERROR);
} finally {

logger.debug("Callable.call() done with the method");
}
logger.debug("Callable.call() returning `result` with status "+result.getResultStatus().toString());
return result;
}
};

mes.submit(runPlan);
logger.debug("runExecutionPlan() created `Callable`. calling submit() of ManagedExecutionService");
try {
mes.submit(runPlan);
} catch (Exception e) {
logger.error("runExecutionPlan() >>>Exception: "+e.getMessage());
} finally {
logger.debug("runExecutionPlan() Finished.");
}
}

private UserTransaction lookup() throws NamingException {
Expand Down
Loading

0 comments on commit b3eefea

Please sign in to comment.