Yair Zaslavsky has uploaded a new change for review.

Change subject: setup: Changing task cleaner utility to also handle removal of 
commands
......................................................................

setup: Changing task cleaner utility to also handle removal of commands

The task cleaner utility now has the ability to present all commands
or commands with running tasks, and to erase commands (or commands with
running tasks)

Change-Id: I1d8f83cb7fc1f7fecb4ea68dff5cf0f3c63eeb6b
Signed-off-by: Yair Zaslavsky <[email protected]>
---
M packaging/setup/dbutils/taskcleaner.sh
M packaging/setup/dbutils/taskcleaner_sp.sql
2 files changed, 93 insertions(+), 7 deletions(-)


  git pull ssh://gerrit.ovirt.org:29418/ovirt-engine refs/changes/27/34927/1

diff --git a/packaging/setup/dbutils/taskcleaner.sh 
b/packaging/setup/dbutils/taskcleaner.sh
index ac225e0..374e386 100755
--- a/packaging/setup/dbutils/taskcleaner.sh
+++ b/packaging/setup/dbutils/taskcleaner.sh
@@ -1,11 +1,13 @@
 #!/bin/sh
 
###############################################################################################################
-# The purpose of this utility is to display and clean asynchronous tasks and 
corresponding
+# The purpose of this utility is to display and clean asynchronous tasks or 
commands and corresponding
 # Job steps/Compensation data.
 # The utility enables to
 # Display
 #     All async tasks
 #     Only Zombie tasks
+#     All commands
+#     All commands with tasks 
 # Delete
 #     All tasks
 #     All Zombie tasks
@@ -38,8 +40,11 @@
     -d DATABASE   - The database name                         (def. 
${DBFUNC_DB_DATABASE})
     -t TASK_ID    - Removes a task by its Task ID.
     -c COMMAND_ID - Removes all tasks related to the given Command Id.
+    -T            - Removes/Displays all commands that have have running tasks.
+    -o            - Removes/Displays all commands.
     -z            - Removes/Displays a Zombie task.
     -R            - Removes all tasks (use with -z to clear only zombie tasks).
+    -r            - Removes all commands (use with -T to clear only those with 
running tasks).
     -C            - Clear related compensation entries.
     -J            - Clear related Job Steps.
     -A            - Clear all Job Steps and compensation entries.
@@ -55,15 +60,19 @@
 TASK_ID=""
 COMMAND_ID=""
 ZOMBIES_ONLY=
+COMMANDS_WITH_RUNNING_TASKS_ONLY=
+ALL_COMMANDS=
 CLEAR_ALL=
+CLEAR_COMMANDS=
 CLEAR_COMPENSATION=
 CLEAR_JOB_STEPS=
 CLEAR_JOB_STEPS_AND_COMPENSATION=
 QUITE_MODE=
-FIELDS="task_id,task_type,status,started_at,result,action_type as 
command_type,command_id,step_id,storage_pool_id as DC"
+TASKS_FIELDS="task_id,task_type,status,started_at,result,action_type as 
command_type,command_id,step_id,storage_pool_id as DC"
+COMMANDS_FIELDS="command_id,command_type,root_command_id,command_parameters,command_params_class,created_at,status,return_value,return_value_class,job_id,step_id,executed"
 
-while getopts hvl:s:p:u:d:t:c:zRCJAq option; do
-       case "${option}" in
+while getopts hvl:s:p:u:d:t:c:zrRCJAToq option; do
+       case "${option}" in
                \?) usage; exit 1;;
                h) usage; exit 0;;
                v) DBFUNC_VERBOSE=1;;
@@ -75,7 +84,10 @@
                t) TASK_ID="${OPTARG}";;
                c) COMMAND_ID="${OPTARG}";;
                z) ZOMBIES_ONLY=1;;
+               T) COMMANDS_WITH_RUNNING_TASKS_ONLY=1;;
+               o) ALL_COMMANDS=1;;
                R) CLEAR_ALL=1;;
+               r) CLEAR_COMMANDS=1;;
                C) CLEAR_COMPENSATION=1;;
                J) CLEAR_JOB_STEPS=1;;
                A) CLEAR_JOB_STEPS_AND_COMPENSATION=1;;
@@ -227,13 +239,25 @@
        else
                die "Please specify task"
        fi
+elif [ -n "${CLEAR_COMMANDS}" ]; then
+echo "flag " ${COMMANDS_WITH_RUNNING_TASKS_ONLY}
+       if [ -n "${COMMANDS_WITH_RUNNING_TASKS_ONLY}" ]; then
+               CMD1="SELECT DeleteAllCommandsWithRunningTasks();"
+       else
+               CMD1="SELECT DeleteAllCommands();"
+       fi
 elif [ -n "${ZOMBIES_ONLY}" ]; then #only display operations block
-       CMD1="SELECT ${FIELDS} FROM GetAsyncTasksZombies();"
+       CMD1="SELECT ${TASKS_FIELDS} FROM GetAsyncTasksZombies();"
+elif [ -n "${ALL_COMMANDS}" ]; then #only display commands
+       CMD1="SELECT ${COMMANDS_FIELDS} FROM GetAllCommands();"
+elif [ -n "${COMMANDS_WITH_RUNNING_TASKS_ONLY}" ]; then #only display commands 
with tasks
+       CMD1="SELECT ${COMMANDS_FIELDS} FROM GetAllCommandsWithRunningTasks();"
 else
