andykimpe / rpms / 389-ds-base

Forked from rpms/389-ds-base 5 months ago
Clone

Blame SOURCES/0003-Issue-5413-Allow-mutliple-MemberOf-fixup-tasks-with-.patch

3d0f51
From 99dbba52eb45628c7f290e9ed3aeabb2a2a67db4 Mon Sep 17 00:00:00 2001
3d0f51
From: Mark Reynolds <mreynolds@redhat.com>
3d0f51
Date: Tue, 13 Dec 2022 09:41:34 -0500
3d0f51
Subject: [PATCH] Issue 5413 - Allow mutliple MemberOf fixup tasks with
3d0f51
 different bases/filters
3d0f51
3d0f51
Description:
3d0f51
3d0f51
A change was made to only allow a single fixup task at a time, but there are
3d0f51
cases where you would want to run mutliple tasks but on different branches/filters.
3d0f51
3d0f51
Now we maintain a linked list of bases/filters of the current running tasks to
3d0f51
monitor this.
3d0f51
3d0f51
relates: https://github.com/389ds/389-ds-base/issues/5413
3d0f51
3d0f51
Reviewed by: tbordaz(Thanks!)
3d0f51
---
3d0f51
 .../suites/memberof_plugin/fixup_test.py      |   5 +-
3d0f51
 ldap/servers/plugins/memberof/memberof.c      | 101 ++++++++++++++----
3d0f51
 2 files changed, 85 insertions(+), 21 deletions(-)
3d0f51
3d0f51
diff --git a/dirsrvtests/tests/suites/memberof_plugin/fixup_test.py b/dirsrvtests/tests/suites/memberof_plugin/fixup_test.py
3d0f51
index 9566e144c..d5369439f 100644
3d0f51
--- a/dirsrvtests/tests/suites/memberof_plugin/fixup_test.py
3d0f51
+++ b/dirsrvtests/tests/suites/memberof_plugin/fixup_test.py
3d0f51
@@ -59,12 +59,15 @@ def test_fixup_task_limit(topo):
3d0f51
     with pytest.raises(ldap.UNWILLING_TO_PERFORM):
3d0f51
         memberof.fixup(DEFAULT_SUFFIX)
3d0f51
 
3d0f51
+    # Add second task but on different suffix which should be allowed
3d0f51
+    memberof.fixup("ou=people," + DEFAULT_SUFFIX)
3d0f51
+
3d0f51
     # Wait for first task to complete
3d0f51
     task.wait()
3d0f51
 
3d0f51
     # Add new task which should be allowed now
3d0f51
     memberof.fixup(DEFAULT_SUFFIX)
3d0f51
-     
3d0f51
+
3d0f51
 
3d0f51
 if __name__ == '__main__':
3d0f51
     # Run isolated
3d0f51
diff --git a/ldap/servers/plugins/memberof/memberof.c b/ldap/servers/plugins/memberof/memberof.c
3d0f51
index 0b8cfe95c..a14617044 100644
3d0f51
--- a/ldap/servers/plugins/memberof/memberof.c
3d0f51
+++ b/ldap/servers/plugins/memberof/memberof.c
3d0f51
@@ -52,7 +52,6 @@ static Slapi_DN* _pluginDN = NULL;
3d0f51
 MemberOfConfig *qsortConfig = 0;
3d0f51
 static int usetxn = 0;
3d0f51
 static int premodfn = 0;
3d0f51
-static PRBool fixup_running = PR_FALSE;
3d0f51
 static PRLock *fixup_lock = NULL;
3d0f51
 static int32_t fixup_progress_count = 0;
3d0f51
 static int64_t fixup_progress_elapsed = 0;
3d0f51
@@ -65,6 +64,15 @@ typedef struct _memberofstringll
3d0f51
     void *next;
3d0f51
 } memberofstringll;
3d0f51
 
3d0f51
+typedef struct _fixup_ll
3d0f51
+{
3d0f51
+    Slapi_DN *sdn;
3d0f51
+    char *filter_str;
3d0f51
+    void *next;
3d0f51
+} mo_fixup_ll;
3d0f51
+
3d0f51
+static mo_fixup_ll *fixup_list = NULL;
3d0f51
+
3d0f51
 typedef struct _memberof_get_groups_data
