diff --git a/index-scheduler/src/batch.rs b/index-scheduler/src/batch.rs index 2972778c2..4831ccbca 100644 --- a/index-scheduler/src/batch.rs +++ b/index-scheduler/src/batch.rs @@ -749,10 +749,8 @@ impl IndexScheduler { let index_uid = op.index_uid(); let index = if must_create_index { // create the index if it doesn't already exist - let mut wtxn = self.env.write_txn()?; - let index = self.index_mapper.create_index(&mut wtxn, index_uid)?; - wtxn.commit()?; - index + let wtxn = self.env.write_txn()?; + self.index_mapper.create_index(wtxn, index_uid)? } else { let rtxn = self.env.read_txn()?; self.index_mapper.index(&rtxn, index_uid)? @@ -765,12 +763,11 @@ impl IndexScheduler { Ok(tasks) } Batch::IndexCreation { index_uid, primary_key, task } => { - let mut wtxn = self.env.write_txn()?; + let wtxn = self.env.write_txn()?; if self.index_mapper.exists(&wtxn, &index_uid)? { return Err(Error::IndexAlreadyExists(index_uid)); } - self.index_mapper.create_index(&mut wtxn, &index_uid)?; - wtxn.commit()?; + self.index_mapper.create_index(wtxn, &index_uid)?; self.process_batch(Batch::IndexUpdate { index_uid, primary_key, task }) } diff --git a/index-scheduler/src/index_mapper.rs b/index-scheduler/src/index_mapper.rs index a35ec2f0a..6fd844a93 100644 --- a/index-scheduler/src/index_mapper.rs +++ b/index-scheduler/src/index_mapper.rs @@ -74,26 +74,31 @@ impl IndexMapper { } /// Get or create the index. - pub fn create_index(&self, wtxn: &mut RwTxn, name: &str) -> Result { - match self.index(wtxn, name) { + pub fn create_index(&self, mut wtxn: RwTxn, name: &str) -> Result { + let ret = match self.index(&mut wtxn, name) { Ok(index) => Ok(index), Err(Error::IndexNotFound(_)) => { let uuid = Uuid::new_v4(); - self.index_mapping.put(wtxn, name, &uuid)?; + self.index_mapping.put(&mut wtxn, name, &uuid)?; let index_path = self.base_path.join(uuid.to_string()); fs::create_dir_all(&index_path)?; let index = self.create_or_open_index(&index_path)?; - // TODO: this is far from perfect. If the caller don't commit or fail his commit - // then we end up with an available index that should not exist and is actually - // not available in the index_mapping database. - self.index_map.write().unwrap().insert(uuid, Available(index.clone())); + // TODO: it would be better to lazyly create the index. But we need an Index::open function for milli. + if let Some(BeingDeleted) = + self.index_map.write().unwrap().insert(uuid, Available(index.clone())) + { + panic!("Uuid v4 conflict."); + } Ok(index) } error => error, - } + }; + let index = ret?; + wtxn.commit()?; + Ok(index) } /// Removes the index from the mapping table and the in-memory index map diff --git a/index-scheduler/src/lib.rs b/index-scheduler/src/lib.rs index 0d9c767d3..60ece740e 100644 --- a/index-scheduler/src/lib.rs +++ b/index-scheduler/src/lib.rs @@ -774,9 +774,8 @@ impl IndexScheduler { /// Create a new index without any associated task. pub fn create_raw_index(&self, name: &str) -> Result { - let mut wtxn = self.env.write_txn()?; - let index = self.index_mapper.create_index(&mut wtxn, name)?; - wtxn.commit()?; + let wtxn = self.env.write_txn()?; + let index = self.index_mapper.create_index(wtxn, name)?; Ok(index) }