d1681e
From 4833168455511785d05f5d20b47644cbda9066cb Mon Sep 17 00:00:00 2001
d1681e
From: Mohit Agrawal <moagrawal@redhat.com>
d1681e
Date: Fri, 24 Aug 2018 10:44:59 +0530
d1681e
Subject: [PATCH 361/362] ctr: skip ctr xlator init if ctr is not enabled
d1681e
d1681e
Problem: If ctr xlator is not required it consumes
d1681e
         resources unnecessarily
d1681e
d1681e
Solution: Call ctr xlator init only while feature is enabled
d1681e
d1681e
> Fixes: bz#1524323
d1681e
d1681e
> Change-Id: I378113a390a286be20c4ade1b1bac170a8ef1b14
d1681e
> (Cherry pick from commit 30e46a9b3cef868e5c781044c99c3d5b066d4760)
d1681e
> (Reviewed on upstream link 30e46a9b3cef868e5c781044c99c3d5b066d4760)
d1681e
d1681e
Change-Id: Ie65c89bf27d2f119fa41cfc143f1f828f9d64b62
d1681e
BUG: 1524336
d1681e
Signed-off-by: Mohit Agrawal <moagrawal@redhat.com>
d1681e
Reviewed-on: https://code.engineering.redhat.com/gerrit/148134
d1681e
Tested-by: Mohit Agrawal <moagrawa@redhat.com>
d1681e
Reviewed-by: Atin Mukherjee <amukherj@redhat.com>
d1681e
---
d1681e
 tests/basic/tier/tier_lookup_heal.t                |   6 -
d1681e
 .../changetimerecorder/src/changetimerecorder.c    | 192 +++++++++++++--------
d1681e
 .../features/changetimerecorder/src/ctr-helper.h   |   2 +-
d1681e
 3 files changed, 120 insertions(+), 80 deletions(-)
d1681e
d1681e
diff --git a/tests/basic/tier/tier_lookup_heal.t b/tests/basic/tier/tier_lookup_heal.t
d1681e
index 7dac1fd..8f8292c 100755
d1681e
--- a/tests/basic/tier/tier_lookup_heal.t
d1681e
+++ b/tests/basic/tier/tier_lookup_heal.t
d1681e
@@ -36,12 +36,6 @@ TEST stat .
d1681e
 TEST touch file1
d1681e
 TEST stat file1
d1681e
 
d1681e
-# gf_file_tb and gf_flink_tb should be empty
d1681e
-ENTRY_COUNT=$(echo "select * from gf_file_tb; select * from gf_flink_tb;" | \
d1681e
-        sqlite3 $B0/${V0}$LAST_BRICK/.glusterfs/${V0}$LAST_BRICK.db | wc -l )
d1681e
-TEST [ $ENTRY_COUNT -eq 0 ]
d1681e
-
d1681e
-
d1681e
 #Attach tier and switch ON CTR Xlator.
d1681e
 TEST $CLI volume attach-tier $V0 replica 2 $H0:$B0/${V0}$CACHE_BRICK_FIRST $H0:$B0/${V0}$CACHE_BRICK_LAST
d1681e
 TEST $CLI volume set $V0 features.ctr-enabled on
d1681e
diff --git a/xlators/features/changetimerecorder/src/changetimerecorder.c b/xlators/features/changetimerecorder/src/changetimerecorder.c
d1681e
index 5f82d33..9d4f8a3 100644
d1681e
--- a/xlators/features/changetimerecorder/src/changetimerecorder.c
d1681e
+++ b/xlators/features/changetimerecorder/src/changetimerecorder.c
d1681e
@@ -2086,6 +2086,84 @@ out:
d1681e
 }
d1681e
 
d1681e
 
