3604df
From fc9748357b79e76fe1c60c9c458c5dde9a9684e3 Mon Sep 17 00:00:00 2001
3604df
From: N Balachandran <nbalacha@redhat.com>
3604df
Date: Fri, 16 Sep 2016 22:08:53 +0530
3604df
Subject: [PATCH 69/86] events/dht: dht cli events
3604df
3604df
Adding events for add/remove brick and rebalance
3604df
from the cli.
3604df
3604df
upstream patch : http://review.gluster.org/15500
3604df
3604df
Change-Id: Ibb3cc307ba5825e5dd6ba3c46acf074e37f365d2
3604df
BUG: 1361066
3604df
Signed-off-by: N Balachandran <nbalacha@redhat.com>
3604df
Reviewed-on: https://code.engineering.redhat.com/gerrit/84838
3604df
Reviewed-by: Atin Mukherjee <amukherj@redhat.com>
3604df
Tested-by: Atin Mukherjee <amukherj@redhat.com>
3604df
---
3604df
 cli/src/cli-cmd-volume.c |  170 +++++++++++++++++++++++++++++++++++++++++++++-
3604df
 events/eventskeygen.py   |   22 ++++++
3604df
 2 files changed, 190 insertions(+), 2 deletions(-)
3604df
3604df
diff --git a/cli/src/cli-cmd-volume.c b/cli/src/cli-cmd-volume.c
3604df
index 4f6caab..f2679d2 100644
3604df
--- a/cli/src/cli-cmd-volume.c
3604df
+++ b/cli/src/cli-cmd-volume.c
3604df
@@ -613,6 +613,10 @@ cli_cmd_volume_defrag_cbk (struct cli_state *state, struct cli_cmd_word *word,
3604df
         int                     sent = 0;
3604df
         int                     parse_error = 0;
3604df
         cli_local_t          *local = NULL;
3604df
+#if (USE_EVENTS)
3604df
+        eventtypes_t         event = EVENT_LAST;
3604df
+#endif
3604df
+
3604df
 #ifdef GF_SOLARIS_HOST_OS
3604df
         cli_out ("Command not supported on Solaris");
3604df
         goto out;
3604df
@@ -642,6 +646,19 @@ out:
3604df
                 cli_cmd_sent_status_get (&sent);
3604df
                 if ((sent == 0) && (parse_error == 0))
3604df
                         cli_out ("Volume rebalance failed");
3604df
+        } else {
3604df
+
3604df
+#if (USE_EVENTS)
3604df
+                if (!(strcmp (words[wordcount-1], "start")) ||
3604df
+                        !(strcmp (words[wordcount-1], "force")))  {
3604df
+                        event = EVENT_VOLUME_REBALANCE_START;
3604df
+                } else if (!strcmp (words[wordcount-1], "stop")) {
3604df
+                        event = EVENT_VOLUME_REBALANCE_STOP;
3604df
+                }
3604df
+
3604df
+                if (event != EVENT_LAST)
3604df
+                        gf_event (event, "volume=%s", (char *)words[2]);
3604df
+#endif
3604df
         }
3604df
 
3604df
         CLI_STACK_DESTROY (frame);
3604df
@@ -853,6 +870,115 @@ out:
3604df
 
3604df
 }
3604df
 
