2022-09-13 17:46:07 +08:00
|
|
|
use crate::{
|
|
|
|
autobatcher::BatchKind,
|
|
|
|
task::{KindWithContent, Status},
|
|
|
|
Error, IndexScheduler, Result, TaskId,
|
|
|
|
};
|
2022-09-14 04:38:43 +08:00
|
|
|
use index::{Settings, Unchecked};
|
2022-09-13 17:46:07 +08:00
|
|
|
use milli::{
|
|
|
|
heed::{RoTxn, RwTxn},
|
|
|
|
update::IndexDocumentsMethod,
|
|
|
|
};
|
2022-09-09 07:09:50 +08:00
|
|
|
use uuid::Uuid;
|
2022-09-07 06:10:14 +08:00
|
|
|
|
|
|
|
use crate::{task::Kind, Task};
|
|
|
|
|
2022-09-08 02:08:07 +08:00
|
|
|
pub(crate) enum Batch {
|
2022-09-07 06:10:14 +08:00
|
|
|
Cancel(Task),
|
|
|
|
Snapshot(Vec<Task>),
|
|
|
|
Dump(Vec<Task>),
|
2022-09-13 17:46:07 +08:00
|
|
|
// IndexSpecific { index_uid: String, kind: BatchKind },
|
|
|
|
DocumentAddition {
|
|
|
|
index_uid: String,
|
|
|
|
primary_key: Option<String>,
|
|
|
|
content_files: Vec<Uuid>,
|
|
|
|
tasks: Vec<Task>,
|
|
|
|
},
|
|
|
|
SettingsAndDocumentAddition {
|
|
|
|
index_uid: String,
|
|
|
|
|
|
|
|
primary_key: Option<String>,
|
|
|
|
content_files: Vec<Uuid>,
|
|
|
|
document_addition_tasks: Vec<Task>,
|
|
|
|
|
2022-09-14 04:38:43 +08:00
|
|
|
settings: Vec<Settings<Unchecked>>,
|
2022-09-13 17:46:07 +08:00
|
|
|
settings_tasks: Vec<Task>,
|
|
|
|
},
|
2022-09-09 07:09:50 +08:00
|
|
|
}
|
|
|
|
|
2022-09-07 06:10:14 +08:00
|
|
|
impl IndexScheduler {
|
2022-09-13 17:46:07 +08:00
|
|
|
pub(crate) fn create_next_batch_index(
|
|
|
|
&self,
|
|
|
|
rtxn: &RoTxn,
|
|
|
|
index_uid: String,
|
|
|
|
batch: BatchKind,
|
|
|
|
) -> Result<Option<Batch>> {
|
|
|
|
match batch {
|
2022-09-14 04:38:43 +08:00
|
|
|
BatchKind::DocumentClear { ids } => todo!(),
|
2022-09-13 17:46:07 +08:00
|
|
|
BatchKind::DocumentAddition { addition_ids } => todo!(),
|
2022-09-14 06:34:02 +08:00
|
|
|
BatchKind::DocumentUpdate { update_ids } => todo!(),
|
2022-09-13 17:46:07 +08:00
|
|
|
BatchKind::DocumentDeletion { deletion_ids } => todo!(),
|
2022-09-14 04:38:43 +08:00
|
|
|
BatchKind::ClearAndSettings {
|
2022-09-13 17:46:07 +08:00
|
|
|
other,
|
|
|
|
settings_ids,
|
|
|
|
} => todo!(),
|
|
|
|
BatchKind::SettingsAndDocumentAddition {
|
|
|
|
addition_ids,
|
|
|
|
settings_ids,
|
|
|
|
} => {
|
|
|
|
// you're not supposed to create an empty BatchKind.
|
|
|
|
assert!(addition_ids.len() > 0);
|
|
|
|
assert!(settings_ids.len() > 0);
|
|
|
|
|
|
|
|
let document_addition_tasks = addition_ids
|
|
|
|
.iter()
|
|
|
|
.map(|tid| {
|
|
|
|
self.get_task(rtxn, *tid)
|
|
|
|
.and_then(|task| task.ok_or(Error::CorruptedTaskQueue))
|
|
|
|
})
|
|
|
|
.collect::<Result<Vec<_>>>()?;
|
|
|
|
let settings_tasks = settings_ids
|
|
|
|
.iter()
|
|
|
|
.map(|tid| {
|
|
|
|
self.get_task(rtxn, *tid)
|
|
|
|
.and_then(|task| task.ok_or(Error::CorruptedTaskQueue))
|
|
|
|
})
|
|
|
|
.collect::<Result<Vec<_>>>()?;
|
|
|
|
|
2022-09-13 20:59:03 +08:00
|
|
|
let primary_key = match &document_addition_tasks[0].kind {
|
2022-09-14 06:34:02 +08:00
|
|
|
KindWithContent::DocumentAddition { primary_key, .. } => primary_key.clone(),
|
2022-09-13 17:46:07 +08:00
|
|
|
_ => unreachable!(),
|
|
|
|
};
|
|
|
|
let content_files = document_addition_tasks
|
|
|
|
.iter()
|
|
|
|
.map(|task| match task.kind {
|
2022-09-14 06:34:02 +08:00
|
|
|
KindWithContent::DocumentAddition { content_file, .. } => content_file,
|
2022-09-13 17:46:07 +08:00
|
|
|
_ => unreachable!(),
|
|
|
|
})
|
|
|
|
.collect();
|
|
|
|
|
|
|
|
let settings = settings_tasks
|
|
|
|
.iter()
|
2022-09-13 20:59:03 +08:00
|
|
|
.map(|task| match &task.kind {
|
2022-09-14 04:38:43 +08:00
|
|
|
KindWithContent::Settings { new_settings, .. } => new_settings.clone(),
|
2022-09-13 17:46:07 +08:00
|
|
|
_ => unreachable!(),
|
|
|
|
})
|
|
|
|
.collect();
|
|
|
|
|
|
|
|
Ok(Some(Batch::SettingsAndDocumentAddition {
|
|
|
|
index_uid,
|
|
|
|
primary_key,
|
|
|
|
content_files,
|
|
|
|
document_addition_tasks,
|
|
|
|
settings,
|
|
|
|
settings_tasks,
|
|
|
|
}))
|
|
|
|
}
|
2022-09-14 06:34:02 +08:00
|
|
|
BatchKind::SettingsAndDocumentUpdate {
|
|
|
|
update_ids,
|
|
|
|
settings_ids,
|
|
|
|
} => todo!(),
|
2022-09-13 17:46:07 +08:00
|
|
|
BatchKind::Settings { settings_ids } => todo!(),
|
2022-09-14 04:38:43 +08:00
|
|
|
BatchKind::IndexCreation { id } => todo!(),
|
|
|
|
BatchKind::IndexDeletion { ids } => todo!(),
|
|
|
|
BatchKind::IndexUpdate { id } => todo!(),
|
|
|
|
BatchKind::IndexSwap { id } => todo!(),
|
|
|
|
BatchKind::IndexRename { id } => todo!(),
|
2022-09-13 17:46:07 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-09-07 06:10:14 +08:00
|
|
|
/// Create the next batch to be processed;
|
|
|
|
/// 1. We get the *last* task to cancel.
|
|
|
|
/// 2. We get the *next* snapshot to process.
|
|
|
|
/// 3. We get the *next* dump to process.
|
|
|
|
/// 4. We get the *next* tasks to process for a specific index.
|
2022-09-09 07:40:28 +08:00
|
|
|
pub(crate) fn create_next_batch(&self, rtxn: &RoTxn) -> Result<Option<Batch>> {
|
2022-09-07 06:10:14 +08:00
|
|
|
let enqueued = &self.get_status(rtxn, Status::Enqueued)?;
|
|
|
|
let to_cancel = self.get_kind(rtxn, Kind::CancelTask)? & enqueued;
|
|
|
|
|
|
|
|
// 1. we get the last task to cancel.
|
|
|
|
if let Some(task_id) = to_cancel.max() {
|
2022-09-09 07:40:28 +08:00
|
|
|
return Ok(Some(Batch::Cancel(
|
2022-09-07 06:10:14 +08:00
|
|
|
self.get_task(rtxn, task_id)?
|
|
|
|
.ok_or(Error::CorruptedTaskQueue)?,
|
2022-09-09 07:40:28 +08:00
|
|
|
)));
|
2022-09-07 06:10:14 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// 2. we batch the snapshot.
|
|
|
|
let to_snapshot = self.get_kind(rtxn, Kind::Snapshot)? & enqueued;
|
|
|
|
if !to_snapshot.is_empty() {
|
2022-09-09 07:40:28 +08:00
|
|
|
return Ok(Some(Batch::Snapshot(
|
|
|
|
self.get_existing_tasks(rtxn, to_snapshot)?,
|
|
|
|
)));
|
2022-09-07 06:10:14 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// 3. we batch the dumps.
|
|
|
|
let to_dump = self.get_kind(rtxn, Kind::DumpExport)? & enqueued;
|
|
|
|
if !to_dump.is_empty() {
|
2022-09-09 07:40:28 +08:00
|
|
|
return Ok(Some(Batch::Dump(self.get_existing_tasks(rtxn, to_dump)?)));
|
2022-09-07 06:10:14 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// 4. We take the next task and try to batch all the tasks associated with this index.
|
|
|
|
if let Some(task_id) = enqueued.min() {
|
|
|
|
let task = self
|
|
|
|
.get_task(rtxn, task_id)?
|
|
|
|
.ok_or(Error::CorruptedTaskQueue)?;
|
2022-09-09 07:40:28 +08:00
|
|
|
|
|
|
|
// This is safe because all the remaining task are associated with
|
|
|
|
// AT LEAST one index. We can use the right or left one it doesn't
|
|
|
|
// matter.
|
|
|
|
let index_name = task.indexes().unwrap()[0];
|
|
|
|
|
|
|
|
let index = self.get_index(rtxn, &index_name)? & enqueued;
|
|
|
|
|
|
|
|
let enqueued = enqueued
|
|
|
|
.into_iter()
|
|
|
|
.map(|task_id| {
|
|
|
|
self.get_task(rtxn, task_id)
|
|
|
|
.and_then(|task| task.ok_or(Error::CorruptedTaskQueue))
|
|
|
|
.map(|task| (task.uid, task.kind.as_kind()))
|
|
|
|
})
|
|
|
|
.collect::<Result<Vec<_>>>()?;
|
|
|
|
|
2022-09-13 17:46:07 +08:00
|
|
|
if let Some(batchkind) = crate::autobatcher::autobatch(enqueued) {
|
|
|
|
return self.create_next_batch_index(rtxn, index_name.to_string(), batchkind);
|
|
|
|
}
|
2022-09-07 06:10:14 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// If we found no tasks then we were notified for something that got autobatched
|
|
|
|
// somehow and there is nothing to do.
|
2022-09-09 07:40:28 +08:00
|
|
|
Ok(None)
|
2022-09-07 06:10:14 +08:00
|
|
|
}
|
|
|
|
|
2022-09-09 18:16:19 +08:00
|
|
|
pub(crate) fn process_batch(&self, wtxn: &mut RwTxn, batch: Batch) -> Result<Vec<Task>> {
|
|
|
|
match batch {
|
2022-09-13 17:46:07 +08:00
|
|
|
Batch::Cancel(_) => todo!(),
|
|
|
|
Batch::Snapshot(_) => todo!(),
|
|
|
|
Batch::Dump(_) => todo!(),
|
|
|
|
Batch::DocumentAddition {
|
|
|
|
index_uid,
|
|
|
|
primary_key,
|
|
|
|
content_files,
|
|
|
|
tasks,
|
|
|
|
} => todo!(),
|
|
|
|
Batch::SettingsAndDocumentAddition {
|
|
|
|
index_uid,
|
|
|
|
primary_key,
|
|
|
|
content_files,
|
|
|
|
document_addition_tasks,
|
|
|
|
settings,
|
|
|
|
settings_tasks,
|
|
|
|
} => {
|
|
|
|
let index = self.create_index(wtxn, &index_uid)?;
|
|
|
|
let mut updated_tasks = Vec::new();
|
|
|
|
|
|
|
|
/*
|
|
|
|
let ret = index.update_settings(settings)?;
|
|
|
|
for (ret, task) in ret.iter().zip(settings_tasks) {
|
|
|
|
match ret {
|
|
|
|
Ok(ret) => task.status = Some(ret),
|
|
|
|
Err(err) => task.error = Some(err),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
*/
|
|
|
|
|
|
|
|
/*
|
|
|
|
for (ret, task) in ret.iter().zip(settings_tasks) {
|
|
|
|
match ret {
|
|
|
|
Ok(ret) => task.status = Some(ret),
|
|
|
|
Err(err) => task.error = Some(err),
|
|
|
|
}
|
|
|
|
updated_tasks.push(task);
|
|
|
|
}
|
|
|
|
*/
|
|
|
|
|
|
|
|
let ret = index.update_documents(
|
|
|
|
IndexDocumentsMethod::ReplaceDocuments,
|
|
|
|
primary_key,
|
2022-09-13 20:59:03 +08:00
|
|
|
self.file_store.clone(),
|
2022-09-13 17:46:07 +08:00
|
|
|
content_files.into_iter(),
|
|
|
|
)?;
|
|
|
|
|
2022-09-13 20:59:03 +08:00
|
|
|
for (ret, mut task) in ret.iter().zip(document_addition_tasks.into_iter()) {
|
2022-09-13 17:46:07 +08:00
|
|
|
match ret {
|
|
|
|
Ok(ret) => task.info = Some(format!("{:?}", ret)),
|
|
|
|
Err(err) => task.error = Some(err.to_string()),
|
2022-09-09 18:16:19 +08:00
|
|
|
}
|
2022-09-13 17:46:07 +08:00
|
|
|
updated_tasks.push(task);
|
2022-09-09 18:16:19 +08:00
|
|
|
}
|
2022-09-13 17:46:07 +08:00
|
|
|
Ok(updated_tasks)
|
2022-09-09 18:16:19 +08:00
|
|
|
}
|
2022-09-07 06:10:14 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2022-09-08 02:08:07 +08:00
|
|
|
|
2022-09-09 18:16:19 +08:00
|
|
|
/*
|
2022-09-08 02:08:07 +08:00
|
|
|
impl Batch {
|
|
|
|
pub fn task_ids(&self) -> impl IntoIterator<Item = TaskId> + '_ {
|
|
|
|
match self {
|
|
|
|
Batch::Cancel(task) | Batch::One(task) => {
|
|
|
|
Box::new(std::iter::once(task.uid)) as Box<dyn Iterator<Item = TaskId>>
|
|
|
|
}
|
|
|
|
Batch::Snapshot(tasks) | Batch::Dump(tasks) | Batch::Contiguous { tasks, .. } => {
|
|
|
|
Box::new(tasks.iter().map(|task| task.uid)) as Box<dyn Iterator<Item = TaskId>>
|
|
|
|
}
|
|
|
|
Batch::Empty => Box::new(std::iter::empty()) as Box<dyn Iterator<Item = TaskId>>,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2022-09-09 18:16:19 +08:00
|
|
|
*/
|