2022-10-20 16:25:34 +08:00
|
|
|
/*!
|
|
|
|
This module handles the creation and processing of batch operations.
|
|
|
|
|
|
|
|
A batch is a combination of multiple tasks that can be processed at once.
|
|
|
|
Executing a batch operation should always be functionally equivalent to
|
|
|
|
executing each of its tasks' operations individually and in order.
|
|
|
|
|
|
|
|
For example, if the user sends two tasks:
|
|
|
|
1. import documents X
|
|
|
|
2. import documents Y
|
|
|
|
|
|
|
|
We can combine the two tasks in a single batch:
|
|
|
|
1. import documents X and Y
|
|
|
|
|
|
|
|
Processing this batch is functionally equivalent to processing the two
|
|
|
|
tasks individally, but should be much faster since we are only performing
|
|
|
|
one indexing operation.
|
|
|
|
*/
|
|
|
|
|
2022-10-13 17:09:00 +08:00
|
|
|
use std::collections::HashSet;
|
2022-10-13 21:02:59 +08:00
|
|
|
use std::fs::File;
|
|
|
|
use std::io::BufWriter;
|
2022-10-13 17:09:00 +08:00
|
|
|
|
2022-10-13 21:02:59 +08:00
|
|
|
use dump::IndexMetadata;
|
2022-10-19 17:48:35 +08:00
|
|
|
use log::{debug, error, info};
|
2022-10-21 00:00:07 +08:00
|
|
|
use meilisearch_types::heed::{RoTxn, RwTxn};
|
|
|
|
use meilisearch_types::milli::documents::{obkv_to_object, DocumentsBatchReader};
|
2022-10-11 23:42:43 +08:00
|
|
|
use meilisearch_types::milli::update::{
|
2022-10-21 00:00:07 +08:00
|
|
|
DocumentAdditionResult, DocumentDeletionResult, IndexDocumentsConfig, IndexDocumentsMethod,
|
|
|
|
Settings as MilliSettings,
|
2022-10-11 23:42:43 +08:00
|
|
|
};
|
2022-10-21 00:00:07 +08:00
|
|
|
use meilisearch_types::milli::{self, BEU32};
|
2022-10-11 23:42:43 +08:00
|
|
|
use meilisearch_types::settings::{apply_settings_to_builder, Settings, Unchecked};
|
2022-10-17 23:19:17 +08:00
|
|
|
use meilisearch_types::tasks::{Details, Kind, KindWithContent, Status, Task};
|
2022-10-21 00:00:07 +08:00
|
|
|
use meilisearch_types::Index;
|
2022-10-06 22:53:21 +08:00
|
|
|
use roaring::RoaringBitmap;
|
2022-10-16 08:01:45 +08:00
|
|
|
use time::OffsetDateTime;
|
2022-09-09 07:09:50 +08:00
|
|
|
use uuid::Uuid;
|
2022-09-07 06:10:14 +08:00
|
|
|
|
2022-10-21 00:00:07 +08:00
|
|
|
use crate::autobatcher::BatchKind;
|
|
|
|
use crate::utils::{self, swap_index_uid_in_task};
|
|
|
|
use crate::{Error, IndexScheduler, Query, Result, TaskId};
|
|
|
|
|
2022-10-20 16:25:34 +08:00
|
|
|
/// Represents a combination of tasks that can all be processed at the same time.
|
|
|
|
///
|
|
|
|
/// A batch contains the set of tasks that it represents (accessible through
|
|
|
|
/// [`self.ids()`](Batch::ids)), as well as additional information on how to
|
|
|
|
/// be processed.
|
2022-10-13 18:48:23 +08:00
|
|
|
#[derive(Debug)]
|
2022-09-08 02:08:07 +08:00
|
|
|
pub(crate) enum Batch {
|
2022-10-17 23:19:17 +08:00
|
|
|
TaskCancelation(Task),
|
2022-10-13 17:09:00 +08:00
|
|
|
TaskDeletion(Task),
|
2022-09-07 06:10:14 +08:00
|
|
|
Snapshot(Vec<Task>),
|
2022-10-13 21:02:59 +08:00
|
|
|
Dump(Task),
|
2022-10-21 00:00:07 +08:00
|
|
|
IndexOperation { op: IndexOperation, must_create_index: bool },
|
|
|
|
IndexCreation { index_uid: String, primary_key: Option<String>, task: Task },
|
|
|
|
IndexUpdate { index_uid: String, primary_key: Option<String>, task: Task },
|
|
|
|
IndexDeletion { index_uid: String, tasks: Vec<Task>, index_has_been_created: bool },
|
|
|
|
IndexSwap { task: Task },
|
2022-09-30 00:15:50 +08:00
|
|
|
}
|
|
|
|
|
2022-10-20 16:25:34 +08:00
|
|
|
/// A [batch](Batch) that combines multiple tasks operating on an index.
|
2022-10-13 18:48:23 +08:00
|
|
|
#[derive(Debug)]
|
2022-09-30 00:15:50 +08:00
|
|
|
pub(crate) enum IndexOperation {
|
2022-09-29 21:49:54 +08:00
|
|
|
DocumentImport {
|
2022-09-16 22:31:16 +08:00
|
|
|
index_uid: String,
|
|
|
|
primary_key: Option<String>,
|
2022-09-29 21:49:54 +08:00
|
|
|
method: IndexDocumentsMethod,
|
2022-10-05 00:50:18 +08:00
|
|
|
documents_counts: Vec<u64>,
|
2022-09-16 22:31:16 +08:00
|
|
|
content_files: Vec<Uuid>,
|
|
|
|
tasks: Vec<Task>,
|
|
|
|
},
|
|
|
|
DocumentDeletion {
|
|
|
|
index_uid: String,
|
2022-09-22 18:14:51 +08:00
|
|
|
documents: Vec<String>,
|
2022-09-16 22:31:16 +08:00
|
|
|
tasks: Vec<Task>,
|
|
|
|
},
|
|
|
|
DocumentClear {
|
|
|
|
index_uid: String,
|
|
|
|
tasks: Vec<Task>,
|
|
|
|
},
|
|
|
|
Settings {
|
|
|
|
index_uid: String,
|
2022-09-29 19:57:28 +08:00
|
|
|
// TODO what's that boolean, does it mean that it removes things or what?
|
2022-09-16 22:31:16 +08:00
|
|
|
settings: Vec<(bool, Settings<Unchecked>)>,
|
|
|
|
tasks: Vec<Task>,
|
|
|
|
},
|
|
|
|
DocumentClearAndSetting {
|
|
|
|
index_uid: String,
|
|
|
|
cleared_tasks: Vec<Task>,
|
|
|
|
|
2022-09-29 20:31:01 +08:00
|
|
|
// TODO what's that boolean, does it mean that it removes things or what?
|
2022-09-16 22:31:16 +08:00
|
|
|
settings: Vec<(bool, Settings<Unchecked>)>,
|
|
|
|
settings_tasks: Vec<Task>,
|
|
|
|
},
|
2022-09-29 21:49:54 +08:00
|
|
|
SettingsAndDocumentImport {
|
2022-09-16 22:31:16 +08:00
|
|
|
index_uid: String,
|
|
|
|
|
|
|
|
primary_key: Option<String>,
|
2022-09-29 21:49:54 +08:00
|
|
|
method: IndexDocumentsMethod,
|
2022-10-05 00:50:18 +08:00
|
|
|
documents_counts: Vec<u64>,
|
2022-09-16 22:31:16 +08:00
|
|
|
content_files: Vec<Uuid>,
|
2022-09-29 21:49:54 +08:00
|
|
|
document_import_tasks: Vec<Task>,
|
2022-09-16 22:31:16 +08:00
|
|
|
|
2022-09-29 20:31:01 +08:00
|
|
|
// TODO what's that boolean, does it mean that it removes things or what?
|
2022-09-16 22:31:16 +08:00
|
|
|
settings: Vec<(bool, Settings<Unchecked>)>,
|
2022-09-13 17:46:07 +08:00
|
|
|
settings_tasks: Vec<Task>,
|
|
|
|
},
|
2022-09-09 07:09:50 +08:00
|
|
|
}
|
|
|
|
|
2022-09-16 07:58:08 +08:00
|
|
|
impl Batch {
|
2022-10-20 16:25:34 +08:00
|
|
|
/// Return the task ids associated with this batch.
|
2022-09-16 07:58:08 +08:00
|
|
|
pub fn ids(&self) -> Vec<TaskId> {
|
|
|
|
match self {
|
2022-10-17 23:19:17 +08:00
|
|
|
Batch::TaskCancelation(task)
|
2022-10-13 17:09:00 +08:00
|
|
|
| Batch::TaskDeletion(task)
|
2022-10-13 21:02:59 +08:00
|
|
|
| Batch::Dump(task)
|
2022-09-17 03:24:49 +08:00
|
|
|
| Batch::IndexCreation { task, .. }
|
|
|
|
| Batch::IndexUpdate { task, .. } => vec![task.uid],
|
2022-10-13 21:02:59 +08:00
|
|
|
Batch::Snapshot(tasks) | Batch::IndexDeletion { tasks, .. } => {
|
2022-09-30 00:15:50 +08:00
|
|
|
tasks.iter().map(|task| task.uid).collect()
|
2022-09-16 22:31:16 +08:00
|
|
|
}
|
2022-10-20 00:27:18 +08:00
|
|
|
Batch::IndexOperation { op, .. } => match op {
|
2022-09-30 00:15:50 +08:00
|
|
|
IndexOperation::DocumentImport { tasks, .. }
|
|
|
|
| IndexOperation::DocumentDeletion { tasks, .. }
|
|
|
|
| IndexOperation::Settings { tasks, .. }
|
|
|
|
| IndexOperation::DocumentClear { tasks, .. } => {
|
|
|
|
tasks.iter().map(|task| task.uid).collect()
|
|
|
|
}
|
|
|
|
IndexOperation::SettingsAndDocumentImport {
|
|
|
|
document_import_tasks: tasks,
|
|
|
|
settings_tasks: other,
|
|
|
|
..
|
|
|
|
}
|
|
|
|
| IndexOperation::DocumentClearAndSetting {
|
|
|
|
cleared_tasks: tasks,
|
|
|
|
settings_tasks: other,
|
|
|
|
..
|
|
|
|
} => tasks.iter().chain(other).map(|task| task.uid).collect(),
|
|
|
|
},
|
2022-10-17 22:30:18 +08:00
|
|
|
Batch::IndexSwap { task } => vec![task.uid],
|
2022-09-16 07:58:08 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-10-20 00:27:18 +08:00
|
|
|
impl IndexOperation {
|
|
|
|
pub fn index_uid(&self) -> &str {
|
|
|
|
match self {
|
|
|
|
IndexOperation::DocumentImport { index_uid, .. }
|
|
|
|
| IndexOperation::DocumentDeletion { index_uid, .. }
|
|
|
|
| IndexOperation::DocumentClear { index_uid, .. }
|
|
|
|
| IndexOperation::Settings { index_uid, .. }
|
|
|
|
| IndexOperation::DocumentClearAndSetting { index_uid, .. }
|
|
|
|
| IndexOperation::SettingsAndDocumentImport { index_uid, .. } => index_uid,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-09-07 06:10:14 +08:00
|
|
|
impl IndexScheduler {
|
2022-10-20 16:25:34 +08:00
|
|
|
/// Convert an [`BatchKind`](crate::autobatcher::BatchKind) into a [`Batch`].
|
|
|
|
///
|
|
|
|
/// ## Arguments
|
|
|
|
/// - `rtxn`: read transaction
|
|
|
|
/// - `index_uid`: name of the index affected by the operations of the autobatch
|
|
|
|
/// - `batch`: the result of the autobatcher
|
2022-09-13 17:46:07 +08:00
|
|
|
pub(crate) fn create_next_batch_index(
|
|
|
|
&self,
|
|
|
|
rtxn: &RoTxn,
|
|
|
|
index_uid: String,
|
|
|
|
batch: BatchKind,
|
2022-10-20 00:27:18 +08:00
|
|
|
must_create_index: bool,
|
2022-09-13 17:46:07 +08:00
|
|
|
) -> Result<Option<Batch>> {
|
|
|
|
match batch {
|
2022-10-20 00:27:18 +08:00
|
|
|
BatchKind::DocumentClear { ids } => Ok(Some(Batch::IndexOperation {
|
|
|
|
op: IndexOperation::DocumentClear {
|
2022-09-30 00:15:50 +08:00
|
|
|
tasks: self.get_existing_tasks(rtxn, ids)?,
|
|
|
|
index_uid,
|
2022-10-20 00:27:18 +08:00
|
|
|
},
|
|
|
|
must_create_index,
|
|
|
|
})),
|
2022-10-20 21:19:28 +08:00
|
|
|
BatchKind::DocumentImport { method, import_ids, .. } => {
|
2022-09-29 21:49:54 +08:00
|
|
|
let tasks = self.get_existing_tasks(rtxn, import_ids)?;
|
2022-09-16 22:31:16 +08:00
|
|
|
let primary_key = match &tasks[0].kind {
|
2022-10-22 00:03:10 +08:00
|
|
|
KindWithContent::DocumentAdditionOrUpdate { primary_key, .. } => {
|
|
|
|
primary_key.clone()
|
|
|
|
}
|
2022-09-13 17:46:07 +08:00
|
|
|
_ => unreachable!(),
|
|
|
|
};
|
2022-10-05 00:50:18 +08:00
|
|
|
|
|
|
|
let mut documents_counts = Vec::new();
|
|
|
|
let mut content_files = Vec::new();
|
|
|
|
for task in &tasks {
|
2022-10-05 19:46:45 +08:00
|
|
|
match task.kind {
|
2022-10-22 00:03:10 +08:00
|
|
|
KindWithContent::DocumentAdditionOrUpdate {
|
|
|
|
content_file,
|
|
|
|
documents_count,
|
|
|
|
..
|
2022-10-05 19:46:45 +08:00
|
|
|
} => {
|
|
|
|
documents_counts.push(documents_count);
|
|
|
|
content_files.push(content_file);
|
|
|
|
}
|
|
|
|
_ => unreachable!(),
|
2022-10-05 00:50:18 +08:00
|
|
|
}
|
|
|
|
}
|
2022-09-13 17:46:07 +08:00
|
|
|
|
2022-10-20 00:27:18 +08:00
|
|
|
Ok(Some(Batch::IndexOperation {
|
|
|
|
op: IndexOperation::DocumentImport {
|
2022-09-30 00:15:50 +08:00
|
|
|
index_uid,
|
|
|
|
primary_key,
|
|
|
|
method,
|
2022-10-05 00:50:18 +08:00
|
|
|
documents_counts,
|
2022-09-30 00:15:50 +08:00
|
|
|
content_files,
|
|
|
|
tasks,
|
|
|
|
},
|
2022-10-20 00:27:18 +08:00
|
|
|
must_create_index,
|
|
|
|
}))
|
2022-09-16 22:31:16 +08:00
|
|
|
}
|
|
|
|
BatchKind::DocumentDeletion { deletion_ids } => {
|
|
|
|
let tasks = self.get_existing_tasks(rtxn, deletion_ids)?;
|
|
|
|
|
|
|
|
let mut documents = Vec::new();
|
|
|
|
for task in &tasks {
|
|
|
|
match task.kind {
|
2022-10-21 00:00:07 +08:00
|
|
|
KindWithContent::DocumentDeletion { ref documents_ids, .. } => {
|
|
|
|
documents.extend_from_slice(documents_ids)
|
|
|
|
}
|
2022-09-16 22:31:16 +08:00
|
|
|
_ => unreachable!(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-10-20 00:27:18 +08:00
|
|
|
Ok(Some(Batch::IndexOperation {
|
2022-10-21 00:00:07 +08:00
|
|
|
op: IndexOperation::DocumentDeletion { index_uid, documents, tasks },
|
2022-10-20 00:27:18 +08:00
|
|
|
must_create_index,
|
|
|
|
}))
|
2022-09-16 22:31:16 +08:00
|
|
|
}
|
2022-10-20 21:19:28 +08:00
|
|
|
BatchKind::Settings { settings_ids, .. } => {
|
2022-09-16 22:31:16 +08:00
|
|
|
let tasks = self.get_existing_tasks(rtxn, settings_ids)?;
|
|
|
|
|
|
|
|
let mut settings = Vec::new();
|
|
|
|
for task in &tasks {
|
|
|
|
match task.kind {
|
2022-10-22 00:03:10 +08:00
|
|
|
KindWithContent::SettingsUpdate {
|
|
|
|
ref new_settings, is_deletion, ..
|
|
|
|
} => settings.push((is_deletion, new_settings.clone())),
|
2022-09-16 22:31:16 +08:00
|
|
|
_ => unreachable!(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-10-20 00:27:18 +08:00
|
|
|
Ok(Some(Batch::IndexOperation {
|
2022-10-21 00:00:07 +08:00
|
|
|
op: IndexOperation::Settings { index_uid, settings, tasks },
|
2022-10-20 00:27:18 +08:00
|
|
|
must_create_index,
|
|
|
|
}))
|
2022-09-16 22:31:16 +08:00
|
|
|
}
|
2022-10-21 00:00:07 +08:00
|
|
|
BatchKind::ClearAndSettings { other, settings_ids, allow_index_creation } => {
|
2022-09-16 22:31:16 +08:00
|
|
|
let (index_uid, settings, settings_tasks) = match self
|
2022-10-06 21:55:48 +08:00
|
|
|
.create_next_batch_index(
|
|
|
|
rtxn,
|
|
|
|
index_uid,
|
2022-10-21 00:00:07 +08:00
|
|
|
BatchKind::Settings { settings_ids, allow_index_creation },
|
2022-10-20 00:27:18 +08:00
|
|
|
must_create_index,
|
2022-10-06 21:55:48 +08:00
|
|
|
)?
|
2022-09-16 22:31:16 +08:00
|
|
|
.unwrap()
|
|
|
|
{
|
2022-10-20 00:27:18 +08:00
|
|
|
Batch::IndexOperation {
|
2022-10-21 00:00:07 +08:00
|
|
|
op: IndexOperation::Settings { index_uid, settings, tasks, .. },
|
2022-10-06 21:51:26 +08:00
|
|
|
..
|
2022-10-20 00:27:18 +08:00
|
|
|
} => (index_uid, settings, tasks),
|
2022-09-16 22:31:16 +08:00
|
|
|
_ => unreachable!(),
|
|
|
|
};
|
|
|
|
let (index_uid, cleared_tasks) = match self
|
|
|
|
.create_next_batch_index(
|
|
|
|
rtxn,
|
|
|
|
index_uid,
|
|
|
|
BatchKind::DocumentClear { ids: other },
|
2022-10-20 00:27:18 +08:00
|
|
|
must_create_index,
|
2022-09-16 22:31:16 +08:00
|
|
|
)?
|
|
|
|
.unwrap()
|
|
|
|
{
|
2022-10-20 00:27:18 +08:00
|
|
|
Batch::IndexOperation {
|
|
|
|
op: IndexOperation::DocumentClear { index_uid, tasks },
|
|
|
|
..
|
|
|
|
} => (index_uid, tasks),
|
2022-09-16 22:31:16 +08:00
|
|
|
_ => unreachable!(),
|
|
|
|
};
|
|
|
|
|
2022-10-20 00:27:18 +08:00
|
|
|
Ok(Some(Batch::IndexOperation {
|
|
|
|
op: IndexOperation::DocumentClearAndSetting {
|
2022-09-30 00:15:50 +08:00
|
|
|
index_uid,
|
|
|
|
cleared_tasks,
|
|
|
|
settings,
|
|
|
|
settings_tasks,
|
|
|
|
},
|
2022-10-20 00:27:18 +08:00
|
|
|
must_create_index,
|
|
|
|
}))
|
2022-09-16 22:31:16 +08:00
|
|
|
}
|
2022-09-29 21:49:54 +08:00
|
|
|
BatchKind::SettingsAndDocumentImport {
|
2022-09-16 22:31:16 +08:00
|
|
|
settings_ids,
|
2022-09-29 21:49:54 +08:00
|
|
|
method,
|
2022-10-06 21:55:48 +08:00
|
|
|
allow_index_creation,
|
2022-09-29 21:49:54 +08:00
|
|
|
import_ids,
|
2022-09-16 22:31:16 +08:00
|
|
|
} => {
|
|
|
|
let settings = self.create_next_batch_index(
|
|
|
|
rtxn,
|
|
|
|
index_uid.clone(),
|
2022-10-21 00:00:07 +08:00
|
|
|
BatchKind::Settings { settings_ids, allow_index_creation },
|
2022-10-20 00:27:18 +08:00
|
|
|
must_create_index,
|
2022-09-16 22:31:16 +08:00
|
|
|
)?;
|
|
|
|
|
2022-09-29 21:49:54 +08:00
|
|
|
let document_import = self.create_next_batch_index(
|
2022-09-16 22:31:16 +08:00
|
|
|
rtxn,
|
|
|
|
index_uid.clone(),
|
2022-10-21 00:00:07 +08:00
|
|
|
BatchKind::DocumentImport { method, allow_index_creation, import_ids },
|
2022-10-20 00:27:18 +08:00
|
|
|
must_create_index,
|
2022-09-16 22:31:16 +08:00
|
|
|
)?;
|
|
|
|
|
2022-09-29 21:49:54 +08:00
|
|
|
match (document_import, settings) {
|
2022-09-16 22:31:16 +08:00
|
|
|
(
|
2022-10-20 00:27:18 +08:00
|
|
|
Some(Batch::IndexOperation {
|
|
|
|
op:
|
|
|
|
IndexOperation::DocumentImport {
|
|
|
|
primary_key,
|
|
|
|
documents_counts,
|
|
|
|
content_files,
|
|
|
|
tasks: document_import_tasks,
|
|
|
|
..
|
|
|
|
},
|
2022-09-16 22:31:16 +08:00
|
|
|
..
|
2022-10-20 00:27:18 +08:00
|
|
|
}),
|
|
|
|
Some(Batch::IndexOperation {
|
2022-10-21 00:00:07 +08:00
|
|
|
op: IndexOperation::Settings { settings, tasks: settings_tasks, .. },
|
2022-09-16 22:31:16 +08:00
|
|
|
..
|
2022-10-20 00:27:18 +08:00
|
|
|
}),
|
|
|
|
) => Ok(Some(Batch::IndexOperation {
|
|
|
|
op: IndexOperation::SettingsAndDocumentImport {
|
2022-09-30 00:15:50 +08:00
|
|
|
index_uid,
|
|
|
|
primary_key,
|
|
|
|
method,
|
2022-10-05 00:50:18 +08:00
|
|
|
documents_counts,
|
2022-09-30 00:15:50 +08:00
|
|
|
content_files,
|
|
|
|
document_import_tasks,
|
|
|
|
settings,
|
|
|
|
settings_tasks,
|
|
|
|
},
|
2022-10-20 00:27:18 +08:00
|
|
|
must_create_index,
|
|
|
|
})),
|
2022-09-16 22:31:16 +08:00
|
|
|
_ => unreachable!(),
|
|
|
|
}
|
|
|
|
}
|
2022-09-17 03:24:49 +08:00
|
|
|
BatchKind::IndexCreation { id } => {
|
|
|
|
let task = self.get_task(rtxn, id)?.ok_or(Error::CorruptedTaskQueue)?;
|
|
|
|
let (index_uid, primary_key) = match &task.kind {
|
2022-10-21 00:00:07 +08:00
|
|
|
KindWithContent::IndexCreation { index_uid, primary_key } => {
|
|
|
|
(index_uid.clone(), primary_key.clone())
|
|
|
|
}
|
2022-09-17 03:24:49 +08:00
|
|
|
_ => unreachable!(),
|
|
|
|
};
|
2022-10-21 00:00:07 +08:00
|
|
|
Ok(Some(Batch::IndexCreation { index_uid, primary_key, task }))
|
2022-09-17 03:24:49 +08:00
|
|
|
}
|
|
|
|
BatchKind::IndexUpdate { id } => {
|
|
|
|
let task = self.get_task(rtxn, id)?.ok_or(Error::CorruptedTaskQueue)?;
|
|
|
|
let primary_key = match &task.kind {
|
|
|
|
KindWithContent::IndexUpdate { primary_key, .. } => primary_key.clone(),
|
|
|
|
_ => unreachable!(),
|
|
|
|
};
|
2022-10-21 00:00:07 +08:00
|
|
|
Ok(Some(Batch::IndexUpdate { index_uid, primary_key, task }))
|
2022-09-17 03:24:49 +08:00
|
|
|
}
|
|
|
|
BatchKind::IndexDeletion { ids } => Ok(Some(Batch::IndexDeletion {
|
|
|
|
index_uid,
|
2022-10-20 19:18:25 +08:00
|
|
|
index_has_been_created: must_create_index,
|
2022-09-17 03:24:49 +08:00
|
|
|
tasks: self.get_existing_tasks(rtxn, ids)?,
|
|
|
|
})),
|
2022-10-17 22:30:18 +08:00
|
|
|
BatchKind::IndexSwap { id } => {
|
|
|
|
let task = self.get_task(rtxn, id)?.ok_or(Error::CorruptedTaskQueue)?;
|
|
|
|
Ok(Some(Batch::IndexSwap { task }))
|
|
|
|
}
|
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.
|
2022-10-06 22:53:21 +08:00
|
|
|
/// 2. We get the *next* task to delete.
|
|
|
|
/// 3. We get the *next* snapshot to process.
|
|
|
|
/// 4. We get the *next* dump to process.
|
|
|
|
/// 5. 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)?;
|
2022-10-17 23:19:17 +08:00
|
|
|
let to_cancel = self.get_kind(rtxn, Kind::TaskCancelation)? & enqueued;
|
2022-09-07 06:10:14 +08:00
|
|
|
|
|
|
|
// 1. we get the last task to cancel.
|
|
|
|
if let Some(task_id) = to_cancel.max() {
|
2022-10-17 23:19:17 +08:00
|
|
|
return Ok(Some(Batch::TaskCancelation(
|
2022-10-21 00:00:07 +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
|
|
|
}
|
|
|
|
|
2022-10-06 22:53:21 +08:00
|
|
|
// 2. we get the next task to delete
|
2022-10-13 18:48:23 +08:00
|
|
|
let to_delete = self.get_kind(rtxn, Kind::TaskDeletion)? & enqueued;
|
2022-10-06 22:53:21 +08:00
|
|
|
if let Some(task_id) = to_delete.min() {
|
2022-10-21 00:00:07 +08:00
|
|
|
let task = self.get_task(rtxn, task_id)?.ok_or(Error::CorruptedTaskQueue)?;
|
2022-10-10 18:57:17 +08:00
|
|
|
|
2022-10-13 17:09:00 +08:00
|
|
|
return Ok(Some(Batch::TaskDeletion(task)));
|
2022-10-06 22:53:21 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// 3. we batch the snapshot.
|
2022-09-07 06:10:14 +08:00
|
|
|
let to_snapshot = self.get_kind(rtxn, Kind::Snapshot)? & enqueued;
|
|
|
|
if !to_snapshot.is_empty() {
|
2022-10-21 00:00:07 +08:00
|
|
|
return Ok(Some(Batch::Snapshot(self.get_existing_tasks(rtxn, to_snapshot)?)));
|
2022-09-07 06:10:14 +08:00
|
|
|
}
|
|
|
|
|
2022-10-06 22:53:21 +08:00
|
|
|
// 4. we batch the dumps.
|
2022-09-07 06:10:14 +08:00
|
|
|
let to_dump = self.get_kind(rtxn, Kind::DumpExport)? & enqueued;
|
2022-10-13 21:02:59 +08:00
|
|
|
if let Some(to_dump) = to_dump.min() {
|
|
|
|
return Ok(Some(Batch::Dump(
|
2022-10-21 00:00:07 +08:00
|
|
|
self.get_task(rtxn, to_dump)?.ok_or(Error::CorruptedTaskQueue)?,
|
2022-10-13 21:02:59 +08:00
|
|
|
)));
|
2022-09-07 06:10:14 +08:00
|
|
|
}
|
|
|
|
|
2022-10-06 22:53:21 +08:00
|
|
|
// 5. We take the next task and try to batch all the tasks associated with this index.
|
2022-09-07 06:10:14 +08:00
|
|
|
if let Some(task_id) = enqueued.min() {
|
2022-10-21 00:00:07 +08:00
|
|
|
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];
|
2022-10-20 00:27:18 +08:00
|
|
|
let index_already_exists = self.index_mapper.exists(rtxn, &index_name)?;
|
2022-09-09 07:40:28 +08:00
|
|
|
|
2022-10-13 17:07:36 +08:00
|
|
|
let index_tasks = self.index_tasks(rtxn, index_name)? & enqueued;
|
2022-09-09 07:40:28 +08:00
|
|
|
|
2022-10-13 17:07:36 +08:00
|
|
|
// If autobatching is disabled we only take one task at a time.
|
2022-10-21 00:00:07 +08:00
|
|
|
let tasks_limit = if self.autobatching_enabled { usize::MAX } else { 1 };
|
2022-10-10 23:01:49 +08:00
|
|
|
|
2022-10-13 17:07:36 +08:00
|
|
|
let enqueued = index_tasks
|
2022-09-09 07:40:28 +08:00
|
|
|
.into_iter()
|
2022-10-10 23:01:49 +08:00
|
|
|
.take(tasks_limit)
|
2022-09-09 07:40:28 +08:00
|
|
|
.map(|task_id| {
|
|
|
|
self.get_task(rtxn, task_id)
|
|
|
|
.and_then(|task| task.ok_or(Error::CorruptedTaskQueue))
|
2022-10-12 09:21:25 +08:00
|
|
|
.map(|task| (task.uid, task.kind))
|
2022-09-09 07:40:28 +08:00
|
|
|
})
|
|
|
|
.collect::<Result<Vec<_>>>()?;
|
|
|
|
|
2022-10-20 00:27:18 +08:00
|
|
|
if let Some((batchkind, create_index)) =
|
|
|
|
crate::autobatcher::autobatch(enqueued, index_already_exists)
|
|
|
|
{
|
|
|
|
return self.create_next_batch_index(
|
|
|
|
rtxn,
|
|
|
|
index_name.to_string(),
|
|
|
|
batchkind,
|
|
|
|
create_index,
|
|
|
|
);
|
2022-09-13 17:46:07 +08:00
|
|
|
}
|
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-10-20 16:25:34 +08:00
|
|
|
/// Apply the operation associated with the given batch.
|
|
|
|
///
|
|
|
|
/// ## Return
|
|
|
|
/// The list of tasks that were processed. The metadata of each task in the returned
|
|
|
|
/// list is updated accordingly, with the exception of the its date fields
|
|
|
|
/// [`finished_at`](meilisearch_types::tasks::Task::finished_at) and [`started_at`](meilisearch_types::tasks::Task::started_at).
|
2022-09-27 04:26:30 +08:00
|
|
|
pub(crate) fn process_batch(&self, batch: Batch) -> Result<Vec<Task>> {
|
2022-09-09 18:16:19 +08:00
|
|
|
match batch {
|
2022-10-17 23:19:17 +08:00
|
|
|
Batch::TaskCancelation(mut task) => {
|
|
|
|
// 1. Retrieve the tasks that matched the query at enqueue-time.
|
|
|
|
let matched_tasks =
|
|
|
|
if let KindWithContent::TaskCancelation { tasks, query: _ } = &task.kind {
|
|
|
|
tasks
|
|
|
|
} else {
|
|
|
|
unreachable!()
|
|
|
|
};
|
|
|
|
|
|
|
|
let mut wtxn = self.env.write_txn()?;
|
2022-10-19 17:48:35 +08:00
|
|
|
let canceled_tasks_content_uuids =
|
2022-10-18 19:57:58 +08:00
|
|
|
self.cancel_matched_tasks(&mut wtxn, task.uid, matched_tasks)?;
|
2022-10-17 23:19:17 +08:00
|
|
|
|
|
|
|
task.status = Status::Succeeded;
|
|
|
|
match &mut task.details {
|
|
|
|
Some(Details::TaskCancelation {
|
|
|
|
matched_tasks: _,
|
|
|
|
canceled_tasks,
|
|
|
|
original_query: _,
|
|
|
|
}) => {
|
2022-10-19 17:48:35 +08:00
|
|
|
*canceled_tasks = Some(canceled_tasks_content_uuids.len() as u64);
|
2022-10-17 23:19:17 +08:00
|
|
|
}
|
|
|
|
_ => unreachable!(),
|
|
|
|
}
|
|
|
|
|
2022-10-19 17:48:35 +08:00
|
|
|
// We must only remove the content files if the transaction is successfuly committed
|
|
|
|
// and if errors occurs when we are deleting files we must do our best to delete
|
|
|
|
// everything. We do not return the encountered errors when deleting the content
|
|
|
|
// files as it is not a breaking operation and we can safely continue our job.
|
|
|
|
match wtxn.commit() {
|
|
|
|
Ok(()) => {
|
|
|
|
for content_uuid in canceled_tasks_content_uuids {
|
|
|
|
if let Err(error) = self.delete_update_file(content_uuid) {
|
|
|
|
error!(
|
|
|
|
"We failed deleting the content file indentified as {}: {}",
|
|
|
|
content_uuid, error
|
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Err(e) => return Err(e.into()),
|
|
|
|
}
|
|
|
|
|
2022-10-17 23:19:17 +08:00
|
|
|
Ok(vec![task])
|
|
|
|
}
|
2022-10-13 17:09:00 +08:00
|
|
|
Batch::TaskDeletion(mut task) => {
|
2022-10-11 15:53:08 +08:00
|
|
|
// 1. Retrieve the tasks that matched the query at enqueue-time.
|
2022-10-06 22:53:21 +08:00
|
|
|
let matched_tasks =
|
2022-10-13 17:09:00 +08:00
|
|
|
if let KindWithContent::TaskDeletion { tasks, query: _ } = &task.kind {
|
2022-10-06 22:53:21 +08:00
|
|
|
tasks
|
|
|
|
} else {
|
|
|
|
unreachable!()
|
|
|
|
};
|
2022-10-10 18:57:17 +08:00
|
|
|
|
2022-10-06 22:53:21 +08:00
|
|
|
let mut wtxn = self.env.write_txn()?;
|
2022-10-18 23:47:47 +08:00
|
|
|
let deleted_tasks_count = self.delete_matched_tasks(&mut wtxn, matched_tasks)?;
|
2022-10-10 18:57:17 +08:00
|
|
|
|
2022-10-06 22:53:21 +08:00
|
|
|
task.status = Status::Succeeded;
|
|
|
|
match &mut task.details {
|
2022-10-13 17:09:00 +08:00
|
|
|
Some(Details::TaskDeletion {
|
2022-10-06 22:53:21 +08:00
|
|
|
matched_tasks: _,
|
|
|
|
deleted_tasks,
|
|
|
|
original_query: _,
|
|
|
|
}) => {
|
2022-10-18 23:47:47 +08:00
|
|
|
*deleted_tasks = Some(deleted_tasks_count);
|
2022-10-06 22:53:21 +08:00
|
|
|
}
|
|
|
|
_ => unreachable!(),
|
|
|
|
}
|
|
|
|
wtxn.commit()?;
|
|
|
|
Ok(vec![task])
|
|
|
|
}
|
2022-09-13 17:46:07 +08:00
|
|
|
Batch::Snapshot(_) => todo!(),
|
2022-10-13 21:02:59 +08:00
|
|
|
Batch::Dump(mut task) => {
|
2022-10-16 08:01:45 +08:00
|
|
|
let started_at = OffsetDateTime::now_utc();
|
2022-10-17 22:45:00 +08:00
|
|
|
let (keys, instance_uid, dump_uid) = if let KindWithContent::DumpExport {
|
|
|
|
keys,
|
|
|
|
instance_uid,
|
|
|
|
dump_uid,
|
|
|
|
} = &task.kind
|
|
|
|
{
|
|
|
|
(keys, instance_uid, dump_uid)
|
|
|
|
} else {
|
2022-10-13 21:02:59 +08:00
|
|
|
unreachable!();
|
|
|
|
};
|
|
|
|
let dump = dump::DumpWriter::new(instance_uid.clone())?;
|
|
|
|
|
|
|
|
// 1. dump the keys
|
2022-10-17 23:04:52 +08:00
|
|
|
let mut dump_keys = dump.create_keys()?;
|
2022-10-13 21:02:59 +08:00
|
|
|
for key in keys {
|
2022-10-17 22:45:00 +08:00
|
|
|
dump_keys.push_key(key)?;
|
2022-10-13 21:02:59 +08:00
|
|
|
}
|
2022-10-17 23:04:52 +08:00
|
|
|
dump_keys.flush()?;
|
2022-10-13 21:02:59 +08:00
|
|
|
|
|
|
|
let rtxn = self.env.read_txn()?;
|
|
|
|
|
|
|
|
// 2. dump the tasks
|
2022-10-17 23:04:52 +08:00
|
|
|
let mut dump_tasks = dump.create_tasks_queue()?;
|
2022-10-13 21:02:59 +08:00
|
|
|
for ret in self.all_tasks.iter(&rtxn)? {
|
2022-10-16 08:01:45 +08:00
|
|
|
let (_, mut t) = ret?;
|
|
|
|
let status = t.status;
|
|
|
|
let content_file = t.content_uuid().map(|uuid| uuid.clone());
|
|
|
|
|
|
|
|
// In the case we're dumping ourselves we want to be marked as finished
|
|
|
|
// to not loop over ourselves indefinitely.
|
|
|
|
if t.uid == task.uid {
|
|
|
|
let finished_at = OffsetDateTime::now_utc();
|
|
|
|
|
|
|
|
// We're going to fake the date because we don't know if everything is going to go well.
|
|
|
|
// But we need to dump the task as finished and successful.
|
|
|
|
// If something fail everything will be set appropriately in the end.
|
|
|
|
t.status = Status::Succeeded;
|
|
|
|
t.started_at = Some(started_at);
|
|
|
|
t.finished_at = Some(finished_at);
|
|
|
|
}
|
2022-10-17 23:04:52 +08:00
|
|
|
let mut dump_content_file = dump_tasks.push_task(&t.into())?;
|
2022-10-13 21:02:59 +08:00
|
|
|
|
2022-10-16 08:01:45 +08:00
|
|
|
// 2.1. Dump the `content_file` associated with the task if there is one and the task is not finished yet.
|
2022-10-13 22:21:54 +08:00
|
|
|
if let Some(content_file) = content_file {
|
2022-10-16 08:01:45 +08:00
|
|
|
if status == Status::Enqueued {
|
|
|
|
let content_file = self.file_store.get_update(content_file)?;
|
|
|
|
|
|
|
|
let reader = DocumentsBatchReader::from_reader(content_file)
|
|
|
|
.map_err(milli::Error::from)?;
|
|
|
|
|
|
|
|
let (mut cursor, documents_batch_index) =
|
|
|
|
reader.into_cursor_and_fields_index();
|
|
|
|
|
|
|
|
while let Some(doc) =
|
|
|
|
cursor.next_document().map_err(milli::Error::from)?
|
|
|
|
{
|
|
|
|
dump_content_file.push_document(&obkv_to_object(
|
|
|
|
&doc,
|
|
|
|
&documents_batch_index,
|
|
|
|
)?)?;
|
|
|
|
}
|
2022-10-17 23:04:52 +08:00
|
|
|
dump_content_file.flush()?;
|
2022-10-13 21:02:59 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2022-10-17 23:04:52 +08:00
|
|
|
dump_tasks.flush()?;
|
2022-10-13 21:02:59 +08:00
|
|
|
|
|
|
|
// 3. Dump the indexes
|
2022-10-17 23:43:23 +08:00
|
|
|
for (uid, index) in self.index_mapper.indexes(&rtxn)? {
|
2022-10-13 21:02:59 +08:00
|
|
|
let rtxn = index.read_txn()?;
|
|
|
|
let metadata = IndexMetadata {
|
|
|
|
uid: uid.clone(),
|
|
|
|
primary_key: index.primary_key(&rtxn)?.map(String::from),
|
|
|
|
created_at: index.created_at(&rtxn)?,
|
|
|
|
updated_at: index.updated_at(&rtxn)?,
|
|
|
|
};
|
|
|
|
let mut index_dumper = dump.create_index(&uid, &metadata)?;
|
|
|
|
|
|
|
|
let fields_ids_map = index.fields_ids_map(&rtxn)?;
|
|
|
|
let all_fields: Vec<_> = fields_ids_map.iter().map(|(id, _)| id).collect();
|
|
|
|
|
|
|
|
// 3.1. Dump the documents
|
|
|
|
for ret in index.all_documents(&rtxn)? {
|
|
|
|
let (_id, doc) = ret?;
|
|
|
|
let document = milli::obkv_to_json(&all_fields, &fields_ids_map, doc)?;
|
|
|
|
index_dumper.push_document(&document)?;
|
|
|
|
}
|
|
|
|
|
|
|
|
// 3.2. Dump the settings
|
|
|
|
let settings = meilisearch_types::settings::settings(&index, &rtxn)?;
|
|
|
|
index_dumper.settings(&settings)?;
|
|
|
|
}
|
|
|
|
|
|
|
|
let path = self.dumps_path.join(format!("{}.dump", dump_uid));
|
2022-10-17 22:45:00 +08:00
|
|
|
let file = File::create(path)?;
|
|
|
|
dump.persist_to(BufWriter::new(file))?;
|
2022-10-13 21:02:59 +08:00
|
|
|
|
2022-10-17 23:04:52 +08:00
|
|
|
// if we reached this step we can tell the scheduler we succeeded to dump ourselves.
|
2022-10-13 21:02:59 +08:00
|
|
|
task.status = Status::Succeeded;
|
|
|
|
Ok(vec![task])
|
|
|
|
}
|
2022-10-21 00:00:07 +08:00
|
|
|
Batch::IndexOperation { op, must_create_index } => {
|
2022-10-20 00:27:18 +08:00
|
|
|
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
|
|
|
|
} else {
|
|
|
|
let rtxn = self.env.read_txn()?;
|
|
|
|
self.index_mapper.index(&rtxn, index_uid)?
|
2022-09-30 00:15:50 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
let mut index_wtxn = index.write_txn()?;
|
2022-10-20 00:27:18 +08:00
|
|
|
let tasks = self.apply_index_operation(&mut index_wtxn, &index, op)?;
|
2022-09-30 00:15:50 +08:00
|
|
|
index_wtxn.commit()?;
|
2022-09-29 20:31:01 +08:00
|
|
|
|
|
|
|
Ok(tasks)
|
|
|
|
}
|
2022-10-21 00:00:07 +08:00
|
|
|
Batch::IndexCreation { index_uid, primary_key, task } => {
|
2022-10-04 17:48:08 +08:00
|
|
|
let mut wtxn = self.env.write_txn()?;
|
2022-10-05 00:50:18 +08:00
|
|
|
self.index_mapper.create_index(&mut wtxn, &index_uid)?;
|
2022-10-04 17:51:39 +08:00
|
|
|
wtxn.commit()?;
|
|
|
|
|
2022-10-21 00:00:07 +08:00
|
|
|
self.process_batch(Batch::IndexUpdate { index_uid, primary_key, task })
|
2022-10-04 17:51:39 +08:00
|
|
|
}
|
2022-10-21 00:00:07 +08:00
|
|
|
Batch::IndexUpdate { index_uid, primary_key, mut task } => {
|
2022-10-04 17:51:39 +08:00
|
|
|
let rtxn = self.env.read_txn()?;
|
|
|
|
let index = self.index_mapper.index(&rtxn, &index_uid)?;
|
2022-10-04 17:48:08 +08:00
|
|
|
|
2022-10-05 00:50:18 +08:00
|
|
|
if let Some(primary_key) = primary_key.clone() {
|
2022-10-04 17:48:08 +08:00
|
|
|
let mut index_wtxn = index.write_txn()?;
|
2022-10-11 23:42:43 +08:00
|
|
|
let mut builder = MilliSettings::new(
|
2022-10-04 17:48:08 +08:00
|
|
|
&mut index_wtxn,
|
|
|
|
&index,
|
|
|
|
self.index_mapper.indexer_config(),
|
|
|
|
);
|
|
|
|
builder.set_primary_key(primary_key);
|
2022-10-19 17:22:59 +08:00
|
|
|
let must_stop_processing = self.must_stop_processing.clone();
|
2022-10-17 23:19:17 +08:00
|
|
|
builder.execute(
|
|
|
|
|indexing_step| debug!("update: {:?}", indexing_step),
|
2022-10-19 17:22:59 +08:00
|
|
|
|| must_stop_processing.get(),
|
2022-10-17 23:19:17 +08:00
|
|
|
)?;
|
2022-10-04 17:48:08 +08:00
|
|
|
index_wtxn.commit()?;
|
|
|
|
}
|
2022-10-05 00:50:18 +08:00
|
|
|
task.status = Status::Succeeded;
|
|
|
|
task.details = Some(Details::IndexInfo { primary_key });
|
|
|
|
|
2022-10-04 17:48:08 +08:00
|
|
|
Ok(vec![task])
|
|
|
|
}
|
2022-10-21 00:00:07 +08:00
|
|
|
Batch::IndexDeletion { index_uid, index_has_been_created, mut tasks } => {
|
2022-10-05 00:19:18 +08:00
|
|
|
let wtxn = self.env.write_txn()?;
|
2022-10-05 20:05:20 +08:00
|
|
|
|
2022-10-20 19:18:25 +08:00
|
|
|
// it's possible that the index doesn't exist
|
|
|
|
let number_of_documents = || -> Result<u64> {
|
2022-10-05 20:05:20 +08:00
|
|
|
let index = self.index_mapper.index(&wtxn, &index_uid)?;
|
|
|
|
let index_rtxn = index.read_txn()?;
|
2022-10-20 19:18:25 +08:00
|
|
|
Ok(index.number_of_documents(&index_rtxn)?)
|
|
|
|
}()
|
|
|
|
.unwrap_or_default();
|
2022-10-05 20:05:20 +08:00
|
|
|
|
2022-10-05 00:50:18 +08:00
|
|
|
// The write transaction is directly owned and commited inside.
|
2022-10-20 19:18:25 +08:00
|
|
|
match self.index_mapper.delete_index(wtxn, &index_uid) {
|
|
|
|
Ok(()) => (),
|
|
|
|
Err(Error::IndexNotFound(_)) if index_has_been_created => (),
|
|
|
|
Err(e) => return Err(e),
|
|
|
|
}
|
2022-10-05 00:50:18 +08:00
|
|
|
|
|
|
|
// We set all the tasks details to the default value.
|
|
|
|
for task in &mut tasks {
|
|
|
|
task.status = Status::Succeeded;
|
2022-10-05 20:05:20 +08:00
|
|
|
task.details = match &task.kind {
|
2022-10-21 00:00:07 +08:00
|
|
|
KindWithContent::IndexDeletion { .. } => {
|
|
|
|
Some(Details::ClearAll { deleted_documents: Some(number_of_documents) })
|
|
|
|
}
|
2022-10-19 22:44:42 +08:00
|
|
|
otherwise => otherwise.default_finished_details(),
|
2022-10-05 20:05:20 +08:00
|
|
|
};
|
2022-10-05 00:50:18 +08:00
|
|
|
}
|
2022-10-05 00:19:18 +08:00
|
|
|
|
2022-10-05 00:50:18 +08:00
|
|
|
Ok(tasks)
|
2022-10-05 00:19:18 +08:00
|
|
|
}
|
2022-10-17 22:30:18 +08:00
|
|
|
Batch::IndexSwap { mut task } => {
|
|
|
|
let mut wtxn = self.env.write_txn()?;
|
|
|
|
let swaps = if let KindWithContent::IndexSwap { swaps } = &task.kind {
|
|
|
|
swaps
|
|
|
|
} else {
|
|
|
|
unreachable!()
|
|
|
|
};
|
|
|
|
for (lhs, rhs) in swaps {
|
|
|
|
self.apply_index_swap(&mut wtxn, task.uid, lhs, rhs)?;
|
|
|
|
}
|
|
|
|
wtxn.commit()?;
|
|
|
|
task.status = Status::Succeeded;
|
|
|
|
Ok(vec![task])
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Swap the index `lhs` with the index `rhs`.
|
|
|
|
fn apply_index_swap(&self, wtxn: &mut RwTxn, task_id: u32, lhs: &str, rhs: &str) -> Result<()> {
|
|
|
|
// 1. Verify that both lhs and rhs are existing indexes
|
|
|
|
let index_lhs_exists = self.index_mapper.index_exists(&wtxn, lhs)?;
|
|
|
|
if !index_lhs_exists {
|
|
|
|
return Err(Error::IndexNotFound(lhs.to_owned()));
|
2022-09-30 00:15:50 +08:00
|
|
|
}
|
2022-10-17 22:30:18 +08:00
|
|
|
let index_rhs_exists = self.index_mapper.index_exists(&wtxn, rhs)?;
|
|
|
|
if !index_rhs_exists {
|
|
|
|
return Err(Error::IndexNotFound(rhs.to_owned()));
|
|
|
|
}
|
|
|
|
|
|
|
|
// 2. Get the task set for index = name.
|
|
|
|
let mut index_lhs_task_ids =
|
|
|
|
self.get_task_ids(&Query::default().with_index(lhs.to_owned()))?;
|
|
|
|
index_lhs_task_ids.remove_range(task_id..);
|
|
|
|
let mut index_rhs_task_ids =
|
|
|
|
self.get_task_ids(&Query::default().with_index(rhs.to_owned()))?;
|
|
|
|
index_rhs_task_ids.remove_range(task_id..);
|
|
|
|
|
|
|
|
// 3. before_name -> new_name in the task's KindWithContent
|
|
|
|
for task_id in &index_lhs_task_ids | &index_rhs_task_ids {
|
2022-10-21 00:00:07 +08:00
|
|
|
let mut task = self.get_task(&wtxn, task_id)?.ok_or(Error::CorruptedTaskQueue)?;
|
2022-10-17 22:30:18 +08:00
|
|
|
swap_index_uid_in_task(&mut task, (lhs, rhs));
|
|
|
|
self.all_tasks.put(wtxn, &BEU32::new(task_id), &task)?;
|
|
|
|
}
|
|
|
|
|
|
|
|
// 4. remove the task from indexuid = before_name
|
|
|
|
// 5. add the task to indexuid = after_name
|
|
|
|
self.update_index(wtxn, lhs, |lhs_tasks| {
|
|
|
|
*lhs_tasks -= &index_lhs_task_ids;
|
|
|
|
*lhs_tasks |= &index_rhs_task_ids;
|
|
|
|
})?;
|
|
|
|
self.update_index(wtxn, rhs, |lhs_tasks| {
|
|
|
|
*lhs_tasks -= &index_rhs_task_ids;
|
|
|
|
*lhs_tasks |= &index_lhs_task_ids;
|
|
|
|
})?;
|
|
|
|
|
|
|
|
// 6. Swap in the index mapper
|
|
|
|
self.index_mapper.swap(wtxn, lhs, rhs)?;
|
|
|
|
|
|
|
|
Ok(())
|
2022-09-30 00:15:50 +08:00
|
|
|
}
|
|
|
|
|
2022-10-20 16:25:34 +08:00
|
|
|
/// Process the index operation on the given index.
|
|
|
|
///
|
|
|
|
/// ## Return
|
|
|
|
/// The list of processed tasks.
|
2022-09-30 00:15:50 +08:00
|
|
|
fn apply_index_operation<'txn, 'i>(
|
|
|
|
&self,
|
|
|
|
index_wtxn: &'txn mut RwTxn<'i, '_>,
|
2022-10-11 23:42:43 +08:00
|
|
|
index: &'i Index,
|
2022-09-30 00:15:50 +08:00
|
|
|
operation: IndexOperation,
|
|
|
|
) -> Result<Vec<Task>> {
|
|
|
|
match operation {
|
2022-10-05 00:50:18 +08:00
|
|
|
IndexOperation::DocumentClear { mut tasks, .. } => {
|
2022-10-05 22:48:43 +08:00
|
|
|
let count = milli::update::ClearDocuments::new(index_wtxn, index).execute()?;
|
|
|
|
|
2022-10-05 22:54:06 +08:00
|
|
|
let mut first_clear_found = false;
|
2022-09-30 00:15:50 +08:00
|
|
|
for task in &mut tasks {
|
2022-10-05 22:48:43 +08:00
|
|
|
task.status = Status::Succeeded;
|
2022-10-05 22:54:06 +08:00
|
|
|
// The first document clear will effectively delete every documents
|
|
|
|
// in the database but the next ones will clear 0 documents.
|
2022-10-05 22:48:43 +08:00
|
|
|
task.details = match &task.kind {
|
2022-10-05 22:54:06 +08:00
|
|
|
KindWithContent::DocumentClear { .. } => {
|
|
|
|
let count = if first_clear_found { 0 } else { count };
|
|
|
|
first_clear_found = true;
|
2022-10-21 00:00:07 +08:00
|
|
|
Some(Details::ClearAll { deleted_documents: Some(count) })
|
2022-10-05 22:54:06 +08:00
|
|
|
}
|
2022-10-05 22:48:43 +08:00
|
|
|
otherwise => otherwise.default_details(),
|
|
|
|
};
|
2022-09-19 04:04:36 +08:00
|
|
|
}
|
|
|
|
|
2022-09-21 18:01:46 +08:00
|
|
|
Ok(tasks)
|
2022-09-19 04:04:36 +08:00
|
|
|
}
|
2022-09-30 00:15:50 +08:00
|
|
|
IndexOperation::DocumentImport {
|
2022-10-05 00:50:18 +08:00
|
|
|
index_uid: _,
|
2022-09-13 17:46:07 +08:00
|
|
|
primary_key,
|
2022-09-29 21:49:54 +08:00
|
|
|
method,
|
2022-10-05 00:50:18 +08:00
|
|
|
documents_counts,
|
2022-09-13 17:46:07 +08:00
|
|
|
content_files,
|
2022-09-30 00:15:50 +08:00
|
|
|
mut tasks,
|
2022-09-13 17:46:07 +08:00
|
|
|
} => {
|
2022-10-22 20:45:20 +08:00
|
|
|
let mut primary_key_has_been_set = false;
|
2022-10-19 17:22:59 +08:00
|
|
|
let must_stop_processing = self.must_stop_processing.clone();
|
2022-09-30 00:15:50 +08:00
|
|
|
let indexer_config = self.index_mapper.indexer_config();
|
2022-10-04 17:48:08 +08:00
|
|
|
// TODO use the code from the IndexCreate operation
|
2022-09-30 00:15:50 +08:00
|
|
|
if let Some(primary_key) = primary_key {
|
|
|
|
if index.primary_key(index_wtxn)?.is_none() {
|
|
|
|
let mut builder =
|
|
|
|
milli::update::Settings::new(index_wtxn, index, indexer_config);
|
|
|
|
builder.set_primary_key(primary_key);
|
2022-10-17 23:19:17 +08:00
|
|
|
builder.execute(
|
|
|
|
|indexing_step| debug!("update: {:?}", indexing_step),
|
2022-10-19 17:22:59 +08:00
|
|
|
|| must_stop_processing.clone().get(),
|
2022-10-17 23:19:17 +08:00
|
|
|
)?;
|
2022-10-22 20:45:20 +08:00
|
|
|
primary_key_has_been_set = true;
|
2022-09-29 22:04:23 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-10-21 00:00:07 +08:00
|
|
|
let config = IndexDocumentsConfig { update_method: method, ..Default::default() };
|
2022-09-29 22:04:23 +08:00
|
|
|
|
2022-09-30 00:15:50 +08:00
|
|
|
let mut builder = milli::update::IndexDocuments::new(
|
|
|
|
index_wtxn,
|
|
|
|
index,
|
|
|
|
indexer_config,
|
|
|
|
config,
|
|
|
|
|indexing_step| debug!("update: {:?}", indexing_step),
|
2022-10-19 17:22:59 +08:00
|
|
|
|| must_stop_processing.get(),
|
2022-09-29 22:04:23 +08:00
|
|
|
)?;
|
|
|
|
|
2022-09-30 00:15:50 +08:00
|
|
|
let mut results = Vec::new();
|
|
|
|
for content_uuid in content_files.into_iter() {
|
|
|
|
let content_file = self.file_store.get_update(content_uuid)?;
|
|
|
|
let reader = DocumentsBatchReader::from_reader(content_file)
|
2022-10-11 23:42:43 +08:00
|
|
|
.map_err(milli::Error::from)?;
|
2022-09-30 00:15:50 +08:00
|
|
|
let (new_builder, user_result) = builder.add_documents(reader)?;
|
|
|
|
builder = new_builder;
|
|
|
|
|
|
|
|
let user_result = match user_result {
|
2022-10-05 00:50:18 +08:00
|
|
|
Ok(count) => Ok(DocumentAdditionResult {
|
|
|
|
indexed_documents: count,
|
2022-10-16 07:39:01 +08:00
|
|
|
number_of_documents: count, // TODO: this is wrong, we should use the value stored in the Details.
|
2022-10-05 00:50:18 +08:00
|
|
|
}),
|
2022-10-11 23:42:43 +08:00
|
|
|
Err(e) => Err(milli::Error::from(e)),
|
2022-09-30 00:15:50 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
results.push(user_result);
|
|
|
|
}
|
|
|
|
|
|
|
|
if results.iter().any(|res| res.is_ok()) {
|
|
|
|
let addition = builder.execute()?;
|
|
|
|
info!("document addition done: {:?}", addition);
|
2022-10-22 20:45:20 +08:00
|
|
|
} else if primary_key_has_been_set {
|
|
|
|
// Everything failed but we've set a primary key.
|
|
|
|
// We need to remove it.
|
|
|
|
let mut builder =
|
|
|
|
milli::update::Settings::new(index_wtxn, index, indexer_config);
|
|
|
|
builder.reset_primary_key();
|
|
|
|
builder.execute(
|
|
|
|
|indexing_step| debug!("update: {:?}", indexing_step),
|
|
|
|
|| must_stop_processing.clone().get(),
|
|
|
|
)?;
|
2022-09-30 00:15:50 +08:00
|
|
|
}
|
|
|
|
|
2022-10-21 00:00:07 +08:00
|
|
|
for (task, (ret, count)) in
|
|
|
|
tasks.iter_mut().zip(results.into_iter().zip(documents_counts))
|
2022-10-05 00:50:18 +08:00
|
|
|
{
|
2022-09-29 22:04:23 +08:00
|
|
|
match ret {
|
2022-10-21 00:00:07 +08:00
|
|
|
Ok(DocumentAdditionResult { indexed_documents, number_of_documents }) => {
|
2022-10-05 00:50:18 +08:00
|
|
|
task.status = Status::Succeeded;
|
2022-10-22 00:03:10 +08:00
|
|
|
task.details = Some(Details::DocumentAdditionOrUpdate {
|
2022-09-29 22:04:23 +08:00
|
|
|
received_documents: number_of_documents,
|
2022-10-13 21:02:59 +08:00
|
|
|
indexed_documents: Some(indexed_documents),
|
2022-10-05 00:50:18 +08:00
|
|
|
});
|
|
|
|
}
|
|
|
|
Err(error) => {
|
|
|
|
task.status = Status::Failed;
|
2022-10-22 00:03:10 +08:00
|
|
|
task.details = Some(Details::DocumentAdditionOrUpdate {
|
2022-10-05 00:50:18 +08:00
|
|
|
received_documents: count,
|
2022-10-13 21:02:59 +08:00
|
|
|
indexed_documents: Some(count),
|
2022-10-05 00:50:18 +08:00
|
|
|
});
|
|
|
|
task.error = Some(error.into())
|
2022-09-29 22:04:23 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Ok(tasks)
|
2022-09-09 18:16:19 +08:00
|
|
|
}
|
2022-10-21 00:00:07 +08:00
|
|
|
IndexOperation::DocumentDeletion { index_uid: _, documents, mut tasks } => {
|
2022-09-30 00:15:50 +08:00
|
|
|
let mut builder = milli::update::DeleteDocuments::new(index_wtxn, index)?;
|
|
|
|
documents.iter().for_each(|id| {
|
|
|
|
builder.delete_external_id(id);
|
|
|
|
});
|
|
|
|
|
2022-10-21 00:00:07 +08:00
|
|
|
let DocumentDeletionResult { deleted_documents, .. } = builder.execute()?;
|
2022-10-05 22:48:43 +08:00
|
|
|
|
2022-09-30 00:15:50 +08:00
|
|
|
for (task, documents) in tasks.iter_mut().zip(documents) {
|
2022-10-05 22:48:43 +08:00
|
|
|
task.status = Status::Succeeded;
|
|
|
|
task.details = Some(Details::DocumentDeletion {
|
|
|
|
received_document_ids: documents.len(),
|
|
|
|
deleted_documents: Some(deleted_documents),
|
|
|
|
});
|
2022-09-29 17:49:47 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
Ok(tasks)
|
|
|
|
}
|
2022-10-21 00:00:07 +08:00
|
|
|
IndexOperation::Settings { index_uid: _, settings, mut tasks } => {
|
2022-09-30 00:15:50 +08:00
|
|
|
let indexer_config = self.index_mapper.indexer_config();
|
|
|
|
// TODO merge the settings to only do *one* reindexation.
|
2022-09-29 19:57:28 +08:00
|
|
|
for (task, (_, settings)) in tasks.iter_mut().zip(settings) {
|
|
|
|
let checked_settings = settings.clone().check();
|
2022-10-22 00:03:10 +08:00
|
|
|
task.details = Some(Details::SettingsUpdate { settings });
|
2022-09-30 00:15:50 +08:00
|
|
|
|
|
|
|
let mut builder =
|
|
|
|
milli::update::Settings::new(index_wtxn, index, indexer_config);
|
|
|
|
apply_settings_to_builder(&checked_settings, &mut builder);
|
2022-10-19 17:22:59 +08:00
|
|
|
let must_stop_processing = self.must_stop_processing.clone();
|
2022-10-17 23:19:17 +08:00
|
|
|
builder.execute(
|
|
|
|
|indexing_step| debug!("update: {:?}", indexing_step),
|
2022-10-19 17:22:59 +08:00
|
|
|
|| must_stop_processing.get(),
|
2022-10-17 23:19:17 +08:00
|
|
|
)?;
|
2022-09-30 00:15:50 +08:00
|
|
|
|
2022-10-05 22:48:43 +08:00
|
|
|
task.status = Status::Succeeded;
|
2022-09-29 19:57:28 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
Ok(tasks)
|
|
|
|
}
|
2022-09-30 00:15:50 +08:00
|
|
|
IndexOperation::SettingsAndDocumentImport {
|
2022-09-16 22:31:16 +08:00
|
|
|
index_uid,
|
2022-09-30 00:15:50 +08:00
|
|
|
primary_key,
|
|
|
|
method,
|
2022-10-05 00:50:18 +08:00
|
|
|
documents_counts,
|
2022-09-30 00:15:50 +08:00
|
|
|
content_files,
|
|
|
|
document_import_tasks,
|
2022-09-16 22:31:16 +08:00
|
|
|
settings,
|
2022-09-30 00:15:50 +08:00
|
|
|
settings_tasks,
|
2022-09-29 20:19:13 +08:00
|
|
|
} => {
|
2022-09-30 00:15:50 +08:00
|
|
|
let settings_tasks = self.apply_index_operation(
|
|
|
|
index_wtxn,
|
|
|
|
index,
|
|
|
|
IndexOperation::Settings {
|
|
|
|
index_uid: index_uid.clone(),
|
|
|
|
settings,
|
|
|
|
tasks: settings_tasks,
|
|
|
|
},
|
|
|
|
)?;
|
2022-09-29 20:19:13 +08:00
|
|
|
|
2022-09-30 00:15:50 +08:00
|
|
|
let mut import_tasks = self.apply_index_operation(
|
|
|
|
index_wtxn,
|
|
|
|
index,
|
|
|
|
IndexOperation::DocumentImport {
|
|
|
|
index_uid,
|
|
|
|
primary_key,
|
|
|
|
method,
|
2022-10-05 00:50:18 +08:00
|
|
|
documents_counts,
|
2022-09-30 00:15:50 +08:00
|
|
|
content_files,
|
|
|
|
tasks: document_import_tasks,
|
|
|
|
},
|
|
|
|
)?;
|
2022-09-29 20:19:13 +08:00
|
|
|
|
2022-09-30 00:15:50 +08:00
|
|
|
let mut tasks = settings_tasks;
|
|
|
|
tasks.append(&mut import_tasks);
|
2022-09-29 20:19:13 +08:00
|
|
|
Ok(tasks)
|
|
|
|
}
|
2022-09-30 00:15:50 +08:00
|
|
|
IndexOperation::DocumentClearAndSetting {
|
2022-09-17 03:24:49 +08:00
|
|
|
index_uid,
|
2022-09-30 00:15:50 +08:00
|
|
|
cleared_tasks,
|
|
|
|
settings,
|
|
|
|
settings_tasks,
|
|
|
|
} => {
|
|
|
|
let mut import_tasks = self.apply_index_operation(
|
|
|
|
index_wtxn,
|
|
|
|
index,
|
|
|
|
IndexOperation::DocumentClear {
|
|
|
|
index_uid: index_uid.clone(),
|
|
|
|
tasks: cleared_tasks,
|
|
|
|
},
|
|
|
|
)?;
|
|
|
|
|
|
|
|
let settings_tasks = self.apply_index_operation(
|
|
|
|
index_wtxn,
|
|
|
|
index,
|
2022-10-21 00:00:07 +08:00
|
|
|
IndexOperation::Settings { index_uid, settings, tasks: settings_tasks },
|
2022-09-30 00:15:50 +08:00
|
|
|
)?;
|
|
|
|
|
|
|
|
let mut tasks = settings_tasks;
|
|
|
|
tasks.append(&mut import_tasks);
|
|
|
|
Ok(tasks)
|
|
|
|
}
|
2022-09-07 06:10:14 +08:00
|
|
|
}
|
|
|
|
}
|
2022-10-06 22:53:21 +08:00
|
|
|
|
|
|
|
/// Delete each given task from all the databases (if it is deleteable).
|
|
|
|
///
|
2022-10-17 18:58:20 +08:00
|
|
|
/// Return the number of tasks that were actually deleted.
|
2022-10-19 17:09:40 +08:00
|
|
|
fn delete_matched_tasks(&self, wtxn: &mut RwTxn, matched_tasks: &RoaringBitmap) -> Result<u64> {
|
2022-10-06 22:53:21 +08:00
|
|
|
// 1. Remove from this list the tasks that we are not allowed to delete
|
|
|
|
let enqueued_tasks = self.get_status(wtxn, Status::Enqueued)?;
|
2022-10-17 19:54:35 +08:00
|
|
|
let processing_tasks = &self.processing_tasks.read().unwrap().processing.clone();
|
2022-10-06 22:53:21 +08:00
|
|
|
|
2022-10-17 18:58:20 +08:00
|
|
|
let all_task_ids = self.all_task_ids(&wtxn)?;
|
|
|
|
let mut to_delete_tasks = all_task_ids & matched_tasks;
|
|
|
|
to_delete_tasks -= processing_tasks;
|
2022-10-13 17:09:00 +08:00
|
|
|
to_delete_tasks -= enqueued_tasks;
|
2022-10-06 22:53:21 +08:00
|
|
|
|
2022-10-13 17:09:00 +08:00
|
|
|
// 2. We now have a list of tasks to delete, delete them
|
2022-10-06 22:53:21 +08:00
|
|
|
|
2022-10-13 17:09:00 +08:00
|
|
|
let mut affected_indexes = HashSet::new();
|
|
|
|
let mut affected_statuses = HashSet::new();
|
|
|
|
let mut affected_kinds = HashSet::new();
|
2022-10-06 22:53:21 +08:00
|
|
|
|
2022-10-13 17:09:00 +08:00
|
|
|
for task_id in to_delete_tasks.iter() {
|
2022-10-21 00:00:07 +08:00
|
|
|
let task = self.get_task(wtxn, task_id)?.ok_or(Error::CorruptedTaskQueue)?;
|
2022-10-18 21:04:14 +08:00
|
|
|
if let Some(task_indexes) = task.indexes() {
|
|
|
|
affected_indexes.extend(task_indexes.into_iter().map(|x| x.to_owned()));
|
2022-10-06 22:53:21 +08:00
|
|
|
}
|
2022-10-18 21:04:14 +08:00
|
|
|
affected_statuses.insert(task.status);
|
|
|
|
affected_kinds.insert(task.kind.as_kind());
|
|
|
|
// Note: don't delete the persisted task data since
|
|
|
|
// we can only delete succeeded, failed, and canceled tasks.
|
|
|
|
// In each of those cases, the persisted data is supposed to
|
|
|
|
// have been deleted already.
|
2022-10-19 18:59:12 +08:00
|
|
|
utils::remove_task_datetime(wtxn, self.enqueued_at, task.enqueued_at, task.uid)?;
|
|
|
|
if let Some(started_at) = task.started_at {
|
|
|
|
utils::remove_task_datetime(wtxn, self.started_at, started_at, task.uid)?;
|
|
|
|
}
|
|
|
|
if let Some(finished_at) = task.finished_at {
|
|
|
|
utils::remove_task_datetime(wtxn, self.finished_at, finished_at, task.uid)?;
|
|
|
|
}
|
2022-10-06 22:53:21 +08:00
|
|
|
}
|
2022-10-18 19:47:22 +08:00
|
|
|
|
2022-10-13 17:09:00 +08:00
|
|
|
for index in affected_indexes {
|
2022-10-18 19:47:22 +08:00
|
|
|
self.update_index(wtxn, &index, |bitmap| *bitmap -= &to_delete_tasks)?;
|
2022-10-13 17:09:00 +08:00
|
|
|
}
|
2022-10-18 19:47:22 +08:00
|
|
|
|
2022-10-13 17:09:00 +08:00
|
|
|
for status in affected_statuses {
|
2022-10-18 19:47:22 +08:00
|
|
|
self.update_status(wtxn, status, |bitmap| *bitmap -= &to_delete_tasks)?;
|
2022-10-13 17:09:00 +08:00
|
|
|
}
|
2022-10-18 19:47:22 +08:00
|
|
|
|
2022-10-13 17:09:00 +08:00
|
|
|
for kind in affected_kinds {
|
2022-10-18 19:47:22 +08:00
|
|
|
self.update_kind(wtxn, kind, |bitmap| *bitmap -= &to_delete_tasks)?;
|
2022-10-13 17:09:00 +08:00
|
|
|
}
|
2022-10-18 19:47:22 +08:00
|
|
|
|
2022-10-13 17:09:00 +08:00
|
|
|
for task in to_delete_tasks.iter() {
|
|
|
|
self.all_tasks.delete(wtxn, &BEU32::new(task))?;
|
|
|
|
}
|
2022-10-18 19:47:22 +08:00
|
|
|
|
2022-10-19 17:09:40 +08:00
|
|
|
Ok(to_delete_tasks.len())
|
2022-10-06 22:53:21 +08:00
|
|
|
}
|
2022-10-18 19:47:22 +08:00
|
|
|
|
|
|
|
/// Cancel each given task from all the databases (if it is cancelable).
|
|
|
|
///
|
2022-10-19 17:48:35 +08:00
|
|
|
/// Returns the content files that the transaction owner must delete if the commit is successful.
|
2022-10-18 19:47:22 +08:00
|
|
|
fn cancel_matched_tasks(
|
|
|
|
&self,
|
|
|
|
wtxn: &mut RwTxn,
|
2022-10-18 19:57:58 +08:00
|
|
|
cancel_task_id: TaskId,
|
2022-10-18 19:47:22 +08:00
|
|
|
matched_tasks: &RoaringBitmap,
|
2022-10-19 17:48:35 +08:00
|
|
|
) -> Result<Vec<Uuid>> {
|
2022-10-18 19:57:58 +08:00
|
|
|
let now = OffsetDateTime::now_utc();
|
|
|
|
|
2022-10-18 19:47:22 +08:00
|
|
|
// 1. Remove from this list the tasks that we are not allowed to cancel
|
|
|
|
// Notice that only the _enqueued_ ones are cancelable and we should
|
|
|
|
// have already aborted the indexation of the _processing_ ones
|
2022-10-18 19:57:58 +08:00
|
|
|
let cancelable_tasks = self.get_status(wtxn, Status::Enqueued)?;
|
2022-10-18 19:47:22 +08:00
|
|
|
let tasks_to_cancel = cancelable_tasks & matched_tasks;
|
|
|
|
|
|
|
|
// 2. We now have a list of tasks to cancel, cancel them
|
2022-10-19 17:48:35 +08:00
|
|
|
let mut content_files_to_delete = Vec::new();
|
2022-10-18 19:57:58 +08:00
|
|
|
for mut task in self.get_existing_tasks(wtxn, tasks_to_cancel.iter())? {
|
2022-10-19 17:48:35 +08:00
|
|
|
if let Some(uuid) = task.content_uuid() {
|
|
|
|
content_files_to_delete.push(*uuid);
|
|
|
|
}
|
2022-10-18 19:57:58 +08:00
|
|
|
task.status = Status::Canceled;
|
|
|
|
task.canceled_by = Some(cancel_task_id);
|
|
|
|
task.finished_at = Some(now);
|
|
|
|
self.update_task(wtxn, &task)?;
|
|
|
|
}
|
2022-10-18 19:47:22 +08:00
|
|
|
|
2022-10-19 17:48:35 +08:00
|
|
|
Ok(content_files_to_delete)
|
2022-10-18 19:47:22 +08:00
|
|
|
}
|
2022-09-07 06:10:14 +08:00
|
|
|
}
|