3604df
+static
3604df
+int
3604df
+cli_event_remove_brick_str (dict_t *options, char **event_str,
3604df
+                            eventtypes_t *event)
3604df
+{
3604df
+        int           ret             = -1;
3604df
+        char          *bricklist      = NULL;
3604df
+        char          *brick          = NULL;
3604df
+        char          *volname        = NULL;
3604df
+        char           key[256]       = {0,};
3604df
+        const char    *eventstrformat = "volume=%s;bricks=%s";
3604df
+        int32_t        command        = 0;
3604df
+        int32_t        i              = 1;
3604df
+        int32_t        count          = 0;
3604df
+        int32_t        eventstrlen    = 1;
3604df
+        char          *tmp_ptr        = NULL;
3604df
+
3604df
+        if (!options || !event_str || !event)
3604df
+                goto out;
3604df
+
3604df
+        ret = dict_get_str (options, "volname", &volname);
3604df
+        if (ret || !volname) {
3604df
+                gf_log ("cli", GF_LOG_ERROR, "Failed to fetch volname");
3604df
+                ret = -1;
3604df
+                goto out;
3604df
+        }
3604df
+        /* Get the list of bricks for the event */
3604df
+        ret = dict_get_int32 (options, "command", &command);
3604df
+        if (ret) {
3604df
+                gf_log ("cli", GF_LOG_ERROR, "Failed to fetch command");
3604df
+                ret = -1;
3604df
+                goto out;
3604df
+        }
3604df
+
3604df
+        switch (command) {
3604df
+        case GF_OP_CMD_START:
3604df
+                *event = EVENT_VOLUME_REMOVE_BRICK_START;
3604df
+                break;
3604df
+        case GF_OP_CMD_COMMIT:
3604df
+                *event = EVENT_VOLUME_REMOVE_BRICK_COMMIT;
3604df
+                break;
3604df
+        case GF_OP_CMD_COMMIT_FORCE:
3604df
+                *event = EVENT_VOLUME_REMOVE_BRICK_FORCE;
3604df
+                break;
3604df
+        case GF_OP_CMD_STOP:
3604df
+                *event = EVENT_VOLUME_REMOVE_BRICK_STOP;
3604df
+                break;
3604df
+        default:
3604df
+                *event = EVENT_LAST;
3604df
+                break;
3604df
+        }
3604df
+
3604df
+        ret = -1;
3604df
+
3604df
+        if (*event == EVENT_LAST) {
3604df
+                goto out;
3604df
+        }
3604df
+
3604df
+        /* I could just get this from words[] but this is cleaner in case the
3604df
+         * format changes  */
3604df
+        while (i) {
3604df
+                snprintf (key, sizeof (key), "brick%d", i);
3604df
+                ret = dict_get_str (options, key, &brick);
3604df
+                if (ret) {
3604df
+                        break;
3604df
+                }
3604df
+                eventstrlen += strlen (brick) + 1;
3604df
+                i++;
3604df
+        }
3604df
+
3604df
+        count = --i;
3604df
+
3604df
+        eventstrlen += 1;
3604df
+
3604df
+        bricklist = GF_CALLOC (eventstrlen, sizeof (char), gf_common_mt_char);
3604df
+        if (!bricklist) {
3604df
+                goto out;
3604df
+        }
3604df
+
3604df
+        tmp_ptr = bricklist;
3604df
+
3604df
+        i = 1;
3604df
+        while (i <= count) {
3604df
+                snprintf (key, sizeof (key), "brick%d", i);
3604df
+                ret = dict_get_str (options, key, &brick);
3604df
+                if (ret) {
3604df
+                        break;
3604df
+                }
3604df
+                snprintf (tmp_ptr, eventstrlen, "%s ", brick);
3604df
+                eventstrlen -= (strlen (brick) + 1);
3604df
+                tmp_ptr += (strlen (brick) + 1);
3604df
+                i++;
3604df
+        }
3604df
+
3604df
+        if (!ret) {
3604df
+                gf_asprintf (event_str, eventstrformat, volname,
3604df
+                             bricklist);
3604df
+        } else {
3604df
+                gf_asprintf (event_str, eventstrformat, volname,
3604df
+                             "<unavailable>");
3604df
+        }
3604df
+
3604df
+        ret = 0;
3604df
+out:
3604df
+        GF_FREE (bricklist);
3604df
+        return ret;
3604df
+}
3604df
+
3604df
+
3604df
 int
