Yair Zaslavsky has uploaded a new change for review.

Change subject: core: Treat "only in db" zombie tasks
......................................................................

core: Treat "only in db" zombie tasks

When system restarts, there may be some tasks
that exist only in the DB (form previous run).
If this occurs, these tasks are not polled, and will not be removed from DB.
The following mechanism introduces zombie tasks handling for these tasks as 
well.
Tasks that are added to db using "createTask" from commands are also added to 
the "polling
tasks" map so they should not be added to the "only in db" collection for 
zombie task treatment.

Change-Id: Ie13ae21eaadb1bb621a7fc8ad471e60387a3699c
Signed-off-by: Yair Zaslavsky <[email protected]>
---
M 
backend/manager/modules/bll/src/main/java/org/ovirt/engine/core/bll/AsyncTaskManager.java
M 
backend/manager/modules/bll/src/main/java/org/ovirt/engine/core/bll/SPMAsyncTask.java
2 files changed, 42 insertions(+), 8 deletions(-)


  git pull ssh://gerrit.ovirt.org:29418/ovirt-engine refs/changes/75/12175/1

diff --git 
a/backend/manager/modules/bll/src/main/java/org/ovirt/engine/core/bll/AsyncTaskManager.java
 
b/backend/manager/modules/bll/src/main/java/org/ovirt/engine/core/bll/AsyncTaskManager.java
index 8f8bed6..0264d9d 100644
--- 
a/backend/manager/modules/bll/src/main/java/org/ovirt/engine/core/bll/AsyncTaskManager.java
+++ 
b/backend/manager/modules/bll/src/main/java/org/ovirt/engine/core/bll/AsyncTaskManager.java
@@ -7,6 +7,7 @@
 import java.util.Map;
 import java.util.Set;
 import java.util.concurrent.TimeUnit;
+import java.util.concurrent.locks.ReentrantLock;
 
 import org.apache.commons.lang.exception.ExceptionUtils;
 import org.ovirt.engine.core.bll.job.ExecutionHandler;
@@ -21,10 +22,10 @@
 import org.ovirt.engine.core.common.businessentities.AsyncTaskResultEnum;
 import org.ovirt.engine.core.common.businessentities.AsyncTaskStatus;
 import org.ovirt.engine.core.common.businessentities.AsyncTaskStatusEnum;
+import org.ovirt.engine.core.common.businessentities.AsyncTasks;
 import org.ovirt.engine.core.common.businessentities.NonOperationalReason;
 import org.ovirt.engine.core.common.businessentities.VDS;
 import org.ovirt.engine.core.common.businessentities.VDSStatus;
-import org.ovirt.engine.core.common.businessentities.AsyncTasks;
 import org.ovirt.engine.core.common.businessentities.storage_pool;
 import org.ovirt.engine.core.common.config.Config;
 import org.ovirt.engine.core.common.config.ConfigValues;
@@ -55,6 +56,8 @@
 
     /** Map which consist all tasks that currently are monitored **/
     private Map<Guid, SPMAsyncTask> _tasks;
+    private List<AsyncTasks> tasksInDbAfterRestart = new 
ArrayList<AsyncTasks>();
+    private ReentrantLock tasksInDbLock = new ReentrantLock();
 
     /** Indication if _tasks has changed for logging process. **/
     private boolean logChangedMap = true;
@@ -68,9 +71,18 @@
         return _taskManager;
     }
 
