andykimpe / rpms / 389-ds-base

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

Blame SOURCES/0037-Issue-5155-RFE-Provide-an-option-to-abort-an-Auto-Me.patch

7a6e0a
From b74fa27d5da3e96f474a9643d6c56cea7e395db8 Mon Sep 17 00:00:00 2001
7a6e0a
From: Mark Reynolds <mreynolds@redhat.com>
7a6e0a
Date: Tue, 8 Feb 2022 12:38:54 -0500
7a6e0a
Subject: [PATCH 3/4] Issue 5155 - RFE - Provide an option to abort an Auto
7a6e0a
 Member rebuild task
7a6e0a
7a6e0a
https://github.com/389ds/389-ds-base/issues/5155
7a6e0a
7a6e0a
https://github.com/389ds/389-ds-base/pull/5157
7a6e0a
---
7a6e0a
 ldap/servers/plugins/automember/automember.c | 124 +++++++++++++------
7a6e0a
 1 file changed, 88 insertions(+), 36 deletions(-)
7a6e0a
7a6e0a
diff --git a/ldap/servers/plugins/automember/automember.c b/ldap/servers/plugins/automember/automember.c
7a6e0a
index d06c6375e..c377431f4 100644
7a6e0a
--- a/ldap/servers/plugins/automember/automember.c
7a6e0a
+++ b/ldap/servers/plugins/automember/automember.c
7a6e0a
@@ -21,6 +21,7 @@
7a6e0a
  */
7a6e0a
 static PRCList *g_automember_config = NULL;
7a6e0a
 static Slapi_RWLock *g_automember_config_lock = NULL;
7a6e0a
+static uint64_t abort_rebuild_task = 0;
7a6e0a
 
7a6e0a
 static void *_PluginID = NULL;
7a6e0a
 static Slapi_DN *_PluginDN = NULL;
7a6e0a
@@ -82,9 +83,11 @@ static int automember_update_member_value(Slapi_Entry *member_e, const char *gro
7a6e0a
 static int automember_task_add(Slapi_PBlock *pb, Slapi_Entry *e, Slapi_Entry *eAfter, int *returncode, char *returntext, void *arg);
7a6e0a
 static int automember_task_add_export_updates(Slapi_PBlock *pb, Slapi_Entry *e, Slapi_Entry *eAfter, int *returncode, char *returntext, void *arg);
7a6e0a
 static int automember_task_add_map_entries(Slapi_PBlock *pb, Slapi_Entry *e, Slapi_Entry *eAfter, int *returncode, char *returntext, void *arg);
7a6e0a
+static int automember_task_abort(Slapi_PBlock *pb, Slapi_Entry *e, Slapi_Entry *eAfter, int *returncode, char *returntext, void *arg);
7a6e0a
 void automember_rebuild_task_thread(void *arg);
7a6e0a
 void automember_export_task_thread(void *arg);
7a6e0a
 void automember_map_task_thread(void *arg);
7a6e0a
+void automember_task_abort_thread(void *arg);
7a6e0a
 static void automember_task_destructor(Slapi_Task *task);
7a6e0a
 static void automember_task_export_destructor(Slapi_Task *task);
7a6e0a
 static void automember_task_map_destructor(Slapi_Task *task);
7a6e0a
@@ -305,6 +308,7 @@ automember_start(Slapi_PBlock *pb)
7a6e0a
                   "--> automember_start\n");
7a6e0a
 
7a6e0a
     slapi_plugin_task_register_handler("automember rebuild membership", automember_task_add, pb);
7a6e0a
+    slapi_plugin_task_register_handler("automember abort rebuild", automember_task_abort, pb);
7a6e0a
     slapi_plugin_task_register_handler("automember export updates", automember_task_add_export_updates, pb);
7a6e0a
     slapi_plugin_task_register_handler("automember map updates", automember_task_add_map_entries, pb);
7a6e0a
 
7a6e0a
@@ -383,6 +387,9 @@ automember_close(Slapi_PBlock *pb __attribute__((unused)))
7a6e0a
                                          automember_task_add_export_updates);
7a6e0a
     slapi_plugin_task_unregister_handler("automember map updates",
7a6e0a
                                          automember_task_add_map_entries);
