Blame SOURCES/0013-Ticket-49076-To-debug-DB_DEADLOCK-condition-allow-to.patch

74ca47
From 1a66f5f232d6c2869ef4e439eafe5a820f61a976 Mon Sep 17 00:00:00 2001
74ca47
From: Thierry Bordaz <tbordaz@redhat.com>
74ca47
Date: Wed, 15 Feb 2017 11:31:27 +0100
74ca47
Subject: [PATCH] Ticket 49076 - To debug DB_DEADLOCK condition, allow to reset
74ca47
 DB_TXN_NOWAIT flag on txn_begin
74ca47
74ca47
Bug Description:
74ca47
    For debug reason it is interesting to have a new configuration ldbm backend config
74ca47
    option (nsslapd-db-transaction-wait) that allows to hang on deadlock
74ca47
    rather to let the server handling retries.
74ca47
74ca47
Fix Description:
74ca47
    The fix introduce a new attribute nsslapd-db-transaction-wait under
74ca47
    "cn=config,cn=ldbm database,cn=plugins,cn=config".
74ca47
    By default it is "off" (ldbm returns DB_DEADLOCK) and can be changed
74ca47
    online.
74ca47
    It is taken into account when a new transcation begin.
74ca47
74ca47
https://pagure.io/389-ds-base/issue/49076
74ca47
74ca47
Reviewed by: William Brown, Ludwig Krispenz
74ca47
74ca47
Platforms tested: F23
74ca47
74ca47
Flag Day: no
74ca47
74ca47
Doc impact: no
74ca47
---
74ca47
 dirsrvtests/tests/tickets/ticket49076_test.py | 103 ++++++++++++++++++++++++++
74ca47
 ldap/servers/slapd/back-ldbm/dblayer.c        |   9 ++-
74ca47
 ldap/servers/slapd/back-ldbm/dblayer.h        |   3 +
74ca47
 ldap/servers/slapd/back-ldbm/ldbm_config.c    |  22 ++++++
74ca47
 ldap/servers/slapd/back-ldbm/ldbm_config.h    |   1 +
74ca47
 5 files changed, 137 insertions(+), 1 deletion(-)
74ca47
 create mode 100644 dirsrvtests/tests/tickets/ticket49076_test.py
