8a4a2a
From b0d593b001d4ef1fb19348335fb39bd45b31764c Mon Sep 17 00:00:00 2001
8a4a2a
From: Ludwig Krispenz <lkrispen@redhat.com>
8a4a2a
Date: Fri, 8 Dec 2017 14:26:06 +0100
8a4a2a
Subject: [PATCH] Ticket 48118 - backport - changelog can be erronously rebuilt
8a4a2a
 at startup
8a4a2a
8a4a2a
---
8a4a2a
 ldap/servers/plugins/replication/cl5_api.c         | 39 ++++++++++++++++++++++
8a4a2a
 ldap/servers/plugins/replication/repl5.h           |  1 -
8a4a2a
 ldap/servers/plugins/replication/repl5_replica.c   | 39 ++--------------------
8a4a2a
 .../plugins/replication/repl5_replica_config.c     |  2 --
8a4a2a
 4 files changed, 41 insertions(+), 40 deletions(-)
8a4a2a
8a4a2a
diff --git a/ldap/servers/plugins/replication/cl5_api.c b/ldap/servers/plugins/replication/cl5_api.c
8a4a2a
index 5c2233f82..1ce8d081f 100644
8a4a2a
--- a/ldap/servers/plugins/replication/cl5_api.c
8a4a2a
+++ b/ldap/servers/plugins/replication/cl5_api.c
8a4a2a
@@ -304,6 +304,8 @@ static void _cl5ReadBerval (struct berval *bv, char** buff);
8a4a2a
 static void _cl5WriteBerval (struct berval *bv, char** buff);
8a4a2a
 static int _cl5ReadBervals (struct berval ***bv, char** buff, unsigned int size);
8a4a2a
 static int _cl5WriteBervals (struct berval **bv, char** buff, u_int32_t *size);
8a4a2a
+static int32_t _cl5CheckMaxRUV(CL5DBFile *file, RUV *maxruv);
8a4a2a
+static int32_t _cl5CheckCSNinCL(const ruv_enum_data *element, void *arg);
8a4a2a
 
8a4a2a
 /* replay iteration */
8a4a2a
 #ifdef FOR_DEBUGGING
8a4a2a
@@ -2885,6 +2887,36 @@ static int _cl5WriteBervals (struct berval **bv, char** buff, u_int32_t *size)
8a4a2a
     return CL5_SUCCESS;
8a4a2a
 }
8a4a2a
 
