Blame SOURCES/0004-Issue-50538-cleanAllRUV-task-limit-is-not-enforced-f.patch

458e05
From 59f03e332061b2c68bb53eed5949ddcfdc563300 Mon Sep 17 00:00:00 2001
458e05
From: Mark Reynolds <mreynolds@redhat.com>
458e05
Date: Wed, 7 Aug 2019 20:36:53 -0400
458e05
Subject: [PATCH] Issue 50538 - cleanAllRUV task limit is not enforced for
458e05
 replicated tasks
458e05
458e05
Bug Description:
458e05
458e05
There is a hard limit of 64 concurrent cleanAllRUV tasks, but this limit is
458e05
only enforced when creating "new" tasks. It was not enforced when a task was
458e05
received via an extended operation. There were also race conditions in the
458e05
existing logic that allowed the array of cleaned rids to get corrupted . This
458e05
allowed for a very large number of task threads to be created.
458e05
458e05
Fix Description:
458e05
458e05
Maintain a new counter to keep track of the number of clean and abort threads
458e05
to make sure it never over runs the rid array buffers.
458e05
458e05
relates: https://pagure.io/389-ds-base/issue/50538
458e05
458e05
Reviewed by: lkrispenz(Thanks!)
458e05
---
458e05
 .../suites/replication/cleanallruv_test.py    |  47 +++-
458e05
 ldap/servers/plugins/replication/repl5.h      |   7 +-
458e05
 .../replication/repl5_replica_config.c        | 247 ++++++++++--------
458e05
 ldap/servers/plugins/replication/repl_extop.c |  19 +-
458e05
 4 files changed, 202 insertions(+), 118 deletions(-)
458e05
458e05
diff --git a/dirsrvtests/tests/suites/replication/cleanallruv_test.py b/dirsrvtests/tests/suites/replication/cleanallruv_test.py
458e05
index 620a53e1a..43801dd52 100644
458e05
--- a/dirsrvtests/tests/suites/replication/cleanallruv_test.py
458e05
+++ b/dirsrvtests/tests/suites/replication/cleanallruv_test.py
458e05
@@ -1,5 +1,5 @@
458e05
 # --- BEGIN COPYRIGHT BLOCK ---
458e05
-# Copyright (C) 2016 Red Hat, Inc.
458e05
+# Copyright (C) 2019 Red Hat, Inc.
458e05
 # All rights reserved.
458e05
 #
458e05
 # License: GPL (version 3 or any later version).
458e05
@@ -7,7 +7,6 @@
458e05
 # --- END COPYRIGHT BLOCK ---
458e05
 #
458e05
 import threading
458e05
-
458e05
 import pytest
458e05
 from lib389.tasks import *
458e05
 from lib389.utils import *
458e05
@@ -859,6 +858,50 @@ def test_multiple_tasks_with_force(topology_m4):
458e05
     restore_master4(topology_m4)
458e05
 
458e05
 
458e05
+def test_max_tasks(topology_m4):
458e05
+    """Test we can not create more than 64 cleaning tasks
458e05
+
458e05
+    :id: c34d0b40-3c3e-4f53-8656-5e4c2a310a1f
458e05
+    :setup: Replication setup with four masters
458e05
+    :steps:
458e05
+        1. Stop masters 3 & 4
458e05
+        2. Create over 64 tasks between m1 and m2
458e05
+        3. Check logs to see if (>65) tasks were rejected
458e05
+
458e05
+    :expectedresults:
458e05
+        1. Success
458e05
+        2. Success
458e05
+        3. Success
458e05
+    """
458e05
+
458e05
+    # Stop masters 3 & 4
458e05
+    m1 = topology_m4.ms["master1"]
458e05
+    m2 = topology_m4.ms["master2"]
458e05
+    m3 = topology_m4.ms["master3"]
458e05
+    m4 = topology_m4.ms["master4"]
458e05
+    m3.stop()
458e05
+    m4.stop()
458e05
+
458e05
+    # Add over 64 tasks between master1 & 2 to try to exceed the 64 task limit
458e05
+    for i in range(1, 64):
458e05
+        cruv_task = CleanAllRUVTask(m1)
458e05
+        cruv_task.create(properties={
458e05
+            'replica-id': str(i),
458e05
+            'replica-base-dn': DEFAULT_SUFFIX,
458e05
+            'replica-force-cleaning': 'no',  # This forces these tasks to stick around
458e05
+        })
458e05
+        cruv_task = CleanAllRUVTask(m2)
458e05
+        cruv_task.create(properties={
458e05
+            'replica-id': "10" + str(i),
458e05
+            'replica-base-dn': DEFAULT_SUFFIX,
458e05
+            'replica-force-cleaning': 'yes',  # This allows the tasks to propagate
458e05
+        })
458e05
+
458e05
+    # Check the errors log for our error message in master 1
458e05
+    assert m1.searchErrorsLog('Exceeded maximum number of active CLEANALLRUV tasks')
458e05
+>>>>>>> ab24aa4cb... Issue 50538 - cleanAllRUV task limit is not enforced for replicated tasks
458e05
+
458e05
+
458e05
 if __name__ == '__main__':