7a6e0a
+    slapi_plugin_task_unregister_handler("automember abort rebuild",
7a6e0a
+                                         automember_task_abort);
7a6e0a
+
7a6e0a
 
7a6e0a
     automember_delete_config();
7a6e0a
     slapi_sdn_free(&_PluginDN);
7a6e0a
@@ -2207,6 +2214,65 @@ automember_task_map_destructor(Slapi_Task *task)
7a6e0a
     }
7a6e0a
 }
7a6e0a
 
7a6e0a
+/*
7a6e0a
+ *  automember_task_abort
7a6e0a
+ *
7a6e0a
+ *  This task is designed to abort and existing rebuild task
7a6e0a
+ *
7a6e0a
+ *  task entry:
7a6e0a
+ *
7a6e0a
+ *    dn: cn=my abort task, cn=automember abort rebuild,cn=tasks,cn=config
7a6e0a
+ *    objectClass: top
7a6e0a
+ *    objectClass: extensibleObject
7a6e0a
+ *    cn: my abort task
7a6e0a
+ */
7a6e0a
+static int
7a6e0a
+automember_task_abort(Slapi_PBlock *pb, Slapi_Entry *e, Slapi_Entry *eAfter __attribute__((unused)), int *returncode, char *returntext __attribute__((unused)), void *arg)
7a6e0a
+{
7a6e0a
+    Slapi_Task *task = NULL;
7a6e0a
+    PRThread *thread = NULL;
7a6e0a
+    int rc;
7a6e0a
+
7a6e0a
+    *returncode = LDAP_SUCCESS; /* can not fail - always success */
7a6e0a
+
7a6e0a
+    task = slapi_plugin_new_task(slapi_entry_get_ndn(e), arg);
7a6e0a
+    thread = PR_CreateThread(PR_USER_THREAD, automember_task_abort_thread,
7a6e0a
+                             (void *)task, PR_PRIORITY_NORMAL, PR_GLOBAL_THREAD,
7a6e0a
+                             PR_UNJOINABLE_THREAD, SLAPD_DEFAULT_THREAD_STACKSIZE);
7a6e0a
+    if (thread == NULL) {
7a6e0a
+        slapi_log_err(SLAPI_LOG_ERR, AUTOMEMBER_PLUGIN_SUBSYSTEM,
7a6e0a
+                      "automember_task_abort - Unable to create task thread!\n");
7a6e0a
+        *returncode = LDAP_OPERATIONS_ERROR;
7a6e0a
+        slapi_task_finish(task, *returncode);
7a6e0a
+        rc = SLAPI_DSE_CALLBACK_ERROR;
7a6e0a
+    } else {
7a6e0a
+        rc = SLAPI_DSE_CALLBACK_OK;
7a6e0a
+    }
7a6e0a
+    return rc;
7a6e0a
+}
7a6e0a
+
7a6e0a
+void
7a6e0a
+automember_task_abort_thread(void *arg)
7a6e0a
+{
7a6e0a
+    Slapi_Task *task = (Slapi_Task *)arg;
7a6e0a
+
7a6e0a
+    slapi_task_inc_refcount(task);
7a6e0a
+    slapi_task_begin(task, 1);
7a6e0a
+    slapi_task_log_notice(task, "Automember abort rebuild task started.");
7a6e0a
+    slapi_task_log_status(task, "Automember abort rebuild task started.");
7a6e0a
+
7a6e0a
+    /* Set the abort flag */
7a6e0a
+    slapi_atomic_store_64(&abort_rebuild_task, 1, __ATOMIC_RELEASE);
7a6e0a
+
7a6e0a
+    /* Wrap things up */
7a6e0a
+    slapi_task_log_notice(task, "Automember abort rebuild task finished.");
7a6e0a
+    slapi_task_log_status(task, "Automember abort rebuild task finished.");
7a6e0a
+    slapi_task_inc_progress(task);
7a6e0a
+    slapi_task_finish(task, 0);
7a6e0a
+    slapi_task_dec_refcount(task);
7a6e0a
+}
7a6e0a
+
7a6e0a
+
7a6e0a
 /*
7a6e0a
  *  automember_task_add
7a6e0a
  *
7a6e0a
@@ -2320,13 +2386,16 @@ automember_rebuild_task_thread(void *arg)
7a6e0a
 {
7a6e0a
     Slapi_Task *task = (Slapi_Task *)arg;
7a6e0a
     struct configEntry *config = NULL;
7a6e0a
-    Slapi_PBlock *search_pb = NULL, *fixup_pb = NULL;
7a6e0a
+    Slapi_PBlock *search_pb = NULL;
7a6e0a
     Slapi_Entry **entries = NULL;
7a6e0a
     task_data *td = NULL;
7a6e0a
     PRCList *list = NULL;
7a6e0a
     PRCList *include_list = NULL;
7a6e0a
     int result = 0;
7a6e0a
-    size_t i = 0, ii = 0;
7a6e0a
+    size_t i = 0;
7a6e0a
+
7a6e0a
+    /* Reset abort flag */
7a6e0a
+    slapi_atomic_store_64(&abort_rebuild_task, 0, __ATOMIC_RELEASE);
7a6e0a
 
