Skip to content

Commit

Permalink
feat: auto detecting local IP to use as hostname (#5314)
Browse files Browse the repository at this point in the history
* feat: auto detecting hostname

* chore: update config.md

* chore: bring back hostname

* chore: apply suggestions from CR

* chore: use const

* chore: apply suggesions from CR

* fix: fix sqlness
  • Loading branch information
WenyXu authored Jan 8, 2025
1 parent 52eebfc commit 7f307a4
Show file tree
Hide file tree
Showing 25 changed files with 131 additions and 20 deletions.
2 changes: 2 additions & 0 deletions Cargo.lock

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

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,7 @@ humantime-serde = "1.1"
itertools = "0.10"
jsonb = { git = "https://github.com/databendlabs/jsonb.git", rev = "8c8d2fc294a39f3ff08909d60f718639cfba3875", default-features = false }
lazy_static = "1.4"
local-ip-address = "0.6"
meter-core = { git = "https://github.com/GreptimeTeam/greptime-meter.git", rev = "a10facb353b41460eeb98578868ebf19c2084fac" }
mockall = "0.11.4"
moka = "0.12"
Expand Down
4 changes: 2 additions & 2 deletions config/config.md
Original file line number Diff line number Diff line change
Expand Up @@ -214,7 +214,7 @@
| `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. |
| `grpc` | -- | -- | The gRPC server options. |
| `grpc.addr` | String | `127.0.0.1:4001` | The address to bind the gRPC server. |
| `grpc.hostname` | String | `127.0.0.1` | The hostname advertised to the metasrv,<br/>and used for connections from outside the host |
| `grpc.hostname` | String | `127.0.0.1:4001` | The hostname advertised to the metasrv,<br/>and used for connections from outside the host |
| `grpc.runtime_size` | Integer | `8` | The number of server worker threads. |
| `grpc.tls` | -- | -- | gRPC server TLS options, see `mysql.tls` section. |
| `grpc.tls.mode` | String | `disable` | TLS mode. |
Expand Down Expand Up @@ -378,7 +378,7 @@
| `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. |
| `grpc` | -- | -- | The gRPC server options. |
| `grpc.addr` | String | `127.0.0.1:3001` | The address to bind the gRPC server. |
| `grpc.hostname` | String | `127.0.0.1` | The hostname advertised to the metasrv,<br/>and used for connections from outside the host |
| `grpc.hostname` | String | `127.0.0.1:3001` | The hostname advertised to the metasrv,<br/>and used for connections from outside the host |
| `grpc.runtime_size` | Integer | `8` | 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. |
Expand Down
2 changes: 1 addition & 1 deletion config/datanode.example.toml
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ body_limit = "64MB"
addr = "127.0.0.1:3001"
## The hostname advertised to the metasrv,
## and used for connections from outside the host
hostname = "127.0.0.1"
hostname = "127.0.0.1:3001"
## The number of server worker threads.
runtime_size = 8
## The maximum receive message size for gRPC server.
Expand Down
2 changes: 1 addition & 1 deletion config/frontend.example.toml
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ body_limit = "64MB"
addr = "127.0.0.1:4001"
## The hostname advertised to the metasrv,
## and used for connections from outside the host
hostname = "127.0.0.1"
hostname = "127.0.0.1:4001"
## The number of server worker threads.
runtime_size = 8

Expand Down
3 changes: 2 additions & 1 deletion src/cmd/src/datanode.rs
Original file line number Diff line number Diff line change
Expand Up @@ -276,7 +276,8 @@ impl StartCommand {
info!("Datanode options: {:#?}", opts);

let plugin_opts = opts.plugins;
let opts = opts.component;
let mut opts = opts.component;
opts.grpc.detect_hostname();
let mut plugins = Plugins::new();
plugins::setup_datanode_plugins(&mut plugins, &plugin_opts, &opts)
.await
Expand Down
3 changes: 2 additions & 1 deletion src/cmd/src/flownode.rs
Original file line number Diff line number Diff line change
Expand Up @@ -222,7 +222,8 @@ impl StartCommand {
info!("Flownode start command: {:#?}", self);
info!("Flownode options: {:#?}", opts);

let opts = opts.component;
let mut opts = opts.component;
opts.grpc.detect_hostname();

// TODO(discord9): make it not optionale after cluster id is required
let cluster_id = opts.cluster_id.unwrap_or(0);
Expand Down
3 changes: 2 additions & 1 deletion src/cmd/src/frontend.rs
Original file line number Diff line number Diff line change
Expand Up @@ -268,7 +268,8 @@ impl StartCommand {
info!("Frontend options: {:#?}", opts);

let plugin_opts = opts.plugins;
let opts = opts.component;
let mut opts = opts.component;
opts.grpc.detect_hostname();
let mut plugins = Plugins::new();
plugins::setup_frontend_plugins(&mut plugins, &plugin_opts, &opts)
.await
Expand Down
3 changes: 2 additions & 1 deletion src/cmd/src/metasrv.rs
Original file line number Diff line number Diff line change
Expand Up @@ -272,7 +272,8 @@ impl StartCommand {
info!("Metasrv options: {:#?}", opts);

let plugin_opts = opts.plugins;
let opts = opts.component;
let mut opts = opts.component;
opts.detect_server_addr();
let mut plugins = Plugins::new();
plugins::setup_metasrv_plugins(&mut plugins, &plugin_opts, &opts)
.await
Expand Down
3 changes: 2 additions & 1 deletion src/cmd/src/standalone.rs
Original file line number Diff line number Diff line change
Expand Up @@ -461,7 +461,8 @@ impl StartCommand {

let mut plugins = Plugins::new();
let plugin_opts = opts.plugins;
let opts = opts.component;
let mut opts = opts.component;
opts.grpc.detect_hostname();
let fe_opts = opts.frontend_options();
let dn_opts = opts.datanode_options();

Expand Down
6 changes: 5 additions & 1 deletion src/cmd/tests/load_config_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,9 @@ fn test_load_datanode_example_config() {
remote_write: Some(Default::default()),
..Default::default()
},
grpc: GrpcOptions::default().with_addr("127.0.0.1:3001"),
grpc: GrpcOptions::default()
.with_addr("127.0.0.1:3001")
.with_hostname("127.0.0.1:3001"),
rpc_addr: Some("127.0.0.1:3001".to_string()),
rpc_hostname: Some("127.0.0.1".to_string()),
rpc_runtime_size: Some(8),
Expand Down Expand Up @@ -137,6 +139,7 @@ fn test_load_frontend_example_config() {
remote_write: Some(Default::default()),
..Default::default()
},
grpc: GrpcOptions::default().with_hostname("127.0.0.1:4001"),
..Default::default()
},
..Default::default()
Expand All @@ -154,6 +157,7 @@ fn test_load_metasrv_example_config() {
component: MetasrvOptions {
selector: SelectorType::default(),
data_home: "/tmp/metasrv/".to_string(),
server_addr: "127.0.0.1:3002".to_string(),
logging: LoggingOptions {
dir: "/tmp/greptimedb/logs".to_string(),
level: Some("info".to_string()),
Expand Down
1 change: 1 addition & 0 deletions src/meta-srv/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ humantime.workspace = true
humantime-serde.workspace = true
itertools.workspace = true
lazy_static.workspace = true
local-ip-address.workspace = true
once_cell.workspace = true
parking_lot.workspace = true
prometheus.workspace = true
Expand Down
32 changes: 30 additions & 2 deletions src/meta-srv/src/metasrv.rs
Original file line number Diff line number Diff line change
Expand Up @@ -142,11 +142,14 @@ pub struct MetasrvOptions {
pub backend: BackendImpl,
}

const DEFAULT_METASRV_ADDR_PORT: &str = "3002";

impl Default for MetasrvOptions {
fn default() -> Self {
Self {
bind_addr: "127.0.0.1:3002".to_string(),
server_addr: "127.0.0.1:3002".to_string(),
bind_addr: format!("127.0.0.1:{}", DEFAULT_METASRV_ADDR_PORT),
// If server_addr is not set, the server will use the local ip address as the server address.
server_addr: String::new(),
store_addrs: vec!["127.0.0.1:2379".to_string()],
selector: SelectorType::default(),
use_memory_store: false,
Expand Down Expand Up @@ -184,6 +187,31 @@ impl Configurable for MetasrvOptions {
}
}

impl MetasrvOptions {
/// Detect server address if `auto_server_addr` is true.
pub fn detect_server_addr(&mut self) {
if self.server_addr.is_empty() {
match local_ip_address::local_ip() {
Ok(ip) => {
let detected_addr = format!(
"{}:{}",
ip,
self.bind_addr
.split(':')
.nth(1)
.unwrap_or(DEFAULT_METASRV_ADDR_PORT)
);
info!("Using detected: {} as server address", detected_addr);
self.server_addr = detected_addr;
}
Err(e) => {
error!("Failed to detect local ip address: {}", e);
}
}
}
}
}

pub struct MetasrvInfo {
pub server_addr: String,
}
Expand Down
24 changes: 22 additions & 2 deletions src/meta-srv/src/mocks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,12 +44,32 @@ pub struct MockInfo {
pub async fn mock_with_memstore() -> MockInfo {
let kv_backend = Arc::new(MemoryKvBackend::new());
let in_memory = Arc::new(MemoryKvBackend::new());
mock(Default::default(), kv_backend, None, None, Some(in_memory)).await
mock(
MetasrvOptions {
server_addr: "127.0.0.1:3002".to_string(),
..Default::default()
},
kv_backend,
None,
None,
Some(in_memory),
)
.await
}

pub async fn mock_with_etcdstore(addr: &str) -> MockInfo {
let kv_backend = EtcdStore::with_endpoints([addr], 128).await.unwrap();
mock(Default::default(), kv_backend, None, None, None).await
mock(
MetasrvOptions {
server_addr: "127.0.0.1:3002".to_string(),
..Default::default()
},
kv_backend,
None,
None,
None,
)
.await
}

pub async fn mock(
Expand Down
5 changes: 5 additions & 0 deletions src/meta-srv/src/service/heartbeat.rs
Original file line number Diff line number Diff line change
Expand Up @@ -194,13 +194,18 @@ mod tests {

use super::get_node_id;
use crate::metasrv::builder::MetasrvBuilder;
use crate::metasrv::MetasrvOptions;

#[tokio::test]
async fn test_ask_leader() {
let kv_backend = Arc::new(MemoryKvBackend::new());

let metasrv = MetasrvBuilder::new()
.kv_backend(kv_backend)
.options(MetasrvOptions {
server_addr: "127.0.0.1:3002".to_string(),
..Default::default()
})
.build()
.await
.unwrap();
Expand Down
1 change: 1 addition & 0 deletions src/meta-srv/src/service/store.rs
Original file line number Diff line number Diff line change
Expand Up @@ -306,6 +306,7 @@ mod tests {

#[tokio::test]
async fn test_batch_put() {
common_telemetry::init_default_ut_logging();
let metasrv = new_metasrv().await;

let mut req = BatchPutRequest::default();
Expand Down
1 change: 1 addition & 0 deletions src/servers/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ influxdb_line_protocol = { git = "https://github.com/evenyag/influxdb_iox", bran
itertools.workspace = true
jsonb.workspace = true
lazy_static.workspace = true
local-ip-address.workspace = true
log-query.workspace = true
loki-api = "0.1"
mime_guess = "2.0"
Expand Down
37 changes: 35 additions & 2 deletions src/servers/src/grpc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,11 +65,39 @@ pub struct GrpcOptions {
pub tls: TlsOption,
}

impl GrpcOptions {
/// Detect hostname if `auto_hostname` is true.
pub fn detect_hostname(&mut self) {
if self.hostname.is_empty() {
match local_ip_address::local_ip() {
Ok(ip) => {
let detected_addr = format!(
"{}:{}",
ip,
self.addr
.split(':')
.nth(1)
.unwrap_or(DEFAULT_GRPC_ADDR_PORT)
);
info!("Using detected: {} as server address", detected_addr);
self.hostname = detected_addr;
}
Err(e) => {
error!("Failed to detect local ip address: {}", e);
}
}
}
}
}

const DEFAULT_GRPC_ADDR_PORT: &str = "4001";

impl Default for GrpcOptions {
fn default() -> Self {
Self {
addr: "127.0.0.1:4001".to_string(),
hostname: "127.0.0.1".to_string(),
addr: format!("127.0.0.1:{}", DEFAULT_GRPC_ADDR_PORT),
// If hostname is not set, the server will use the local ip address as the hostname.
hostname: String::new(),
max_recv_message_size: DEFAULT_MAX_GRPC_RECV_MESSAGE_SIZE,
max_send_message_size: DEFAULT_MAX_GRPC_SEND_MESSAGE_SIZE,
runtime_size: 8,
Expand All @@ -83,6 +111,11 @@ impl GrpcOptions {
self.addr = addr.to_string();
self
}

pub fn with_hostname(mut self, hostname: &str) -> Self {
self.hostname = hostname.to_string();
self
}
}

pub struct GrpcServer {
Expand Down
1 change: 1 addition & 0 deletions tests-integration/src/cluster.rs
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,7 @@ impl GreptimeDbClusterBuilder {
max_metadata_value_size: None,
},
wal: self.metasrv_wal_config.clone(),
server_addr: "127.0.0.1:3002".to_string(),
..Default::default()
};

Expand Down
2 changes: 2 additions & 0 deletions tests-integration/src/standalone.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ use frontend::instance::builder::FrontendBuilder;
use frontend::instance::{FrontendInstance, Instance, StandaloneDatanodeManager};
use meta_srv::metasrv::{FLOW_ID_SEQ, TABLE_ID_SEQ};
use query::stats::StatementStatistics;
use servers::grpc::GrpcOptions;
use servers::Mode;
use snafu::ResultExt;

Expand Down Expand Up @@ -291,6 +292,7 @@ impl GreptimeDbStandaloneBuilder {
procedure: procedure_config,
metadata_store: kv_backend_config,
wal: self.metasrv_wal_config.clone().into(),
grpc: GrpcOptions::default().with_hostname("127.0.0.1:4001"),
..StandaloneOptions::default()
};

Expand Down
4 changes: 3 additions & 1 deletion tests-integration/src/test_util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -349,7 +349,9 @@ pub(crate) fn create_datanode_opts(
providers,
store: default_store,
},
grpc: GrpcOptions::default().with_addr(PEER_PLACEHOLDER_ADDR),
grpc: GrpcOptions::default()
.with_addr(PEER_PLACEHOLDER_ADDR)
.with_hostname(PEER_PLACEHOLDER_ADDR),
mode,
wal: wal_config,
..Default::default()
Expand Down
2 changes: 1 addition & 1 deletion tests-integration/tests/http.rs
Original file line number Diff line number Diff line change
Expand Up @@ -850,7 +850,7 @@ is_strict_mode = false
[grpc]
addr = "127.0.0.1:4001"
hostname = "127.0.0.1"
hostname = "127.0.0.1:4001"
max_recv_message_size = "512MiB"
max_send_message_size = "512MiB"
runtime_size = 8
Expand Down
2 changes: 0 additions & 2 deletions tests/conf/datanode-test.toml.template
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
node_id = 1
mode = 'distributed'
require_lease_before_startup = true
rpc_addr = '127.0.0.1:29410'
rpc_hostname = '127.0.0.1'
rpc_runtime_size = 8

[wal]
Expand Down
3 changes: 3 additions & 0 deletions tests/conf/frontend-test.toml.template
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
[grpc]
addr = "127.0.0.1:29401"
hostname = "127.0.0.1:29401"
Loading

0 comments on commit 7f307a4

Please sign in to comment.