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