Mercurial > hg > Database > Alice
view src/main/java/alice/topology/manager/keeparive/TaskExecuter.java @ 489:9a7dd7591ddc dispose
bug fix
author | sugi |
---|---|
date | Tue, 09 Dec 2014 14:25:57 +0900 |
parents | be0b61986ff7 |
children | e5d2141af6c2 |
line wrap: on
line source
package alice.topology.manager.keeparive; import alice.codesegment.CodeSegment; import alice.datasegment.CommandType; import alice.datasegment.DataSegment; import alice.datasegment.Receiver; public class TaskExecuter extends CodeSegment { private Receiver info = ids.create(CommandType.TAKE); private TaskInfo nowTask; private boolean sleepFlag = false; private boolean skipFlag = false; private long startTime = 0; private long remainingTime = 0; private static TaskExecuter instance = new TaskExecuter(); private TaskExecuter() {} public static TaskExecuter getInstance() { return instance; } public void setKey() { ids.init(); info.setKey("_WAITINGLIST"); } @Override public synchronized void run() { ListManager list = info.asClass(ListManager.class); if (list.getTaskList().size() == 0){ remainingTime = 0; ods.update("_WAITINGLIST", list); } else { nowTask = list.getTaskList().poll(); ods.update("_WAITINGLIST", list); if (skipFlag) { skipFlag = false; nowTask.setSleepTime(remainingTime + nowTask.getSleepTime()); remainingTime = 0; } startTime = System.currentTimeMillis(); if (nowTask.getSleepTime() != 0){ sleepFlag = true; try { wait(nowTask.getSleepTime()); } catch (InterruptedException e) { e.printStackTrace(); } sleepFlag = false; } if (!skipFlag){ // ping or close if (nowTask.getType() == TaskType.PING) { ods.ping(nowTask.getManagerKey(), nowTask.getReturnKey()); TaskInfo task = new TaskInfo(TaskType.CLOSE); task.setInfo(nowTask.getManagerKey(), 10 * 1000); ods.put("_TASKINFO", task); new RespondPing(nowTask.getReturnKey()); } else if (nowTask.getType() == TaskType.CLOSE) { // no response from the Remote DataSegment. So close this connection. if (DataSegment.contains(nowTask.getManagerKey())) { DataSegment.get(nowTask.getManagerKey()).shutdown(); System.out.println(nowTask.getManagerKey() +" IS SHOTDOWN"); } else { System.out.println(nowTask.getManagerKey() +" IS ALREADY SHOTDOWN"); } } else if (nowTask.getType() == TaskType.CREATE) { new CreateTask(); } } nowTask = null; startTime = 0; } setKey(); } public synchronized void skip() { if (sleepFlag){ skipFlag = true; if (startTime == 0){ remainingTime = nowTask.getSleepTime(); } else { remainingTime = nowTask.getSleepTime() - (System.currentTimeMillis() - startTime); } nowTask = null; notify(); } } public synchronized void ignore() { if (sleepFlag){ skipFlag = true; remainingTime = 0; nowTask = null; notify(); } } public synchronized TaskInfo getNowTask() { return nowTask; } // only use in ListManagerTest public synchronized void setNowTask(TaskInfo info) { nowTask = info; } public synchronized long getRemainingTime() { if (sleepFlag) { if (startTime !=0) { return nowTask.getSleepTime() - (System.currentTimeMillis() - startTime); } else { return nowTask.getSleepTime(); } } else { return remainingTime; } } public synchronized boolean compareNowTask(TaskInfo task) { if (nowTask != null){ if (nowTask.getType().equals(task.getType()) && nowTask.getManagerKey().equals(task.getManagerKey())){ return true; } } return false; } }