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