Initial populations

This commit is contained in:
2021-11-16 11:19:21 +07:00
parent cdac14e7fd
commit bee21d279a
2168 changed files with 849329 additions and 0 deletions
@@ -0,0 +1,108 @@
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>com.fourelementscapital</groupId>
<artifactId>lib-scheduler</artifactId>
<version>1.0</version>
</parent>
<groupId>com.fourelementscapital</groupId>
<artifactId>lib-scheduler-queue</artifactId>
<version>1.0</version>
<packaging>jar</packaging>
<name>lib-scheduler-queue</name>
<description>Scheduler Libraries related to queue</description>
<url>http://www.fourelementscapital.com</url>
<dependencies>
<dependency>
<groupId>com.googlecode.cqengine</groupId>
<artifactId>cqengine</artifactId>
<version>1.2.2</version>
</dependency>
<dependency>
<groupId>com.jcraft</groupId>
<artifactId>jsch</artifactId>
<version>0.1.50</version>
</dependency>
<dependency>
<groupId>io.netty</groupId>
<artifactId>netty-all</artifactId>
<version>4.0.9.Final</version>
</dependency>
<dependency>
<groupId>net.jxta</groupId>
<artifactId>jxta-jxse</artifactId>
<version>2.5</version>
</dependency>
<dependency>
<groupId>org.json</groupId>
<artifactId>json</artifactId>
<version>20090211</version>
</dependency>
<dependency>
<groupId>org.quartz-scheduler</groupId>
<artifactId>quartz</artifactId>
<version>2.1.1</version>
</dependency>
<dependency>
<groupId>org.apache.tomcat</groupId>
<artifactId>tomcat-catalina</artifactId>
<version>7.0.42</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.apache.jcs</groupId>
<artifactId>jcs</artifactId>
<version>1.3.3.1</version>
<systemPath>${pom.basedir}/lib/jcs-1.3.3.1-RC.jar</systemPath>
<scope>system</scope>
</dependency>
<dependency>
<groupId>org.mozilla.javascript.tools.shell</groupId>
<artifactId>js-14</artifactId>
<version>1.2.3</version>
<systemPath>${pom.basedir}/lib/js-14.jar</systemPath>
<scope>system</scope>
</dependency>
<dependency>
<groupId>com.fourelementscapital</groupId>
<artifactId>lib-fileutils</artifactId>
<version>2.0</version>
</dependency>
<dependency>
<groupId>com.fourelementscapital</groupId>
<artifactId>lib-db</artifactId>
<version>2.0</version>
</dependency>
<dependency>
<groupId>com.fourelementscapital</groupId>
<artifactId>lib-scheduler-exception</artifactId>
<version>1.0</version>
</dependency>
<dependency>
<groupId>com.fourelementscapital</groupId>
<artifactId>lib-scheduler-common</artifactId>
<version>1.0</version>
</dependency>
<dependency>
<groupId>com.fourelementscapital</groupId>
<artifactId>lib-scheduler-engines</artifactId>
<version>1.0</version>
</dependency>
<dependency>
<groupId>com.fourelementscapital</groupId>
<artifactId>lib-scheduler-p2p</artifactId>
<version>1.0</version>
</dependency>
</dependencies>
</project>
@@ -0,0 +1,159 @@
/******************************************************************************
*
* Copyright: Intellectual Property of Four Elements Capital Pte Ltd, Singapore.
* All rights reserved.
*
******************************************************************************/
package com.fourelementscapital.scheduler;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.TreeSet;
import java.util.Vector;
import com.fourelementscapital.db.SchedulerDB;
import com.fourelementscapital.scheduler.engines.ScheduledTask;
import com.fourelementscapital.scheduler.group.BBDownloadScheduledTask;
import com.fourelementscapital.scheduler.group.DirectRServeExecuteRUnix;
import com.fourelementscapital.scheduler.group.REngineScriptTask;
import com.fourelementscapital.scheduler.group.RScriptScheduledTask;
import com.fourelementscapital.scheduler.group.RServeScheduledTask;
import com.fourelementscapital.scheduler.group.RServeUnixTask;
public class ScheduledTaskFactory {
//private static Vector<ScheduledTask> scheduledTasks=new Vector();
//private static Vector<ScheduledTask> allTasks=new Vector();
private static List<ScheduledTask> scheduledTasks=Collections.synchronizedList(new ArrayList());
private static List<ScheduledTask> allTasks=Collections.synchronizedList(new ArrayList());
public ScheduledTaskFactory(){
init();
}
public synchronized void refreshTaskLoaded(){
synchronized(scheduledTasks){
if(allTasks.size()>0 || scheduledTasks.size()>0){
allTasks.clear();
scheduledTasks.clear();
init();
}
}
}
private synchronized void init(){
synchronized(scheduledTasks){
if(allTasks.size()>0 || scheduledTasks.size()>0){
}else{
allTasks.clear();
scheduledTasks.clear();
Vector venabled=SchedulerEngine.getEnabledTaskTypes();
SchedulerDB sdb=SchedulerDB.getSchedulerDB();
try{
sdb.connectDB();
Vector allgroups=sdb.getAllGroups();
for(Iterator i=allgroups.iterator();i.hasNext();){
Map data=(Map)i.next();
String taskuid=(String)data.get("taskuid");
String name=(String)data.get("name");
String enginetype=(String)data.get("enginetype");
ScheduledTask st=null;
if(enginetype.equalsIgnoreCase("rscript")){st=new RScriptScheduledTask(name,taskuid); }
if(enginetype.equalsIgnoreCase("rscript4rserve")){st=new RServeScheduledTask(name,taskuid); }
if(enginetype.equalsIgnoreCase("bb_download")){st=new BBDownloadScheduledTask(name,taskuid); }
if(enginetype.equalsIgnoreCase(RServeUnixTask.ENGINE_NAME)){st=new RServeUnixTask(name,taskuid); }
if(enginetype.equalsIgnoreCase("direct_script")){st=new REngineScriptTask(name,taskuid); }
if(enginetype.equalsIgnoreCase("direct_script_unix")){st=new DirectRServeExecuteRUnix(name,taskuid);}
//if(enginetype.equalsIgnoreCase("rscript4rconsole")){st=new RConsoleScript(name,taskuid);}
if(st!=null){
allTasks.add(st);
}
}
}catch(Exception e){
}finally{
try{
sdb.closeDB();
}catch(Exception e){}
}
for(Iterator<ScheduledTask> tasks=allTasks.iterator();tasks.hasNext(); ){
ScheduledTask task=tasks.next();
if(venabled.contains(task.getUniqueid())){
scheduledTasks.add(task);
}
}
//scheduledTasks.add(new MyTestScheduledTask());
}
}
}
public Set getTaskUids() {
TreeSet ts=new TreeSet();
synchronized(scheduledTasks){
for(Iterator<ScheduledTask> i=this.scheduledTasks.iterator();i.hasNext();){
ScheduledTask st=i.next();
ts.add(st.getUniqueid());
}
}
return ts;
}
public List<ScheduledTask> getTasks(){
return this.scheduledTasks;
}
public List<ScheduledTask> getAllConfiguredTasks(){
return this.allTasks;
}
public ScheduledTask getTask(String uid){
ScheduledTask rtn=null;
synchronized(scheduledTasks){
for(Iterator<ScheduledTask> i=getTasks().iterator();i.hasNext();){
ScheduledTask st=i.next();
if(st.getUniqueid().equals(uid)){
rtn=st ;
}
}
}
return rtn;
}
public ScheduledTask getTaskFromAll(String uid){
ScheduledTask rtn=null;
for(Iterator<ScheduledTask> i=this.allTasks.iterator();i.hasNext();){
ScheduledTask st=i.next();
if(st.getUniqueid().equals(uid)){
rtn=st ;
}
}
return rtn;
}
}
@@ -0,0 +1,162 @@
/******************************************************************************
*
* Copyright: Intellectual Property of Four Elements Capital Pte Ltd, Singapore.
* All rights reserved.
*
******************************************************************************/
package com.fourelementscapital.scheduler;
import java.util.Date;
import java.util.Map;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import com.fourelementscapital.db.SchedulerDB;
import com.fourelementscapital.scheduler.balance.LoadBalancingQueue;
import com.fourelementscapital.scheduler.balance.LoadBalancingQueueItem;
import com.fourelementscapital.scheduler.config.Config;
import com.fourelementscapital.scheduler.engines.ScheduledTask;
import com.fourelementscapital.scheduler.engines.StackFrame;
import com.fourelementscapital.scheduler.p2p.P2PService;
public class ScheduledTaskJob implements Job {
private Logger log = LogManager.getLogger(ScheduledTaskJob.class.getName());
public void execute(JobExecutionContext context) throws JobExecutionException {
//ate sdate=new Date();
//log.debug("Excecuted task previous fire time:"+context.getTrigger().getPreviousFireTime().getTime()+" next firetime:"+context.getTrigger().getNextFireTime().getTime());
/*
Map data=(Map)context.getJobDetail().getJobDataMap().get("data");
ScheduledTask task=(ScheduledTask)context.getJobDetail().getJobDataMap().get("task");
StackFrame sframe=(StackFrame)context.getJobDetail().getJobDataMap().get("stackframe");
*/
int scheduler_id=(Integer)context.getJobDetail().getJobDataMap().get("scheduler_id");
String taskuid=(String)context.getJobDetail().getJobDataMap().get("taskuid");
String invoked_by=(String)context.getJobDetail().getJobDataMap().get(SchedulerEngine.JOBDATA_INVOKED_BY);
String updatedtime=(String)context.getJobDetail().getJobDataMap().get(SchedulerEngine.JOBDATA_UPDATED_TIME);
Number trigger_row_id=(Number)context.getJobDetail().getJobDataMap().get(SchedulerEngine.JOBDATA_TRIGGER_ROW_ID);
//System.out.println("ScheduledTaskJob.execute() 2be removed later:taskuid:"+taskuid+" scheduler_id:"+scheduler_id);
try{
//Map data=getSchedulerData(scheduler_id);
SchedulerDB sdb=SchedulerDB.getSchedulerDB();
Map data=null;
String inject_code=null;
try{
sdb.connectDB();
data=sdb.getScheduler(scheduler_id);
if(trigger_row_id!=null){
Map trig=sdb.getOneRowTriggerData(trigger_row_id.longValue());
inject_code=(String)trig.get("inject_code");
}
}catch(Exception e){
throw e;
}finally {
sdb.closeDB();
}
if(data.get("deleted")!=null && ((Number)data.get("deleted")).intValue()==1){
throw new Exception("Deleted Task can't be executed");
}
log.debug("trigger_row_id:"+trigger_row_id+" inject_code:"+inject_code);
ScheduledTask task=new ScheduledTaskFactory().getTask(taskuid);
if(task==null) {
throw new Exception("Task Group not found for the task:"+scheduler_id);
}
StackFrame sframe=new StackFrame(task,data);
if(invoked_by!=null && !invoked_by.equals("")) {
sframe.setInvokedby(invoked_by);
}else{
if(updatedtime!=null){
sframe.setInvokedby("Scheduler ("+updatedtime+")");
}else{
sframe.setInvokedby("Scheduler");
}
}
if(context.getTrigger().getPreviousFireTime()!=null){
sframe.setTrigger_time(context.getTrigger().getPreviousFireTime().getTime());
}else{
sframe.setTrigger_time(new Date().getTime());
}
if(context.getTrigger().getNextFireTime()!=null){
sframe.setNexttrigger_time(context.getTrigger().getNextFireTime().getTime());
}
if(Config.getValue("load_balancing_server")!=null && Config.getValue("load_balancing_server").equals(P2PService.getComputerName())){
LoadBalancingQueueItem li=new LoadBalancingQueueItem();
li.setSf(sframe);
Integer id=(Integer)data.get("id");
li.setInject_code(inject_code);
li.setSchedulerid(id);
LoadBalancingQueue.getDefault().add(li);
}else{
/*ScheduledTaskQueue.add(sframe);*/
}
log.debug("adding task to queue: task:"+data.get("name"));
}catch(Exception e){
e.printStackTrace();
//ClientErrorMgmt.reportError(e, "Error while triggering task");
}
//String status=null;
//try{
//task.execute();
//status=task.EXCECUTION_SUCCESS;
//}catch(Exception e){
// status=task.EXCECUTION_FAIL;
// ClientErrorMgmt.reportError(e, null);
//}finally{
// try{
// addLog(sdate,data,status);
// }catch(Exception e){
// ClientErrorMgmt.reportError(e, null);
// }
//}
}
/**
* @deprecated
*/
private void addLog(Date start, Map data, String status) throws Exception {
SchedulerDB sdb=SchedulerDB.getSchedulerDB();
sdb.connectDB();
//System.out.println("ScheduledTaskJob.addLog() data:"+data);
Number nid=(Number)data.get("id");
String timezone=(String)data.get("timezone");
Date end=new Date();
int id= nid.intValue();
sdb.addSchedulerLog(id, start, end, timezone, status,null);
sdb.closeDB();
}
}
@@ -0,0 +1,375 @@
/******************************************************************************
*
* Copyright: Intellectual Property of Four Elements Capital Pte Ltd, Singapore.
* All rights reserved.
*
******************************************************************************/
package com.fourelementscapital.scheduler;
import java.util.Date;
import java.util.Iterator;
import java.util.Map;
import java.util.TreeMap;
import java.util.Vector;
import java.util.concurrent.ConcurrentLinkedQueue;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import com.fourelementscapital.db.SchedulerDB;
import com.fourelementscapital.scheduler.engines.ScheduledTask;
import com.fourelementscapital.scheduler.engines.SchedulerExePlanLogs;
import com.fourelementscapital.scheduler.engines.StackFrame;
import com.fourelementscapital.scheduler.error.ClientError;
import com.fourelementscapital.scheduler.group.RScriptScheduledTask;
import com.fourelementscapital.scheduler.p2p.P2PService;
/**
*
* @author Rams Kannan
*
*/
public class ScheduledTaskQueue implements Runnable {
private static ConcurrentLinkedQueue<StackFrame> queue= new ConcurrentLinkedQueue<StackFrame>();
//private static boolean threadRunning=false;
private Logger log = LogManager.getLogger(ScheduledTaskQueue.class.getName());
private static StackFrame curExecFrame=null;
/*
public static synchronized void add(ScheduledTask task, Map data){
queue.add(new StackFrame(task,data));
if(!threadRunning){
threadRunning=true;
new Thread(new ScheduledTaskQueue()).start();
}
}
*/
private static long lastExcecutedTime=0;
private static Thread thread=null;
/**
*@deprecated
*/
public static synchronized void add(StackFrame frame){
Number nid=(Number)frame.getData().get("id");
if(!queue.contains(frame)){
queue.add(frame);
new SchedulerExePlanLogs(nid.intValue(),frame.getTrigger_time()).log("Task added to local queue ",SchedulerExePlanLogs.IGNORE_CODE);
}else if(queue.contains(frame) && frame.isDependencyfailed()){
new SchedulerExePlanLogs(nid.intValue(),frame.getTrigger_time()).log("Task dependency failed ",SchedulerExePlanLogs.IGNORE_CODE);
queue.add(frame);
}
if(thread==null || (thread!=null && !thread.isAlive())){
thread=new Thread(new ScheduledTaskQueue());
thread.start();
}
/*
if(!threadRunning){
threadRunning=true;
new Thread(new ScheduledTaskQueue()).start();
}
*/
}
public static long lastExcecutedTime(){
return lastExcecutedTime;
}
public synchronized static boolean isExecutingOrQueued(){
boolean rtn=false;
if(queue.size()>0){
if(thread==null || (thread!=null && !thread.isAlive())){
thread=new Thread(new ScheduledTaskQueue());
thread.start();
}
}
if((thread!=null && thread.isAlive()) || queue.size()>0 || curExecFrame!=null){
rtn=true;
}
return rtn;
}
/**
* @deprecated
* @return
*/
public static synchronized Number getExecutingTaskId(){
if(isExecutingOrQueued()){
//if(addOrCheckQueue(null)>0){
return (Number)curExecFrame.getData().get("id");
}else{
return null;
}
}
public static synchronized StackFrame getExecutingStackFrame(){
if(isExecutingOrQueued()){
//if(addOrCheckQueue(null)>0){
return curExecFrame;
}else{
return null;
}
}
public static void killQueueThread() throws Exception {
try{
ScheduledTaskQueue.thread.interrupt();
}catch(Exception e){
throw e;
}
}
public void run() {
log.debug("thread started");
//threadRunning=true;
try{
while(!queue.isEmpty()){
//StackFrame sframe=queue.poll();
StackFrame sframe=queue.peek();
curExecFrame=sframe;
boolean scheduled_taskmode=true;
ScheduledTask task=sframe.getTask();
log.debug("Executing task1:"+sframe.getData().get("name"));
if(curExecFrame.getData().get("id")==null){
scheduled_taskmode=false;
}
String status=null;
Date sdate=new Date();
Number nid=(Number)sframe.getData().get("id");
try{
if(sframe.isDependencyfailed()){
//commented because warning symbol will let the user to see the error message
//execution overlap also added, so the below line commented to let the user know it is overlapped.
status=ScheduledTask.DEPENDENCY_TIMEOUT;
Thread.sleep(100);
}else{
//task.execute(sframe.getData(), null);
log.debug("just before executing task:sframe:"+sframe);
log.debug("just before executing task:data:"+sframe.getData());
log.debug("task"+task);
try{
traceHostAndStart(sdate,sframe.getData(),sframe);
//System.out.println("ScheduledTaskQueue.run() host update");
}catch(Exception e){
log.error("Error in updating queue log");
}
if(scheduled_taskmode){
new SchedulerExePlanLogs(nid.intValue(),sframe.getTrigger_time()).log("Execution started ",SchedulerExePlanLogs.IGNORE_CODE);
task.execute(sframe);
}else{
//task.execute(sframe);
new RScriptScheduledTask("Adhoc Rscript","rscript").executeScript(sframe);
}
log.debug("just after executed task");
status=ScheduledTask.EXCECUTION_SUCCESS;
lastExcecutedTime=new Date().getTime();
}
}catch(Exception e){
log.error("error:::::"+e.getMessage());
e.printStackTrace();
status=ScheduledTask.EXCECUTION_FAIL;
ClientError.reportError(e, null);
}finally{
if(sframe.getCallBack()!=null){
log.debug("sframe call back");
try{
sframe.getCallBack().callBack(sframe, status,null);
}catch(Exception e){
ClientError.reportError(e, null);
}
}
try{
log.debug("finally");
new SchedulerExePlanLogs(nid.intValue(),sframe.getTrigger_time()).log("Execution completed, Status:"+status,SchedulerExePlanLogs.IGNORE_CODE);
if(scheduled_taskmode){
int logid=addLog(sdate,sframe.getData(),status,sframe);
sframe.setLogid(logid);
}else{
addScriptLog(sdate,sframe.getData(),status,sframe);
}
}catch(Exception e){
e.printStackTrace();
ClientError.reportError(e, null);
}
queue.remove(sframe);
}
}
}catch(Exception e){
e.printStackTrace();
log.error("Errror while running this thread, Error:"+e.getMessage());
}finally{
//threadRunning=false;
log.debug("exiting thread:");
curExecFrame=null;
}
}
public synchronized static Map getQueuedIds(){
Vector rtn=new Vector();
Logger log = LogManager.getLogger(ScheduledTaskQueue.class.getName());
//currently executing task
TreeMap t=new TreeMap();
if(curExecFrame!=null){
//String uid=curExecFrame.getData().get("id")+"_"+curExecFrame.getTask().getTrigger_time();
String uid=curExecFrame.getData().get("id")+"_"+curExecFrame.getTrigger_time();
rtn.add(uid);
t.put("executing", uid);
}
for(Iterator<StackFrame> i=queue.iterator();i.hasNext();){
StackFrame stack=i.next();
String uid=stack.getData().get("id")+"_"+stack.getTrigger_time();
rtn.add(uid);
//log.debug("uid");
}
t.put("alltasks", rtn);
return t;
}
public synchronized static Vector<Number> getQueuedTaskIds(){
Vector rtn=new Vector();
Logger log = LogManager.getLogger(ScheduledTaskQueue.class.getName());
//currently executing task
if(curExecFrame!=null && curExecFrame.getData().get("id")!=null){
//String uid=curExecFrame.getData().get("id")+"_"+curExecFrame.getTask().getTrigger_time();
Number uid=(Number)curExecFrame.getData().get("id");
rtn.add(uid);
}
for(Iterator<StackFrame> i=queue.iterator();i.hasNext();){
StackFrame stack=i.next();
Number uid=(Number)stack.getData().get("id");
rtn.add(uid);
//log.debug("uid");
}
return rtn;
}
private void addScriptLog(Date start, Map data, String status,StackFrame sframe) throws Exception {
SchedulerDB sdb=SchedulerDB.getSchedulerDB();
sdb.connectDB();
//System.out.println("ScheduledTaskJob.addLog() data:"+data);
Number script_id=(Number)data.get("script_id");
//String timezone=(String)data.get("timezone");
Date end=new Date();
sdb.addRScriptLog(script_id.intValue(), P2PService.getComputerName(), status, start, end, sframe.getTasklog());
sdb.closeDB();
}
private void traceHostAndStart(Date start,Map data, StackFrame sframe) throws Exception {
SchedulerDB sdb=SchedulerDB.getSchedulerDB();
sdb.connectDB();
Number nid=(Number)data.get("id");
sdb.updateHostAndStarted(nid.intValue(), sframe.getTrigger_time(), start, P2PService.getComputerName());
sdb.closeDB();
}
private int addLog(Date start, Map data, String status,StackFrame sframe) throws Exception {
SchedulerDB sdb=SchedulerDB.getSchedulerDB();
sdb.connectDB();
//System.out.println("ScheduledTaskJob.addLog() data:"+data);
Number nid=(Number)data.get("id");
String timezone=(String)data.get("timezone");
Date end=new Date();
int id= nid.intValue();
int logid=sdb.addSchedulerLog(id, start, end, timezone, status,null);
//calculates different and adds in server's time
if(sframe.getStarted_time()>0){
long diff=end.getTime()-start.getTime();
start.setTime(sframe.getStarted_time());
long endtime=sframe.getStarted_time()+diff;
end.setTime(endtime);
}
try{
TreeMap record=new TreeMap();
record.put("scheduler_id", nid);
record.put("trigger_time", new Long(sframe.getTrigger_time()));
record.put("start_time", start);
record.put("end_time", end);
record.put("status", status);
record.put("is_triggered",new Integer(1));
record.put("log_id",new Integer(logid));
record.put("host",P2PService.getComputerName());
Vector v=new Vector();
v.add(record);
if(sframe.getNexttrigger_time()>0){
TreeMap record1=new TreeMap();
record1.put("scheduler_id", nid);
record1.put("trigger_time", new Long(sframe.getNexttrigger_time()));
record.put("log_id",new Integer(logid));
v.add(record1);
}
log.debug("logging job:+"+sframe.getTrigger_time()+" scheduler_Id:"+nid);
sdb.updateQueueLog(v,new Vector(), P2PService.getComputerName());
}catch(Exception e){
ClientError.reportError(e, null);
}
log.debug("before adding into db sframe.getTaskLog():"+sframe.getTasklog());
if(sframe.getTasklog()!=null && !sframe.getTasklog().equals("")){
try{
sdb.updateSchedulerLogMsg(logid,sframe.getTasklog());
}catch(Exception e){
log.error("Error while updating log message of R Engine:"+e.getMessage());
}
}
sdb.closeDB();
return logid;
}
}
@@ -0,0 +1,36 @@
/******************************************************************************
*
* Copyright: Intellectual Property of Four Elements Capital Pte Ltd, Singapore.
* All rights reserved.
*
******************************************************************************/
package com.fourelementscapital.scheduler;
import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import com.fourelementscapital.scheduler.balance.LoadBalancingQueue;
public class ScheduledTimeoutJob implements Job {
public static final String SCHEDULER_ID="scheduler_id";
public static final String TRIGGER_TIME="trigger_time";
public static final String STARTED_TIME="started_time";
public void execute(JobExecutionContext context) throws JobExecutionException {
int scheduler_id=(Integer)context.getJobDetail().getJobDataMap().get(ScheduledTimeoutJob.SCHEDULER_ID);
Number trigger_time=(Number)context.getJobDetail().getJobDataMap().get(ScheduledTimeoutJob.TRIGGER_TIME);
Number started_time=(Number)context.getJobDetail().getJobDataMap().get(ScheduledTimeoutJob.STARTED_TIME);
LoadBalancingQueue.getDefault().taskTimedOut(scheduler_id,trigger_time.longValue(),started_time.longValue());
//System.out.println("---Executing task");
//System.out.println("scheduler_id:"+scheduler_id);
//System.out.println("trigger_id:"+trigger_time);
}
}
@@ -0,0 +1,937 @@
/******************************************************************************
*
* Copyright: Intellectual Property of Four Elements Capital Pte Ltd, Singapore.
* All rights reserved.
*
******************************************************************************/
package com.fourelementscapital.scheduler;
import static org.quartz.CronScheduleBuilder.cronSchedule;
import static org.quartz.JobBuilder.newJob;
import static org.quartz.TriggerBuilder.newTrigger;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.TimeZone;
import java.util.TreeMap;
import java.util.Vector;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.mozilla.javascript.edu.emory.mathcs.backport.java.util.Arrays;
import org.quartz.CronScheduleBuilder;
import org.quartz.CronTrigger;
import org.quartz.JobDetail;
import org.quartz.JobKey;
import org.quartz.Scheduler;
import org.quartz.SchedulerFactory;
import org.quartz.Trigger;
import org.quartz.TriggerKey;
import org.quartz.impl.StdSchedulerFactory;
import org.quartz.impl.matchers.GroupMatcher;
import com.fourelementscapital.db.SchedulerDB;
import com.fourelementscapital.db.vo.ValueObject;
import com.fourelementscapital.scheduler.balance.LoadBalancingQueue;
import com.fourelementscapital.scheduler.balance.LoadBalancingQueueItem;
import com.fourelementscapital.scheduler.config.Config;
import com.fourelementscapital.scheduler.engines.ScheduledTask;
import com.fourelementscapital.scheduler.engines.StackFrame;
import com.fourelementscapital.scheduler.error.ClientError;
import com.fourelementscapital.scheduler.p2p.P2PService;
public class SchedulerEngine {
private Logger log = LogManager.getLogger(SchedulerEngine.class.getName());
public static final String SCHEDULE_TASK_GROUP="MY_SCHEDULER_Group";
public static final String SCHEDULE_TASK_TIMEOUT_GROUP="MY_SCHEDULER_TIMEOUT_Group";
public static final String JOBDATA_UPDATED_TIME="jobdata_updatedtime";
public static final String JOBDATA_INVOKED_BY="jobdata_invoked_by";
public static final String JOBDATA_TRIGGER_ROW_ID="trigger_row_id";
private String generateCronExpr(Map data) throws Exception {
String cronex="";
if(data.get("exp_second")!=null && !data.get("exp_second").equals("")){
cronex+=data.get("exp_second");
}else{
cronex+=" 0";
}
if(data.get("exp_minute")!=null && !data.get("exp_minute").equals("")){
cronex+=" "+data.get("exp_minute");
}else{
cronex+=" 0";
}
if(data.get("exp_hour")!=null && !data.get("exp_hour").equals("")){
cronex+=" "+data.get("exp_hour");
}else{
cronex+=" *";
}
boolean day_question_mark=false;
if(data.get("exp_day")!=null && !data.get("exp_day").equals("")){
cronex+=" "+data.get("exp_day");
}else{
cronex+=" ?";
day_question_mark=true;
}
if(data.get("exp_month")!=null && !data.get("exp_month").equals("")){
cronex+=" "+data.get("exp_month");
}else{
cronex+=" *";
}
if(data.get("exp_week")!=null && !data.get("exp_week").equals("")){
cronex+=" "+data.get("exp_week");
}else{
cronex+=(day_question_mark)? " *":" ?";
}
return cronex;
}
private String getNext5Times(Trigger trigger, TimeZone timez) throws Exception {
Calendar nextmin=Calendar.getInstance();
SimpleDateFormat format=new SimpleDateFormat("dd MMM, yyyy hh:mm:ss a");
SimpleDateFormat convert1=new SimpleDateFormat("dd MMM, yyyy HH:mm:ss");
//String localtime=null;
//if(timezone!=null && !timezone.equals("")){
convert1.setTimeZone(timez);
//localtime=trigger.getTimeZone().getDisplayName();
//}else{
// localtime=TimeZone.getDefault().getDisplayName();
//}
SimpleDateFormat convert2=new SimpleDateFormat("dd MMM, yyyy HH:mm:ss");
SimpleDateFormat today_tomm=new SimpleDateFormat("hh:mm:ss a");
String rtn="";
//rtn+="--------------------------------------\n";
int count=0;
Date nextfirtime=null;
if(trigger.getPreviousFireTime()!=null){
nextfirtime=trigger.getPreviousFireTime();
}else{
nextfirtime=trigger.getStartTime();
}
while(trigger.getFireTimeAfter(nextfirtime)!=null && count<5){
Date noccur1=trigger.getFireTimeAfter(nextfirtime);
Date noccur=convert2.parse(convert1.format(noccur1));
Date now1=new Date();
Date now=convert2.parse(convert1.format(now1));
Calendar cal1=Calendar.getInstance(); cal1.setTime(noccur);
Calendar cal2=Calendar.getInstance(); cal2.setTime(now);
String line=null;
if(
cal1.get(Calendar.DAY_OF_MONTH)==cal2.get(Calendar.DAY_OF_MONTH) &&
cal1.get(Calendar.MONTH)==cal2.get(Calendar.MONTH) &&
cal1.get(Calendar.YEAR)==cal2.get(Calendar.YEAR)
){
line="Today at "+today_tomm.format(noccur);
}
Calendar cal3=Calendar.getInstance(); cal3.setTime(now);
cal3.add(Calendar.DAY_OF_MONTH, 1);
if(
cal1.get(Calendar.DAY_OF_MONTH)==cal3.get(Calendar.DAY_OF_MONTH) &&
cal1.get(Calendar.MONTH)==cal3.get(Calendar.MONTH) &&
cal1.get(Calendar.YEAR)==cal3.get(Calendar.YEAR)
){
line="Tomorrow at "+today_tomm.format(noccur);
}
String rtn1=null;
if(line==null){
rtn1="\n"+format.format(noccur);
}else{
rtn1="\n"+line;
}
//System.out.println("calcuating:"+rtn1);
nextfirtime=trigger.getFireTimeAfter(nextfirtime);
rtn+=rtn1;
count++;
}
return rtn;
}
public String getNext5Times(String taskname, String taskuid, TimeZone timezone,int scheduler_id) throws Exception {
String job_tri_name=generateUniqueJobName(taskname,taskuid,scheduler_id,0);
SchedulerFactory sf=new StdSchedulerFactory();
Scheduler scheduler=sf.getScheduler();
if(!scheduler.isStarted()){
scheduler.start();
}
Trigger trigger = scheduler.getTrigger(new TriggerKey(job_tri_name, SCHEDULE_TASK_GROUP ));
//Trigger trigger=scheduler.getTrigger(job_tri_name,SCHEDULE_TASK_GROUP +taskuid);
if(trigger!=null){
String rtn=getNext5Times(trigger, timezone) ;
return rtn;
}else{
return null;
}
}
/*
public static Vector getEnabledTaskTypes(){
String services=Config.getString("scheduler_services_on");
StringTokenizer st=new StringTokenizer(services,",");
Vector vservices=new Vector();
while(st.hasMoreTokens()){
String token=st.nextToken();
vservices.add(token);
}
return vservices;
}
*/
public static Vector getEnabledTaskTypes() {
Vector vservices=new Vector();
SchedulerDB sdb=SchedulerDB.getSchedulerDB();
try{
sdb.connectDB();
Vector tasks=sdb.getActiveGroups();
for (Iterator i=tasks.iterator();i.hasNext();){
Map data=(Map)i.next();
//String token=st.nextToken();
vservices.add(data.get("taskuid"));
}
}catch(Exception e){
}finally{
try{
sdb.closeDB();
}catch(Exception e){}
}
return vservices;
}
public void startSchedulerQueue() throws Exception {
log.debug("startSchedulerQueue() called ()");
log.debug("scheduler queue called at:"+new Date());
SchedulerDB sdb=SchedulerDB.getSchedulerDB();
sdb.connectDB();
List<Map> datalist=sdb.listScheduler();
Vector vs=getEnabledTaskTypes();
String tasktypes="";
for(Map data : datalist){
String taskuid=(String)data.get("taskuid");
log.debug("taskuid:"+taskuid);
if(vs.contains(taskuid)){
tasktypes+=(tasktypes.equals("") ? "'"+taskuid+"'":",'"+taskuid+"'");
}
}
sdb.removeQueueLogs(new Date().getTime(),tasktypes);
log.debug("vs:"+vs);
for(Map data : datalist){
String taskuid=(String)data.get("taskuid");
log.debug("taskuid:"+taskuid);
if(vs.contains(taskuid)){
Number number=(Number)data.get("id");
Map data1=sdb.getScheduler(number.intValue());
try{
updateJob(data1,taskuid,sdb);
}catch(Exception e){
log.error("Error while adding task id:"+number+" to the queue");
//ClientErrorMgmt.reportError(e, "Error while adding task id:"+number+" to the queue");
}
}
}
sdb.closeDB();
}
public void removeGroup2Queue(String taskuid) throws Exception {
SchedulerDB sdb=SchedulerDB.getSchedulerDB();
sdb.connectDB();
log.debug("removeGroup2Queue() called");
try{
List<Map> datalist=sdb.listAllTasksByUID(taskuid);
Vector vs=getEnabledTaskTypes();
if(!vs.contains(taskuid)){
sdb.removeQueueLogs(new Date().getTime(),"'"+taskuid+"'");
//log.debug("vs:"+vs);
//for(Map data : datalist){
// Number number=(Number)data.get("id");
// Map data1=sdb.getScheduler(number.intValue());
// updateJob(data1,taskuid,sdb);
//}
SchedulerFactory sf=new StdSchedulerFactory();
Scheduler scheduler=sf.getScheduler();
if(!scheduler.isStarted()){
throw new Exception("Removing tasks failed, scheduler is not started first of all");
}
new ScheduledTaskFactory().refreshTaskLoaded();
//quartz 1.8.x to quartz 2.1.1 migration
/*
String jobnames[]=scheduler.getJobNames(SCHEDULE_TASK_GROUP+taskuid);
for(int i=0;i<jobnames.length;i++){
JobDetail ojob=scheduler.getJobDetail(jobnames[i],SCHEDULE_TASK_GROUP+taskuid );
if(ojob!=null){
log.debug("Removing job from the queue: name:"+jobnames[i]);
scheduler.deleteJob(jobnames[i],SCHEDULE_TASK_GROUP+taskuid);
}
}
*/
// String jobnames[]=scheduler.getJobNames(SCHEDULE_TASK_GROUP+taskuid);
Set<JobKey> jobs= scheduler.getJobKeys(GroupMatcher.jobGroupEquals(SCHEDULE_TASK_GROUP));
for(Iterator<JobKey> i=jobs.iterator();i.hasNext();){
JobKey jky=i.next();
//JobDetail ojob=scheduler.getJobDetail(jobnames[i],SCHEDULE_TASK_GROUP+taskuid );
JobDetail ojob=scheduler.getJobDetail(jky);
if(ojob!=null){
//log.debug("Removing job from the queue: name:"+jobnames[i]);
scheduler.deleteJob(jky);
}
}
}else{
throw new Exception("Taskuid:"+taskuid+" is active, please deactivate before remove them from queue");
}
}catch(Exception e){
throw e;
}finally{
sdb.closeDB();
}
}
public void addGroup2Queue(String taskuid) throws Exception {
log.debug("addGroup2Queue() called");
SchedulerDB sdb=SchedulerDB.getSchedulerDB();
sdb.connectDB();
try{
List<Map> datalist=sdb.listAllTasksByUID(taskuid);
Vector vs=getEnabledTaskTypes();
if(vs.contains(taskuid)){
sdb.removeQueueLogs(new Date().getTime(),"'"+taskuid+"'");
//log.debug("vs:"+vs);
new ScheduledTaskFactory().refreshTaskLoaded();
for(Map data : datalist){
Number number=(Number)data.get("id");
Map data1=sdb.getScheduler(number.intValue());
log.debug("adding task into live queue:id:"+number);
try{
updateJob(data1,taskuid,sdb);
}catch(Exception e){
log.error("Error while adding task id:"+number+" to the queue");
//ClientErrorMgmt.reportError(e, "Error while adding task id:"+number+" to the queue");
}
}
}else{
throw new Exception("Taskuid:"+taskuid+" is not active, please make it active before add them into queue");
}
}catch(Exception e){
throw e;
}finally{
sdb.closeDB();
}
}
/**
*
* @param name
* @param taskuid
* @param scheduler_id
* @param sequence
* @return
*/
public String generateUniqueJobName(String name, String taskuid, int scheduler_id, int sequence){
//this changed because, when task name or type changed the triggers are not updated once any of these changed.
//return taskuid+"_"+name+"_"+scheduler_id+"_"+sequence;
return scheduler_id+"_"+sequence;
}
/*
public String validateTaskData(Map data, String taskuid) throws Exception {
//log.debug("cron Expression:"+cronex);
//JobDetail holds the definition for Jobs
String cronex=generateCronExpr(data);
String name1=(String)data.get("name");
String job_tri_name=generateTriggerJobName(name1,taskuid);
JobDetail jobDetail = new JobDetail(job_tri_name, SCHEDULE_TASK_GROUP,ScheduledTaskJob.class);
jobDetail.getJobDataMap().put("data",data);
//jobDetail.getJobDataMap().put("task",getTask(taskuid));
String timezone=(String)data.get("timezone");
CronTrigger trigger=new CronTrigger(job_tri_name,SCHEDULE_TASK_GROUP,cronex);
if(timezone!=null && !timezone.equals("")){
trigger.setTimeZone(TimeZone.getTimeZone(timezone));
}
String localtime=null;
if(timezone!=null && !timezone.equals("")){
localtime=TimeZone.getTimeZone(timezone).getID();
}else{
localtime=TimeZone.getDefault().getID();
}
String rtn="";
rtn="The followings are task execution time ("+((localtime!=null)?localtime:timezone)+") pattern:\n";
rtn+="--------------------------------------\n";
rtn+=getNext5Times(trigger,trigger.getTimeZone());
rtn+="\n\nWould you like to continue saving this schedule? \n\n";
return rtn;
}
*/
public void updateJob(Map data, String taskuid, SchedulerDB sdb) throws Exception {
if(Config.getValue("load_balancing_server")!=null && Config.getValue("load_balancing_server").equals(P2PService.getComputerName())){
}else{
throw new Exception("Couldn't update as this computer configuration doesn't support load balancing");
}
SchedulerFactory sf=new StdSchedulerFactory();
Scheduler scheduler=sf.getScheduler();
if(!scheduler.isStarted()){
scheduler.start();
}
int scheduler_id=((Number)data.get("id")).intValue();
String name=(String)data.get("name");
for(int i=0;i<1000;i++){
String job_tri_name=generateUniqueJobName(name,taskuid,scheduler_id,i);
//JobDetail ojob=scheduler.getJobDetail(job_tri_name,SCHEDULE_TASK_GROUP+taskuid );
JobDetail ojob=scheduler.getJobDetail(new JobKey(job_tri_name,SCHEDULE_TASK_GROUP));
if(ojob!=null){
//scheduler.deleteJob(job_tri_name,SCHEDULE_TASK_GROUP+taskuid);
scheduler.deleteJob(new JobKey(job_tri_name,SCHEDULE_TASK_GROUP));
log.debug("deleleting job:"+job_tri_name);
}else{
i=1001;
}
}
sdb.removeQueueLog(new Date().getTime(),scheduler_id);
Vector trigdata=sdb.getTriggerData(scheduler_id);
Number active=(Number)data.get("active");
//activate only the active jobs.
if( (active==null || (active!=null && active.intValue()!=-1))
&& trigdata!=null && trigdata.size()>0
){
//String cronex=generateCronExpr(data);
int trigcount=0;
for(Iterator i=trigdata.iterator();i.hasNext();){
Map tdrow=(Map)i.next();
String cronex=
new SchedulerEngineUtils().generateCronExpr(
(String)tdrow.get("exp_second"),
(String)tdrow.get("exp_minute"),
(String)tdrow.get("exp_hour"),
(String)tdrow.get("exp_week"),
(String)tdrow.get("exp_day"),
(String)tdrow.get("exp_month")
);
String job_tri_name=generateUniqueJobName(name,taskuid,scheduler_id,trigcount);
//JobDetail jobDetail = new JobDetail(job_tri_name,SCHEDULE_TASK_GROUP+taskuid ,ScheduledTaskJob.class);
//jobDetail.getJobDataMap().put("scheduler_id",scheduler_id);
//jobDetail.getJobDataMap().put("taskuid",taskuid);
SimpleDateFormat sdf=new SimpleDateFormat("dd/MM/yyyy HH:mm:ss");
JobDetail jobDetail = newJob(ScheduledTaskJob.class)
.withIdentity(job_tri_name, SCHEDULE_TASK_GROUP)
.usingJobData("scheduler_id", scheduler_id)
.usingJobData("taskuid", taskuid)
.usingJobData(SchedulerEngine.JOBDATA_TRIGGER_ROW_ID, ((Number)tdrow.get("id")).longValue())
.usingJobData(SchedulerEngine.JOBDATA_UPDATED_TIME,sdf.format(new Date()))
.build();
String timezone=(String)data.get("timezone");
/*
CronTrigger trigger=new CronTrigger(job_tri_name,SCHEDULE_TASK_GROUP+taskuid,cronex);
if(timezone!=null && !timezone.equals("")){
trigger.setTimeZone(TimeZone.getTimeZone(timezone));
}
*/
try{
CronScheduleBuilder csb=cronSchedule(cronex);
List list=Arrays.asList(TimeZone.getAvailableIDs());
if(timezone!=null && !timezone.trim().equals("") && list.contains(timezone.trim())) {
csb.inTimeZone(TimeZone.getTimeZone(timezone));
}
CronTrigger trigger=newTrigger()
.withIdentity(job_tri_name,SCHEDULE_TASK_GROUP)
.withSchedule(csb)
.build();
Calendar nextmin=Calendar.getInstance();
scheduler.scheduleJob(jobDetail, trigger );
try{
Number nid=(Number)data.get("id");
TreeMap record=new TreeMap();
record.put("scheduler_id", nid);
record.put("trigger_time", new Long(trigger.getNextFireTime().getTime()));
Vector v=new Vector();
v.add(record);
sdb.updateQueueLog(v,new Vector(), P2PService.getComputerName());
}catch(Exception e){
ClientError.reportError(e, null);
}
}catch(Exception e){
log.error("Trigger Expression "+cronex+" failed for scheduler,please review the cron syntax "+scheduler_id);
}
trigcount++;
}
}
}
public void runJobDelayed(Map data, String taskuid, SchedulerDB sdb, int delay_in_minutes, String invoked_by) throws Exception {
SchedulerFactory sf = new StdSchedulerFactory();
Scheduler scheduler = sf.getScheduler();
if (!scheduler.isStarted()) {
scheduler.start();
}
int scheduler_id = ((Number) data.get("id")).intValue();
String name = (String) data.get("name");
Calendar c = Calendar.getInstance();
c.add(Calendar.MINUTE, delay_in_minutes);
SimpleDateFormat format = new SimpleDateFormat("ddMMyyHHmm");
// to avoid 2 tasks being added into the same time (minute), you need
// minimum 1 minute distance between 2 task in queue
log.debug("c:"+c.getTime());
log.debug("format:"+format);
log.debug("delay_in_minutes:"+delay_in_minutes);
String job_tri_name = name + "_" + format.format(c.getTime());
/*
JobDetail jobDetail = new JobDetail(job_tri_name, SCHEDULE_TASK_GROUP + taskuid, ScheduledTaskJob.class);
jobDetail.getJobDataMap().put("scheduler_id", scheduler_id);
jobDetail.getJobDataMap().put("taskuid", taskuid);
*/
JobDetail jobDetail = newJob(ScheduledTaskJob.class)
.withIdentity(job_tri_name, SCHEDULE_TASK_GROUP)
.usingJobData("scheduler_id", scheduler_id)
.usingJobData("taskuid", taskuid)
.usingJobData(JOBDATA_INVOKED_BY,invoked_by)
.build();
//String timezone = (String) data.get("timezone");
// CronTrigger trigger=new
// CronTrigger(job_tri_name,SCHEDULE_TASK_GROUP+taskuid,cronex);
/*
SimpleTrigger trigger = new SimpleTrigger(job_tri_name,
SCHEDULE_TASK_GROUP + taskuid);
trigger.setStartTime(c.getTime());
trigger.setNextFireTime(c.getTime());
trigger.setRepeatCount(0);
*/
Trigger trigger=newTrigger()
.withIdentity(job_tri_name, SCHEDULE_TASK_GROUP )
.startAt(c.getTime())
.endAt(c.getTime())
.build();
//System.out.println("SchedulerEngine.runJobDelayed(): nextScheduledTime():"+format.format(trigger.getNextFireTime())+" name:"+job_tri_name);
Calendar nextmin = Calendar.getInstance();
scheduler.scheduleJob(jobDetail, trigger);
try {
Number nid = (Number) data.get("id");
TreeMap record = new TreeMap();
record.put("scheduler_id", nid);
//record.put("trigger_time", new Long(trigger.getNextFireTime()
// .getTime()));
record.put("trigger_time", c.getTime().getTime());
Vector v = new Vector();
v.add(record);
sdb.updateQueueLog(v,new Vector(), P2PService.getComputerName());
} catch (Exception e) {
ClientError.reportError(e, null);
}
}
public boolean isSchedulerStarted() throws Exception {
SchedulerFactory sf=new StdSchedulerFactory();
Scheduler scheduler=sf.getScheduler();
if(scheduler.isStarted()){
return true;
}else{
return false;
}
}
public void removeJob(long schedulerid, Map data, String taskuid) throws Exception {
SchedulerFactory sf=new StdSchedulerFactory();
Scheduler scheduler=sf.getScheduler();
if(!scheduler.isStarted()){
scheduler.start();
}
String name=(String)data.get("name");
for(int i=0;i<1000;i++){
String job_tri_name=generateUniqueJobName(name,taskuid, (int)schedulerid,i);
//JobDetail ojob=scheduler.getJobDetail(job_tri_name,SCHEDULE_TASK_GROUP+taskuid );
JobDetail ojob=scheduler.getJobDetail(new JobKey(job_tri_name,SCHEDULE_TASK_GROUP));
if(ojob!=null){
//scheduler.deleteJob(job_tri_name,SCHEDULE_TASK_GROUP+taskuid);
scheduler.deleteJob(new JobKey(job_tri_name,SCHEDULE_TASK_GROUP));
log.debug("deleleting job:"+job_tri_name);
}else{
i=1001;
}
}
}
public synchronized String executeScriptExpression(String expression,String sender, String suffixcode) throws Exception {
ArrayList<ValueObject> v=new SchedulerEngineUtils().parseCodeInjection(expression);
SchedulerDB sdb=SchedulerDB.getSchedulerDB();
String rtn="";
try{
sdb.connectDB();
String failed_ids=null;
boolean success=false;
//for(Iterator i=v.iterator();i.hasNext();){
// String sid=(String)i.next();
for(ValueObject vo:v){
log.debug("vo:key:"+vo.getKey());
int scheduler_id=Integer.parseInt(vo.getKey());
//sdb.connectDB();
Map data=sdb.getScheduler(scheduler_id);
if(data!=null ){
if(data.get("deleted")==null || (data.get("deleted")!=null && ((Number)data.get("deleted")).intValue()!=1 )) {
Number active=(Number)data.get("active");
String taskuid=(String)data.get("taskuid");
String name=(String)data.get("name");
String vovalue=(vo.getValue()==null)?"": vo.getValue();
String inject=suffixcode!=null ? vovalue+"\n"+suffixcode:vovalue;
log.debug("inject:"+inject);
log.debug("vovalue:"+vovalue+" vo.key:"+vo.getKey()+" vo:"+vo.getValue());
new SchedulerEngine().executeJobNow(name, taskuid,data,sdb,sender,inject);
success=true;
}else{
//log.error("Scheduler ID:"+scheduler_id+" not exist or deleted");
failed_ids=(failed_ids==null)?scheduler_id+"":failed_ids+","+scheduler_id;
}
//sdb.closeDB();
}else{
//throw new Exception("Invalid ID");
}
}
if(success)
rtn="done "+(failed_ids!=null?", but "+failed_ids+" failed":"");
else
rtn="no success";
return rtn;
}catch(Exception e){
throw e;
}finally {
try{
sdb.closeDB();
}catch(Exception e){}
}
}
public synchronized void executeJobNow(String name, String taskuid, Map data, SchedulerDB sdb, String invoked_by, String inject_code) throws Exception {
if(data.get("deleted")!=null && ((Number)data.get("deleted")).intValue()==1){
throw new Exception("Deleted Task can't be executed");
}
SchedulerFactory sf=new StdSchedulerFactory();
Scheduler scheduler=sf.getScheduler();
if(!scheduler.isStarted()){
scheduler.start();
}
Number nid=(Number)data.get("id");
//String name=(String)data.get("name");
String job_tri_name=generateUniqueJobName(name,taskuid,nid.intValue(),0);
//JobDetail ojob=scheduler.getJobDetail(job_tri_name,SCHEDULE_TASK_GROUP );
Date trigtime=null;
//if(ojob!=null){
trigtime=new Date();
//scheduler.triggerJob(job_tri_name,SCHEDULE_TASK_GROUP);
//ScheduledTask task1=(ScheduledTask)ojob.getJobDataMap().get("task");
//Map data1=(Map)ojob.getJobDataMap().get("data");
//if it not already in the execution queue.
if(ScheduledTaskQueue.getQueuedTaskIds().contains(nid)){
throw new Exception("This task is currently in the queue or already excuting...");
}else{
//StackFrame sframe=(StackFrame)ojob.getJobDataMap().get("stackframe");
ScheduledTask task=new ScheduledTaskFactory().getTask(taskuid);
StackFrame sframe=new StackFrame(task,data);
sframe.setInvokedby(invoked_by);
sframe.setTrigger_time(new Date().getTime());
//Trigger trigger=scheduler.getTrigger(job_tri_name,SCHEDULE_TASK_GROUP+taskuid);
Trigger trigger=scheduler.getTrigger(new TriggerKey(job_tri_name,SCHEDULE_TASK_GROUP));
if(trigger!=null && trigger.getNextFireTime()!=null){
sframe.setNexttrigger_time(trigger.getNextFireTime().getTime());
}
//ScheduledTaskQueue.add(sframe);
if(Config.getValue("load_balancing_server")!=null && Config.getValue("load_balancing_server").equals(P2PService.getComputerName())){
LoadBalancingQueueItem li=new LoadBalancingQueueItem();
li.setSf(sframe);
Integer id=(Integer)data.get("id");
li.setSchedulerid(id);
li.setInject_code(inject_code);
LoadBalancingQueue.getDefault().add(li);
}else{
ScheduledTaskQueue.add(sframe);
}
TreeMap record=new TreeMap();
record.put("scheduler_id", nid);
record.put("trigger_time",sframe.getTrigger_time());
Vector v=new Vector();
v.add(record);
sdb.updateQueueLog(v,new Vector(), P2PService.getComputerName());
//sdb.closeDB();
log.debug("triggering job:"+job_tri_name+" time:"+sframe.getTrigger_time()+" scheduler_Id:"+nid);
}
//this listener exected once.
/*
task.setAddhocListener(job_tri_name,new ScheduledTaskJobAdhocListener() {
public void executeBeforeQueue(Trigger trigger, Map data){
try{
SchedulerDB sdb=new SchedulerDB();
sdb.connectDB();
log.debug("triggering: current:"+trigger.getPreviousFireTime());
Number nid=(Number)data.get("id");
TreeMap record=new TreeMap();
record.put("scheduler_id", nid);
record.put("trigger_time", trigger.getPreviousFireTime().getTime());
Vector v=new Vector();
v.add(record);
sdb.updateQueueLog(v);
sdb.closeDB();
}catch(Exception e){
ClientErrorMgmt.reportError(e, null);
}
}
});
*/
//}
}
/**
* @deprecated
* @return
* @throws Exception
*/
/*
public Collection getQueueTest() throws Exception {
SchedulerFactory sf=new StdSchedulerFactory();
Scheduler scheduler=sf.getScheduler();
//Vector r=new Vector();
//scheduler.shutdown();
TreeSet r=new TreeSet();
SimpleDateFormat format=new SimpleDateFormat("dd MMM, yyyy HH:mm:ss");
if(scheduler.isStarted()){
String jobs[]=scheduler.getJobNames(SCHEDULE_TASK_GROUP);
for(int i=0;i<jobs.length;i++){
SQueuedItem record= new SQueuedItem();
Trigger t[]=scheduler.getTriggersOfJob(jobs[i],SCHEDULE_TASK_GROUP );
Map data=(Map)scheduler.getJobDetail(jobs[i],SCHEDULE_TASK_GROUP).getJobDataMap().get("data");
Integer id=(Integer)data.get("id");
//log.debug("Job name:"+data.get("name"));
String name=(String)data.get("name");
record.put("id", id+"");
record.put("name", name);
for(int ia=0;ia<t.length;ia++){
Date nf=t[ia].getNextFireTime();
Date nf1=t[ia].getFireTimeAfter(nf);
record.put("execute_dt", nf);
record.put("execute_longtime", nf.getTime());
record.put("execute_at", format.format(nf));
record.put("execute_at1", format.format(nf1));
//if(dt1.get(id)!=null && nf.before(dt1.get(id))){
// dt1.put(id, nf);
//}else if(dt1.get(id)==null){
// dt1.put(id, nf);
//}
r.add(record);
}
}
//for(Iterator i=dt1.keySet().iterator();i.hasNext();){
//Integer ky=(Integer)i.next();
//log.debug(" ID:"+ky+" next first occ:"+ dt1.get(ky));
//}
}else{
throw new Exception("Scheduler isn't started yet");
}
return r;
}
*/
public class SQueuedItem extends TreeMap implements Comparable {
public int compareTo(Object o) {
SQueuedItem o1=(SQueuedItem)o;
Date thisdate=(Date)get("execute_dt");
Date otherdate=(Date)o1.get("execute_dt");
if(thisdate.after(otherdate)){
return 1;
}else{
return -1;
}
}
}
}
@@ -0,0 +1,188 @@
/******************************************************************************
*
* Copyright: Intellectual Property of Four Elements Capital Pte Ltd, Singapore.
* All rights reserved.
*
******************************************************************************/
package com.fourelementscapital.scheduler;
import static org.quartz.CronScheduleBuilder.cronSchedule;
import static org.quartz.TriggerBuilder.newTrigger;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.StringTokenizer;
import java.util.TimeZone;
import java.util.Vector;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.mozilla.javascript.edu.emory.mathcs.backport.java.util.Arrays;
import org.quartz.CronScheduleBuilder;
import org.quartz.Trigger;
import com.fourelementscapital.db.vo.ValueObject;
public class SchedulerEngineUtils {
private Logger log = LogManager.getLogger(SchedulerEngineUtils.class.getName());
public String generateCronExpr(String sec,String min, String hr, String dw,String dm,String mn) throws Exception {
String cronex="";
cronex+=(sec!=null && !sec.equals(""))?sec:" 0";
cronex+=(min!=null && !min.equals(""))?" "+min:" 0";
cronex+=(hr!=null && !hr.equals(""))?" "+hr:" *";
cronex+=(dm!=null && !dm.equals(""))?" "+dm:" ?";
boolean day_question_mark=false;
if(dm!=null && !dm.equals("")){}else{
day_question_mark=true;
}
cronex+=(mn!=null && !mn.equals(""))?" "+mn:" *";
cronex+=(dw!=null && !dw.equals(""))?" "+dw:((day_question_mark)? " *":" ?");
//System.out.println("SchedulerEngineUtils: cronex:"+cronex);
return cronex;
}
public Vector getTriggerTimes(String sec,String min, String hr, String dw,String dm,String mn, int numoftimes,String timezone) throws Exception {
String cronExp=generateCronExpr(sec,min,hr,dw,dm,mn);
//CronTrigger trigger=new CronTrigger("test","default",cronExp);
CronScheduleBuilder csb=cronSchedule(cronExp);
List list=Arrays.asList(TimeZone.getAvailableIDs());
if(timezone!=null && !timezone.trim().equals("") && list.contains(timezone.trim())) {
csb.inTimeZone(TimeZone.getTimeZone(timezone));
}
Trigger trigger=newTrigger()
.withIdentity("test","default")
.withSchedule(csb)
.build();
return getNext5Times(trigger,null,numoftimes,timezone);
}
private Vector getNext5Times(Trigger trigger, TimeZone timez, int numoftimes,String timezone) throws Exception {
Calendar nextmin=Calendar.getInstance();
SimpleDateFormat format=new SimpleDateFormat("dd MMM, yyyy hh:mm:ss a (EEE)");
SimpleDateFormat convert1=new SimpleDateFormat("dd MMM, yyyy HH:mm:ss");
//convert1.setTimeZone(timez);
SimpleDateFormat convert2=new SimpleDateFormat("dd MMM, yyyy HH:mm:ss");
SimpleDateFormat today_tomm=new SimpleDateFormat("hh:mm:ss a");
String rtn="";
String tzsuffix="";
List list=Arrays.asList(TimeZone.getAvailableIDs());
if(timezone!=null && !timezone.trim().equals("") && list.contains(timezone.trim())) {
tzsuffix=" "+TimeZone.getTimeZone(timezone).getDisplayName(false, TimeZone.SHORT)+"";
}
log.debug("Timezone:suffix:"+tzsuffix);
Vector rtnSDates=new Vector();
int count=0;
Date nextfirtime=null;
if(trigger.getPreviousFireTime()!=null){
nextfirtime=trigger.getPreviousFireTime();
}else{
nextfirtime=trigger.getStartTime();
}
while(trigger.getFireTimeAfter(nextfirtime)!=null && count<numoftimes){
Date noccur1=trigger.getFireTimeAfter(nextfirtime);
Date noccur=convert2.parse(convert1.format(noccur1));
Date now1=new Date();
Date now=convert2.parse(convert1.format(now1));
Calendar cal1=Calendar.getInstance(); cal1.setTime(noccur);
Calendar cal2=Calendar.getInstance(); cal2.setTime(now);
String line=null;
if(
cal1.get(Calendar.DAY_OF_MONTH)==cal2.get(Calendar.DAY_OF_MONTH) &&
cal1.get(Calendar.MONTH)==cal2.get(Calendar.MONTH) &&
cal1.get(Calendar.YEAR)==cal2.get(Calendar.YEAR)
){
line="Today at "+today_tomm.format(noccur) ;
}
Calendar cal3=Calendar.getInstance(); cal3.setTime(now);
cal3.add(Calendar.DAY_OF_MONTH, 1);
if(
cal1.get(Calendar.DAY_OF_MONTH)==cal3.get(Calendar.DAY_OF_MONTH) &&
cal1.get(Calendar.MONTH)==cal3.get(Calendar.MONTH) &&
cal1.get(Calendar.YEAR)==cal3.get(Calendar.YEAR)
){
line="Tomorrow at "+today_tomm.format(noccur);
}
String rtn1=null;
if(line==null){
rtn1="\n"+format.format(noccur)+tzsuffix;;
rtnSDates.add(format.format(noccur)+tzsuffix);
}else{
rtn1="\n"+line+tzsuffix;
rtnSDates.add(line+tzsuffix);
}
//System.out.println("calcuating:"+rtn1);
nextfirtime=trigger.getFireTimeAfter(nextfirtime);
rtn+=rtn1;
count++;
}
return rtnSDates;
}
public ArrayList<ValueObject> parseCodeInjection(String txt) {
Pattern p = Pattern.compile("(\\[)([0-9]+)(:inj\\])(.*?)(\\[inj\\])",Pattern.DOTALL);
Matcher m = p.matcher(txt);
ArrayList al=new ArrayList();
//System.out.println("count:"+m.groupCount());
while(m.find()){
if(m.groupCount()==5){
//System.out.println("id:"+m.group(2));
//System.out.println("code:"+m.group(4));
ValueObject vobj=new ValueObject();
vobj.setKey(m.group(2));
vobj.setValue(m.group(4));
al.add(vobj );
}
}
if(al.size()==0){
StringTokenizer st=new StringTokenizer(txt,",");
while(st.hasMoreTokens()){
ValueObject vobj=new ValueObject();
vobj.setKey(st.nextToken());
al.add(vobj );
}
}
log.debug("al:"+al);
return al;
}
}
@@ -0,0 +1,218 @@
/******************************************************************************
*
* Copyright: Intellectual Property of Four Elements Capital Pte Ltd, Singapore.
* All rights reserved.
*
******************************************************************************/
package com.fourelementscapital.scheduler.alarm;
import java.io.File;
import java.io.InputStream;
import java.io.StringWriter;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import org.apache.commons.io.IOUtils;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import com.fourelementscapital.alarm.Alarm;
import com.fourelementscapital.alarm.AlarmType;
import com.fourelementscapital.alarm.ThemeVO;
import com.fourelementscapital.scheduler.config.Config;
public class SchedulerAlarm {
public static String ALARM_SUB_FAILED="Failed";
public static String ALARM_SUB_TIMEOUT="Timeout";
public static String ALARM_SUB_CRASHED="Crashed";
public static String SCHEDULER_TEAM_THEME="computing";
private static final int SERVER_ERROR_ALARM_SENT=3005;
/**
* Construct alarm message using Scheduler data and send it to iMonitor API via lib-alarm
* @param vo SchedulerAlarmVO
* @throws Exception
*/
public static void sendAlarm(SchedulerAlarmVO vo) throws Exception {
final Logger log = LogManager.getLogger(Alarm.class.getName());
String bypass_phone_call_alarms = Config.getString("ignore.phone.alarm");
List<String> errList = Arrays.asList(bypass_phone_call_alarms.split(","));
boolean isIgnorePhone = "phone".equalsIgnoreCase(vo.getAlarmType()) && errList.contains(Integer.toString(vo.getErrCode()));
if(isIgnorePhone){
vo.setAlarmType("email"); // pass phone alarm. update alarmType to email.
}
boolean failed=vo.isRepCodeExist();
if(failed){
log.error("--tried sending alarm for alarm sent failure, trigger_time:"+vo.getTriggerTime()+" scheduler_id:"+vo.getSchedulerId());
}
SimpleDateFormat sdf=new SimpleDateFormat("hh:mm:ss");
ArrayList<String> themes=(ArrayList) vo.getThemeTags();
//put owner theme on top...
String owner_them=vo.getOwnerTheme();
if(owner_them!=null && themes.contains(owner_them)){
themes.remove(owner_them);
themes.add(0, owner_them);
}
Map hlog=vo.getQueueLog();
int respCode=0;
try{
respCode=((Number)hlog.get("response_code")).intValue();
}catch(Exception e){
//no code exist.
}
String msgfrom="";
if(vo.getFrom()!=null) {
String friendlyname=vo.getPeerFriendlyName();
msgfrom=""+(friendlyname!=null ?friendlyname+"("+vo.getFrom()+") " :vo.getFrom()+" ");
}
if(themes!=null && themes.size()>0){
String themes1="";
for(Iterator<String> i=themes.iterator();i.hasNext();) {
themes1+=themes1.equals("")?i.next():","+i.next();
}
String subject1="Task "+vo.getSchedulerId()+" ("+vo.getName()+") "+vo.getSubject()+((respCode>0)?",Err Code:"+respCode:"");
String composed="<div style='marging-bottom:30px;border:3px solid #D1D1D1;font-size:1.3em;background-color:#F0F0F0;padding:10px 20px;border-radius: 5px;'>";
composed+="<div>Script:<strong>"+vo.getName()+" ("+vo.getSchedulerId()+")"+"</strong></div>" ;
composed+="<div>Scheduled At:<strong>"+sdf.format(new Date(vo.getTriggerTime()))+"</strong></div>" ;
composed+="<div>Peer:<strong>"+msgfrom+"</strong></div>";
composed+="<div>Status:<strong>"+vo.getSubject()+"</strong></div>" ;
composed+="<div>Response Code:<strong>"+respCode+"</strong></div>" ;
composed+="<div><u>Message:</u></div>" ;
composed+="<div><pre style='margin:0px;color:red'>"+vo.getMessage()+"</pre></div>";
composed+="</div>";
composed+="<div>";
log.debug("resp_code:"+composed.length());
composed+=collectExecLogs(vo.getExecLogs());
if(respCode>0)composed+=getRespCodeError(respCode);
composed+=getConsoleMsg(vo.getConsoleMsg());
composed+="</div>";
log.debug("composed:"+composed);
boolean say=true;
if (vo.isExceptionSchedulerTeamRelated()) {
themes.add(SCHEDULER_TEAM_THEME);
subject1="[TO_COMPUTING]"+subject1;
}
// convert String array list to ThemeVO array list required by Alarm.sendAlarm() :
ArrayList<ThemeVO> themeList = new ArrayList<ThemeVO>();
for (int i=0; i<themes.size(); i++) {
themeList.add(new ThemeVO(themes.get(i)));
}
Alarm.sendAlarm(themeList, "email".equalsIgnoreCase(vo.getAlarmType()) ? AlarmType.EMAIL : AlarmType.PHONE, subject1, composed, false, "email".equalsIgnoreCase(vo.getAlarmType()), "phone".equalsIgnoreCase(vo.getAlarmType()), null, null);
}
}
/**
* Collect execution logs, format them in html
* @param execLogs Execution logs
* @return Execution logs in html
* @throws Exception
*/
private static String collectExecLogs(List<Map> execLogs) throws Exception {
final Logger log = LogManager.getLogger(SchedulerAlarm.class.getName());
try {
String rtn="<br><span style='margin-top:30px;margin-bottom:0px;font-size:1.5em'>Execution Log</span><table width='100%' cellpadding='3' cellspacing='0' border='1' style='border:1px solid #D1D1D1;border-collapse:collapse;'><thead><tr> <th align='left'>Date&Time</th> <th align='left'>Machine</th> <th align='left'>Code</th> <th align='left'>Message</th> </tr></thead>";
log.debug("collectExecLogs, list:"+execLogs);
if(execLogs==null || (execLogs!=null && execLogs.size()==0)){
return null;
}
rtn+="<tbody>";
for(Map record:execLogs){
String cls="";
int repcode=0;
try{
repcode=((Number)record.get("repcode")).intValue();
if((repcode>=3000 && repcode<=4999) || (repcode>=6000 && repcode<=7999)){
cls=" style='color:#d14836' ";
}
}catch(Exception e){
// do nothing
}
if(repcode!=SERVER_ERROR_ALARM_SENT){
rtn+="<tr "+cls+">";
rtn+="<td width='width:105px'>"+record.get("trans_datetime1")+"</td>";
rtn+="<td>"+record.get("machine")+"</td>";
rtn+="<td>"+((repcode>0)?repcode+"":"")+"</td>";
rtn+="<td>"+record.get("message")+"</td>";
rtn+="</tr>";
}
}
rtn+="</tbody></table>";
return rtn;
} catch (Exception e) {
return "Couldn't get execution logs, error:"+e.getMessage();
}
}
/**
* Get error description from html file, format it in html
* @param code Error code
* @return error description in html
*/
private static String getRespCodeError(int code) throws Exception {
final Logger log = LogManager.getLogger(SchedulerAlarm.class.getName());
ClassLoader cLoader = Alarm.class.getClassLoader();
InputStream inputStream = cLoader.getResourceAsStream("codehelp"+File.separator+code+".html");
if (inputStream != null && inputStream.available() > 0) {
StringWriter writer = new StringWriter();
IOUtils.copy(inputStream, writer);
return "<br><span style='margin-top:30px;margin-bottom:0px;font-size:1.5em'>Learn more about the issue</span>"+writer.toString();
}else{
log.error("codehelp"+File.separator+code+".html doesn't exist");
return "";
}
}
/**
* Get scheduler console message, format it in html
* @param consoleMsg Console message
* @return Console message in html
*/
private static String getConsoleMsg(String consoleMsg) {
try {
if(consoleMsg!=null && !consoleMsg.trim().equals("")){
String rtn="<br><span style='margin-top:30px;margin-bottom:0px;font-size:1.5em'>Console Output</span><div style='font-family: courier;'><pre style='margin-top:0px'>"+consoleMsg+"</pre></div>";
return rtn;
}else{
return "";
}
} catch (Exception e) {
return "Error: couldn't get console message, Error:"+e.getMessage();
}
}
}
@@ -0,0 +1,381 @@
/******************************************************************************
*
* Copyright: Intellectual Property of Four Elements Capital Pte Ltd, Singapore.
* All rights reserved.
*
******************************************************************************/
package com.fourelementscapital.scheduler.alarm;
import java.io.Serializable;
import java.util.List;
import java.util.Map;
/**
* Value object used as scheduler data container
*/
public class SchedulerAlarmVO implements Serializable {
private static final long serialVersionUID = 1L;
/**
* Scheduler id
*/
private int schedulerId;
/**
* Trigger time
*/
private long triggerTime;
/**
* Alarm type : 'phone' or 'email'
*/
private String alarmType;
/**
* Task's name
*/
private String name;
/**
* Alarm subject : 'Failed', 'Timeout', or 'Crashed'
*/
private String subject;
/**
* Message field contains Error log
*/
private String message;
/**
* From field contains Peer computer name
*/
private String from;
/**
* Error code
*/
private int errCode;
/**
* Is the error instance of ExceptionSchedulerTeamRelated
*/
private boolean isExceptionSchedulerTeamRelated;
/**
* Peer computer name
*/
private String computerName;
/**
* Console message
*/
private String consoleMsg;
/**
* Execution logs
*/
private List<Map> execLogs;
/**
* Is RepCode exist
*/
private boolean isRepCodeExist;
/**
* Task's theme tags
*/
private List themeTags;
/**
* Task's owner theme
*/
private String ownerTheme;
/**
* Task's queue log
*/
private Map queueLog;
/**
* Peer friendly name
*/
private String peerFriendlyName;
/**
* Get scheduler id
* @return scheduler id
*/
public int getSchedulerId() {
return schedulerId;
}
/**
* Set scheduler id
* @param schedulerId scheduler id
*/
public void setSchedulerId(int schedulerId) {
this.schedulerId = schedulerId;
}
/**
* Get trigger time
* @return trigger time
*/
public long getTriggerTime() {
return triggerTime;
}
/**
* Set trigger time
* @param triggerTime trigger time
*/
public void setTriggerTime(long triggerTime) {
this.triggerTime = triggerTime;
}
/**
* Get alarm type
* @return alarm type
*/
public String getAlarmType() {
return alarmType;
}
/**
* Set alarm type
* @param alarmType alarm type
*/
public void setAlarmType(String alarmType) {
this.alarmType = alarmType;
}
/**
* Get name
* @return name
*/
public String getName() {
return name;
}
/**
* Set name
* @param name name
*/
public void setName(String name) {
this.name = name;
}
/**
* Get subject
* @return subject
*/
public String getSubject() {
return subject;
}
/**
* Set subject
* @param subject subject
*/
public void setSubject(String subject) {
this.subject = subject;
}
/**
* Get message
* @return message
*/
public String getMessage() {
return message;
}
/**
* Set message
* @param message message
*/
public void setMessage(String message) {
this.message = message;
}
/**
* Get from
* @return from
*/
public String getFrom() {
return from;
}
/**
* Set from
* @param from from
*/
public void setFrom(String from) {
this.from = from;
}
/**
* Get error code
* @return error code
*/
public int getErrCode() {
return errCode;
}
/**
* Set error code
* @param errCode errCode
*/
public void setErrCode(int errCode) {
this.errCode = errCode;
}
/**
* Get is ExceptionSchedulerTeamRelated class
* @return true if ExceptionSchedulerTeamRelated class
*/
public boolean isExceptionSchedulerTeamRelated() {
return isExceptionSchedulerTeamRelated;
}
/**
* Set is ExceptionSchedulerTeamRelated class
* @param isExceptionSchedulerTeamRelated is ExceptionSchedulerTeamRelated class
*/
public void setExceptionSchedulerTeamRelated(
boolean isExceptionSchedulerTeamRelated) {
this.isExceptionSchedulerTeamRelated = isExceptionSchedulerTeamRelated;
}
/**
* Get computer name
* @return computer name
*/
public String getComputerName() {
return computerName;
}
/**
* Set computer name
* @param computerName computerName
*/
public void setComputerName(String computerName) {
this.computerName = computerName;
}
/**
* Get console message
* @return console message
*/
public String getConsoleMsg() {
return consoleMsg;
}
/**
* Set console message
* @param consoleMsg console message
*/
public void setConsoleMsg(String consoleMsg) {
this.consoleMsg = consoleMsg;
}
/**
* Get execution logs
* @return execution logs
*/
public List<Map> getExecLogs() {
return execLogs;
}
/**
* Set execution logs
* @param execLogs execution logs
*/
public void setExecLogs(List<Map> execLogs) {
this.execLogs = execLogs;
}
/**
* Set is rep code exist
* @return true if rep code exist
*/
public boolean isRepCodeExist() {
return isRepCodeExist;
}
/**
* Set is rep code exist
* @param isRepCodeExist is rep code exist
*/
public void setRepCodeExist(boolean isRepCodeExist) {
this.isRepCodeExist = isRepCodeExist;
}
/**
* Get theme tags
* @return theme tags
*/
public List getThemeTags() {
return themeTags;
}
/**
* Set theme tags
* @param themeTags theme tags
*/
public void setThemeTags(List themeTags) {
this.themeTags = themeTags;
}
/**
* Get owner theme
* @return owner theme
*/
public String getOwnerTheme() {
return ownerTheme;
}
/**
* Set owner theme
* @param ownerTheme owner theme
*/
public void setOwnerTheme(String ownerTheme) {
this.ownerTheme = ownerTheme;
}
/**
* Get queue log
* @return queue log
*/
public Map getQueueLog() {
return queueLog;
}
/**
* Set queue log
* @param queueLog queue log
*/
public void setQueueLog(Map queueLog) {
this.queueLog = queueLog;
}
/**
* Get peer friendly name
* @return peer friendly name
*/
public String getPeerFriendlyName() {
return peerFriendlyName;
}
/**
* Set peer friendly name
* @param peerFriendlyName peer friendly name
*/
public void setPeerFriendlyName(String peerFriendlyName) {
this.peerFriendlyName = peerFriendlyName;
}
}
@@ -0,0 +1,333 @@
/******************************************************************************
*
* Copyright: Intellectual Property of Four Elements Capital Pte Ltd, Singapore.
* All rights reserved.
*
******************************************************************************/
package com.fourelementscapital.scheduler.balance;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Timer;
import java.util.TimerTask;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import com.fourelementscapital.db.SchedulerDB;
import com.fourelementscapital.scheduler.alarm.SchedulerAlarm;
import com.fourelementscapital.scheduler.alarm.SchedulerAlarmVO;
import com.fourelementscapital.scheduler.balance.hsqldb.LoadBalancingHSQLQueue;
import com.fourelementscapital.scheduler.balance.hsqldb.LoadBalancingHSQLQueueItem;
import com.fourelementscapital.scheduler.engines.ScheduledTask;
import com.fourelementscapital.scheduler.engines.SchedulerExePlanLogs;
import com.fourelementscapital.scheduler.error.ClientError;
import com.fourelementscapital.scheduler.exception.ExceptionPeerNoResponse;
import com.fourelementscapital.scheduler.exception.ExceptionRemoveFromQ;
import com.fourelementscapital.scheduler.exception.ExceptionRemoveFromQNotInPeer;
import com.fourelementscapital.scheduler.exception.ExceptionSchedulerTeamRelated;
import com.fourelementscapital.scheduler.exception.SchedulerException;
import com.fourelementscapital.scheduler.p2p.P2PService;
import com.fourelementscapital.scheduler.p2p.listener.IncomingMessage;
import com.fourelementscapital.scheduler.p2p.peer.PeerMachine;
import com.fourelementscapital.scheduler.p2p.peer.PeerManagerHSQL;
public class ExecutingQueueCleaner extends TimerTask {
private static long frequency =65;
private Logger log = LogManager.getLogger(ExecutingQueueCleaner.class.getName());
private static TimerTask cleanerTask=null;
private static Timer timer=new Timer(); ;
public static void clean(){
if(cleanerTask==null){
cleanerTask=new ExecutingQueueCleaner();
long freq=frequency*1000;
//Timer timer = new Timer();
ExecutingQueueCleaner.timer.scheduleAtFixedRate(cleanerTask,freq, freq);
}
}
public static void stop(){
ExecutingQueueCleaner.timer.cancel();
}
public void run() {
log.debug("~~~~~~~~~~~~~~~~~~~~`Cleaning thread started ");
Collection<LoadBalancingQueueItem> exetasks=LoadBalancingQueue.getDefault().getExecutingTasks();
if(exetasks.size()>0){
try{
LoadBalancingQueue.getDefault().findAndUpdateOnlinePeers();
Thread.sleep(3000); //2 seconds to get all the reply at least.
}catch(Exception e){
//log.error("Error:"+e.getMessage());
}
//the following checks database to see if any items already finished but not updated in server queue.
SchedulerDB sdb1=SchedulerDB.getSchedulerDB();
try{
//calculates different and adds in server's time
sdb1.connectDB();
ArrayList suspects=sdb1.getLast10minuteSuspectedFailure();
for(Iterator itm=suspects.iterator();itm.hasNext();) {
Map record=(Map)itm.next();
Integer scd=(Integer)record.get("scheduler_id");
Long tr_time=(Long)record.get("trigger_time");
//LoadBalancingQueueItem lq1=LoadBalancingQueue.getItemFromProcessingQueue(scd.intValue(), tr_time.longValue());
//LoadBalancingQueueItem lq1=LoadBalancingQueue.getQueuedTasks()
Collection queue=LoadBalancingQueue.getDefault().getAllTasks();
boolean found=true;
if(queue.size()>0){
found=false;
}
for(Iterator<LoadBalancingQueueItem> i=queue.iterator();i.hasNext();){
LoadBalancingQueueItem item=i.next();
long trigg_time=0;
if(item instanceof LoadBalancingHSQLQueueItem) {
trigg_time= ((LoadBalancingHSQLQueueItem)item).getTrigger_time();
}else if(item.getSf()!=null){
trigg_time= item.getSf().getTrigger_time();
}
if(!found && trigg_time==tr_time.longValue() ){
found=true;
}
}
if(!found) {
Map log=sdb1.getQueueLog(scd.intValue(),tr_time.longValue());
if(log.get("status")==null || (log.get("status")!=null && ((String)log.get("status")).equals("") )){
sdb1.updateQueueNullStatus(scd.intValue(),tr_time.longValue(),"fail");
String msg="Checking based on the log, the item not found in the Q,hence updating the status as failed";
new SchedulerExePlanLogs(scd.intValue(),tr_time.longValue()).log(msg,sdb1,SchedulerExePlanLogs.SERVER_ERROR_REMOVING_QUEUE_BASEDON_LOG);
ExceptionRemoveFromQ exp=new ExceptionRemoveFromQ(msg);
sdb1.updateResponseCode(scd.intValue(),tr_time.longValue(), exp.getErrorcode());
Map data=sdb1.getScheduler(scd.intValue());
String type=(String)data.get("alert_type");
if(type!=null && !type.equals("")){
// send alarm :
int sc_id = scd.intValue();
long tri_time = tr_time.longValue();
SchedulerAlarmVO vo = new SchedulerAlarmVO();
vo.setAlarmType(type);
vo.setName((String)data.get("name"));
vo.setSubject(SchedulerAlarm.ALARM_SUB_FAILED);
vo.setMessage(msg);
vo.setFrom(null);
vo.setErrCode(exp.getErrorcode());
vo.setExceptionSchedulerTeamRelated(exp!=null && exp instanceof ExceptionSchedulerTeamRelated);
vo.setComputerName(P2PService.getComputerName());
vo.setConsoleMsg(sdb1.getConsoleMsg(sc_id, tri_time));
vo.setExecLogs(sdb1.getSchedulerExeLogs(sc_id, tri_time));
vo.setRepCodeExist(sdb1.execLogsRepcodeExist(sc_id, tri_time, SchedulerExePlanLogs.SERVER_ERROR_ALARM_SENT));
vo.setThemeTags(sdb1.getThemeTags(sc_id));
vo.setOwnerTheme(sdb1.getOwnerTheme(sc_id));
vo.setQueueLog(sdb1.getQueueLog(sc_id, tri_time));
vo.setPeerFriendlyName(sdb1.getPeerFriendlyName(vo.getFrom()));
vo.setSchedulerId(sc_id);
vo.setTriggerTime(tri_time);
SchedulerAlarm.sendAlarm(vo);
new SchedulerExePlanLogs(sc_id, tri_time).log("Alarm sent",sdb1,SchedulerExePlanLogs.SERVER_ERROR_ALARM_SENT);
}
}
}
}
}catch(Exception e) {
}finally{
try{sdb1.closeDB(); }catch(Exception e1){}
}
//tasks in executing queue but peer do not run it
//ArrayList scid_trig=new ArrayList();
//for(Iterator<LoadBalancingQueueItem> itms=exetasks.iterator();itms.hasNext(); ){
// LoadBalancingQueueItem itm=itms.next();
// scid_trig.add(itm.getSchedulerid()+"_"+itm.getSf().getTrigger_time());
//}
int last_milli_sec=5000; // 5minutes
int last_3mins_ms=1000*60*3;
LoadBalancingQueue lq=LoadBalancingQueue.getDefault();
//the following function disabled for a while, remove the whole block of code after 6 months (disabled on 23-may-2013)
if(lq instanceof LoadBalancingHSQLQueue ){
LoadBalancingHSQLQueue lqhsql=(LoadBalancingHSQLQueue)lq;
List<PeerMachine> onlinePeer=new PeerManagerHSQL().getOnlinePeers(last_milli_sec);
List<PeerMachine> online3mins=new PeerManagerHSQL().getOnlinePeers(last_3mins_ms);
List<LoadBalancingHSQLQueueItem> deaditem=lqhsql.getRunningMoreThan3Mins();
for(LoadBalancingHSQLQueueItem di:deaditem){
String scid_trig=di.getSchedulerid()+"_"+di.getTrigger_time();
PeerMachine pm=new PeerMachine(di.getMachine());
boolean alert=false;
String message=null;
SchedulerException se=null;
if(onlinePeer.indexOf(pm)>=0){
//peer reports no task running then remove
try{
sdb1.connectDB();
if(!onlinePeer.get(onlinePeer.indexOf(pm)).getRunning().contains(scid_trig) && !sdb1.isAnyExecLogsInLast3Mins(di.getSchedulerid(),di.getTrigger_time())){
message="Removing because this no longer running in peer";
//lqhsql.removeItemProcessing(di,message);
if(!lqhsql.removeFaultyProcessingTask(di.getSchedulerid(),di.getTrigger_time())){
message+=" Q Update failed";
}
new SchedulerExePlanLogs(di.getSchedulerid(),di.getTrigger_time()).log(message,SchedulerExePlanLogs.SERVER_ERROR_REMOVE_QUEUE_NOTRUNNING_INPEER);
se=new ExceptionRemoveFromQNotInPeer(message);
alert=true;
}
} catch (Exception e) {
log.error("Error11A, Err: " + e.getMessage());
}finally{
try{sdb1.closeDB(); }catch(Exception e1){}
}
}else{
try{
sdb1.connectDB();
if(!online3mins.contains(pm) && !sdb1.isAnyExecLogsInLast3Mins(di.getSchedulerid(),di.getTrigger_time())){
message="Removed from the Q: Peer not responded last 3 or more minutes";
if(!lqhsql.removeFaultyProcessingTask(di.getSchedulerid(),di.getTrigger_time())){
message+=" Q Update failed";
}
new SchedulerExePlanLogs(di.getSchedulerid(),di.getTrigger_time()).log(message,SchedulerExePlanLogs.SERVER_ERROR_REMOVING_QUEUE_PEER_NO_RESPONSE);
se=new ExceptionPeerNoResponse(message);
alert=true;
}
} catch (Exception e) {
log.error("Error11B, Err: " + e.getMessage());
}finally{
try{sdb1.closeDB(); }catch(Exception e1){}
}
}
if (alert) {
try {
sdb1.connectDB();
Map data = sdb1.getScheduler(di.getSchedulerid());
String type = (String) data.get("alert_type");
if (type != null && !type.equals("")) {
Map log=sdb1.getQueueLog(di.getSchedulerid(),di.getTrigger_time());
//send alert only if not success.
if(log.get("status")==null || (log.get("status")!=null
&& !((String)log.get("status")).equals(ScheduledTask.EXCECUTION_SUCCESS) )){
if(se!=null) sdb1.updateResponseCode(di.getSchedulerid(),di.getTrigger_time(), se.getErrorcode());
// send alarm :
int sc_id = di.getSchedulerid();
long tri_time = di.getTrigger_time();
SchedulerAlarmVO vo = new SchedulerAlarmVO();
vo.setAlarmType(type);
vo.setName((String)data.get("name"));
vo.setSubject(SchedulerAlarm.ALARM_SUB_FAILED);
vo.setMessage(message);
vo.setFrom(null);
vo.setErrCode(se.getErrorcode());
vo.setExceptionSchedulerTeamRelated(se!=null && se instanceof ExceptionSchedulerTeamRelated);
vo.setComputerName(P2PService.getComputerName());
vo.setConsoleMsg(sdb1.getConsoleMsg(sc_id, tri_time));
vo.setExecLogs(sdb1.getSchedulerExeLogs(sc_id, tri_time));
vo.setRepCodeExist(sdb1.execLogsRepcodeExist(sc_id, tri_time, SchedulerExePlanLogs.SERVER_ERROR_ALARM_SENT));
vo.setThemeTags(sdb1.getThemeTags(sc_id));
vo.setOwnerTheme(sdb1.getOwnerTheme(sc_id));
vo.setQueueLog(sdb1.getQueueLog(sc_id, tri_time));
vo.setPeerFriendlyName(sdb1.getPeerFriendlyName(vo.getFrom()));
vo.setSchedulerId(sc_id);
vo.setTriggerTime(tri_time);
SchedulerAlarm.sendAlarm(vo);
new SchedulerExePlanLogs(sc_id, tri_time).log("Alarm sent",sdb1,SchedulerExePlanLogs.SERVER_ERROR_ALARM_SENT);
}
}
} catch (Exception e) {
log.error("Error1, Err: " + e.getMessage());
}finally{
try{sdb1.closeDB(); }catch(Exception e1){}
}
}
}
}
}
}
public String removeFaultyExecutingTask(int scheduler_id, long trigger_time) throws Exception {
try{
/*
StringTokenizer st=new StringTokenizer(id_time,"_");
int scheduler_id=0;
long trigger_time=0;
if(st.countTokens()==2){
scheduler_id=Integer.parseInt(st.nextToken());
trigger_time=Long.parseLong(st.nextToken());
}
*/
boolean killedstatus=false;
if(scheduler_id>0 && trigger_time>0){
killedstatus=LoadBalancingQueue.getDefault().removeFaultyProcessingTask(scheduler_id,trigger_time);
//removes the scheduler task from peer data.
Map peertimes=IncomingMessage.getExecutingPeersTime();
//added to avoid to get currentModification error message.
synchronized(peertimes){
for(Iterator i=peertimes.values().iterator();i.hasNext();){
Map coll=(Map)i.next();
if(coll.keySet().contains(scheduler_id)){
coll.remove(scheduler_id);
}
}
}
}
if(killedstatus){
return "Task has been removed";
}else{
return "Removing failed";
}
}catch(Exception e){
ClientError.reportError(e, null);
throw e;
}
}
}
@@ -0,0 +1,869 @@
/******************************************************************************
*
* Copyright: Intellectual Property of Four Elements Capital Pte Ltd, Singapore.
* All rights reserved.
*
******************************************************************************/
package com.fourelementscapital.scheduler.balance;
import static org.quartz.JobBuilder.newJob;
import static org.quartz.TriggerBuilder.newTrigger;
import java.sql.Timestamp;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Collection;
import java.util.Date;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.StringTokenizer;
import java.util.TreeMap;
import java.util.Vector;
import java.util.concurrent.Semaphore;
import java.util.concurrent.TimeUnit;
import net.jxta.peergroup.PeerGroup;
import net.jxta.pipe.PipeService;
import net.jxta.protocol.PipeAdvertisement;
import org.apache.jcs.JCS;
import org.apache.jcs.engine.behavior.IElementAttributes;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.quartz.JobDetail;
import org.quartz.JobKey;
import org.quartz.Scheduler;
import org.quartz.SchedulerFactory;
import org.quartz.SimpleTrigger;
import org.quartz.impl.StdSchedulerFactory;
import com.fourelementscapital.db.SchedulerDB;
import com.fourelementscapital.scheduler.ScheduledTaskFactory;
import com.fourelementscapital.scheduler.ScheduledTimeoutJob;
import com.fourelementscapital.scheduler.SchedulerEngine;
import com.fourelementscapital.scheduler.alarm.SchedulerAlarm;
import com.fourelementscapital.scheduler.alarm.SchedulerAlarmVO;
import com.fourelementscapital.scheduler.balance.executeR.LoadBalancingNewExecuteRQueue;
import com.fourelementscapital.scheduler.balance.hsqldb.LoadBalancingHSQLQueue;
import com.fourelementscapital.scheduler.balance.hsqldb.LoadBalancingHSQLQueueItem;
import com.fourelementscapital.scheduler.engines.ScheduledTask;
import com.fourelementscapital.scheduler.engines.SchedulerExePlanLogs;
import com.fourelementscapital.scheduler.exception.ExceptionExecutionTimeout;
import com.fourelementscapital.scheduler.exception.ExceptionSchedulerTeamRelated;
import com.fourelementscapital.scheduler.p2p.MessageBean;
import com.fourelementscapital.scheduler.p2p.P2PAdvertisement;
import com.fourelementscapital.scheduler.p2p.P2PService;
import com.fourelementscapital.scheduler.p2p.listener.IncomingMessage;
import com.fourelementscapital.scheduler.p2p.listener.IncomingMessageParser;
import com.fourelementscapital.scheduler.p2p.listener.OutgoingMessage;
import com.fourelementscapital.scheduler.p2p.listener.P2PTransportMessage;
import com.fourelementscapital.scheduler.p2p.msg.PostMessage;
import com.fourelementscapital.scheduler.p2p.msg.scheduler.PeerOnlineStatus;
import com.fourelementscapital.scheduler.p2p.msg.scheduler.TenderSchedulerTask;
import com.fourelementscapital.scheduler.rscript.RScript;
import com.fourelementscapital.scheduler.rscript.RScriptListener;
/**
* This class is abstract for all load balancing algorithms,
* For example there are some commmon events for all queue processing,
*
*
* Some queue implementations for example queue implementation for ExecuteR server is not necessary to implement
*
*
* ***** Scheduler Task queue events ****
* add()
* startedIfNotStarted()
* executionEnded()
* taskTimedOut()
* getQueuedTasks()
* getItemFromProcessingQueue()
* getAllTasks()
* removeItemProcessing()
* killQueuedTask()
* executionFailed()
* cleanupProcessingQueue()
*
*
* ******* ExecuteR Script queue events
* addExecuteRScript()
* startedScriptIfNotStarted()
* scriptFinished()
* getScriptQueue()
* getScriptProcessingQueue()
*
*
* also other common events
* peerStarted()
* updatePeerData()
* findAndUpdateOnlinePeers()
*
*
*
*
*
*/
public abstract class LoadBalancingQueue {
private static Logger log = LogManager.getLogger(LoadBalancingQueue.class.getName());
//if your queue management implementation support scheduler task and
//executeR then it is necessary to explicitly set mode, otherwise it is not nessary
private final static int MODE_TASK=1;
private final static int MODE_SCRIPT=2;
private static LoadBalancingQueue loadBalancingQ=null;
private static LoadBalancingQueue loadERBalancingQ=null;
public static boolean priorityQueue=false;
public String PEER_QUEUE_RESP="PEER_QUEUE_RESP";
//java caching and attributes constants
protected JCS cache=null;
protected JCS cacheGrouped=null;
public static String CACHE_GROUP_TIMEOUT="cached_timedout";
public static String CACHE_GROUP_FINISHED="cached_finished";
public static String CACHE_GROUP_ACTIVEPEERS="cached_activepeers";
public static String CACHE_GROUP_TENDERSCHEDULERTASK="cached_tenderschedulertask";
public static int CACHE_GROUP_EXPIRY=45;
protected Vector<String> shuffleIteration=new Vector<String>();
private static Semaphore lock=new Semaphore(1,true);
public static synchronized LoadBalancingQueue getDefault(){
try{
lock.tryAcquire(1000,TimeUnit.MICROSECONDS);
if(LoadBalancingQueue.loadBalancingQ==null){
//LoadBalancingQueue.loadBalancingQ=new LoadBalancingLinkedQueue(MODE_TASK);;
LoadBalancingQueue.loadBalancingQ=new LoadBalancingHSQLQueue();;
}
lock.release();
}catch(Exception e){
log.error("Error while acquiring lock");
}
return LoadBalancingQueue.loadBalancingQ;
}
/**
* Get current default implementation for scheduler queue mangement
* @return
*/
public static synchronized LoadBalancingQueue getHSQLQueue(){
if(LoadBalancingQueue.loadBalancingQ==null){
LoadBalancingQueue.loadBalancingQ=new LoadBalancingHSQLQueue();
}
return LoadBalancingQueue.loadBalancingQ;
}
/**
* Get current default implementation for executeR queue management.
* @return
*/
public static synchronized LoadBalancingQueue getExecuteRScriptDefault(){
if(LoadBalancingQueue.loadERBalancingQ==null){
LoadBalancingQueue.loadERBalancingQ=new LoadBalancingNewExecuteRQueue();
}
return LoadBalancingQueue.loadERBalancingQ;
}
protected Vector getPeers4PriorityGr(String peer_query, String taskuid) throws Exception {
if(cache==null){
//cache=JCS.getInstance("perminentpeers");
getCache();
}
IElementAttributes att= cache.getDefaultElementAttributes();
att.setMaxLifeSeconds(4);
Vector peers=(Vector)cache.get("peers_priority_available");
if(peers!=null && peers.size()>0) {Object obj=peers.get(0);} //keep the obj in memory even after it expires.
if(peers==null){
SchedulerDB sdb=SchedulerDB.getSchedulerDB();
try{
sdb.connectDB();
peers=sdb.getAvailablePeers(peer_query, taskuid);
cache.put("peers_priority_available",peers,att);
}catch(Exception e){ log.error("ERROR:"+e.getMessage()+" Peer Query:"+peer_query+" taskuid:"+taskuid);}
finally{
sdb.closeDB();
}
}
return peers;
}
public void releasePeersCache4PriorityGr() {
if(cache!=null && cache.get("peers_priority_available")!=null){
try{
cache.remove("peers_priority_available");
}catch(Exception e){
}
}
}
protected JCS getCache() throws Exception {
if(cache==null){
cache=JCS.getInstance("LoadBalancingQueue");
}
return cache;
}
public JCS getGroupedCache() throws Exception {
if(cacheGrouped==null){
cacheGrouped=JCS.getInstance("LoadBalancingQueueGroupped");
}
return cacheGrouped;
}
public Vector<Object> getRunOnlyOn(String taskuid) {
try{
Vector<Object> rtn=(Vector<Object>)getCache().get("available_p_"+taskuid);
if(rtn!=null && rtn.size()>0){Object obj=rtn.get(0); } //to keep the object in memory even after it expires.
if(rtn==null){
SchedulerDB sdb=SchedulerDB.getSchedulerDB();
sdb.connectDB();
try{
rtn=sdb.getAssoAvailablePeers(taskuid);
}finally{
sdb.closeDB();
}
if(getCache()==null) getCache();
IElementAttributes att= getCache().getDefaultElementAttributes();
att.setMaxLifeSeconds(5);
getCache().put("available_p_"+taskuid,rtn,att);
}
return rtn;
}catch(Exception e){
//ClientErrorMgmt.reportError(e, null);
log.error("Error in retrieving associated peers ");
return new Vector<Object>();
}finally{
}
}
protected synchronized void sendTask2Peer(Vector<Object> peers,int scheduler_id, long trigger_time, long nextTrigger_time, String taskuid ) throws Exception {
boolean first=true;
Vector<String> v2=(Vector<String>)shuffleIteration.clone();
Vector<Object> v3=new Vector<Object>();
for(Iterator<String> i=v2.iterator();i.hasNext();){
Object ob=i.next();
if(peers.contains(ob)){v3.add(ob);};
}
peers.removeAll(v3);
peers.addAll(v3);
// StackFrame sframe=currentItem.getSf();
boolean firstElement=true;
boolean sent=false;
for(Iterator<Object> i=peers.iterator();i.hasNext();){
String clientname=(String)i.next();
TenderSchedulerTask tst=new TenderSchedulerTask();
tst.setScheduler_id(scheduler_id+"");
tst.setTrigger_time(trigger_time+"");
tst.setNext_trigger_time(nextTrigger_time+"");
tst.setTaskuid(taskuid);
String iden=scheduler_id+"_"+trigger_time;
//add into cache so it server doesn't have to answer tender respond call once it is started.
if(getGroupedCache().getFromGroup(iden, LoadBalancingQueue.CACHE_GROUP_TENDERSCHEDULERTASK)!=null){
getGroupedCache().remove(iden, LoadBalancingQueue.CACHE_GROUP_TENDERSCHEDULERTASK);
}
IElementAttributes att1= getGroupedCache().getDefaultElementAttributes();
att1.setMaxLifeSeconds(CACHE_GROUP_EXPIRY);
getGroupedCache().putInGroup(iden, LoadBalancingQueue.CACHE_GROUP_TENDERSCHEDULERTASK, "0");
//
new PostMessage(tst,clientname).send();
//Debugger.addDebugMsg("Msg to peer "+clientname+ " sc_id:"+ scheduler_id+" tr_time: "+trigger_time,clientname+ " "+ scheduler_id+" "+trigger_time);
//log.debug("Sending to message to peer:"+clientname);
if(first){
if(shuffleIteration.contains(clientname)){
shuffleIteration.remove(clientname);
}
shuffleIteration.add(clientname);
}
first=false;
Thread.sleep(5);
}
v2=null;
v3=null;
}
public final void updatePeerData(String what) throws Exception {
//IncomingMessage.getMessages().clear();
PeerGroup netPeerGroup=P2PService.getPeerGroup();
MessageBean mb=new MessageBean();
mb.setType(MessageBean.TYPE_REQUEST);
mb.setReply(MessageBean.REPLYBACK);
mb.setCommand(what);
Vector peers=getStaticClients();
for(Iterator i=peers.iterator();i.hasNext();){
String clientname=(String)i.next();;
PipeAdvertisement pipeAdv = new P2PAdvertisement().getPipeAdvertisement(clientname,netPeerGroup);
OutgoingMessage ogM=new OutgoingMessage(null,mb,clientname);
PipeService pipeService = P2PService.getPipeService();
try{
pipeService.createOutputPipe(pipeAdv,ogM);
}catch(Exception e){
e.printStackTrace();
}
}
//if(what.equals(STATISTICS)){
if(what.equals(P2PTransportMessage.COMMAND_STATISTICS)){
//update server's statistics
IncomingMessage.updatePeerStatistics(P2PService.getComputerName(),IncomingMessageParser.getServerStatistics());
}
//if(what.equals(PEER_QUEUE)){
if(what.equals(P2PTransportMessage.COMMAND_PEER_QUEUE)){
//update server's statistics
IncomingMessage.updatePeerQueueStat(P2PService.getComputerName(),IncomingMessageParser.getServerPeerQueueStat());
}
//if(what.equals(R_PACKAGES)){
if(what.equals(P2PTransportMessage.COMMAND_R_PACKAGES)){
//update server's package information
try{
//IncomingMessage.updatePeerRPackages(P2PService.getComputerName(),RScriptScheduledTask.getRPackageVersion());
IncomingMessage.updatePeerRPackages(P2PService.getComputerName(),null);
}catch(Exception e){
//ClientErrorMgmt.reportError(e, null);
throw e;
}
}
}
private Vector getStaticClients() throws Exception {
if(cache==null){
//cache=JCS.getInstance("perminentpeers");
getCache();
}
IElementAttributes att= cache.getDefaultElementAttributes();
att.setMaxLifeSeconds(7);
Vector peers1=(Vector)cache.get("perminentpeers");
if(peers1!=null && peers1.size()>0) {Object obj=peers1.get(0);} //to keep the object in memory even after cache expires.
if(peers1==null){
SchedulerDB sdb=SchedulerDB.getSchedulerDB();
sdb.connectDB();
try{
peers1=sdb.getPeersList();
}finally{
sdb.closeDB();
}
peers1.remove(P2PService.getComputerName());
//System.out.println("SchedulerMgmt.getOnlinePeers():peers:storing:"+peers1);
cache.put("perminentpeers",peers1,att);
//System.out.println("storing...");
}
return peers1;
}
public final void findAndUpdateOnlinePeers() throws Exception {
Vector peers=getStaticClients();
for(Iterator i=peers.iterator();i.hasNext();){
String clientname=(String)i.next();;
PeerOnlineStatus pos=new PeerOnlineStatus();
PostMessage ps=new PostMessage(pos,clientname);
ps.send();
}
}
public boolean isPeerBusyWithTask(String machinename,int bid_scheduler_id) {
//1 started at this call
//0 is not started at this call
//-1 invalid
boolean rtn=false;
SchedulerDB sdb=SchedulerDB.getSchedulerDB();
try{
Vector<LoadBalancingQueueItem> nonresp=new Vector<LoadBalancingQueueItem>();
Collection queuep= getExecutingTasks();
sdb.connectDB();
for(Iterator<LoadBalancingQueueItem> i=queuep.iterator();i.hasNext();){
LoadBalancingQueueItem item=i.next();
long trigger_time=0;
if(item instanceof LoadBalancingHSQLQueueItem) {
trigger_time=((LoadBalancingHSQLQueueItem) item).getTrigger_time();
}else{
trigger_time=item.getSf().getTrigger_time();
}
if(item.getMachine().equals(machinename)){
//nonresp.add(item);
//checks if bid response id and executing queque scheduler id is not the same, that means peer could have completed or crashed the
//previous task that sent to the peer.
if(item.getSchedulerid()!=bid_scheduler_id){
Map data=sdb.getQueueLog(item.getSchedulerid(),trigger_time);
if(data.get("status")!=null && data.get("host")!=null && data.get("end_time")!=null){
removeItemProcessing(item,"LoadBalancingQueue removing it from processing Queue",SchedulerExePlanLogs.SERVER_ERROR_PEER_CRASHED_REMOVED_QUEUE);
}else{
item.getSf().setTasklog("No response from "+machinename+" for executed task, probably peer could have been crashed");
item.getSf().setDependencyfailed(true);
item.setExecuting(false);
removeItemProcessing(item,"No response from "+machinename+" for executed task, probably peer could have been crashed",SchedulerExePlanLogs.SERVER_ERROR_PEER_NORESP_REMOVED_QUEUE);
}
}else{
rtn=true;
}
}
}
/*
if(nonresp.size()>0){
rtn=true;
}
for(Iterator<LoadBalancingQueueItem> i=nonresp.iterator();i.hasNext();){
LoadBalancingQueueItem item=i.next();
Map data=sdb.getQueueLog(item.getSchedulerid(),item.getSf().getTrigger_time());
if(data.get("status")!=null && data.get("host")!=null && data.get("end_time")!=null){
removeItemProcessing(item);
}else{
item.getSf().setTasklog("No response from "+machinename+" for executed task, probably peer could have been crashed");
item.getSf().setDependencyfailed(true);
item.setExecuting(false);
removeItemProcessing(item);
}
}
*/
}catch(Exception e){
e.printStackTrace();
}finally{
try{
sdb.closeDB();
}catch(Exception e){ }
}
return rtn;
}
protected final void removeTimeoutForTask(int scheduler_id, long trigger_time) {
try{
SchedulerFactory sf = new StdSchedulerFactory();
Scheduler scheduler = sf.getScheduler();
if(scheduler.isStarted()){
String job_tri_name=getTimeoutJobName(scheduler_id,trigger_time);
boolean isdeleted=scheduler.deleteJob(new JobKey(job_tri_name,SchedulerEngine.SCHEDULE_TASK_TIMEOUT_GROUP));
if(isdeleted){
//System.out.println("LoadBalancingQueue:removing timeout into queue scheduler_id:"+scheduler_id);
}
}
}catch(Exception e){
//System.out.println("LoadBalancingQueue:removing timeout into queue"+"error while removing timeout for the task, error:"+e.getMessage());
log.error("error while removing timeout for the task, error:"+e.getMessage()+" scheduler_id:"+scheduler_id);
}
}
protected void addLastExecutionDuration(SchedulerDB sdb, LoadBalancingQueueItem item) throws Exception {
try{
Map record=sdb.getLastSuccessfulQLog(item.getSchedulerid()) ;
if(record!=null){
Timestamp s=(Timestamp)record.get("start_time");
Timestamp e=(Timestamp)record.get("end_time");
long his_dura=e.getTime()-s.getTime();
item.setLastExecutedDuration(his_dura);
}
}catch(Exception e){
log.error("addLastExecutionDuration():"+e.getMessage());
}
}
protected void addTimeoutAndLastExecTimes(SchedulerDB sdb, LoadBalancingQueueItem item) throws Exception {
try{
long timeout=sdb.getMaxDurationInLast50Exec(item.getSchedulerid());
item.setOverlaptimeout(timeout);
LoadBalancingQueueTimeout tqt=new LoadBalancingQueueTimeout(sdb,new ScheduledTaskFactory().getTaskUids());
// if(tqt.getCriteriaQuery()!=null && !tqt.getCriteriaQuery().trim().equals("")) {
// move the query inside this method
long tc=sdb.getTimeoutCriteriaInMs(item.getSchedulerid());
if(tc>0){
int mins=Math.round(tc/1000/60);
if(mins<=tqt.getFewerminutes()){
item.setTimeoutexpiry(tqt.getFewerminutesexpiry()*1000*60);
}else{
item.setTimeoutexpiry(tc*tqt.getElsecritieriaxtime());
}
}
// }
}catch(Exception e){
e.printStackTrace();
//System.out.println("~~~~ LoadBalancingQueue.class Error 21:"+e.getMessage());
}
}
protected synchronized final void addTimeoutForTask(int scheduler_id, long trigger_time, long started_time, long expiry_time) {
try{
SchedulerFactory sf = new StdSchedulerFactory();
Scheduler scheduler = sf.getScheduler();
if(scheduler.isStarted() && expiry_time>0){
String job_tri_name=getTimeoutJobName(scheduler_id,trigger_time);
JobDetail ojob=scheduler.getJobDetail(new JobKey(job_tri_name,SchedulerEngine.SCHEDULE_TASK_TIMEOUT_GROUP));
if(ojob!=null){
scheduler.deleteJob(new JobKey(job_tri_name,SchedulerEngine.SCHEDULE_TASK_TIMEOUT_GROUP));
}
JobDetail jobDetail = newJob(ScheduledTimeoutJob.class)
.withIdentity(job_tri_name, SchedulerEngine.SCHEDULE_TASK_TIMEOUT_GROUP)
.usingJobData(ScheduledTimeoutJob.SCHEDULER_ID, scheduler_id)
.usingJobData(ScheduledTimeoutJob.TRIGGER_TIME, trigger_time)
.usingJobData(ScheduledTimeoutJob.STARTED_TIME, started_time)
.build();
Date exp=new Date();
exp.setTime(started_time+expiry_time);
SimpleTrigger trigger = (SimpleTrigger) newTrigger()
.withIdentity(job_tri_name, SchedulerEngine.SCHEDULE_TASK_TIMEOUT_GROUP)
.startAt(exp) // some Date
.build();
scheduler.scheduleJob(jobDetail, trigger );
SimpleDateFormat sdf=new SimpleDateFormat("dd.MMM.yyyy HH:mm:ss");
log.debug("----------LoadBalancingQueue:adding timeout into queue will be fired :"+sdf.format(exp)+" scheduler_id:"+scheduler_id);
}
}catch(Exception e ) {
log.error("error while setting timeout for the task, error:"+e.getMessage());
}
}
private String getTimeoutJobName(int scheduler_id, long trigger_time) {
return "timeout_"+scheduler_id+"_"+trigger_time;
}
public final void taskTimedOut(int scheduler_id, long trigger_time, long started_time) {
int step=0;
SchedulerDB sdb=SchedulerDB.getSchedulerDB();
try{
LoadBalancingQueueItem item=getItemFromProcessingQueue(scheduler_id,trigger_time);
removeItemProcessing(item,null,SchedulerExePlanLogs.SERVER_ERROR_EXEC_TIMEDOUT);
//removeFaultyProcessingTask(scheduler_id,trigger_time);
step=1;
sdb.connectDB();
Map data=sdb.getScheduler(scheduler_id);
step=2;
String type=(String)data.get("alert_type");
String name=(String)data.get("name");
String host=item.getMachine();
step=3;
IncomingMessage.updateFinishedPeersTime(host,scheduler_id,trigger_time); //remove from the updated
Map log=sdb.getQueueLog(scheduler_id, trigger_time);
step=4;
boolean success=false;
if(log!=null && log.get("status")!=null){
String status=(String)log.get("status");
if(status!=null && status.equalsIgnoreCase("success")){
success=true;
}
}
step=5;
if(!success){
new SchedulerExePlanLogs(scheduler_id,trigger_time).log("Execution TimedOut, removed from the Queue",SchedulerExePlanLogs.SERVER_ERROR_EXEC_TIMEDOUT);
TreeMap<String, Comparable> ldata=new TreeMap<String, Comparable>();
ldata.put("scheduler_id", scheduler_id);
ldata.put("trigger_time",trigger_time);
ldata.put("host",host);
ldata.put("status", ScheduledTask.TIMOUT_WARNING);
Vector<TreeMap<String, Comparable>> v=new Vector<TreeMap<String, Comparable>>();
v.add(ldata);
sdb.updateQueueLog(v,new Vector(), P2PService.getComputerName());
step=6;
Map<String,String> d1=sdb.getTaskEventActions(scheduler_id, trigger_time);
if(d1.containsKey(ScheduledTask.FIELD_DEPENDENCY_TIMEOUT) && d1.get(ScheduledTask.FIELD_DEPENDENCY_TIMEOUT)!=null
&& !d1.get(ScheduledTask.FIELD_DEPENDENCY_TIMEOUT).trim().equals("")
){
String expression=d1.get(ScheduledTask.FIELD_DEPENDENCY_TIMEOUT);
String suffi=ScheduledTask.TASK_EVENT_CALL_EXP_ID_VARIABLE+"="+scheduler_id+"\n";
suffi+=ScheduledTask.TASK_EVENT_CALL_EXP_TRIGGERTIME_VARIABLE+"="+trigger_time+"\n";
new SchedulerEngine().executeScriptExpression(expression, "onExecution timeout of "+scheduler_id, suffi);
}
step=7;
//SchedulerAlert sa=new SchedulerAlert(scheduler_id,trigger_time);
SimpleDateFormat sdf=new SimpleDateFormat("dd-MM-yyyy HH:mm:ss");
Date start_t=new Date(); start_t.setTime(started_time);
Date current=new Date();
String msg="Task timed-out! Task started at "+sdf.format(start_t)+" on peer "+host+" and no response till "+sdf.format(current)+" and removed from the queue";
step=8;
ExceptionExecutionTimeout exp=new ExceptionExecutionTimeout(msg);
sdb.updateResponseCode(scheduler_id, trigger_time, exp.getErrorcode());
step=9;
// send alarm :
int sc_id = scheduler_id;
long tri_time = trigger_time;
SchedulerAlarmVO vo = new SchedulerAlarmVO();
vo.setAlarmType(type);
vo.setName(name);
vo.setSubject(SchedulerAlarm.ALARM_SUB_TIMEOUT);
vo.setMessage(msg);
vo.setFrom(null);
vo.setErrCode(exp.getErrorcode());
vo.setExceptionSchedulerTeamRelated(exp!=null && exp instanceof ExceptionSchedulerTeamRelated);
vo.setComputerName(P2PService.getComputerName());
vo.setConsoleMsg(sdb.getConsoleMsg(sc_id, tri_time));
vo.setExecLogs(sdb.getSchedulerExeLogs(sc_id, tri_time));
vo.setRepCodeExist(sdb.execLogsRepcodeExist(sc_id, tri_time, SchedulerExePlanLogs.SERVER_ERROR_ALARM_SENT));
vo.setThemeTags(sdb.getThemeTags(sc_id));
vo.setOwnerTheme(sdb.getOwnerTheme(sc_id));
vo.setQueueLog(sdb.getQueueLog(sc_id, tri_time));
vo.setPeerFriendlyName(sdb.getPeerFriendlyName(vo.getFrom()));
vo.setSchedulerId(sc_id);
vo.setTriggerTime(tri_time);
SchedulerAlarm.sendAlarm(vo);
new SchedulerExePlanLogs(sc_id, tri_time).log("Alarm sent",sdb,SchedulerExePlanLogs.SERVER_ERROR_ALARM_SENT);
step=10;
}
/*
log.debug("sending alert message:"+msg);
String stop="cmd.exe /c sc stop 4EPeer";
String start="cmd.exe /c sc start 4EPeer";
String resp="<pre>"+SendCommand2Helper.sendCommand(host, stop);
Thread.sleep(3000);
resp+="<br>"+SendCommand2Helper.sendCommand(host, start);
resp+="</pre>";
new SchedulerExePlanLogs(scheduler_id,trigger_time).log("Peer Restarted, Output:"+resp);
*/
}catch(Exception e){
e.printStackTrace();
log.error("error while writing timedout log, e:"+e.getMessage()+" reached step:"+step);
}finally{
try{
sdb.closeDB();
}catch(Exception e1){}
}
}
/**
*
* @param currentItem
* @param ids
* @param timecheck
* @return 1=pass, 0=not pass, -1 =time out
*/
protected int dependencyCheck(LoadBalancingQueueItem currentItem, String ids, String timecheck,SchedulerDB sdb) {
int rtn=1;
long nexttrig=currentItem.getSf().getNexttrigger_time();
//time out just before 1 minute next trigger time
if(nexttrig>0){
Date next=new Date(nexttrig);
Calendar c1=Calendar.getInstance();
c1.setTime(next);
c1.add(Calendar.MINUTE, -1);
//log.debug("next trigger time:"+c1);
Date now=new Date();
if(now.after(c1.getTime())){
rtn=-1;
return rtn;
}
}
StringTokenizer st=new StringTokenizer(ids,",");
TreeMap<Number, String> idNumb=new TreeMap<Number, String> ();
Integer timespan=null;
while(st.hasMoreTokens()){
String cid=st.nextToken();
try{
idNumb.put(Integer.parseInt(cid),"fail");
}catch(Exception e){}
}
try{
timespan= Integer.parseInt(timecheck);
}catch(Exception e){}
if(idNumb.size()>0 && timespan>0){
String ids1=null;
for(Iterator<Number> i=idNumb.keySet().iterator();i.hasNext();){
ids1=(ids1==null)?""+i.next():ids1+","+i.next();
}
Calendar c=Calendar.getInstance();
c.add(Calendar.MINUTE, -timespan);
Date pdate=c.getTime();
//SchedulerDB sdb=SchedulerDB.getSchedulerDB();
try{
//log.debug(timespan+" minutes before "+pdate);
List data=(List)getCache().get("depids:"+ids);
if(data!=null && data.size()>0) { Object obj=data.get(0); }//this is to make sure the object doesn't expire even if the cache expires.
if(data==null){
IElementAttributes att= getCache().getDefaultElementAttributes();
att.setMaxLifeSeconds(3); //seconds maximum wait
data=sdb.listDependencyList(ids1,pdate);
getCache().put("depids:"+ids,data,att);
}
//log.debug("data: size()"+data.size());
if(data.size()>0){
//rtn=0;
for(Iterator reco=data.iterator();reco.hasNext();){
Map rdata=(Map)reco.next();
if(rdata.get("scheduler_id")!=null){
idNumb.put((Integer)rdata.get("scheduler_id"),"pass");
}
}
if(idNumb.containsValue("fail")){
rtn=0;
}
//log.debug("idNumb:"+idNumb);
}else{
rtn=0;
}
}catch(Exception e){
//e.printStackTrace();
}finally{
try{
// sdb.closeDB();
}catch(Exception e1){}
}
}
//log.debug("rtn:"+rtn);
return rtn;
}
public abstract void add(LoadBalancingQueueItem item);
public abstract void addExecuteR(RScript item, RScriptListener listener) throws Exception;
public abstract List<LoadBalancingQueueItem> getAllTasks();
public abstract long lastExcecutedTime();
public abstract RScript startScriptIfNotStarted(RScript rscript,String peer);
public abstract void scriptFinished(RScript rscript, String result,String status);
public abstract void removeScriptFromAllQueue(RScript rscript);
public abstract void removeItemProcessing(LoadBalancingQueueItem item,String message,int respCode);
public abstract LoadBalancingQueueItem getItemFromProcessingQueue(int scheduler_id, long trigger_time);
public abstract boolean killQueuedTask(int scheduler_id, long trigger_time);
//public abstract void taskTimedOut(int scheduler_id, long trigger_time, long started_time);
public abstract boolean removeFaultyProcessingTask(int scheduler_id, long trigger_time);
public abstract int startedIfNotStarted(int schedulerid,long trigger_time, String machinename);
public abstract void executionFailed(int schedulerid,long trigger_time, String machinename);
//public abstract void executionStarted(LoadBalancingQueueItem item,long trigger_time, String machinename);
public abstract void executionEnded(int schedulerid, long trigger_time);
/**
* @deprecated
*/
public abstract void cleanupProccesingQueue(int schedulerid, String computername);
public abstract Collection<LoadBalancingQueueItem> getExecutingTasks();
public abstract Collection<LoadBalancingQueueItem> getQueuedTasks();
public abstract void executeScript(Vector<Object> peers,int scriptid) throws Exception;
public abstract void peerStarted(int scheduler_id, long trigger_time, String peername) throws Exception;
/**
* @deprecated
* @param schedulerid
*/
public abstract void executionEnded(int schedulerid);
public abstract Collection<RScript> getScriptQueue() throws Exception;
public abstract Collection<RScript> getScriptProcessingQueue() throws Exception;
}
@@ -0,0 +1,130 @@
/******************************************************************************
*
* Copyright: Intellectual Property of Four Elements Capital Pte Ltd, Singapore.
* All rights reserved.
*
******************************************************************************/
package com.fourelementscapital.scheduler.balance;
import java.util.Date;
import com.fourelementscapital.scheduler.engines.StackFrame;
public class LoadBalancingQueueItem {
private int schedulerid;
private StackFrame sf;
private boolean executing=false;
private String machine=null;
private Date started=null;
private long lastExecutedDuration=0;
private long overlaptimeout=0;
private String inject_code=null;
private long timeoutexpiry=0;
public long getTimeoutexpiry() {
return timeoutexpiry;
}
public void setTimeoutexpiry(long timeoutexpiry) {
this.timeoutexpiry = timeoutexpiry;
}
public String getInject_code() {
return inject_code;
}
public void setInject_code(String inject_code) {
this.inject_code = inject_code;
}
public long getOverlaptimeout() {
return overlaptimeout;
}
public void setOverlaptimeout(long overlaptimeout) {
this.overlaptimeout = overlaptimeout;
}
public Date getStarted() {
return started;
}
public void setStarted(Date started) {
this.started = started;
}
public int getSchedulerid() {
return schedulerid;
}
public void setSchedulerid(int schedulerid) {
this.schedulerid = schedulerid;
}
public StackFrame getSf() {
return sf;
}
public void setSf(StackFrame sf) {
this.sf = sf;
}
public boolean isExecuting() {
return executing;
}
public void setExecuting(boolean executing) {
this.executing = executing;
}
public String getMachine() {
return machine;
}
public void setMachine(String machine) {
this.machine = machine;
}
public long getLastExecutedDuration() {
return lastExecutedDuration;
}
public void setLastExecutedDuration(long lastExecutedDuration) {
this.lastExecutedDuration = lastExecutedDuration;
}
public boolean equals1(Object other) {
if(other!=null && ((LoadBalancingQueueItem)other).getSchedulerid()==this.getSchedulerid()){
return true;
}else{
return false;
}
}
public boolean equals(Object o) {
LoadBalancingQueueItem other=(LoadBalancingQueueItem)o;
if(other!=null && (other.getSf()==null || this.getSf()==null )&& other.getSchedulerid()==this.getSchedulerid()){
return true;
} else if(other!=null && other.getSf()!=null && this.getSf()!=null && other.getSf().getTrigger_time()==this.getSf().getTrigger_time() && other.getSchedulerid()==this.getSchedulerid()){
return true;
}else{
return false;
}
}
public String toString(){
return "Running;"+this.executing+" id:"+this.schedulerid;
}
}
@@ -0,0 +1,102 @@
/******************************************************************************
*
* Copyright: Intellectual Property of Four Elements Capital Pte Ltd, Singapore.
* All rights reserved.
*
******************************************************************************/
package com.fourelementscapital.scheduler.balance;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;
import org.apache.commons.beanutils.BeanUtils;
import com.fourelementscapital.db.SchedulerDB;
public class LoadBalancingQueueTimeout {
public LoadBalancingQueueTimeout(SchedulerDB sdb, Set types) throws Exception {
Map data=sdb.getTimeoutSettings();
HashMap dset=new HashMap();
//LoadBalancingQueueTimeout lqt=new LoadBalancingQueueTimeout();
BeanUtils.populate(this, data);
for(Iterator tkeys=types.iterator();tkeys.hasNext();){
String tkey=(String)tkeys.next();
dset.put(tkey, data.get(tkey));
}
this.setMaxWaitingAlert(dset);
}
private int fewerminutes=1;
private int fewerminutesexpiry=2;
private int elsecritieriaxtime=2;
private String alert_theme="itools";
private String alert_type="Phone";
private HashMap<String,String> maxWaitingAlert=new HashMap();
public String getAlert_theme() {
return alert_theme;
}
public void setAlert_theme(String alert_theme) {
this.alert_theme = alert_theme;
}
public String getAlert_type() {
return alert_type;
}
public void setAlert_type(String alert_type) {
this.alert_type = alert_type;
}
public HashMap<String,String> getMaxWaitingAlert() {
return maxWaitingAlert;
}
public void setMaxWaitingAlert(HashMap<String,String> maxWaitingAlert) {
this.maxWaitingAlert = maxWaitingAlert;
}
public int getFewerminutes() {
return fewerminutes;
}
public void setFewerminutes(int fewerminutes) {
this.fewerminutes = fewerminutes;
}
public int getFewerminutesexpiry() {
return fewerminutesexpiry;
}
public void setFewerminutesexpiry(int fewerminutesexpiry) {
this.fewerminutesexpiry = fewerminutesexpiry;
}
public int getElsecritieriaxtime() {
return elsecritieriaxtime;
}
public void setElsecritieriaxtime(int elsecritieriaxtime) {
this.elsecritieriaxtime = elsecritieriaxtime;
}
/*
public TreeMap<String, String> getMaxWaitingAlert() {
TreeMap<String,String> range=new TreeMap<String, String>();
range.put("rscript4rserve11", "5");
range.put("xavier", "1");
range.put("rscript4rconsole_p1", "10");
range.put("rhinoscript1","1");
range.put("bb_download2", "1");
range.put("rhinoscript4priority", "1");
range.put("rscript4rserveunix", "20");
return range;
}
*/
}
@@ -0,0 +1,190 @@
/******************************************************************************
*
* Copyright: Intellectual Property of Four Elements Capital Pte Ltd, Singapore.
* All rights reserved.
*
******************************************************************************/
package com.fourelementscapital.scheduler.balance;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.Iterator;
import java.util.Map;
import java.util.TimeZone;
import java.util.Timer;
import java.util.TimerTask;
import java.util.TreeMap;
import java.util.concurrent.ConcurrentLinkedQueue;
import org.apache.jcs.JCS;
import org.apache.jcs.engine.behavior.IElementAttributes;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import com.fourelementscapital.alarm.Alarm;
import com.fourelementscapital.alarm.AlarmType;
import com.fourelementscapital.alarm.ThemeVO;
import com.fourelementscapital.db.SchedulerDB;
import com.fourelementscapital.scheduler.ScheduledTaskFactory;
import com.fourelementscapital.scheduler.engines.ScheduledTask;
public class WaitingQueueList<E> extends ConcurrentLinkedQueue {
private static TreeMap<String, Long> task_stat_max_waiting=new TreeMap<String, Long>();
private static Timer timer=null;
private static Map<String, String> alert_range=null;
private static JCS cache=null;
private Logger log = LogManager.getLogger(WaitingQueueList.class.getName());
private static int ALERT_FREQUENCY_MINUTES=5;
private static WaitingQueueList localqueue=null;
public boolean add(Object itm){
boolean rtn=super.add(itm);
if(WaitingQueueList.localqueue==null){
WaitingQueueList.localqueue=this;
}
//try{
// calculateQueueAndAlertTraffic();
//}catch(Exception e){
// Log.debug("Error while alerting the scheduelr queue traffic, Error:"+e.getMessage());
//}
if(WaitingQueueList.timer==null){
startTimer();
}
return rtn;
}
public static void setAlertRange(Map<String, String> range) {
alert_range=range;
}
public boolean remove(Object itm){
boolean rtn=super.remove(itm);
//try{
// calculateQueueAndAlertTraffic();
//}catch(Exception e){
// Log.debug("Error while alerting the scheduelr queue traffic, Error:"+e.getMessage());
//}
return rtn;
}
protected static void calculateQueueAndAlertTraffic() throws Exception {
if(alert_range!=null && getCache().get("alerted")==null && WaitingQueueList.localqueue.size()>0){
task_stat_max_waiting.clear();
for(Iterator<LoadBalancingQueueItem> it=WaitingQueueList.localqueue.iterator();it.hasNext(); ){
LoadBalancingQueueItem lq=(LoadBalancingQueueItem)it.next();
Map data=lq.getSf().getData();
String dids=(String)data.get(ScheduledTask.FIELD_DEPENDENCY_IDS);
if(dids==null){
String taskuid=lq.getSf().getTask().getUniqueid();
Long waiting=new Date().getTime()-lq.getSf().getTrigger_time();
if(task_stat_max_waiting.containsKey(taskuid)){
waiting=task_stat_max_waiting.get(taskuid)>waiting?task_stat_max_waiting.get(taskuid):waiting;
}
task_stat_max_waiting.put(taskuid, waiting);
}
//String taskuid=lq
}
String bodymsg="";
ScheduledTaskFactory stf=new ScheduledTaskFactory();
SimpleDateFormat sdfh=new SimpleDateFormat("HH:mm:ss 'Hrs'"); sdfh.setTimeZone(TimeZone.getTimeZone("GMT"));
SimpleDateFormat sdfm=new SimpleDateFormat("mm:ss 'Mins'"); sdfm.setTimeZone(TimeZone.getTimeZone("GMT"));
Date d1=new Date();
for(String ky : alert_range.keySet() ){
Long al_range=null;
try{
al_range=Long.parseLong(alert_range.get(ky));
}catch(Exception e){
//error
}
if(al_range!=null && task_stat_max_waiting.containsKey(ky) && task_stat_max_waiting.get(ky)>(al_range*1000*60) ){
ScheduledTask st=stf.getTask(ky);
d1.setTime(task_stat_max_waiting.get(ky));
if(task_stat_max_waiting.get(ky)>=3600000){
bodymsg+=st.getName()+"("+ sdfh.format(d1)+") ";
}else{
bodymsg+=st.getName()+"("+ sdfm.format(d1)+") ";
}
IElementAttributes att= getCache().getDefaultElementAttributes();
att.setMaxLifeSeconds((ALERT_FREQUENCY_MINUTES*60));
getCache().put("alerted", "alerted", att);
}
}
if(!bodymsg.equals("")){
//System.out.println("WaitingQueueList.calculateQueueAndAlertTraffic() bodymsg: Task waiting :"+bodymsg);
SchedulerDB sdb=SchedulerDB.getSchedulerDB();
try{
sdb.connectDB();
LoadBalancingQueueTimeout lbqt=new LoadBalancingQueueTimeout(sdb, stf.getTaskUids());
String theme=lbqt.getAlert_theme();
String type=lbqt.getAlert_type();
String subj="Slow moving of Scheduler Queue";
ArrayList<String> themes=new ArrayList();
themes.add(theme);
// convert String array list to ThemeVO array list required by Alarm.sendAlarm() :
ArrayList<ThemeVO> themeList = new ArrayList<ThemeVO>();
for (int i=0; i<themes.size(); i++) {
themeList.add(new ThemeVO(themes.get(i)));
}
Alarm.sendAlarm( themeList, "email".equalsIgnoreCase(type) ? AlarmType.EMAIL : AlarmType.PHONE, subj, bodymsg, false, true, false, null,null);
}catch(Exception e){
System.out.println("Error WaitingQueueList.calculateQueueAndAlertTraffic(): e:"+e.getMessage());
}finally{
}
}
}
//System.out.println("WaitingQueueList.calculateQueueAndAlertTraffic() Queue stat:"+taskstat);
}
private static JCS getCache() throws Exception {
if(cache==null){
cache=JCS.getInstance(WaitingQueueList.class.getName());
}
return cache;
}
private void startTimer(){
WaitingQueueList.timer=new Timer();
TimerTask tm=new TimerTask() {
public void run() {
try{
WaitingQueueList.calculateQueueAndAlertTraffic();
}catch(Exception e){
log.error("Error "+e.getMessage());
}
}
};
timer.schedule(tm, 60000, 60000); //start after 1 minute in every 1 minute
}
}
@@ -0,0 +1,271 @@
/******************************************************************************
*
* Copyright: Intellectual Property of Four Elements Capital Pte Ltd, Singapore.
* All rights reserved.
*
******************************************************************************/
package com.fourelementscapital.scheduler.balance.executeR;
import java.util.Date;
import java.util.Hashtable;
import java.util.Iterator;
import java.util.Map;
import java.util.Vector;
import java.util.concurrent.Callable;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentLinkedQueue;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
import org.apache.jcs.JCS;
import org.apache.jcs.engine.behavior.IElementAttributes;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.mozilla.javascript.edu.emory.mathcs.backport.java.util.Collections;
import com.fourelementscapital.scheduler.balance.LoadBalancingQueue;
import com.fourelementscapital.scheduler.error.ClientError;
import com.fourelementscapital.scheduler.p2p.listener.OutgoingMessageCallBack;
import com.fourelementscapital.scheduler.p2p.msg.PostMessage;
import com.fourelementscapital.scheduler.p2p.msg.impl.TenderScript;
import com.fourelementscapital.scheduler.rscript.RScript;
import com.fourelementscapital.scheduler.rscript.RScriptListener;
public class GroupQueue implements Callable {
private Logger log = LogManager.getLogger(GroupQueue.class.getName());
private ConcurrentLinkedQueue<RScript> scriptQueue= new ConcurrentLinkedQueue<RScript>();
private ConcurrentLinkedQueue<RScript> scriptQueueProcessing= new ConcurrentLinkedQueue<RScript>();
private Hashtable<String, RScriptListener> scriptQueueListener=new Hashtable<String, RScriptListener>();
private ExecutorService executorService= Executors.newSingleThreadScheduledExecutor();
private Future<String> future=null;
protected static Vector<RScript> scriptQueueVec=new Vector<RScript>();
protected JCS cache=null;
protected JCS cacheGrouped=null;
private String uid=null;
private GroupQueue(String uid) {
this.uid=uid;
}
private static ConcurrentHashMap<String,GroupQueue> queueInstances=new ConcurrentHashMap<String,GroupQueue>();
protected static GroupQueue getGroupInstance(String s) {
synchronized(queueInstances){
if(queueInstances.containsKey(s)){
return queueInstances.get(s);
}else{
GroupQueue g =new GroupQueue(s);
queueInstances.put(s,g);
return g;
}
}
}
public static Map<String,GroupQueue> getAllGroupQueues(){
return queueInstances;
}
protected ConcurrentLinkedQueue<RScript> getScriptQueue() {
return scriptQueue;
}
protected ConcurrentLinkedQueue<RScript> getScriptQueueProcessing() {
return scriptQueueProcessing;
}
protected Hashtable<String, RScriptListener> getScriptQueueListener() {
return scriptQueueListener;
}
protected ExecutorService getExecutorService() {
return executorService;
}
protected Future<String> getFuture() {
return future;
}
protected String getUid() {
return uid;
}
public void start(){
this.future=executorService.submit(this);
}
public Object call() throws Exception {
log.debug("call() made");
try{
while(!scriptQueue.isEmpty() ){
try{
if(!scriptQueue.isEmpty()){
processScriptQueue();
}
}catch(Exception e){
log.error("error while processing queue: err: "+e.getMessage());
}
Thread.sleep(5);
} //while loop
}catch(Exception e){
log.error("loadbalancing queue thread terminiated: e: "+e.getMessage());
e.printStackTrace();
}
return "done";
}
private Vector roundRobin=new Vector();
private void processScriptQueue() throws Exception{
RScript rs=scriptQueue.peek();
if(!rs.isExecuting()){
cache=getCache();
if(getCache().get(rs.getUid())==null){
IElementAttributes att1= getGroupedCache().getDefaultElementAttributes();
att1.setMaxLifeSeconds(LoadBalancingExecuteRQueue.CACHE_GROUP_EXPIRY);
getGroupedCache().putInGroup(rs.getUid(), LoadBalancingExecuteRQueue.CACHE_GROUP_TIMEOUT,"timeout", att1);
//log.debug("adding into timeoud cache...------------>");
scriptTimedOut(rs);
//just to retrieve
for(Object key: getGroupedCache().getGroupKeys(LoadBalancingExecuteRQueue.CACHE_GROUP_TIMEOUT)){
getGroupedCache().getFromGroup(key,LoadBalancingExecuteRQueue.CACHE_GROUP_TIMEOUT);
}
}else{
Vector<Object> autclients=LoadBalancingQueue.getExecuteRScriptDefault().getRunOnlyOn(rs.getTaskuid());
Vector<Object> autclients1;
if(rs.getExecuteAt()!=null){
autclients1=new Vector<Object>();
if(autclients.contains(rs.getExecuteAt())){
autclients1.add(rs.getExecuteAt());
}
}else{
//autclients1=(Vector<Object>)autclients.clone();
//round robin load balance of peers list
for(Iterator i=autclients.iterator();i.hasNext();){
String peer=(String)i.next();
if(!roundRobin.contains(peer)){
roundRobin.add(peer);
}
}
autclients1=new Vector<Object>();
for(Iterator i=roundRobin.iterator();i.hasNext();){
String peer=(String)i.next();
if(autclients.contains(peer)){
autclients1.add(peer);
}
}
}
//log.debug("authorized cients found:"+autclients1 +" for taskuid:"+rs.getTaskuid());
if(autclients1!=null && autclients1.size()>0){
//log.debug("autclients1:"+autclients1);
for(Iterator<Object> i=autclients1.iterator();i.hasNext();){
String clientname=(String)i.next();
String ky=clientname+"_"+rs.getTaskuid();
//sends only 1 post message in a second
IElementAttributes att= cache.getDefaultElementAttributes();
att.setMaxLifeSeconds(1);
Long last=(Long)cache.get(ky);
if(last==null || (new Date().getTime()-last)>100 ){
TenderScript ts=new TenderScript();
ts.setPriority(OutgoingMessageCallBack.PRIORITY_LOW );
ts.setUid(rs.getUid());
ts.setTaskuid(rs.getTaskuid());
PostMessage ps=new PostMessage(ts,clientname);
ps.send();
cache.put(ky, new Date().getTime(),att);
ts=null;
ps=null;
log.debug("sending to :"+clientname);
}
}
}
if(!roundRobin.isEmpty()){
Collections.rotate(roundRobin, -1);
}
autclients=null;
autclients1=null;
}
}
rs=null;
}
private synchronized void scriptTimedOut(RScript rs){
try{
if(scriptQueueListener.get(rs.getUid())!=null){
RScriptListener rslisten=scriptQueueListener.get(rs.getUid());
try{
LoadBalancingExecuteRQueue lhq=(LoadBalancingExecuteRQueue)LoadBalancingQueue.getExecuteRScriptDefault();
rslisten.onScriptTimedOut(rs);
lhq.toWSTimedout(rs.getUid());
}catch(Exception e){
ClientError.reportError(e, "Error while invoking listener");
}finally{
scriptQueueListener.remove(rs.getUid());
}
}
}catch(Exception e){
log.error("Error while time out");
}finally{
//scriptQueueVec.remove(rs);
scriptQueue.remove(rs);
}
}
protected JCS getCache() throws Exception {
if(cache==null){
cache=JCS.getInstance("LoadBalancingQueue");
}
return cache;
}
public JCS getGroupedCache() throws Exception {
if(cacheGrouped==null){
cacheGrouped=JCS.getInstance("LoadBalancingQueueGroupped");
}
return cacheGrouped;
}
}
@@ -0,0 +1,587 @@
/******************************************************************************
*
* Copyright: Intellectual Property of Four Elements Capital Pte Ltd, Singapore.
* All rights reserved.
*
******************************************************************************/
package com.fourelementscapital.scheduler.balance.executeR;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.Vector;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
import org.apache.commons.beanutils.BeanUtils;
import org.apache.jcs.JCS;
import org.apache.jcs.engine.behavior.IElementAttributes;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.json.JSONObject;
import com.fourelementscapital.scheduler.balance.LoadBalancingQueue;
import com.fourelementscapital.scheduler.balance.LoadBalancingQueueItem;
import com.fourelementscapital.scheduler.balance.hsqldb.LoadBalancingHSQLQueue;
import com.fourelementscapital.scheduler.error.ClientError;
import com.fourelementscapital.scheduler.p2p.websocket.TomcatWSServer;
import com.fourelementscapital.scheduler.rscript.RScript;
import com.fourelementscapital.scheduler.rscript.RScriptListener;
public class LoadBalancingExecuteRQueue extends LoadBalancingQueue {
private Logger log = LogManager.getLogger(LoadBalancingExecuteRQueue.class.getName());
//private static ExecutorService startScriptService= Executors.newFixedThreadPool(3);
private static ExecutorService startScriptService= Executors.newCachedThreadPool();
protected String getGroupUid(RScript item){
return item.getTaskuid()!=null?item.getTaskuid():"general";
}
@Override
public void addExecuteR(RScript item, RScriptListener listener) throws Exception {
log.debug("adding script:"+item.getScript());
item.setExecuting(false);
item.setQueued_time(new Date().getTime());
String qid=getGroupUid(item);
GroupQueue gq=GroupQueue.getGroupInstance(qid);
gq.getScriptQueue().add(item);
GroupQueue.scriptQueueVec.add(item);
IElementAttributes att= gq.getCache().getDefaultElementAttributes();
att.setMaxLifeSeconds(CACHE_GROUP_EXPIRY);
gq.getCache().put(item.getUid(), "alive", att);
if(listener!=null) gq.getScriptQueueListener().put(item.getUid(), listener);
if(gq.getFuture()==null || (gq.getFuture()!=null && gq.getFuture().isDone())){
gq.start();
}
toWSAdded(item);
}
/**
* making this single threaded queue.
* @see com.fe.scheduler.balance.LoadBalancingQueue#startedIfNotStarted(int, long, java.lang.String)
*/
public final RScript startScriptIfNotStarted(RScript rscript,String peer){
Future fu=startScriptService.submit(
new Callable<RScript>() {
public RScript call(){
//return new Integer(new LoadBalancingHSQLQueue().startedIfNotStarted1(sc_id,tri_time,peer));
RScript rtn=null;
if(LoadBalancingQueue.getDefault() instanceof LoadBalancingHSQLQueue){
LoadBalancingExecuteRQueue lhq=(LoadBalancingExecuteRQueue)LoadBalancingQueue.getExecuteRScriptDefault();
rtn=lhq.startScriptIfNotStarted1(this.rscript,this.peer);
}
return rtn;
}
private RScript rscript=null;
private String peer=null;
public Callable<RScript> init(RScript rscript,String peer){
this.rscript=rscript;
this.peer=peer;
return this;
}
}.init(rscript, peer)
);
RScript rs=null;
try{
rs=(RScript)fu.get();
}catch(Exception e){
log.error("error while retriveing future result");
//e.printStackTrace();
}
return rs;
}
private RScript getRScript(Collection<RScript> col,RScript rscript) {
RScript rtn=null;
for(RScript rr:col){
if(rr.equals(rscript)) rtn=rr;
}
return rtn;
}
protected synchronized RScript startScriptIfNotStarted1(RScript rscript,String peer) {
RScript rtn=GroupQueue.scriptQueueVec.get(GroupQueue.scriptQueueVec.indexOf(rscript));
String qid=getGroupUid(rtn);
GroupQueue gq=GroupQueue.getGroupInstance(qid);
if(gq.getScriptQueue().contains(rscript)){
gq.getScriptQueue().remove(rscript);
rtn.setPeer(peer);
rtn.setDelay(new Date().getTime()- rtn.getQueued_time());
rtn.setExecuting(true);
rtn.setStartedtime(new Date());
gq.getScriptQueueProcessing().add(rtn);
if(gq.getScriptQueueListener().get(rtn.getUid())!=null){
RScriptListener rslisten=gq.getScriptQueueListener().get(rtn.getUid());
try{
rslisten.onScriptSent(rtn, peer);
}catch(Exception e){
ClientError.reportError(e, "Error while invoking listener");
}
}
try{
IElementAttributes att1= getGroupedCache().getDefaultElementAttributes();
att1.setMaxLifeSeconds(CACHE_GROUP_EXPIRY);
getGroupedCache().putInGroup(peer, CACHE_GROUP_ACTIVEPEERS,"active", att1);
}catch(Exception e){
log.error("error while caching active peer");
}
toWSStarted(rscript.getUid(),peer);
return rtn;
}else{
return null;
}
}
public synchronized void scriptFinished(RScript rscript, String result,String status) {
RScript rscript1=GroupQueue.scriptQueueVec.get(GroupQueue.scriptQueueVec.indexOf(rscript));
String qid=getGroupUid(rscript1);
GroupQueue gq=GroupQueue.getGroupInstance(qid);
gq.getScriptQueueProcessing().remove(rscript);
SimpleDateFormat sdf=new SimpleDateFormat("dd-MM HH:mm:ss");
Date d=new Date();
d.setTime(rscript1.getQueued_time());
log.debug("scriptFinished called:"+sdf.format(d)+" qid:"+qid);
try{
String ky=rscript1.getPeer()+"_"+rscript1.getTaskuid();
rscript1.setError(rscript.getError());
//gq.cache=getCache();
IElementAttributes att= gq.getGroupedCache().getDefaultElementAttributes();
att.setMaxLifeSeconds(CACHE_GROUP_EXPIRY);
gq.getGroupedCache().putInGroup(rscript1.getUid(), CACHE_GROUP_FINISHED,rscript1.getDelay(), att);
//getGroupedCache().getGroupKeys(CACHE_GROUP_FINISHED);
//just to retrieve so that, expired won't in the memory
for(Object key: gq.getGroupedCache().getGroupKeys(CACHE_GROUP_FINISHED)){
gq.getGroupedCache().getFromGroup(key,CACHE_GROUP_FINISHED);
}
if(gq.getScriptQueueListener().get(rscript1.getUid())!=null){
RScriptListener rslisten=gq.getScriptQueueListener().get(rscript1.getUid());
try{
rslisten.onScriptFinished(rscript1, rscript1.getPeer(),result, status);
}catch(Exception e){
ClientError.reportError(e, "Error while invoking listener");
}
gq.getScriptQueueListener().remove(rscript1.getUid());
rslisten=null;
}
gq.getCache().remove(ky);
rscript1=null;
toWSFinished(rscript.getUid());
}catch(Exception e){
e.printStackTrace();
}
GroupQueue.scriptQueueVec.remove(rscript);
rscript=null;
}
@Override
public void removeScriptFromAllQueue(RScript rscript) {
try{
//getScriptProcessingQueue().remove(rscript);
RScript rscript1=GroupQueue.scriptQueueVec.get(GroupQueue.scriptQueueVec.indexOf(rscript));
String qid=getGroupUid(rscript1);
GroupQueue gq=GroupQueue.getGroupInstance(qid);
gq.getScriptQueueProcessing().remove(rscript1);
String ky=rscript1.getPeer()+"_"+rscript1.getTaskuid();
rscript1.setError(rscript.getError());
gq.getCache().remove(ky);
GroupQueue.scriptQueueVec.remove(rscript);
}catch(Exception e){
e.printStackTrace();
}
}
@Override
public Collection<RScript> getScriptQueue() throws Exception {
ArrayList<RScript> list=new ArrayList<RScript>();
for(GroupQueue gp: GroupQueue.getAllGroupQueues().values()){
list.addAll(gp.getScriptQueue());
}
//java.util.Collections.sort(list);
return list;
}
@Override
public Collection<RScript> getScriptProcessingQueue() throws Exception {
ArrayList<RScript> list=new ArrayList<RScript>();
for(GroupQueue gp: GroupQueue.getAllGroupQueues().values()){
list.addAll(gp.getScriptQueueProcessing());
}
//java.util.Collections.sort(list);
return list;
}
private Map getScriptListeners() throws Exception {
HashMap list=new HashMap();
for(GroupQueue gp: GroupQueue.getAllGroupQueues().values()){
list.putAll(gp.getScriptQueueListener());
}
//java.util.Collections.sort(list);
return list;
}
public Map<String, Integer> debug_data() throws Exception {
HashMap<String, Integer> h=new HashMap<String, Integer>();
h.put("scriptQueueListener_size",getScriptListeners().size());
h.put("scriptQueueVec_size",GroupQueue.scriptQueueVec.size());
h.put("scriptQueue_size",getScriptQueue().size());
h.put("queue_size",getScriptQueue().size());
h.put("queueprocessing_size",getScriptProcessingQueue().size());
return h;
}
@Override
public List<LoadBalancingQueueItem> getAllTasks() {
// TODO Auto-generated method stub
return null;
}
@Override
public long lastExcecutedTime() {
// TODO Auto-generated method stub
return 0;
}
@Override
public void removeItemProcessing(LoadBalancingQueueItem item,
String message, int respCode) {
// TODO Auto-generated method stub
}
@Override
public void executeScript(Vector<Object> peers, int scriptid)
throws Exception {
// TODO Auto-generated method stub
}
@Override
public void peerStarted(int scheduler_id, long trigger_time, String peername)
throws Exception {
// TODO Auto-generated method stub
}
@Override
public void executionEnded(int schedulerid) {
// TODO Auto-generated method stub
}
@Override
public void add(LoadBalancingQueueItem item) {
// TODO Auto-generated method stub
}
@Override
public LoadBalancingQueueItem getItemFromProcessingQueue(int scheduler_id,
long trigger_time) {
// TODO Auto-generated method stub
return null;
}
@Override
public boolean killQueuedTask(int scheduler_id, long trigger_time) {
// TODO Auto-generated method stub
return false;
}
@Override
public boolean removeFaultyProcessingTask(int scheduler_id,
long trigger_time) {
// TODO Auto-generated method stub
return false;
}
@Override
public int startedIfNotStarted(int schedulerid, long trigger_time,
String machinename) {
// TODO Auto-generated method stub
return 0;
}
@Override
public void executionFailed(int schedulerid, long trigger_time,
String machinename) {
// TODO Auto-generated method stub
}
@Override
public void executionEnded(int schedulerid, long trigger_time) {
// TODO Auto-generated method stub
}
@Override
public void cleanupProccesingQueue(int schedulerid, String computername) {
// TODO Auto-generated method stub
}
@Override
public Collection<LoadBalancingQueueItem> getExecutingTasks() {
// TODO Auto-generated method stub
return null;
}
@Override
public Collection<LoadBalancingQueueItem> getQueuedTasks() {
// TODO Auto-generated method stub
return null;
}
protected void toWSAdded(RScript item){
try{
JSONObject jsonb=new JSONObject();
Map data=BeanUtils.describe(item);
jsonb.put("added", data);
jsonb.put("queue_size", getScriptQueue().size());
TomcatWSServer.broadcast(jsonb.toString());
}catch(Exception e){
e.printStackTrace();
}
}
protected void toWSStarted(String uid, String peer){
try{
JSONObject jsonb=new JSONObject();
HashMap data=new HashMap();
data.put("uid", uid);
data.put("peer", peer);
jsonb.put("started", data);
jsonb.put("executing_size", getScriptProcessingQueue().size());
jsonb.put("queue_size", getScriptQueue().size());
//h.put("queueprocessing_size",getScriptProcessingQueue().size());
TomcatWSServer.broadcast(jsonb.toString());
}catch(Exception e){
e.printStackTrace();
}
}
protected void toWSFinished(String uid){
try{
JSONObject jsonb=new JSONObject();
HashMap data=new HashMap();
data.put("uid", uid);
jsonb.put("finished", data);
jsonb.put("executing_size", getScriptProcessingQueue().size());
try{
get30SecsData(jsonb);
}catch(Exception e){
log.error("Error while geting 30secs data, e:"+e.getMessage());
}
TomcatWSServer.broadcast(jsonb.toString());
}catch(Exception e){
e.printStackTrace();
}
}
protected void toWSTimedout(String uid){
try{
JSONObject jsonb=new JSONObject();
HashMap data=new HashMap();
data.put("uid", uid);
jsonb.put("timedout", data);
try{
get30SecsData(jsonb);
}catch(Exception e){
log.error("Error while geting 30secs data, e:"+e.getMessage());
}
TomcatWSServer.broadcast(jsonb.toString());
}catch(Exception e){
e.printStackTrace();
}
}
protected void get30SecsData(JSONObject jsonb) throws Exception {
if(getCache().get("WS30SecData")==null){
JCS gjcs=getGroupedCache();
//just to retrieve so that, expired won't in the memory
int counter=0;
long totaldelay=0;
for(Object key: gjcs.getGroupKeys(LoadBalancingQueue.CACHE_GROUP_FINISHED)){
Long delay=(Long)gjcs.getFromGroup(key,LoadBalancingQueue.CACHE_GROUP_FINISHED);
if(delay!=null){
totaldelay=totaldelay+delay;
counter++;
}
}
log.debug("totaldelay:"+totaldelay+" counter:"+counter);
long ave_delay=(counter>0 && totaldelay>0)?(totaldelay/counter)/1000:0;
//just to retrieve so that, expired won't in the memory
for(Object key: gjcs.getGroupKeys(LoadBalancingQueue.CACHE_GROUP_TIMEOUT)){
gjcs.getFromGroup(key,LoadBalancingQueue.CACHE_GROUP_TIMEOUT);
}
for(Object key: gjcs.getGroupKeys(LoadBalancingQueue.CACHE_GROUP_FINISHED)){
gjcs.getFromGroup(key,LoadBalancingQueue.CACHE_GROUP_FINISHED);
}
for(Object key: gjcs.getGroupKeys(LoadBalancingQueue.CACHE_GROUP_ACTIVEPEERS)){
gjcs.getFromGroup(key,LoadBalancingQueue.CACHE_GROUP_ACTIVEPEERS);
}
Set finished=gjcs.getGroupKeys(LoadBalancingQueue.CACHE_GROUP_FINISHED);
Set timedout=gjcs.getGroupKeys(LoadBalancingQueue.CACHE_GROUP_TIMEOUT);
Set activePeers=gjcs.getGroupKeys(LoadBalancingQueue.CACHE_GROUP_ACTIVEPEERS);
HashMap h=new HashMap();
h.put("ave_delay", (ave_delay>0?ave_delay:0));
h.put("active_peers", activePeers.size());
h.put("finished_count", finished.size());
h.put("timedout_count", timedout.size());
jsonb.put("ave_delay", (ave_delay>0?ave_delay:0));
jsonb.put("active_peers", activePeers.size());
jsonb.put("finished_count", finished.size());
jsonb.put("timedout_count", timedout.size());
IElementAttributes att=getCache().getDefaultElementAttributes();
att.setMaxLifeSeconds(2);
getCache().put("WS30SecData",h,att);
}else{
Map h=(Map)getCache().get("WS30SecData");
for(Iterator i=h.keySet().iterator();i.hasNext();){
String ky=(String)i.next();
jsonb.put(ky, h.get(ky));
}
}
}
}
@@ -0,0 +1,364 @@
/******************************************************************************
*
* Copyright: Intellectual Property of Four Elements Capital Pte Ltd, Singapore.
* All rights reserved.
*
******************************************************************************/
package com.fourelementscapital.scheduler.balance.executeR;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.concurrent.Callable;
import java.util.concurrent.Future;
import org.apache.jcs.engine.behavior.IElementAttributes;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.json.JSONObject;
import com.fourelementscapital.scheduler.error.ClientError;
import com.fourelementscapital.scheduler.io.request.IOExcecutors;
import com.fourelementscapital.scheduler.p2p.websocket.TomcatWSServer;
import com.fourelementscapital.scheduler.rscript.RScript;
import com.fourelementscapital.scheduler.rscript.RScriptListener;
public class LoadBalancingNewExecuteRQueue extends LoadBalancingExecuteRQueue{
private Logger log = LogManager.getLogger(LoadBalancingNewExecuteRQueue.class.getName());
@Override
public synchronized void addExecuteR(RScript item, RScriptListener listener) throws Exception {
log.debug("adding script:"+item.getScript()+" uid:"+item.getUid());
item.setExecuting(false);
item.setQueued_time(new Date().getTime());
String qid=getGroupUid(item);
NIOGroupQueue gq=NIOGroupQueue.getGroupInstance(qid);
//script with existing token id will not be added into the queue
//instead the existing script with the same token will be
//replaced with new script and everything else remains as old.
boolean added=false;
RScript existing=null;
for(Iterator<RScript> i=gq.getScriptQueue().iterator();i.hasNext();){
RScript cur=i.next();
if(cur.getUniquename().equals(item.getUniquename())){
existing=cur;
}
}
if(existing!=null){
log.debug("script id:"+item.getUid()+" existing");
existing.setScript(item.getScript());
gq.getCache().remove(item.getUid());
}else{
gq.getScriptQueue().add(item);
NIOGroupQueue.scriptQueueVec.add(item);
if(listener!=null) gq.getScriptQueueListener().put(item.getUid(), listener);
added=true;
}
IElementAttributes att= gq.getCache().getDefaultElementAttributes();
att.setMaxLifeSeconds(CACHE_GROUP_EXPIRY);
gq.getCache().put(item.getUid(), "alive", att);
if(gq.getFuture()==null || (gq.getFuture()!=null && gq.getFuture().isDone())){
gq.start();
}
if(added){
toWSAdded(item);
}
}
@Override
protected synchronized RScript startScriptIfNotStarted1(RScript rscript,String peer) {
RScript rtn=NIOGroupQueue.scriptQueueVec.get(NIOGroupQueue.scriptQueueVec.indexOf(rscript));
String qid=getGroupUid(rtn);
NIOGroupQueue gq=NIOGroupQueue.getGroupInstance(qid);
if(gq.getScriptQueue().contains(rscript)){
gq.getScriptQueue().remove(rscript);
rtn.setPeer(peer);
rtn.setDelay(new Date().getTime()- rtn.getQueued_time());
rtn.setExecuting(true);
rtn.setStartedtime(new Date());
gq.getScriptQueueProcessing().add(rtn);
if(gq.getScriptQueueListener().get(rtn.getUid())!=null){
RScriptListener rslisten=gq.getScriptQueueListener().get(rtn.getUid());
try{
rslisten.onScriptSent(rtn, peer);
}catch(Exception e){
ClientError.reportError(e, "Error while invoking listener");
}
}
try{
IElementAttributes att1= getGroupedCache().getDefaultElementAttributes();
att1.setMaxLifeSeconds(CACHE_GROUP_EXPIRY);
getGroupedCache().putInGroup(peer, CACHE_GROUP_ACTIVEPEERS,"active", att1);
}catch(Exception e){
log.error("error while caching active peer");
}
toWSStarted(rscript.getUid(),peer);
return rtn;
}else{
return null;
}
}
@Override
public synchronized void scriptFinished(RScript rscript, String result,String status) {
RScript rscript1=NIOGroupQueue.scriptQueueVec.get(NIOGroupQueue.scriptQueueVec.indexOf(rscript));
String qid=getGroupUid(rscript1);
NIOGroupQueue gq=NIOGroupQueue.getGroupInstance(qid);
gq.getScriptQueueProcessing().remove(rscript);
SimpleDateFormat sdf=new SimpleDateFormat("dd-MM HH:mm:ss");
Date d=new Date();
d.setTime(rscript1.getQueued_time());
//log.debug("scriptFinished called:"+sdf.format(d)+" qid:"+qid);
try{
String ky=rscript1.getPeer()+"_"+rscript1.getTaskuid();
rscript1.setError(rscript.getError());
//gq.cache=getCache();
IElementAttributes att= gq.getGroupedCache().getDefaultElementAttributes();
att.setMaxLifeSeconds(CACHE_GROUP_EXPIRY);
//gq.getGroupedCache().putInGroup(rscript1.getUid(), CACHE_GROUP_FINISHED,rscript1.getDelay(), att);
//getGroupedCache().getGroupKeys(CACHE_GROUP_FINISHED);
//just to retrieve so that, expired won't in the memory
//for(Object key: gq.getGroupedCache().getGroupKeys(CACHE_GROUP_FINISHED)){
// gq.getGroupedCache().getFromGroup(key,CACHE_GROUP_FINISHED);
//}
if(gq.getScriptQueueListener().get(rscript1.getUid())!=null){
RScriptListener rslisten=gq.getScriptQueueListener().get(rscript1.getUid());
try{
rslisten.onScriptFinished(rscript1, rscript1.getPeer(),result, status);
}catch(Exception e){
ClientError.reportError(e, "Error while invoking listener");
}
gq.getScriptQueueListener().remove(rscript1.getUid());
rslisten=null;
}
gq.getCache().remove(ky);
rscript1=null;
toWSFinished(rscript.getUid());
}catch(Exception e){
e.printStackTrace();
}finally{
NIOGroupQueue.scriptQueueVec.remove(rscript);
}
rscript=null;
}
@Override
public void removeScriptFromAllQueue(RScript rscript) {
try{
//getScriptProcessingQueue().remove(rscript);
RScript rscript1=NIOGroupQueue.scriptQueueVec.get(NIOGroupQueue.scriptQueueVec.indexOf(rscript));
String qid=getGroupUid(rscript1);
NIOGroupQueue gq=NIOGroupQueue.getGroupInstance(qid);
gq.getScriptQueueProcessing().remove(rscript1);
String ky=rscript1.getPeer()+"_"+rscript1.getTaskuid();
rscript1.setError(rscript.getError());
gq.getCache().remove(ky);
NIOGroupQueue.scriptQueueVec.remove(rscript);
}catch(Exception e){
e.printStackTrace();
}
}
@Override
public Collection<RScript> getScriptQueue() throws Exception {
ArrayList<RScript> list=new ArrayList<RScript>();
for(NIOGroupQueue gp: NIOGroupQueue.getAllGroupQueues().values()){
list.addAll(gp.getScriptQueue());
}
//java.util.Collections.sort(list);
return list;
}
@Override
public Collection<RScript> getScriptProcessingQueue() throws Exception {
ArrayList<RScript> list=new ArrayList<RScript>();
for(NIOGroupQueue gp: NIOGroupQueue.getAllGroupQueues().values()){
list.addAll(gp.getScriptQueueProcessing());
}
//java.util.Collections.sort(list);
return list;
}
public int getAllScriptObjectsSize() throws Exception {
return NIOGroupQueue.scriptQueueVec.size();
}
public Map getScriptListeners() throws Exception {
HashMap list=new HashMap();
for(NIOGroupQueue gp: NIOGroupQueue.getAllGroupQueues().values()){
list.putAll(gp.getScriptQueueListener());
}
//java.util.Collections.sort(list);
return list;
}
@Override
protected void toWSStarted(String uid, String peer){
Future fu=IOExcecutors.threadpool.submit(
new Callable<String>() {
public String call(){
JSONObject jsonb=new JSONObject();
HashMap data=new HashMap();
data.put("uid", uid);
data.put("peer", peer);
try{
jsonb.put("started", data);
jsonb.put("executing_size", getScriptProcessingQueue().size());
jsonb.put("queue_size", getScriptQueue().size());
}catch(Exception e){
e.printStackTrace();
}
//h.put("queueprocessing_size",getScriptProcessingQueue().size());
TomcatWSServer.broadcast(jsonb.toString());
return "";
}
private String uid;
private String peer;
public Callable<String> init(String u, String p){
this.uid=u;
this.peer=p;
return this;
}
}.init(uid,peer)
);
}
@Override
protected void toWSFinished(String uid){
/*
try{
JSONObject jsonb=new JSONObject();
HashMap data=new HashMap();
data.put("uid", uid);
jsonb.put("finished", data);
jsonb.put("executing_size", getScriptProcessingQueue().size());
try{
get30SecsData(jsonb);
}catch(Exception e){
log.error("Error while geting 30secs data, e:"+e.getMessage());
}
TomcatWSServer.broadcast(jsonb.toString());
}catch(Exception e){
e.printStackTrace();
}
*/
Future fu=IOExcecutors.threadpool.submit(
new Callable<String>() {
public String call(){
JSONObject jsonb=new JSONObject();
HashMap data=new HashMap();
data.put("uid", uid);
try{
jsonb.put("finished", data);
jsonb.put("executing_size", getScriptProcessingQueue().size());
try{
get30SecsData(jsonb);
}catch(Exception e){
log.error("Error while geting 30secs data, e:"+e.getMessage());
}
}catch(Exception e){
log.error("toWSFinished()"+e.getMessage());
}
TomcatWSServer.broadcast(jsonb.toString());
return "";
}
private String uid;
public Callable<String> init(String u){
this.uid=u;
return this;
}
}.init(uid)
);
}
}
@@ -0,0 +1,287 @@
/******************************************************************************
*
* Copyright: Intellectual Property of Four Elements Capital Pte Ltd, Singapore.
* All rights reserved.
*
******************************************************************************/
package com.fourelementscapital.scheduler.balance.executeR;
import java.util.ArrayList;
import java.util.Hashtable;
import java.util.Map;
import java.util.Vector;
import java.util.concurrent.Callable;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentLinkedQueue;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Future;
import org.apache.jcs.JCS;
import org.apache.jcs.engine.behavior.IElementAttributes;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import com.fourelementscapital.scheduler.balance.LoadBalancingQueue;
import com.fourelementscapital.scheduler.error.ClientError;
import com.fourelementscapital.scheduler.io.msg.ServerExecuteScript;
import com.fourelementscapital.scheduler.io.request.IOExcecutors;
import com.fourelementscapital.scheduler.queue.QueueStack;
import com.fourelementscapital.scheduler.queue.QueueStackManager;
import com.fourelementscapital.scheduler.rscript.RScript;
import com.fourelementscapital.scheduler.rscript.RScriptListener;
public class NIOGroupQueue implements Callable {
private Logger log = LogManager.getLogger(NIOGroupQueue.class.getName());
private ConcurrentLinkedQueue<RScript> scriptQueue= new ConcurrentLinkedQueue<RScript>();
private ConcurrentLinkedQueue<RScript> scriptQueueProcessing= new ConcurrentLinkedQueue<RScript>();
private Hashtable<String, RScriptListener> scriptQueueListener=new Hashtable<String, RScriptListener>();
//private ExecutorService executorService= Executors.newCachedThreadPool();
//private static ExecutorService dispatchService=Executors.newCachedThreadPool(); // Executors.newFixedThreadPool(20);
//private static ExecutorService dispatchService=Executors.newFixedThreadPool(10); // Executors.newFixedThreadPool(20);
private Future<String> future=null;
protected static Vector<RScript> scriptQueueVec=new Vector<RScript>();
protected JCS cache=null;
protected JCS cacheGrouped=null;
private String uid=null;
private NIOGroupQueue(String uid) {
this.uid=uid;
}
private static ConcurrentHashMap<String,NIOGroupQueue> queueInstances=new ConcurrentHashMap<String,NIOGroupQueue>();
public static NIOGroupQueue getGroupInstance(String s) {
synchronized(queueInstances){
if(queueInstances.containsKey(s)){
return queueInstances.get(s);
}else{
NIOGroupQueue g =new NIOGroupQueue(s);
queueInstances.put(s,g);
return g;
}
}
}
public static Map<String,NIOGroupQueue> getAllGroupQueues(){
return queueInstances;
}
protected ConcurrentLinkedQueue<RScript> getScriptQueue() {
return scriptQueue;
}
protected ConcurrentLinkedQueue<RScript> getScriptQueueProcessing() {
return scriptQueueProcessing;
}
protected Hashtable<String, RScriptListener> getScriptQueueListener() {
return scriptQueueListener;
}
protected ExecutorService getExecutorService() {
//return executorService;
return IOExcecutors.threadpool;
}
protected Future<String> getFuture() {
return future;
}
protected String getUid() {
return uid;
}
public void start(){
//this.future=executorService.submit(this);
this.future=IOExcecutors.threadpool.submit(this);
}
private long prevtime=0;
public Object call() throws Exception {
log.debug("call() made");
try{
while(!scriptQueue.isEmpty() ){
try{
//if(!scriptQueue.isEmpty()){
processScriptQueue();
//}
}catch(Exception e){
e.printStackTrace();
}
Thread.sleep(2);
} //while loop
}catch(Exception e){
log.error("loadbalancing queue thread terminiated: e: "+e.getMessage());
e.printStackTrace();
}
return "done";
}
private Vector roundRobin=new Vector();
private void processScriptQueue() throws Exception{
RScript rs=scriptQueue.peek();
if(!rs.isExecuting()){
cache=getCache();
if(getCache().get(rs.getUid())==null){
IElementAttributes att1= getGroupedCache().getDefaultElementAttributes();
att1.setMaxLifeSeconds(LoadBalancingExecuteRQueue.CACHE_GROUP_EXPIRY);
getGroupedCache().putInGroup(rs.getUid(), LoadBalancingExecuteRQueue.CACHE_GROUP_TIMEOUT,"timeout", att1);
//log.debug("adding into timeoud cache...------------>");
scriptTimedOut(rs);
//just to retrieve
for(Object key: getGroupedCache().getGroupKeys(LoadBalancingExecuteRQueue.CACHE_GROUP_TIMEOUT)){
getGroupedCache().getFromGroup(key,LoadBalancingExecuteRQueue.CACHE_GROUP_TIMEOUT);
}
}else{
Vector<Object> autclients=LoadBalancingQueue.getExecuteRScriptDefault().getRunOnlyOn(rs.getTaskuid());
// Vector<Object> autclients1;
ArrayList al=new ArrayList();
if(rs.getExecuteAt()!=null){
al.add(rs.getExecuteAt());
}else{
al.addAll(autclients);
}
if(al.size()>0){
QueueStack qs=QueueStackManager.useNextAvailableQueue(al, rs.getTaskuid());
log.debug("qs:"+qs+" rs.getTaskuid():"+rs.getTaskuid());
if(qs!=null){
//Future fu=dispatchService.submit(
Future fu=IOExcecutors.threadpool.submit(
new Callable<RScript>() {
public RScript call(){
RScript started=null;
if(qs!=null){
started=LoadBalancingQueue.getExecuteRScriptDefault().startScriptIfNotStarted(rscript,qs.getPeername());
//send implementation will be done here..
if(started!=null){
ServerExecuteScript ses=new ServerExecuteScript();
ses.setQueue_uid(qs.getUid());
ses.setScript(rscript.getScript());
ses.setTaskuid(rscript.getTaskuid());
ses.setScript_uid(rscript.getUid());
ses.send(qs.getPeername());
}
}
return started;
}
private RScript rscript=null;
private QueueStack qs=null;
public Callable<RScript> init(RScript rscript,QueueStack q){
this.rscript=rscript;
this.qs=q;
return this;
}
}.init(rs, qs)
);
//behaves badly if
//I removes the folloing block;
//extensive test needed while removing the followings
RScript rs1=null;
try{
rs1=(RScript)fu.get();
}catch(Exception e){
log.error("error while retriveing future result");
e.printStackTrace();
}
}
/*
RScript started=LoadBalancingQueue.getExecuteRScriptDefault().startScriptIfNotStarted(rs,qs.getPeername());
//send implementation will be done here..
if(started!=null){
ServerExecuteScript ses=new ServerExecuteScript();
ses.setQueue_uid(qs.getUid());
ses.setScript(rs.getScript());
ses.setTaskuid(rs.getTaskuid());
ses.setScript_uid(rs.getUid());
ses.send(qs.getPeername());
}
*/
}
autclients=null;
}
}
rs=null;
}
private synchronized void scriptTimedOut(RScript rs){
try{
if(scriptQueueListener.get(rs.getUid())!=null){
RScriptListener rslisten=scriptQueueListener.get(rs.getUid());
try{
LoadBalancingExecuteRQueue lhq=(LoadBalancingExecuteRQueue)LoadBalancingQueue.getExecuteRScriptDefault();
rslisten.onScriptTimedOut(rs);
lhq.toWSTimedout(rs.getUid());
}catch(Exception e){
ClientError.reportError(e, "Error while invoking listener");
}finally{
scriptQueueListener.remove(rs.getUid());
}
}
}catch(Exception e){
log.error("Error while time out");
}finally{
//
scriptQueue.remove(rs);
scriptQueueVec.remove(rs);
}
}
protected JCS getCache() throws Exception {
if(cache==null){
cache=JCS.getInstance("LoadBalancingQueue");
}
return cache;
}
public JCS getGroupedCache() throws Exception {
if(cacheGrouped==null){
cacheGrouped=JCS.getInstance("LoadBalancingQueueGroupped");
}
return cacheGrouped;
}
}
@@ -0,0 +1,345 @@
/******************************************************************************
*
* Copyright: Intellectual Property of Four Elements Capital Pte Ltd, Singapore.
* All rights reserved.
*
******************************************************************************/
package com.fourelementscapital.scheduler.balance.hsqldb;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.StringTokenizer;
import java.util.TreeMap;
import java.util.Vector;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
import java.util.concurrent.Semaphore;
import org.apache.commons.beanutils.BeanUtils;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import com.fourelementscapital.db.SchedulerDB;
import com.fourelementscapital.scheduler.ScheduledTaskFactory;
import com.fourelementscapital.scheduler.balance.LoadBalancingQueueItem;
import com.fourelementscapital.scheduler.balance.LoadBalancingQueueTimeout;
import com.fourelementscapital.scheduler.engines.ScheduledTask;
import com.fourelementscapital.scheduler.engines.SchedulerExePlanLogs;
import com.fourelementscapital.scheduler.p2p.P2PService;
import com.fourelementscapital.scheduler.rscript.RScript;
import com.fourelementscapital.scheduler.rscript.RScriptListener;
public class LoadBalancingHSQLQueue extends LoadBalancingHSQLLayerDB implements Callable {
private Logger log = LogManager.getLogger(LoadBalancingHSQLQueue.class.getName());
private Future<String> future=null;
private ExecutorService executor = Executors.newCachedThreadPool();
private static Semaphore conslock=new Semaphore(1,true);
public LoadBalancingHSQLQueue(){
initDB();
future=executor.submit(this);
}
public void add(LoadBalancingQueueItem item_parent) {
SchedulerDB sdb=SchedulerDB.getSchedulerDB();
acquireLock();
try{
sdb.connectDB();
LoadBalancingQueueTimeout tqt=new LoadBalancingQueueTimeout(sdb,new ScheduledTaskFactory().getTaskUids());
setAlertRange( tqt.getMaxWaitingAlert());
LoadBalancingHSQLQueueItem item=new LoadBalancingHSQLQueueItem();
item.setTaskuid(item_parent.getSf().getTask().getUniqueid());
//BeanUtils.copyProperties throws an error for null date properties
if(item_parent.getStarted()==null){
item_parent.setStarted(new Date());
}
BeanUtils.copyProperties(item, item_parent);
BeanUtils.copyProperties(item, item_parent.getSf()); //copy items from stackframe
Map data=item_parent.getSf().getData();
if(data!=null){
String dids=(String)data.get(ScheduledTask.FIELD_DEPENDENCY_IDS);
item.setDependentids(dids);
String dtime=(String)data.get(ScheduledTask.FIELD_DEPENDENCY_CHECKTIME);
item.setDependentchecktime(dtime);
try{
item.setConcurrentexecution(Integer.parseInt((String)data.get(ScheduledTask.FIELD_CONCURRENT_EXEC)));
}catch(Exception e){ }
}
TreeMap<String, Comparable> ldata=new TreeMap<String, Comparable>();
ldata.put("scheduler_id", item.getSchedulerid());
ldata.put("trigger_time",item.getTrigger_time());
ldata.put("inject_code", item.getInject_code());
//set it back to null, as the execution isn't started.
item.setStarted(null); //as BeanUtil
int status=0;
String adderror=null;
try{
addLastExecutionDuration(sdb, item);
addTimeoutAndLastExecTimes(sdb,item);
log.debug("addTimeoutAndLastExecTimes queue:"+item.getLastExecutedDuration());
status=add2DBQueue(item);
}catch(Exception e){
adderror=e.getMessage();
status=QUEUE_ERROR_FOUND;
}
boolean logging_required=true;
//overlapping
if(status==QUEUE_OVERLAPPED){
ldata.put("status", ScheduledTask.EXCECUTION_OVERLAPPED);
ldata.put("is_triggered",new Integer(1));
//LoadBalancingHSQLQueueItem other_item=getItemFromQueue(item.getSchedulerid());
List<LoadBalancingHSQLQueueItem> other_items=getItemsFromQueue(item.getSchedulerid());
if(other_items!=null && other_items.size()>0){
logging_required=true;
SimpleDateFormat sdf=new SimpleDateFormat("dd-MMM hh:mm:ss");
Date d=new Date();
String message="";
log.debug("Item overlapped and adding execution log");
if( other_items.size()==1){
d.setTime(other_items.get(0).getTrigger_time());
message="Rejected, The task is already queued, added at "+sdf.format(d);
if(other_items.get(0).getStarted()!=null){
message="Failed, The task is already executing, started at "+sdf.format(other_items.get(0).getStarted());
}
}
//concurrent execution overlap info.
if( other_items.size()>1){
message="Rejected, The task is already queued/executing in "+other_items.size()+" concurrent threads and are scheduled/added at ";
String ma="";
for(LoadBalancingHSQLQueueItem ot_it:other_items){
d.setTime(ot_it.getTrigger_time());
ma+=ma.equals("")?sdf.format(d):", "+sdf.format(d);
}
message+=ma;
}
new SchedulerExePlanLogs(item.getSchedulerid(),item.getTrigger_time()).log(message,SchedulerExePlanLogs.SERVER_WARNING_OVERLAPPED);
}
}else if(status==QUEUE_DUPLICATE_FOUND){
logging_required=false; //ignores the same scheduler id and trigger time, this could be due to multiple cron expression time overlapping
}else if(status==QUEUE_ERROR_FOUND){
if(adderror!=null){
new SchedulerExePlanLogs(item.getSchedulerid(),item.getTrigger_time()).log("Error while adding into queue, scheduler_id: "+item.getSchedulerid()+" trig_time:"+item.getTrigger_time()+" Err:"+adderror,SchedulerExePlanLogs.SERVER_ERROR_WHILE_ADDING_QUEUE);
}
//ldata.put("status", "failed");
}else{
}
if(logging_required){
ArrayList<TreeMap<String, Comparable>> v=new ArrayList<TreeMap<String, Comparable>>();
v.add(ldata);
sdb.updateQueueLog(v,new Vector(), P2PService.getComputerName());
}
if(future==null || (future!=null && future.isDone() )){
future=executor.submit(this);
}
}catch(Exception e){
e.printStackTrace();
log.error("Error while adding into queue, Error:"+e.getMessage());
}finally{
releaseLock();
try{
sdb.closeDB();
}catch(Exception e){
e.printStackTrace();
}
}
}
public void end() throws Exception {
//this.connection.close();
System.out.println("closing the connection....");
}
/**
* override this method here and ignore it as LoadbalancingHSQLQueue uses semaphores
*/
public boolean isPeerBusyWithTask(String machinename,int bid_scheduler_id) {
return false;
}
public Object call() throws Exception {
log.debug("call()");
queueLoop();
return "done";
}
protected void processQueueItem(LoadBalancingHSQLQueueItem currentItem) throws Exception {
if(currentItem!=null && !currentItem.isExecuting()){
String taskuid=currentItem.getSf().getTask().getUniqueid();
Vector<Object> autclients=getRunOnlyOn(taskuid);
//unix server is not authorized to exeucte any task in any case.
Vector<Object> autclients1=(Vector<Object>)autclients.clone();
if(autclients1!=null && autclients1.size()>0){
//sendTask2Peer(autclients1,currentItem);
//to be implemented here (remove the above line )
//sendTask2Peer(autclients1,currentItem.getSchedulerid(),currentItem.getTrigger_time(),currentItem.getNexttrigger_time(),currentItem.getTaskuid());
//remove peers that are already rejected because of technical issues.
if(currentItem.getStarted_peers()!=null){
StringTokenizer st=new StringTokenizer(currentItem.getStarted_peers(),",");
while(st.hasMoreTokens()){
String p=st.nextToken();
if(autclients1.contains(p)) autclients1.remove(p);
}
}
sendTask2Peer(autclients1,currentItem.getSchedulerid(),currentItem.getTrigger_time(),currentItem.getNexttrigger_time(),currentItem.getTaskuid());
}
autclients1=null;
if(autclients!=null){
autclients.clear();
}
autclients=null;
}
}
@Override
public void addExecuteR(RScript item, RScriptListener listener) throws Exception {
//TODO Auto-generated method stub
}
//@Override
//public Vector<LoadBalancingQueueItem> getAllTasks() {
// // TODO Auto-generated method stub
// return null;
//}
public long lastExcecutedTime(){
return lastExcecutedTime;
}
@Override
public RScript startScriptIfNotStarted(RScript rscript, String peer) {
// TODO Auto-generated method stub
return null;
}
@Override
public void scriptFinished(RScript rscript, String result, String status) {
// TODO Auto-generated method stub
}
//@Override
//public void removeItemProcessing(LoadBalancingQueueItem item, String message) {
// // TODO Auto-generated method stub
//
//}
//@Override
//public boolean removeFaultyProcessingTask(int scheduler_id,
// long trigger_time) {
// // TODO Auto-generated method stub
// return false;
//}
//@Override
//public void executionFailed(int schedulerid, String machinename) {
// TODO Auto-generated method stub
//}
//@Override
//public void executionEnded(int schedulerid, long trigger_time) {
// TODO Auto-generated method stub
//
//}
//@Override
//public void cleanupProccesingQueue(int schedulerid, String computername) {
// TODO Auto-generated method stub
//}
//@Override
//public Collection<LoadBalancingQueueItem> getExecutingTasks() {
// TODO Auto-generated method stub
// return null;
//}
//@Override
//public Collection<LoadBalancingQueueItem> getQueuedTasks() {
// // TODO Auto-generated method stub
// return null;
//}
@Override
public void executeScript(Vector<Object> peers, int scriptid)
throws Exception {
// TODO Auto-generated method stub
}
/**
* @deprecated
*/
@Override
public void executionEnded(int schedulerid) {
// TODO Auto-generated method stub
}
@Override
public Collection<RScript> getScriptQueue() throws Exception {
// TODO Auto-generated method stub
return null;
}
@Override
public Collection<RScript> getScriptProcessingQueue() throws Exception {
// TODO Auto-generated method stub
return null;
}
@Override
public void removeScriptFromAllQueue(RScript rscript) {
}
//@Override
//public LoadBalancingQueueItem getItemFromProcessingQueue(int scheduler_id,
// long trigger_time) {
// // TODO Auto-generated method stub
// return null;
//}
}
@@ -0,0 +1,127 @@
/******************************************************************************
*
* Copyright: Intellectual Property of Four Elements Capital Pte Ltd, Singapore.
* All rights reserved.
*
******************************************************************************/
package com.fourelementscapital.scheduler.balance.hsqldb;
import com.fourelementscapital.scheduler.balance.LoadBalancingQueueItem;
public class LoadBalancingHSQLQueueItem extends LoadBalancingQueueItem {
private long trigger_time=0;
private long nexttrigger_time=0;
private long started_time=0;
private String status=null;
private String taskuid=null;
private Byte queuegate=null;
private String dependentids=null;
private String dependentchecktime=null;
private boolean waitingfordp=false;
private String started_peers=null;
private int concurrentexecution=1;
public int getConcurrentexecution() {
return concurrentexecution;
}
public void setConcurrentexecution(int concurrentexecution) {
this.concurrentexecution = concurrentexecution;
}
public String getStarted_peers() {
return started_peers;
}
public void setStarted_peers(String started_peers) {
this.started_peers = started_peers;
}
private long id;
public String getDependentids() {
return dependentids;
}
public boolean isWaitingfordp() {
return waitingfordp;
}
public void setWaitingfordp(boolean waitingfordp) {
this.waitingfordp = waitingfordp;
}
public void setDependentids(String dependentids) {
this.dependentids = dependentids;
}
public String getDependentchecktime() {
return dependentchecktime;
}
public void setDependentchecktime(String dependentchecktime) {
this.dependentchecktime = dependentchecktime;
}
public long getId() {
return id;
}
public Byte getQueuegate() {
return queuegate;
}
public void setQueuegate(Byte queuegate) {
this.queuegate = queuegate;
}
public void setId(long id) {
this.id = id;
}
public String getTaskuid() {
return taskuid;
}
public void setTaskuid(String taskuid) {
this.taskuid = taskuid;
}
private long lastTenderTime=0;
public long getLastTenderTime() {
return lastTenderTime;
}
public void setLastTenderTime(long lastTenderTime) {
this.lastTenderTime = lastTenderTime;
}
public long getTrigger_time() {
return trigger_time;
}
public void setTrigger_time(long trigger_time) {
this.trigger_time = trigger_time;
}
public long getNexttrigger_time() {
return nexttrigger_time;
}
public void setNexttrigger_time(long nexttrigger_time) {
this.nexttrigger_time = nexttrigger_time;
}
public long getStarted_time() {
return started_time;
}
public void setStarted_time(long started_time) {
this.started_time = started_time;
}
public String getStatus() {
return status;
}
public void setStatus(String status) {
this.status = status;
}
}
@@ -0,0 +1,60 @@
/******************************************************************************
*
* Copyright: Intellectual Property of Four Elements Capital Pte Ltd, Singapore.
* All rights reserved.
*
******************************************************************************/
package com.fourelementscapital.scheduler.io.msg;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import com.fourelementscapital.scheduler.io.request.IOPeerRequest;
import com.fourelementscapital.scheduler.io.server.ServerConnection;
import com.fourelementscapital.scheduler.io.server.ServerConnectionHandler;
import com.fourelementscapital.scheduler.queue.QueueStackManager;
public class PeerRequestLogin extends IOPeerRequest {
private String peername=null;
private Logger log = LogManager.getLogger(PeerRequestLogin.class.getName());
private String queuestring=null;
public String getQueuestring() {
return queuestring;
}
public void setQueuestring(String queuestring) {
this.queuestring = queuestring;
}
public String getPeername() {
return peername;
}
public void setPeername(String peername) {
this.peername = peername;
}
@Override
public void executeAtServer(ServerConnection sc) {
log.debug(" logged-in message...from peer:"+this.peername+" ServerConnection, IP:"+sc.getIp()+" connected time:"+sc.getConnectedtime()+" qstring:"+getQueuestring());
sc.setUser(this.peername);
try{
ServerConnectionHandler.refreshServerConnection(sc);
QueueStackManager.buildQueue4Peer(this.peername);
QueueStackManager.server2SyncPeerQueue(this.peername, getQueuestring());
}catch(Exception e){
log.error("error while refreshing, e:"+e.getMessage());
}
}
}
@@ -0,0 +1,99 @@
/******************************************************************************
*
* Copyright: Intellectual Property of Four Elements Capital Pte Ltd, Singapore.
* All rights reserved.
*
******************************************************************************/
package com.fourelementscapital.scheduler.io.msg;
import java.util.Date;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import com.fourelementscapital.scheduler.balance.LoadBalancingQueue;
import com.fourelementscapital.scheduler.io.request.IOPeerRequest;
import com.fourelementscapital.scheduler.io.server.ServerConnection;
import com.fourelementscapital.scheduler.queue.QueueStackManager;
import com.fourelementscapital.scheduler.rscript.RScript;
public class PeerScriptFinished extends IOPeerRequest {
private Logger log = LogManager.getLogger(PeerScriptFinished.class.getName());
private String script_uid;
private String queue_uid;
private String resultxml;
private String error;
private String status;
public String getStatus() {
return status;
}
public void setStatus(String status) {
this.status = status;
}
public String getResultxml() {
return resultxml;
}
public String getError() {
return error;
}
public void setResultxml(String resultxml) {
this.resultxml = resultxml;
}
public void setError(String error) {
this.error = error;
}
public String getScript_uid() {
return script_uid;
}
public String getQueue_uid() {
return queue_uid;
}
public void setScript_uid(String script_uid) {
this.script_uid = script_uid;
}
public void setQueue_uid(String queue_uid) {
this.queue_uid = queue_uid;
}
@Override
public void executeAtServer(ServerConnection pc) {
// TODO Auto-generated method stub
long start=new Date().getTime();
RScript rs=new RScript();
rs.setUid(this.getScript_uid());
rs.setError(this.getError());
rs.setResultXML(this.getResultxml());
LoadBalancingQueue.getExecuteRScriptDefault().scriptFinished(rs, getResultxml(),getStatus());
long delay0=new Date().getTime()-start;
try{
QueueStackManager.setStackIdle(pc.getUser(), getQueue_uid());
}catch(Exception e){
e.printStackTrace();
}
long delay=new Date().getTime()-start;
log.debug("executeAtServer() : called:"+this.getScript_uid()+" finished, thread:"+Thread.currentThread().getName() +" --->delay0:"+delay+" delay:"+delay);
}
}
@@ -0,0 +1,273 @@
/******************************************************************************
*
* Copyright: Intellectual Property of Four Elements Capital Pte Ltd, Singapore.
* All rights reserved.
*
******************************************************************************/
package com.fourelementscapital.scheduler.io.msg;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Iterator;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.json.JSONArray;
import org.rosuda.JRI.REXP;
import org.rosuda.JRI.RVector;
import com.fourelementscapital.scheduler.ScheduledTaskFactory;
import com.fourelementscapital.scheduler.engines.ScheduledTask;
import com.fourelementscapital.scheduler.engines.StackFrame;
import com.fourelementscapital.scheduler.engines.StackFrameCallBack;
import com.fourelementscapital.scheduler.error.ClientError;
import com.fourelementscapital.scheduler.exception.SchedulerException;
import com.fourelementscapital.scheduler.io.request.IOServerRequest;
import com.fourelementscapital.scheduler.p2p.P2PService;
import com.fourelementscapital.scheduler.queue.QueueStackManager;
import com.fourelementscapital.scheduler.rscript.RScript;
public class ServerExecuteScript extends IOServerRequest {
private Logger log = LogManager.getLogger(ServerExecuteScript.class.getName());
private String script_uid;
private String script;
private String taskuid;
private String queue_uid;
public String getTaskuid() {
return taskuid;
}
public String getQueue_uid() {
return queue_uid;
}
public void setTaskuid(String taskuid) {
this.taskuid = taskuid;
}
public void setQueue_uid(String queue_uid) {
this.queue_uid = queue_uid;
}
public String getScript() {
return script;
}
public String getScript_uid() {
return script_uid;
}
public void setScript_uid(String script_uid) {
this.script_uid = script_uid;
}
public void setScript(String script) {
this.script = script;
}
public void executeAtPeer() {
String peername=P2PService.getComputerName();
try{
QueueStackManager.setStackBusy(peername, this.queue_uid);
ScheduledTask task=new ScheduledTaskFactory().getTaskFromAll(getTaskuid());
StackFrame sf=new StackFrame(task,null);
RScript rs=new RScript();
rs.setUid(getScript_uid());
rs.setScript(getScript());
sf.setRscript(rs);
sf.setQueue_uid(this.queue_uid);
sf.setStarted_time(new Date().getTime());
sf.addCallBack(new StackFrameCallBack(){
public void callBack(StackFrame sf,String status,SchedulerException se){
String result=null;
if(sf.getRscript()!=null){
result="<?xml version=\"1.0\"?><output status=\""+status+"\" peer=\""+P2PService.getComputerName()+"\">";
if(sf.getRscript().getResultXML()!=null){
result+="<result>"+sf.getRscript().getResultXML()+"</result>";
}
REXP x1=sf.getRscript().getResult();
String jsonresult="";
if(x1!=null){
RVector rv=x1.asVector();
String prtn="";
prtn=parse(x1,prtn);
try{
jsonresult=parseJSON(x1, jsonresult);
}catch(Exception e){
String errmsg="Error while parsing to json";
log.error(errmsg);
sf.getRscript().setError(
sf.getRscript().getError()!=null?sf.getRscript().getError()+", "+errmsg:" Error while parsing result to JSON "
);
}
result+="<result>"+prtn+"</result>";
}
SimpleDateFormat format=new SimpleDateFormat("d MMM yyyy HH:mm:ss SSS");
String err=sf.getRscript().getError()!=null ?sf.getRscript().getError():"";
result+="<error>"+err+"</error>";
result+="<startedTime>"+format.format(new Date(sf.getStarted_time()))+"</startedTime>";
result+="<duration in=\"milliseconds\">"+(new Date().getTime()-sf.getStarted_time())+"</duration>";
result+="</output>";
try{
String peername=P2PService.getComputerName();
QueueStackManager.setStackIdle(peername, sf.getQueue_uid());
}catch(Exception e){
log.error("error while settting queue stack idle,e:"+e.getMessage());
}
log.debug("script finished and call back, just before sending back to server");
PeerScriptFinished psf=new PeerScriptFinished();
psf.setScript_uid(sf.getRscript().getUid());
psf.setQueue_uid(getQueue_uid());
psf.setError(err);
psf.setResultxml(result);
psf.setStatus(status);
psf.send();
}
}
});
log.debug("before executing script, task:"+task);
String status=ScheduledTask.EXCECUTION_FAIL;
try{
log.debug("just before executed task:"+task.getClass().getName());
task.execute(sf);
log.debug("just after executed task");
status=ScheduledTask.EXCECUTION_SUCCESS;
}catch(Exception e){
log.error("error:::::"+e.getMessage()+" task:"+task+" getTaskuid():"+getTaskuid());
e.printStackTrace();
//Number nid=(Number)this.frame.getData().get("id");
status=ScheduledTask.EXCECUTION_FAIL;
ClientError.reportError(e, null);
}finally{
if(sf.getCallBack()!=null){
try{
sf.getCallBack().callBack(sf, status,null);
}catch(Exception e){
ClientError.reportError(e, null);
}
}
log.debug("thread finised....");
}
}catch(Exception e){
}finally{
try{
//QueueStackManager.setStackIdle(peername, this.queue_uid);
}catch(Exception e){
log.error("error while settting queue stack idle,e:"+e.getMessage());
}
}
}
private String parse(REXP rxp, String rtn) {
if(rxp.getType()==REXP.XT_VECTOR ){
RVector rv=rxp.asVector();
for(Iterator i=rv.iterator();i.hasNext();){
Object obj=i.next();
if(obj instanceof REXP){
//log.debug("~~~~~Parsing.......obj:"+obj);
rtn+=parse((REXP)obj, "");
}
}
}
if(rxp.getType()==REXP.XT_DOUBLE){
rtn+="<double>"+rxp.asDouble()+"</double>";
}
if(rxp.getType()==REXP.XT_ARRAY_DOUBLE){
double[] ar=rxp.asDoubleArray();
rtn+="<array>";
for(int i=0;i<ar.length;i++){
rtn+="<double>"+ar[i]+"</double>";
}
rtn+="</array>";
}
if(rxp.getType()==REXP.XT_STR){
rtn+="<string>"+rxp.asString()+"</string>";
}
//if(rxp.getType()==REXP.XT_STR || rxp.getType()==REXP.XT_VECTOR || rxp.getType()==REXP.XT_DOUBLE || rxp.getType()==REXP.XT_ARRAY_DOUBLE){}
//else log.debug("rxp.getType():"+rxp.getType());
return rtn;
}
private String parseJSON(REXP rxp, String rtn) throws Exception {
if(rxp.getType()==REXP.XT_VECTOR ){
RVector rv=rxp.asVector();
for(Iterator i=rv.iterator();i.hasNext();){
Object obj=i.next();
if(obj instanceof REXP){
//log.debug("~~~~~Parsing.......obj:"+obj);
rtn+=parseJSON((REXP)obj, "");
}
}
}
JSONArray arr=new JSONArray();
if(rxp.getType()==REXP.XT_DOUBLE){
arr.put(rxp.asDouble());
}
if(rxp.getType()==REXP.XT_ARRAY_DOUBLE){
double[] ar=rxp.asDoubleArray();
for(int i=0;i<ar.length;i++){
arr.put(ar[i]);
}
}
if(rxp.getType()==REXP.XT_STR){
//rtn+="<string>"+rxp.asString()+"</string>";
arr.put(rxp.asString());
}
//if(rxp.getType()==REXP.XT_STR || rxp.getType()==REXP.XT_VECTOR || rxp.getType()==REXP.XT_DOUBLE || rxp.getType()==REXP.XT_ARRAY_DOUBLE){}
//else log.debug("rxp.getType():"+rxp.getType());
return arr.toString();
}
}
@@ -0,0 +1,101 @@
/******************************************************************************
*
* Copyright: Intellectual Property of Four Elements Capital Pte Ltd, Singapore.
* All rights reserved.
*
******************************************************************************/
package com.fourelementscapital.scheduler.io.peer;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;
import java.util.Map;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import com.fourelementscapital.scheduler.io.msg.PeerRequestLogin;
import com.fourelementscapital.scheduler.p2p.P2PService;
import com.fourelementscapital.scheduler.queue.QueueStackManager;
public class PeerClientHandler extends ChannelInboundHandlerAdapter {
private static final Logger log = LogManager.getLogger(PeerClientHandler.class.getName());
private static ChannelHandlerContext connection2server=null;
@Override
public void channelActive(ChannelHandlerContext ctx) throws Exception{
log.debug("channelActive");
if(connection2server==null || (connection2server!=null && !connection2server.channel().isActive()) ){
connection2server=ctx;
PeerRequestLogin login=new PeerRequestLogin();
String computername=P2PService.getComputerName();
log.debug("computer name");
login.setPeername(computername);
try{
//in-case of earlier had somthing...
//QueueStackManager.peerDisconnected(computername);
if(QueueStackManager.getAllQueueStacks().size()>0){
}else{
QueueStackManager.buildQueue4Peer(computername);
}
login.setQueuestring(QueueStackManager.getPeerQueueStatForServer());
}catch(Exception e){
e.printStackTrace();
}
login.send();
//new Post2Server(login).send();
}
log.debug("channelActive end");
}
@Override
public void channelInactive(ChannelHandlerContext ctx) throws Exception{
ctx.close();
if(ctx.channel().isOpen()){
ctx.disconnect();
}
connection2server=null;
System.out.println("PeerClientHandler() channel inactive");
}
@Override
public void channelRead(ChannelHandlerContext ctx, Object request) throws Exception {
//System.out.println("PeerClientHandler +"+request);
if(request!=null && request instanceof Map){
new PeerIORequestReceive((Map)request).process(ctx);
}
}
@Override
public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
//logger.log(Level.WARNING, "Unexpected exception from downstream.", cause);
connection2server=null;
System.out.println("PeerClientHandler() channel exceptionCaught");
log.error("exceptionCaught()", cause);
ctx.close();
if(ctx.channel().isOpen()){
ctx.disconnect();
}
}
public static ChannelHandlerContext getChannelHandler(){
return PeerClientHandler.connection2server;
}
}
@@ -0,0 +1,106 @@
/******************************************************************************
*
* Copyright: Intellectual Property of Four Elements Capital Pte Ltd, Singapore.
* All rights reserved.
*
******************************************************************************/
package com.fourelementscapital.scheduler.io.peer;
import io.netty.channel.ChannelHandlerContext;
import java.lang.reflect.Constructor;
import java.util.Map;
import java.util.concurrent.Callable;
import java.util.concurrent.Future;
import org.apache.commons.beanutils.BeanUtils;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import com.fourelementscapital.scheduler.io.request.IOExcecutors;
import com.fourelementscapital.scheduler.io.request.IOServerRequest;
import com.fourelementscapital.scheduler.io.request.IOServerRequestCallback;
import com.fourelementscapital.scheduler.p2p.msg.MessageNames;
public class PeerIORequestReceive {
private Map data=null;
private Logger log = LogManager.getLogger(PeerIORequestReceive.class.getName());
//private static ExecutorService dispatchService=Executors.newCachedThreadPool() ; // Executors.newFixedThreadPool(15);
public PeerIORequestReceive(Map attachments){
this.data=attachments;
}
public void process(ChannelHandlerContext ctx){
String classname=(String)this.data.get(MessageNames.MESSAGE_BEAN_NAME);
String loc="1";
if(classname!=null && !classname.equals("")){
try{
Class c=Class.forName(classname);
Constructor ct = c.getConstructor();
IOServerRequest req=(IOServerRequest)ct.newInstance();
BeanUtils.populate(req, this.data);
//System.out.println("PeerIORequestReceive req:"+req);
log.debug("thread name:"+Thread.currentThread().getName());
if(req instanceof IOServerRequest){
//req.executeAtPeer();
Future fu=IOExcecutors.threadpool.submit(
new Callable<String>() {
public String call(){
this.request.executeAtPeer();
return "";
}
private IOServerRequest request;
public Callable<String> init(IOServerRequest req){
this.request=req;
return this;
}
}.init(req)
);
//fu.get();
}
if(req instanceof IOServerRequestCallback){
IOServerRequestCallback req1=(IOServerRequestCallback)req;
Future fu=IOExcecutors.threadpool.submit(
new Callable<String>() {
public String call(){
this.request.executeAtPeer();
try{
Map data=BeanUtils.describe(this.request);
data.put(MessageNames.MESSAGE_BEAN_NAME,this.request.getClass().getName());
this.ctx.writeAndFlush(data);
}catch(Exception e){
log.error("while invoking callback: e:"+e.getMessage());
}
return null;
}
private IOServerRequestCallback request;
private ChannelHandlerContext ctx;
public Callable<String> init(IOServerRequestCallback req,ChannelHandlerContext c){
this.request=req;
this.ctx=c;
return this;
}
}.init(req1,ctx)
);
//fu.get();
}
//HashMap rtn=req.executeAtPeer();
//rtn will be passed back to client...
}catch(Exception e){
e.printStackTrace();
log.error("process() E:"+e.getMessage()+" classname:"+classname+" loc:"+loc);
}
}
}
}
@@ -0,0 +1,21 @@
/******************************************************************************
*
* Copyright: Intellectual Property of Four Elements Capital Pte Ltd, Singapore.
* All rights reserved.
*
******************************************************************************/
package com.fourelementscapital.scheduler.io.request;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
public class IOExcecutors {
//public static ExecutorService threadpool= Executors.newFixedThreadPool(30);
public static ExecutorService threadpool= Executors.newCachedThreadPool();
}
@@ -0,0 +1,44 @@
/******************************************************************************
*
* Copyright: Intellectual Property of Four Elements Capital Pte Ltd, Singapore.
* All rights reserved.
*
******************************************************************************/
package com.fourelementscapital.scheduler.io.request;
import java.util.HashMap;
import java.util.Map;
import org.apache.commons.beanutils.BeanUtils;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import com.fourelementscapital.scheduler.io.peer.PeerClientHandler;
import com.fourelementscapital.scheduler.io.server.ServerConnection;
import com.fourelementscapital.scheduler.p2p.msg.MessageNames;
public abstract class IOPeerRequest {
private Logger log = LogManager.getLogger(IOPeerRequest.class.getName());
public abstract void executeAtServer(ServerConnection pc);
public final void send(){
try{
HashMap h=new HashMap();
Map data=BeanUtils.describe(this);
data.put(MessageNames.MESSAGE_BEAN_NAME,getClass().getName());
PeerClientHandler.getChannelHandler().writeAndFlush(data);
}catch(Exception e){
log.error("Error:"+e.getMessage());
}
}
}
@@ -0,0 +1,82 @@
/******************************************************************************
*
* Copyright: Intellectual Property of Four Elements Capital Pte Ltd, Singapore.
* All rights reserved.
*
******************************************************************************/
package com.fourelementscapital.scheduler.io.request;
import java.util.HashMap;
import java.util.Map;
import org.apache.commons.beanutils.BeanUtils;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import com.fourelementscapital.scheduler.io.server.ServerConnection;
import com.fourelementscapital.scheduler.io.server.ServerConnectionHandler;
import com.fourelementscapital.scheduler.p2p.msg.MessageNames;
public abstract class IOServerRequest {
public abstract void executeAtPeer();
private Logger log = LogManager.getLogger(IOServerRequest.class.getName());
//private static long lastsent=0 ;
public final void send(String peername){
try{
HashMap h=new HashMap();
Map data=BeanUtils.describe(this);
data.put(MessageNames.MESSAGE_BEAN_NAME,getClass().getName());
ServerConnection sc=ServerConnectionHandler.getServerConnection(peername);
if(sc!=null){
sc.getChcontext().writeAndFlush(data);
}
log.debug("sending this:peername"+peername);
/*
if(lastsent>0){
long delay=new Date().getTime()-lastsent;
if(delay>1000){
log.error("delay >500ms: "+delay);
}
}
lastsent=new Date().getTime();
*/
}catch(Exception e){
log.error("Error:"+e.getMessage());
}
}
/**
* @deprecated
* @param sc
*/
private final void send(ServerConnection sc){
try{
HashMap h=new HashMap();
Map data=BeanUtils.describe(this);
data.put(MessageNames.MESSAGE_BEAN_NAME,getClass().getName());
log.debug("sc:"+sc);
if(sc!=null){
sc.getChcontext().writeAndFlush(data);
}
}catch(Exception e){
log.error("Error:"+e.getMessage());
}
}
}
@@ -0,0 +1,19 @@
/******************************************************************************
*
* Copyright: Intellectual Property of Four Elements Capital Pte Ltd, Singapore.
* All rights reserved.
*
******************************************************************************/
package com.fourelementscapital.scheduler.io.request;
public abstract class IOServerRequestCallback extends IOServerRequest {
public abstract void callBack();
}
@@ -0,0 +1,135 @@
/******************************************************************************
*
* Copyright: Intellectual Property of Four Elements Capital Pte Ltd, Singapore.
* All rights reserved.
*
******************************************************************************/
package com.fourelementscapital.scheduler.io.server;
import io.netty.channel.ChannelHandlerContext;
import java.net.InetSocketAddress;
import com.googlecode.cqengine.attribute.Attribute;
import com.googlecode.cqengine.attribute.SimpleAttribute;
import com.googlecode.cqengine.attribute.SimpleNullableAttribute;
import com.googlecode.cqengine.codegen.AttributesGenerator;
public class ServerConnection {
private String user;
private ChannelHandlerContext chcontext;
private String ip;
private long connectedtime;
public String getUser() {
return user;
}
public long getConnectedtime() {
return connectedtime;
}
public void setUser(String user) {
this.user = user;
}
public void setChcontext(ChannelHandlerContext chcontext) {
this.chcontext = chcontext;
}
public void setConnectedtime(long connectedtime) {
this.connectedtime = connectedtime;
}
public ChannelHandlerContext getChcontext() {
return chcontext;
}
public String getIp() {
if(this.ip==null){
InetSocketAddress add=(InetSocketAddress)this.chcontext.channel().remoteAddress();
ip=add.getHostName();
}
return ip;
}
public void setIp(String ip) {
this.ip = ip;
}
public ServerConnection(ChannelHandlerContext chc){
this.chcontext=chc;
}
public static void main(String[] args) {
System.out.println(AttributesGenerator.generateAttributesForPastingIntoTargetClass(ServerConnection.class));
}
/*
public boolean equals(Object other) {
if(other instanceof ServerConnection){
ServerConnection ot=(ServerConnection)other;
if(ot.chcontext.equals(this.chcontext)){
return true;
}else{
return false;
}
}else{
return false;
}
}
*/
/**
* CQEngine attribute for accessing field {@code ServerConnection.user}.
*/
// Note: For best performance:
// - if this field cannot be null, replace this SimpleNullableAttribute with a SimpleAttribute
public static final Attribute<ServerConnection, String> USER = new SimpleNullableAttribute<ServerConnection, String>("USER") {
public String getValue(ServerConnection serverconnection) { return serverconnection.user; }
};
/**
* CQEngine attribute for accessing field {@code ServerConnection.chcontext}.
*/
// Note: For best performance:
// - if this field cannot be null, replace this SimpleNullableAttribute with a SimpleAttribute
public static final Attribute<ServerConnection, ChannelHandlerContext> CHCONTEXT = new SimpleAttribute<ServerConnection, ChannelHandlerContext>("CHCONTEXT") {
public ChannelHandlerContext getValue(ServerConnection serverconnection) { return serverconnection.chcontext; }
};
/**
* CQEngine attribute for accessing field {@code ServerConnection.ip}.
*/
// Note: For best performance:
// - if this field cannot be null, replace this SimpleNullableAttribute with a SimpleAttribute
public static final Attribute<ServerConnection, String> IP = new SimpleNullableAttribute<ServerConnection, String>("IP") {
public String getValue(ServerConnection serverconnection) { return serverconnection.ip; }
};
/**
* CQEngine attribute for accessing field {@code ServerConnection.connectedtime}.
*/
public static final Attribute<ServerConnection, Long> CONNECTEDTIME = new SimpleAttribute<ServerConnection, Long>("CONNECTEDTIME") {
public Long getValue(ServerConnection serverconnection) { return serverconnection.connectedtime; }
};
}
@@ -0,0 +1,145 @@
/******************************************************************************
*
* Copyright: Intellectual Property of Four Elements Capital Pte Ltd, Singapore.
* All rights reserved.
*
******************************************************************************/
package com.fourelementscapital.scheduler.io.server;
import static com.googlecode.cqengine.query.QueryFactory.equal;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;
import java.util.Date;
import java.util.Iterator;
import java.util.Map;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import com.fourelementscapital.scheduler.queue.QueueStackManager;
import com.googlecode.cqengine.CQEngine;
import com.googlecode.cqengine.IndexedCollection;
import com.googlecode.cqengine.index.navigable.NavigableIndex;
import com.googlecode.cqengine.index.unique.UniqueIndex;
import com.googlecode.cqengine.query.Query;
import com.googlecode.cqengine.resultset.ResultSet;
public class ServerConnectionHandler extends ChannelInboundHandlerAdapter {
private static final Logger log = LogManager.getLogger(ServerConnectionHandler.class.getName());
//private static Vector<ServerConnection> connection=new Vector();
private static IndexedCollection<ServerConnection> connections = CQEngine.newInstance();
static{
connections.addIndex(NavigableIndex.onAttribute(ServerConnection.IP));
connections.addIndex(UniqueIndex.onAttribute(ServerConnection.USER));
connections.addIndex(NavigableIndex.onAttribute(ServerConnection.CONNECTEDTIME));
connections.addIndex(UniqueIndex.onAttribute(ServerConnection.CHCONTEXT));
}
public void channelActive(ChannelHandlerContext ctx) throws Exception {
if(getServerConnection(ctx)==null){
log.debug("new connection adding connection pool");
ServerConnection sc=new ServerConnection(ctx);
sc.setConnectedtime(new Date().getTime());
connections.add(sc);
log.debug("added");
}
}
@Override
public void channelRead(ChannelHandlerContext ctx, Object request) throws Exception {
//ServerConnection pc=new ServerConnection(ctx);
if(request!=null && request instanceof Map){
ServerConnection sc=getServerConnection(ctx);
log.debug("received map data from ip:"+sc.getIp());
new ServerIORequestReceive((Map)request).process(sc);
}
}
@Override
public void channelReadComplete(ChannelHandlerContext ctx) throws Exception {
ctx.flush();
}
@Override
public void channelInactive(ChannelHandlerContext ctx) throws Exception {
removeServerConnection(ctx);
log.debug("channel inactive and removing server connection");
ctx.close();
}
@Override
public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
//log.debug("exception caught");
log.error("exceptionCaught()", cause);
removeServerConnection(ctx);
ctx.close();
}
private ServerConnection getServerConnection(ChannelHandlerContext ctx) throws Exception {
Query<ServerConnection> query=equal(ServerConnection.CHCONTEXT,ctx);
ResultSet<ServerConnection> rs=connections.retrieve(query);
if(!rs.isEmpty()){
return rs.iterator().next();
}else{
return null;
}
}
private boolean removeServerConnection(ChannelHandlerContext ctx) throws Exception {
Query<ServerConnection> query=equal(ServerConnection.CHCONTEXT,ctx);
ResultSet<ServerConnection> rs=connections.retrieve(query);
boolean success=false;
if(!rs.isEmpty()){
ServerConnection sc=rs.iterator().next();
if(sc!=null && sc.getUser()!=null){
QueueStackManager.peerDisconnected(sc.getUser());
connections.remove(sc);
success=true;
}
}
return success;
}
public static ServerConnection getServerConnection(String peername) throws Exception {
Query<ServerConnection> query=equal(ServerConnection.USER,peername);
ResultSet<ServerConnection> rs=connections.retrieve(query);
if(!rs.isEmpty()){
return rs.iterator().next();
}else{
return null;
}
}
public static Iterator<ServerConnection> getAllServerConnection() throws Exception {
return connections.iterator();
}
public static void refreshServerConnection(ServerConnection sc) throws Exception {
synchronized(sc){
connections.remove(sc);
connections.add(sc);
}
}
}
@@ -0,0 +1,130 @@
/******************************************************************************
*
* Copyright: Intellectual Property of Four Elements Capital Pte Ltd, Singapore.
* All rights reserved.
*
******************************************************************************/
package com.fourelementscapital.scheduler.io.server;
import java.lang.reflect.Constructor;
import java.util.Map;
import java.util.concurrent.Callable;
import java.util.concurrent.Future;
import org.apache.commons.beanutils.BeanUtils;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import com.fourelementscapital.scheduler.io.request.IOExcecutors;
import com.fourelementscapital.scheduler.io.request.IOPeerRequest;
import com.fourelementscapital.scheduler.io.request.IOServerRequestCallback;
import com.fourelementscapital.scheduler.p2p.msg.MessageNames;
public class ServerIORequestReceive {
private Map data=null;
private Logger log = LogManager.getLogger(ServerIORequestReceive.class.getName());
//private static ExecutorService dispatchService= Executors.newFixedThreadPool(30);
public ServerIORequestReceive(Map attachments){
this.data=attachments;
}
public void process( ServerConnection pc){
String classname=(String)this.data.get(MessageNames.MESSAGE_BEAN_NAME);
String loc="1";
if(classname!=null && !classname.equals("")){
try{
Class c=Class.forName(classname);
Constructor ct = c.getConstructor();
Object req=ct.newInstance();
BeanUtils.populate(req, this.data);
//Future fu=dispatchService.submit(
Future fu=IOExcecutors.threadpool.submit(
new Callable<String>() {
public String call(){
if(this.request instanceof IOPeerRequest){
IOPeerRequest req1=(IOPeerRequest)this.request;
req1.executeAtServer(this.pc);
}
if(this.request instanceof IOServerRequestCallback){
IOServerRequestCallback req2=(IOServerRequestCallback)this.request;
req2.callBack();
}
return null;
}
private Object request;
private ServerConnection pc;
public Callable<String> init(Object req,ServerConnection p){
this.request=req;
this.pc=p;
return this;
}
}.init(req,pc)
);
/*
* this block is not good, as it is creating so many threds after some times
* the performance is really going bad.
Thread th= new Thread( new Runnable() {
public void run(){
if(this.request instanceof IOPeerRequest){
IOPeerRequest req1=(IOPeerRequest)this.request;
req1.executeAtServer(this.pc);
}
if(this.request instanceof IOServerRequestCallback){
IOServerRequestCallback req2=(IOServerRequestCallback)this.request;
req2.callBack();
}
}
private Object request;
private ServerConnection pc;
public Runnable init(Object req,ServerConnection p){
this.request=req;
this.pc=p;
return this;
}
}.init(req,pc));
th.start();
*/
log.debug("received, data:classname:"+classname+":script_uid:"+this.data.get("script_uid"));
//fu.get();
/*
if(req instanceof IOPeerRequest){
IOPeerRequest req1=(IOPeerRequest)req;
req1.executeAtServer(pc);
}
if(req instanceof IOServerRequestCallback){
IOServerRequestCallback req2=(IOServerRequestCallback)req;
req2.callBack();
}
*/
//rtn will be passed back to client...
}catch(Exception e){
e.printStackTrace();
log.error("process() E:"+e.getMessage()+" classname:"+classname+" loc:"+loc);
}
}
}
}
@@ -0,0 +1,288 @@
/******************************************************************************
*
* Copyright: Intellectual Property of Four Elements Capital Pte Ltd, Singapore.
* All rights reserved.
*
******************************************************************************/
package com.fourelementscapital.scheduler.p2p.msg.impl;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.StringTokenizer;
import org.apache.jcs.JCS;
import org.apache.jcs.engine.behavior.IElementAttributes;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.json.JSONArray;
import org.rosuda.JRI.REXP;
import org.rosuda.JRI.RVector;
import com.fourelementscapital.scheduler.ScheduledTaskFactory;
import com.fourelementscapital.scheduler.balance.LoadBalancingQueue;
import com.fourelementscapital.scheduler.engines.ScheduledTask;
import com.fourelementscapital.scheduler.engines.StackFrame;
import com.fourelementscapital.scheduler.engines.StackFrameCallBack;
import com.fourelementscapital.scheduler.exception.SchedulerException;
import com.fourelementscapital.scheduler.p2p.P2PService;
import com.fourelementscapital.scheduler.p2p.listener.OutgoingMessageCallBack;
import com.fourelementscapital.scheduler.p2p.msg.PostCallBack;
import com.fourelementscapital.scheduler.p2p.msg.PostMessage;
import com.fourelementscapital.scheduler.peer.QueueAbstract;
import com.fourelementscapital.scheduler.peer.QueueFactory;
import com.fourelementscapital.scheduler.rscript.RScript;
public class ExecuteScript extends RScript implements PostCallBack {
private Logger log = LogManager.getLogger(ExecuteScript.class.getName());
public synchronized Map executeAtDestination() {
//System.out.println("Excecution stated:");
HashMap rtndata=new HashMap();
try{
log.debug("getTaskuid():"+getTaskuid());
ScheduledTask task=new ScheduledTaskFactory().getTaskFromAll(getTaskuid());
log.debug("task:"+task);
StackFrame sf=new StackFrame(task,null);
//log.debug("~~~ executeAtDestination() called");
log.debug("-------->"+getUid());
//log.debug("task class:"+task.getClass().getName());
//log.debug("taskUid:"+getTaskuid());
sf.setRscript(this);
sf.setReplyTo( getMsgCreator()) ;
sf.addCallBack(new StackFrameCallBack(){
public void callBack(StackFrame sf,String status,SchedulerException se){
String result=null;
if(sf.getRscript()!=null){
result="<?xml version=\"1.0\"?><output status=\""+status+"\" peer=\""+P2PService.getComputerName()+"\">";
if(sf.getRscript().getResultXML()!=null){
result+="<result>"+sf.getRscript().getResultXML()+"</result>";
}
REXP x1=sf.getRscript().getResult();
String jsonresult="";
if(x1!=null){
RVector rv=x1.asVector();
String prtn="";
prtn=parse(x1,prtn);
try{
jsonresult=parseJSON(x1, jsonresult);
}catch(Exception e){
String errmsg="Error while parsing to json";
log.error(errmsg);
sf.getRscript().setError(
sf.getRscript().getError()!=null?sf.getRscript().getError()+", "+errmsg:" Error while parsing result to JSON "
);
}
result+="<result>"+prtn+"</result>";
}
SimpleDateFormat format=new SimpleDateFormat("d MMM yyyy HH:mm:ss SSS");
String err=sf.getRscript().getError()!=null ?sf.getRscript().getError():"";
result+="<error>"+err+"</error>";
result+="<startedTime>"+format.format(new Date(sf.getStarted_time()))+"</startedTime>";
result+="<duration in=\"milliseconds\">"+(new Date().getTime()-sf.getStarted_time())+"</duration>";
result+="</output>";
ScriptFinished finished=new ScriptFinished();
finished.setStatus(status);
finished.setResult(result);
finished.setUid(sf.getRscript().getUid());
finished.setError(sf.getRscript().getError());
finished.setJsonresult(jsonresult);
finished.setPriority(OutgoingMessageCallBack.PRIORITY_VERY_VERY_HIGH);
PostMessage ps=new PostMessage(finished,sf.getReplyTo());
ps.send();
}
}
});
boolean rtn=false;
QueueAbstract qa=new QueueFactory().getQueue(getTaskuid());
if(qa.isRoomForThread()){
qa.addExThread(sf);
rtn=true;
}
Object objs[]=qa.getExecutingStacks();
String sc_uids="";;
for(int i=0;i<objs.length;i++){
StackFrame sf1=(StackFrame)objs[i];
if(sf1.getRscript()!=null){
sc_uids+=(sc_uids.equals(""))?sf1.getRscript().getUid():"|"+sf1.getRscript().getUid();
}
}
rtndata.put("running_uids", sc_uids);
}catch(Exception e){
e.printStackTrace();
}
return rtndata;
}
private String parse(REXP rxp, String rtn) {
if(rxp.getType()==REXP.XT_VECTOR ){
RVector rv=rxp.asVector();
for(Iterator i=rv.iterator();i.hasNext();){
Object obj=i.next();
if(obj instanceof REXP){
//log.debug("~~~~~Parsing.......obj:"+obj);
rtn+=parse((REXP)obj, "");
}
}
}
if(rxp.getType()==REXP.XT_DOUBLE){
rtn+="<double>"+rxp.asDouble()+"</double>";
}
if(rxp.getType()==REXP.XT_ARRAY_DOUBLE){
double[] ar=rxp.asDoubleArray();
rtn+="<array>";
for(int i=0;i<ar.length;i++){
rtn+="<double>"+ar[i]+"</double>";
}
rtn+="</array>";
}
if(rxp.getType()==REXP.XT_STR){
rtn+="<string>"+rxp.asString()+"</string>";
}
//if(rxp.getType()==REXP.XT_STR || rxp.getType()==REXP.XT_VECTOR || rxp.getType()==REXP.XT_DOUBLE || rxp.getType()==REXP.XT_ARRAY_DOUBLE){}
//else log.debug("rxp.getType():"+rxp.getType());
return rtn;
}
private String parseJSON(REXP rxp, String rtn) throws Exception {
if(rxp.getType()==REXP.XT_VECTOR ){
RVector rv=rxp.asVector();
for(Iterator i=rv.iterator();i.hasNext();){
Object obj=i.next();
if(obj instanceof REXP){
//log.debug("~~~~~Parsing.......obj:"+obj);
rtn+=parseJSON((REXP)obj, "");
}
}
}
JSONArray arr=new JSONArray();
if(rxp.getType()==REXP.XT_DOUBLE){
arr.put(rxp.asDouble());
}
if(rxp.getType()==REXP.XT_ARRAY_DOUBLE){
double[] ar=rxp.asDoubleArray();
for(int i=0;i<ar.length;i++){
arr.put(ar[i]);
}
}
if(rxp.getType()==REXP.XT_STR){
//rtn+="<string>"+rxp.asString()+"</string>";
arr.put(rxp.asString());
}
//if(rxp.getType()==REXP.XT_STR || rxp.getType()==REXP.XT_VECTOR || rxp.getType()==REXP.XT_DOUBLE || rxp.getType()==REXP.XT_ARRAY_DOUBLE){}
//else log.debug("rxp.getType():"+rxp.getType());
return arr.toString();
}
/*
private String parseVector(REXP x1,Object rtnobj) throws Exception {
if(x1.getType()==REXP.XT_VECTOR){
}
return null;
}
*/
private static JCS cache=null;
private static JCS getCache() throws Exception {
if(cache==null){
cache=JCS.getInstance(ExecuteScript.class.getName());
}
return cache;
}
public void callBack(Map data) {
String uids=(String)data.get("running_uids");
String peer=getMsgCreator();
String ky=peer+"_delay";
try{
if(getCache().get(ky)==null){
log.debug("<<--------"+getUid());
if(uids!=null && !uids.equals("")){
ArrayList list_uids=new ArrayList();
StringTokenizer st=new StringTokenizer(uids,"|");
while(st.hasMoreTokens()){
list_uids.add(st.nextToken());
}
//ArrayList<RScript> peer_s=new ArrayList<RScript>();
Collection<RScript> queue=LoadBalancingQueue.getExecuteRScriptDefault().getScriptProcessingQueue();
for(Iterator<RScript> it=queue.iterator();it.hasNext();){
RScript rs=it.next();
if(rs.getPeer().equals(peer)){
long waiting=new Date().getTime()-rs.getStartedtime().getTime();
//if more than 3 minutes
if(waiting>(1000*60*1) && !list_uids.contains(rs.getUid())){
log.debug("removing task uid:"+rs.getUid());
rs.setError("Removing because it is not running on peer");
String result="";
LoadBalancingQueue.getExecuteRScriptDefault().scriptFinished(rs,result,ScheduledTask.EXCECUTION_FAIL);
}
//peer_s.add(rs);
}
}
IElementAttributes att= getCache().getDefaultElementAttributes();
att.setMaxLifeSeconds(60);
getCache().put(ky,"delay",att);
}
}
}catch(Exception e){
e.printStackTrace();
}
}
@Override
public void onCallBackSendingFailed() {
// TODO Auto-generated method stub
}
}
@@ -0,0 +1,102 @@
/******************************************************************************
*
* Copyright: Intellectual Property of Four Elements Capital Pte Ltd, Singapore.
* All rights reserved.
*
******************************************************************************/
package com.fourelementscapital.scheduler.p2p.msg.impl;
import java.util.Map;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import com.fourelementscapital.scheduler.balance.LoadBalancingQueue;
import com.fourelementscapital.scheduler.p2p.msg.MessageHandler;
import com.fourelementscapital.scheduler.rscript.RScript;
public class ScriptFinished extends MessageHandler{
private String status;
private String result;
private String uid;
private String error;
private String jsonresult;
public String getJsonresult() {
return jsonresult;
}
public void setJsonresult(String jsonresult) {
this.jsonresult = jsonresult;
}
public String getError() {
return error;
}
public void setError(String error) {
this.error = error;
}
private Logger log = LogManager.getLogger(ScriptFinished.class.getName());
public String getUid() {
return uid;
}
public void setUid(String uid) {
this.uid = uid;
}
public Map executeAtDestination() {
//log.debug("received signal from peer, script finished,");
log.debug("status:"+status);
log.debug("result:"+result);
log.debug("error:"+error);
RScript rs=new ExecuteScript();
rs.setUid(this.getUid());
rs.setError(this.getError());
rs.setResultJSON(this.jsonresult);
LoadBalancingQueue.getExecuteRScriptDefault().scriptFinished(rs, this.result,this.status);
return null;
}
public String getStatus() {
return status;
}
public void setStatus(String status) {
this.status = status;
}
public String getResult() {
return result;
}
public void setResult(String result) {
this.result = result;
}
@Override
public void onSendingFailed() {
log.error("sending failed, uid:"+this.uid);
}
}
@@ -0,0 +1,174 @@
/******************************************************************************
*
* Copyright: Intellectual Property of Four Elements Capital Pte Ltd, Singapore.
* All rights reserved.
*
******************************************************************************/
package com.fourelementscapital.scheduler.p2p.msg.impl;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.Semaphore;
import java.util.concurrent.TimeUnit;
import org.apache.jcs.JCS;
import org.apache.jcs.engine.behavior.IElementAttributes;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import com.fourelementscapital.scheduler.balance.LoadBalancingQueue;
import com.fourelementscapital.scheduler.p2p.msg.MessageHandler;
import com.fourelementscapital.scheduler.p2p.msg.PostCallBack;
import com.fourelementscapital.scheduler.p2p.msg.PostMessage;
import com.fourelementscapital.scheduler.peer.QueueFactory;
import com.fourelementscapital.scheduler.rscript.RScript;
public class TenderScript extends MessageHandler implements PostCallBack {
private String uid;
private String taskuid;
protected JCS cache=null;
private String requestid=null;
private Logger log = LogManager.getLogger(TenderScript.class.getName());
private static long TIMEOUT_MS=2000;
private static Semaphore lock=new Semaphore(1,true);
public TenderScript(){
this.requestid=new Date().getTime()+"-"+Thread.currentThread().getName();
}
public String getRequestid() {
return requestid;
}
public void setRequestid(String requestid) {
this.requestid = requestid;
}
public String getUid() {
return uid;
}
public void setUid(String uid) {
this.uid = uid;
}
public String getTaskuid() {
return taskuid;
}
public void setTaskuid(String taskuid) {
this.taskuid = taskuid;
}
public Map executeAtDestination() {
HashMap h=new HashMap();
int p=0;
try{
acquireLock();
log.debug("getTaskuid()"+getTaskuid());
log.debug("getQueue():"+new QueueFactory().getQueue(getTaskuid()));
log.debug("Excecuting threads:"+new QueueFactory().getQueue(getTaskuid()).getExecutingStacks());
log.debug("taskuid:"+getTaskuid());
log.debug("getUid():"+getUid());
log.debug("getCache():"+getCache());
p=1;
Long last=(Long)getCache().get(getUid());
p=2;
if(last==null || (new Date().getTime()-last)>100){
p=3;
if(new QueueFactory().getQueue(getTaskuid()).isRoomForThread()){
p=4;
h.put("okStart", getUid());
p=5;
IElementAttributes att= getCache().getDefaultElementAttributes();
att.setMaxLifeSeconds(1);
p=6;
getCache().put(getUid(), new Date().getTime(), att);
p=7;
}
}else{
h.put(IGNORE_CALLBACK, "");
}
}catch(Exception e){
e.printStackTrace();
log.error("Error executeAtDestination(), E:"+e.getMessage()+" p:"+p);
}finally{
releaseLock();
}
return h;
}
public synchronized void callBack(Map data) {
log.debug("call back tender script:");
log.debug("data:"+data+" okStart:"+data.get("okStart"));
if(data.get("okStart")!=null){
//log.debug("this.getUid():"+this.getUid());
RScript rs=new ExecuteScript();
rs.setUid(this.getUid());
RScript started=LoadBalancingQueue.getExecuteRScriptDefault().startScriptIfNotStarted(rs,getMsgCreator());
//log.debug("started:"+started);
if(started!=null){
//log.debug("received:"+data);
ExecuteScript es=new ExecuteScript();
es.setUid(started.getUid());
es.setTaskuid(this.getTaskuid());
es.setScript(started.getScript());
new PostMessage(es,getMsgCreator()).send();
}
rs=null;
started=null;
}
}
protected JCS getCache() throws Exception {
if(cache==null){
cache=JCS.getInstance("TenderScript");
log.debug("cache:"+cache);
}
log.debug("cache1:"+cache);
return cache;
}
@Override
public void onCallBackSendingFailed() {
// TODO Auto-generated method stub
log.error("postcallback failed, uid:"+this.uid);
}
@Override
public void onSendingFailed() {
// TODO Auto-generated method stub
log.error("Sending failed.. script uid:"+this.uid);
}
private void acquireLock() throws Exception{
TenderScript.lock.tryAcquire(TIMEOUT_MS, TimeUnit.MILLISECONDS);
}
private void releaseLock(){
TenderScript.lock.release();
}
}
@@ -0,0 +1,139 @@
/******************************************************************************
*
* Copyright: Intellectual Property of Four Elements Capital Pte Ltd, Singapore.
* All rights reserved.
*
******************************************************************************/
package com.fourelementscapital.scheduler.p2p.msg.scheduler;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.TimeZone;
import java.util.UUID;
import java.util.Vector;
import org.apache.jcs.JCS;
import org.apache.jcs.engine.behavior.IElementAttributes;
import com.fourelementscapital.scheduler.p2p.listener.OutgoingMessageCallBack;
import com.fourelementscapital.scheduler.p2p.msg.MessageHandler;
import com.fourelementscapital.scheduler.p2p.msg.PostCallBack;
import com.fourelementscapital.scheduler.p2p.msg.PostMessage;
import com.fourelementscapital.scheduler.peer.QueueFactory;
public class InstantPeerStatus extends MessageHandler implements PostCallBack {
private String uid=null;
public String getUid() {
return uid;
}
public void setUid(String uid) {
this.uid = uid;
}
public int getPriority() {
return OutgoingMessageCallBack.PRIORITY_LOW;
}
public Map getStatus(String clientname) throws Exception {
InstantPeerStatus ins=new InstantPeerStatus();
ins.setUid(UUID.randomUUID().toString());
new PostMessage(ins,clientname).send();
int count=0;
Map data=null;
while(count < 20 && data==null){
data=(Map)getCache().get(ins.getUid());
try{Thread.sleep(100);}catch(Exception e){}
count++;
}
//System.out.println("<><><><><><><><>retrived:"+ins.getUid());
return data;
}
public Map executeAtDestination() {
Map h=new HashMap();
QueueFactory qfactory=new QueueFactory();
SimpleDateFormat sdf=new SimpleDateFormat("HH:mm:ss");
sdf.setTimeZone(TimeZone.getTimeZone("GMT"));
Vector v= qfactory.getExecutingTasksData();
for(Iterator i=v.iterator();i.hasNext();){
Map row=(Map)i.next();
if(row.get(QueueFactory.KEY_SCHEDULER_ID)!=null){
Long st=(Long)row.get(QueueFactory.KEY_STARTED_TIME );
Long cu=(Long)row.get(QueueFactory.KEY_CURRENT_TIME );
long dur=cu-st;
Date d=new Date(dur);
String rd="Task:"+row.get(QueueFactory.KEY_TASK_NAME)+" ("+row.get(QueueFactory.KEY_SCHEDULER_ID)+") Running:"+sdf.format(d);
h.put(row.get(QueueFactory.KEY_SCHEDULER_ID),rd);
}
}
//System.out.println("@@@@@@@@@@@@@@@@ -------->Data collected at client: data: "+h+" ins-id:"+getUid());
return h;
}
public void callBack(Map data) {
//System.out.println("<-----@@@@@@@ Data received at server:uid:"+getUid()+" data: "+data);
try{
String uid=getUid();
IElementAttributes att= getCache().getDefaultElementAttributes();
att.setMaxLifeSeconds(20);
getCache().put(uid, data);
}catch(Exception e){
e.printStackTrace();
}
}
private static JCS cache=null;
private static JCS getCache() throws Exception {
if(cache==null){
cache=JCS.getInstance(InstantPeerStatus.class.getName());
}
return cache;
}
@Override
public void onCallBackSendingFailed() {
// TODO Auto-generated method stub
}
@Override
public void onSendingFailed() {
// TODO Auto-generated method stub
}
}
@@ -0,0 +1,255 @@
/******************************************************************************
*
* Copyright: Intellectual Property of Four Elements Capital Pte Ltd, Singapore.
* All rights reserved.
*
******************************************************************************/
package com.fourelementscapital.scheduler.p2p.msg.scheduler;
import java.util.Map;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import com.fourelementscapital.db.SchedulerDB;
import com.fourelementscapital.scheduler.SchedulerEngine;
import com.fourelementscapital.scheduler.alarm.SchedulerAlarm;
import com.fourelementscapital.scheduler.alarm.SchedulerAlarmVO;
import com.fourelementscapital.scheduler.balance.LoadBalancingQueue;
import com.fourelementscapital.scheduler.engines.ScheduledTask;
import com.fourelementscapital.scheduler.engines.SchedulerExePlanLogs;
import com.fourelementscapital.scheduler.exception.ExceptionRServeUnixFailure;
import com.fourelementscapital.scheduler.exception.ExceptionRServeWindowsFailure;
import com.fourelementscapital.scheduler.exception.ExceptionSchedulerTeamRelated;
import com.fourelementscapital.scheduler.exception.ExceptionWarningNoFullData;
import com.fourelementscapital.scheduler.p2p.P2PService;
import com.fourelementscapital.scheduler.p2p.listener.IncomingMessage;
import com.fourelementscapital.scheduler.p2p.listener.P2PPipeLog;
import com.fourelementscapital.scheduler.p2p.msg.ExceptionMessageHandler;
public class PeerFinishedTask extends ExceptionMessageHandler {
private String scheduler_id;
private String trigger_time;
private String taskuid;
private String status;
public String getStatus() {
return status;
}
public void setStatus(String status) {
this.status = status;
}
private Logger log = LogManager.getLogger(PeerFinishedTask.class.getName());
public String getTaskuid() {
return taskuid;
}
public void setTaskuid(String taskuid) {
this.taskuid = taskuid;
}
public String getScheduler_id() {
return scheduler_id;
}
public void setScheduler_id(String scheduler_id) {
this.scheduler_id = scheduler_id;
}
public String getTrigger_time() {
return trigger_time;
}
public void setTrigger_time(String trigger_time) {
this.trigger_time = trigger_time;
}
/**
* will be executed on server side.
*/
public Map executeAtDestination() {
log.debug("finished:"+getScheduler_id()+" tri_time:"+getTrigger_time()+" from :"+getMsgCreator());
log.debug("===========>PeerFinishedTask receiving message with Exception "+exception());
if(exception()!=null){
log.debug(" ===========>Message "+exception().getMessage());
}
P2PPipeLog.receiveMsg("Task Execution Finished:"+"Schduler ID:"+getScheduler_id()+" Trigger Time:"+getTrigger_time(),getMsgCreator()+" Status:"+getStatus());
//Debugger.addDebugMsg("Msg from peer "+getMsgCreator()+ " sc_id:"+ getScheduler_id()+" tr_time: "+getTrigger_time(),getMsgCreator()+" "+ getScheduler_id()+" "+getTrigger_time());
int sc_id=0; try{sc_id=Integer.parseInt(getScheduler_id());}catch(Exception e){}
long tri_time=0; try{tri_time=Long.parseLong(getTrigger_time());}catch(Exception e){}
PeerCacheLock.releasePeer(getMsgCreator(),getTaskuid());
LoadBalancingQueue.getDefault().releasePeersCache4PriorityGr();
if( (getExceptionClass()!=null && getExceptionClass().equals(ExceptionRServeUnixFailure.class.getName()) ) || (getExceptionClass()!=null && getExceptionClass().equals(ExceptionRServeWindowsFailure.class.getName()) ) ){
//re-scheduled...
SchedulerDB sdb=SchedulerDB.getSchedulerDB();
try{
sdb.connectDB();
LoadBalancingQueue.getDefault().executionFailed(sc_id,tri_time, getMsgCreator());
LoadBalancingQueue.getDefault().releasePeersCache4PriorityGr();
LoadBalancingQueue.getDefault().peerStarted(sc_id,tri_time, getMsgCreator());
sdb.updateQueueLogStatus(sc_id,tri_time,null, P2PService.getComputerName()); //over-ridding failed long with null message;
}catch(Exception e) {
log.error("error while saving peerstarted Error:"+e.getMessage()+" scd_id:"+sc_id+" trig_time:"+tri_time);
}finally{
try {sdb.closeDB(); } catch (Exception e) {log.error("Error while closing sdb connection, error:"+e.getMessage()); }
}
} else{
if(sc_id>=0){
new SchedulerExePlanLogs(sc_id,tri_time).log("Server rcvd completed signal, Status:"+getStatus(),SchedulerExePlanLogs.SERVER_OK_RECEIVED_STATUS_FROM_PEER);
LoadBalancingQueue.getDefault().executionEnded(sc_id,tri_time);
//IncomingMessage.updateFinishedPeersTime(this.mbean.getSender(),id,trg_time);
IncomingMessage.updateFinishedPeersTime(getMsgCreator(),sc_id,tri_time);
SchedulerDB sdb=SchedulerDB.getSchedulerDB();
try{
sdb.connectDB();
try{
if(exception()!=null ){
sdb.updateResponseCode(sc_id, tri_time, exception().getErrorcode());
}
}catch(Exception e){
log.error("Couldn't get Scheduler Exception, Error:"+e.getMessage());
}
if(getStatus()!=null && !getStatus().equals("")){
//this tries 5 times with interval of 500 milliseconds;
for(int i=0;i<5;i++){
try{
sdb.updateQueueNullStatus(sc_id, tri_time, getStatus());
i=100;
}catch(Exception e){
log.error("Error:"+e.getMessage()+" trying "+i);
Thread.sleep(500);
//sdb.updateQueueNullStatus(sc_id, tri_time, getStatus());
}
}
}
//IElementAttributes att= cache.getDefaultElementAttributes();
//att.setMaxLifeSeconds(1);
//cache.put("just_finished_id_"+id,"yes",att);
String errorLog=sdb.getErrorMessageEvenNull(sc_id,tri_time);
if(errorLog==null && getExceptionMessage()!=null){
if(getExceptionClass().equalsIgnoreCase(ExceptionWarningNoFullData.class.getName())){
}else{
errorLog=getExceptionMessage();
}
}else if(errorLog!=null && getExceptionMessage()!=null){
errorLog+="\n "+getExceptionMessage();
}
log.debug("~~~~ Server rcvd completed signal:"+errorLog+" sc_id:"+sc_id+" tri_time:"+tri_time);
if(errorLog!=null){
Map data=sdb.getScheduler(sc_id);
String type=(String)data.get("alert_type");
String name=(String)data.get("name");
if(type!=null && !type.equals("")){
// send alarm :
SchedulerAlarmVO vo = new SchedulerAlarmVO();
vo.setAlarmType(type);
vo.setName(name);
vo.setSubject(SchedulerAlarm.ALARM_SUB_FAILED);
vo.setMessage(errorLog);
vo.setFrom(getMsgCreator());
vo.setErrCode(exception().getErrorcode());
vo.setExceptionSchedulerTeamRelated(exception()!=null && exception() instanceof ExceptionSchedulerTeamRelated);
vo.setComputerName(P2PService.getComputerName());
vo.setConsoleMsg(sdb.getConsoleMsg(sc_id, tri_time));
vo.setExecLogs(sdb.getSchedulerExeLogs(sc_id, tri_time));
vo.setRepCodeExist(sdb.execLogsRepcodeExist(sc_id, tri_time, SchedulerExePlanLogs.SERVER_ERROR_ALARM_SENT));
vo.setThemeTags(sdb.getThemeTags(sc_id));
vo.setOwnerTheme(sdb.getOwnerTheme(sc_id));
vo.setQueueLog(sdb.getQueueLog(sc_id, tri_time));
vo.setPeerFriendlyName(sdb.getPeerFriendlyName(vo.getFrom()));
vo.setSchedulerId(sc_id);
vo.setTriggerTime(tri_time);
SchedulerAlarm.sendAlarm(vo);
new SchedulerExePlanLogs(sc_id, tri_time).log("Alarm sent",sdb,SchedulerExePlanLogs.SERVER_ERROR_ALARM_SENT);
}
}
Map<String,String> data=sdb.getTaskEventActions(sc_id, tri_time);
String statuskey="status";
if( data.containsKey(statuskey) && data.get(statuskey).equals(ScheduledTask.EXCECUTION_SUCCESS)
&& data.containsKey(ScheduledTask.FIELD_DEPENDENCY_SUCCESS) && data.get(ScheduledTask.FIELD_DEPENDENCY_SUCCESS)!=null
&& !data.get(ScheduledTask.FIELD_DEPENDENCY_SUCCESS).trim().equals("")
){
String expression=data.get(ScheduledTask.FIELD_DEPENDENCY_SUCCESS);
String suffi=ScheduledTask.TASK_EVENT_CALL_EXP_ID_VARIABLE+"="+sc_id+"\n";
suffi+=ScheduledTask.TASK_EVENT_CALL_EXP_TRIGGERTIME_VARIABLE+"="+tri_time+"\n";
new SchedulerEngine().executeScriptExpression(expression, "onSuccess of "+sc_id, suffi);
}
if( data.containsKey(statuskey) && data.get(statuskey).equals(ScheduledTask.EXCECUTION_FAIL)
&& data.containsKey(ScheduledTask.FIELD_DEPENDENCY_FAIL) && data.get(ScheduledTask.FIELD_DEPENDENCY_FAIL)!=null
&& !data.get(ScheduledTask.FIELD_DEPENDENCY_FAIL).trim().equals("")
){
String expression=data.get(ScheduledTask.FIELD_DEPENDENCY_FAIL);
String suffi=ScheduledTask.TASK_EVENT_CALL_EXP_ID_VARIABLE+"="+sc_id+"\n";
suffi+=ScheduledTask.TASK_EVENT_CALL_EXP_TRIGGERTIME_VARIABLE+"="+tri_time+"\n";
if(errorLog!=null){
suffi+=ScheduledTask.TASK_EVENT_CALL_EXP_ERRORMSG_VARIABLE+"=\""+tri_time+"\";\n";
}
new SchedulerEngine().executeScriptExpression(expression, "onFailure of "+sc_id, suffi);
}
}catch(Exception e){
e.printStackTrace();
//log.error("error s:"+e.getMessage());
}finally{try{sdb.closeDB();}catch(Exception e1){}}
}
}
return null;
}
@Override
public void onSendingFailed() {
// TODO Auto-generated method stub
}
}
@@ -0,0 +1,176 @@
/******************************************************************************
*
* Copyright: Intellectual Property of Four Elements Capital Pte Ltd, Singapore.
* All rights reserved.
*
******************************************************************************/
package com.fourelementscapital.scheduler.p2p.msg.scheduler;
import java.util.Collection;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import org.apache.jcs.JCS;
import org.apache.jcs.engine.behavior.IElementAttributes;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import com.fourelementscapital.db.SchedulerDB;
import com.fourelementscapital.scheduler.p2p.listener.IncomingMessage;
import com.fourelementscapital.scheduler.p2p.listener.OutgoingMessageCallBack;
import com.fourelementscapital.scheduler.p2p.msg.MessageHandler;
import com.fourelementscapital.scheduler.p2p.msg.PostCallBack;
import com.fourelementscapital.scheduler.p2p.peer.PeerMachine;
import com.fourelementscapital.scheduler.p2p.peer.PeerManagerHSQL;
import com.fourelementscapital.scheduler.peer.QueueFactory;
public class PeerOnlineStatus extends MessageHandler implements PostCallBack {
private String status=null;
private String peerversion=null;
public String getPeerversion() {
return peerversion;
}
public void setPeerversion(String peerversion) {
this.peerversion = peerversion;
}
public String getStatus() {
return status;
}
public void setStatus(String status) {
this.status = status;
}
private Logger log = LogManager.getLogger(PeerOnlineStatus.class.getName());
public int getPriority() {
return OutgoingMessageCallBack.PRIORITY_LOW;
}
/**
* Will be executed in peer side.
*/
public Map executeAtDestination() {
Map h=new HashMap();
QueueFactory qfactory=new QueueFactory();
setPeerversion(PeerMachine.getLastVersion());
if(qfactory.countExcTasksInPeer()>0){
//Number scheduler_id=(Number)ScheduledTaskQueue.getExecutingStackFrame().getData().get("id");
String ids="";
String times="";
h=qfactory.getExecutingIDAndSTimes();
setStatus("BUSY");
}else{
setStatus("NOBUSY");
}
return h;
}
public void callBack(Map data) {
String peer=getMsgCreator();
updatePeerStatus(data,peer);
log.debug("Message reccied from client:~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ peer "+peer+" ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~");
//remove finished ones from time, if no busy then removes from the timing collection.
}
public void updatePeerStatus(Map data, String peer) {
try{
if(getCache().get("peer_"+peer)==null){
IElementAttributes att= getCache().getDefaultElementAttributes();
att.setMaxLifeSeconds(30);
getCache().put("peer_"+peer,peer,att);
updateLastOnline(peer);
log.debug("----->caching peer:"+peer);
}
}catch(Exception e){
log.error("Error while caching peer online status");
}
IncomingMessage.peersUpdate(peer,getStatus());
new PeerManagerHSQL().updatePeerResponse(peer, data,getPeerversion());
}
private static JCS cache=null;
private static JCS getCache() throws Exception {
if(cache==null){
cache=JCS.getInstance("PeerOnlineStatus");
}
return cache;
}
public synchronized void updateLastOnline(String peername) {
SchedulerDB sdb=SchedulerDB.getSchedulerDB();
try{
sdb.connectDB();
sdb.updatePeersLastOnline(peername, new Date().getTime());
}catch(Exception e){
log.error("Error:"+e.getMessage());
}finally{
try{sdb.closeDB();}catch(Exception e1){}
}
}
public synchronized void removePeerThreadStatus(String peername) {
SchedulerDB sdb=SchedulerDB.getSchedulerDB();
try{
sdb.connectDB();
sdb.removeAllPeerThreadStatus(peername);
}catch(Exception e){
log.error("Error:"+e.getMessage());
}finally{
try{sdb.closeDB();}catch(Exception e1){}
}
}
public static Collection getOnlineRespondingPeers() throws Exception {
Map peers=getCache().getMatching("^[A-Za-z0-9_]+$");
return peers.values();
}
@Override
public void onCallBackSendingFailed() {
// TODO Auto-generated method stub
}
@Override
public void onSendingFailed() {
// TODO Auto-generated method stub
}
}
@@ -0,0 +1,302 @@
/******************************************************************************
*
* Copyright: Intellectual Property of Four Elements Capital Pte Ltd, Singapore.
* All rights reserved.
*
******************************************************************************/
package com.fourelementscapital.scheduler.p2p.msg.scheduler;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.Semaphore;
import java.util.concurrent.TimeUnit;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import com.fourelementscapital.db.SchedulerDB;
import com.fourelementscapital.scheduler.ScheduledTaskFactory;
import com.fourelementscapital.scheduler.balance.LoadBalancingQueue;
import com.fourelementscapital.scheduler.engines.ScheduledTask;
import com.fourelementscapital.scheduler.engines.SchedulerExePlanLogs;
import com.fourelementscapital.scheduler.engines.StackFrame;
import com.fourelementscapital.scheduler.engines.StackFrameCallBack;
import com.fourelementscapital.scheduler.error.ClientError;
import com.fourelementscapital.scheduler.exception.ExceptionPeerRejected;
import com.fourelementscapital.scheduler.exception.ExceptionPeerUnknown;
import com.fourelementscapital.scheduler.exception.SchedulerException;
import com.fourelementscapital.scheduler.group.RScriptScheduledTask;
import com.fourelementscapital.scheduler.p2p.listener.OutgoingMessageCallBack;
import com.fourelementscapital.scheduler.p2p.listener.P2PPipeLog;
import com.fourelementscapital.scheduler.p2p.msg.PostCallBack;
import com.fourelementscapital.scheduler.p2p.msg.PostMessage;
import com.fourelementscapital.scheduler.peer.QueueAbstract;
import com.fourelementscapital.scheduler.peer.QueueFactory;
public class SendTask extends TaskMessage implements PostCallBack {
private Logger log = LogManager.getLogger(SendTask.class.getName());
private String started_time;
private static String EXCEPTION_CLASS_NAME="exception_class";
private static String EXCEPTION_CLASS_MESSAGE="exception_message";
private static Semaphore slock=new Semaphore(1,true);
private static final long TIMEOUT_MS=1000;
public String getStarted_time() {
return started_time;
}
public void setStarted_time(String started_time) {
this.started_time = started_time;
}
private void acquireLock(){
try{
SendTask.slock.tryAcquire(TIMEOUT_MS, TimeUnit.MILLISECONDS);
//LoadBalancingHSQLLayerDB.dblock.acquire();
}catch(Exception e){
log.error("Error:"+e.getMessage());
}
}
private void releaseLock(){
try{
SendTask.slock.release();
//log.debug("....releasing lock: thread:"+Thread.currentThread().getId());
}catch(Exception e){
log.error("Error:"+e.getMessage());
}
}
/**
* this method will be executed at the peer side.
*/
public synchronized Map executeAtDestination() {
HashMap rtn=new HashMap();
int sc_id=0; try{sc_id=Integer.parseInt(getScheduler_id());}catch(Exception e){}
long tri_time=0; try{tri_time=Long.parseLong(getTrigger_time());}catch(Exception e){}
long ntri_time=0; try{ntri_time=Long.parseLong(getNext_trigger_time());}catch(Exception e){}
long started=0;try{started=Long.parseLong(getStarted_time());}catch(Exception e){}
if(sc_id>0 && tri_time>0){
new SchedulerExePlanLogs(sc_id,tri_time).log("peer.sendtask.received",new HashMap(),SchedulerExePlanLogs.PEER_OK_PEER_RECEVED_TASK);
}
log.debug("got task: sc_id:"+sc_id+" tri_time:"+tri_time+" uid:"+getTaskuid());
QueueFactory qfactory=new QueueFactory();
QueueAbstract qa=qfactory.getQueue(getTaskuid());
SchedulerException se=null;
//String specific_excec_log=null;
try{
acquireLock();
if(!qa.isRoomForThread()){
throw new ExceptionPeerRejected(null);
}else{
if(sc_id>=0){
String replyTo=getMsgCreator();
executeTask(sc_id,tri_time,ntri_time,started,replyTo);
rtn.putAll( qfactory.getExecutingIDAndSTimes());
}else{
throw new ExceptionPeerUnknown("sc_id: not found");
}
}
releaseLock();
}catch(ExceptionPeerRejected epr){
se=epr;
rtn.put("failed", "YES");
if(sc_id>0 && tri_time>0){
new SchedulerExePlanLogs(sc_id,tri_time).log("peer.sendtask.noroom2execute",new HashMap(),SchedulerExePlanLogs.PEER_WARNING_NOROOM_TO_EXEC);
}
}catch(SchedulerException se1){
se=se1;
rtn.put("failed", "YES");
if(sc_id>0 && tri_time>0){
HashMap h=new HashMap(); h.put("error_message", se1.getMessage());
new SchedulerExePlanLogs(sc_id,tri_time).log("peer.sendtask.unknown",h,SchedulerExePlanLogs.PEER_ERROR_WHILE_RECEIVINGTASK);
}
}catch(Exception e){
e.printStackTrace();
//no specic error at this point
new SchedulerExePlanLogs(sc_id,tri_time).log("Error caught at SendTask.executeAtDestination(), Error:"+e.getMessage(),SchedulerExePlanLogs.PEER_ERROR_WHILE_RECEIVINGTASK);
}finally{
if(se!=null){
exception(se);
setPriority(OutgoingMessageCallBack.PRIORITY_VERY_VERY_HIGH);
rtn.put(EXCEPTION_CLASS_NAME, se.getExceptionclass());
rtn.put(EXCEPTION_CLASS_MESSAGE, se.getMessage());
}
}
return rtn;
}
public synchronized void callBack(Map data) {
log.debug("****SendTask**** callback");
String peer=getMsgCreator();
if(data!=null && data.get("failed")!=null && ((String)data.get("failed")).equalsIgnoreCase("YES") ){
int sc_id=0; try{sc_id=Integer.parseInt(getScheduler_id());}catch(Exception e){}
long tri_time=0; try{tri_time=Long.parseLong(getTrigger_time());}catch(Exception e){}
P2PPipeLog.receiveMsg("Received failed Msg:"+"Schduler ID:"+getScheduler_id()+" Trigger Time:"+getTrigger_time(),peer);
if(sc_id>0 && tri_time>0){
new SchedulerExePlanLogs(sc_id,tri_time).log("Sent task bounced back at the server, looking for another peer",SchedulerExePlanLogs.SERVER_WARNING_BOUNCEDTASK_FROMPEER);
}
if(sc_id>0){
LoadBalancingQueue.getDefault().executionFailed(sc_id,tri_time, peer);
LoadBalancingQueue.getDefault().releasePeersCache4PriorityGr();
}
}else{
if(data.size()>0){
int sc_id=0; try{sc_id=Integer.parseInt(getScheduler_id());}catch(Exception e){}
long tri_time=0; try{tri_time=Long.parseLong(getTrigger_time());}catch(Exception e){}
P2PPipeLog.receiveMsg("Task Execution Started confirmation Schduler ID:"+getScheduler_id()+" Trigger Time:"+getTrigger_time(),peer);
log.debug("peer queue data received");
PeerOnlineStatus pos=new PeerOnlineStatus();
pos.setStatus("BUSY");
pos.updatePeerStatus(data,peer);
if(sc_id>0 && tri_time>0){
try{
LoadBalancingQueue.getDefault().peerStarted(sc_id,tri_time, peer);
new SchedulerExePlanLogs(sc_id,tri_time).log("Peer accepted task and responded server ",SchedulerExePlanLogs.SERVER_OK_PEER_ACCEPTED_TASK);
}catch(Exception e){
log.error("callBack(), Error:"+e.getMessage());
}
}
}
}
}
/**
* This will be executed on peer side.
*
*
* @param scheduler_id
* @param trig_time
* @param ntrig_time
* @param startedtime
* @param replyTo
* @throws Exception
*/
private synchronized void executeTask(int scheduler_id,long trig_time,long ntrig_time,long startedtime, String replyTo) throws Exception,SchedulerException,ExceptionPeerRejected {
SchedulerDB sdb=null;
try{
sdb=SchedulerDB.getSchedulerDB();
Map data=new HashMap();
try{
sdb.connectDB();
data=sdb.getScheduler(scheduler_id);
}catch(Exception e){
throw new ExceptionPeerRejected("Error while accessing database, Error:"+e.getMessage());
}
String taskuid=(String)data.get("taskuid");
//the following block where the code injection takes place.
if(data.get("rscript")!=null){
String scd_trig=scheduler_id+"_"+trig_time;
String code="";
try{
code=sdb.getInjectCode4QLog(scd_trig);
}catch(Exception e){
throw new ExceptionPeerRejected("Error while accessing database, Error:"+e.getMessage());
}
//String code=sdb.getInjectCode4QLog(scd_trig);
String param=(String)data.get("rscript_param");
String script=(String)data.get("rscript");
String newcode= RScriptScheduledTask.codeInjectConcatenate(param,script,code);
data.put("rscript", newcode);
}
//ScheduledTask task=new ScheduledTaskFactory().getTask(taskuid);
//added
ScheduledTask task=new ScheduledTaskFactory().getTaskFromAll(taskuid);
StackFrame sf=new StackFrame(task,data);
sf.setTrigger_time(trig_time);
sf.setNexttrigger_time(ntrig_time);
//sf.setMbean(this.mbean);
sf.setReplyTo(replyTo);
sf.setStarted_time(startedtime);
if(data.get("rscript")!=null) sf.setExecuted_code((String)data.get("rscript"));
sf.addCallBack(new StackFrameCallBack(){
public void callBack(StackFrame sf,String status,SchedulerException se){
Number nid=(Number)sf.getData().get("id");
new SchedulerExePlanLogs(nid.intValue(),sf.getTrigger_time()).log("Task completed, Reply server, STATUS:"+status,SchedulerExePlanLogs.PEER_OK_RESPOND_TASKCOMPLETED_WITHSTATUS);
PeerFinishedTask pft=new PeerFinishedTask();
pft.exception(se);
//added to see if it sending task for sure....
pft.setPriority(OutgoingMessageCallBack.PRIORITY_VERY_VERY_HIGH);
pft.setScheduler_id(getScheduler_id());
pft.setTrigger_time(getTrigger_time());
pft.setTaskuid(getTaskuid());
pft.setStatus(status);
log.debug("reply to :"+sf.getReplyTo());
new PostMessage(pft,sf.getReplyTo()).send();
}
});
//boolean rtn=false;
QueueAbstract qa=new QueueFactory().getQueue(taskuid);
if(qa.isRoomForThread()){
qa.addExThread(sf);
//rtn=true;
}else{
throw new ExceptionPeerRejected(null);
}
//return rtn;
}catch(ExceptionPeerRejected rjse){
ClientError.reportError(rjse, "ExceptionPeerRejected..");
throw rjse;
}catch(SchedulerException se){
ClientError.reportError(se, "SchedulerException..");
throw se;
}catch(Exception e){
ClientError.reportError(e, "Exception");
throw e;
}finally{
if(sdb!=null)sdb.closeDB();
}
}
@Override
public void onCallBackSendingFailed() {
// TODO Auto-generated method stub
}
@Override
public void onSendingFailed() {
// TODO Auto-generated method stub
}
}
@@ -0,0 +1,192 @@
/******************************************************************************
*
* Copyright: Intellectual Property of Four Elements Capital Pte Ltd, Singapore.
* All rights reserved.
*
******************************************************************************/
package com.fourelementscapital.scheduler.p2p.msg.scheduler;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.StringTokenizer;
import net.jxta.pipe.OutputPipeEvent;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import com.fourelementscapital.scheduler.ScheduledTaskFactory;
import com.fourelementscapital.scheduler.balance.LoadBalancingQueue;
import com.fourelementscapital.scheduler.engines.ScheduledTask;
import com.fourelementscapital.scheduler.engines.SchedulerExePlanLogs;
import com.fourelementscapital.scheduler.group.RServeUnixTask;
import com.fourelementscapital.scheduler.p2p.MessageBean;
import com.fourelementscapital.scheduler.p2p.listener.IncomingMessage;
import com.fourelementscapital.scheduler.p2p.listener.OutgoingMessageCallBack;
import com.fourelementscapital.scheduler.p2p.listener.P2PPipeLog;
import com.fourelementscapital.scheduler.p2p.msg.PostCallBack;
import com.fourelementscapital.scheduler.p2p.msg.PostMessage;
import com.fourelementscapital.scheduler.peer.QueueFactory;
public class TenderSchedulerTask extends TaskMessage implements PostCallBack {
private Logger log = LogManager.getLogger(TenderSchedulerTask.class.getName());
public synchronized Map executeAtDestination() {
log.debug("task request:"+getScheduler_id()+" tri_time:"+getTrigger_time()+" task uid:"+getTaskuid());
HashMap map=new HashMap();
if(new QueueFactory().getQueue(getTaskuid()).isRoomForThread()){ //&& cache.get("resp2server_on_"+tuid)==null){
/*
OutgoingMessageCallBack omc=new OutgoingMessageCallBack(getTaskuid(),null){
public boolean validateBeforeSend(){
if(getTuid()!=null){
if(new QueueFactory().getQueue(getTuid()).isRoomForThread()){
return true;
}else{
return false;
}
}else{
return true;
}
}
};
*/
//respond(MessageBean.TYPE_REQUEST,"EXECUTETASK:"+id+":"+tri_time+":"+ntri_time+":"+tuid+":BID",this.mbean,omc);
map.put("startTask", "YES");
}else{
map.put(IGNORE_CALLBACK, "");
}
return map;
}
/**
* Run on server side
*/
public synchronized void callBack(Map retdata) {
log.debug("callback:"+getScheduler_id()+" tri_time:"+getTrigger_time()+" task uid:"+getTaskuid()+" returned data:"+retdata);
//Debugger.addDebugMsg("Task accepted frm peer "+getMsgCreator()+ " sc_id:"+ getScheduler_id()+" tr_time: "+getTrigger_time(),getMsgCreator()+ " "+ getScheduler_id()+" "+getTrigger_time()
// );
if(retdata!=null &&retdata.get("startTask")!=null && ((String)retdata.get("startTask")).equalsIgnoreCase("YES")){
int sc_id=0; try{sc_id=Integer.parseInt(getScheduler_id());}catch(Exception e){}
long tri_time=0; try{tri_time=Long.parseLong(getTrigger_time());}catch(Exception e){}
long ntri_time=0; try{ntri_time=Long.parseLong(getNext_trigger_time());}catch(Exception e){}
String peer=getMsgCreator();
if(sc_id>=0 && peer!=null && !peer.trim().equals("")){
//log.debug("LoadBalancingQueue.getDefault().isPeerBusyWithTask(peer,sc_id):"+LoadBalancingQueue.getDefault().isPeerBusyWithTask(peer,sc_id)+" PeerCacheLock.lockPeerIfFree(peer,getTaskuid()):"+PeerCacheLock.lockPeerIfFree(peer,getTaskuid()));
//try{
//}catch(Exception e){}
ScheduledTask task=new ScheduledTaskFactory().getTask(getTaskuid());
//RServeUnixTask can handle more than script in a time
LoadBalancingQueue lb=LoadBalancingQueue.getDefault();
boolean isPeerBusy=lb.isPeerBusyWithTask(peer,sc_id);
log.debug("isPeerBusy:"+isPeerBusy);
if( task instanceof RServeUnixTask || (!isPeerBusy && PeerCacheLock.lockPeerIfFree(peer,getTaskuid())) ){ //&& !isPeerCommCached(this.mbean.getSender(),tuid) ){
//noTender4Seconds(this.mbean.getSender(),tuid);
//lockPeer(this.mbean.getSender());
int status=LoadBalancingQueue.getDefault().startedIfNotStarted(sc_id,tri_time,peer);
log.debug("status:"+status+" scd_id:"+sc_id+" tri_time:"+tri_time);
if(status==1){
HashMap data=new HashMap();
try{
data.put("scheduler_id", sc_id);
data.put("trigger_time", tri_time);
}catch(Exception e){
}
OutgoingMessageCallBack omc=new OutgoingMessageCallBack(getTaskuid(),data){
public void onFail(OutputPipeEvent pipe, MessageBean mbean, String destination){
StringTokenizer st=new StringTokenizer(mbean.getCommand(),":");
int id=0;
if(st.countTokens()>=5){
st.nextToken();
String sid=st.nextToken();
try{id=Integer.parseInt(sid);}catch(Exception e){}
}
if(id>0){
//System.out.println("IncomingMessageParser.parse();, execution failed, added back to queue again");
Long trigger_time=(Long)getData().get("trigger_time");
LoadBalancingQueue.getDefault().executionFailed(id,trigger_time.longValue(),destination);
}
try{
PeerCacheLock.releasePeer(getDestination(),tuid);
}catch(Exception e){}
LoadBalancingQueue.getDefault().releasePeersCache4PriorityGr();
}
public void after(OutputPipeEvent event){
//releasePeer(getDestination());
LoadBalancingQueue.getDefault().releasePeersCache4PriorityGr();
Map data=getData();
if(data!=null && data.get("scheduler_id")!=null && data.get("trigger_time")!=null){
int scheduler_id=(Integer)data.get("scheduler_id");
long tigger_time=(Long)data.get("trigger_time");
IncomingMessage.updateExecutingPeersTime(getDestination(),"BUSY",scheduler_id,tigger_time);
new SchedulerExePlanLogs(scheduler_id,tigger_time).log("Server fixes peer "+getDestination()+" for execution",SchedulerExePlanLogs.SERVER_OK_FIXEDPEER);
P2PPipeLog.sendMsg("Fixing peer to run scheduler_id:"+scheduler_id+" trigger_time:"+tigger_time, getDestination());
}
}
};
omc.setPriority(OutgoingMessageCallBack.PRIORITY_VERY_HIGH);
//respond(MessageBean.TYPE_RESPONSE,"EXECUTETASK:"+id+":"+tri_time+":"+ntri_time+":"+new Date().getTime()+":"+tuid+":CONFIRM",this.mbean,omc);
SendTask st=new SendTask();
st.setScheduler_id(getScheduler_id());
st.setTrigger_time(getTrigger_time());
st.setNext_trigger_time(getNext_trigger_time());
st.setTaskuid(getTaskuid());
st.setStarted_time(new Date().getTime()+"");
new PostMessage(st,peer,omc).send();
}
} //task instanceof RServeUnixTask ||...
} //if(sc_id>=0 && peer!=null && !peer.trim().equals(""))
}
}
@Override
public void onCallBackSendingFailed() {
// TODO Auto-generated method stub
}
@Override
public void onSendingFailed() {
// TODO Auto-generated method stub
}
}
@@ -0,0 +1,158 @@
/******************************************************************************
*
* Copyright: Intellectual Property of Four Elements Capital Pte Ltd, Singapore.
* All rights reserved.
*
******************************************************************************/
package com.fourelementscapital.scheduler.p2p.msg.scheduler.rserve;
import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.rosuda.REngine.Rserve.RConnection;
import com.fourelementscapital.scheduler.p2p.msg.MessageHandler;
import com.fourelementscapital.scheduler.p2p.msg.PostCallBack;
import com.fourelementscapital.scheduler.rserve.RServeConnectionPool;
import com.fourelementscapital.scheduler.rserve.RServeSession;
public class RServeSessionQuery extends MessageHandler implements PostCallBack {
private String closeAll="";
private int kill_process=0;
public int getKill_process() {
return kill_process;
}
public void setKill_process(int kill_process) {
this.kill_process = kill_process;
}
public String getCloseAll() {
return closeAll;
}
public void setCloseAll(String closeAll) {
this.closeAll = closeAll;
}
@Override
public Map executeAtDestination() {
HashMap h=new HashMap();
List<RServeSession> all=RServeConnectionPool.getAllSessions();
ArrayList<RServeSession> remove_s=new ArrayList<RServeSession> ();
for(RServeSession rs: all){
if( this.closeAll!=null && this.closeAll.equalsIgnoreCase("yes")) {
try{
if(rs.getRconnection()!=null){
//System.out.println("RServeSessionQuery.exeuteAtDestination()....intrrupting..");
//rs.getThread().interrupt();
//System.out.println("RServeSessionQuery.exeuteAtDestination()....stopping..");
//rs.getThread().stop();
//System.out.println("RServeSessionQuery.exeuteAtDestination()....connection:..: isConnected:"+rs.getRconnection().isConnected());
//System.out.println("RServeSessionQuery.exeuteAtDestination()....connection:..: serverShutdown:");
rs.getRconnection().serverShutdown();
}else{
RConnection rc=rs.getRsession().attach();
//System.out.println("RServeSessionQuery.exeuteAtDestination()....connection:..: shutDown():");
rc.shutdown();
}
//System.out.println("RServeSessionQuery.exeuteAtDestination()....removing conn..");
RServeConnectionPool.remove(rs);
}catch(Exception e){
e.printStackTrace();
}
}else{
if(this.kill_process>0 && rs.getProcessid()>0 && this.kill_process==rs.getProcessid()){
int process_id=rs.getProcessid();
try{
//System.out.println("RServeSessionQuery.exeuteAtDestination() killing connection id:"+this.kill_process);
Process p=Runtime.getRuntime().exec("kill -9 " + process_id);
//Process p=Runtime.getRuntime().exec("ls -lht ");
rs.setKillmessage("This RServe connectivity killed by force, pid:"+process_id);
processOutput(p);
String detail="killing sc_id:"+rs.getScheduler_id()+" tri_id:"+rs.getTrigger_time() +" process_id:"+rs.getProcessid();
h.put(rs.getScheduler_id()+"_"+rs.getTrigger_time(), detail);
remove_s.add(rs);
}catch(Exception e){
e.printStackTrace();
}
}else{
SimpleDateFormat sdf=new SimpleDateFormat("dd-MMM HH:mm:ss");
String time=(rs.getStarted_time()>0) ? sdf.format(new Date(rs.getStarted_time())) :"";
String detail="Running sc_id:"+rs.getScheduler_id()+" tri_id:"+rs.getTrigger_time()+" started:"+time +" process_id:"+rs.getProcessid();
h.put(rs.getUid(), detail);
}
}
}
for(RServeSession rs:remove_s){
RServeConnectionPool.remove(rs);
}
//for(Iterator )
System.out.println("RServeSessionQuery.exeuteAtDestination()....");
return h;
}
private void processOutput(Process p) throws Exception {
InputStream inputStream = p.getInputStream();
InputStreamReader inputStreamReader = new InputStreamReader(inputStream);
BufferedReader bufferedReader = new BufferedReader(inputStreamReader);
String line;
while ((line = bufferedReader.readLine()) != null)
{
System.out.println("RServeSessionQuery. console_output:"+line);
}
}
@Override
public void callBack(Map data) {
System.out.println("RServeSessionQuery.callBack()....");
for(Object obj: data.values()){
System.out.println("----"+obj);
}
}
@Override
public void onCallBackSendingFailed() {
// TODO Auto-generated method stub
}
@Override
public void onSendingFailed() {
// TODO Auto-generated method stub
}
}
@@ -0,0 +1,151 @@
/******************************************************************************
*
* Copyright: Intellectual Property of Four Elements Capital Pte Ltd, Singapore.
* All rights reserved.
*
******************************************************************************/
package com.fourelementscapital.scheduler.p2p.msg.scheduler.rserve;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.StringTokenizer;
import org.apache.jcs.JCS;
import org.apache.jcs.engine.behavior.IElementAttributes;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.json.JSONObject;
import com.fourelementscapital.scheduler.p2p.msg.MessageHandler;
import com.fourelementscapital.scheduler.p2p.msg.PostCallBack;
import com.fourelementscapital.scheduler.rserve.RServeConnectionPool;
import com.fourelementscapital.scheduler.rserve.RServeSession;
public class RServeSessionStat extends MessageHandler implements PostCallBack {
private Logger log = LogManager.getLogger(RServeSessionStat.class.getName());
public Map executeAtDestination() {
HashMap h=new HashMap();
List<RServeSession> all=RServeConnectionPool.getAllSessions();
ArrayList<RServeSession> remove_s=new ArrayList<RServeSession> ();
for(RServeSession rs: all){
try {
int process_id=rs.getProcessid();
Process p=Runtime.getRuntime().exec("ps -p "+process_id+" -o %cpu,%mem --no-header");
String stat=processOutput(p);
StringTokenizer st=new StringTokenizer(stat," ");
JSONObject obj = new JSONObject();
if(st.countTokens()>=2){
obj.put("CPU", st.nextToken());
obj.put("Memeory", st.nextToken());
obj.put("scheduler_id", rs.getScheduler_id());
obj.put("trigger_time", rs.getTrigger_time());
obj.put("no_executions", rs.getNoexecutions());
obj.put("process_id", process_id);
obj.put("scriptname", rs.getScriptname());
obj.put("uid", rs.getUid());
h.put(rs.getUid(), obj.toString());
}else{
log.error("error, the process doesn't exisit and removing process id:"+process_id+" from connection pool");
RServeConnectionPool.remove(rs);
}
} catch (IOException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
}
return h;
}
private String processOutput(Process p) throws Exception {
InputStream inputStream = p.getInputStream();
InputStreamReader inputStreamReader = new InputStreamReader(inputStream);
BufferedReader bufferedReader = new BufferedReader(inputStreamReader);
String line;
int linecount=0;
String rtn="";
while ((line = bufferedReader.readLine()) != null)
{
rtn+=rtn.equals("")?line:"|"+line;
linecount++;
}
return rtn;
}
public void callBack(Map data) {
//System.out.println("RServeSessionQuery.callBack()....");
try{
IElementAttributes att= getCache().getDefaultElementAttributes();
att.setMaxLifeSeconds(5);
getCache().put("peer_"+this.getMsgCreator(),data,att);
}catch(Exception e){
log.error("error,e:"+e.getMessage());
}
// for(Object obj: data.values()){
// System.out.println("----"+obj+" msg creator:"+this.getMsgCreator()+" recipient:"+this.getMsgRecipient());
//}
}
public static Map getPeerCachedStat(String peername) throws Exception {
Map data=(Map)getCache().get("peer_"+peername);
return data;
}
private static JCS cache=null;
private static JCS getCache() throws Exception {
if(cache==null){
cache=JCS.getInstance(RServeSessionStat.class.getName());
}
return cache;
}
@Override
public void onCallBackSendingFailed() {
// TODO Auto-generated method stub
}
@Override
public void onSendingFailed() {
// TODO Auto-generated method stub
}
}
@@ -0,0 +1,86 @@
/******************************************************************************
*
* Copyright: Intellectual Property of Four Elements Capital Pte Ltd, Singapore.
* All rights reserved.
*
******************************************************************************/
package com.fourelementscapital.scheduler.p2p.websocket.cmd;
import java.util.Iterator;
import org.apache.commons.cli.CommandLine;
import org.apache.commons.cli.Options;
import com.fourelementscapital.scheduler.balance.LoadBalancingQueue;
import com.fourelementscapital.scheduler.balance.executeR.LoadBalancingNewExecuteRQueue;
import com.fourelementscapital.scheduler.p2p.websocket.CommandAbstract;
import com.fourelementscapital.scheduler.queue.QueueStack;
import com.fourelementscapital.scheduler.queue.QueueStackManager;
public class Queue extends CommandAbstract {
@Override
public String executeValidCommand(CommandLine cmd, String command) {
String result="";
if(cmd.hasOption("o")){
//result="Not implemented yet";
LoadBalancingQueue leq=LoadBalancingQueue.getExecuteRScriptDefault();
if(leq instanceof LoadBalancingNewExecuteRQueue){
LoadBalancingNewExecuteRQueue leqnew=(LoadBalancingNewExecuteRQueue)leq;
try{
int q_size=leqnew.getScriptQueue().size();
int p_size=leqnew.getScriptProcessingQueue().size();
int o_size=leqnew.getAllScriptObjectsSize();
result+="Queued :"+q_size+"\n";
result+="Processing :"+p_size+"\n";
result+="Total Objects in Mem :"+o_size+"\n";
}catch(Exception e){
result+=" Error, e:"+e.getMessage();
}
}else{
result+="Current queue implementation doesn't support this feature";
}
}else{
//result=showHelp();
try{
Iterator<QueueStack> qst= QueueStackManager.getAllQueueStacks().iterator();
while(qst.hasNext()){
QueueStack qs=qst.next();
result+=qs.getPeername()+":"+qs.getUid()+" -----> "+(qs.isRunning()?"Busy":"Idle")+" "+qs.getSupportedtaskuids()+"\n";
}
if(result.equals("")){
result="No queue from peers";
}
}catch(Exception e){
result="Error, "+e.getMessage();
}
}
return result;
}
@Override
public Options getOptions() {
Options options = new Options();
//options.addOption("s", "show", false, "Show online peers");
//options.addOption("p", "peer", true, "Show queue stack of server side - example: -q 4ecappcsg2");
options.addOption("o", "queue-objects", false, "Show number of queued and processing objects");
return options;
}
@Override
public String getHeader() {
return "Show queue stacks and objects in the different queue in server side";
}
@Override
public String getFooter() {
return "";
}
}
@@ -0,0 +1,192 @@
/******************************************************************************
*
* Copyright: Intellectual Property of Four Elements Capital Pte Ltd, Singapore.
* All rights reserved.
*
******************************************************************************/
package com.fourelementscapital.scheduler.peer;
import java.util.Vector;
import java.util.concurrent.ConcurrentLinkedQueue;
import java.util.concurrent.Semaphore;
import java.util.concurrent.TimeUnit;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import com.fourelementscapital.db.SchedulerDB;
import com.fourelementscapital.scheduler.engines.SchedulerExePlanLogs;
import com.fourelementscapital.scheduler.engines.StackFrame;
import com.fourelementscapital.scheduler.exception.ExceptionPeerRejected;
import com.fourelementscapital.scheduler.p2p.P2PService;
public abstract class QueueAbstract {
private long lastExcecutedTime;
private Logger log = LogManager.getLogger(QueueAbstract.class.getName());
private ConcurrentLinkedQueue threads=new ConcurrentLinkedQueue();
private static Semaphore queueCheckLock=new Semaphore(1,true);
private static final long TIMEOUT_MS=1000;
private void acquireLock(){
try{
QueueAbstract.queueCheckLock.tryAcquire(TIMEOUT_MS, TimeUnit.MILLISECONDS);
//LoadBalancingHSQLLayerDB.dblock.acquire();
}catch(Exception e){
log.error("Error:"+e.getMessage());
}
}
private void releaseLock(){
try{
QueueAbstract.queueCheckLock.release();
//log.debug("....releasing lock: thread:"+Thread.currentThread().getId());
}catch(Exception e){
log.error("Error:"+e.getMessage());
}
}
private String name=null;
public QueueAbstract(String name){
this.name=name;
}
public String getName(){
return this.name;
}
public abstract Vector getTaskUids();
public abstract int getConcurrentThreads();
protected void setLastExecutedTime(long date){
this.lastExcecutedTime=date;
}
public long getLastExecutedTime(){
return this.lastExcecutedTime;
}
public synchronized boolean isRoomForThread(){
acquireLock();
log.debug("====>> currently "+this.threads+" in queue and maximum in the Q can be "+this.getConcurrentThreads());
if(this.threads.size()<this.getConcurrentThreads() && !QueueFactory.restartRequested){
try{
//this is to make sure that the tasks are equality distributed on multi-thread running peers.
if(this.threads.size()>0){
releaseLock();
Thread.sleep(200*this.threads.size());
}else{
releaseLock();
}
}catch(Exception e){
log.error("Error while delaying thread");
}
return true;
}else{
releaseLock();
return false;
}
}
public Object[] getExecutingStacks() {
return this.threads.toArray();
}
public int getExecutingStacksSize() {
return this.threads.size();
}
public synchronized void addExThread(StackFrame frame) throws Exception {
acquireLock();
try{
if(this.threads.size()<this.getConcurrentThreads() && !QueueFactory.restartRequested){
Number nid=null;
if(frame.getData()!=null){
nid=(Number)frame.getData().get("id");
new SchedulerExePlanLogs(nid.intValue(),frame.getTrigger_time()).log("Task added into local queue at client side",SchedulerExePlanLogs.PEER_OK_ADDED_PEER_QUEUE);
}
this.threads.add(frame);
Thread thread=new Thread(new QueueExeThread(this,frame));
thread.start();
if(frame.getData()!=null){
SchedulerDB sdb=SchedulerDB.getSchedulerDB();
try{
sdb.connectDB();
sdb.addPeerThreadStatus(P2PService.getComputerName(), this.name, frame.getTask().getUniqueid(), this.getConcurrentThreads(), nid.intValue());
}catch(Exception e){ log.error("error:"+e.getMessage()); }finally{
try{sdb.closeDB();}catch(Exception e1){log.error("error:"+e1.getMessage()); }
}
}
}else{
throw new ExceptionPeerRejected(null);
}
}catch(Exception e){
log.error(e.getMessage());
throw e;
}finally{
releaseLock();
}
}
/**
* this updates the peer running status in the database.
*
* @param frame
*
*/
protected void finishedExec(StackFrame frame){
acquireLock();
try{
this.threads.remove(frame);
}catch(Exception e){
log.error(e.getMessage());
}finally{
releaseLock();
}
if(frame.getData()!=null){
SchedulerDB sdb=SchedulerDB.getSchedulerDB();
try{
Number nid=(Number)frame.getData().get("id");
sdb.connectDB();
sdb.removePeerThreadStatus(P2PService.getComputerName(),nid.intValue());
}catch(Exception e){ }finally{
try{sdb.closeDB();}catch(Exception e1){log.error("error:"+e1.getMessage()); }
if(QueueFactory.restartRequested){
//RestartTomcat.restartPeerLater();
}
}
}
}
}
@@ -0,0 +1,342 @@
/******************************************************************************
*
* Copyright: Intellectual Property of Four Elements Capital Pte Ltd, Singapore.
* All rights reserved.
*
******************************************************************************/
package com.fourelementscapital.scheduler.peer;
import java.util.Date;
import java.util.Iterator;
import java.util.Map;
import java.util.TreeMap;
import java.util.Vector;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import com.fourelementscapital.db.SchedulerDB;
import com.fourelementscapital.scheduler.engines.ScheduledTask;
import com.fourelementscapital.scheduler.engines.SchedulerExePlanLogs;
import com.fourelementscapital.scheduler.engines.StackFrame;
import com.fourelementscapital.scheduler.error.ClientError;
import com.fourelementscapital.scheduler.exception.ExceptionPeerUnknown;
import com.fourelementscapital.scheduler.exception.ExceptionWarningNoFullData;
import com.fourelementscapital.scheduler.exception.SchedulerException;
import com.fourelementscapital.scheduler.group.RScriptScheduledTask;
import com.fourelementscapital.scheduler.p2p.P2PService;
public class QueueExeThread implements Runnable {
private Logger log = LogManager.getLogger(QueueExeThread.class.getName());
private QueueAbstract qa;
private StackFrame frame;
private String status;
public QueueExeThread(QueueAbstract qa,StackFrame frame){
this.qa=qa;
this.frame=frame;
}
public void run() {
this.status=null;
try{
log.debug("run():"+this.frame.getRscript());
if(this.frame.getRscript()!=null){
runRScript();
}else{
runTask();
}
}catch(Exception e){
e.printStackTrace();
log.error("Error:"+e.getMessage());
}
}
private void runRScript() {
ScheduledTask task=this.frame.getTask();
try{
log.debug("just before executed task:"+task);
task.execute(this.frame);
log.debug("just after executed task");
this.status=ScheduledTask.EXCECUTION_SUCCESS;
this.qa.setLastExecutedTime(new Date().getTime());
}catch(Exception e){
log.error("error:::::"+e.getMessage());
e.printStackTrace();
//Number nid=(Number)this.frame.getData().get("id");
this.status=ScheduledTask.EXCECUTION_FAIL;
ClientError.reportError(e, null);
}finally{
if(this.frame.getCallBack()!=null){
log.debug("sframe call back");
try{
this.frame.getCallBack().callBack(this.frame, this.status,null);
}catch(Exception e){
ClientError.reportError(e, null);
}
}
this.qa.finishedExec(this.frame);
log.debug("thread finised....");
}
}
private void runTask() {
boolean scheduled_taskmode=true;
ScheduledTask task=this.frame.getTask();
if(this.frame.getData().get("id")==null){
scheduled_taskmode=false;
}
Date sdate=new Date();
Number nid=(Number)this.frame.getData().get("id");
SchedulerException se=null;
try{
if(this.frame.isDependencyfailed()){
this.status=ScheduledTask.EXCECUTION_FAIL;
Thread.sleep(100);
}else{
log.debug("just before executing task:data:"+this.frame.getData());
log.debug("task"+task);
try{
traceHostAndStart(sdate,this.frame.getData(),this.frame);
}catch(Exception e){
log.error("Error in updating queue log");
}
if(scheduled_taskmode){
new SchedulerExePlanLogs(nid.intValue(),frame.getTrigger_time()).log("Execution starting..",SchedulerExePlanLogs.PEER_OK_EXECUTION_STARTING);
task.execute(this.frame);
//new SchedulerExePlanLogs(nid.intValue(),frame.getTrigger_time()).log("Execution completed");
}else{
//new ExecuteRWindowsHighPriority("Adhoc Rscript","rscript").execute(this.frame);
//ignored because multiple layers
new RScriptScheduledTask("Adhoc Rscript","rscript").executeScript(this.frame);
}
log.debug("just after executed task");
this.status=ScheduledTask.EXCECUTION_SUCCESS;
this.qa.setLastExecutedTime(new Date().getTime());
}
}catch(ExceptionWarningNoFullData ewnd){
this.status=ScheduledTask.EXCECUTION_WARNING;
se=ewnd;
}catch(SchedulerException se1){
this.status=ScheduledTask.EXCECUTION_FAIL;
se=se1;
}catch(Exception e){
new SchedulerExePlanLogs(nid.intValue(),frame.getTrigger_time()).log("Execution failed: MSG:"+e.getMessage(),SchedulerExePlanLogs.PEER_ERROR_EXECUTION_FAILURE);
this.status=ScheduledTask.EXCECUTION_FAIL;
se=new ExceptionPeerUnknown("Error Occured at QueueExceThread:"+e.getMessage());
ClientError.reportError(e, null);
}finally{
try{
if(se!=null){
if(se instanceof ExceptionWarningNoFullData){
}else{
new SchedulerExePlanLogs(nid.intValue(),frame.getTrigger_time()).log(se.getMessage()+" Err Code:"+se.getErrorcode(),SchedulerExePlanLogs.PEER_ERROR_EXECUTION_COMPLETED_WITH_EXCEPTION);
}
}else{
new SchedulerExePlanLogs(nid.intValue(),frame.getTrigger_time()).log("Execution completed, Status:"+this.status,SchedulerExePlanLogs.PEER_OK_EXECUTION_COMPLETED_WITH_NOEXCEPTION);
}
log.debug("finally");
try{
if(scheduled_taskmode){
this.status=(this.frame.getStatus()!=null)?this.frame.getStatus() : this.status;
int logid=addLog(sdate,this.frame.getData(),this.status,this.frame);
this.frame.setLogid(logid);
}else{
addScriptLog(sdate,this.frame.getData(),this.status,this.frame);
}
}catch(Exception e){
//e.printStackTrace();
throw e;
}
}catch(Exception e){
ClientError.reportError(e, null);
}finally{
//this.qa.finishedExec(this.frame);
}
try{
if(this.frame.getCallBack()!=null){
log.debug("sframe call back");
try{
this.frame.getCallBack().callBack(this.frame, this.status,se);
}catch(Exception e){
ClientError.reportError(e, null);
}
}
}catch(Exception e){
ClientError.reportError(e, null);
}
this.qa.finishedExec(this.frame);
log.debug("thread finised....");
}
}
private void traceHostAndStart(Date start,Map data, StackFrame sframe) throws Exception {
SchedulerDB sdb=SchedulerDB.getSchedulerDB();
sdb.connectDB();
Number nid=(Number)data.get("id");
sdb.updateHostAndStarted(nid.intValue(), sframe.getTrigger_time(), start, P2PService.getComputerName());
sdb.closeDB();
}
private int addLog(Date start, Map data, String status,StackFrame sframe) throws Exception {
SchedulerDB sdb=SchedulerDB.getSchedulerDB();
sdb.connectDB();
//System.out.println("ScheduledTaskJob.addLog() data:"+data);
Number nid=(Number)data.get("id");
String timezone=(String)data.get("timezone");
Date end=new Date();
int id= nid.intValue();
int logid=sdb.addSchedulerLog(id, start, end, timezone, status,null);
//calculates different and adds in server's time
if(sframe.getStarted_time()>0){
long diff=end.getTime()-start.getTime();
start.setTime(sframe.getStarted_time());
long endtime=sframe.getStarted_time()+diff;
end.setTime(endtime);
}
try{
TreeMap record=new TreeMap();
record.put("scheduler_id", nid);
record.put("trigger_time", new Long(sframe.getTrigger_time()));
record.put("start_time", start);
record.put("end_time", end);
record.put("status", status);
record.put("is_triggered",new Integer(1));
record.put("log_id",new Integer(logid));
record.put("host",P2PService.getComputerName());
record.put("console_message",sframe.getConsole_message());
log.debug("addLog:sframe.getDbConnectionIds():"+sframe.getDbConnectionIds());
//Vector connection_id=c;
Map cd=null;
if(sframe.getDbConnectionIds()!=null && sframe.getDbConnectionIds().size()>0) {
String con_ids="";
for(Iterator it=sframe.getDbConnectionIds().iterator();it.hasNext();){
con_ids+=(con_ids.equals("")?"":",")+"'"+it.next()+"'";
}
log.debug("###########con_ids::"+con_ids);
cd=sdb.getDBLogSummary(con_ids);
if(cd!=null && cd.size()>0){
//record.put("db_connection_ids",con_ids);
record.putAll(cd);
}
}
Vector v=new Vector();
v.add(record);
if(sframe.getNexttrigger_time()>0){
TreeMap record1=new TreeMap();
record1.put("scheduler_id", nid);
record1.put("trigger_time", new Long(sframe.getNexttrigger_time()));
record.put("log_id",new Integer(logid));
v.add(record1);
}
log.debug("logging job:+"+sframe.getTrigger_time()+" scheduler_Id:"+nid);
//this tries 10 times with interval of 500 milliseconds;
for(int i=0;i<10;i++){
try{
sdb.updateQueueLog(v,(cd!=null && cd.size()>0)?sframe.getDbConnectionIds():new Vector(), P2PService.getComputerName());
i=100;
}catch(Exception e){
log.error("SQL Error:"+e.getMessage()+" trying "+i);
Thread.sleep(500);
//sdb.updateQueueNullStatus(sc_id, tri_time, getStatus());
}
}
if(sframe.getExecuted_code()!=null){
for(int i=0;i<10;i++){
try{
sdb.updateExecutedCodeQLog(nid.intValue(),sframe.getTrigger_time(),sframe.getExecuted_code());
i=100;
}catch(Exception e){
log.error("Error:"+e.getMessage()+" trying "+i); Thread.sleep(500);
}
}
}
}catch(Exception e){
ClientError.reportError(e, null);
}
log.debug("before adding into db sframe.getTaskLog():"+sframe.getTasklog());
if(sframe.getTasklog()!=null && !sframe.getTasklog().equals("")){
try{
sdb.updateSchedulerLogMsg(logid,sframe.getTasklog());
}catch(Exception e){
log.error("Error while updating log message of R Engine:"+e.getMessage());
}
}
sdb.closeDB();
return logid;
}
private void addScriptLog(Date start, Map data, String status,StackFrame sframe) throws Exception {
SchedulerDB sdb=SchedulerDB.getSchedulerDB();
sdb.connectDB();
//System.out.println("ScheduledTaskJob.addLog() data:"+data);
Number script_id=(Number)data.get("script_id");
//String timezone=(String)data.get("timezone");
Date end=new Date();
sdb.addRScriptLog(script_id.intValue(), P2PService.getComputerName(), status, start, end, sframe.getTasklog());
sdb.closeDB();
}
}
@@ -0,0 +1,316 @@
/******************************************************************************
*
* Copyright: Intellectual Property of Four Elements Capital Pte Ltd, Singapore.
* All rights reserved.
*
******************************************************************************/
package com.fourelementscapital.scheduler.peer;
import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;
import java.util.Vector;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import com.fourelementscapital.db.SchedulerDB;
import com.fourelementscapital.scheduler.ScheduledTaskFactory;
import com.fourelementscapital.scheduler.config.Config;
import com.fourelementscapital.scheduler.engines.ScheduledTask;
import com.fourelementscapital.scheduler.engines.StackFrame;
import com.fourelementscapital.scheduler.p2p.P2PService;
import com.fourelementscapital.scheduler.p2p.peer.PeerSpecificConfigurations;
public class QueueFactory {
private static TreeMap queueHandlers=new TreeMap();
public static String KEY_STARTED_TIME="started_time";
public static String KEY_TRIGGER_TIME="trigger_time";
public static String KEY_SCHEDULER_ID="scheduler_id";
public static String KEY_CURRENT_TIME="current_time";
public static String KEY_TASK_NAME="name";
private final static int NUM_SIMULTANEOUS_CONN=10;
protected static boolean restartRequested=false;
private Logger log = LogManager.getLogger(QueueFactory.class.getName());
public QueueFactory(){
if(queueHandlers.size()==0){
initQueue();
}
}
public QueueAbstract getQueue(String taskuid){
return (QueueAbstract)queueHandlers.get(taskuid);
}
public TreeMap getQueue(){
return queueHandlers;
}
public static void setRestartRequested(){
restartRequested=true;
}
/**
* this method used to send BUSY/NOBUSY signal to the server.
* @return
*/
public int countExcTasksInPeer(){
int executing=0;
for(Iterator i=queueHandlers.values().iterator();i.hasNext();){
QueueAbstract aq=(QueueAbstract)i.next();
if(aq.getExecutingStacksSize()>0){
//executing=true;
executing=executing+aq.getExecutingStacksSize();
}
}
return executing;
}
/**
* this method will be used to send running tasks id of the peer to server.
* @return
*/
public Vector getExecutingIDs(){
Vector executing=new Vector();
for(Iterator i=queueHandlers.values().iterator();i.hasNext();){
QueueAbstract aq=(QueueAbstract)i.next();
Object stacks[]=aq.getExecutingStacks();
for(int a=0;a<stacks.length;a++){
StackFrame sf=(StackFrame)stacks[a];
if(sf.getData()!=null && !executing.contains(sf.getData().get("id"))){
executing.add(sf.getData().get("id"));
}
}
}
return executing;
}
/**
* this method is used by getPeerInfo method of schedulerAPI.
* @return
*/
public Vector getExecutingTasksData(){
Vector executing=new Vector();
Vector rtn=new Vector();
for(Iterator i=queueHandlers.values().iterator();i.hasNext();){
QueueAbstract aq=(QueueAbstract)i.next();
Object stacks[]=aq.getExecutingStacks();
for(int a=0;a<stacks.length;a++){
StackFrame sf=(StackFrame)stacks[a];
if(sf.getData()!=null && !executing.contains(sf.getData().get("id"))){
executing.add(sf.getData().get("id"));
Map data=new HashMap();
data.put(KEY_SCHEDULER_ID, sf.getData().get("id"));
data.put(KEY_STARTED_TIME, sf.getStarted_time());
data.put(KEY_TRIGGER_TIME, sf.getTrigger_time());
data.put(KEY_TASK_NAME, sf.getData().get("name"));
data.put(KEY_CURRENT_TIME, new Date().getTime());
rtn.add(data);
}
}
}
return rtn;
}
/*
* This method will is being used by peer to send currently running tasks and time to server to update the scheduler user interface
*/
public Map<String,String> getExecutingIDAndSTimes(){
TreeMap executing=new TreeMap();
for(Iterator i=queueHandlers.values().iterator();i.hasNext();){
QueueAbstract aq=(QueueAbstract)i.next();
Object stacks[]=aq.getExecutingStacks();
for(int a=0;a<stacks.length;a++){
StackFrame sf=(StackFrame)stacks[a];
if(sf.getData()!=null){
executing.put(sf.getData().get("id")+"", sf.getTrigger_time()+"");
}
//executing.add(sf.getData().get("id"));
}
}
return executing;
}
private void initQueue(){
SchedulerDB sdb=SchedulerDB.getSchedulerDB();
try{
sdb.connectDB();
sdb.removeAllPeerThreadStatus(P2PService.getComputerName());
}catch(Exception e){ }finally{
try{sdb.closeDB();}catch(Exception e1){}
}
//over-rides
QueueAbstract rserv=new QueueAbstract("rserv"){
public Vector getTaskUids(){
Vector v=new Vector();
SchedulerDB sdb=SchedulerDB.getSchedulerDB();
try{
sdb.connectDB();
Vector grp=sdb.getGroups("rscript4rserve");
//Vector allgroups=sdb.getAllGroups();
for(Iterator i=grp.iterator();i.hasNext();){
Map data=(Map)i.next();
String taskuid=(String)data.get("taskuid");
v.add(taskuid);
}
}catch(Exception e){ }finally{
try{sdb.closeDB();}catch(Exception e1){}
}
//v.add(new RServeScheduledTask().getUniqueid());
//v.add(new RServeLowPriorityTask().getUniqueid());
//v.add(new RServeUnixTask().getUniqueid());
//v.add(new RScript4Weather().getUniqueid());
return v;
}
public int getConcurrentThreads(){
if(System.getProperty("os.name").toLowerCase().startsWith("windows")){
return 1;
}else{
int rtn=NUM_SIMULTANEOUS_CONN;
try{
if(Config.getValue(Config.CONFIG_NUMBEROF_RSERVE_THREADS)!=null){
rtn=Integer.parseInt(Config.getValue(Config.CONFIG_NUMBEROF_RSERVE_THREADS).trim());
log.debug("group rscript4rserve found peer specific thread size, currently"+rtn);
}
}catch(Exception e){
System.out.println("QueueFactory.initQueue() Error:"+e.getMessage());
}
return rtn;
}
}
};
add2Qhandler(rserv);
//over-rides
QueueAbstract rservunix=new QueueAbstract("rservunix"){
public Vector getTaskUids(){
Vector v=new Vector();
v.add("direct_script_unix");
SchedulerDB sdb=SchedulerDB.getSchedulerDB();
try{
sdb.connectDB();
Vector grp=sdb.getGroups("rscript4rserveunix");
//Vector allgroups=sdb.getAllGroups();
for(Iterator i=grp.iterator();i.hasNext();){
Map data=(Map)i.next();
String taskuid=(String)data.get("taskuid");
v.add(taskuid);
}
}catch(Exception e){ }finally{
try{sdb.closeDB();}catch(Exception e1){}
}
return v;
}
public int getConcurrentThreads(){
int rtn=NUM_SIMULTANEOUS_CONN;
try{
//if(Config.getValue(Config.CONFIG_NUMBEROF_RSERVE_THREADS)!=null){
// rtn=Integer.parseInt(Config.getValue(Config.CONFIG_NUMBEROF_RSERVE_THREADS).trim());
// log.debug("group rscript4rservunix found peer specific thread size, currently"+rtn);
//}
String val=PeerSpecificConfigurations.getProperties().getProperty(PeerSpecificConfigurations.KEY_CONCURRENT_SESSION);
if(val!=null && !val.trim().equals("")){
rtn=Integer.parseInt(val);
}
}catch(Exception e){System.out.println("QueueFactory.initQueue() Error:"+e.getMessage());}
return rtn;
}
};
add2Qhandler(rservunix);
//over-rides
QueueAbstract rscript=new QueueAbstract("rscript"){
public Vector getTaskUids(){
Vector v=new Vector();
//add exclicitly direct_script into rscript group to make single threaded.
v.add("direct_script");
SchedulerDB sdb=SchedulerDB.getSchedulerDB();
try{
sdb.connectDB();
Vector grp=sdb.getGroups("rscript");
//Vector allgroups=sdb.getAllGroups();
for(Iterator i=grp.iterator();i.hasNext();){
Map data=(Map)i.next();
String taskuid=(String)data.get("taskuid");
if(taskuid!=null ){
v.add(taskuid);
}
}
}catch(Exception e){ }finally{
try{sdb.closeDB();}catch(Exception e1){}
}
return v;
}
public int getConcurrentThreads(){
return 1;
}
};
add2Qhandler(rscript);
QueueAbstract othercripts=new QueueAbstract("othercripts"){
public Vector getTaskUids(){
Vector v=new Vector();
List tasks=new ScheduledTaskFactory().getAllConfiguredTasks();
for(Iterator i=tasks.iterator();i.hasNext();){
ScheduledTask st=(ScheduledTask)i.next();
if(!queueHandlers.containsKey(st.getUniqueid())){
v.add(st.getUniqueid());
}
}
return v;
}
public int getConcurrentThreads(){
return 1;
}
};
add2Qhandler(othercripts);
}
private void add2Qhandler(QueueAbstract qa){
for(Iterator i=qa.getTaskUids().iterator();i.hasNext();){
queueHandlers.put(i.next(), qa);
}
}
}
@@ -0,0 +1,191 @@
/******************************************************************************
*
* Copyright: Intellectual Property of Four Elements Capital Pte Ltd, Singapore.
* All rights reserved.
*
******************************************************************************/
package com.fourelementscapital.scheduler.queue;
import java.util.List;
import com.googlecode.cqengine.attribute.Attribute;
import com.googlecode.cqengine.attribute.MultiValueNullableAttribute;
import com.googlecode.cqengine.attribute.SimpleAttribute;
import com.googlecode.cqengine.attribute.SimpleNullableAttribute;
import com.googlecode.cqengine.codegen.AttributesGenerator;
public class QueueStack {
private String name=null;
private List<String> supportedtaskuids=null;
private String peername=null;
private int executioncount=0;
private int priority=0;
private long lastexecuted=0;
private String uid="";
private boolean running=false;
private boolean available=false;
public boolean isAvailable() {
return available;
}
public void setAvailable(boolean available) {
this.available = available;
}
public boolean isRunning() {
return running;
}
public void setRunning(boolean running) {
this.running = running;
}
public QueueStack(){
}
public String getName() {
return name;
}
public List<String> getSupportedtaskuids() {
return supportedtaskuids;
}
public String getPeername() {
return peername;
}
public int getExecutioncount() {
return executioncount;
}
public int getPriority() {
return priority;
}
public long getLastexecuted() {
return lastexecuted;
}
public String getUid() {
return uid;
}
public void setName(String name) {
this.name = name;
}
public void setSupportedtaskuids(List<String> supportedtaskuids) {
this.supportedtaskuids = supportedtaskuids;
}
public void setPeername(String peername) {
this.peername = peername;
}
public void setExecutioncount(int executioncount) {
this.executioncount = executioncount;
}
public void setPriority(int priority) {
this.priority = priority;
}
public void setLastexecuted(long lastexecuted) {
this.lastexecuted = lastexecuted;
}
public void setUid(String uid) {
this.uid = uid;
}
public static void main(String[] args) {
System.out.println(AttributesGenerator.generateAttributesForPastingIntoTargetClass(QueueStack.class));
}
/**
* CQEngine attribute for accessing field {@code QueueStack.name}.
*/
// Note: For best performance:
// - if this field cannot be null, replace this SimpleNullableAttribute with a SimpleAttribute
public static final Attribute<QueueStack, String> NAME = new SimpleNullableAttribute<QueueStack, String>("NAME") {
public String getValue(QueueStack queuestack) { return queuestack.name; }
};
/**
* CQEngine attribute for accessing field {@code QueueStack.supportedtaskuids}.
*/
// Note: For best performance:
// - if the list cannot contain null elements change true to false in the following constructor, or
// - if the list cannot contain null elements AND the field itself cannot be null, replace this
// MultiValueNullableAttribute with a MultiValueAttribute (and change getNullableValues() to getValues())
public static final Attribute<QueueStack, String> SUPPORTEDTASKUIDS = new MultiValueNullableAttribute<QueueStack, String>("SUPPORTEDTASKUIDS", true) {
public List<String> getNullableValues(QueueStack queuestack) { return queuestack.supportedtaskuids; }
};
/**
* CQEngine attribute for accessing field {@code QueueStack.peername}.
*/
// Note: For best performance:
// - if this field cannot be null, replace this SimpleNullableAttribute with a SimpleAttribute
public static final Attribute<QueueStack, String> PEERNAME = new SimpleNullableAttribute<QueueStack, String>("PEERNAME") {
public String getValue(QueueStack queuestack) { return queuestack.peername; }
};
/**
* CQEngine attribute for accessing field {@code QueueStack.executioncount}.
*/
public static final Attribute<QueueStack, Integer> EXECUTIONCOUNT = new SimpleAttribute<QueueStack, Integer>("EXECUTIONCOUNT") {
public Integer getValue(QueueStack queuestack) { return queuestack.executioncount; }
};
/**
* CQEngine attribute for accessing field {@code QueueStack.priority}.
*/
public static final Attribute<QueueStack, Integer> PRIORITY = new SimpleAttribute<QueueStack, Integer>("PRIORITY") {
public Integer getValue(QueueStack queuestack) { return queuestack.priority; }
};
/**
* CQEngine attribute for accessing field {@code QueueStack.lastexecuted}.
*/
public static final Attribute<QueueStack, Long> LASTEXECUTED = new SimpleAttribute<QueueStack, Long>("LASTEXECUTED") {
public Long getValue(QueueStack queuestack) { return queuestack.lastexecuted; }
};
/**
* CQEngine attribute for accessing field {@code QueueStack.uid}.
*/
// Note: For best performance:
// - if this field cannot be null, replace this SimpleNullableAttribute with a SimpleAttribute
public static final Attribute<QueueStack, String> UID = new SimpleNullableAttribute<QueueStack, String>("UID") {
public String getValue(QueueStack queuestack) { return queuestack.uid; }
};
/**
* CQEngine attribute for accessing field {@code QueueStack.running}.
*/
public static final Attribute<QueueStack, Boolean> RUNNING = new SimpleAttribute<QueueStack, Boolean>("RUNNING") {
public Boolean getValue(QueueStack queuestack) { return queuestack.running; }
};
/**
* CQEngine attribute for accessing field {@code QueueStack.available}.
*/
public static final Attribute<QueueStack, Boolean> AVAILABLE = new SimpleAttribute<QueueStack, Boolean>("AVAILABLE") {
public Boolean getValue(QueueStack queuestack) { return queuestack.available; }
};
}
@@ -0,0 +1,198 @@
/******************************************************************************
*
* Copyright: Intellectual Property of Four Elements Capital Pte Ltd, Singapore.
* All rights reserved.
*
******************************************************************************/
package com.fourelementscapital.scheduler.queue;
import static com.googlecode.cqengine.query.QueryFactory.and;
import static com.googlecode.cqengine.query.QueryFactory.ascending;
import static com.googlecode.cqengine.query.QueryFactory.equal;
import static com.googlecode.cqengine.query.QueryFactory.in;
import static com.googlecode.cqengine.query.QueryFactory.orderBy;
import static com.googlecode.cqengine.query.QueryFactory.queryOptions;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.StringTokenizer;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import com.fourelementscapital.scheduler.peer.QueueAbstract;
import com.fourelementscapital.scheduler.peer.QueueFactory;
import com.googlecode.cqengine.CQEngine;
import com.googlecode.cqengine.IndexedCollection;
import com.googlecode.cqengine.index.hash.HashIndex;
import com.googlecode.cqengine.index.navigable.NavigableIndex;
import com.googlecode.cqengine.query.Query;
public class QueueStackManager {
private static Logger log = LogManager.getLogger(QueueStackManager.class.getName());
private static IndexedCollection<QueueStack> qstack = CQEngine.newInstance();
static{
qstack.addIndex(NavigableIndex.onAttribute(QueueStack.PEERNAME));
qstack.addIndex(NavigableIndex.onAttribute(QueueStack.NAME));
qstack.addIndex(NavigableIndex.onAttribute(QueueStack.PRIORITY));
qstack.addIndex(NavigableIndex.onAttribute(QueueStack.RUNNING));
qstack.addIndex(NavigableIndex.onAttribute(QueueStack.AVAILABLE));
qstack.addIndex(NavigableIndex.onAttribute(QueueStack.UID));
qstack.addIndex(HashIndex.onAttribute(QueueStack.SUPPORTEDTASKUIDS));
}
public static void buildQueue4Peer(String peername) {
QueueFactory qf=new QueueFactory();
Map<String, QueueAbstract> q=qf.getQueue();
ArrayList<QueueAbstract> uniquelist=new ArrayList();
for(QueueAbstract qa:q.values()){
if(!uniquelist.contains(qa)){
uniquelist.add(qa);
for(int i=0;i<qa.getConcurrentThreads();i++){
if(qa.getTaskUids().size()>0){
QueueStack qs=new QueueStack();
qs.setName(qa.getName());
qs.setPeername(peername);
qs.setSupportedtaskuids(qa.getTaskUids());
qs.setUid(qa.getName()+i);
qs.setRunning(false);
qs.setAvailable(false);
qstack.add(qs);
}
}
}
}
}
public static Set<QueueStack> getAllQueueStacks() throws Exception {
return QueueStackManager.qstack;
}
public static void peerDisconnected(String peername){
if(peername!=null ){
Query<QueueStack> query3 = equal(QueueStack.PEERNAME, peername);
Iterator<QueueStack> it3=qstack.retrieve(query3,queryOptions(orderBy(ascending(QueueStack.UID)))).iterator();
while(it3.hasNext()) {
qstack.remove(it3.next());
}
}
}
private static void refreshQS(QueueStack qs) {
synchronized(qs){
QueueStackManager.qstack.remove(qs);
QueueStackManager.qstack.add(qs);
}
}
public static void setStackBusy(String peername, String q_uid) throws Exception {
setStackRunning(peername,q_uid,true);
}
public static void setStackIdle(String peername, String q_uid) throws Exception {
setStackRunning(peername,q_uid,false);
}
private static void setStackRunning(String peername, String q_uid,boolean flag) throws Exception {
Query<QueueStack> query3 = and(equal(QueueStack.PEERNAME, peername),equal(QueueStack.UID, q_uid));
Iterator<QueueStack> it3=QueueStackManager.qstack.retrieve(query3).iterator();
if(it3!=null && it3.hasNext()){
QueueStack qs=it3.next();
qs.setRunning(flag);
refreshQS(qs);
}
}
public static String getPeerQueueStatForServer() throws Exception {
Iterator<QueueStack> qst=QueueStackManager.qstack.iterator();
String result="";
while(qst.hasNext()){
QueueStack qs=qst.next();
result+=qs.getUid()+"="+qs.isRunning()+"|";
}
return result;
}
public static void server2SyncPeerQueue(String peername,String qstring) throws Exception {
StringTokenizer st=new StringTokenizer(qstring,"|");
while(st.hasMoreTokens()){
String tkn=st.nextToken();
//log.debug("server2SyncPeerQueue() tkn:"+tkn);
if(tkn!=null && !tkn.trim().equals("")){
Pattern p=Pattern.compile("^(.*?)=(.*?)$");
Matcher matcher = p.matcher(tkn);
//log.debug("server2SyncPeerQueue() find:"+matcher.find()+" groupcount : "+matcher.groupCount());
if(matcher.find() && matcher.groupCount()==2){
String uid=matcher.group(1);
String r=matcher.group(2);
//log.debug("server2SyncPeerQueue() uid:"+uid+" r:"+r);
boolean running=r.toLowerCase().equals("true")?true:false;
boolean available=true;
Query<QueueStack> query3 = and(equal(QueueStack.PEERNAME, peername),equal(QueueStack.UID, uid));
Iterator<QueueStack> it3=QueueStackManager.qstack.retrieve(query3).iterator();
if(it3!=null && it3.hasNext()){
QueueStack qs=it3.next();
qs.setRunning(running);
qs.setAvailable(available); //it is available on peer.
log.debug("adding queue, q:"+qs.getUid()+" name:"+qs.getName()+" peer:"+qs.getPeername());
refreshQS(qs);
}
}
}
}
//this block is synchronizes number queue stack that available on peers.
Query<QueueStack> q1 = and(equal(QueueStack.AVAILABLE, false),equal(QueueStack.PEERNAME, peername));
for (QueueStack q : QueueStackManager.qstack.retrieve(q1)) {
//need to check isAvailable because Object query filter is not working properly.
if(!q.isAvailable()){
log.debug("removing from server queue, q:"+q.getUid()+" name:"+q.getName()+" peer:"+q.getPeername()+" available:"+q.isAvailable());
QueueStackManager.qstack.remove(q); //remove which are not available in peers, so that each peer can have its own number concurrent threads.
}
}
}
public static QueueStack useNextAvailableQueue(List peers, String taskuid) throws Exception {
QueueStack rtn=null;
Query<QueueStack> query3 =null;
if(peers.size()>1){
query3 = and(in(QueueStack.PEERNAME, (Collection)peers),equal(QueueStack.SUPPORTEDTASKUIDS, taskuid),equal(QueueStack.RUNNING,false));
}else {
query3 = and(equal(QueueStack.PEERNAME,(String)peers.get(0)),equal(QueueStack.SUPPORTEDTASKUIDS, taskuid),equal(QueueStack.RUNNING,false));
}
Iterator<QueueStack> it3=QueueStackManager.qstack.retrieve(query3,queryOptions(orderBy(ascending(QueueStack.EXECUTIONCOUNT)))).iterator();
if(it3!=null && it3.hasNext()){
rtn=it3.next();
rtn.setExecutioncount(rtn.getExecutioncount()+1);
rtn.setRunning(true);
refreshQS(rtn);
}
return rtn;
}
}
@@ -0,0 +1,46 @@
/******************************************************************************
*
* Copyright: Intellectual Property of Four Elements Capital Pte Ltd, Singapore.
* All rights reserved.
*
******************************************************************************/
package com.fourelementscapital.scheduler.queue;
import junit.framework.Test;
import junit.framework.TestCase;
import junit.framework.TestSuite;
/**
* Unit test for App
*/
public class AppTest extends TestCase {
/**
* Create the test case
*
* @param testName name of the test case
*/
public AppTest( String testName )
{
super( testName );
}
/**
* @return the suite of tests being tested
*/
public static Test suite()
{
return new TestSuite( AppTest.class );
}
/**
* Empty Test
*/
public void testApp()
{
assertTrue( true );
}
}