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