-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
collab: Setup database for LLM service (#15882)
This PR puts the initial infrastructure for the LLM service's database in place. The LLM service will be using a separate Postgres database, with its own set of migrations. Currently we only connect to the database in development, as we don't yet have the database setup for the staging/production environments. Release Notes: - N/A
- Loading branch information
1 parent
a649067
commit 7f6d091
Showing
25 changed files
with
627 additions
and
74 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
16 changes: 16 additions & 0 deletions
16
crates/collab/migrations_llm.sqlite/20240806182921_test_schema.sql
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
create table providers ( | ||
id integer primary key autoincrement, | ||
name text not null | ||
); | ||
|
||
create unique index uix_providers_on_name on providers (name); | ||
|
||
create table models ( | ||
id integer primary key autoincrement, | ||
provider_id integer not null references providers (id) on delete cascade, | ||
name text not null | ||
); | ||
|
||
create unique index uix_models_on_provider_id_name on models (provider_id, name); | ||
create index ix_models_on_provider_id on models (provider_id); | ||
create index ix_models_on_name on models (name); |
16 changes: 16 additions & 0 deletions
16
crates/collab/migrations_llm/20240806182921_create_providers_and_models.sql
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
create table if not exists providers ( | ||
id serial primary key, | ||
name text not null | ||
); | ||
|
||
create unique index uix_providers_on_name on providers (name); | ||
|
||
create table if not exists models ( | ||
id serial primary key, | ||
provider_id integer not null references providers (id) on delete cascade, | ||
name text not null | ||
); | ||
|
||
create unique index uix_models_on_provider_id_name on models (provider_id, name); | ||
create index ix_models_on_provider_id on models (provider_id); | ||
create index ix_models_on_name on models (name); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,118 @@ | ||
mod ids; | ||
mod queries; | ||
mod tables; | ||
|
||
#[cfg(test)] | ||
mod tests; | ||
|
||
pub use ids::*; | ||
pub use tables::*; | ||
|
||
#[cfg(test)] | ||
pub use tests::TestLlmDb; | ||
|
||
use std::future::Future; | ||
use std::sync::Arc; | ||
|
||
use anyhow::anyhow; | ||
use sea_orm::prelude::*; | ||
pub use sea_orm::ConnectOptions; | ||
use sea_orm::{ | ||
ActiveValue, DatabaseConnection, DatabaseTransaction, IsolationLevel, TransactionTrait, | ||
}; | ||
|
||
use crate::db::TransactionHandle; | ||
use crate::executor::Executor; | ||
use crate::Result; | ||
|
||
/// The database for the LLM service. | ||
pub struct LlmDatabase { | ||
options: ConnectOptions, | ||
pool: DatabaseConnection, | ||
#[allow(unused)] | ||
executor: Executor, | ||
#[cfg(test)] | ||
runtime: Option<tokio::runtime::Runtime>, | ||
} | ||
|
||
impl LlmDatabase { | ||
/// Connects to the database with the given options | ||
pub async fn new(options: ConnectOptions, executor: Executor) -> Result<Self> { | ||
sqlx::any::install_default_drivers(); | ||
Ok(Self { | ||
options: options.clone(), | ||
pool: sea_orm::Database::connect(options).await?, | ||
executor, | ||
#[cfg(test)] | ||
runtime: None, | ||
}) | ||
} | ||
|
||
pub fn options(&self) -> &ConnectOptions { | ||
&self.options | ||
} | ||
|
||
pub async fn transaction<F, Fut, T>(&self, f: F) -> Result<T> | ||
where | ||
F: Send + Fn(TransactionHandle) -> Fut, | ||
Fut: Send + Future<Output = Result<T>>, | ||
{ | ||
let body = async { | ||
let (tx, result) = self.with_transaction(&f).await?; | ||
match result { | ||
Ok(result) => match tx.commit().await.map_err(Into::into) { | ||
Ok(()) => return Ok(result), | ||
Err(error) => { | ||
return Err(error); | ||
} | ||
}, | ||
Err(error) => { | ||
tx.rollback().await?; | ||
return Err(error); | ||
} | ||
} | ||
}; | ||
|
||
self.run(body).await | ||
} | ||
|
||
async fn with_transaction<F, Fut, T>(&self, f: &F) -> Result<(DatabaseTransaction, Result<T>)> | ||
where | ||
F: Send + Fn(TransactionHandle) -> Fut, | ||
Fut: Send + Future<Output = Result<T>>, | ||
{ | ||
let tx = self | ||
.pool | ||
.begin_with_config(Some(IsolationLevel::ReadCommitted), None) | ||
.await?; | ||
|
||
let mut tx = Arc::new(Some(tx)); | ||
let result = f(TransactionHandle(tx.clone())).await; | ||
let Some(tx) = Arc::get_mut(&mut tx).and_then(|tx| tx.take()) else { | ||
return Err(anyhow!( | ||
"couldn't complete transaction because it's still in use" | ||
))?; | ||
}; | ||
|
||
Ok((tx, result)) | ||
} | ||
|
||
async fn run<F, T>(&self, future: F) -> Result<T> | ||
where | ||
F: Future<Output = Result<T>>, | ||
{ | ||
#[cfg(test)] | ||
{ | ||
if let Executor::Deterministic(executor) = &self.executor { | ||
executor.simulate_random_delay().await; | ||
} | ||
|
||
self.runtime.as_ref().unwrap().block_on(future) | ||
} | ||
|
||
#[cfg(not(test))] | ||
{ | ||
future.await | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
use sea_orm::{entity::prelude::*, DbErr}; | ||
use serde::{Deserialize, Serialize}; | ||
|
||
use crate::id_type; | ||
|
||
id_type!(ProviderId); | ||
id_type!(ModelId); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
use super::*; | ||
|
||
pub mod providers; |
Oops, something went wrong.