Skip to content

Commit

Permalink
feat: flow's http server (#5372)
Browse files Browse the repository at this point in the history
* feat: flow's http server

* feat: add cli options for http addr

* test: sqlness runner http addr

* feat: metrics

* chore: also shutdown http server
  • Loading branch information
discord9 authored Jan 16, 2025
1 parent a4761d6 commit 317fe9e
Show file tree
Hide file tree
Showing 6 changed files with 57 additions and 0 deletions.
4 changes: 4 additions & 0 deletions config/config.md
Original file line number Diff line number Diff line change
Expand Up @@ -546,6 +546,10 @@
| `grpc.runtime_size` | Integer | `2` | The number of server worker threads. |
| `grpc.max_recv_message_size` | String | `512MB` | The maximum receive message size for gRPC server. |
| `grpc.max_send_message_size` | String | `512MB` | The maximum send message size for gRPC server. |
| `http` | -- | -- | The HTTP server options. |
| `http.addr` | String | `127.0.0.1:4000` | The address to bind the HTTP server. |
| `http.timeout` | String | `30s` | HTTP request timeout. Set to 0 to disable timeout. |
| `http.body_limit` | String | `64MB` | HTTP request body limit.<br/>The following units are supported: `B`, `KB`, `KiB`, `MB`, `MiB`, `GB`, `GiB`, `TB`, `TiB`, `PB`, `PiB`.<br/>Set to 0 to disable limit. |
| `meta_client` | -- | -- | The metasrv client options. |
| `meta_client.metasrv_addrs` | Array | -- | The addresses of the metasrv. |
| `meta_client.timeout` | String | `3s` | Operation timeout. |
Expand Down
10 changes: 10 additions & 0 deletions config/flownode.example.toml
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,16 @@ max_recv_message_size = "512MB"
## The maximum send message size for gRPC server.
max_send_message_size = "512MB"

## The HTTP server options.
[http]
## The address to bind the HTTP server.
addr = "127.0.0.1:4000"
## HTTP request timeout. Set to 0 to disable timeout.
timeout = "30s"
## HTTP request body limit.
## The following units are supported: `B`, `KB`, `KiB`, `MB`, `MiB`, `GB`, `GiB`, `TB`, `TiB`, `PB`, `PiB`.
## Set to 0 to disable limit.
body_limit = "64MB"

## The metasrv client options.
[meta_client]
Expand Down
14 changes: 14 additions & 0 deletions src/cmd/src/flownode.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
// limitations under the License.

use std::sync::Arc;
use std::time::Duration;

use cache::{build_fundamental_cache_registry, with_default_composite_cache_registry};
use catalog::information_extension::DistributedInformationExtension;
Expand Down Expand Up @@ -142,6 +143,11 @@ struct StartCommand {
/// The prefix of environment variables, default is `GREPTIMEDB_FLOWNODE`;
#[clap(long, default_value = "GREPTIMEDB_FLOWNODE")]
env_prefix: String,
#[clap(long)]
http_addr: Option<String>,
/// HTTP request timeout in seconds.
#[clap(long)]
http_timeout: Option<u64>,
}

impl StartCommand {
Expand Down Expand Up @@ -198,6 +204,14 @@ impl StartCommand {
opts.mode = Mode::Distributed;
}

if let Some(http_addr) = &self.http_addr {
opts.http.addr.clone_from(http_addr);
}

if let Some(http_timeout) = self.http_timeout {
opts.http.timeout = Duration::from_secs(http_timeout);
}

if let (Mode::Distributed, None) = (&opts.mode, &opts.node_id) {
return MissingConfigSnafu {
msg: "Missing node id option",
Expand Down
3 changes: 3 additions & 0 deletions src/flow/src/adapter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ use query::QueryEngine;
use serde::{Deserialize, Serialize};
use servers::grpc::GrpcOptions;
use servers::heartbeat_options::HeartbeatOptions;
use servers::http::HttpOptions;
use servers::Mode;
use session::context::QueryContext;
use snafu::{ensure, OptionExt, ResultExt};
Expand Down Expand Up @@ -106,6 +107,7 @@ pub struct FlownodeOptions {
pub node_id: Option<u64>,
pub flow: FlowConfig,
pub grpc: GrpcOptions,
pub http: HttpOptions,
pub meta_client: Option<MetaClientOptions>,
pub logging: LoggingOptions,
pub tracing: TracingOptions,
Expand All @@ -120,6 +122,7 @@ impl Default for FlownodeOptions {
node_id: None,
flow: FlowConfig::default(),
grpc: GrpcOptions::default().with_addr("127.0.0.1:3004"),
http: HttpOptions::default(),
meta_client: None,
logging: LoggingOptions::default(),
tracing: TracingOptions::default(),
Expand Down
25 changes: 25 additions & 0 deletions src/flow/src/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@ use operator::statement::StatementExecutor;
use partition::manager::PartitionRuleManager;
use query::{QueryEngine, QueryEngineFactory};
use servers::error::{AlreadyStartedSnafu, StartGrpcSnafu, TcpBindSnafu, TcpIncomingSnafu};
use servers::http::{HttpServer, HttpServerBuilder};
use servers::metrics_handler::MetricsHandler;
use servers::server::Server;
use session::context::{QueryContextBuilder, QueryContextRef};
use snafu::{ensure, OptionExt, ResultExt};
Expand Down Expand Up @@ -210,6 +212,9 @@ impl servers::server::Server for FlownodeServer {
pub struct FlownodeInstance {
server: FlownodeServer,
addr: SocketAddr,
/// only used for health check
http_server: HttpServer,
http_addr: SocketAddr,
heartbeat_task: Option<HeartbeatTask>,
}

Expand All @@ -224,6 +229,12 @@ impl FlownodeInstance {
.start(self.addr)
.await
.context(StartServerSnafu)?;

self.http_server
.start(self.http_addr)
.await
.context(StartServerSnafu)?;

Ok(())
}
pub async fn shutdown(&self) -> Result<(), crate::Error> {
Expand All @@ -233,6 +244,11 @@ impl FlownodeInstance {
task.shutdown();
}

self.http_server
.shutdown()
.await
.context(ShutdownServerSnafu)?;

Ok(())
}

Expand Down Expand Up @@ -305,12 +321,21 @@ impl FlownodeBuilder {

let server = FlownodeServer::new(FlowService::new(manager.clone()));

let http_addr = self.opts.http.addr.parse().context(ParseAddrSnafu {
addr: self.opts.http.addr.clone(),
})?;
let http_server = HttpServerBuilder::new(self.opts.http)
.with_metrics_handler(MetricsHandler)
.build();

let heartbeat_task = self.heartbeat_task;

let addr = self.opts.grpc.addr;
let instance = FlownodeInstance {
server,
addr: addr.parse().context(ParseAddrSnafu { addr })?,
http_server,
http_addr,
heartbeat_task,
};
Ok(instance)
Expand Down
1 change: 1 addition & 0 deletions tests/runner/src/env.rs
Original file line number Diff line number Diff line change
Expand Up @@ -490,6 +490,7 @@ impl Env {
sqlness_home.display()
));
args.push("--metasrv-addrs=127.0.0.1:29302".to_string());
args.push(format!("--http-addr=127.0.0.1:2951{id}"));
(args, format!("127.0.0.1:2968{id}"))
}

Expand Down

0 comments on commit 317fe9e

Please sign in to comment.