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: I753c6343496ffe7c70c0c66ed8ab3facbcb533e8
Bug-Url: https://bugzilla.redhat.com/1161012
Signed-off-by: Yair Zaslavsky <yzasl...@redhat.com>
---
M packaging/setup/dbutils/taskcleaner.sh
M packaging/setup/dbutils/taskcleaner_sp.sql
2 files changed, 147 insertions(+), 40 deletions(-)


  git pull ssh://gerrit.ovirt.org:29418/ovirt-engine refs/changes/57/36057/1

diff --git a/packaging/setup/dbutils/taskcleaner.sh 
b/packaging/setup/dbutils/taskcleaner.sh
index ac225e0..435af9b 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
@@ -25,7 +27,59 @@
 trap cleanup 0
 dbfunc_init
 
-usage() {
+#Using two variables for sql commands in order to control command priority 
where data should be removed first from
+#business_entity_snapshot and step table before removing it from the 
async_tasks table.
+CMD1="";
+CMD2="";
+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=
+
+while getopts hvl:s:p:u:d:t:c:zrRCJAToq option; do
+       case "${option}" in
+               \?) PRINT_USAGE=1; USAGE_EXIT_CODE=1;;
+               h) PRINT_USAGE=1; USAGE_EXIT_CODE=0;;
+               v) DBFUNC_VERBOSE=1;;
+               l) DBFUNC_LOGFILE="${OPTARG}";;
+               s) DBFUNC_DB_HOST="${OPTARG}";;
+               p) DBFUNC_DB_PORT="${OPTARG}";;
+               d) DBFUNC_DB_DATABASE="${OPTARG}";;
+               u) DBFUNC_DB_USER="${OPTARG}";;
+               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;;
+               q) QUITE_MODE=1;;
+       esac
+done
+
+if [ -z "${DBFUNC_DB_USER}" ] || [ -z "${DBFUNC_DB_DATABASE}" ]; then
+       VERSION=3.4
+else
+       dbfunc_psql_die --command="select exists (select * from 
information_schema.tables where table_schema = 'public' and table_name = 
'command_entities');" | grep "t"
+       VERSION=$?
+       if [[ $VERSION = 0 ]]; then
+         VERSION=3.5
+       else
+         VERSION=3.4
+       fi
+fi
+
+if [[ $PRINT_USAGE = 1 ]] ; then
     cat << __EOF__
 Usage: $0 [options]
 
@@ -44,44 +98,21 @@
     -J            - Clear related Job Steps.
     -A            - Clear all Job Steps and compensation entries.
     -q            - Quite mode, do not prompt for confirmation.
-
 __EOF__
-}
+    if [ $VERSION = 3.5 ]; then
+        cat << __EOF__
 
-#Using two variables for sql commands in order to control command priority 
where data should be removed first from
-#business_entity_snapshot and step table before removing it from the 
async_tasks table.
-CMD1="";
-CMD2="";
-TASK_ID=""
-COMMAND_ID=""
-ZOMBIES_ONLY=
-CLEAR_ALL=
-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"
+    -T            - Removes/Displays all commands that have running tasks.
+    -o            - Removes/Displays all commands.
+    -r            - Removes all commands (use with -T to clear only those with 
running tasks).
+__EOF__
+    fi
+    exit $USAGE_EXIT_CODE
+fi
 
-while getopts hvl:s:p:u:d:t:c:zRCJAq option; do
-       case "${option}" in
-               \?) usage; exit 1;;
-               h) usage; exit 0;;
-               v) DBFUNC_VERBOSE=1;;
-               l) DBFUNC_LOGFILE="${OPTARG}";;
-               s) DBFUNC_DB_HOST="${OPTARG}";;
-               p) DBFUNC_DB_PORT="${OPTARG}";;
-               d) DBFUNC_DB_DATABASE="${OPTARG}";;
-               u) DBFUNC_DB_USER="${OPTARG}";;
-               t) TASK_ID="${OPTARG}";;
-               c) COMMAND_ID="${OPTARG}";;
-               z) ZOMBIES_ONLY=1;;
-               R) CLEAR_ALL=1;;
-               C) CLEAR_COMPENSATION=1;;
-               J) CLEAR_JOB_STEPS=1;;
-               A) CLEAR_JOB_STEPS_AND_COMPENSATION=1;;
-               q) QUITE_MODE=1;;
-       esac
-done
+
+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"
 
 caution() {
        if [ -z "${QUITE_MODE}" ]; then
@@ -99,6 +130,7 @@
 [ -n "${DBFUNC_DB_USER}" ] || die "Please specify user name"
 [ -n "${DBFUNC_DB_DATABASE}" ] || die "Please specify database"
 
+echo "ALL commands " $ALL_COMMANDS
 if [ "${TASK_ID}" != "" -o "${COMMAND_ID}" != "" -o -n "${CLEAR_ALL}" -o -n 
"${CLEAR_COMPENSATION}" -o -n "${CLEAR_JOB_STEPS}" ]; then #delete operations 
block
        if [ -n "${TASK_ID}" ]; then
                if [ -n "${ZOMBIES_ONLY}" ]; then
@@ -228,12 +260,27 @@
                die "Please specify task"
        fi
 elif [ -n "${ZOMBIES_ONLY}" ]; then #only display operations block
-       CMD1="SELECT ${FIELDS} FROM GetAsyncTasksZombies();"
-else
-       CMD1="SELECT ${FIELDS} FROM GetAllFromasync_tasks();"
+               CMD1="SELECT ${TASKS_FIELDS} FROM GetAsyncTasksZombies();"
+
+elif [[ $VERSION=3.5 ]]; then
+       echo "here!!!"
+       if [ -n "${CLEAR_COMMANDS}" ]; then
+               if [ -n "${COMMANDS_WITH_RUNNING_TASKS_ONLY}" ]; then
+                       CMD1="SELECT DeleteAllCommandsWithRunningTasks();"
+               else
+                       CMD1="SELECT DeleteAllCommands();"
+               fi
+       elif [ -n "${ALL_COMMANDS}" ]; then #only display commands
+               echo "here!!!"
+               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();"
+       fi
+       
 fi
 
 # Install taskcleaner procedures
 dbfunc_psql_die --file="$(dirname "$0")/taskcleaner_sp.sql" > /dev/null
 # Execute
+
 dbfunc_psql_die --command="${CMD1}${CMD2}"
diff --git a/packaging/setup/dbutils/taskcleaner_sp.sql 
b/packaging/setup/dbutils/taskcleaner_sp.sql
index bf90648..8f9e887 100644
--- a/packaging/setup/dbutils/taskcleaner_sp.sql
+++ b/packaging/setup/dbutils/taskcleaner_sp.sql
@@ -29,17 +29,33 @@
 
 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;
+    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 +134,47 @@
     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/36057
To unsubscribe, visit http://gerrit.ovirt.org/settings

Gerrit-MessageType: newchange
Gerrit-Change-Id: I753c6343496ffe7c70c0c66ed8ab3facbcb533e8
Gerrit-PatchSet: 1
Gerrit-Project: ovirt-engine
Gerrit-Branch: master
Gerrit-Owner: Yair Zaslavsky <yzasl...@redhat.com>
_______________________________________________
Engine-patches mailing list
Engine-patches@ovirt.org
http://lists.ovirt.org/mailman/listinfo/engine-patches

Reply via email to