2022-06-06 18:38:46 +08:00
|
|
|
use meilisearch_types::error::ResponseError;
|
|
|
|
use meilisearch_types::index_uid::IndexUid;
|
2021-12-02 23:03:26 +08:00
|
|
|
use milli::update::{DocumentAdditionResult, IndexDocumentsMethod};
|
|
|
|
use serde::{Deserialize, Serialize};
|
2022-02-14 22:32:41 +08:00
|
|
|
use time::OffsetDateTime;
|
2021-12-02 23:03:26 +08:00
|
|
|
use uuid::Uuid;
|
|
|
|
|
|
|
|
use super::batch::BatchId;
|
2022-05-31 23:18:40 +08:00
|
|
|
use crate::index::{Settings, Unchecked};
|
2021-12-02 23:03:26 +08:00
|
|
|
|
2022-06-01 18:04:01 +08:00
|
|
|
pub type TaskId = u32;
|
2021-12-02 23:03:26 +08:00
|
|
|
|
|
|
|
#[derive(Clone, Debug, Serialize, Deserialize, PartialEq, Eq)]
|
|
|
|
#[cfg_attr(test, derive(proptest_derive::Arbitrary))]
|
|
|
|
pub enum TaskResult {
|
|
|
|
DocumentAddition { indexed_documents: u64 },
|
|
|
|
DocumentDeletion { deleted_documents: u64 },
|
|
|
|
ClearAll { deleted_documents: u64 },
|
|
|
|
Other,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl From<DocumentAdditionResult> for TaskResult {
|
|
|
|
fn from(other: DocumentAdditionResult) -> Self {
|
|
|
|
Self::DocumentAddition {
|
|
|
|
indexed_documents: other.indexed_documents,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Clone, Debug, Serialize, Deserialize, PartialEq, Eq)]
|
|
|
|
#[cfg_attr(test, derive(proptest_derive::Arbitrary))]
|
|
|
|
pub enum TaskEvent {
|
2022-03-02 02:02:32 +08:00
|
|
|
Created(
|
|
|
|
#[cfg_attr(test, proptest(strategy = "test::datetime_strategy()"))]
|
|
|
|
#[serde(with = "time::serde::rfc3339")]
|
|
|
|
OffsetDateTime,
|
|
|
|
),
|
2021-12-02 23:03:26 +08:00
|
|
|
Batched {
|
|
|
|
#[cfg_attr(test, proptest(strategy = "test::datetime_strategy()"))]
|
2022-03-02 02:02:32 +08:00
|
|
|
#[serde(with = "time::serde::rfc3339")]
|
2022-02-14 22:32:41 +08:00
|
|
|
timestamp: OffsetDateTime,
|
2021-12-02 23:03:26 +08:00
|
|
|
batch_id: BatchId,
|
|
|
|
},
|
2022-03-02 02:02:32 +08:00
|
|
|
Processing(
|
|
|
|
#[cfg_attr(test, proptest(strategy = "test::datetime_strategy()"))]
|
|
|
|
#[serde(with = "time::serde::rfc3339")]
|
|
|
|
OffsetDateTime,
|
|
|
|
),
|
2022-05-30 22:42:51 +08:00
|
|
|
Succeeded {
|
2021-12-02 23:03:26 +08:00
|
|
|
result: TaskResult,
|
|
|
|
#[cfg_attr(test, proptest(strategy = "test::datetime_strategy()"))]
|
2022-03-02 02:02:32 +08:00
|
|
|
#[serde(with = "time::serde::rfc3339")]
|
2022-02-14 22:32:41 +08:00
|
|
|
timestamp: OffsetDateTime,
|
2021-12-02 23:03:26 +08:00
|
|
|
},
|
|
|
|
Failed {
|
|
|
|
error: ResponseError,
|
|
|
|
#[cfg_attr(test, proptest(strategy = "test::datetime_strategy()"))]
|
2022-03-02 02:02:32 +08:00
|
|
|
#[serde(with = "time::serde::rfc3339")]
|
2022-02-14 22:32:41 +08:00
|
|
|
timestamp: OffsetDateTime,
|
2021-12-02 23:03:26 +08:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2022-05-20 02:18:43 +08:00
|
|
|
impl TaskEvent {
|
|
|
|
pub fn succeeded(result: TaskResult) -> Self {
|
2022-05-30 22:42:51 +08:00
|
|
|
Self::Succeeded {
|
2022-05-20 02:18:43 +08:00
|
|
|
result,
|
|
|
|
timestamp: OffsetDateTime::now_utc(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-06-02 21:04:33 +08:00
|
|
|
pub fn failed(error: impl Into<ResponseError>) -> Self {
|
2022-05-20 02:18:43 +08:00
|
|
|
Self::Failed {
|
2022-06-02 21:04:33 +08:00
|
|
|
error: error.into(),
|
2022-05-20 02:18:43 +08:00
|
|
|
timestamp: OffsetDateTime::now_utc(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-01-27 00:43:16 +08:00
|
|
|
/// A task represents an operation that Meilisearch must do.
|
2021-12-02 23:03:26 +08:00
|
|
|
/// It's stored on disk and executed from the lowest to highest Task id.
|
2022-06-15 09:38:45 +08:00
|
|
|
/// Every time a new task is created it has a higher Task id than the previous one.
|
2021-12-02 23:03:26 +08:00
|
|
|
/// See also `Job`.
|
|
|
|
#[derive(Clone, Debug, Serialize, Deserialize, PartialEq)]
|
|
|
|
#[cfg_attr(test, derive(proptest_derive::Arbitrary))]
|
|
|
|
pub struct Task {
|
|
|
|
pub id: TaskId,
|
2022-05-30 20:34:49 +08:00
|
|
|
/// The name of the index the task is targeting. If it isn't targeting any index (i.e Dump task)
|
2022-05-17 01:50:45 +08:00
|
|
|
/// then this is None
|
|
|
|
// TODO: when next forward breaking dumps, it would be a good idea to move this field inside of
|
|
|
|
// the TaskContent.
|
2021-12-02 23:03:26 +08:00
|
|
|
pub content: TaskContent,
|
|
|
|
pub events: Vec<TaskEvent>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Task {
|
|
|
|
/// Return true when a task is finished.
|
|
|
|
/// A task is finished when its last state is either `Succeeded` or `Failed`.
|
|
|
|
pub fn is_finished(&self) -> bool {
|
|
|
|
self.events.last().map_or(false, |event| {
|
2022-05-30 22:42:51 +08:00
|
|
|
matches!(
|
|
|
|
event,
|
|
|
|
TaskEvent::Succeeded { .. } | TaskEvent::Failed { .. }
|
|
|
|
)
|
2021-12-02 23:03:26 +08:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Return the content_uuid of the `Task` if there is one.
|
|
|
|
pub fn get_content_uuid(&self) -> Option<Uuid> {
|
|
|
|
match self {
|
|
|
|
Task {
|
|
|
|
content: TaskContent::DocumentAddition { content_uuid, .. },
|
|
|
|
..
|
|
|
|
} => Some(*content_uuid),
|
|
|
|
_ => None,
|
|
|
|
}
|
|
|
|
}
|
2022-05-31 23:18:40 +08:00
|
|
|
|
|
|
|
pub fn index_uid(&self) -> Option<&str> {
|
|
|
|
match &self.content {
|
|
|
|
TaskContent::DocumentAddition { index_uid, .. }
|
|
|
|
| TaskContent::DocumentDeletion { index_uid, .. }
|
|
|
|
| TaskContent::SettingsUpdate { index_uid, .. }
|
|
|
|
| TaskContent::IndexDeletion { index_uid }
|
|
|
|
| TaskContent::IndexCreation { index_uid, .. }
|
|
|
|
| TaskContent::IndexUpdate { index_uid, .. } => Some(index_uid.as_str()),
|
|
|
|
TaskContent::Dump { .. } => None,
|
|
|
|
}
|
|
|
|
}
|
2021-12-02 23:03:26 +08:00
|
|
|
}
|
|
|
|
|
2022-08-12 20:18:27 +08:00
|
|
|
#[derive(Clone, Debug, Serialize, Deserialize, PartialEq, Eq)]
|
2021-12-02 23:03:26 +08:00
|
|
|
#[cfg_attr(test, derive(proptest_derive::Arbitrary))]
|
|
|
|
pub enum DocumentDeletion {
|
|
|
|
Clear,
|
|
|
|
Ids(Vec<String>),
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Clone, Debug, Serialize, Deserialize, PartialEq)]
|
|
|
|
#[cfg_attr(test, derive(proptest_derive::Arbitrary))]
|
|
|
|
#[allow(clippy::large_enum_variant)]
|
|
|
|
pub enum TaskContent {
|
|
|
|
DocumentAddition {
|
2022-05-31 23:18:40 +08:00
|
|
|
index_uid: IndexUid,
|
2021-12-02 23:03:26 +08:00
|
|
|
#[cfg_attr(test, proptest(value = "Uuid::new_v4()"))]
|
|
|
|
content_uuid: Uuid,
|
|
|
|
#[cfg_attr(test, proptest(strategy = "test::index_document_method_strategy()"))]
|
|
|
|
merge_strategy: IndexDocumentsMethod,
|
|
|
|
primary_key: Option<String>,
|
|
|
|
documents_count: usize,
|
2021-12-15 21:52:33 +08:00
|
|
|
allow_index_creation: bool,
|
2021-12-02 23:03:26 +08:00
|
|
|
},
|
2022-05-31 23:18:40 +08:00
|
|
|
DocumentDeletion {
|
|
|
|
index_uid: IndexUid,
|
|
|
|
deletion: DocumentDeletion,
|
|
|
|
},
|
2021-12-02 23:03:26 +08:00
|
|
|
SettingsUpdate {
|
2022-05-31 23:18:40 +08:00
|
|
|
index_uid: IndexUid,
|
2021-12-02 23:03:26 +08:00
|
|
|
settings: Settings<Unchecked>,
|
|
|
|
/// Indicates whether the task was a deletion
|
|
|
|
is_deletion: bool,
|
2021-12-15 21:52:33 +08:00
|
|
|
allow_index_creation: bool,
|
2021-12-02 23:03:26 +08:00
|
|
|
},
|
2022-05-31 23:18:40 +08:00
|
|
|
IndexDeletion {
|
|
|
|
index_uid: IndexUid,
|
|
|
|
},
|
2021-12-02 23:03:26 +08:00
|
|
|
IndexCreation {
|
2022-05-31 23:18:40 +08:00
|
|
|
index_uid: IndexUid,
|
2021-12-02 23:03:26 +08:00
|
|
|
primary_key: Option<String>,
|
|
|
|
},
|
|
|
|
IndexUpdate {
|
2022-05-31 23:18:40 +08:00
|
|
|
index_uid: IndexUid,
|
2021-12-02 23:03:26 +08:00
|
|
|
primary_key: Option<String>,
|
|
|
|
},
|
2022-05-20 02:18:43 +08:00
|
|
|
Dump {
|
|
|
|
uid: String,
|
|
|
|
},
|
2021-12-02 23:03:26 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod test {
|
|
|
|
use proptest::prelude::*;
|
|
|
|
|
|
|
|
use super::*;
|
|
|
|
|
|
|
|
pub(super) fn index_document_method_strategy() -> impl Strategy<Value = IndexDocumentsMethod> {
|
|
|
|
prop_oneof![
|
|
|
|
Just(IndexDocumentsMethod::ReplaceDocuments),
|
|
|
|
Just(IndexDocumentsMethod::UpdateDocuments),
|
|
|
|
]
|
|
|
|
}
|
|
|
|
|
2022-02-14 22:32:41 +08:00
|
|
|
pub(super) fn datetime_strategy() -> impl Strategy<Value = OffsetDateTime> {
|
|
|
|
Just(OffsetDateTime::now_utc())
|
2021-12-02 23:03:26 +08:00
|
|
|
}
|
|
|
|
}
|