mirror of
https://github.com/meilisearch/meilisearch.git
synced 2024-11-30 17:14:59 +08:00
457a473b72
* Fix error code of the "duplicate index found" error * Use the content of the ProcessingTasks in the tasks cancelation system * Change the missing_filters error code into missing_task_filters * WIP Introduce the invalid_task_uid error code * Use more precise error codes/message for the task routes + Allow star operator in delete/cancel tasks + rename originalQuery to originalFilters + Display error/canceled_by in task view even when they are = null + Rename task filter fields by using their plural forms + Prepare an error code for canceledBy filter + Only return global tasks if the API key action `index.*` is there * Add canceledBy task filter * Update tests following task API changes * Rename original_query to original_filters everywhere * Update more insta-snap tests * Make clippy happy They're a happy clip now. * Make rustfmt happy >:-( * Fix Index name parsing error message to fit the specification * Bump milli version to 0.35.1 * Fix the new error messages * fix the error messages and add tests * rename the error codes for the sake of consistency * refactor the way we send the cli informations + add the analytics for the config file and ssl usage * Apply suggestions from code review Co-authored-by: Clément Renault <clement@meilisearch.com> * add a comment over the new infos structure * reformat, sorry @kero * Store analytics for the documents deletions * Add analytics on all the settings * Spawn threads with names * Spawn rayon threads with names * update the distinct attributes to the spec update * update the analytics on the search route * implements the analytics on the health and version routes * Fix task details serialization * Add the question mark to the task deletion query filter * Add the question mark to the task cancelation query filter * Fix tests * add analytics on the task route * Add all the missing fields of the new task query type * Create a new analytics for the task deletion * Create a new analytics for the task creation * batch the tasks seen events * Update the finite pagination analytics * add the analytics of the swap-indexes route * Stop removing the DB when failing to read it * Rename originalFilters into originalFilters * Rename matchedDocuments into providedIds * Add `workflow_dispatch` to flaky.yml * Bump grenad to 0.4.4 * Bump milli to version v0.37.0 * Don't multiply total memory returned by sysinfo anymore sysinfo now returns bytes rather than KB * Add a dispatch to the publish binaries workflow * Fix publish release CI * Don't use gold but the default linker * Always display details for the indexDeletion task * Fix the insta tests * refactorize the whole test suite 1. Make a call to assert_internally_consistent automatically when snapshoting the scheduler. There is no point in snapshoting something broken and expect the dumb humans to notice. 2. Replace every possible call to assert_internally_consistent by a snapshot of the scheduler. It takes as many lines and ensure we never change something without noticing in any tests ever. 3. Name every snapshots: it's easier to debug when something goes wrong and easier to review in general. 4. Stop skipping breakpoints, it's too easy to miss something. Now you must explicitely show which path is the scheduler supposed to use. 5. Add a timeout on the channel.recv, it eases the process of writing tests, now when something file you get a failure instead of a deadlock. * rebase on release-v0.30 * makes clippy happy * update the snapshots after a rebase * try to remove the flakyness of the failing test * Add more analytics on the ranking rules positions * Update the dump test to check for the dumpUid dumpCreation task details * send the ranking rules as a string because amplitude is too dumb to process an array as a single value * Display a null dumpUid until we computed the dump itself on disk * Update tests * Check if the master key is missing before returning an error Co-authored-by: Loïc Lecrenier <loic.lecrenier@me.com> Co-authored-by: bors[bot] <26634292+bors[bot]@users.noreply.github.com> Co-authored-by: Kerollmops <clement@meilisearch.com> Co-authored-by: ManyTheFish <many@meilisearch.com> Co-authored-by: Tamo <tamo@meilisearch.com> Co-authored-by: Louis Dureuil <louis@meilisearch.com>
418 lines
15 KiB
Rust
418 lines
15 KiB
Rust
#![allow(rustdoc::private_intra_doc_links)]
|
|
#[macro_use]
|
|
pub mod error;
|
|
pub mod analytics;
|
|
#[macro_use]
|
|
pub mod extractors;
|
|
pub mod option;
|
|
pub mod routes;
|
|
pub mod search;
|
|
|
|
#[cfg(feature = "metrics")]
|
|
pub mod metrics;
|
|
#[cfg(feature = "metrics")]
|
|
pub mod route_metrics;
|
|
|
|
use std::fs::File;
|
|
use std::io::{BufReader, BufWriter};
|
|
use std::path::Path;
|
|
use std::sync::atomic::AtomicBool;
|
|
use std::sync::Arc;
|
|
use std::thread;
|
|
use std::time::Duration;
|
|
|
|
use actix_cors::Cors;
|
|
use actix_http::body::MessageBody;
|
|
use actix_web::dev::{ServiceFactory, ServiceResponse};
|
|
use actix_web::error::JsonPayloadError;
|
|
use actix_web::web::Data;
|
|
use actix_web::{middleware, web, HttpRequest};
|
|
use analytics::Analytics;
|
|
use anyhow::bail;
|
|
use error::PayloadError;
|
|
use extractors::payload::PayloadConfig;
|
|
use http::header::CONTENT_TYPE;
|
|
use index_scheduler::{IndexScheduler, IndexSchedulerOptions};
|
|
use log::error;
|
|
use meilisearch_auth::AuthController;
|
|
use meilisearch_types::milli::documents::{DocumentsBatchBuilder, DocumentsBatchReader};
|
|
use meilisearch_types::milli::update::{IndexDocumentsConfig, IndexDocumentsMethod};
|
|
use meilisearch_types::settings::apply_settings_to_builder;
|
|
use meilisearch_types::tasks::KindWithContent;
|
|
use meilisearch_types::versioning::{check_version_file, create_version_file};
|
|
use meilisearch_types::{compression, milli, VERSION_FILE_NAME};
|
|
pub use option::Opt;
|
|
|
|
use crate::error::MeilisearchHttpError;
|
|
|
|
pub static AUTOBATCHING_ENABLED: AtomicBool = AtomicBool::new(false);
|
|
|
|
/// Check if a db is empty. It does not provide any information on the
|
|
/// validity of the data in it.
|
|
/// We consider a database as non empty when it's a non empty directory.
|
|
fn is_empty_db(db_path: impl AsRef<Path>) -> bool {
|
|
let db_path = db_path.as_ref();
|
|
|
|
if !db_path.exists() {
|
|
true
|
|
// if we encounter an error or if the db is a file we consider the db non empty
|
|
} else if let Ok(dir) = db_path.read_dir() {
|
|
dir.count() == 0
|
|
} else {
|
|
true
|
|
}
|
|
}
|
|
|
|
pub fn create_app(
|
|
index_scheduler: Data<IndexScheduler>,
|
|
auth_controller: AuthController,
|
|
opt: Opt,
|
|
analytics: Arc<dyn Analytics>,
|
|
enable_dashboard: bool,
|
|
) -> actix_web::App<
|
|
impl ServiceFactory<
|
|
actix_web::dev::ServiceRequest,
|
|
Config = (),
|
|
Response = ServiceResponse<impl MessageBody>,
|
|
Error = actix_web::Error,
|
|
InitError = (),
|
|
>,
|
|
> {
|
|
let app = actix_web::App::new()
|
|
.configure(|s| {
|
|
configure_data(
|
|
s,
|
|
index_scheduler.clone(),
|
|
auth_controller.clone(),
|
|
&opt,
|
|
analytics.clone(),
|
|
)
|
|
})
|
|
.configure(routes::configure)
|
|
.configure(|s| dashboard(s, enable_dashboard));
|
|
#[cfg(feature = "metrics")]
|
|
let app = app.configure(|s| configure_metrics_route(s, opt.enable_metrics_route));
|
|
|
|
#[cfg(feature = "metrics")]
|
|
let app = app.wrap(Condition::new(opt.enable_metrics_route, route_metrics::RouteMetrics));
|
|
app.wrap(
|
|
Cors::default()
|
|
.send_wildcard()
|
|
.allow_any_header()
|
|
.allow_any_origin()
|
|
.allow_any_method()
|
|
.max_age(86_400), // 24h
|
|
)
|
|
.wrap(middleware::Logger::default())
|
|
.wrap(middleware::Compress::default())
|
|
.wrap(middleware::NormalizePath::new(middleware::TrailingSlash::Trim))
|
|
}
|
|
|
|
// TODO: TAMO: Finish setting up things
|
|
pub fn setup_meilisearch(opt: &Opt) -> anyhow::Result<(Arc<IndexScheduler>, AuthController)> {
|
|
// we don't want to create anything in the data.ms yet, thus we
|
|
// wrap our two builders in a closure that'll be executed later.
|
|
let auth_controller_builder = || AuthController::new(&opt.db_path, &opt.master_key);
|
|
let index_scheduler_builder = || {
|
|
IndexScheduler::new(IndexSchedulerOptions {
|
|
version_file_path: opt.db_path.join(VERSION_FILE_NAME),
|
|
auth_path: opt.db_path.join("auth"),
|
|
tasks_path: opt.db_path.join("tasks"),
|
|
update_file_path: opt.db_path.join("update_files"),
|
|
indexes_path: opt.db_path.join("indexes"),
|
|
snapshots_path: opt.snapshot_dir.clone(),
|
|
dumps_path: opt.dumps_dir.clone(),
|
|
task_db_size: opt.max_task_db_size.get_bytes() as usize,
|
|
index_size: opt.max_index_size.get_bytes() as usize,
|
|
indexer_config: (&opt.indexer_options).try_into()?,
|
|
autobatching_enabled: !opt.scheduler_options.disable_auto_batching,
|
|
})
|
|
};
|
|
|
|
enum OnFailure {
|
|
RemoveDb,
|
|
KeepDb,
|
|
}
|
|
|
|
let meilisearch_builder = |on_failure: OnFailure| -> anyhow::Result<_> {
|
|
// if anything wrong happens we delete the `data.ms` entirely.
|
|
match (
|
|
index_scheduler_builder().map_err(anyhow::Error::from),
|
|
auth_controller_builder().map_err(anyhow::Error::from),
|
|
create_version_file(&opt.db_path).map_err(anyhow::Error::from),
|
|
) {
|
|
(Ok(i), Ok(a), Ok(())) => Ok((i, a)),
|
|
(Err(e), _, _) | (_, Err(e), _) | (_, _, Err(e)) => {
|
|
if matches!(on_failure, OnFailure::RemoveDb) {
|
|
std::fs::remove_dir_all(&opt.db_path)?;
|
|
}
|
|
Err(e)
|
|
}
|
|
}
|
|
};
|
|
|
|
let empty_db = is_empty_db(&opt.db_path);
|
|
let (index_scheduler, auth_controller) = if let Some(ref snapshot_path) = opt.import_snapshot {
|
|
let snapshot_path_exists = snapshot_path.exists();
|
|
if empty_db && snapshot_path_exists {
|
|
match compression::from_tar_gz(snapshot_path, &opt.db_path) {
|
|
Ok(()) => meilisearch_builder(OnFailure::RemoveDb)?,
|
|
Err(e) => {
|
|
std::fs::remove_dir_all(&opt.db_path)?;
|
|
return Err(e);
|
|
}
|
|
}
|
|
} else if !empty_db && !opt.ignore_snapshot_if_db_exists {
|
|
bail!(
|
|
"database already exists at {:?}, try to delete it or rename it",
|
|
opt.db_path.canonicalize().unwrap_or_else(|_| opt.db_path.to_owned())
|
|
)
|
|
} else if !snapshot_path_exists && !opt.ignore_missing_snapshot {
|
|
bail!("snapshot doesn't exist at {}", snapshot_path.display())
|
|
} else {
|
|
meilisearch_builder(OnFailure::RemoveDb)?
|
|
}
|
|
} else if let Some(ref path) = opt.import_dump {
|
|
let src_path_exists = path.exists();
|
|
if empty_db && src_path_exists {
|
|
let (mut index_scheduler, mut auth_controller) =
|
|
meilisearch_builder(OnFailure::RemoveDb)?;
|
|
match import_dump(&opt.db_path, path, &mut index_scheduler, &mut auth_controller) {
|
|
Ok(()) => (index_scheduler, auth_controller),
|
|
Err(e) => {
|
|
std::fs::remove_dir_all(&opt.db_path)?;
|
|
return Err(e);
|
|
}
|
|
}
|
|
} else if !empty_db && !opt.ignore_dump_if_db_exists {
|
|
bail!(
|
|
"database already exists at {:?}, try to delete it or rename it",
|
|
opt.db_path.canonicalize().unwrap_or_else(|_| opt.db_path.to_owned())
|
|
)
|
|
} else if !src_path_exists && !opt.ignore_missing_dump {
|
|
bail!("dump doesn't exist at {:?}", path)
|
|
} else {
|
|
let (mut index_scheduler, mut auth_controller) =
|
|
meilisearch_builder(OnFailure::RemoveDb)?;
|
|
match import_dump(&opt.db_path, path, &mut index_scheduler, &mut auth_controller) {
|
|
Ok(()) => (index_scheduler, auth_controller),
|
|
Err(e) => {
|
|
std::fs::remove_dir_all(&opt.db_path)?;
|
|
return Err(e);
|
|
}
|
|
}
|
|
}
|
|
} else {
|
|
if !empty_db {
|
|
check_version_file(&opt.db_path)?;
|
|
}
|
|
meilisearch_builder(OnFailure::KeepDb)?
|
|
};
|
|
|
|
// We create a loop in a thread that registers snapshotCreation tasks
|
|
let index_scheduler = Arc::new(index_scheduler);
|
|
if opt.schedule_snapshot {
|
|
let snapshot_delay = Duration::from_secs(opt.snapshot_interval_sec);
|
|
let index_scheduler = index_scheduler.clone();
|
|
thread::Builder::new()
|
|
.name(String::from("register-snapshot-tasks"))
|
|
.spawn(move || loop {
|
|
thread::sleep(snapshot_delay);
|
|
if let Err(e) = index_scheduler.register(KindWithContent::SnapshotCreation) {
|
|
error!("Error while registering snapshot: {}", e);
|
|
}
|
|
})
|
|
.unwrap();
|
|
}
|
|
|
|
Ok((index_scheduler, auth_controller))
|
|
}
|
|
|
|
fn import_dump(
|
|
db_path: &Path,
|
|
dump_path: &Path,
|
|
index_scheduler: &mut IndexScheduler,
|
|
auth: &mut AuthController,
|
|
) -> Result<(), anyhow::Error> {
|
|
let reader = File::open(dump_path)?;
|
|
let mut dump_reader = dump::DumpReader::open(reader)?;
|
|
|
|
if let Some(date) = dump_reader.date() {
|
|
log::info!(
|
|
"Importing a dump of meilisearch `{:?}` from the {}",
|
|
dump_reader.version(), // TODO: get the meilisearch version instead of the dump version
|
|
date
|
|
);
|
|
} else {
|
|
log::info!(
|
|
"Importing a dump of meilisearch `{:?}`",
|
|
dump_reader.version(), // TODO: get the meilisearch version instead of the dump version
|
|
);
|
|
}
|
|
|
|
let instance_uid = dump_reader.instance_uid()?;
|
|
|
|
// 1. Import the instance-uid.
|
|
if let Some(ref instance_uid) = instance_uid {
|
|
// we don't want to panic if there is an error with the instance-uid.
|
|
let _ = std::fs::write(db_path.join("instance-uid"), instance_uid.to_string().as_bytes());
|
|
};
|
|
|
|
// 2. Import the `Key`s.
|
|
let mut keys = Vec::new();
|
|
auth.raw_delete_all_keys()?;
|
|
for key in dump_reader.keys()? {
|
|
let key = key?;
|
|
auth.raw_insert_key(key.clone())?;
|
|
keys.push(key);
|
|
}
|
|
|
|
let indexer_config = index_scheduler.indexer_config();
|
|
|
|
// /!\ The tasks must be imported AFTER importing the indexes or else the scheduler might
|
|
// try to process tasks while we're trying to import the indexes.
|
|
|
|
// 3. Import the indexes.
|
|
for index_reader in dump_reader.indexes()? {
|
|
let mut index_reader = index_reader?;
|
|
let metadata = index_reader.metadata();
|
|
log::info!("Importing index `{}`.", metadata.uid);
|
|
let index = index_scheduler.create_raw_index(&metadata.uid)?;
|
|
|
|
let mut wtxn = index.write_txn()?;
|
|
|
|
let mut builder = milli::update::Settings::new(&mut wtxn, &index, indexer_config);
|
|
// 3.1 Import the primary key if there is one.
|
|
if let Some(ref primary_key) = metadata.primary_key {
|
|
builder.set_primary_key(primary_key.to_string());
|
|
}
|
|
|
|
// 3.2 Import the settings.
|
|
log::info!("Importing the settings.");
|
|
let settings = index_reader.settings()?;
|
|
apply_settings_to_builder(&settings, &mut builder);
|
|
builder.execute(|indexing_step| log::debug!("update: {:?}", indexing_step), || false)?;
|
|
|
|
// 3.3 Import the documents.
|
|
// 3.3.1 We need to recreate the grenad+obkv format accepted by the index.
|
|
log::info!("Importing the documents.");
|
|
let file = tempfile::tempfile()?;
|
|
let mut builder = DocumentsBatchBuilder::new(BufWriter::new(file));
|
|
for document in index_reader.documents()? {
|
|
builder.append_json_object(&document?)?;
|
|
}
|
|
|
|
// This flush the content of the batch builder.
|
|
let file = builder.into_inner()?.into_inner()?;
|
|
|
|
// 3.3.2 We feed it to the milli index.
|
|
let reader = BufReader::new(file);
|
|
let reader = DocumentsBatchReader::from_reader(reader)?;
|
|
|
|
let builder = milli::update::IndexDocuments::new(
|
|
&mut wtxn,
|
|
&index,
|
|
indexer_config,
|
|
IndexDocumentsConfig {
|
|
update_method: IndexDocumentsMethod::ReplaceDocuments,
|
|
..Default::default()
|
|
},
|
|
|indexing_step| log::debug!("update: {:?}", indexing_step),
|
|
|| false,
|
|
)?;
|
|
|
|
let (builder, user_result) = builder.add_documents(reader)?;
|
|
log::info!("{} documents found.", user_result?);
|
|
builder.execute()?;
|
|
wtxn.commit()?;
|
|
log::info!("All documents successfully imported.");
|
|
}
|
|
|
|
// 4. Import the tasks.
|
|
for ret in dump_reader.tasks()? {
|
|
let (task, file) = ret?;
|
|
index_scheduler.register_dumped_task(task, file)?;
|
|
}
|
|
Ok(())
|
|
}
|
|
|
|
pub fn configure_data(
|
|
config: &mut web::ServiceConfig,
|
|
index_scheduler: Data<IndexScheduler>,
|
|
auth: AuthController,
|
|
opt: &Opt,
|
|
analytics: Arc<dyn Analytics>,
|
|
) {
|
|
let http_payload_size_limit = opt.http_payload_size_limit.get_bytes() as usize;
|
|
config
|
|
.app_data(index_scheduler)
|
|
.app_data(auth)
|
|
.app_data(web::Data::from(analytics))
|
|
.app_data(
|
|
web::JsonConfig::default()
|
|
.content_type(|mime| mime == mime::APPLICATION_JSON)
|
|
.error_handler(|err, req: &HttpRequest| match err {
|
|
JsonPayloadError::ContentType => match req.headers().get(CONTENT_TYPE) {
|
|
Some(content_type) => MeilisearchHttpError::InvalidContentType(
|
|
content_type.to_str().unwrap_or("unknown").to_string(),
|
|
vec![mime::APPLICATION_JSON.to_string()],
|
|
)
|
|
.into(),
|
|
None => MeilisearchHttpError::MissingContentType(vec![
|
|
mime::APPLICATION_JSON.to_string(),
|
|
])
|
|
.into(),
|
|
},
|
|
err => PayloadError::from(err).into(),
|
|
}),
|
|
)
|
|
.app_data(PayloadConfig::new(http_payload_size_limit))
|
|
.app_data(
|
|
web::QueryConfig::default().error_handler(|err, _req| PayloadError::from(err).into()),
|
|
);
|
|
}
|
|
|
|
#[cfg(feature = "mini-dashboard")]
|
|
pub fn dashboard(config: &mut web::ServiceConfig, enable_frontend: bool) {
|
|
use actix_web::HttpResponse;
|
|
use static_files::Resource;
|
|
|
|
mod generated {
|
|
include!(concat!(env!("OUT_DIR"), "/generated.rs"));
|
|
}
|
|
|
|
if enable_frontend {
|
|
let generated = generated::generate();
|
|
// Generate routes for mini-dashboard assets
|
|
for (path, resource) in generated.into_iter() {
|
|
let Resource { mime_type, data, .. } = resource;
|
|
// Redirect index.html to /
|
|
if path == "index.html" {
|
|
config.service(web::resource("/").route(web::get().to(move || async move {
|
|
HttpResponse::Ok().content_type(mime_type).body(data)
|
|
})));
|
|
} else {
|
|
config.service(web::resource(path).route(web::get().to(move || async move {
|
|
HttpResponse::Ok().content_type(mime_type).body(data)
|
|
})));
|
|
}
|
|
}
|
|
} else {
|
|
config.service(web::resource("/").route(web::get().to(routes::running)));
|
|
}
|
|
}
|
|
|
|
#[cfg(not(feature = "mini-dashboard"))]
|
|
pub fn dashboard(config: &mut web::ServiceConfig, _enable_frontend: bool) {
|
|
config.service(web::resource("/").route(web::get().to(routes::running)));
|
|
}
|
|
|
|
#[cfg(feature = "metrics")]
|
|
pub fn configure_metrics_route(config: &mut web::ServiceConfig, enable_metrics_route: bool) {
|
|
if enable_metrics_route {
|
|
config.service(
|
|
web::resource("/metrics").route(web::get().to(crate::route_metrics::get_metrics)),
|
|
);
|
|
}
|
|
}
|