From 9e5701cdd76154fb8ffb2f7594927c30ee9f896d Mon Sep 17 00:00:00 2001
From: Howard Chu <hyc@openldap.org>
Date: Thu, 2 Jun 2022 15:55:06 +0100
Subject: [PATCH] ITS#9858 back-mdb: delay indexer task startup

until after monitor backend is set up.
---
 servers/slapd/back-mdb/config.c    | 14 +++++++++++---
 servers/slapd/back-mdb/init.c      |  6 +++++-
 servers/slapd/back-mdb/proto-mdb.h |  3 ++-
 3 files changed, 18 insertions(+), 5 deletions(-)

diff --git a/servers/slapd/back-mdb/config.c b/servers/slapd/back-mdb/config.c
index 54361a5ea..1b3cf98fe 100644
--- a/servers/slapd/back-mdb/config.c
+++ b/servers/slapd/back-mdb/config.c
@@ -400,20 +400,20 @@ done:
 	return rc;
 }
 
-void
+int
 mdb_resume_index( BackendDB *be, MDB_txn *txn )
 {
 	struct mdb_info *mdb = be->be_private;
 	MDB_cursor *curs;
 	MDB_val key, data;
-	int i, rc;
+	int i, rc, do_task = 0;
 	unsigned short *s;
 	slap_mask_t *mask;
 	AttributeDescription *ad;
 
 	rc = mdb_cursor_open( txn, mdb->mi_idxckp, &curs );
 	if ( rc )
-		return;
+		return 0;
 
 	while(( rc = mdb_cursor_get( curs, &key, &data, MDB_NEXT )) == 0) {
 		s = key.mv_data;
@@ -425,11 +425,19 @@ mdb_resume_index( BackendDB *be, MDB_txn *txn )
 				mask = data.mv_data;
 				mdb->mi_attrs[i]->ai_indexmask = mask[0];
 				mdb->mi_attrs[i]->ai_newmask = mask[1];
+				do_task = 1;
 				break;
 			}
 		}
 	}
 	mdb_cursor_close( curs );
+	return do_task;
+}
+
+void
+mdb_start_index_task( BackendDB *be )
+{
+	struct mdb_info *mdb = be->be_private;
 	ldap_pvt_thread_mutex_lock( &slapd_rq.rq_mutex );
 	mdb->mi_index_task = ldap_pvt_runqueue_insert( &slapd_rq, 36000,
 		mdb_online_index, be,
diff --git a/servers/slapd/back-mdb/init.c b/servers/slapd/back-mdb/init.c
index 0a0137470..615f912e3 100644
--- a/servers/slapd/back-mdb/init.c
+++ b/servers/slapd/back-mdb/init.c
@@ -91,6 +91,7 @@ mdb_db_open( BackendDB *be, ConfigReply *cr )
 	unsigned flags;
 	char *dbhome;
 	MDB_txn *txn;
+	int do_index = 0;
 
 	if ( be->be_suffix == NULL ) {
 		Debug( LDAP_DEBUG_ANY,
@@ -291,7 +292,7 @@ mdb_db_open( BackendDB *be, ConfigReply *cr )
 		MDB_stat st;
 		rc = mdb_stat( txn, mdb->mi_idxckp, &st );
 		if ( st.ms_entries )
-			mdb_resume_index( be, txn );
+			do_index = mdb_resume_index( be, txn );
 	}
 
 	rc = mdb_txn_commit(txn);
@@ -311,6 +312,9 @@ mdb_db_open( BackendDB *be, ConfigReply *cr )
 
 	mdb->mi_flags |= MDB_IS_OPEN;
 
+	if ( do_index )
+		mdb_start_index_task( be );
+
 	return 0;
 
 fail:
diff --git a/servers/slapd/back-mdb/proto-mdb.h b/servers/slapd/back-mdb/proto-mdb.h
index a0806dd9d..58191e186 100644
--- a/servers/slapd/back-mdb/proto-mdb.h
+++ b/servers/slapd/back-mdb/proto-mdb.h
@@ -64,7 +64,8 @@ void mdb_ad_unwind( struct mdb_info *mdb, int prev_ads );
  */
 
 int mdb_back_init_cf( BackendInfo *bi );
-void mdb_resume_index( BackendDB *be, MDB_txn *txn );
+int mdb_resume_index( BackendDB *be, MDB_txn *txn );
+void mdb_start_index_task( BackendDB *be );
 
 /*
  * dn2entry.c
-- 
2.33.0

