mirror of
https://github.com/EasyTier/EasyTier.git
synced 2026-09-01 08:49:16 +00:00
node & server: add log
server & node: log node & server: instrument
This commit is contained in:
@@ -18,6 +18,7 @@ use tokio::sync::{broadcast, Notify};
|
|||||||
use tokio::task::JoinSet;
|
use tokio::task::JoinSet;
|
||||||
use tokio::time::{sleep, sleep_until, Instant};
|
use tokio::time::{sleep, sleep_until, Instant};
|
||||||
use tokio_util::sync::CancellationToken;
|
use tokio_util::sync::CancellationToken;
|
||||||
|
use tracing::instrument;
|
||||||
use uuid::Uuid;
|
use uuid::Uuid;
|
||||||
|
|
||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
@@ -73,7 +74,9 @@ impl DnsNode {
|
|||||||
pub fn start(&self) {
|
pub fn start(&self) {
|
||||||
let this = self.clone();
|
let this = self.clone();
|
||||||
self.runtime.start(None, |token| async move {
|
self.runtime.start(None, |token| async move {
|
||||||
tokio::join!(this.run_election(token.clone()), this.run_node(token));
|
tracing::info!("starting DnsNode");
|
||||||
|
this.elect.notify_one();
|
||||||
|
tokio::join!(this.run_election(token.clone()), this.run(token));
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -81,6 +84,7 @@ impl DnsNode {
|
|||||||
self.runtime.stop().await.unwrap_or(Ok(()))
|
self.runtime.stop().await.unwrap_or(Ok(()))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[instrument(skip_all, name = "DnsNode election loop")]
|
||||||
async fn run_election(&self, token: CancellationToken) {
|
async fn run_election(&self, token: CancellationToken) {
|
||||||
loop {
|
loop {
|
||||||
tokio::select! {
|
tokio::select! {
|
||||||
@@ -93,11 +97,16 @@ impl DnsNode {
|
|||||||
_ = sleep(DNS_SERVER_ELECTION_INTERVAL) => {}
|
_ = sleep(DNS_SERVER_ELECTION_INTERVAL) => {}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
tracing::info!("trying to become DNS server");
|
||||||
|
|
||||||
let mut rpc =
|
let mut rpc =
|
||||||
StandAloneServer::new(TcpTunnelListener::new(DNS_SERVER_RPC_ADDR.clone()));
|
StandAloneServer::new(TcpTunnelListener::new(DNS_SERVER_RPC_ADDR.clone()));
|
||||||
|
|
||||||
if rpc.serve().await.is_err() {
|
if rpc.serve().await.is_err() {
|
||||||
// Another instance already owns the address — that's fine.
|
// Another node already owns the address — that's fine.
|
||||||
|
tracing::info!(
|
||||||
|
"failed to bind RPC server, another node might have won the election"
|
||||||
|
);
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -129,7 +138,8 @@ impl DnsNode {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
async fn run_node(&self, token: CancellationToken) {
|
#[instrument(skip_all, name = "DnsNode main loop")]
|
||||||
|
async fn run(&self, token: CancellationToken) {
|
||||||
let mut rpc = StandAloneClient::new(TcpTunnelConnector::new(DNS_SERVER_RPC_ADDR.clone()));
|
let mut rpc = StandAloneClient::new(TcpTunnelConnector::new(DNS_SERVER_RPC_ADDR.clone()));
|
||||||
let mut heartbeat = HeartbeatRequest {
|
let mut heartbeat = HeartbeatRequest {
|
||||||
id: Some(self.id().into()),
|
id: Some(self.id().into()),
|
||||||
|
|||||||
@@ -34,6 +34,7 @@ use std::io;
|
|||||||
use std::net::{IpAddr, Ipv4Addr, SocketAddr, SocketAddrV4};
|
use std::net::{IpAddr, Ipv4Addr, SocketAddr, SocketAddrV4};
|
||||||
use std::{sync::Arc, time::Duration};
|
use std::{sync::Arc, time::Duration};
|
||||||
use tokio_util::sync::CancellationToken;
|
use tokio_util::sync::CancellationToken;
|
||||||
|
use tracing::{instrument, Instrument};
|
||||||
|
|
||||||
#[derive(Clone)]
|
#[derive(Clone)]
|
||||||
pub struct DynamicCatalog {
|
pub struct DynamicCatalog {
|
||||||
@@ -187,11 +188,14 @@ impl DnsServer {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
runtime.start(Some(server.shutdown_token().clone()), |_| async move {
|
runtime.start(Some(server.shutdown_token().clone()), |_| {
|
||||||
server
|
async move {
|
||||||
.block_until_done()
|
server
|
||||||
.await
|
.block_until_done()
|
||||||
.unwrap_or_else(|e| tracing::error!("DNS server exited with error: {:?}", e));
|
.await
|
||||||
|
.unwrap_or_else(|e| tracing::error!("DNS server exited with error: {:?}", e));
|
||||||
|
}
|
||||||
|
.instrument(tracing::info_span!("DNS server backend runtime"))
|
||||||
});
|
});
|
||||||
|
|
||||||
Ok(())
|
Ok(())
|
||||||
@@ -241,6 +245,7 @@ impl DnsServer {
|
|||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[instrument(skip_all, name = "DnsServer main loop")]
|
||||||
pub async fn run(&self, token: CancellationToken) {
|
pub async fn run(&self, token: CancellationToken) {
|
||||||
let dirty = &self.mgr.dirty;
|
let dirty = &self.mgr.dirty;
|
||||||
let mut runtime = None;
|
let mut runtime = None;
|
||||||
@@ -412,6 +417,8 @@ impl DnsServer {
|
|||||||
return None;
|
return None;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
tracing::warn!("HIJACKING PACKET");
|
||||||
|
|
||||||
let response_payload = {
|
let response_payload = {
|
||||||
let response = ResponseHandle::new(512);
|
let response = ResponseHandle::new(512);
|
||||||
|
|
||||||
@@ -526,7 +533,12 @@ mod tests {
|
|||||||
async fn create_test_server() -> Arc<DnsServer> {
|
async fn create_test_server() -> Arc<DnsServer> {
|
||||||
let peer_mgr = create_mock_peer_manager().await;
|
let peer_mgr = create_mock_peer_manager().await;
|
||||||
let global_ctx = peer_mgr.get_global_ctx();
|
let global_ctx = peer_mgr.get_global_ctx();
|
||||||
Arc::new(DnsServer::new(peer_mgr, global_ctx, #[cfg(feature = "tun")] ArcNicCtx::default()))
|
Arc::new(DnsServer::new(
|
||||||
|
peer_mgr,
|
||||||
|
global_ctx,
|
||||||
|
#[cfg(feature = "tun")]
|
||||||
|
ArcNicCtx::default(),
|
||||||
|
))
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Build a raw IPv4 packet (as `Vec<u8>`) carrying the given L4 payload bytes.
|
/// Build a raw IPv4 packet (as `Vec<u8>`) carrying the given L4 payload bytes.
|
||||||
|
|||||||
Reference in New Issue
Block a user