Eli Mesika has uploaded a new change for review.

Change subject: core:Async Tasks cleaner utility...
......................................................................

core:Async Tasks cleaner utility...

Adding a utility to clean async tasks.
The utility enables to
  display all async tasks
  display only Zombie tasks
  Delete
      All tasks
      All Zombie tasks
      A task related to a given task id
      A Zombie task related to a given task id
      All tasks related to a given command id
      All Zombie tasks related to a given command id
   Flags may be added (-C, -J) to specify if Job Steps & Compensation data
   should be cleaned as well.

Usage: taskcleaner.sh [-h] [-s server] [-p PORT]] [-d DATABASE] [-u
USERNAME] [-l LOGFILE]  [-t taskId] [-c commandId] [-z] [-R] [-C] [-J]
[-v]

-s SERVERNAME - The database servername for the database  (def.
localhost)
-p PORT       - The database port for the database        (def.
5432)
-d DATABASE   - The database name                         (def.
engine)
-u USERNAME   - The admin username for the database.
-l LOGFILE    - The logfile for capturing output          (def.
taskcleaner.sh.log)
-t TASK_ID    - Removes a task by its Task ID.
-c COMMAND_ID - Removes all tasks related to the given Command
Id.
-z            - Removes/Displays a Zombie task.
-R            - Removes all Zombie tasks.
-C            - Clear related compensation entries.
-J            - Clear related Job Steps.
-v            - Turn on verbosity
(WARNING: lots of output)
-h            - This help text.

Change-Id: Idffc35eb5fca0e72c9e4c9e3a08c5b9799569fca
Signed-off-by: Eli Mesika <[email protected]>
Bug-Url: https://bugzilla.redhat.com/show_bug.cgi?id=891639
---
M backend/manager/dbscripts/async_tasks_sp.sql
M backend/manager/dbscripts/job_sp.sql
A backend/manager/dbscripts/taskcleaner.sh
3 files changed, 211 insertions(+), 0 deletions(-)


  git pull ssh://gerrit.ovirt.org:29418/ovirt-engine refs/changes/05/11005/1

diff --git a/backend/manager/dbscripts/async_tasks_sp.sql 
b/backend/manager/dbscripts/async_tasks_sp.sql
index 970b2bd..7f6f4af 100644
--- a/backend/manager/dbscripts/async_tasks_sp.sql
+++ b/backend/manager/dbscripts/async_tasks_sp.sql
@@ -129,3 +129,54 @@
 
 END; $procedure$
 LANGUAGE plpgsql;
+
+/************************************************************************************************
+ The following are helper SP for taskcleaner utility and are not exposed to 
the application DAO
+************************************************************************************************/
+CREATE OR REPLACE FUNCTION GetAsyncTasksZombies() RETURNS SETOF async_tasks
+   AS $procedure$
+DECLARE
+    zombie_task_life varchar;
+    zombie_date date;
+BEGIN
+   zombie_task_life = option_value FROM vdc_options WHERE option_name = 
'AsyncTaskZombieTaskLifeInMinutes';
+   EXECUTE 'SELECT now() - interval ''' || zombie_task_life || ' minute'''  
INTO zombie_date;
+
+   RETURN QUERY SELECT *
+   FROM async_tasks
+   WHERE started_at < zombie_date;
+
+END; $procedure$
+LANGUAGE plpgsql;
+
+CREATE OR REPLACE FUNCTION DeleteAsyncTaskZombiesByTaskId(v_task_id UUID) 
RETURNS VOID
+   AS $procedure$
+BEGIN
+    IF EXISTS (SELECT 1 FROM GetAsyncTasksZombies() WHERE task_id = v_task_id) 
THEN
+        SELECT Deleteasync_tasks(v_task_id);
+    END IF;
+END; $procedure$
+LANGUAGE plpgsql;
+
+CREATE OR REPLACE FUNCTION DeleteAsyncTaskZombiesByCommandId(v_command_id 
UUID) RETURNS VOID
+   AS $procedure$
+BEGIN
+    IF EXISTS (SELECT 1 FROM GetAsyncTasksZombies() WHERE command_id = 
v_command_id) THEN
+        DELETE FROM async_tasks WHERE command_id = v_command_id;
+    END IF;
+END; $procedure$
+LANGUAGE plpgsql;
+
+CREATE OR REPLACE FUNCTION DeleteAsyncTaskByCommandId(v_command_id UUID) 
RETURNS VOID
+   AS $procedure$
+BEGIN
+        DELETE FROM async_tasks WHERE command_id = v_command_id;
+END; $procedure$
+LANGUAGE plpgsql;
+
+CREATE OR REPLACE FUNCTION DeleteAsyncTasksZombies() RETURNS VOID
+   AS $procedure$
+BEGIN
+    DELETE FROM async_tasks WHERE task_id in (SELECT task_id FROM 
GetAsyncTasksZombies());
+END; $procedure$
+LANGUAGE plpgsql;
diff --git a/backend/manager/dbscripts/job_sp.sql 
b/backend/manager/dbscripts/job_sp.sql
index f10cc76..47d717b 100644
--- a/backend/manager/dbscripts/job_sp.sql
+++ b/backend/manager/dbscripts/job_sp.sql
@@ -493,3 +493,14 @@
 END; $procedure$
 LANGUAGE plpgsql;
 