d1681e
+/* Call to initialize db for ctr xlator while ctr is enabled */
d1681e
+int32_t
d1681e
+initialize_ctr_resource (xlator_t *this, gf_ctr_private_t *priv)
d1681e
+{
d1681e
+        int ret_db              = -1;
d1681e
+        dict_t *params_dict      = NULL;
d1681e
+
d1681e
+        if (!priv)
d1681e
+                goto error;
d1681e
+
d1681e
+        /* For compaction */
d1681e
+        priv->compact_active = _gf_false;
d1681e
+        priv->compact_mode_switched = _gf_false;
d1681e
+        ret_db = pthread_mutex_init (&priv->compact_lock, NULL);
d1681e
+
d1681e
+        if (ret_db) {
d1681e
+                gf_msg (this->name, GF_LOG_ERROR, 0,
d1681e
+                        CTR_MSG_FATAL_ERROR,
d1681e
+                        "FATAL: Failed initializing compaction mutex");
d1681e
+                goto error;
d1681e
+        }
d1681e
+
d1681e
+        params_dict = dict_new ();
d1681e
+        if (!params_dict) {
d1681e
+                gf_msg (this->name, GF_LOG_ERROR, 0,
d1681e
+                        CTR_MSG_INIT_DB_PARAMS_FAILED,
d1681e
+                        "DB Params cannot initialized!");
d1681e
+                goto error;
d1681e
+        }
d1681e
+
d1681e
+        /*Extract db params options*/
d1681e
+        ret_db = extract_db_params(this, params_dict, priv->gfdb_db_type);
d1681e
+        if (ret_db) {
d1681e
+                gf_msg (this->name, GF_LOG_ERROR, 0,
d1681e
+                        CTR_MSG_EXTRACT_DB_PARAM_OPTIONS_FAILED,
d1681e
+                        "Failed extracting db params options");
d1681e
+                goto error;
d1681e
+        }
d1681e
+
d1681e
+        /*Create a memory pool for ctr xlator*/
d1681e
+        this->local_pool = mem_pool_new (gf_ctr_local_t, 64);
d1681e
+        if (!this->local_pool) {
d1681e
+                gf_msg (this->name, GF_LOG_ERROR, 0,
d1681e
+                        CTR_MSG_CREATE_LOCAL_MEMORY_POOL_FAILED,
d1681e
+                        "failed to create local memory pool");
d1681e
+                ret_db = -1;
d1681e
+                goto error;
d1681e
+        }
d1681e
+
d1681e
+        /*Initialize Database Connection*/
d1681e
+        priv->_db_conn = init_db(params_dict, priv->gfdb_db_type);
d1681e
+        if (!priv->_db_conn) {
d1681e
+                gf_msg (this->name, GF_LOG_ERROR, 0,
d1681e
+                        CTR_MSG_FATAL_ERROR,
d1681e
+                        "FATAL: Failed initializing data base");
d1681e
+                ret_db = -1;
d1681e
+                goto error;
d1681e
+        }
d1681e
+
d1681e
+        ret_db = 0;
d1681e
+        goto out;
d1681e
+
d1681e
+error:
d1681e
+        if (this)
d1681e
+                mem_pool_destroy (this->local_pool);
d1681e
+
d1681e
+        if (priv) {
d1681e
+                GF_FREE (priv->ctr_db_path);
d1681e
+        }
d1681e
+        GF_FREE (priv);
d1681e
+
d1681e
+out:
d1681e
+        if (params_dict)
d1681e
+                dict_unref (params_dict);
d1681e
+
d1681e
+        return ret_db;
d1681e
+}
d1681e
+
d1681e
 /******************************************************************************/
d1681e
 int
d1681e
 reconfigure (xlator_t *this, dict_t *options)
d1681e
@@ -2095,6 +2173,7 @@ reconfigure (xlator_t *this, dict_t *options)
d1681e
         gf_ctr_private_t *priv = NULL;
d1681e
 
d1681e
         priv = this->private;
