14f8ab
From 86eee7e829bb33cac9b611da511ecbd2f03fab25 Mon Sep 17 00:00:00 2001
14f8ab
From: Mohit Agrawal <moagrawal@redhat.com>
14f8ab
Date: Fri, 17 May 2019 19:26:48 +0530
14f8ab
Subject: [PATCH 149/169] glusterd: Optimize code to copy dictionary in
14f8ab
 handshake code path
14f8ab
14f8ab
Problem: While high no. of volumes are configured around 2000
14f8ab
         glusterd has bottleneck during handshake at the time
14f8ab
         of copying dictionary
14f8ab
14f8ab
Solution: To avoid the bottleneck serialize a dictionary instead
14f8ab
          of copying key-value pair one by one
14f8ab
14f8ab
> Change-Id: I9fb332f432e4f915bc3af8dcab38bed26bda2b9a
14f8ab
> fixes: bz#1711297
14f8ab
> Cherry picked from commit f8f09178bb890924a8050b466cc2e7a0a30e35a7
14f8ab
> (Reviewed on upstream link https://review.gluster.org/#/c/glusterfs/+/22742/)
14f8ab
14f8ab
BUG: 1711296
14f8ab
Change-Id: I9fb332f432e4f915bc3af8dcab38bed26bda2b9a
14f8ab
Signed-off-by: Mohit Agrawal <moagrawal@redhat.com>
14f8ab
Reviewed-on: https://code.engineering.redhat.com/gerrit/172255
14f8ab
Reviewed-by: Atin Mukherjee <amukherj@redhat.com>
14f8ab
Tested-by: RHGS Build Bot <nigelb@redhat.com>
14f8ab
---
14f8ab
 libglusterfs/src/dict.c                      |   6 +-
14f8ab
 libglusterfs/src/glusterfs/dict.h            |   6 +
14f8ab
 libglusterfs/src/libglusterfs.sym            |   1 +
14f8ab
 xlators/mgmt/glusterd/src/glusterd-rpc-ops.c |  27 ++--
14f8ab
 xlators/mgmt/glusterd/src/glusterd-utils.c   | 187 +++++++++++++++++++++++----
14f8ab
 xlators/mgmt/glusterd/src/glusterd-utils.h   |   3 +-
14f8ab
 xlators/mgmt/glusterd/src/glusterd.h         |   5 +
14f8ab
 7 files changed, 194 insertions(+), 41 deletions(-)
14f8ab
14f8ab
diff --git a/libglusterfs/src/dict.c b/libglusterfs/src/dict.c
14f8ab
index 4cd1fcf..6917df9 100644
14f8ab
--- a/libglusterfs/src/dict.c
14f8ab
+++ b/libglusterfs/src/dict.c
14f8ab
@@ -2799,10 +2799,6 @@ dict_rename_key(dict_t *this, char *key, char *replace_key)
14f8ab
  *     4        4         4       <key len>   <value len>
14f8ab
  */
14f8ab
 
14f8ab
-#define DICT_HDR_LEN 4
14f8ab
-#define DICT_DATA_HDR_KEY_LEN 4
14f8ab
-#define DICT_DATA_HDR_VAL_LEN 4
14f8ab
-
14f8ab
 /**
14f8ab
  * dict_serialized_length_lk - return the length of serialized dict. This
14f8ab
  *                             procedure has to be called with this->lock held.
14f8ab
@@ -2812,7 +2808,7 @@ dict_rename_key(dict_t *this, char *key, char *replace_key)
14f8ab
  *        : failure: -errno
14f8ab
  */
14f8ab
 
14f8ab
-static int
14f8ab
+int
14f8ab
 dict_serialized_length_lk(dict_t *this)
14f8ab
 {
14f8ab
     int ret = -EINVAL;
14f8ab
diff --git a/libglusterfs/src/glusterfs/dict.h b/libglusterfs/src/glusterfs/dict.h
14f8ab
index 52b833f..022f564 100644
14f8ab
--- a/libglusterfs/src/glusterfs/dict.h
14f8ab
+++ b/libglusterfs/src/glusterfs/dict.h
14f8ab
@@ -91,6 +91,9 @@ typedef struct _data_pair data_pair_t;
14f8ab
 #define DICT_MAX_FLAGS 256
14f8ab
 #define DICT_FLAG_SET 1
14f8ab
 #define DICT_FLAG_CLEAR 0
14f8ab
+#define DICT_HDR_LEN 4
14f8ab
+#define DICT_DATA_HDR_KEY_LEN 4
14f8ab
+#define DICT_DATA_HDR_VAL_LEN 4
14f8ab
 
14f8ab
 struct _data {
14f8ab
     char *data;
14f8ab
@@ -412,4 +415,7 @@ are_dicts_equal(dict_t *one, dict_t *two,
14f8ab
                 gf_boolean_t (*value_ignore)(char *k));
14f8ab
 int
14f8ab
 dict_has_key_from_array(dict_t *dict, char **strings, gf_boolean_t *result);
14f8ab
+
14f8ab
+int
14f8ab
+dict_serialized_length_lk(dict_t *this);
14f8ab
 #endif
14f8ab
diff --git a/libglusterfs/src/libglusterfs.sym b/libglusterfs/src/libglusterfs.sym
14f8ab
index cf5757c..ec474e7 100644
14f8ab
--- a/libglusterfs/src/libglusterfs.sym
14f8ab
+++ b/libglusterfs/src/libglusterfs.sym
14f8ab
@@ -405,6 +405,7 @@ dict_rename_key
14f8ab
 dict_reset
14f8ab
 dict_serialize
14f8ab
 dict_serialized_length
14f8ab
+dict_serialized_length_lk
14f8ab
 dict_serialize_value_with_delim
14f8ab
 dict_set
14f8ab
 dict_setn
14f8ab
diff --git a/xlators/mgmt/glusterd/src/glusterd-rpc-ops.c b/xlators/mgmt/glusterd/src/glusterd-rpc-ops.c
14f8ab
index 4ec9700..45f8f17 100644
14f8ab
--- a/xlators/mgmt/glusterd/src/glusterd-rpc-ops.c
14f8ab
+++ b/xlators/mgmt/glusterd/src/glusterd-rpc-ops.c
14f8ab
@@ -1528,11 +1528,9 @@ glusterd_rpc_friend_add(call_frame_t *frame, xlator_t *this, void *data)
14f8ab
 
14f8ab
     RCU_READ_UNLOCK;
14f8ab
 
14f8ab
-    ret = glusterd_add_volumes_to_export_dict(&peer_data);
14f8ab
-    if (ret) {
14f8ab
-        gf_msg(this->name, GF_LOG_ERROR, 0, GD_MSG_DICT_SET_FAILED,
14f8ab
-               "Unable to add list of volumes "
14f8ab
-               "in the peer_data dict for handshake");
14f8ab
+    peer_data = dict_new();
14f8ab
+    if (!peer_data) {
14f8ab
+        errno = ENOMEM;
14f8ab
         goto out;
14f8ab
     }
14f8ab
 
14f8ab
@@ -1563,10 +1561,23 @@ glusterd_rpc_friend_add(call_frame_t *frame, xlator_t *this, void *data)
14f8ab
         }
14f8ab
     }
14f8ab
 
14f8ab
-    ret = dict_allocate_and_serialize(peer_data, &req.vols.vols_val,
14f8ab
-                                      &req.vols.vols_len);
14f8ab
-    if (ret)
14f8ab
+    /* Don't add any key-value in peer_data dictionary after call this function
14f8ab
+     */
14f8ab
+    ret = glusterd_add_volumes_to_export_dict(peer_data, &req.vols.vols_val,
14f8ab
+                                              &req.vols.vols_len);
14f8ab
+    if (ret) {
14f8ab
+        gf_msg(this->name, GF_LOG_ERROR, 0, GD_MSG_DICT_SET_FAILED,
14f8ab
+               "Unable to add list of volumes "
14f8ab
+               "in the peer_data dict for handshake");
14f8ab
         goto out;
14f8ab
+    }
14f8ab
+
14f8ab
+    if (!req.vols.vols_len) {
14f8ab
+        ret = dict_allocate_and_serialize(peer_data, &req.vols.vols_val,
14f8ab
+                                          &req.vols.vols_len);
14f8ab
+        if (ret)
14f8ab
+            goto out;
14f8ab
+    }
14f8ab
 
14f8ab
     ret = glusterd_submit_request(
14f8ab
         peerinfo->rpc, &req, frame, peerinfo->peer, GLUSTERD_FRIEND_ADD, NULL,
14f8ab
diff --git a/xlators/mgmt/glusterd/src/glusterd-utils.c b/xlators/mgmt/glusterd/src/glusterd-utils.c
14f8ab
index 8f1525e..2bc4836 100644
14f8ab
--- a/xlators/mgmt/glusterd/src/glusterd-utils.c
14f8ab
+++ b/xlators/mgmt/glusterd/src/glusterd-utils.c
14f8ab
@@ -3466,11 +3466,118 @@ out:
14f8ab
     return NULL;
14f8ab
 }
14f8ab
 
14f8ab
+int
14f8ab
+glusterd_dict_searialize(dict_t *dict_arr[], int count, int totcount, char *buf)
14f8ab
+{
14f8ab
+    int i = 0;
14f8ab
+    int32_t keylen = 0;
14f8ab
+    int64_t netword = 0;
14f8ab
+    data_pair_t *pair = NULL;
14f8ab
+    int dict_count = 0;
14f8ab
+    int ret = 0;
14f8ab
+
14f8ab
+    netword = hton32(totcount);
14f8ab
+    memcpy(buf, &netword, sizeof(netword));
14f8ab
+    buf += DICT_HDR_LEN;
14f8ab
+
14f8ab
+    for (i = 0; i < count; i++) {
14f8ab
+        if (dict_arr[i]) {
14f8ab
+            dict_count = dict_arr[i]->count;
14f8ab
+            pair = dict_arr[i]->members_list;
14f8ab
+            while (dict_count) {
14f8ab
+                if (!pair) {
14f8ab
+                    gf_msg("glusterd", GF_LOG_ERROR, 0,
14f8ab
+                           LG_MSG_PAIRS_LESS_THAN_COUNT,
14f8ab
+                           "less than count data pairs found!");
14f8ab
+                    ret = -1;
14f8ab
+                    goto out;
14f8ab
+                }
14f8ab
+
14f8ab
+                if (!pair->key) {
14f8ab
+                    gf_msg("glusterd", GF_LOG_ERROR, 0, LG_MSG_NULL_PTR,
14f8ab
+                           "pair->key is null!");
14f8ab
+                    ret = -1;
14f8ab
+                    goto out;
14f8ab
+                }
14f8ab
+
14f8ab
+                keylen = strlen(pair->key);
14f8ab
+                netword = hton32(keylen);
14f8ab
+                memcpy(buf, &netword, sizeof(netword));
14f8ab
+                buf += DICT_DATA_HDR_KEY_LEN;
14f8ab
+                if (!pair->value) {
14f8ab
+                    gf_msg("glusterd", GF_LOG_ERROR, 0, LG_MSG_NULL_PTR,
14f8ab
+                           "pair->value is null!");
14f8ab
+                    ret = -1;
14f8ab
+                    goto out;
14f8ab
+                }
14f8ab
+
14f8ab
+                netword = hton32(pair->value->len);
14f8ab
+                memcpy(buf, &netword, sizeof(netword));
14f8ab
+                buf += DICT_DATA_HDR_VAL_LEN;
14f8ab
+
14f8ab
+                memcpy(buf, pair->key, keylen);
14f8ab
+                buf += keylen;
14f8ab
+                *buf++ = '\0';
14f8ab
+
14f8ab
+                if (pair->value->data) {
14f8ab
+                    memcpy(buf, pair->value->data, pair->value->len);
14f8ab
+                    buf += pair->value->len;
14f8ab
+                }
14f8ab
+
14f8ab
+                pair = pair->next;
14f8ab
+                dict_count--;
14f8ab
+            }
14f8ab
+        }
14f8ab
+    }
14f8ab
+
14f8ab
+out:
14f8ab
+    for (i = 0; i < count; i++) {
14f8ab
+        if (dict_arr[i])
14f8ab
+            dict_unref(dict_arr[i]);
14f8ab
+    }
14f8ab
+    return ret;
14f8ab
+}
14f8ab
+
14f8ab
+int
14f8ab
+glusterd_dict_arr_serialize(dict_t *dict_arr[], int count, char **buf,
14f8ab
+                            u_int *length)
14f8ab
+{
14f8ab
+    ssize_t len = 0;
14f8ab
+    int i = 0;
14f8ab
+    int totcount = 0;
14f8ab
+    int ret = 0;
14f8ab
+
14f8ab
+    for (i = 0; i < count; i++) {
14f8ab
+        if (dict_arr[i]) {
14f8ab
+            len += dict_serialized_length_lk(dict_arr[i]);
14f8ab
+            totcount += dict_arr[i]->count;
14f8ab
+        }
14f8ab
+    }
14f8ab
+
14f8ab
+    // Subtract HDR_LEN except one dictionary
14f8ab
+    len = len - ((count - 1) * DICT_HDR_LEN);
14f8ab
+
14f8ab
+    *buf = GF_MALLOC(len, gf_common_mt_char);
14f8ab
+    if (*buf == NULL) {
14f8ab
+        ret = -ENOMEM;
14f8ab
+        goto out;
14f8ab
+    }
14f8ab
+
14f8ab
+    if (length != NULL) {
14f8ab
+        *length = len;
14f8ab
+    }
14f8ab
+
14f8ab
+    ret = glusterd_dict_searialize(dict_arr, count, totcount, *buf);
14f8ab
+
14f8ab
+out:
14f8ab
+    return ret;
14f8ab
+}
14f8ab
+
14f8ab
 int32_t
14f8ab
-glusterd_add_volumes_to_export_dict(dict_t **peer_data)
14f8ab
+glusterd_add_volumes_to_export_dict(dict_t *peer_data, char **buf,
14f8ab
+                                    u_int *length)
14f8ab
 {
14f8ab
     int32_t ret = -1;
14f8ab
-    dict_t *dict = NULL;
14f8ab
     dict_t *dict_arr[128] = {
14f8ab
         0,
14f8ab
     };
14f8ab
@@ -3496,10 +3603,6 @@ glusterd_add_volumes_to_export_dict(dict_t **peer_data)
14f8ab
     priv = this->private;
14f8ab
     GF_ASSERT(priv);
14f8ab
 
14f8ab
-    dict = dict_new();
14f8ab
-    if (!dict)
14f8ab
-        goto out;
14f8ab
-
14f8ab
     /* Count the total number of volumes */
14f8ab
     cds_list_for_each_entry(volinfo, &priv->volumes, vol_list) volcnt++;
14f8ab
 
14f8ab
@@ -3520,14 +3623,15 @@ glusterd_add_volumes_to_export_dict(dict_t **peer_data)
14f8ab
         cds_list_for_each_entry(volinfo, &priv->volumes, vol_list)
14f8ab
         {
14f8ab
             count++;
14f8ab
-            ret = glusterd_add_volume_to_dict(volinfo, dict, count, "volume");
14f8ab
+            ret = glusterd_add_volume_to_dict(volinfo, peer_data, count,
14f8ab
+                                              "volume");
14f8ab
             if (ret)
14f8ab
                 goto out;
14f8ab
 
14f8ab
             if (!dict_get_sizen(volinfo->dict, VKEY_FEATURES_QUOTA))
14f8ab
                 continue;
14f8ab
 
14f8ab
-            ret = glusterd_vol_add_quota_conf_to_dict(volinfo, dict, count,
14f8ab
+            ret = glusterd_vol_add_quota_conf_to_dict(volinfo, peer_data, count,
14f8ab
                                                       "volume");
14f8ab
             if (ret)
14f8ab
                 goto out;
14f8ab
@@ -3569,34 +3673,34 @@ glusterd_add_volumes_to_export_dict(dict_t **peer_data)
14f8ab
 
14f8ab
         gf_log(this->name, GF_LOG_INFO,
14f8ab
                "Finished dictionary popluation in all threads");
14f8ab
-        for (i = 0; i < totthread; i++) {
14f8ab
-            dict_copy_with_ref(dict_arr[i], dict);
14f8ab
-            dict_unref(dict_arr[i]);
14f8ab
-        }
14f8ab
-        gf_log(this->name, GF_LOG_INFO,
14f8ab
-               "Finished merger of all dictionraies into single one");
14f8ab
     }
14f8ab
 
14f8ab
-    ret = dict_set_int32n(dict, "count", SLEN("count"), volcnt);
14f8ab
+    ret = dict_set_int32n(peer_data, "count", SLEN("count"), volcnt);
14f8ab
     if (ret)
14f8ab
         goto out;
14f8ab
 
14f8ab
-    ctx.dict = dict;
14f8ab
+    ctx.dict = peer_data;
14f8ab
     ctx.prefix = "global";
14f8ab
     ctx.opt_count = 1;
14f8ab
     ctx.key_name = "key";
14f8ab
     ctx.val_name = "val";
14f8ab
     dict_foreach(priv->opts, _add_dict_to_prdict, &ctx;;
14f8ab
     ctx.opt_count--;
14f8ab
-    ret = dict_set_int32n(dict, "global-opt-count", SLEN("global-opt-count"),
14f8ab
-                          ctx.opt_count);
14f8ab
+    ret = dict_set_int32n(peer_data, "global-opt-count",
14f8ab
+                          SLEN("global-opt-count"), ctx.opt_count);
14f8ab
     if (ret)
14f8ab
         goto out;
14f8ab
 
14f8ab
-    *peer_data = dict;
14f8ab
+    if (totthread) {
14f8ab
+        gf_log(this->name, GF_LOG_INFO,
14f8ab
+               "Finished merger of all dictionraies into single one");
14f8ab
+        dict_arr[totthread++] = peer_data;
14f8ab
+        ret = glusterd_dict_arr_serialize(dict_arr, totthread, buf, length);
14f8ab
+        gf_log(this->name, GF_LOG_INFO,
14f8ab
+               "Serialize dictionary data return is %d", ret);
14f8ab
+    }
14f8ab
+
14f8ab
 out:
14f8ab
-    if (ret)
14f8ab
-        dict_unref(dict);
14f8ab
 
14f8ab
     gf_msg_trace(this->name, 0, "Returning %d", ret);
14f8ab
     return ret;
14f8ab
@@ -4940,6 +5044,7 @@ glusterd_import_friend_volumes_synctask(void *opaque)
14f8ab
     xlator_t *this = NULL;
14f8ab
     glusterd_conf_t *conf = NULL;
14f8ab
     dict_t *peer_data = NULL;
14f8ab
+    glusterd_friend_synctask_args_t *arg = NULL;
14f8ab
 
14f8ab
     this = THIS;
14f8ab
     GF_ASSERT(this);
14f8ab
@@ -4947,8 +5052,20 @@ glusterd_import_friend_volumes_synctask(void *opaque)
14f8ab
     conf = this->private;
14f8ab
     GF_ASSERT(conf);
14f8ab
 
14f8ab
-    peer_data = (dict_t *)opaque;
14f8ab
-    GF_ASSERT(peer_data);
14f8ab
+    arg = opaque;
14f8ab
+    if (!arg)
14f8ab
+        goto out;
14f8ab
+
14f8ab
+    peer_data = dict_new();
14f8ab
+    if (!peer_data) {
14f8ab
+        goto out;
14f8ab
+    }
14f8ab
+
14f8ab
+    ret = dict_unserialize(arg->dict_buf, arg->dictlen, &peer_data);
14f8ab
+    if (ret) {
14f8ab
+        errno = ENOMEM;
14f8ab
+        goto out;
14f8ab
+    }
14f8ab
 
14f8ab
     ret = dict_get_int32n(peer_data, "count", SLEN("count"), &count);
14f8ab
     if (ret)
14f8ab
@@ -4980,6 +5097,11 @@ glusterd_import_friend_volumes_synctask(void *opaque)
14f8ab
 out:
14f8ab
     if (peer_data)
14f8ab
         dict_unref(peer_data);
14f8ab
+    if (arg) {
14f8ab
+        if (arg->dict_buf)
14f8ab
+            GF_FREE(arg->dict_buf);
14f8ab
+        GF_FREE(arg);
14f8ab
+    }
14f8ab
 
14f8ab
     gf_msg_debug("glusterd", 0, "Returning with %d", ret);
14f8ab
     return ret;
14f8ab
@@ -5146,7 +5268,7 @@ glusterd_compare_friend_data(dict_t *peer_data, int32_t *status, char *hostname)
14f8ab
     gf_boolean_t update = _gf_false;
14f8ab
     xlator_t *this = NULL;
14f8ab
     glusterd_conf_t *priv = NULL;
14f8ab
-    dict_t *peer_data_copy = NULL;
14f8ab
+    glusterd_friend_synctask_args_t *arg = NULL;
14f8ab
 
14f8ab
     this = THIS;
14f8ab
     GF_ASSERT(this);
14f8ab
@@ -5188,12 +5310,23 @@ glusterd_compare_friend_data(dict_t *peer_data, int32_t *status, char *hostname)
14f8ab
          * first brick to come up before attaching the subsequent bricks
14f8ab
          * in case brick multiplexing is enabled
14f8ab
          */
14f8ab
-        peer_data_copy = dict_copy_with_ref(peer_data, NULL);
14f8ab
-        glusterd_launch_synctask(glusterd_import_friend_volumes_synctask,
14f8ab
-                                 peer_data_copy);
14f8ab
+        arg = GF_CALLOC(1, sizeof(*arg), gf_common_mt_char);
14f8ab
+        ret = dict_allocate_and_serialize(peer_data, &arg->dict_buf,
14f8ab
+                                          &arg->dictlen);
14f8ab
+        if (ret < 0) {
14f8ab
+            gf_log(this->name, GF_LOG_ERROR,
14f8ab
+                   "dict_serialize failed while handling "
14f8ab
+                   " import friend volume request");
14f8ab
+            goto out;
14f8ab
+        }
14f8ab
+
14f8ab
+        glusterd_launch_synctask(glusterd_import_friend_volumes_synctask, arg);
14f8ab
     }
14f8ab
 
14f8ab
 out:
14f8ab
+    if (ret && arg) {
14f8ab
+        GF_FREE(arg);
14f8ab
+    }
14f8ab
     gf_msg_debug(this->name, 0, "Returning with ret: %d, status: %d", ret,
14f8ab
                  *status);
14f8ab
     return ret;
14f8ab
diff --git a/xlators/mgmt/glusterd/src/glusterd-utils.h b/xlators/mgmt/glusterd/src/glusterd-utils.h
14f8ab
index 3647c34..6ad8062 100644
14f8ab
--- a/xlators/mgmt/glusterd/src/glusterd-utils.h
14f8ab
+++ b/xlators/mgmt/glusterd/src/glusterd-utils.h
14f8ab
@@ -227,7 +227,8 @@ glusterd_volume_brickinfo_get_by_brick(char *brick, glusterd_volinfo_t *volinfo,
14f8ab
                                        gf_boolean_t construct_real_path);
14f8ab
 
14f8ab
 int32_t
14f8ab
-glusterd_add_volumes_to_export_dict(dict_t **peer_data);
14f8ab
+glusterd_add_volumes_to_export_dict(dict_t *peer_data, char **buf,
14f8ab
+                                    u_int *length);
14f8ab
 
14f8ab
 int32_t
14f8ab
 glusterd_compare_friend_data(dict_t *peer_data, int32_t *status,
14f8ab
diff --git a/xlators/mgmt/glusterd/src/glusterd.h b/xlators/mgmt/glusterd/src/glusterd.h
14f8ab
index 2ea8560..f96bca3 100644
14f8ab
--- a/xlators/mgmt/glusterd/src/glusterd.h
14f8ab
+++ b/xlators/mgmt/glusterd/src/glusterd.h
14f8ab
@@ -240,6 +240,11 @@ typedef struct glusterd_add_dict_args {
14f8ab
     int end;
14f8ab
 } glusterd_add_dict_args_t;
14f8ab
 
14f8ab
+typedef struct glusterd_friend_synctask_args {
14f8ab
+    char *dict_buf;
14f8ab
+    u_int dictlen;
14f8ab
+} glusterd_friend_synctask_args_t;
14f8ab
+
14f8ab
 typedef enum gf_brick_status {
14f8ab
     GF_BRICK_STOPPED,
14f8ab
     GF_BRICK_STARTED,
14f8ab
-- 
14f8ab
1.8.3.1
14f8ab