aboutsummaryrefslogtreecommitdiff
path: root/src/rpc/system.rs
diff options
context:
space:
mode:
authorAlex Auvolat <alex@adnab.me>2021-10-21 00:29:50 +0200
committerAlex Auvolat <alex@adnab.me>2021-10-21 00:29:50 +0200
commit5e986c443f014d96bd0c27113fcf4571f2e1e881 (patch)
tree4dfaf4ae73037a8c5c0264d3f1efe2ff41db700b /src/rpc/system.rs
parent64553479554ba437d7400cc51fe69b0f83d469b7 (diff)
downloadgarage-5e986c443f014d96bd0c27113fcf4571f2e1e881.tar.gz
garage-5e986c443f014d96bd0c27113fcf4571f2e1e881.zip
Discovery via consul
Diffstat (limited to 'src/rpc/system.rs')
-rw-r--r--src/rpc/system.rs34
1 files changed, 32 insertions, 2 deletions
diff --git a/src/rpc/system.rs b/src/rpc/system.rs
index 68edabdf..3a81bc3b 100644
--- a/src/rpc/system.rs
+++ b/src/rpc/system.rs
@@ -28,7 +28,7 @@ use garage_util::error::Error;
use garage_util::persister::Persister;
use garage_util::time::*;
-use crate::consul::get_consul_nodes;
+use crate::consul::*;
use crate::ring::*;
use crate::rpc_helper::*;
@@ -80,6 +80,7 @@ pub struct System {
system_endpoint: Arc<Endpoint<SystemRpc, System>>,
rpc_listen_addr: SocketAddr,
+ rpc_public_addr: Option<SocketAddr>,
bootstrap_peers: Vec<(NodeID, SocketAddr)>,
consul_host: Option<String>,
consul_service_name: Option<String>,
@@ -199,6 +200,7 @@ impl System {
system_endpoint,
replication_factor,
rpc_listen_addr: config.rpc_bind_addr,
+ rpc_public_addr: config.rpc_public_addr,
bootstrap_peers: config.bootstrap_peers.clone(),
consul_host: config.consul_host.clone(),
consul_service_name: config.consul_service_name.clone(),
@@ -224,6 +226,32 @@ impl System {
// ---- INTERNALS ----
+ async fn advertise_to_consul(self: Arc<Self>) -> Result<(), Error> {
+ let (consul_host, consul_service_name) =
+ match (&self.consul_host, &self.consul_service_name) {
+ (Some(ch), Some(csn)) => (ch, csn),
+ _ => return Ok(()),
+ };
+
+ let rpc_public_addr = match self.rpc_public_addr {
+ Some(addr) => addr,
+ None => {
+ warn!("Not advertising to Consul because rpc_public_addr is not defined in config file.");
+ return Ok(());
+ }
+ };
+
+ publish_consul_service(
+ consul_host,
+ consul_service_name,
+ self.netapp.id,
+ &self.local_status.load_full().hostname,
+ rpc_public_addr,
+ )
+ .await
+ .map_err(|e| Error::Message(format!("Error while publishing Consul service: {}", e)))
+ }
+
/// Save network configuration to disc
async fn save_network_config(self: Arc<Self>) -> Result<(), Error> {
let ring: Arc<Ring> = self.ring.borrow().clone();
@@ -375,7 +403,7 @@ impl System {
}
}
- async fn discovery_loop(&self, mut stop_signal: watch::Receiver<bool>) {
+ async fn discovery_loop(self: &Arc<Self>, mut stop_signal: watch::Receiver<bool>) {
let consul_config = match (&self.consul_host, &self.consul_service_name) {
(Some(ch), Some(csn)) => Some((ch.clone(), csn.clone())),
_ => None,
@@ -419,6 +447,8 @@ impl System {
}
}
+ self.background.spawn(self.clone().advertise_to_consul());
+
let restart_at = tokio::time::sleep(DISCOVERY_INTERVAL);
select! {
_ = restart_at.fuse() => {},