Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

99 changes: 94 additions & 5 deletions apps/hash-graph/src/subcommand/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@ use alloc::sync::Arc;
use core::{
fmt,
net::{AddrParseError, SocketAddr},
str::FromStr as _,
num::NonZero,
str::FromStr,
time::Duration,
};
use std::path::PathBuf;
Expand All @@ -14,7 +15,10 @@ use harpc_codec::json::JsonCodec;
use harpc_server::Server;
use hash_codec::bytes::JsonLinesEncoder;
use hash_graph_api::{
rest::{ApiConfig, QueryLogger, RestApiStore, RestRouterDependencies, rest_api_router},
rest::{
ApiConfig, QueryLogger, RestApiStore, RestRouterDependencies, hashql::CompilerContext,
rest_api_router,
},
rpc::Dependencies,
};
use hash_graph_authorization::policies::store::{PolicyStore, PrincipalStore};
Expand Down Expand Up @@ -103,6 +107,63 @@ pub struct TemporalConfig {
pub address: TemporalAddress,
}

/// A pool size that can be either a concrete count or unbounded.
///
/// Parses positive integers as a bounded size and `0` as unbounded.
Comment thread
indietyp marked this conversation as resolved.
#[derive(Debug, Copy, Clone)]
Comment thread
indietyp marked this conversation as resolved.
pub struct PoolSize(Option<NonZero<usize>>);

impl PoolSize {
#[inline]
const fn get(self) -> Option<NonZero<usize>> {
self.0
}

#[inline]
fn as_usize(self) -> Option<usize> {
self.0.map(NonZero::get)
}
}

impl fmt::Display for PoolSize {
fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
match self.0 {
Some(size) => write!(fmt, "{size}"),
None => write!(fmt, "0"),
}
}
}

impl FromStr for PoolSize {
type Err = <usize as FromStr>::Err;

fn from_str(s: &str) -> Result<Self, Self::Err> {
let value = s.parse::<usize>()?;
Ok(Self(NonZero::new(value)))
}
Comment thread
indietyp marked this conversation as resolved.
}

/// Configuration for the HashQL compiler and execution pool.
#[derive(Debug, Clone, Parser)]
pub struct CompilerConfig {
/// Number of retained heap/scratch instances in the compiler memory pool.
///
/// Set to 0 for an unbounded pool that grows without limit.
#[clap(
long,
default_value = "16",
env = "HASH_GRAPH_COMPILER_MEMORY_POOL_SIZE"
)]
pub compiler_memory_pool_size: PoolSize,

/// Number of threads in the compiler execution pool.
///
/// Each thread runs a `LocalSet` for `!Send` query execution. Set to 0 to use the number
/// of available CPU cores.
#[clap(long, default_value = "0", env = "HASH_GRAPH_COMPILER_EXEC_POOL_SIZE")]
pub compiler_exec_pool_size: PoolSize,
}

/// Configuration for the main graph API server.
///
/// Groups HTTP address, RPC address, temporal client, store behavior, and
Expand Down Expand Up @@ -167,6 +228,9 @@ pub struct ServerConfig {
#[clap(flatten)]
pub api_config: ApiConfig,

#[clap(flatten)]
pub compiler: CompilerConfig,

/// Outputs the queries made to the graph to the specified file.
#[clap(long)]
pub log_queries: Option<PathBuf>,
Expand Down Expand Up @@ -233,7 +297,12 @@ async fn run_rest_server(
async fn create_temporal_client(
config: &TemporalConfig,
) -> Result<Option<TemporalClient>, Report<GraphError>> {
if let Some(host) = &config.address.temporal_host {
if let Some(host) = config
.address
.temporal_host
.as_deref()
.filter(|host| !host.is_empty())
{
TemporalClientConfig::new(
Url::from_str(&format!("{host}:{}", config.address.temporal_port))
.change_context(GraphError)?,
Expand Down Expand Up @@ -314,6 +383,8 @@ where
/// Starts the main graph API server (REST + optional RPC).
async fn start_server<S>(
pool: S,
postgres: PostgresStorePool,
compiler: Arc<CompilerContext>,
config: ServerConfig,
query_logger: Option<QueryLogger>,
lifecycle: &ServerLifecycle,
Expand Down Expand Up @@ -343,10 +414,12 @@ where

let router = rest_api_router(RestRouterDependencies {
store,
domain_regex: DomainValidator::new(config.allowed_url_domain),
postgres,
temporal_client,
domain_regex: DomainValidator::new(config.allowed_url_domain),
query_logger,
api_config: config.api_config,
compiler,
});
start_rest_server(router, config.http_address, lifecycle);

Expand Down Expand Up @@ -405,6 +478,8 @@ pub async fn server(mut args: ServerArgs) -> Result<(), Report<GraphError>> {

let lifecycle = ServerLifecycle::new();

let postgres = pool.clone();

if args.embed_admin {
start_admin_server(pool.clone(), args.admin, &lifecycle);
}
Expand Down Expand Up @@ -441,7 +516,21 @@ pub async fn server(mut args: ServerArgs) -> Result<(), Report<GraphError>> {
None
};

if let Err(error) = start_server(pool, args.config, query_logger, &lifecycle).await {
let compiler = Arc::new(CompilerContext::new(
args.config.compiler.compiler_memory_pool_size.as_usize(),
args.config.compiler.compiler_exec_pool_size.get(),
));

if let Err(error) = start_server(
pool,
postgres,
compiler,
args.config,
query_logger,
&lifecycle,
)
.await
{
lifecycle.shutdown_and_wait().await;
return Err(error);
}
Expand Down
1 change: 1 addition & 0 deletions libs/@local/graph/api/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ hashql-ast = { workspace = true }
hashql-diagnostics = { workspace = true, features = ["serde", "render"] }
hashql-eval = { workspace = true }
hashql-hir = { workspace = true }
hashql-mir = { workspace = true }
hashql-syntax-jexpr = { workspace = true }
type-system = { workspace = true, features = ["utoipa"] }

Expand Down
75 changes: 75 additions & 0 deletions libs/@local/graph/api/openapi/openapi.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions libs/@local/graph/api/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
"@rust/hashql-diagnostics": "workspace:*",
"@rust/hashql-eval": "workspace:*",
"@rust/hashql-hir": "workspace:*",
"@rust/hashql-mir": "workspace:*",
"@rust/hashql-syntax-jexpr": "workspace:*"
}
}
1 change: 1 addition & 0 deletions libs/@local/graph/api/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

// Library Features
error_generic_member_access,
allocator_api
)]

extern crate alloc;
Expand Down
Loading
Loading