8a4a2a
+static int32_t
8a4a2a
+_cl5CheckCSNinCL(const ruv_enum_data *element, void *arg)
8a4a2a
+{
8a4a2a
+    CL5DBFile *file = (CL5DBFile *)arg;
8a4a2a
+    int rc = 0;
8a4a2a
+
8a4a2a
+    DBT key = {0}, data = {0};
8a4a2a
+    char csnStr[CSN_STRSIZE];
8a4a2a
+
8a4a2a
+    /* construct the key */
8a4a2a
+    key.data = csn_as_string(element->csn, PR_FALSE, csnStr);
8a4a2a
+    key.size = CSN_STRSIZE;
8a4a2a
+
8a4a2a
+    data.flags = DB_DBT_MALLOC;
8a4a2a
+
8a4a2a
+    rc = file->db->get(file->db, NULL /*txn*/, &key, &data, 0);
8a4a2a
+
8a4a2a
+    slapi_ch_free(&(data.data));
8a4a2a
+    return rc;
8a4a2a
+}
8a4a2a
+
8a4a2a
+static int32_t
8a4a2a
+_cl5CheckMaxRUV(CL5DBFile *file, RUV *maxruv)
8a4a2a
+{
8a4a2a
+    int rc = 0;
8a4a2a
+
8a4a2a
+    rc = ruv_enumerate_elements(maxruv, _cl5CheckCSNinCL, (void *)file);
8a4a2a
+
8a4a2a
+    return rc;
8a4a2a
+}
8a4a2a
 /* upgrade from db33 to db41
8a4a2a
  * 1. Run recovery on the database environment using the DB_ENV->open method
8a4a2a
  * 2. Remove any Berkeley DB environment using the DB_ENV->remove method 
8a4a2a
@@ -4248,6 +4280,13 @@ static int _cl5WriteRUV (CL5DBFile *file, PRBool purge)
8a4a2a
 		rc = ruv_to_bervals(file->maxRUV, &vals);
8a4a2a
 	}
8a4a2a
 
8a4a2a
+	if (!purge && _cl5CheckMaxRUV(file, file->maxRUV)) {
8a4a2a
+		slapi_log_err(SLAPI_LOG_ERR, repl_plugin_name_cl,
8a4a2a
+				"_cl5WriteRUV - changelog maxRUV not found in changelog for file %s\n",
8a4a2a
+				file->name);
8a4a2a
+		return CL5_DB_ERROR;
8a4a2a
+	}
8a4a2a
+
8a4a2a
 	key.size = CSN_STRSIZE;
8a4a2a
     
8a4a2a
 	rc = _cl5WriteBervals (vals, &buff, &data.size);
8a4a2a
diff --git a/ldap/servers/plugins/replication/repl5.h b/ldap/servers/plugins/replication/repl5.h
8a4a2a
index 718f64ef1..9c4789f9e 100644
8a4a2a
--- a/ldap/servers/plugins/replication/repl5.h
8a4a2a
+++ b/ldap/servers/plugins/replication/repl5.h
8a4a2a
@@ -620,7 +620,6 @@ Object *replica_get_for_backend (const char *be_name);
8a4a2a
 void replica_set_purge_delay (Replica *r, PRUint32 purge_delay);
8a4a2a
 void replica_set_tombstone_reap_interval (Replica *r, long interval);
8a4a2a
 void replica_update_ruv_consumer (Replica *r, RUV *supplier_ruv);
8a4a2a
-void replica_set_ruv_dirty (Replica *r);
8a4a2a
 Slapi_Entry *get_in_memory_ruv(Slapi_DN *suffix_sdn);
8a4a2a
 int replica_write_ruv (Replica *r);
8a4a2a
 char *replica_get_dn(Replica *r);
8a4a2a
diff --git a/ldap/servers/plugins/replication/repl5_replica.c b/ldap/servers/plugins/replication/repl5_replica.c
8a4a2a
index 7927ac30a..3c7281a42 100644
8a4a2a
--- a/ldap/servers/plugins/replication/repl5_replica.c
8a4a2a
+++ b/ldap/servers/plugins/replication/repl5_replica.c
8a4a2a
@@ -46,7 +46,6 @@ struct replica {
8a4a2a
 	char*   legacy_purl;            /* partial url of the legacy supplier   */
8a4a2a
 	ReplicaId repl_rid;				/* replicaID							*/
8a4a2a
 	Object	*repl_ruv;				/* replica update vector				*/
8a4a2a
-	PRBool repl_ruv_dirty;          /* Dirty flag for ruv                   */
8a4a2a
 	CSNPL *min_csn_pl;              /* Pending list for minimal CSN         */
8a4a2a
 	void *csn_pl_reg_id;            /* registration assignment for csn callbacks */
8a4a2a
 	unsigned long repl_state_flags;	/* state flags							*/
8a4a2a
@@ -855,7 +854,6 @@ replica_set_ruv (Replica *r, RUV *ruv)
8a4a2a
     }
8a4a2a
 
8a4a2a
 	r->repl_ruv = object_new((void*)ruv, (FNFree)ruv_destroy);
8a4a2a
-	r->repl_ruv_dirty = PR_TRUE;
8a4a2a
 
8a4a2a
 	replica_unlock(r->repl_lock);
8a4a2a
 }
8a4a2a
@@ -941,11 +939,6 @@ replica_update_ruv(Replica *r, const CSN *updated_csn, const char *replica_purl)
8a4a2a
 						slapi_sdn_get_dn(r->repl_root),
8a4a2a
 						csn_as_string(updated_csn, PR_FALSE, csn_str));
8a4a2a
 				}
