Add admin diagnostic query suite for sync state.

Signed-off-by: Jason Volk <jason@zemos.net>
This commit is contained in:
Jason Volk
2025-10-07 08:55:24 +00:00
parent a6127fcd1a
commit 7fee459b1a
4 changed files with 125 additions and 7 deletions

View File

@@ -10,6 +10,7 @@ mod room_state_cache;
mod room_timeline;
mod sending;
mod short;
mod sync;
mod users;
use clap::Subcommand;
@@ -20,7 +21,7 @@ use self::{
presence::PresenceCommand, pusher::PusherCommand, raw::RawCommand, resolver::ResolverCommand,
room_alias::RoomAliasCommand, room_state_cache::RoomStateCacheCommand,
room_timeline::RoomTimelineCommand, sending::SendingCommand, short::ShortCommand,
users::UsersCommand,
sync::SyncCommand, users::UsersCommand,
};
use crate::admin_command_dispatch;
@@ -76,6 +77,10 @@ pub(super) enum QueryCommand {
#[command(subcommand)]
Short(ShortCommand),
/// - sync service
#[command(subcommand)]
Sync(SyncCommand),
/// - raw service
#[command(subcommand)]
Raw(RawCommand),

75
src/admin/query/sync.rs Normal file
View File

@@ -0,0 +1,75 @@
use clap::Subcommand;
use ruma::{OwnedDeviceId, OwnedUserId};
use tuwunel_core::Result;
use tuwunel_service::sync::into_connection_key;
use crate::{admin_command, admin_command_dispatch};
#[admin_command_dispatch]
#[derive(Debug, Subcommand)]
/// Query sync service state
pub(crate) enum SyncCommand {
/// List sliding-sync connections.
ListConnections,
/// Show details of sliding sync connection by ID.
ShowConnection {
user_id: OwnedUserId,
device_id: OwnedDeviceId,
conn_id: Option<String>,
},
/// Drop connections for a user, device, or all.
DropConnections {
user_id: Option<OwnedUserId>,
device_id: Option<OwnedDeviceId>,
conn_id: Option<String>,
},
}
#[admin_command]
pub(super) async fn list_connections(&self) -> Result {
let connections = self.services.sync.list_connections();
for connection_key in connections {
self.write_str(&format!("{connection_key:?}"))
.await?;
}
Ok(())
}
#[admin_command]
pub(super) async fn show_connection(
&self,
user_id: OwnedUserId,
device_id: OwnedDeviceId,
conn_id: Option<String>,
) -> Result {
let key = into_connection_key(user_id, device_id, conn_id);
let cache = self.services.sync.find_connection(&key)?;
let out;
{
let cached = cache.lock()?;
out = format!("{cached:#?}");
};
self.write_str(out.as_str()).await
}
#[admin_command]
pub(super) async fn drop_connections(
&self,
user_id: Option<OwnedUserId>,
device_id: Option<OwnedDeviceId>,
conn_id: Option<String>,
) -> Result {
self.services.sync.clear_connections(
user_id.as_deref(),
device_id.as_deref(),
conn_id.map(Into::into).as_ref(),
);
Ok(())
}