3d0f51
 {
3d0f51
     MemberOfConfig *config;
3d0f51
@@ -438,6 +446,15 @@ memberof_postop_close(Slapi_PBlock *pb __attribute__((unused)))
3d0f51
     PR_DestroyLock(fixup_lock);
3d0f51
     fixup_lock = NULL;
3d0f51
 
3d0f51
+    mo_fixup_ll *fixup_task = fixup_list;
3d0f51
+    while (fixup_task != NULL) {
3d0f51
+        mo_fixup_ll *tmp = fixup_task;
3d0f51
+        fixup_task = fixup_task->next;
3d0f51
+        slapi_sdn_free(&tmp->sdn);
3d0f51
+        slapi_ch_free_string(&tmp->filter_str);
3d0f51
+        slapi_ch_free((void**)&tmp);
3d0f51
+    }
3d0f51
+
3d0f51
     slapi_log_err(SLAPI_LOG_TRACE, MEMBEROF_PLUGIN_SUBSYSTEM,
3d0f51
                   "<-- memberof_postop_close\n");
3d0f51
     return 0;
3d0f51
@@ -2817,7 +2834,6 @@ memberof_fixup_task_thread(void *arg)
3d0f51
     }
3d0f51
 
3d0f51
     PR_Lock(fixup_lock);
3d0f51
-    fixup_running = PR_TRUE;
3d0f51
     fixup_progress_count = 0;
3d0f51
     fixup_progress_elapsed = slapi_current_rel_time_t();
3d0f51
     fixup_start_time = slapi_current_rel_time_t();
3d0f51
@@ -2849,11 +2865,10 @@ memberof_fixup_task_thread(void *arg)
3d0f51
     /* Mark this as a task operation */
3d0f51
     configCopy.fixup_task = 1;
3d0f51
     configCopy.task = task;
