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