458e05
     # Run isolated
458e05
     # -s for DEBUG mode
458e05
diff --git a/ldap/servers/plugins/replication/repl5.h b/ldap/servers/plugins/replication/repl5.h
458e05
index 3c7f06f36..b06c6fbf4 100644
458e05
--- a/ldap/servers/plugins/replication/repl5.h
458e05
+++ b/ldap/servers/plugins/replication/repl5.h
458e05
@@ -80,6 +80,8 @@
458e05
 #define CLEANRUV_FINISHED  "finished"
458e05
 #define CLEANRUV_CLEANING  "cleaning"
458e05
 #define CLEANRUV_NO_MAXCSN "no maxcsn"
458e05
+#define CLEANALLRUV_ID "CleanAllRUV Task"
458e05
+#define ABORT_CLEANALLRUV_ID "Abort CleanAllRUV Task"
458e05
 
458e05
 /* DS 5.0 replication protocol error codes */
458e05
 #define NSDS50_REPL_REPLICA_READY             0x00  /* Replica ready, go ahead */
458e05
@@ -784,6 +786,7 @@ void multimaster_mtnode_construct_replicas(void);
458e05
 void multimaster_be_state_change(void *handle, char *be_name, int old_be_state, int new_be_state);
458e05
 
458e05
 #define CLEANRIDSIZ 64 /* maximum number for concurrent CLEANALLRUV tasks */
458e05
+#define CLEANRID_BUFSIZ 128
458e05
 
458e05
 typedef struct _cleanruv_data
458e05
 {
458e05
@@ -815,6 +818,8 @@ int get_replica_type(Replica *r);
458e05
 int replica_execute_cleanruv_task_ext(Object *r, ReplicaId rid);
458e05
 void add_cleaned_rid(cleanruv_data *data, char *maxcsn);
458e05
 int is_cleaned_rid(ReplicaId rid);
458e05
+int32_t check_and_set_cleanruv_task_count(ReplicaId rid);
458e05
+int32_t check_and_set_abort_cleanruv_task_count(void);
458e05
 int replica_cleanall_ruv_abort(Slapi_PBlock *pb, Slapi_Entry *e, Slapi_Entry *eAfter, int *returncode, char *returntext, void *arg);
458e05
 void replica_cleanallruv_thread_ext(void *arg);
458e05
 void stop_ruv_cleaning(void);
458e05
@@ -833,8 +838,6 @@ void set_cleaned_rid(ReplicaId rid);
458e05
 void cleanruv_log(Slapi_Task *task, int rid, char *task_type, int sev_level, char *fmt, ...);
458e05
 char *replica_cleanallruv_get_local_maxcsn(ReplicaId rid, char *base_dn);
458e05
 
458e05
-
458e05
-
458e05
 /* replutil.c */
458e05
 LDAPControl *create_managedsait_control(void);
458e05
 LDAPControl *create_backend_control(Slapi_DN *sdn);
458e05
diff --git a/ldap/servers/plugins/replication/repl5_replica_config.c b/ldap/servers/plugins/replication/repl5_replica_config.c
458e05
index 62bfcf6ce..80a079784 100644
458e05
--- a/ldap/servers/plugins/replication/repl5_replica_config.c
458e05
+++ b/ldap/servers/plugins/replication/repl5_replica_config.c
458e05
@@ -30,17 +30,18 @@
458e05
 #define CLEANALLRUV "CLEANALLRUV"
458e05
 #define CLEANALLRUVLEN 11
458e05
 #define REPLICA_RDN "cn=replica"
458e05
-#define CLEANALLRUV_ID "CleanAllRUV Task"
458e05
-#define ABORT_CLEANALLRUV_ID "Abort CleanAllRUV Task"
458e05
 
458e05
 int slapi_log_urp = SLAPI_LOG_REPL;
458e05
-static ReplicaId cleaned_rids[CLEANRIDSIZ + 1] = {0};
458e05
-static ReplicaId pre_cleaned_rids[CLEANRIDSIZ + 1] = {0};
458e05
-static ReplicaId aborted_rids[CLEANRIDSIZ + 1] = {0};
458e05
-static Slapi_RWLock *rid_lock = NULL;
458e05
-static Slapi_RWLock *abort_rid_lock = NULL;
458e05
+static ReplicaId cleaned_rids[CLEANRID_BUFSIZ] = {0};
458e05
+static ReplicaId pre_cleaned_rids[CLEANRID_BUFSIZ] = {0};
458e05
+static ReplicaId aborted_rids[CLEANRID_BUFSIZ] = {0};
458e05
+static PRLock *rid_lock = NULL;
458e05
+static PRLock *abort_rid_lock = NULL;
458e05
 static PRLock *notify_lock = NULL;
458e05
 static PRCondVar *notify_cvar = NULL;
458e05
+static PRLock *task_count_lock = NULL;
458e05
+static int32_t clean_task_count = 0;
458e05
+static int32_t abort_task_count = 0;
458e05
 
458e05
 /* Forward Declartions */
458e05
 static int replica_config_add(Slapi_PBlock *pb, Slapi_Entry *e, Slapi_Entry *entryAfter, int *returncode, char *returntext, void *arg);
458e05
@@ -67,8 +68,6 @@ static int replica_cleanallruv_send_abort_extop(Repl_Agmt *ra, Slapi_Task *task,
458e05
 static int replica_cleanallruv_check_maxcsn(Repl_Agmt *agmt, char *basedn, char *rid_text, char *maxcsn, Slapi_Task *task);
458e05
 static int replica_cleanallruv_replica_alive(Repl_Agmt *agmt);
458e05
 static int replica_cleanallruv_check_ruv(char *repl_root, Repl_Agmt *ra, char *rid_text, Slapi_Task *task, char *force);
458e05
-static int get_cleanruv_task_count(void);
458e05
-static int get_abort_cleanruv_task_count(void);
458e05
 static int replica_cleanup_task(Object *r, const char *task_name, char *returntext, int apply_mods);
458e05
 static int replica_task_done(Replica *replica);
458e05
 static void delete_cleaned_rid_config(cleanruv_data *data);
458e05
@@ -114,20 +113,27 @@ replica_config_init()
458e05
                       PR_GetError());
458e05
         return -1;
458e05
     }