d1681e
+
d1681e
         if (dict_get_str(options, "changetimerecorder.frequency",
d1681e
                          &temp_str)) {
d1681e
                 gf_msg(this->name, GF_LOG_TRACE, 0, CTR_MSG_SET, "set");
d1681e
@@ -2102,6 +2181,26 @@ reconfigure (xlator_t *this, dict_t *options)
d1681e
 
d1681e
         GF_OPTION_RECONF ("ctr-enabled", priv->enabled, options,
d1681e
                           bool, out);
d1681e
+        if (!priv->enabled) {
d1681e
+                gf_msg (GFDB_DATA_STORE, GF_LOG_INFO, 0,
d1681e
+                        CTR_MSG_XLATOR_DISABLED,
d1681e
+                        "CTR Xlator is not enabled so skip ctr reconfigure");
d1681e
+                goto out;
d1681e
+        }
d1681e
+
d1681e
+        /* If ctr is enabled after skip init for ctr xlator then call
d1681e
+           initialize_ctr_resource during reconfigure phase to allocate resources for
d1681e
+           xlator
d1681e
+        */
d1681e
+        if (priv->enabled && !priv->_db_conn) {
d1681e
+                ret = initialize_ctr_resource (this, priv);
d1681e
+                if (ret)  {
d1681e
+                        gf_msg (this->name, GF_LOG_ERROR, 0,
d1681e
+                                CTR_MSG_FATAL_ERROR,
d1681e
+                                "FATAL: Failed ctr initialize resource");
d1681e
+                        goto out;
d1681e
+                }
d1681e
+        }
d1681e
 
d1681e
         GF_OPTION_RECONF ("record-counters", priv->ctr_record_counter, options,
d1681e
                           bool, out);
d1681e
@@ -2174,15 +2273,19 @@ init (xlator_t *this)
d1681e
 {
d1681e
         gf_ctr_private_t *priv = NULL;
d1681e
         int ret_db              = -1;
d1681e
-        dict_t *params_dict      = NULL;
d1681e
 
d1681e
-        GF_VALIDATE_OR_GOTO ("ctr", this, error);
d1681e
+        if (!this) {
d1681e
+                gf_msg (this->name, GF_LOG_ERROR, 0,
d1681e
+                        CTR_MSG_FATAL_ERROR,
d1681e
+                        "FATAL: ctr this is not initialized");
d1681e
+                return -1;
d1681e
+        }
d1681e
 
d1681e
         if (!this->children || this->children->next) {
d1681e
                 gf_msg (this->name, GF_LOG_ERROR, 0,
d1681e
                         CTR_MSG_FATAL_ERROR,
d1681e
                         "FATAL: ctr should have exactly one child");
d1681e
-                goto error;
d1681e
+                return -1;
d1681e
         }
d1681e
 
d1681e
         if (!this->parents) {
d1681e
@@ -2196,7 +2299,7 @@ init (xlator_t *this)
d1681e
                 gf_msg (this->name, GF_LOG_ERROR, ENOMEM,
d1681e
                         CTR_MSG_CALLOC_FAILED,
d1681e
                         "Calloc did not work!!!");
d1681e
-                goto error;
d1681e
+                return -1;
d1681e
         }
d1681e
 
d1681e
         /*Default values for the translator*/
d1681e
@@ -2205,94 +2308,37 @@ init (xlator_t *this)
d1681e
         priv->ctr_hot_brick            = _gf_false;
d1681e
         priv->gfdb_db_type             = GFDB_SQLITE3;
d1681e
         priv->gfdb_sync_type           = GFDB_DB_SYNC;
d1681e
-        priv->enabled                  = _gf_true;
d1681e
         priv->_db_conn                 = NULL;
d1681e
         priv->ctr_lookupheal_link_timeout =
d1681e
                                 CTR_DEFAULT_HARDLINK_EXP_PERIOD;
d1681e
         priv->ctr_lookupheal_inode_timeout =
d1681e
                                 CTR_DEFAULT_INODE_EXP_PERIOD;
d1681e
 
d1681e
-        /* For compaction */
d1681e
-        priv->compact_active = _gf_false;
d1681e
-        priv->compact_mode_switched = _gf_false;
d1681e
-        ret_db = pthread_mutex_init (&priv->compact_lock, NULL);
d1681e
-
d1681e
-        if (ret_db) {
d1681e
-                gf_msg (this->name, GF_LOG_ERROR, 0,
d1681e
-                        CTR_MSG_FATAL_ERROR,
d1681e
-                        "FATAL: Failed initializing compaction mutex");
d1681e
-                goto error;
d1681e
-        }
d1681e
-
d1681e
         /*Extract ctr xlator options*/
d1681e
         ret_db = extract_ctr_options (this, priv);
d1681e
         if (ret_db) {
d1681e
                 gf_msg (this->name, GF_LOG_ERROR, 0,
d1681e
                         CTR_MSG_EXTRACT_CTR_XLATOR_OPTIONS_FAILED,
d1681e
                         "Failed extracting ctr xlator options");
d1681e
-                goto error;
d1681e
+                return -1;
d1681e
         }
d1681e
 
d1681e
-        params_dict = dict_new ();
d1681e
-        if (!params_dict) {
d1681e
-                gf_msg (this->name, GF_LOG_ERROR, 0,
d1681e
-                        CTR_MSG_INIT_DB_PARAMS_FAILED,
d1681e
-                        "DB Params cannot initialized!");
d1681e
-                goto error;
d1681e
-        }
d1681e
-
d1681e
-        /*Extract db params options*/
d1681e
-        ret_db = extract_db_params(this, params_dict, priv->gfdb_db_type);
d1681e
-        if (ret_db) {
d1681e
-                gf_msg (this->name, GF_LOG_ERROR, 0,
d1681e
-                        CTR_MSG_EXTRACT_DB_PARAM_OPTIONS_FAILED,
d1681e
-                        "Failed extracting db params options");
d1681e
-                goto error;
d1681e
+        if (!priv->enabled) {
d1681e
+                gf_msg (GFDB_DATA_STORE, GF_LOG_INFO, 0,
d1681e
+                        CTR_MSG_XLATOR_DISABLED,
d1681e
+                        "CTR Xlator is not enabled so skip ctr init");
d1681e
+                goto out;
d1681e
         }
d1681e
 
d1681e
-        /*Create a memory pool for ctr xlator*/
d1681e
-        this->local_pool = mem_pool_new (gf_ctr_local_t, 64);
d1681e
-        if (!this->local_pool) {
d1681e
+        ret_db = initialize_ctr_resource (this, priv);
d1681e
+        if (ret_db)  {
d1681e
                 gf_msg (this->name, GF_LOG_ERROR, 0,
d1681e
-                        CTR_MSG_CREATE_LOCAL_MEMORY_POOL_FAILED,
d1681e
-                        "failed to create local memory pool");
d1681e
-                goto error;
d1681e
-        }
d1681e
-
d1681e
-        /*Initialize Database Connection*/
d1681e
-        priv->_db_conn = init_db(params_dict, priv->gfdb_db_type);
d1681e
-        if (!priv->_db_conn) {
d1681e
-                gf_msg (this->name, GF_LOG_ERROR, 0,
d1681e
-                       CTR_MSG_FATAL_ERROR,
d1681e
-                       "FATAL: Failed initializing data base");
d1681e
-                        goto error;
d1681e
-        }
d1681e
-
d1681e
-
d1681e
-        ret_db = 0;
d1681e
-        goto out;
d1681e
-
d1681e
-/*Error handling */
d1681e
-error:
d1681e
-
d1681e
-        if (this)
d1681e
-                mem_pool_destroy (this->local_pool);
d1681e
-
d1681e
-        if (priv) {
d1681e
-                GF_FREE (priv->ctr_db_path);
d1681e
+                        CTR_MSG_FATAL_ERROR,
d1681e
+                        "FATAL: Failed ctr initialize resource");
d1681e
+                return -1;
d1681e
         }
d1681e
-        GF_FREE (priv);
d1681e
-
d1681e
-        if (params_dict)
d1681e
-                dict_unref (params_dict);
d1681e
-
d1681e
-        return -1;
d1681e
 
d1681e
 out:
d1681e
-
d1681e
-        if (params_dict)
d1681e
-                dict_unref (params_dict);
d1681e
-
d1681e
         this->private = (void *)priv;
d1681e
         return 0;
d1681e
 }
d1681e
@@ -2343,7 +2389,7 @@ fini (xlator_t *this)
d1681e
 
d1681e
         priv = this->private;
d1681e
 
d1681e
-        if (priv) {
d1681e
+        if (priv && priv->enabled) {
d1681e
                 if (fini_db (priv->_db_conn)) {
d1681e
                         gf_msg (this->name, GF_LOG_WARNING, 0,
d1681e
                                 CTR_MSG_CLOSE_DB_CONN_FAILED, "Failed closing "
d1681e
diff --git a/xlators/features/changetimerecorder/src/ctr-helper.h b/xlators/features/changetimerecorder/src/ctr-helper.h
d1681e
index 4fd4f74..f0b0dce 100644
d1681e
--- a/xlators/features/changetimerecorder/src/ctr-helper.h
d1681e
+++ b/xlators/features/changetimerecorder/src/ctr-helper.h
d1681e
@@ -408,7 +408,7 @@ do {\
d1681e
         GF_ASSERT (this);\
d1681e
         GF_ASSERT (this->private);\
d1681e
         _priv = this->private;\
d1681e
-        if (!_priv->enabled)\
d1681e
+        if (!_priv->_db_conn)\
d1681e
                 goto label;\
d1681e
  } while (0)
d1681e
 
d1681e
-- 
d1681e
1.8.3.1
d1681e