From a2188f928fa0a8d08c2d5de2dc74e73f7e569477 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tim=20Vis=C3=A9e?= Date: Tue, 15 Jul 2025 16:33:53 +0200 Subject: [PATCH] After payload index migration, delete RocksDB files (#6873) * After payload index migration, delete RocksDB files * Add comment on why it is safe to not clean up RocksDB files in some cases * Update lib/segment/src/index/struct_payload_index.rs Co-authored-by: Roman Titov * Reformat --------- Co-authored-by: Roman Titov --- lib/segment/src/index/struct_payload_index.rs | 37 +++++++++++++++++++ 1 file changed, 37 insertions(+) diff --git a/lib/segment/src/index/struct_payload_index.rs b/lib/segment/src/index/struct_payload_index.rs index 4acf485a14..ccfc0b771b 100644 --- a/lib/segment/src/index/struct_payload_index.rs +++ b/lib/segment/src/index/struct_payload_index.rs @@ -390,6 +390,43 @@ impl StructPayloadIndex { index.load_all_fields(create)?; + // If we have a RocksDB instance, but no more index using it, completely delete it here + #[cfg(feature = "rocksdb")] + if !index.config.indices.any_is_rocksdb() + && let Some(db) = index.db.take() + { + match Arc::try_unwrap(db) { + Ok(db) => { + log::trace!( + "Deleting RocksDB for payload indices, no payload index uses it anymore" + ); + + // Close RocksDB instance + let db = db.into_inner(); + drop(db); + + // Destroy all RocksDB files + let options = crate::common::rocksdb_wrapper::make_db_options(); + match rocksdb::DB::destroy(&options, &index.path) { + Ok(_) => log::debug!("Deleted RocksDB for payload indices"), + Err(err) => { + log::warn!("Failed to delete RocksDB for payload indices: {err}") + } + } + } + // Here we don't have exclusive ownership of RocksDB, which prevents us from + // controlling and closing the instance. Because of it, we cannot destroy the + // RocksDB files, and leave them behind. We don't consider this a problem, because + // a future optimization run will get rid of these files. + Err(db) => { + log::warn!( + "RocksDB for payload indices could not be deleted, does not have exclusive ownership" + ); + index.db.replace(db); + } + } + } + Ok(index) }