Skip to content
Draft
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
2 changes: 1 addition & 1 deletion Cargo.lock

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

2 changes: 2 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -249,6 +249,8 @@ smallvec = { version = "2.0.0-alpha.11", default-featu
smol_str = { version = "0.3.4" }
sort-package-json = { version = "0.0.12" }
specta = { version = "2.0.0-rc.22", default-features = false }
sqruff-lib = { version = "0.37.3" }
sqruff-lib-core = { version = "0.37.3" }
stacker = { version = "0.1.22", default-features = false }
supports-color = { version = "3.0.2", default-features = false }
supports-unicode = { version = "3.0.0", default-features = false }
Expand Down
108 changes: 103 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,7 @@ use alloc::sync::Arc;
use core::{
fmt,
net::{AddrParseError, SocketAddr},
str::FromStr as _,
str::FromStr,
time::Duration,
};
use std::path::PathBuf;
Expand All @@ -14,7 +14,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 +106,73 @@ 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 `-1` as unbounded.
#[derive(Debug, Copy, Clone)]
Comment on lines +109 to +112
pub struct PoolSize(Option<usize>);

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

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, "-1"),
}
}
}

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

#[expect(
clippy::cast_sign_loss,
clippy::cast_possible_truncation,
reason = "negative values produce None, and pool sizes never approach u32::MAX"
)]
fn from_str(s: &str) -> Result<Self, Self::Err> {
let value = s.parse::<i64>()?;
if value < 0 {
Ok(Self(None))
} else {
Ok(Self(Some(value as usize)))
}
}
}

/// Configuration for the HashQL compiler and execution pool.
#[derive(Debug, Clone, Parser)]
pub struct CompilerConfig {
/// Number of pre-allocated heap/scratch instances in the compiler memory pool.
///
/// Set to -1 for an unbounded pool that grows without limit.
#[clap(
long,
default_value = "16",
env = "HASH_GRAPH_COMPILER_MEMORY_POOL_SIZE",
allow_hyphen_values = true
)]
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 -1 to use the number
/// of available CPU cores.
#[clap(
long,
default_value = "-1",
env = "HASH_GRAPH_COMPILER_EXEC_POOL_SIZE",
allow_hyphen_values = true
)]
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 +237,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 +306,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 +392,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 +423,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 +487,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 +525,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.get(),
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
3 changes: 2 additions & 1 deletion libs/@local/graph/api/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,9 @@ hash-graph-type-defs = { workspace = true }
hash-graph-validation = { workspace = true }
hashql-ast = { workspace = true }
hashql-diagnostics = { workspace = true, features = ["serde", "render"] }
hashql-eval = { workspace = true, features = ["graph"] }
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
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