8a4a2a
-				else
8a4a2a
-				{
8a4a2a
-					/* RUV updated - mark as dirty */
8a4a2a
-					r->repl_ruv_dirty = PR_TRUE;
8a4a2a
-				}
8a4a2a
 			}
8a4a2a
 			else
8a4a2a
 			{
8a4a2a
@@ -1526,8 +1519,6 @@ replica_dump(Replica *r)
8a4a2a
     slapi_log_err(SLAPI_LOG_REPL, repl_plugin_name, "\tupdate dn: %s\n",
8a4a2a
             updatedn_list? updatedn_list : "not configured");
8a4a2a
     slapi_ch_free_string(&updatedn_list);
8a4a2a
-    slapi_log_err(SLAPI_LOG_REPL, repl_plugin_name, "\truv: %s configured and is %sdirty\n",
8a4a2a
-                    r->repl_ruv ? "" : "not", r->repl_ruv_dirty ? "" : "not ");
8a4a2a
     slapi_log_err(SLAPI_LOG_REPL, repl_plugin_name, "\tCSN generator: %s configured\n",
8a4a2a
                     r->repl_csngen ? "" : "not");
8a4a2a
 	/* JCMREPL - Dump Referrals */
8a4a2a
@@ -1877,7 +1868,6 @@ int replica_check_for_data_reload (Replica *r, void *arg)
8a4a2a
 
8a4a2a
                     ruv_force_csn_update_from_ruv(upper_bound_ruv, r_ruv, 
8a4a2a
                             "Force update of database RUV (from CL RUV) -> ", SLAPI_LOG_NOTICE);
8a4a2a
-                    replica_set_ruv_dirty(r);
8a4a2a
                 }
8a4a2a
                 
