1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422
#![allow(dead_code)]
use crate::{
data::Data,
model::{
algorithms::{algorithm::Algorithm, algorithm_entry_point::AlgorithmEntryPoint},
App,
},
observability::tracing::create_tracer_from_env,
routes::{graphql_playground, health},
};
use async_graphql_poem::GraphQL;
use itertools::Itertools;
use poem::{
get,
listener::TcpListener,
middleware::{CookieJarManager, CookieJarManagerEndpoint, Cors, CorsEndpoint},
EndpointExt, Route, Server,
};
use raphtory::{
db::api::view::{DynamicGraph, IntoDynamic},
vectors::{
document_template::{DefaultTemplate, DocumentTemplate},
vectorisable::Vectorisable,
EmbeddingFunction,
},
};
use crate::server_config::{load_config, AppConfig, LoggingConfig};
use config::ConfigError;
use std::{
fs,
path::{Path, PathBuf},
sync::Arc,
};
use thiserror::Error;
use tokio::{
io,
io::Result as IoResult,
signal,
sync::{
mpsc,
mpsc::{Receiver, Sender},
},
task::JoinHandle,
};
use tracing_subscriber::{
layer::SubscriberExt, util::SubscriberInitExt, EnvFilter, FmtSubscriber, Registry,
};
use url::ParseError;
#[derive(Error, Debug)]
pub enum ServerError {
#[error("Config error: {0}")]
ConfigError(#[from] ConfigError),
#[error("Cache error: {0}")]
CacheError(String),
#[error("No client id provided")]
MissingClientId,
#[error("No client secret provided")]
MissingClientSecret,
#[error("No tenant id provided")]
MissingTenantId,
#[error("Parse error: {0}")]
FailedToParseUrl(#[from] ParseError),
#[error("Failed to fetch JWKS")]
FailedToFetchJWKS,
}
impl From<ServerError> for io::Error {
fn from(error: ServerError) -> Self {
io::Error::new(io::ErrorKind::Other, error)
}
}
/// A struct for defining and running a Raphtory GraphQL server
pub struct GraphServer {
data: Data,
configs: AppConfig,
}
impl GraphServer {
pub fn new(
work_dir: PathBuf,
app_config: Option<AppConfig>,
config_path: Option<PathBuf>,
) -> IoResult<Self> {
if !work_dir.exists() {
fs::create_dir_all(&work_dir).unwrap();
}
let configs =
load_config(app_config, config_path).map_err(|err| ServerError::ConfigError(err))?;
let data = Data::new(work_dir.as_path(), &configs);
Ok(Self { data, configs })
}
/// Vectorise a subset of the graphs of the server.
///
/// Arguments:
/// * `graph_names` - the names of the graphs to vectorise. All if None is provided.
/// * `embedding` - the embedding function to translate documents to embeddings.
/// * `cache` - the directory to use as cache for the embeddings.
/// * `template` - the template to use for creating documents.
///
/// Returns:
/// A new server object containing the vectorised graphs.
pub async fn with_vectorised<F, T>(
self,
graph_names: Option<Vec<String>>,
embedding: F,
cache: &Path,
template: Option<T>,
) -> IoResult<Self>
where
F: EmbeddingFunction + Clone + 'static,
T: DocumentTemplate<DynamicGraph> + 'static,
{
let graphs = &self.data.global_plugins.graphs;
let stores = &self.data.global_plugins.vectorised_graphs;
graphs.write().extend(
self.data
.get_graphs_from_work_dir()
.map_err(|err| ServerError::CacheError(err.to_string()))?,
);
let template = template
.map(|template| Arc::new(template) as Arc<dyn DocumentTemplate<DynamicGraph>>)
.unwrap_or(Arc::new(DefaultTemplate));
let graph_names = graph_names.unwrap_or_else(|| {
let all_graph_names = {
let read_guard = graphs.read();
read_guard
.iter()
.map(|(graph_name, _)| graph_name.to_string())
.collect_vec()
};
all_graph_names
});
for graph_name in graph_names {
let graph_cache = cache.join(&graph_name);
let graph = graphs.read().get(&graph_name).unwrap().clone();
println!("Loading embeddings for {graph_name} using cache from {graph_cache:?}");
let vectorised = graph
.into_dynamic()
.vectorise_with_template(
Box::new(embedding.clone()),
Some(graph_cache),
true,
template.clone(),
true,
)
.await;
stores.write().insert(graph_name, vectorised);
}
println!("Embeddings were loaded successfully");
Ok(self)
}
pub fn register_algorithm<
'a,
E: AlgorithmEntryPoint<'a> + 'static,
A: Algorithm<'a, E> + 'static,
>(
self,
name: &str,
) -> Self {
E::lock_plugins().insert(name.to_string(), Box::new(A::register_algo));
self
}
/// Start the server on the default port and return a handle to it.
pub async fn start(self) -> IoResult<RunningGraphServer> {
self.start_with_port(1736).await
}
/// Start the server on the port `port` and return a handle to it.
pub async fn start_with_port(self, port: u16) -> IoResult<RunningGraphServer> {
fn configure_logger(configs: &LoggingConfig) {
let log_level = &configs.log_level;
let filter = EnvFilter::new(log_level);
let subscriber = FmtSubscriber::builder().with_env_filter(filter).finish();
if let Err(err) = tracing::subscriber::set_global_default(subscriber) {
eprintln!(
"Log level cannot be updated within the same runtime environment: {}",
err
);
}
}
configure_logger(&self.configs.logging);
let registry = Registry::default().with(tracing_subscriber::fmt::layer().pretty());
let env_filter = EnvFilter::try_from_default_env().unwrap_or(EnvFilter::new("INFO"));
match create_tracer_from_env() {
Some(tracer) => registry
.with(tracing_opentelemetry::layer().with_tracer(tracer))
.with(env_filter)
.try_init(),
None => registry.with(env_filter).try_init(),
}
.unwrap_or(());
// it is important that this runs after algorithms have been pushed to PLUGIN_ALGOS static variable
let app: CorsEndpoint<CookieJarManagerEndpoint<Route>> = self.generate_endpoint().await?;
let (signal_sender, signal_receiver) = mpsc::channel(1);
println!("Playground: http://localhost:{port}");
let server_task = Server::new(TcpListener::bind(format!("0.0.0.0:{port}")))
.run_with_graceful_shutdown(app, server_termination(signal_receiver), None);
let server_result = tokio::spawn(server_task);
Ok(RunningGraphServer {
signal_sender,
server_result,
})
}
async fn generate_endpoint(self) -> IoResult<CorsEndpoint<CookieJarManagerEndpoint<Route>>> {
let schema_builder = App::create_schema();
let schema_builder = schema_builder.data(self.data);
let schema = schema_builder.finish().unwrap();
let app = Route::new()
.at("/", get(graphql_playground).post(GraphQL::new(schema)))
.at("/health", get(health))
.with(CookieJarManager::new())
.with(Cors::new());
Ok(app)
}
// async fn generate_microsoft_endpoint_with_auth(
// self,
// enable_tracing: bool,
// port: u16,
// ) -> IoResult<CorsEndpoint<CookieJarManagerEndpoint<Route>>> {
// let schema_builder = App::create_schema();
// let schema_builder = schema_builder.data(self.data);
// let schema = if enable_tracing {
// let schema_builder = schema_builder.extension(ApolloTracing);
// schema_builder.finish().unwrap()
// } else {
// schema_builder.finish().unwrap()
// };
//
// dotenv().ok();
// let client_id = self
// .configs
// .auth
// .client_id
// .ok_or(ServerError::MissingClientId)?;
// let client_secret = self
// .configs
// .auth
// .client_secret
// .ok_or(ServerError::MissingClientSecret)?;
// let tenant_id = self
// .configs
// .auth
// .tenant_id
// .ok_or(ServerError::MissingTenantId)?;
//
// let client_id = ClientId::new(client_id);
// let client_secret = ClientSecret::new(client_secret);
//
// let auth_url = AuthUrl::new(format!(
// "https://login.microsoftonline.com/{}/oauth2/v2.0/authorize",
// tenant_id.clone()
// ))
// .map_err(|e| ServerError::FailedToParseUrl(e))?;
// let token_url = TokenUrl::new(format!(
// "https://login.microsoftonline.com/{}/oauth2/v2.0/token",
// tenant_id.clone()
// ))
// .map_err(|e| ServerError::FailedToParseUrl(e))?;
//
// println!("Loading client");
// let client = BasicClient::new(
// client_id.clone(),
// Some(client_secret.clone()),
// auth_url,
// Some(token_url),
// )
// .set_redirect_uri(
// RedirectUrl::new(format!(
// "http://localhost:{}/auth/callback",
// port.to_string()
// ))
// .map_err(|e| ServerError::FailedToParseUrl(e))?,
// );
//
// println!("Fetching JWKS");
// let jwks = get_jwks()
// .await
// .map_err(|_| ServerError::FailedToFetchJWKS)?;
//
// let app_state = AppState {
// oauth_client: Arc::new(client),
// csrf_state: Arc::new(Mutex::new(HashMap::new())),
// pkce_verifier: Arc::new(Mutex::new(HashMap::new())),
// jwks: Arc::new(jwks),
// };
//
// let token_middleware = TokenMiddleware::new(Arc::new(app_state.clone()));
//
// println!("Making app");
// let app = Route::new()
// .at(
// "/",
// get(graphql_playground)
// .post(GraphQL::new(schema))
// .with(token_middleware.clone()),
// )
// .at("/health", get(health))
// .at("/login", login.data(app_state.clone()))
// .at("/auth/callback", auth_callback.data(app_state.clone()))
// .at(
// "/verify",
// verify
// .data(app_state.clone())
// .with(token_middleware.clone()),
// )
// .at("/logout", logout.with(token_middleware.clone()))
// .with(CookieJarManager::new())
// .with(Cors::new());
// println!("App done");
// Ok(app)
// }
/// Run the server on the default port until completion.
pub async fn run(self) -> IoResult<()> {
self.start().await?.wait().await
}
/// Run the server on the port `port` until completion.
pub async fn run_with_port(self, port: u16) -> IoResult<()> {
self.start_with_port(port).await?.wait().await
}
}
/// A Raphtory server handler
pub struct RunningGraphServer {
signal_sender: Sender<()>,
server_result: JoinHandle<IoResult<()>>,
}
impl RunningGraphServer {
/// Stop the server.
pub async fn stop(&self) {
let _ignored = self.signal_sender.send(()).await;
}
/// Wait until server completion.
pub async fn wait(self) -> IoResult<()> {
self.server_result
.await
.expect("Coudln't join tokio task for the server")
}
// TODO: make this optional with some python feature flag
pub fn _get_sender(&self) -> &Sender<()> {
&self.signal_sender
}
}
async fn server_termination(mut internal_signal: Receiver<()>) {
let ctrl_c = async {
signal::ctrl_c()
.await
.expect("failed to install Ctrl+C handler");
};
#[cfg(unix)]
let terminate = async {
signal::unix::signal(signal::unix::SignalKind::terminate())
.expect("failed to install signal handler")
.recv()
.await;
};
#[cfg(not(unix))]
let terminate = std::future::pending::<()>();
let internal_terminate = async {
internal_signal.recv().await;
};
tokio::select! {
_ = ctrl_c => {},
_ = terminate => {},
_ = internal_terminate => {},
}
opentelemetry::global::shutdown_tracer_provider();
}
#[cfg(test)]
mod server_tests {
extern crate chrono;
use crate::server::GraphServer;
use chrono::prelude::*;
use tokio::time::{sleep, Duration};
#[tokio::test]
async fn test_server_start_stop() {
let tmp_dir = tempfile::tempdir().unwrap();
let server = GraphServer::new(tmp_dir.path().to_path_buf(), None, None).unwrap();
println!("Calling start at time {}", Local::now());
let handler = server.start_with_port(0);
sleep(Duration::from_secs(1)).await;
println!("Calling stop at time {}", Local::now());
handler.await.unwrap().stop().await
}
}