3604df
 cli_cmd_volume_add_brick_cbk (struct cli_state *state,
3604df
                               struct cli_cmd_word *word, const char **words,
3604df
@@ -867,6 +993,12 @@ cli_cmd_volume_add_brick_cbk (struct cli_state *state,
3604df
         gf_answer_t             answer = GF_ANSWER_NO;
3604df
         cli_local_t             *local = NULL;
3604df
 
3604df
+#if (USE_EVENTS)
3604df
+        char                    *event_str = NULL;
3604df
+        char                    *bricks = NULL;
3604df
+        const char              *eventstrformat = "volume=%s;bricks=%s";
3604df
+#endif
3604df
+
3604df
         const char *question = "Changing the 'stripe count' of the volume is "
3604df
                 "not a supported feature. In some cases it may result in data "
3604df
                 "loss on the volume. Also there may be issues with regular "
3604df
@@ -895,6 +1027,20 @@ cli_cmd_volume_add_brick_cbk (struct cli_state *state,
3604df
                 }
3604df
         }
3604df
 
3604df
+#if (USE_EVENTS)
3604df
+        /* Get the list of bricks for the event */
3604df
+
3604df
+        ret = dict_get_str (options, "bricks", &bricks);
3604df
+
3604df
+        if (!ret) {
3604df
+                gf_asprintf (&event_str, eventstrformat, (char *)words[2],
3604df
+                            &bricks[1] /*Skip leading space*/);
3604df
+        } else {
3604df
+                gf_asprintf (&event_str, eventstrformat, (char *)words[2],
3604df
+                            "<unavailable>");
3604df
+        }
3604df
+#endif
3604df
+
3604df
         if (state->mode & GLUSTER_MODE_WIGNORE) {
3604df
                 ret = dict_set_int32 (options, "force", _gf_true);
3604df
                 if (ret) {
3604df
@@ -917,10 +1063,14 @@ out:
3604df
                 cli_cmd_sent_status_get (&sent);
3604df
                 if ((sent == 0) && (parse_error == 0))
3604df
                         cli_out ("Volume add-brick failed");
3604df
+        } else {
3604df
+#if (USE_EVENTS)
3604df
+                gf_event (EVENT_VOLUME_ADD_BRICK, event_str);
3604df
+                GF_FREE (event_str);
3604df
+#endif
3604df
         }
3604df
 
3604df
         CLI_STACK_DESTROY (frame);
3604df
-
3604df
         return ret;
3604df
 }
3604df
 
3604df
@@ -1792,7 +1942,11 @@ cli_cmd_volume_remove_brick_cbk (struct cli_state *state,
3604df
         int                     need_question = 0;
3604df
         cli_local_t             *local = NULL;
3604df
         char                    *volname = NULL;
3604df
-
3604df
+#if (USE_EVENTS)
3604df
+        eventtypes_t            event = EVENT_LAST;
3604df
+        char                    *event_str = NULL;
3604df
+        int                     event_ret = -1;
3604df
+#endif
3604df
         const char *question = "Removing brick(s) can result in data loss. "
3604df
                                "Do you want to Continue?";
3604df
 
3604df
@@ -1815,6 +1969,10 @@ cli_cmd_volume_remove_brick_cbk (struct cli_state *state,
3604df
                 goto out;
3604df
         }
3604df
 
3604df
+#if (USE_EVENTS)
3604df
+        event_ret = cli_event_remove_brick_str (options, &event_str, &event);
3604df
+#endif
3604df
+
3604df
         if (!strcmp (volname, GLUSTER_SHARED_STORAGE)) {
3604df
                 question = "Removing brick from the shared storage volume"
3604df
                            "(gluster_shared_storage), will affect features "
3604df
@@ -1846,10 +2004,18 @@ out:
3604df
                 cli_cmd_sent_status_get (&sent);
3604df
                 if ((sent == 0) && (parse_error == 0))
3604df
                         cli_out ("Volume remove-brick failed");
3604df
+        } else {
3604df
+#if (USE_EVENTS)
3604df
+                if (!event_ret) {
3604df
+                        gf_event (event, event_str);
3604df
+                        GF_FREE (event_str);
3604df
+                }
3604df
+#endif
3604df
         }
3604df
 
3604df
         CLI_STACK_DESTROY (frame);
3604df
 
3604df
+
3604df
         return ret;
3604df
 
3604df
 }
3604df
diff --git a/events/eventskeygen.py b/events/eventskeygen.py
3604df
index 4f7ec44..7fdf6a8 100644
3604df
--- a/events/eventskeygen.py
3604df
+++ b/events/eventskeygen.py
3604df
@@ -151,6 +151,7 @@ keys = (
3604df
     "EVENT_AFR_SUBVOLS_DOWN",
3604df
     "EVENT_AFR_SPLIT_BRAIN",
3604df
 
3604df
+    #tier events
3604df
     "EVENT_TIER_ATTACH",
3604df
     "EVENT_TIER_ATTACH_FORCE",
3604df
     "EVENT_TIER_DETACH_START",
3604df
@@ -163,6 +164,27 @@ keys = (
3604df
     "EVENT_TIER_WATERMARK_DROPPED_TO_MID",
3604df
     "EVENT_TIER_WATERMARK_RAISED_TO_MID",
3604df
     "EVENT_TIER_WATERMARK_DROPPED_TO_LOW",
3604df
+
3604df
+    #dht events
3604df
+    #add/remove brick events
3604df
+    "EVENT_VOLUME_ADD_BRICK",
3604df
+    "EVENT_VOLUME_ADD_BRICK_FAILED",
3604df
+    "EVENT_VOLUME_REMOVE_BRICK_START",
3604df
+    "EVENT_VOLUME_REMOVE_BRICK_START_FAILED",
3604df
+    "EVENT_VOLUME_REMOVE_BRICK_COMMIT",
3604df
+    "EVENT_VOLUME_REMOVE_BRICK_COMMIT_FAILED",
3604df
+    "EVENT_VOLUME_REMOVE_BRICK_STOP",
3604df
+    "EVENT_VOLUME_REMOVE_BRICK_STOP_FAILED",
3604df
+    "EVENT_VOLUME_REMOVE_BRICK_FORCE",
3604df
+    "EVENT_VOLUME_REMOVE_BRICK_FORCE_FAILED",
3604df
+    "EVENT_VOLUME_REMOVE_BRICK_FAILED",
3604df
+
3604df
+    #rebalance events
3604df
+    "EVENT_VOLUME_REBALANCE_START",
3604df
+    "EVENT_VOLUME_REBALANCE_STOP",
3604df
+    "EVENT_VOLUME_REBALANCE_FAILED",
3604df
+    "EVENT_VOLUME_REBALANCE_COMPLETE",
3604df
+
3604df
 )
3604df
 
3604df
 LAST_EVENT = "EVENT_LAST"
3604df
-- 
3604df
1.7.1
3604df