2023-10-19 21:45:57 +02:00
|
|
|
use std::sync::{Arc, RwLock};
|
|
|
|
|
2023-06-22 22:56:44 +02:00
|
|
|
use meilisearch_types::features::{InstanceTogglableFeatures, RuntimeTogglableFeatures};
|
|
|
|
use meilisearch_types::heed::types::{SerdeJson, Str};
|
2023-10-19 21:45:57 +02:00
|
|
|
use meilisearch_types::heed::{Database, Env, RwTxn};
|
2023-06-22 22:56:44 +02:00
|
|
|
|
|
|
|
use crate::error::FeatureNotEnabledError;
|
|
|
|
use crate::Result;
|
|
|
|
|
|
|
|
const EXPERIMENTAL_FEATURES: &str = "experimental-features";
|
|
|
|
|
|
|
|
#[derive(Clone)]
|
|
|
|
pub(crate) struct FeatureData {
|
2023-10-19 21:45:57 +02:00
|
|
|
persisted: Database<Str, SerdeJson<RuntimeTogglableFeatures>>,
|
|
|
|
runtime: Arc<RwLock<RuntimeTogglableFeatures>>,
|
2023-06-22 22:56:44 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Debug, Clone, Copy)]
|
|
|
|
pub struct RoFeatures {
|
|
|
|
runtime: RuntimeTogglableFeatures,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl RoFeatures {
|
2023-10-23 10:38:56 +02:00
|
|
|
fn new(data: &FeatureData) -> Self {
|
|
|
|
let runtime = data.runtime_features();
|
|
|
|
Self { runtime }
|
2023-06-22 22:56:44 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn runtime_features(&self) -> RuntimeTogglableFeatures {
|
|
|
|
self.runtime
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn check_metrics(&self) -> Result<()> {
|
2023-10-14 00:12:54 +02:00
|
|
|
if self.runtime.metrics {
|
2023-06-22 22:56:44 +02:00
|
|
|
Ok(())
|
|
|
|
} else {
|
|
|
|
Err(FeatureNotEnabledError {
|
|
|
|
disabled_action: "Getting metrics",
|
|
|
|
feature: "metrics",
|
2023-09-25 14:29:17 +02:00
|
|
|
issue_link: "https://github.com/meilisearch/product/discussions/625",
|
2023-06-22 22:56:44 +02:00
|
|
|
}
|
|
|
|
.into())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-02-05 13:29:01 +01:00
|
|
|
pub fn check_logs_route(&self) -> Result<()> {
|
|
|
|
if self.runtime.logs_route {
|
|
|
|
Ok(())
|
|
|
|
} else {
|
|
|
|
Err(FeatureNotEnabledError {
|
2024-02-12 11:06:37 +01:00
|
|
|
disabled_action: "Modifying logs through the `/logs/*` routes",
|
2024-02-05 14:05:24 +01:00
|
|
|
feature: "logs route",
|
2024-02-07 15:56:47 +01:00
|
|
|
issue_link: "https://github.com/orgs/meilisearch/discussions/721",
|
2024-02-05 13:29:01 +01:00
|
|
|
}
|
|
|
|
.into())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-11-15 15:46:37 +01:00
|
|
|
pub fn check_vector(&self, disabled_action: &'static str) -> Result<()> {
|
2023-06-22 22:56:44 +02:00
|
|
|
if self.runtime.vector_store {
|
|
|
|
Ok(())
|
|
|
|
} else {
|
|
|
|
Err(FeatureNotEnabledError {
|
2023-11-15 15:46:37 +01:00
|
|
|
disabled_action,
|
2023-06-22 22:56:44 +02:00
|
|
|
feature: "vector store",
|
2023-06-26 17:28:07 +02:00
|
|
|
issue_link: "https://github.com/meilisearch/product/discussions/677",
|
2023-06-22 22:56:44 +02:00
|
|
|
}
|
|
|
|
.into())
|
|
|
|
}
|
|
|
|
}
|
2024-07-08 16:46:53 +02:00
|
|
|
|
|
|
|
pub fn check_edit_documents_by_function(&self, disabled_action: &'static str) -> Result<()> {
|
|
|
|
if self.runtime.edit_documents_by_function {
|
|
|
|
Ok(())
|
|
|
|
} else {
|
|
|
|
Err(FeatureNotEnabledError {
|
|
|
|
disabled_action,
|
|
|
|
feature: "edit documents by function",
|
|
|
|
issue_link: "https://github.com/orgs/meilisearch/discussions/762",
|
|
|
|
}
|
|
|
|
.into())
|
|
|
|
}
|
|
|
|
}
|
2024-07-17 11:13:37 +02:00
|
|
|
|
|
|
|
pub fn check_contains_filter(&self) -> Result<()> {
|
|
|
|
if self.runtime.contains_filter {
|
|
|
|
Ok(())
|
|
|
|
} else {
|
|
|
|
Err(FeatureNotEnabledError {
|
|
|
|
disabled_action: "Using `CONTAINS` in a filter",
|
|
|
|
feature: "contains filter",
|
|
|
|
issue_link: "https://github.com/orgs/meilisearch/discussions/763",
|
|
|
|
}
|
|
|
|
.into())
|
|
|
|
}
|
|
|
|
}
|
2023-06-22 22:56:44 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
impl FeatureData {
|
|
|
|
pub fn new(env: &Env, instance_features: InstanceTogglableFeatures) -> Result<Self> {
|
|
|
|
let mut wtxn = env.write_txn()?;
|
2023-10-19 21:45:57 +02:00
|
|
|
let runtime_features_db = env.create_database(&mut wtxn, Some(EXPERIMENTAL_FEATURES))?;
|
2023-06-22 22:56:44 +02:00
|
|
|
wtxn.commit()?;
|
|
|
|
|
2023-10-19 21:45:57 +02:00
|
|
|
let txn = env.read_txn()?;
|
|
|
|
let persisted_features: RuntimeTogglableFeatures =
|
|
|
|
runtime_features_db.get(&txn, EXPERIMENTAL_FEATURES)?.unwrap_or_default();
|
2024-07-17 11:13:37 +02:00
|
|
|
let InstanceTogglableFeatures { metrics, logs_route, contains_filter } = instance_features;
|
2023-10-19 21:45:57 +02:00
|
|
|
let runtime = Arc::new(RwLock::new(RuntimeTogglableFeatures {
|
2024-07-17 11:13:37 +02:00
|
|
|
metrics: metrics || persisted_features.metrics,
|
|
|
|
logs_route: logs_route || persisted_features.logs_route,
|
|
|
|
contains_filter: contains_filter || persisted_features.contains_filter,
|
2023-10-19 21:45:57 +02:00
|
|
|
..persisted_features
|
|
|
|
}));
|
|
|
|
|
|
|
|
Ok(Self { persisted: runtime_features_db, runtime })
|
2023-06-22 22:56:44 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn put_runtime_features(
|
|
|
|
&self,
|
|
|
|
mut wtxn: RwTxn,
|
|
|
|
features: RuntimeTogglableFeatures,
|
|
|
|
) -> Result<()> {
|
2023-10-19 21:45:57 +02:00
|
|
|
self.persisted.put(&mut wtxn, EXPERIMENTAL_FEATURES, &features)?;
|
2023-06-22 22:56:44 +02:00
|
|
|
wtxn.commit()?;
|
2023-10-19 21:45:57 +02:00
|
|
|
|
2023-10-23 10:38:56 +02:00
|
|
|
// safe to unwrap, the lock will only fail if:
|
|
|
|
// 1. requested by the same thread concurrently -> it is called and released in methods that don't call each other
|
|
|
|
// 2. there's a panic while the thread is held -> it is only used for an assignment here.
|
|
|
|
let mut toggled_features = self.runtime.write().unwrap();
|
2023-10-19 21:45:57 +02:00
|
|
|
*toggled_features = features;
|
2023-06-22 22:56:44 +02:00
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2023-10-23 10:38:56 +02:00
|
|
|
fn runtime_features(&self) -> RuntimeTogglableFeatures {
|
|
|
|
// sound to unwrap, the lock will only fail if:
|
|
|
|
// 1. requested by the same thread concurrently -> it is called and released in methods that don't call each other
|
|
|
|
// 2. there's a panic while the thread is held -> it is only used for copying the data here
|
|
|
|
*self.runtime.read().unwrap()
|
2023-06-22 22:56:44 +02:00
|
|
|
}
|
|
|
|
|
2023-10-23 10:38:56 +02:00
|
|
|
pub fn features(&self) -> RoFeatures {
|
2023-10-19 21:45:57 +02:00
|
|
|
RoFeatures::new(self)
|
2023-06-22 22:56:44 +02:00
|
|
|
}
|
|
|
|
}
|