74ca47
74ca47
diff --git a/dirsrvtests/tests/tickets/ticket49076_test.py b/dirsrvtests/tests/tickets/ticket49076_test.py
74ca47
new file mode 100644
74ca47
index 0000000..c4a2c1b
74ca47
--- /dev/null
74ca47
+++ b/dirsrvtests/tests/tickets/ticket49076_test.py
74ca47
@@ -0,0 +1,103 @@
74ca47
+import time
74ca47
+import ldap
74ca47
+import logging
74ca47
+import pytest
74ca47
+from lib389 import DirSrv, Entry, tools, tasks
74ca47
+from lib389.tools import DirSrvTools
74ca47
+from lib389._constants import *
74ca47
+from lib389.properties import *
74ca47
+from lib389.tasks import *
74ca47
+from lib389.utils import *
74ca47
+from lib389.topologies import topology_st as topo
74ca47
+
74ca47
+DEBUGGING = os.getenv("DEBUGGING", default=False)
74ca47
+if DEBUGGING:
74ca47
+    logging.getLogger(__name__).setLevel(logging.DEBUG)
74ca47
+else:
74ca47
+    logging.getLogger(__name__).setLevel(logging.INFO)
74ca47
+log = logging.getLogger(__name__)
74ca47
+
74ca47
+ldbm_config = "cn=config,%s" % (DN_LDBM)
74ca47
+txn_begin_flag = "nsslapd-db-transaction-wait"
74ca47
+TEST_USER_DN = 'cn=test,%s' % SUFFIX
74ca47
+TEST_USER = "test"
74ca47
+
74ca47
+def _check_configured_value(topology_st, attr=txn_begin_flag, expected_value=None, required=False):
74ca47
+    entries = topology_st.standalone.search_s(ldbm_config, ldap.SCOPE_BASE, 'cn=config')
74ca47
+    if required:
74ca47
+        assert (entries[0].hasValue(attr))
74ca47
+    if entries[0].hasValue(attr):
74ca47
+        topology_st.standalone.log.info('Current value is %s' % entries[0].getValue(attr))
74ca47
+        assert (entries[0].getValue(attr) == expected_value)
74ca47
+        
74ca47
+def _update_db(topology_st):
74ca47
+    topology_st.standalone.add_s(
74ca47
+        Entry((TEST_USER_DN, {'objectclass': "top person organizationalPerson inetOrgPerson".split(),
74ca47
+                              'cn': TEST_USER,
74ca47
+                              'sn': TEST_USER,
74ca47
+                              'givenname': TEST_USER})))
74ca47
+    topology_st.standalone.delete_s(TEST_USER_DN)
74ca47
+
74ca47
+def test_ticket49076(topo):
74ca47
+    """Write your testcase here...
74ca47
+
74ca47
+    Also, if you need any testcase initialization,
74ca47
+    please, write additional fixture for that(include finalizer).
74ca47
+    """
74ca47
+    
74ca47
+    # check default value is DB_TXN_NOWAIT
74ca47
+    _check_configured_value(topo, expected_value="off")
74ca47
+    
74ca47
+    # tests we are able to update DB
74ca47
+    _update_db(topo)
74ca47
+    
74ca47
+    # switch to wait mode
74ca47
+    topo.standalone.modify_s(ldbm_config,
74ca47
+                                    [(ldap.MOD_REPLACE, txn_begin_flag, "on")])
74ca47
+                                    # check default value is DB_TXN_NOWAIT
74ca47
+    _check_configured_value(topo, expected_value="on")
74ca47
+    _update_db(topo)
74ca47
+    
74ca47
+    
74ca47
+    # switch back to "normal mode"
74ca47
+    topo.standalone.modify_s(ldbm_config,
74ca47
+                                    [(ldap.MOD_REPLACE, txn_begin_flag, "off")])
74ca47
+    # check default value is DB_TXN_NOWAIT
74ca47
+    _check_configured_value(topo, expected_value="off")
74ca47
+    # tests we are able to update DB
74ca47
+    _update_db(topo)
74ca47
+    
74ca47
+    # check that settings are not reset by restart
74ca47
+    topo.standalone.modify_s(ldbm_config,
74ca47
+                                    [(ldap.MOD_REPLACE, txn_begin_flag, "on")])
74ca47
+                                    # check default value is DB_TXN_NOWAIT
74ca47
+    _check_configured_value(topo, expected_value="on")
74ca47
+    _update_db(topo)
74ca47
+    topo.standalone.restart(timeout=10)
74ca47
+    _check_configured_value(topo, expected_value="on")
74ca47
+    _update_db(topo)
74ca47
+    
74ca47
+    # switch default value
74ca47
+    topo.standalone.modify_s(ldbm_config,
74ca47
+                                    [(ldap.MOD_DELETE, txn_begin_flag, None)])
74ca47
+    # check default value is DB_TXN_NOWAIT
74ca47
+    _check_configured_value(topo, expected_value="off")
74ca47
+    # tests we are able to update DB
74ca47
+    _update_db(topo)
74ca47
+    topo.standalone.restart(timeout=10)
74ca47
+    _check_configured_value(topo, expected_value="off")
74ca47
+    # tests we are able to update DB
74ca47
+    _update_db(topo)    
74ca47
+                              
74ca47
+
74ca47
+    if DEBUGGING:
74ca47
+        # Add debugging steps(if any)...
74ca47
+        pass
74ca47
+
74ca47
+
74ca47
+if __name__ == '__main__':
74ca47
+    # Run isolated
74ca47
+    # -s for DEBUG mode
74ca47
+    CURRENT_FILE = os.path.realpath(__file__)
74ca47
+    pytest.main("-s %s" % CURRENT_FILE)
74ca47
+
74ca47
diff --git a/ldap/servers/slapd/back-ldbm/dblayer.c b/ldap/servers/slapd/back-ldbm/dblayer.c
74ca47
index 683994f..507a3cc 100644
74ca47
--- a/ldap/servers/slapd/back-ldbm/dblayer.c
74ca47
+++ b/ldap/servers/slapd/back-ldbm/dblayer.c
74ca47
@@ -3374,6 +3374,8 @@ dblayer_txn_begin_ext(struct ldbminfo *li, back_txnid parent_txn, back_txn *txn,
74ca47
 
74ca47
     if (priv->dblayer_enable_transactions)
74ca47
     {
74ca47
+        int txn_begin_flags;
74ca47
+
74ca47
         dblayer_private_env *pEnv = priv->dblayer_env;
74ca47
         if(use_lock) slapi_rwlock_rdlock(pEnv->dblayer_env_lock);
74ca47
         if (!parent_txn)
74ca47
@@ -3383,11 +3385,16 @@ dblayer_txn_begin_ext(struct ldbminfo *li, back_txnid parent_txn, back_txn *txn,
74ca47
             if (par_txn_txn) {
74ca47
                 parent_txn = par_txn_txn->back_txn_txn;
74ca47
             }
74ca47
+                }
74ca47
+        if (priv->dblayer_txn_wait) {
74ca47
+                txn_begin_flags = 0;
74ca47
+        } else {
74ca47
+                txn_begin_flags = DB_TXN_NOWAIT;
74ca47
         }
74ca47
         return_value = TXN_BEGIN(pEnv->dblayer_DB_ENV,
74ca47
                                  (DB_TXN*)parent_txn,
74ca47
                                  &new_txn.back_txn_txn,
74ca47
-                                 DB_TXN_NOWAIT);
74ca47
+                                 txn_begin_flags);
74ca47
         if (0 != return_value) 
74ca47
         {
74ca47
             if(use_lock) slapi_rwlock_unlock(priv->dblayer_env->dblayer_env_lock);
74ca47
diff --git a/ldap/servers/slapd/back-ldbm/dblayer.h b/ldap/servers/slapd/back-ldbm/dblayer.h
74ca47
index e02e6e0..e4307fc 100644
74ca47
--- a/ldap/servers/slapd/back-ldbm/dblayer.h
74ca47
+++ b/ldap/servers/slapd/back-ldbm/dblayer.h
74ca47
@@ -104,6 +104,9 @@ struct dblayer_private
74ca47
                                         * the mpool */
74ca47
     int dblayer_recovery_required;
74ca47
     int dblayer_enable_transactions;
74ca47
+    int dblayer_txn_wait;           /* Default is "off" (DB_TXN_NOWAIT) but for
74ca47
+                                     * support purpose it could be helpful to set
74ca47
+                                     * "on" so that backend hang on deadlock */
74ca47
     int dblayer_durable_transactions;
74ca47
     int dblayer_checkpoint_interval;
74ca47
     int dblayer_circular_logging;
74ca47
diff --git a/ldap/servers/slapd/back-ldbm/ldbm_config.c b/ldap/servers/slapd/back-ldbm/ldbm_config.c
74ca47
index 8541224..dfe7a13 100644
74ca47
--- a/ldap/servers/slapd/back-ldbm/ldbm_config.c
74ca47
+++ b/ldap/servers/slapd/back-ldbm/ldbm_config.c
74ca47
@@ -636,6 +636,27 @@ static int ldbm_config_db_transaction_logging_set(void *arg, void *value, char *
74ca47
     return retval;
74ca47
 }
74ca47
 
74ca47
+
74ca47
+static void *ldbm_config_db_transaction_wait_get(void *arg)
74ca47
+{
74ca47
+    struct ldbminfo *li = (struct ldbminfo *) arg;
74ca47
+
74ca47
+    return (void *) ((uintptr_t)li->li_dblayer_private->dblayer_txn_wait);
74ca47
+}
74ca47
+
74ca47
+static int ldbm_config_db_transaction_wait_set(void *arg, void *value, char *errorbuf, int phase, int apply)
74ca47
+{
74ca47
+    struct ldbminfo *li = (struct ldbminfo *) arg;
74ca47
+    int retval = LDAP_SUCCESS;
74ca47
+    int val = (int) ((uintptr_t)value);
74ca47
+
74ca47
+    if (apply) {
74ca47
+        li->li_dblayer_private->dblayer_txn_wait = val;
74ca47
+    }
74ca47
+
74ca47
+    return retval;
74ca47
+}
74ca47
+
74ca47
 static void *ldbm_config_db_logbuf_size_get(void *arg) 
74ca47
 {
74ca47
     struct ldbminfo *li = (struct ldbminfo *) arg;
74ca47
@@ -1517,6 +1538,7 @@ static config_info ldbm_config[] = {
74ca47
     {CONFIG_DB_DURABLE_TRANSACTIONS, CONFIG_TYPE_ONOFF, "on", &ldbm_config_db_durable_transactions_get, &ldbm_config_db_durable_transactions_set, CONFIG_FLAG_ALWAYS_SHOW},
74ca47
     {CONFIG_DB_CIRCULAR_LOGGING, CONFIG_TYPE_ONOFF, "on", &ldbm_config_db_circular_logging_get, &ldbm_config_db_circular_logging_set, 0},
74ca47
     {CONFIG_DB_TRANSACTION_LOGGING, CONFIG_TYPE_ONOFF, "on", &ldbm_config_db_transaction_logging_get, &ldbm_config_db_transaction_logging_set, 0},
74ca47
+    {CONFIG_DB_TRANSACTION_WAIT, CONFIG_TYPE_ONOFF, "off", &ldbm_config_db_transaction_wait_get, &ldbm_config_db_transaction_wait_set, CONFIG_FLAG_ALWAYS_SHOW|CONFIG_FLAG_ALLOW_RUNNING_CHANGE},
74ca47
     {CONFIG_DB_CHECKPOINT_INTERVAL, CONFIG_TYPE_INT, "60", &ldbm_config_db_checkpoint_interval_get, &ldbm_config_db_checkpoint_interval_set, CONFIG_FLAG_ALWAYS_SHOW|CONFIG_FLAG_ALLOW_RUNNING_CHANGE},
74ca47
     {CONFIG_DB_COMPACTDB_INTERVAL, CONFIG_TYPE_INT, "2592000"/*30days*/, &ldbm_config_db_compactdb_interval_get, &ldbm_config_db_compactdb_interval_set, CONFIG_FLAG_ALWAYS_SHOW|CONFIG_FLAG_ALLOW_RUNNING_CHANGE},
74ca47
     {CONFIG_DB_TRANSACTION_BATCH, CONFIG_TYPE_INT, "0", &dblayer_get_batch_transactions, &dblayer_set_batch_transactions, CONFIG_FLAG_ALWAYS_SHOW|CONFIG_FLAG_ALLOW_RUNNING_CHANGE},
74ca47
diff --git a/ldap/servers/slapd/back-ldbm/ldbm_config.h b/ldap/servers/slapd/back-ldbm/ldbm_config.h
74ca47
index f481937..ddec3a8 100644
74ca47
--- a/ldap/servers/slapd/back-ldbm/ldbm_config.h
74ca47
+++ b/ldap/servers/slapd/back-ldbm/ldbm_config.h
74ca47
@@ -80,6 +80,7 @@ struct config_info {
74ca47
 #define CONFIG_DB_DURABLE_TRANSACTIONS "nsslapd-db-durable-transaction"
74ca47
 #define CONFIG_DB_CIRCULAR_LOGGING "nsslapd-db-circular-logging"
74ca47
 #define CONFIG_DB_TRANSACTION_LOGGING "nsslapd-db-transaction-logging"
74ca47
+#define CONFIG_DB_TRANSACTION_WAIT "nsslapd-db-transaction-wait"
74ca47
 #define CONFIG_DB_CHECKPOINT_INTERVAL "nsslapd-db-checkpoint-interval"
74ca47
 #define CONFIG_DB_COMPACTDB_INTERVAL "nsslapd-db-compactdb-interval"
74ca47
 #define CONFIG_DB_TRANSACTION_BATCH  "nsslapd-db-transaction-batch-val"
74ca47
-- 
74ca47
2.9.3
74ca47