+    public void removeTask(AsyncTasks task) {
+        tasksInDbLock.lock();
+        try {
+            tasksInDbAfterRestart.remove(task);
+        } finally {
+            tasksInDbLock.unlock();
+        }
+    }
+
     private AsyncTaskManager() {
         _tasks = new HashMap<Guid, SPMAsyncTask>();
-
+        tasksInDbAfterRestart = 
DbFacade.getInstance().getAsyncTaskDao().getAll();
         SchedulerUtil scheduler = SchedulerUtilQuartzImpl.getInstance();
         scheduler.scheduleAFixedDelayJob(this, "_timer_Elapsed", new Class[] 
{},
                 new Object[] {}, Config.<Integer> 
GetValue(ConfigValues.AsyncTaskPollingRate),
@@ -88,6 +100,9 @@
 
     @OnTimerMethodAnnotation("_timer_Elapsed")
     public synchronized void _timer_Elapsed() {
+        long timeForZombieTasks = DateTime.getNow()
+                .AddMinutes((-1) * Config.<Integer> 
GetValue(ConfigValues.AsyncTaskZombieTaskLifeInMinutes)).getTime();
+
         if (ThereAreTasksToPoll()) {
             PollAndUpdateAsyncTasks();
 
@@ -102,8 +117,16 @@
 
             // check for zombie tasks
             if (_tasks.size() > 0) {
-                cleanZombieTasks();
+               cleanPolledZombieTasks(timeForZombieTasks);
             }
+        }
+        tasksInDbLock.lock();
+        try {
+            if (tasksInDbAfterRestart.size() > 0) {
+                cleanOnlyInDbZombieTasks(timeForZombieTasks);
+            }
+        } finally {
+            tasksInDbLock.unlock();
         }
     }
 
@@ -169,12 +192,9 @@
                 && (task.getState() != AsyncTaskState.ClearFailed);
     }
 
-    private void cleanZombieTasks() {
-        long maxTime = DateTime.getNow()
-                .AddMinutes((-1) * Config.<Integer> 
GetValue(ConfigValues.AsyncTaskZombieTaskLifeInMinutes)).getTime();
+    private void cleanPolledZombieTasks(long timeForZombieTasks) {
         for (SPMAsyncTask task : _tasks.values()) {
-
-            if (task.getParameters().getDbAsyncTask().getStartTime().getTime() 
< maxTime) {
+            if (task.getParameters().getDbAsyncTask().getStartTime().getTime() 
< timeForZombieTasks) {
                 AuditLogableBase logable = new AuditLogableBase();
                 logable.addCustomValue("CommandName", 
task.getParameters().getDbAsyncTask().getaction_type().toString());
                 logable.addCustomValue("Date", 
task.getParameters().getDbAsyncTask().getStartTime().toString());
@@ -203,6 +223,19 @@
         }
     }
 
+    private void cleanOnlyInDbZombieTasks(long timeForZombieTasks) {
+        List<AsyncTasks> toRemove = new ArrayList<AsyncTasks>();
+        for (AsyncTasks task : tasksInDbAfterRestart) {
+            if (!_tasks.containsKey(task.gettask_id())) {
+                if (task.getStartTime().getTime() < timeForZombieTasks) {
+                    toRemove.add(task);
+                    
DbFacade.getInstance().getAsyncTaskDao().remove(task.gettask_id());
+                }
+            }
+        }
+        tasksInDbAfterRestart.removeAll(toRemove);
+    }
+
     private int NumberOfTasksToPoll() {
         int retValue = 0;
         for (SPMAsyncTask task : _tasks.values()) {
diff --git 
a/backend/manager/modules/bll/src/main/java/org/ovirt/engine/core/bll/SPMAsyncTask.java
 
b/backend/manager/modules/bll/src/main/java/org/ovirt/engine/core/bll/SPMAsyncTask.java
index 75ee506..0d16500 100644
--- 
a/backend/manager/modules/bll/src/main/java/org/ovirt/engine/core/bll/SPMAsyncTask.java
+++ 
b/backend/manager/modules/bll/src/main/java/org/ovirt/engine/core/bll/SPMAsyncTask.java
@@ -244,6 +244,7 @@
                     "BaseAsyncTask::RemoveTaskFromDB: Removing task %1$s from 
DataBase threw an exception.",
                     getTaskID()), e);
         }
+        
AsyncTaskManager.getInstance().removeTask(getParameters().getDbAsyncTask());
     }
 
     private boolean HasTaskEndedSuccessfully() {


--
To view, visit http://gerrit.ovirt.org/12175
To unsubscribe, visit http://gerrit.ovirt.org/settings

Gerrit-MessageType: newchange
Gerrit-Change-Id: Ie13ae21eaadb1bb621a7fc8ad471e60387a3699c
Gerrit-PatchSet: 1
Gerrit-Project: ovirt-engine
Gerrit-Branch: master
Gerrit-Owner: Yair Zaslavsky <[email protected]>
_______________________________________________
Engine-patches mailing list
[email protected]
http://lists.ovirt.org/mailman/listinfo/engine-patches

Reply via email to