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