+/************************************************************************************************
+ The following are helper SP for taskcleaner utility and are not exposed to 
the application DAO
+************************************************************************************************/
+
+CREATE OR REPLACE FUNCTION DeleteJobStepsByTaskId(v_task_id UUID) RETURNS VOID
+   AS $procedure$
+BEGIN
+    DELETE FROM step WHERE external_id = v_task_id;
+END; $procedure$
+LANGUAGE plpgsql;
+
diff --git a/backend/manager/dbscripts/taskcleaner.sh 
b/backend/manager/dbscripts/taskcleaner.sh
new file mode 100755
index 0000000..5961455
--- /dev/null
+++ b/backend/manager/dbscripts/taskcleaner.sh
@@ -0,0 +1,149 @@
+###############################################################################################################
+# The purpose of this utility is to display and clean asynchronous tasks and 
corresponding
+# Job steps/Compensation data.
+# The utility enables to
+# display all async tasks
+# display only Zombie tasks
+# Delete
+#     All tasks
+#     All Zombie tasks
+#     A task related to a given task id
+#     A Zombie task related to a given task id
+#     All tasks related to a given command id
+#     All Zombie tasks related to a given command id
+#  Flags may be added (-C, -J) to specify if Job Steps & Compensation data
+#  should be cleaned as well.
+###############################################################################################################
+
+#!/bin/bash
+#include db general functions
+source ./dbfunctions.sh
+source ./dbcustomfunctions.sh
+
+#setting defaults
+set_defaults
+
+
+usage() {
+    printf "Usage: ${ME} [-h] [-s server] [-p PORT]] [-d DATABASE] [-u 
USERNAME] [-l LOGFILE]  [-t taskId] [-c commandId] [-z] [-R] [-C] [-J] [-v]\n"
+    printf "\n"
+    printf "\t-s SERVERNAME - The database servername for the database  (def. 
${SERVERNAME})\n"
+    printf "\t-p PORT       - The database port for the database        (def. 
${PORT})\n"
+    printf "\t-d DATABASE   - The database name                         (def. 
${DATABASE})\n"
+    printf "\t-u USERNAME   - The admin username for the database.\n"
+    printf "\t-l LOGFILE    - The logfile for capturing output          (def. 
${LOGFILE})\n"
+    printf "\t-t TASK_ID    - Removes a task by its Task ID.\n"
+    printf "\t-c COMMAND_ID - Removes all tasks related to the given Command 
Id.\n"
+    printf "\t-z            - Removes/Displays a Zombie task.\n"
+    printf "\t-R            - Removes all Zombie tasks.\n"
+    printf "\t-C            - Clear related compensation entries.\n"
+    printf "\t-J            - Clear related Job Steps.\n"
+    printf "\t-v            - Turn on verbosity                         
(WARNING: lots of output)\n"
+    printf "\t-h            - This help text.\n"
+    printf "\n"
+
+    exit $ret
+}
+
+DEBUG () {
+    if $VERBOSE; then
+        printf "DEBUG: $*"
+    fi
+}
+
+CMD="";
+TASK_ID=""
+COMMAND_ID=""
+ZOMBIES_ONLY=false
+CLEAR_ALL=false
+CLEAR_COMPENSATION=false
+CLEAR_JOB_STEPS=false
+
+while getopts hs:d:u:p:l:t:c:zRCJv option; do
+    case $option in
+        s) SERVERNAME=$OPTARG;;
+        p) PORT=$OPTARG;;
+        d) DATABASE=$OPTARG;;
+        u) USERNAME=$OPTARG;;
+        l) LOGFILE=$OPTARG;;
+        t) TASK_ID=$OPTARG;;
+        c) COMMAND_ID=$OPTARG;;
+        z) ZOMBIES_ONLY=true;;
+        R) CLEAR_ALL=true;;
+        C) CLEAR_COMPENSATION=true;;
+        J) CLEAR_JOB_STEPS=true;;
+        v) VERBOSE=true;;
+        h) ret=0 && usage;;
+       \?) ret=1 && usage;;
+    esac
+done
+
+caution() {
+    echo $1
+    echo "Caution, this operation should be used with care. Please contact 
support prior to running this command"
+    echo "Are you sure you want to proceed? [y/n]"
+    read answer
+    if [ "${answer}" = "n" ]; then
+       echo "Please contact support for further assistance."
+       exit 1
+    fi
+}
+
+if [ "${TASK_ID}" != "" -o "${COMMAND_ID}" != "" -o  "${CLEAR_ALL}" = "true" 
-o "${CLEAR_COMPENSATION}" = "true" -o "${CLEAR_JOB_STEPS}" = "true" ]; then
+    if [ "${TASK_ID}" != "" ]; then
+        if [ "${ZOMBIES_ONLY}" = "true" ]; then
+            if [ "${CLEAR_JOB_STEPS}" = "true" ]; then
+                caution "This will remove the given Zombie Task and its 
related Job Steps!!!"
+                CMD="select 
DeleteAsyncTaskZombiesByTaskId('${TASK_ID}');select 
DeleteJobStepsByTaskId('${TASK_ID}');"
+            else
+                caution "This will remove the given Zombie Task!!!"
+                CMD="select DeleteAsyncTaskZombiesByTaskId('${TASK_ID}');"
+            fi
+        else
+            if [ "${CLEAR_JOB_STEPS}" = "true" ]; then
+                caution "This will remove the given Task and its related Job 
Steps!!!"
+                CMD="select Deleteasync_tasks('${TASK_ID}');select 
DeleteJobStepsByTaskId('${TASK_ID}');"
+            else
+                caution "This will remove the given Task!!!"
+                CMD="select Deleteasync_tasks('${TASK_ID}');"
+            fi
+        fi
+    elif [ "${COMMAND_ID}" != "" ]; then
+        if [ "${ZOMBIES_ONLY}" = "true" ]; then
+            if [ "${CLEAR_COMPENSATION}" = "true" ]; then
+                caution "This will remove all Zombie Tasks of the given 
Command and its related Compensation data!!!"
+                CMD="select 
DeleteAsyncTaskZombiesByCommandId('${COMMAND_ID}');select 
delete_entity_snapshot_by_command_id('${COMMAND_ID}');"
+            else
+                caution "This will remove all Zombie Tasks of the given 
Command!!!"
+                CMD="select 
DeleteAsyncTaskZombiesByCommandId('${COMMAND_ID}');"
+            fi
+        else
+            if [ "${CLEAR_COMPENSATION}" = "true" ]; then
+                caution "This will remove all Tasks of the given Command and 
its related Compensation data!!!"
+                CMD="select DeleteAsyncTaskByCommandId('${COMMAND_ID}');select 
delete_entity_snapshot_by_command_id('${COMMAND_ID}');"
+            else
+                caution "This will remove all Tasks of the given Command!!!"
+                CMD="select DeleteAsyncTaskByCommandId('${COMMAND_ID}');"
+            fi
+        fi
+    elif [ "${CLEAR_ALL}" = "true" ]; then
+        if [ "${ZOMBIES_ONLY}" = "true" ]; then
+            caution "This will remove all Zombie Tasks in async_tasks table!!!"
+            CMD="select DeleteAsyncTasksZombies();"
+        else
+            caution "This will remove all async_tasks table content!!!"
+            CMD="truncate table async_tasks;"
+        fi
+    else
+        usage
+    fi
+elif [ "${ZOMBIES_ONLY}" = "true" ]; then
+
+        CMD="select task_id,task_type,status,started_at,result,action_type as 
command_type,command_id,step_id,storage_pool_id as DC from 
GetAsyncTasksZombies();"
+else
+        CMD="select task_id,task_type,status,started_at,result,action_type as 
command_type,command_id,step_id,storage_pool_id as DC from 
GetAllFromasync_tasks();"
+fi
+
+execute_command "$CMD" ${DATABASE} ${SERVERNAME} ${PORT}
+
+exit $?


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

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

Reply via email to