2023-10-20 03:45:57 +08:00
|
|
|
use std::sync::{Arc, RwLock};
|
|
|
|
|
2023-06-23 04:56:44 +08:00
|
|
|
use meilisearch_types::features::{InstanceTogglableFeatures, RuntimeTogglableFeatures};
|
|
|
|
use meilisearch_types::heed::types::{SerdeJson, Str};
|
2023-10-20 03:45:57 +08:00
|
|
|
use meilisearch_types::heed::{Database, Env, RwTxn};
|
2023-06-23 04:56:44 +08:00
|
|
|
|
2023-10-20 03:45:57 +08:00
|
|
|
use crate::error::Error::RuntimeFeatureToggleError;
|
2023-06-23 04:56:44 +08:00
|
|
|
use crate::error::FeatureNotEnabledError;
|
|
|
|
use crate::Result;
|
|
|
|
|
|
|
|
const EXPERIMENTAL_FEATURES: &str = "experimental-features";
|
|
|
|
|
|
|
|
#[derive(Clone)]
|
|
|
|
pub(crate) struct FeatureData {
|
2023-10-20 03:45:57 +08:00
|
|
|
persisted: Database<Str, SerdeJson<RuntimeTogglableFeatures>>,
|
|
|
|
runtime: Arc<RwLock<RuntimeTogglableFeatures>>,
|
2023-06-23 04:56:44 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Debug, Clone, Copy)]
|
|
|
|
pub struct RoFeatures {
|
|
|
|
runtime: RuntimeTogglableFeatures,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl RoFeatures {
|
2023-10-20 03:45:57 +08:00
|
|
|
fn new(data: &FeatureData) -> Result<Self> {
|
|
|
|
let runtime = data.runtime_features()?;
|
2023-10-14 06:12:54 +08:00
|
|
|
Ok(Self { runtime })
|
2023-06-23 04:56:44 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn runtime_features(&self) -> RuntimeTogglableFeatures {
|
|
|
|
self.runtime
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn check_score_details(&self) -> Result<()> {
|
|
|
|
if self.runtime.score_details {
|
|
|
|
Ok(())
|
|
|
|
} else {
|
|
|
|
Err(FeatureNotEnabledError {
|
|
|
|
disabled_action: "Computing score details",
|
|
|
|
feature: "score details",
|
|
|
|
issue_link: "https://github.com/meilisearch/product/discussions/674",
|
|
|
|
}
|
|
|
|
.into())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn check_metrics(&self) -> Result<()> {
|
2023-10-14 06:12:54 +08:00
|
|
|
if self.runtime.metrics {
|
2023-06-23 04:56:44 +08:00
|
|
|
Ok(())
|
|
|
|
} else {
|
|
|
|
Err(FeatureNotEnabledError {
|
|
|
|
disabled_action: "Getting metrics",
|
|
|
|
feature: "metrics",
|
2023-09-25 20:29:17 +08:00
|
|
|
issue_link: "https://github.com/meilisearch/product/discussions/625",
|
2023-06-23 04:56:44 +08:00
|
|
|
}
|
|
|
|
.into())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn check_vector(&self) -> Result<()> {
|
|
|
|
if self.runtime.vector_store {
|
|
|
|
Ok(())
|
|
|
|
} else {
|
|
|
|
Err(FeatureNotEnabledError {
|
|
|
|
disabled_action: "Passing `vector` as a query parameter",
|
|
|
|
feature: "vector store",
|
2023-06-26 23:28:07 +08:00
|
|
|
issue_link: "https://github.com/meilisearch/product/discussions/677",
|
2023-06-23 04:56:44 +08:00
|
|
|
}
|
|
|
|
.into())
|
|
|
|
}
|
|
|
|
}
|
2023-09-25 20:28:54 +08:00
|
|
|
|
|
|
|
pub fn check_puffin(&self) -> Result<()> {
|
|
|
|
if self.runtime.export_puffin_reports {
|
|
|
|
Ok(())
|
|
|
|
} else {
|
|
|
|
Err(FeatureNotEnabledError {
|
|
|
|
disabled_action: "Outputting Puffin reports to disk",
|
|
|
|
feature: "export puffin reports",
|
|
|
|
issue_link: "https://github.com/meilisearch/product/discussions/693",
|
|
|
|
}
|
|
|
|
.into())
|
|
|
|
}
|
|
|
|
}
|
2023-06-23 04:56:44 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
impl FeatureData {
|
|
|
|
pub fn new(env: &Env, instance_features: InstanceTogglableFeatures) -> Result<Self> {
|
|
|
|
let mut wtxn = env.write_txn()?;
|
2023-10-20 03:45:57 +08:00
|
|
|
let runtime_features_db = env.create_database(&mut wtxn, Some(EXPERIMENTAL_FEATURES))?;
|
2023-06-23 04:56:44 +08:00
|
|
|
wtxn.commit()?;
|
|
|
|
|
2023-10-20 03:45:57 +08:00
|
|
|
let txn = env.read_txn()?;
|
|
|
|
let persisted_features: RuntimeTogglableFeatures =
|
|
|
|
runtime_features_db.get(&txn, EXPERIMENTAL_FEATURES)?.unwrap_or_default();
|
|
|
|
let runtime = Arc::new(RwLock::new(RuntimeTogglableFeatures {
|
|
|
|
metrics: instance_features.metrics || persisted_features.metrics,
|
|
|
|
..persisted_features
|
|
|
|
}));
|
|
|
|
|
|
|
|
Ok(Self { persisted: runtime_features_db, runtime })
|
2023-06-23 04:56:44 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn put_runtime_features(
|
|
|
|
&self,
|
|
|
|
mut wtxn: RwTxn,
|
|
|
|
features: RuntimeTogglableFeatures,
|
|
|
|
) -> Result<()> {
|
2023-10-20 03:45:57 +08:00
|
|
|
self.persisted.put(&mut wtxn, EXPERIMENTAL_FEATURES, &features)?;
|
2023-06-23 04:56:44 +08:00
|
|
|
wtxn.commit()?;
|
2023-10-20 03:45:57 +08:00
|
|
|
|
|
|
|
let mut toggled_features = self.runtime.write().map_err(|_| RuntimeFeatureToggleError)?;
|
|
|
|
*toggled_features = features;
|
2023-06-23 04:56:44 +08:00
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2023-10-20 03:45:57 +08:00
|
|
|
fn runtime_features(&self) -> Result<RuntimeTogglableFeatures> {
|
|
|
|
Ok(*self.runtime.read().map_err(|_| RuntimeFeatureToggleError)?)
|
2023-06-23 04:56:44 +08:00
|
|
|
}
|
|
|
|
|
2023-10-20 03:45:57 +08:00
|
|
|
pub fn features(&self) -> Result<RoFeatures> {
|
|
|
|
RoFeatures::new(self)
|
2023-06-23 04:56:44 +08:00
|
|
|
}
|
|
|
|
}
|