7a6e0a
     if (!task) {
7a6e0a
         return; /* no task */
7a6e0a
@@ -2350,6 +2419,8 @@ automember_rebuild_task_thread(void *arg)
7a6e0a
     /*
7a6e0a
      *  Search the database
7a6e0a
      */
7a6e0a
+    automember_config_read_lock();
7a6e0a
+
7a6e0a
     search_pb = slapi_pblock_new();
7a6e0a
     slapi_search_internal_set_pb_ext(search_pb, td->base_dn, td->scope, td->filter_str, NULL,
7a6e0a
                                      0, NULL, NULL, automember_get_plugin_id(), 0);
7a6e0a
@@ -2372,30 +2443,19 @@ automember_rebuild_task_thread(void *arg)
7a6e0a
     slapi_pblock_get(search_pb, SLAPI_PLUGIN_INTOP_SEARCH_ENTRIES, &entries);
7a6e0a
 
7a6e0a
     /*
7a6e0a
-     * If this is a backend txn plugin, start the transaction
7a6e0a
+     * loop over the entries
7a6e0a
      */
7a6e0a
-    if (plugin_is_betxn) {
7a6e0a
-        Slapi_Backend *be = slapi_be_select(td->base_dn);
7a6e0a
-
7a6e0a
-        if (be) {
7a6e0a
-            fixup_pb = slapi_pblock_new();
7a6e0a
-            slapi_pblock_set(fixup_pb, SLAPI_BACKEND, be);
7a6e0a
-            if (slapi_back_transaction_begin(fixup_pb) != LDAP_SUCCESS) {
7a6e0a
-                slapi_log_err(SLAPI_LOG_ERR, AUTOMEMBER_PLUGIN_SUBSYSTEM,
7a6e0a
-                              "automember_rebuild_task_thread - Failed to start transaction\n");
7a6e0a
-            }
7a6e0a
-        } else {
7a6e0a
-            slapi_log_err(SLAPI_LOG_ERR, AUTOMEMBER_PLUGIN_SUBSYSTEM,
7a6e0a
-                          "automember_rebuild_task_thread - Failed to get be backend from %s\n",
7a6e0a
-                          slapi_sdn_get_dn(td->base_dn));
7a6e0a
+    for (i = 0; entries && (entries[i] != NULL); i++) {
7a6e0a
+        if (slapi_atomic_load_64(&abort_rebuild_task, __ATOMIC_ACQUIRE) == 1) {
7a6e0a
+            /* The task was aborted */
7a6e0a
+            slapi_task_log_notice(task, "Automember rebuild task was intentionally aborted");
7a6e0a
+            slapi_task_log_status(task, "Automember rebuild task was intentionally aborted");
7a6e0a
+            slapi_log_err(SLAPI_LOG_NOTICE, AUTOMEMBER_PLUGIN_SUBSYSTEM,
7a6e0a
+                          "automember_rebuild_task_thread - task was intentionally aborted\n");
7a6e0a
+            result = -1;
7a6e0a
+            goto out;
7a6e0a
         }
7a6e0a
-    }
7a6e0a
 
7a6e0a
-    /*
7a6e0a
-     *  Grab the config read lock, and loop over the entries
7a6e0a
-     */
7a6e0a
-    automember_config_read_lock();
7a6e0a
-    for (i = 0; entries && (entries[i] != NULL); i++) {
7a6e0a
         if (!PR_CLIST_IS_EMPTY(g_automember_config)) {
7a6e0a
             list = PR_LIST_HEAD(g_automember_config);
7a6e0a
             while (list != g_automember_config) {
7a6e0a
@@ -2405,7 +2465,7 @@ automember_rebuild_task_thread(void *arg)
7a6e0a
                     (slapi_filter_test_simple(entries[i], config->filter) == 0))
7a6e0a
                 {
7a6e0a
                     /* First clear out all the defaults groups */
7a6e0a
-                    for (ii = 0; config->default_groups && config->default_groups[ii]; ii++) {
7a6e0a
+                    for (size_t ii = 0; config->default_groups && config->default_groups[ii]; ii++) {
7a6e0a
                         if ((result = automember_update_member_value(entries[i], config->default_groups[ii],
7a6e0a
                                 config->grouping_attr, config->grouping_value, NULL, DEL_MEMBER)))
7a6e0a
                         {
7a6e0a
@@ -2418,7 +2478,6 @@ automember_rebuild_task_thread(void *arg)
7a6e0a
                             slapi_log_err(SLAPI_LOG_ERR, AUTOMEMBER_PLUGIN_SUBSYSTEM,
7a6e0a
                                           "automember_rebuild_task_thread - Unable to unable to delete from (%s) error (%d)\n",
7a6e0a
                                           config->default_groups[ii], result);
7a6e0a
-                            automember_config_unlock();
7a6e0a
                             goto out;
7a6e0a
                         }
7a6e0a
                     }
7a6e0a
@@ -2440,7 +2499,6 @@ automember_rebuild_task_thread(void *arg)
7a6e0a
                                 slapi_log_err(SLAPI_LOG_ERR, AUTOMEMBER_PLUGIN_SUBSYSTEM,
7a6e0a
                                               "automember_rebuild_task_thread - Unable to unable to delete from (%s) error (%d)\n",
7a6e0a
                                               slapi_sdn_get_dn(curr_rule->target_group_dn), result);
7a6e0a
-                                automember_config_unlock();
7a6e0a
                                 goto out;
7a6e0a
                             }
7a6e0a
                             include_list = PR_NEXT_LINK(include_list);
7a6e0a
@@ -2452,7 +2510,6 @@ automember_rebuild_task_thread(void *arg)
7a6e0a
                         automember_update_membership(config, entries[i], NULL) == SLAPI_PLUGIN_FAILURE)
7a6e0a
                     {
7a6e0a
                         result = SLAPI_PLUGIN_FAILURE;
7a6e0a
-                        automember_config_unlock();
7a6e0a
                         goto out;
7a6e0a
                     }
7a6e0a
                 }
7a6e0a
@@ -2460,17 +2517,10 @@ automember_rebuild_task_thread(void *arg)
7a6e0a
             }
7a6e0a
         }
7a6e0a
     }
7a6e0a
-    automember_config_unlock();
7a6e0a
 
7a6e0a
 out:
7a6e0a
-    if (plugin_is_betxn && fixup_pb) {
7a6e0a
-        if (i == 0 || result != 0) { /* no updates performed */
7a6e0a
-            slapi_back_transaction_abort(fixup_pb);
7a6e0a
-        } else {
7a6e0a
-            slapi_back_transaction_commit(fixup_pb);
7a6e0a
-        }
7a6e0a
-        slapi_pblock_destroy(fixup_pb);
7a6e0a
-    }
7a6e0a
+    automember_config_unlock();
7a6e0a
+
7a6e0a
     slapi_free_search_results_internal(search_pb);
7a6e0a
     slapi_pblock_destroy(search_pb);
7a6e0a
 
7a6e0a
@@ -2485,6 +2535,8 @@ out:
7a6e0a
     slapi_task_inc_progress(task);
7a6e0a
     slapi_task_finish(task, result);
7a6e0a
     slapi_task_dec_refcount(task);
7a6e0a
+    slapi_atomic_store_64(&abort_rebuild_task, 0, __ATOMIC_RELEASE);
7a6e0a
+
7a6e0a
     slapi_log_err(SLAPI_LOG_PLUGIN, AUTOMEMBER_PLUGIN_SUBSYSTEM,
7a6e0a
                   "automember_rebuild_task_thread - Refcount decremented.\n");
7a6e0a
 }
7a6e0a
-- 
7a6e0a
2.31.1
7a6e0a