Blame SOURCES/0043-Issue-4644-Large-updates-can-reset-the-CLcache-to-th.patch

36233f
From f05f5f20a468efa82d13a99687ac5d3a5d80a3c9 Mon Sep 17 00:00:00 2001
36233f
From: tbordaz <tbordaz@redhat.com>
36233f
Date: Tue, 23 Feb 2021 13:42:31 +0100
36233f
Subject: [PATCH] Issue 4644 - Large updates can reset the CLcache to the
36233f
 beginning of the changelog (#4647)
36233f
36233f
Bug description:
36233f
	The replication agreements are using bulk load to load updates.
36233f
	For bulk load it uses a cursor with DB_MULTIPLE_KEY and DB_NEXT.
36233f
	Before using the cursor, it must be initialized with DB_SET.
36233f
36233f
	If during the cursor/DB_SET the CSN refers to an update that is larger than
36233f
	the size of the provided buffer, then the cursor remains not initialized and
36233f
	c_get returns DB_BUFFER_SMALL.
36233f
36233f
	The consequence is that the next c_get(DB_MULTIPLE_KEY and DB_NEXT) will return the
36233f
	first record in the changelog DB. This break CLcache.
36233f
36233f
Fix description:
36233f
	The fix is to harden cursor initialization so that if DB_SET fails
36233f
	because of DB_BUFFER_SMALL. It reallocates buf_data and retries a DB_SET.
36233f
	If DB_SET can not be initialized it logs a warning.
36233f
36233f
	The patch also changes the behaviour of the fix #4492.
36233f
	#4492 detected a massive (1day) jump prior the starting csn and ended the
36233f
	replication session. If the jump was systematic, for example
36233f
	if the CLcache got broken because of a too large updates, then
36233f
	replication was systematically stopped.
36233f
	This patch suppress the systematically stop, letting RA doing a big jump.
36233f
	From #4492 only remains the warning.
36233f
36233f
relates: https://github.com/389ds/389-ds-base/issues/4644
36233f
36233f
Reviewed by: Pierre Rogier (Thanks !!!!)
36233f
36233f
Platforms tested: F31
36233f
---
36233f
 .../servers/plugins/replication/cl5_clcache.c | 68 +++++++++++++++----
36233f
 1 file changed, 53 insertions(+), 15 deletions(-)
36233f
36233f
diff --git a/ldap/servers/plugins/replication/cl5_clcache.c b/ldap/servers/plugins/replication/cl5_clcache.c
36233f
index fcbca047a..90dec4d54 100644
36233f
--- a/ldap/servers/plugins/replication/cl5_clcache.c
36233f
+++ b/ldap/servers/plugins/replication/cl5_clcache.c
36233f
@@ -370,9 +370,7 @@ clcache_load_buffer(CLC_Buffer *buf, CSN **anchorCSN, int *continue_on_miss, cha
36233f
             }
36233f
             csn_as_string(buf->buf_current_csn, 0, curr);
36233f
             slapi_log_err(loglevel, buf->buf_agmt_name,
36233f
-                      "clcache_load_buffer - bulk load cursor (%s) is lower than starting csn %s. Ending session.\n", curr, initial_starting_csn);
36233f
-            /* it just end the session with UPDATE_NO_MORE_UPDATES */
36233f
-            rc = CLC_STATE_DONE;
36233f
+                      "clcache_load_buffer - bulk load cursor (%s) is lower than starting csn %s.\n", curr, initial_starting_csn);
36233f
         }
36233f
     }
36233f
 
36233f
@@ -413,10 +411,7 @@ clcache_load_buffer(CLC_Buffer *buf, CSN **anchorCSN, int *continue_on_miss, cha
36233f
                     }
36233f
                     csn_as_string(buf->buf_current_csn, 0, curr);
36233f
                     slapi_log_err(loglevel, buf->buf_agmt_name,
36233f
-                            "clcache_load_buffer - (DB_SET_RANGE) bulk load cursor (%s) is lower than starting csn %s. Ending session.\n", curr, initial_starting_csn);
36233f
-                    rc = DB_NOTFOUND;
36233f
-
36233f
-                    return rc;
36233f
+                            "clcache_load_buffer - (DB_SET_RANGE) bulk load cursor (%s) is lower than starting csn %s.\n", curr, initial_starting_csn);
36233f
                 }
36233f
             }