3d0f51
-
3d0f51
+    Slapi_DN *sdn = slapi_sdn_new_dn_byref(td->dn);
3d0f51
     if (usetxn) {
3d0f51
-        Slapi_DN *sdn = slapi_sdn_new_dn_byref(td->dn);
3d0f51
         Slapi_Backend *be = slapi_be_select_exact(sdn);
3d0f51
-        slapi_sdn_free(&sdn;;
3d0f51
+
3d0f51
         if (be) {
3d0f51
             fixup_pb = slapi_pblock_new();
3d0f51
             slapi_pblock_set(fixup_pb, SLAPI_BACKEND, be);
3d0f51
@@ -2894,14 +2909,37 @@ done:
3d0f51
                           fixup_progress_count, slapi_current_rel_time_t() - fixup_start_time);
3d0f51
     slapi_task_inc_progress(task);
3d0f51
 
3d0f51
+    /* Cleanup task linked list */
3d0f51
+    PR_Lock(fixup_lock);
3d0f51
+    mo_fixup_ll *prev = NULL;
3d0f51
+    for (mo_fixup_ll *curr = fixup_list; curr; curr = curr->next) {
3d0f51
+        mo_fixup_ll *next = curr->next;
3d0f51
+        if (slapi_sdn_compare(curr->sdn, sdn) == 0 &&
3d0f51
+            strcasecmp(curr->filter_str, td->filter_str) == 0)
3d0f51
+        {
3d0f51
+            /* free current code */
3d0f51
+            slapi_sdn_free(&curr->sdn);
3d0f51
+            slapi_ch_free_string(&curr->filter_str);
3d0f51
+            slapi_ch_free((void**)&curr);
3d0f51
+
3d0f51
+            /* update linked list */
3d0f51
+            if (prev == NULL) {
3d0f51
+                /* first node */
3d0f51
+                fixup_list = next;
3d0f51
+            } else {
3d0f51
+                prev->next = next;
3d0f51
+            }
3d0f51
+            break;
3d0f51
+        }
3d0f51
+        prev = curr;
3d0f51
+    }
3d0f51
+    PR_Unlock(fixup_lock);
3d0f51
+    slapi_sdn_free(&sdn;;
3d0f51
+
3d0f51
     /* this will queue the destruction of the task */
3d0f51
     slapi_task_finish(task, rc);
3d0f51
     slapi_task_dec_refcount(task);
3d0f51
 
3d0f51
-    PR_Lock(fixup_lock);
3d0f51
-    fixup_running = PR_FALSE;
3d0f51
-    PR_Unlock(fixup_lock);
3d0f51
-
3d0f51
     slapi_log_err(SLAPI_LOG_INFO, MEMBEROF_PLUGIN_SUBSYSTEM,
3d0f51
                   "memberof_fixup_task_thread - Memberof task finished (processed %d entries in %ld seconds)\n",
3d0f51
                   fixup_progress_count, slapi_current_rel_time_t() - fixup_start_time);
3d0f51
@@ -2919,23 +2957,13 @@ memberof_task_add(Slapi_PBlock *pb,
3d0f51
     int rv = SLAPI_DSE_CALLBACK_OK;
3d0f51
     task_data *mytaskdata = NULL;
3d0f51
     Slapi_Task *task = NULL;
3d0f51
+    Slapi_DN *sdn = NULL;
3d0f51
     char *bind_dn;
3d0f51
     const char *filter;
3d0f51
     const char *dn = 0;
3d0f51
 
3d0f51
     *returncode = LDAP_SUCCESS;
3d0f51
 
3d0f51
-    PR_Lock(fixup_lock);
3d0f51
-    if (fixup_running) {
3d0f51
-        PR_Unlock(fixup_lock);
3d0f51
-        *returncode = LDAP_UNWILLING_TO_PERFORM;
3d0f51
-        slapi_log_err(SLAPI_LOG_ERR, MEMBEROF_PLUGIN_SUBSYSTEM,
3d0f51
-                "memberof_task_add - there is already a fixup task running\n");
3d0f51
-        rv = SLAPI_DSE_CALLBACK_ERROR;
3d0f51
-        goto out;
3d0f51
-    }
3d0f51
-    PR_Unlock(fixup_lock);
3d0f51
-
3d0f51
     /* get arg(s) */
3d0f51
     if ((dn = slapi_entry_attr_get_ref(e, "basedn")) == NULL) {
3d0f51
         *returncode = LDAP_OBJECT_CLASS_VIOLATION;
3d0f51
@@ -2949,6 +2977,39 @@ memberof_task_add(Slapi_PBlock *pb,
3d0f51
         goto out;
3d0f51
     }
3d0f51
 
3d0f51
+    PR_Lock(fixup_lock);
3d0f51
+    sdn = slapi_sdn_new_dn_byval(dn);
3d0f51
+    if (fixup_list == NULL) {
3d0f51
+        fixup_list = (mo_fixup_ll *)slapi_ch_calloc(1, sizeof(mo_fixup_ll));
3d0f51
+        fixup_list->sdn = sdn;
3d0f51
+        fixup_list->filter_str = slapi_ch_strdup(filter);
3d0f51
+    } else {
3d0f51
+        for (mo_fixup_ll *fixup_task = fixup_list; fixup_task; fixup_task = fixup_task->next) {
3d0f51
+            if (slapi_sdn_compare(sdn, fixup_task->sdn) == 0 &&
3d0f51
+                strcasecmp(filter, fixup_task->filter_str) == 0)
3d0f51
+            {
3d0f51
+                /* Found an identical running task, reject it */
3d0f51
+                PR_Unlock(fixup_lock);
3d0f51
+                slapi_log_err(SLAPI_LOG_ERR, MEMBEROF_PLUGIN_SUBSYSTEM,
3d0f51
+                        "memberof_task_add - there is already an identical fixup task running: base: %s filter: %s\n",
3d0f51
+                        slapi_sdn_get_dn(sdn), filter);
3d0f51
+                slapi_sdn_free(&sdn;;
3d0f51
+                *returncode = LDAP_UNWILLING_TO_PERFORM;
3d0f51
+                rv = SLAPI_DSE_CALLBACK_ERROR;
3d0f51
+                goto out;
3d0f51
+            }
3d0f51
+        }
3d0f51
+        /* Add the new task DN to the top of the list */
3d0f51
+        mo_fixup_ll *head = fixup_list;
3d0f51
+        mo_fixup_ll *new_task = (mo_fixup_ll *)slapi_ch_calloc(1, sizeof(mo_fixup_ll));
3d0f51
+        new_task->sdn = sdn;
3d0f51
+        new_task->filter_str = slapi_ch_strdup(filter);
3d0f51
+        new_task->next = head;
3d0f51
+        fixup_list = new_task;
3d0f51
+    }
3d0f51
+    PR_Unlock(fixup_lock);
3d0f51
+
3d0f51
+
3d0f51
     /* setup our task data */
3d0f51
     slapi_pblock_get(pb, SLAPI_REQUESTOR_DN, &bind_dn);
3d0f51
     mytaskdata = (task_data *)slapi_ch_malloc(sizeof(task_data));
3d0f51
-- 
3d0f51
2.38.1
3d0f51