-       CMD1="SELECT ${FIELDS} FROM GetAllFromasync_tasks();"
+       CMD1="SELECT ${TASKS_FIELDS} FROM GetAllFromasync_tasks();"
 fi
 
 # Install taskcleaner procedures
 dbfunc_psql_die --file="$(dirname "$0")/taskcleaner_sp.sql" > /dev/null
 # Execute
-dbfunc_psql_die --command="${CMD1}${CMD2}"
+
+dbfunc_psql_die --command="${CMD1}${CMD2}"                                     
                                                                                
                                                                                
                                                                                
                                                                                
                                                                                
                                                                                
                                                                                
                                                                                
                                                                                
                                                                                
                                                                                
                                                                                
                                                                                
                                                                                
                                                                                
                                                                                
                                                                                
                                                                                
                                                                                
                                                                                
                                                                                
                                                                                
                                                                                
                                                                                
                                                                                
                                                                                
                                                                                
                                                                                
                                                                                
                                                                                
                                                                                
                                                                                
                                                                                
                                                                                
                                                                                
                                                                                
                                                                                
                                                                                
                                                                                
                                                                                
                                                                                
                                                                                
                                                                                
                                                                                
                                                                                
                                                                                
                                                                                
                                                                                
                                                                                
                
diff --git a/packaging/setup/dbutils/taskcleaner_sp.sql 
b/packaging/setup/dbutils/taskcleaner_sp.sql
index bf90648..7ea0d88 100644
--- a/packaging/setup/dbutils/taskcleaner_sp.sql
+++ b/packaging/setup/dbutils/taskcleaner_sp.sql
@@ -29,17 +29,34 @@
 
 CREATE OR REPLACE FUNCTION DeleteAsyncTaskZombiesByCommandId(v_command_id 
UUID) RETURNS VOID
    AS $procedure$
+DECLARE
+deleted_rows int;
 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;
+--externalize the next 4 lines to a function
+    DELETE FROM command_entities where command_id = v_command_id;
+    GET DIAGNOSTICS deleted_rows = ROW_COUNT;
+    IF deleted_rows > 0 THEN
+       DELETE FROM command_entities C WHERE command_id = 
root_command_id_of_deleted_cmds AND NOT EXISTS (SELECT * from COMMAND_ENTITIES 
WHERE root_command_id = C.command_id);
     END IF;
 END; $procedure$
 LANGUAGE plpgsql;
 
 CREATE OR REPLACE FUNCTION DeleteAsyncTaskByCommandId(v_command_id UUID) 
RETURNS VOID
    AS $procedure$
+DECLARE
+deleted_rows int;
+root_command_id_of_deleted_cmds UUID;
 BEGIN
         DELETE FROM async_tasks WHERE command_id = v_command_id;
+       SELECT root_command_id  into root_command_id_of_deleted_cmds FROM 
COMMAND_entities WHERE command_id = v_command_id;
+        DELETE FROM command_entities where command_id = v_command_id;
+       GET DIAGNOSTICS deleted_rows = ROW_COUNT;
+       IF deleted_rows > 0 THEN
+               DELETE FROM command_entities C WHERE command_id = 
root_command_id_of_deleted_cmds AND NOT EXISTS (SELECT * from COMMAND_ENTITIES 
WHERE root_command_id = C.command_id);
+       END IF;
 END; $procedure$
 LANGUAGE plpgsql;
 
@@ -118,3 +135,48 @@
     DELETE FROM business_entity_snapshot;
 END; $procedure$
 LANGUAGE plpgsql;
+
+Create or replace FUNCTION GetAllCommandsWithRunningTasks() RETURNS SETOF 
COMMAND_ENTITIES STABLE
+   AS $procedure$
+BEGIN
+   RETURN QUERY SELECT *
+   FROM COMMAND_ENTITIES C
+   WHERE EXISTS (SELECT * FROM ASYNC_TASKS A WHERE A.COMMAND_ID = 
C.COMMAND_ID);
+END; $procedure$
+LANGUAGE plpgsql;
+
+Create or replace FUNCTION  GetAllCommands()
+RETURNS SETOF COMMAND_ENTITIES STABLE
+   AS $procedure$
+BEGIN
+   RETURN QUERY SELECT *
+   FROM COMMAND_ENTITIES;
+END; $procedure$
+LANGUAGE plpgsql;
+
+Create or replace FUNCTION DeleteAllCommands()
+RETURNS integer
+   AS $procedure$
+DECLARE
+deleted_rows int;
+BEGIN
+   DELETE FROM COMMAND_ENTITIES;
+   GET DIAGNOSTICS deleted_rows = ROW_COUNT;
+   RETURN deleted_rows;
+
+END; $procedure$
+LANGUAGE plpgsql;
+
+Create or replace FUNCTION DeleteAllCommandsWithRunningTasks()
+RETURNS integer
+   AS $procedure$
+DECLARE
+deleted_rows int;
+BEGIN
+   DELETE FROM COMMAND_ENTITIES C 
+   WHERE EXISTS (SELECT * FROM ASYNC_TASKS A WHERE A.COMMAND_ID = 
C.COMMAND_ID);
+   GET DIAGNOSTICS deleted_rows = ROW_COUNT;
+   RETURN deleted_rows;
+
+END; $procedure$
+LANGUAGE plpgsql;


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

Gerrit-MessageType: newchange
Gerrit-Change-Id: I1d8f83cb7fc1f7fecb4ea68dff5cf0f3c63eeb6b
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