8a4a2a
             } else {
8a4a2a
@@ -2994,12 +2984,6 @@ replica_write_ruv (Replica *r)
8a4a2a
 
8a4a2a
 	replica_lock(r->repl_lock);
8a4a2a
 
8a4a2a
-	if (!r->repl_ruv_dirty)
8a4a2a
-	{
8a4a2a
-		replica_unlock(r->repl_lock);
8a4a2a
-		return rc;
8a4a2a
-	}
8a4a2a
-
8a4a2a
 	PR_ASSERT (r->repl_ruv);
8a4a2a
 	
8a4a2a
 	ruv_to_smod ((RUV*)object_get_data(r->repl_ruv), &smod);
8a4a2a
@@ -3034,19 +3018,13 @@ replica_write_ruv (Replica *r)
8a4a2a
     /* ruv does not exist - create one */
8a4a2a
     replica_lock(r->repl_lock);
8a4a2a
 
8a4a2a
-    if (rc == LDAP_SUCCESS)
8a4a2a
-    {
8a4a2a
-        r->repl_ruv_dirty = PR_FALSE;
8a4a2a
-    }
8a4a2a
-    else if (rc == LDAP_NO_SUCH_OBJECT)
8a4a2a
+    if (rc == LDAP_NO_SUCH_OBJECT)
8a4a2a
     {
8a4a2a
         /* this includes an internal operation - but since this only happens
8a4a2a
            during server startup - its ok that we have lock around it */
8a4a2a
         rc = _replica_configure_ruv  (r, PR_TRUE);
8a4a2a
-        if (rc == 0)
8a4a2a
-            r->repl_ruv_dirty = PR_FALSE;
8a4a2a
     }
8a4a2a
-	else /* error */
8a4a2a
+	else if (rc != LDAP_SUCCESS) /* error */
8a4a2a
 	{
8a4a2a
 		slapi_log_err(SLAPI_LOG_REPL, repl_plugin_name, 
8a4a2a
 			"replica_write_ruv - Failed to update RUV tombstone for %s; "
8a4a2a
@@ -3570,7 +3548,6 @@ replica_create_ruv_tombstone(Replica *r)
8a4a2a
 
8a4a2a
             if (ruv_init_new(csnstr, r->repl_rid, purl, &ruv) == RUV_SUCCESS){
8a4a2a
                 r->repl_ruv = object_new((void*)ruv, (FNFree)ruv_destroy);
8a4a2a
-                r->repl_ruv_dirty = PR_TRUE;
8a4a2a
                 return_value = LDAP_SUCCESS;
8a4a2a
             } else {
8a4a2a
                 slapi_log_err(SLAPI_LOG_ERR, repl_plugin_name, "replica_create_ruv_tombstone - "
8a4a2a
@@ -3610,8 +3587,6 @@ replica_create_ruv_tombstone(Replica *r)
8a4a2a
     slapi_add_internal_pb(pb);
8a4a2a
     e = NULL; /* add consumes e, upon success or failure */
8a4a2a
     slapi_pblock_get(pb, SLAPI_PLUGIN_INTOP_RESULT, &return_value);
8a4a2a
-    if (return_value == LDAP_SUCCESS)
8a4a2a
-        r->repl_ruv_dirty = PR_FALSE;
8a4a2a
 		
8a4a2a
 done:
8a4a2a
     slapi_entry_free (e);
8a4a2a
@@ -3930,7 +3905,6 @@ replica_strip_cleaned_rids(Replica *r)
8a4a2a
     ruv_get_cleaned_rids(ruv, rid);
8a4a2a
     while(rid[i] != 0){
8a4a2a
         ruv_delete_replica(ruv, rid[i]);
8a4a2a
-        replica_set_ruv_dirty(r);
8a4a2a
         if (replica_write_ruv(r)) {
8a4a2a
             slapi_log_err(SLAPI_LOG_REPL, repl_plugin_name,
8a4a2a
                     "replica_strip_cleaned_rids - Failed to write RUV\n");
8a4a2a
@@ -4052,15 +4026,6 @@ replica_update_ruv_consumer(Replica *r, RUV *supplier_ruv)
8a4a2a
 	}
8a4a2a
 }
8a4a2a
 
8a4a2a
-void 
8a4a2a
-replica_set_ruv_dirty(Replica *r)
8a4a2a
-{
8a4a2a
-	PR_ASSERT(r);
8a4a2a
-	replica_lock(r->repl_lock);
8a4a2a
-	r->repl_ruv_dirty = PR_TRUE;
8a4a2a
-	replica_unlock(r->repl_lock);
8a4a2a
-}
8a4a2a
-
8a4a2a
 PRBool
8a4a2a
 replica_is_state_flag_set(Replica *r, PRInt32 flag)
8a4a2a
 {
8a4a2a
diff --git a/ldap/servers/plugins/replication/repl5_replica_config.c b/ldap/servers/plugins/replication/repl5_replica_config.c
8a4a2a
index 814f1cac0..128c9423a 100644
8a4a2a
--- a/ldap/servers/plugins/replication/repl5_replica_config.c
8a4a2a
+++ b/ldap/servers/plugins/replication/repl5_replica_config.c
8a4a2a
@@ -1034,7 +1034,6 @@ replica_config_change_type_and_id (Replica *r, const char *new_type,
8a4a2a
                     replica_reset_csn_pl(r);
8a4a2a
                 }
8a4a2a
                 ruv_delete_replica(ruv, oldrid);
8a4a2a
-                replica_set_ruv_dirty(r);
8a4a2a
                 cl5CleanRUV(oldrid);
8a4a2a
                 replica_set_csn_assigned(r);
8a4a2a
             }
8a4a2a
@@ -1454,7 +1453,6 @@ replica_execute_cleanruv_task (Object *r, ReplicaId rid, char *returntext /* not
8a4a2a
 		return LDAP_UNWILLING_TO_PERFORM;
8a4a2a
 	}
8a4a2a
 	rc = ruv_delete_replica(local_ruv, rid);
8a4a2a
-	replica_set_ruv_dirty(replica);
8a4a2a
 	if (replica_write_ruv(replica)) {
8a4a2a
 		slapi_log_err(SLAPI_LOG_REPL, repl_plugin_name, "cleanAllRUV_task - Could not write RUV\n");
8a4a2a
 	}
8a4a2a
-- 
8a4a2a
2.13.6
8a4a2a