458e05
-    rid_lock = slapi_new_rwlock();
458e05
+    rid_lock = PR_NewLock();
458e05
     if (rid_lock == NULL) {
458e05
         slapi_log_err(SLAPI_LOG_ERR, repl_plugin_name, "replica_config_init - "
458e05
                                                        "Failed to create rid_lock; NSPR error - %d\n",
458e05
                       PR_GetError());
458e05
         return -1;
458e05
     }
458e05
-    abort_rid_lock = slapi_new_rwlock();
458e05
+    abort_rid_lock = PR_NewLock();
458e05
     if (abort_rid_lock == NULL) {
458e05
         slapi_log_err(SLAPI_LOG_ERR, repl_plugin_name, "replica_config_init - "
458e05
                                                        "Failed to create abort_rid_lock; NSPR error - %d\n",
458e05
                       PR_GetError());
458e05
         return -1;
458e05
     }
458e05
+    task_count_lock = PR_NewLock();
458e05
+    if (task_count_lock == NULL) {
458e05
+        slapi_log_err(SLAPI_LOG_ERR, repl_plugin_name, "replica_config_init - "
458e05
+                                                       "Failed to create task_count_lock; NSPR error - %d\n",
458e05
+                      PR_GetError());
458e05
+        return -1;
458e05
+    }
458e05
     if ((notify_lock = PR_NewLock()) == NULL) {
458e05
         slapi_log_err(SLAPI_LOG_ERR, repl_plugin_name, "replica_config_init - "
458e05
                                                        "Failed to create notify lock; NSPR error - %d\n",
458e05
@@ -1483,12 +1489,6 @@ replica_execute_cleanall_ruv_task(Object *r, ReplicaId rid, Slapi_Task *task, co
458e05
 
458e05
     cleanruv_log(pre_task, rid, CLEANALLRUV_ID, SLAPI_LOG_INFO, "Initiating CleanAllRUV Task...");
458e05
 
458e05
-    if (get_cleanruv_task_count() >= CLEANRIDSIZ) {
458e05
-        /* we are already running the maximum number of tasks */
458e05
-        cleanruv_log(pre_task, rid, CLEANALLRUV_ID, SLAPI_LOG_ERR,
458e05
-                     "Exceeded maximum number of active CLEANALLRUV tasks(%d)", CLEANRIDSIZ);
458e05
-        return LDAP_UNWILLING_TO_PERFORM;
458e05
-    }
458e05
     /*
458e05
      *  Grab the replica
458e05
      */
458e05
@@ -1540,6 +1540,13 @@ replica_execute_cleanall_ruv_task(Object *r, ReplicaId rid, Slapi_Task *task, co
458e05
         goto fail;
458e05
     }
458e05
 
458e05
+    if (check_and_set_cleanruv_task_count(rid) != LDAP_SUCCESS) {
458e05
+        cleanruv_log(NULL, rid, CLEANALLRUV_ID, SLAPI_LOG_ERR,
458e05
+                     "Exceeded maximum number of active CLEANALLRUV tasks(%d)", CLEANRIDSIZ);
458e05
+        rc = LDAP_UNWILLING_TO_PERFORM;
458e05
+        goto fail;
458e05
+    }
458e05
+
458e05
     /*
458e05
      *  Launch the cleanallruv thread.  Once all the replicas are cleaned it will release the rid
458e05
      */
458e05
@@ -1547,6 +1554,9 @@ replica_execute_cleanall_ruv_task(Object *r, ReplicaId rid, Slapi_Task *task, co
458e05
     if (data == NULL) {
458e05
         cleanruv_log(pre_task, rid, CLEANALLRUV_ID, SLAPI_LOG_ERR, "Failed to allocate cleanruv_data.  Aborting task.");
458e05
         rc = -1;
458e05
+        PR_Lock(task_count_lock);
458e05
+        clean_task_count--;
458e05
+        PR_Unlock(task_count_lock);
458e05
         goto fail;
458e05
     }
458e05
     data->repl_obj = r;
458e05
@@ -1629,13 +1639,13 @@ replica_cleanallruv_thread(void *arg)
458e05
     int aborted = 0;
458e05
     int rc = 0;
458e05
 
458e05
-    if (!data || slapi_is_shutting_down()) {
458e05
-        return; /* no data */
458e05
-    }
458e05
-
458e05
     /* Increase active thread count to prevent a race condition at server shutdown */
458e05
     g_incr_active_threadcnt();
458e05
 
458e05
+    if (!data || slapi_is_shutting_down()) {
458e05
+        goto done;
458e05
+    }
458e05
+
458e05
     if (data->task) {
458e05
         slapi_task_inc_refcount(data->task);
458e05
         slapi_log_err(SLAPI_LOG_PLUGIN, repl_plugin_name,
458e05
@@ -1682,16 +1692,13 @@ replica_cleanallruv_thread(void *arg)
458e05
         slapi_task_begin(data->task, 1);
458e05
     }
458e05
     /*
458e05
-     *  Presetting the rid prevents duplicate thread creation, but allows the db and changelog to still
458e05
-     *  process updates from the rid.
458e05
-     *  set_cleaned_rid() blocks updates, so we don't want to do that... yet unless we are in force mode.
458e05
-     *  If we are forcing a clean independent of state of other servers for this RID we can set_cleaned_rid()
458e05
+     *  We have already preset this rid, but if we are forcing a clean independent of state
458e05
+     *  of other servers for this RID we can set_cleaned_rid()
458e05
      */
458e05
     if (data->force) {
458e05
         set_cleaned_rid(data->rid);
458e05
-    } else {
458e05
-        preset_cleaned_rid(data->rid);
458e05
     }
458e05
+
458e05
     rid_text = slapi_ch_smprintf("%d", data->rid);
458e05
     csn_as_string(data->maxcsn, PR_FALSE, csnstr);
458e05
     /*
458e05
@@ -1861,6 +1868,9 @@ done:
458e05
     /*
458e05
      *  If the replicas are cleaned, release the rid
458e05
      */
458e05
+    if (slapi_is_shutting_down()) {
458e05
+        stop_ruv_cleaning();
458e05
+    }
458e05
     if (!aborted && !slapi_is_shutting_down()) {
458e05
         /*
458e05
          * Success - the rid has been cleaned!
458e05
@@ -1879,10 +1889,9 @@ done:
458e05
         } else {
458e05
             cleanruv_log(data->task, data->rid, CLEANALLRUV_ID, SLAPI_LOG_INFO, "Propagated task does not delete Keep alive entry (%d).", data->rid);
458e05
         }
458e05
-
458e05
         clean_agmts(data);
458e05
         remove_cleaned_rid(data->rid);
458e05
-        cleanruv_log(data->task, data->rid, CLEANALLRUV_ID, SLAPI_LOG_INFO, "Successfully cleaned rid(%d).", data->rid);
458e05
+        cleanruv_log(data->task, data->rid, CLEANALLRUV_ID, SLAPI_LOG_INFO, "Successfully cleaned rid(%d)", data->rid);
458e05
     } else {
458e05
         /*
458e05
          *  Shutdown or abort
458e05
@@ -1915,6 +1924,10 @@ done:
458e05
     slapi_ch_free_string(&data->force);
458e05
     slapi_ch_free_string(&rid_text);
458e05
     slapi_ch_free((void **)&data);
458e05
+    /* decrement task count */
458e05
+    PR_Lock(task_count_lock);
458e05
+    clean_task_count--;
458e05
+    PR_Unlock(task_count_lock);
458e05
     g_decr_active_threadcnt();
458e05
 }
458e05
 
458e05
@@ -2414,16 +2427,14 @@ replica_send_cleanruv_task(Repl_Agmt *agmt, cleanruv_data *clean_data)
458e05
 int
458e05
 is_cleaned_rid(ReplicaId rid)
458e05
 {
458e05
-    int i;
458e05
-
458e05
-    slapi_rwlock_rdlock(rid_lock);
458e05
-    for (i = 0; i < CLEANRIDSIZ && cleaned_rids[i] != 0; i++) {
458e05
+    PR_Lock(rid_lock);
458e05
+    for (size_t i = 0; i < CLEANRID_BUFSIZ; i++) {
458e05
         if (rid == cleaned_rids[i]) {
458e05
-            slapi_rwlock_unlock(rid_lock);
458e05
+            PR_Unlock(rid_lock);
458e05
             return 1;
458e05
         }
458e05
     }
458e05
-    slapi_rwlock_unlock(rid_lock);
458e05
+    PR_Unlock(rid_lock);
458e05
 
458e05
     return 0;
458e05
 }
458e05
@@ -2431,16 +2442,14 @@ is_cleaned_rid(ReplicaId rid)
458e05
 int
458e05
 is_pre_cleaned_rid(ReplicaId rid)
458e05
 {
458e05
-    int i;
458e05
-
458e05
-    slapi_rwlock_rdlock(rid_lock);
458e05
-    for (i = 0; i < CLEANRIDSIZ && pre_cleaned_rids[i] != 0; i++) {
458e05
+    PR_Lock(rid_lock);
458e05
+    for (size_t i = 0; i < CLEANRID_BUFSIZ; i++) {
458e05
         if (rid == pre_cleaned_rids[i]) {
458e05
-            slapi_rwlock_unlock(rid_lock);
458e05
+            PR_Unlock(rid_lock);
458e05
             return 1;
458e05
         }
458e05
     }
458e05
-    slapi_rwlock_unlock(rid_lock);
458e05
+    PR_Unlock(rid_lock);
458e05
 
458e05
     return 0;
458e05
 }
458e05
@@ -2453,14 +2462,14 @@ is_task_aborted(ReplicaId rid)
458e05
     if (rid == 0) {
458e05
         return 0;
458e05
     }
458e05
-    slapi_rwlock_rdlock(abort_rid_lock);
458e05
-    for (i = 0; i < CLEANRIDSIZ && aborted_rids[i] != 0; i++) {
458e05
+    PR_Lock(abort_rid_lock);
458e05
+    for (i = 0; i < CLEANRID_BUFSIZ && aborted_rids[i] != 0; i++) {
458e05
         if (rid == aborted_rids[i]) {
458e05
-            slapi_rwlock_unlock(abort_rid_lock);
458e05
+            PR_Unlock(abort_rid_lock);
458e05
             return 1;
458e05
         }
458e05
     }
458e05
-    slapi_rwlock_unlock(abort_rid_lock);
458e05
+    PR_Unlock(abort_rid_lock);
458e05
     return 0;
458e05
 }
458e05
 
458e05
@@ -2469,15 +2478,14 @@ preset_cleaned_rid(ReplicaId rid)
458e05
 {
458e05
     int i;
458e05
 
458e05
-    slapi_rwlock_wrlock(rid_lock);
458e05
-    for (i = 0; i < CLEANRIDSIZ; i++) {
458e05
+    PR_Lock(rid_lock);
458e05
+    for (i = 0; i < CLEANRID_BUFSIZ && pre_cleaned_rids[i] != rid; i++) {
458e05
         if (pre_cleaned_rids[i] == 0) {
458e05
             pre_cleaned_rids[i] = rid;
458e05
-            pre_cleaned_rids[i + 1] = 0;
458e05
             break;
458e05
         }
458e05
     }
458e05
-    slapi_rwlock_unlock(rid_lock);
458e05
+    PR_Unlock(rid_lock);
458e05
 }
458e05
 
458e05
 /*
458e05
@@ -2490,14 +2498,13 @@ set_cleaned_rid(ReplicaId rid)
458e05
 {
458e05
     int i;
458e05
 
458e05
-    slapi_rwlock_wrlock(rid_lock);
458e05
-    for (i = 0; i < CLEANRIDSIZ; i++) {
458e05
+    PR_Lock(rid_lock);
458e05
+    for (i = 0; i < CLEANRID_BUFSIZ && cleaned_rids[i] != rid; i++) {
458e05
         if (cleaned_rids[i] == 0) {
458e05
             cleaned_rids[i] = rid;
458e05
-            cleaned_rids[i + 1] = 0;
458e05
         }
458e05
     }
458e05
-    slapi_rwlock_unlock(rid_lock);
458e05
+    PR_Unlock(rid_lock);
458e05
 }
458e05
 
458e05
 /*
458e05
@@ -2569,15 +2576,14 @@ add_aborted_rid(ReplicaId rid, Replica *r, char *repl_root)
458e05
     int rc;
458e05
     int i;
458e05
 
458e05
-    slapi_rwlock_wrlock(abort_rid_lock);
458e05
-    for (i = 0; i < CLEANRIDSIZ; i++) {
458e05
+    PR_Lock(abort_rid_lock);
458e05
+    for (i = 0; i < CLEANRID_BUFSIZ; i++) {
458e05
         if (aborted_rids[i] == 0) {
458e05
             aborted_rids[i] = rid;
458e05
-            aborted_rids[i + 1] = 0;
458e05
             break;
458e05
         }
458e05
     }
458e05
-    slapi_rwlock_unlock(abort_rid_lock);
458e05
+    PR_Unlock(abort_rid_lock);
458e05
     /*
458e05
      *  Write the rid to the config entry
458e05
      */
458e05
@@ -2620,21 +2626,24 @@ delete_aborted_rid(Replica *r, ReplicaId rid, char *repl_root, int skip)
458e05
     char *data;
458e05
     char *dn;
458e05
     int rc;
458e05
-    int i;
458e05
 
458e05
     if (r == NULL)
458e05
         return;
458e05
 
458e05
     if (skip) {
458e05
         /* skip the deleting of the config, and just remove the in memory rid */
458e05
-        slapi_rwlock_wrlock(abort_rid_lock);
458e05
-        for (i = 0; i < CLEANRIDSIZ && aborted_rids[i] != rid; i++)
458e05
-            ; /* found rid, stop */
458e05
-        for (; i < CLEANRIDSIZ; i++) {
458e05
-            /* rewrite entire array */
458e05
-            aborted_rids[i] = aborted_rids[i + 1];
458e05
-        }
458e05
-        slapi_rwlock_unlock(abort_rid_lock);
458e05
+        ReplicaId new_abort_rids[CLEANRID_BUFSIZ] = {0};
458e05
+        int32_t idx = 0;
458e05
+
458e05
+        PR_Lock(abort_rid_lock);
458e05
+        for (size_t i = 0; i < CLEANRID_BUFSIZ; i++) {
458e05
+            if (aborted_rids[i] != rid) {
458e05
+                new_abort_rids[idx] = aborted_rids[i];
458e05
+                idx++;
458e05
+            }
458e05
+        }
458e05
+        memcpy(aborted_rids, new_abort_rids, sizeof(new_abort_rids));
458e05
+        PR_Unlock(abort_rid_lock);
458e05
     } else {
458e05
         /* only remove the config, leave the in-memory rid */
458e05
         dn = replica_get_dn(r);
458e05
@@ -2792,27 +2801,31 @@ bail:
458e05
 void
458e05
 remove_cleaned_rid(ReplicaId rid)
458e05
 {
458e05
-    int i;
458e05
-    /*
458e05
-     *  Remove this rid, and optimize the array
458e05
-     */
458e05
-    slapi_rwlock_wrlock(rid_lock);
458e05
+    ReplicaId new_cleaned_rids[CLEANRID_BUFSIZ] = {0};
458e05
+    ReplicaId new_pre_cleaned_rids[CLEANRID_BUFSIZ] = {0};
458e05
+    size_t idx = 0;
458e05
+
458e05
+    PR_Lock(rid_lock);
458e05
 
458e05
-    for (i = 0; i < CLEANRIDSIZ && cleaned_rids[i] != rid; i++)
458e05
-        ; /* found rid, stop */
458e05
-    for (; i < CLEANRIDSIZ; i++) {
458e05
-        /* rewrite entire array */
458e05
-        cleaned_rids[i] = cleaned_rids[i + 1];
458e05
+    for (size_t i = 0; i < CLEANRID_BUFSIZ; i++) {
458e05
+        if (cleaned_rids[i] != rid) {
458e05
+            new_cleaned_rids[idx] = cleaned_rids[i];
458e05
+            idx++;
458e05
+        }
458e05
     }
458e05
+    memcpy(cleaned_rids, new_cleaned_rids, sizeof(new_cleaned_rids));
458e05
+
458e05
     /* now do the preset cleaned rids */
458e05
-    for (i = 0; i < CLEANRIDSIZ && pre_cleaned_rids[i] != rid; i++)
458e05
-        ; /* found rid, stop */
458e05
-    for (; i < CLEANRIDSIZ; i++) {
458e05
-        /* rewrite entire array */
458e05
-        pre_cleaned_rids[i] = pre_cleaned_rids[i + 1];
458e05
+    idx = 0;
458e05
+    for (size_t i = 0; i < CLEANRID_BUFSIZ; i++) {
458e05
+        if (pre_cleaned_rids[i] != rid) {
458e05
+            new_pre_cleaned_rids[idx] = pre_cleaned_rids[i];
458e05
+            idx++;
458e05
+        }
458e05
     }
458e05
+    memcpy(pre_cleaned_rids, new_pre_cleaned_rids, sizeof(new_pre_cleaned_rids));
458e05
 
458e05
-    slapi_rwlock_unlock(rid_lock);
458e05
+    PR_Unlock(rid_lock);
458e05
 }
458e05
 
458e05
 /*
458e05
@@ -2840,16 +2853,6 @@ replica_cleanall_ruv_abort(Slapi_PBlock *pb __attribute__((unused)),
458e05
     char *ridstr = NULL;
458e05
     int rc = SLAPI_DSE_CALLBACK_OK;
458e05
 
458e05
-    if (get_abort_cleanruv_task_count() >= CLEANRIDSIZ) {
458e05
-        /* we are already running the maximum number of tasks */
458e05
-        PR_snprintf(returntext, SLAPI_DSE_RETURNTEXT_SIZE,
458e05
-                    "Exceeded maximum number of active ABORT CLEANALLRUV tasks(%d)",
458e05
-                    CLEANRIDSIZ);
458e05
-        cleanruv_log(task, -1, ABORT_CLEANALLRUV_ID, SLAPI_LOG_ERR, "%s", returntext);
458e05
-        *returncode = LDAP_OPERATIONS_ERROR;
458e05
-        return SLAPI_DSE_CALLBACK_ERROR;
458e05
-    }
458e05
-
458e05
     /* allocate new task now */
458e05
     task = slapi_new_task(slapi_entry_get_ndn(e));
458e05
 
458e05
@@ -2934,6 +2937,16 @@ replica_cleanall_ruv_abort(Slapi_PBlock *pb __attribute__((unused)),
458e05
          */
458e05
         certify_all = "no";
458e05
     }
458e05
+
458e05
+    if (check_and_set_abort_cleanruv_task_count() != LDAP_SUCCESS) {
458e05
+        /* we are already running the maximum number of tasks */
458e05
+        PR_snprintf(returntext, SLAPI_DSE_RETURNTEXT_SIZE,
458e05
+                    "Exceeded maximum number of active ABORT CLEANALLRUV tasks(%d)",
458e05
+                    CLEANRIDSIZ);
458e05
+        cleanruv_log(task, -1, ABORT_CLEANALLRUV_ID, SLAPI_LOG_ERR, "%s", returntext);
458e05
+        *returncode = LDAP_UNWILLING_TO_PERFORM;
458e05
+        goto out;
458e05
+    }
458e05
     /*
458e05
      *  Create payload
458e05
      */
458e05
@@ -3142,6 +3155,9 @@ done:
458e05
     slapi_ch_free_string(&data->certify);
458e05
     slapi_sdn_free(&data->sdn);
458e05
     slapi_ch_free((void **)&data);
458e05
+    PR_Lock(task_count_lock);
458e05
+    abort_task_count--;
458e05
+    PR_Unlock(task_count_lock);
458e05
     g_decr_active_threadcnt();
458e05
 }
458e05
 
458e05
@@ -3493,36 +3509,43 @@ replica_cleanallruv_check_ruv(char *repl_root, Repl_Agmt *agmt, char *rid_text,
458e05
     return rc;
458e05
 }
458e05
 
458e05
-static int
458e05
-get_cleanruv_task_count(void)
458e05
+/*
458e05
+ * Before starting a cleanAllRUV task make sure there are not
458e05
+ * too many task threads already running.  If everything is okay
458e05
+ * also pre-set the RID now so rebounding extended ops do not
458e05
+ * try to clean it over and over.
458e05
+ */
458e05
+int32_t
458e05
+check_and_set_cleanruv_task_count(ReplicaId rid)
458e05
 {
458e05
-    int i, count = 0;
458e05
+    int32_t rc = 0;
458e05
 
458e05
-    slapi_rwlock_wrlock(rid_lock);
458e05
-    for (i = 0; i < CLEANRIDSIZ; i++) {
458e05
-        if (pre_cleaned_rids[i] != 0) {
458e05
-            count++;
458e05
-        }
458e05
+    PR_Lock(task_count_lock);
458e05
+    if (clean_task_count >= CLEANRIDSIZ) {
458e05
+        rc = -1;
458e05
+    } else {
458e05
+        clean_task_count++;
458e05
+        preset_cleaned_rid(rid);
458e05
     }
458e05
-    slapi_rwlock_unlock(rid_lock);
458e05
+    PR_Unlock(task_count_lock);
458e05
 
458e05
-    return count;
458e05
+    return rc;
458e05
 }
458e05
 
458e05
-static int
458e05
-get_abort_cleanruv_task_count(void)
458e05
+int32_t
458e05
+check_and_set_abort_cleanruv_task_count(void)
458e05
 {
458e05
-    int i, count = 0;
458e05
+    int32_t rc = 0;
458e05
 
458e05
-    slapi_rwlock_wrlock(rid_lock);
458e05
-    for (i = 0; i < CLEANRIDSIZ; i++) {
458e05
-        if (aborted_rids[i] != 0) {
458e05
-            count++;
458e05
+    PR_Lock(task_count_lock);
458e05
+    if (abort_task_count > CLEANRIDSIZ) {
458e05
+            rc = -1;
458e05
+        } else {
458e05
+            abort_task_count++;
458e05
         }
458e05
-    }
458e05
-    slapi_rwlock_unlock(rid_lock);
458e05
+    PR_Unlock(task_count_lock);
458e05
 
458e05
-    return count;
458e05
+    return rc;
458e05
 }
458e05
 
458e05
 /*
458e05
diff --git a/ldap/servers/plugins/replication/repl_extop.c b/ldap/servers/plugins/replication/repl_extop.c
458e05
index 68e2544b4..0c2abb6d5 100644
458e05
--- a/ldap/servers/plugins/replication/repl_extop.c
458e05
+++ b/ldap/servers/plugins/replication/repl_extop.c
458e05
@@ -1393,6 +1393,12 @@ multimaster_extop_abort_cleanruv(Slapi_PBlock *pb)
458e05
         rc = LDAP_OPERATIONS_ERROR;
458e05
         goto out;
458e05
     }
458e05
+    if (check_and_set_abort_cleanruv_task_count() != LDAP_SUCCESS) {
458e05
+        cleanruv_log(NULL, rid, CLEANALLRUV_ID, SLAPI_LOG_ERR,
458e05
+                     "Exceeded maximum number of active abort CLEANALLRUV tasks(%d)", CLEANRIDSIZ);
458e05
+        rc = LDAP_UNWILLING_TO_PERFORM;
458e05
+        goto out;
458e05
+    }
458e05
     /*
458e05
      *  Prepare the abort data
458e05
      */
458e05
@@ -1499,6 +1505,7 @@ multimaster_extop_cleanruv(Slapi_PBlock *pb)
458e05
     if (force == NULL) {
458e05
         force = "no";
458e05
     }
458e05
+
458e05
     maxcsn = csn_new();
458e05
     csn_init_by_string(maxcsn, csnstr);
458e05
     /*
458e05
@@ -1535,13 +1542,21 @@ multimaster_extop_cleanruv(Slapi_PBlock *pb)
458e05
         goto free_and_return;
458e05
     }
458e05
 
458e05
+    if (check_and_set_cleanruv_task_count((ReplicaId)rid) != LDAP_SUCCESS) {
458e05
+        cleanruv_log(NULL, rid, CLEANALLRUV_ID, SLAPI_LOG_ERR,
458e05
+                     "Exceeded maximum number of active CLEANALLRUV tasks(%d)", CLEANRIDSIZ);
458e05
+        rc = LDAP_UNWILLING_TO_PERFORM;
458e05
+        goto free_and_return;
458e05
+    }
458e05
+
458e05
     if (replica_get_type(r) != REPLICA_TYPE_READONLY) {
458e05
         /*
458e05
          *  Launch the cleanruv monitoring thread.  Once all the replicas are cleaned it will release the rid
458e05
          *
458e05
          *  This will also release mtnode_ext->replica
458e05
          */
458e05
-        slapi_log_err(SLAPI_LOG_INFO, repl_plugin_name, "multimaster_extop_cleanruv - CleanAllRUV Task - Launching cleanAllRUV thread...\n");
458e05
+
458e05
+        cleanruv_log(NULL, rid, CLEANALLRUV_ID, SLAPI_LOG_ERR, "Launching cleanAllRUV thread...\n");
458e05
         data = (cleanruv_data *)slapi_ch_calloc(1, sizeof(cleanruv_data));
458e05
         if (data == NULL) {
458e05
             slapi_log_err(SLAPI_LOG_ERR, repl_plugin_name, "multimaster_extop_cleanruv - CleanAllRUV Task - Failed to allocate "
458e05
@@ -1635,7 +1650,7 @@ free_and_return:
458e05
         ber_printf(resp_bere, "{s}", CLEANRUV_ACCEPTED);
458e05
         ber_flatten(resp_bere, &resp_bval);
458e05
         slapi_pblock_set(pb, SLAPI_EXT_OP_RET_VALUE, resp_bval);
458e05
-        slapi_send_ldap_result(pb, LDAP_SUCCESS, NULL, NULL, 0, NULL);
458e05
+        slapi_send_ldap_result(pb, rc, NULL, NULL, 0, NULL);
458e05
         /* resp_bere */
458e05
         if (NULL != resp_bere) {
458e05
             ber_free(resp_bere, 1);
458e05
-- 
458e05
2.21.0
458e05