36233f
         }
36233f
@@ -444,6 +439,42 @@ clcache_load_buffer(CLC_Buffer *buf, CSN **anchorCSN, int *continue_on_miss, cha
36233f
     return rc;
36233f
 }
36233f
 
36233f
+/* Set a cursor to a specific key (buf->buf_key)
36233f
+ * In case buf_data is too small to receive the value, DB_SET fails
36233f
+ * (DB_BUFFER_SMALL). This let the cursor uninitialized that is
36233f
+ * problematic because further cursor DB_NEXT will reset the cursor
36233f
+ * to the beginning of the CL.
36233f
+ * If buf_data is too small, this function reallocates enough space
36233f
+ *
36233f
+ * It returns the return code of cursor->c_get
36233f
+ */
36233f
+static int
36233f
+clcache_cursor_set(DBC *cursor, CLC_Buffer *buf)
36233f
+{
36233f
+    int rc;
36233f
+    uint32_t ulen;
36233f
+    uint32_t dlen;
36233f
+    uint32_t size;
36233f
+
36233f
+    rc = cursor->c_get(cursor, &buf->buf_key, &buf->buf_data, DB_SET);
36233f
+    if (rc == DB_BUFFER_SMALL) {
36233f
+        uint32_t ulen;
36233f
+
36233f
+        /* Fortunately, buf->buf_data.size has been set by
36233f
+         * c_get() to the actual data size needed. So we can
36233f
+         * reallocate the data buffer and try to set again.
36233f
+         */
36233f
+        ulen = buf->buf_data.ulen;
36233f
+        buf->buf_data.ulen = (buf->buf_data.size / DEFAULT_CLC_BUFFER_PAGE_SIZE + 1) * DEFAULT_CLC_BUFFER_PAGE_SIZE;
36233f
+        buf->buf_data.data = slapi_ch_realloc(buf->buf_data.data, buf->buf_data.ulen);
36233f
+        slapi_log_err(SLAPI_LOG_REPL, buf->buf_agmt_name,
36233f
+                      "clcache_cursor_set - buf data len reallocated %d -> %d bytes (DB_BUFFER_SMALL)\n",
36233f
+                      ulen, buf->buf_data.ulen);
36233f
+        rc = cursor->c_get(cursor, &buf->buf_key, &buf->buf_data, DB_SET);
36233f
+    }
36233f
+    return rc;
36233f
+}
36233f
+
36233f
 static int
36233f
 clcache_load_buffer_bulk(CLC_Buffer *buf, int flag)
36233f
 {
36233f
@@ -478,17 +509,24 @@ retry:
36233f
 
36233f
         if (use_flag == DB_NEXT) {
36233f
             /* For bulk read, position the cursor before read the next block */
36233f
-            rc = cursor->c_get(cursor,
36233f
-                               &buf->buf_key,
36233f
-                               &buf->buf_data,
36233f
-                               DB_SET);
36233f
+            rc = clcache_cursor_set(cursor, buf);
36233f
         }
36233f
 
36233f
-        /*
36233f
-         * Continue if the error is no-mem since we don't need to
36233f
-         * load in the key record anyway with DB_SET.
36233f
-         */
36233f
         if (0 == rc || DB_BUFFER_SMALL == rc) {
36233f
+           /*
36233f
+            * It should not have failed  with DB_BUFFER_SMALL as we tried
36233f
+            * to adjust buf_data in clcache_cursor_set.
36233f
+            * But if it failed with DB_BUFFER_SMALL, there is a risk in clcache_cursor_get
36233f
+            * that the cursor will be reset to the beginning of the changelog.
36233f
+            * Returning an error at this point will stop replication that is
36233f
+            * a risk. So just accept the risk of a reset to the beginning of the CL
36233f
+            * and log an alarming message.
36233f
+            */
36233f
+           if (rc == DB_BUFFER_SMALL) {
36233f
+               slapi_log_err(SLAPI_LOG_WARNING, buf->buf_agmt_name,
36233f
+                             "clcache_load_buffer_bulk - Fail to position on csn=%s from the changelog (too large update ?). Risk of full CL evaluation.\n",
36233f
+                             (char *)buf->buf_key.data);
36233f
+           }
36233f
             rc = clcache_cursor_get(cursor, buf, use_flag);
36233f
         }
36233f
     }
36233f
-- 
36233f
2.31.1
36233f