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

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