Skip to content

Commit

Permalink
add debug api and view
Browse files Browse the repository at this point in the history
  • Loading branch information
saketh-are committed Nov 3, 2023
1 parent 6b02fa2 commit a38478f
Show file tree
Hide file tree
Showing 10 changed files with 124 additions and 2 deletions.
3 changes: 2 additions & 1 deletion chain/jsonrpc-primitives/src/types/status.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use near_client_primitives::debug::{
#[cfg(feature = "debug_types")]
use near_primitives::views::{
CatchupStatusView, ChainProcessingInfo, NetworkGraphView, NetworkRoutesView, PeerStoreView,
RecentOutboundConnectionsView, RequestedStatePartsView, SyncStatusView,
RecentOutboundConnectionsView, RequestedStatePartsView, SnapshotHostsView, SyncStatusView,
};

#[derive(Debug, serde::Serialize, serde::Deserialize)]
Expand Down Expand Up @@ -33,6 +33,7 @@ pub enum DebugStatusResponse {
NetworkGraph(NetworkGraphView),
RecentOutboundConnections(RecentOutboundConnectionsView),
Routes(NetworkRoutesView),
SnapshotHosts(SnapshotHostsView),
}

#[cfg(feature = "debug_types")]
Expand Down
3 changes: 3 additions & 0 deletions chain/jsonrpc/src/api/status.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,9 @@ impl RpcFrom<near_network::debug::DebugStatus>
near_network::debug::DebugStatus::Routes(x) => {
near_jsonrpc_primitives::types::status::DebugStatusResponse::Routes(x)
}
near_network::debug::DebugStatus::SnapshotHosts(x) => {
near_jsonrpc_primitives::types::status::DebugStatusResponse::SnapshotHosts(x)
}
}
}
}
Expand Down
4 changes: 4 additions & 0 deletions chain/jsonrpc/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -761,6 +761,10 @@ impl JsonRpcHandler {
.peer_manager_send(near_network::debug::GetDebugStatus::Routes)
.await?
.rpc_into(),
"/debug/api/snapshot_hosts" => self
.peer_manager_send(near_network::debug::GetDebugStatus::SnapshotHosts)
.await?
.rpc_into(),
_ => return Ok(None),
};
Ok(Some(near_jsonrpc_primitives::types::status::RpcDebugStatusResponse {
Expand Down
3 changes: 3 additions & 0 deletions chain/network/src/debug.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use ::actix::Message;
use near_primitives::views::{
NetworkGraphView, NetworkRoutesView, PeerStoreView, RecentOutboundConnectionsView,
SnapshotHostsView,
};

// Different debug requests that can be sent by HTML pages, via GET.
Expand All @@ -9,6 +10,7 @@ pub enum GetDebugStatus {
Graph,
RecentOutboundConnections,
Routes,
SnapshotHosts,
}

#[derive(actix::MessageResponse, Debug)]
Expand All @@ -17,6 +19,7 @@ pub enum DebugStatus {
Graph(NetworkGraphView),
RecentOutboundConnections(RecentOutboundConnectionsView),
Routes(NetworkRoutesView),
SnapshotHosts(SnapshotHostsView),
}

impl Message for GetDebugStatus {
Expand Down
16 changes: 15 additions & 1 deletion chain/network/src/peer_manager/peer_manager_actor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ use near_primitives::network::{AnnounceAccount, PeerId};
use near_primitives::types::EpochHeight;
use near_primitives::views::{
ConnectionInfoView, EdgeView, KnownPeerStateView, NetworkGraphView, PeerStoreView,
RecentOutboundConnectionsView,
RecentOutboundConnectionsView, SnapshotHostInfoView, SnapshotHostsView,
};
use rand::seq::IteratorRandom;
use rand::thread_rng;
Expand Down Expand Up @@ -1111,6 +1111,20 @@ impl actix::Handler<GetDebugStatus> for PeerManagerActor {
})
}
GetDebugStatus::Routes => DebugStatus::Routes(self.state.graph_v2.get_debug_view()),
GetDebugStatus::SnapshotHosts => DebugStatus::SnapshotHosts(SnapshotHostsView {
hosts: self
.state
.snapshot_hosts
.get_hosts()
.iter()
.map(|h| SnapshotHostInfoView {
peer_id: h.peer_id.clone(),
sync_hash: h.sync_hash,
epoch_height: h.epoch_height,
shards: h.shards.clone(),
})
.collect::<Vec<_>>(),
}),
}
}
}
14 changes: 14 additions & 0 deletions core/primitives/src/views.rs
Original file line number Diff line number Diff line change
Expand Up @@ -284,6 +284,15 @@ pub struct ConnectionInfoView {
pub time_connected_until: i64,
}

#[cfg_attr(feature = "deepsize_feature", derive(deepsize::DeepSizeOf))]
#[derive(serde::Serialize, serde::Deserialize, Debug, PartialEq, Eq, Clone)]
pub struct SnapshotHostInfoView {
pub peer_id: PeerId,
pub sync_hash: CryptoHash,
pub epoch_height: u64,
pub shards: Vec<u64>,
}

#[cfg_attr(feature = "deepsize_feature", derive(deepsize::DeepSizeOf))]
#[derive(Debug, PartialEq, Eq, Clone)]
pub enum QueryResponseKind {
Expand Down Expand Up @@ -446,6 +455,11 @@ pub struct RecentOutboundConnectionsView {
pub recent_outbound_connections: Vec<ConnectionInfoView>,
}

#[derive(serde::Serialize, serde::Deserialize, Debug, PartialEq, Eq)]
pub struct SnapshotHostsView {
pub hosts: Vec<SnapshotHostInfoView>,
}

#[derive(serde::Serialize, serde::Deserialize, Debug, PartialEq, Eq)]
pub struct EdgeView {
pub peer0: PeerId,
Expand Down
5 changes: 5 additions & 0 deletions tools/debug-ui/src/NetworkInfoView.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { PeerStorageView } from './PeerStorageView';
import { ConnectionStorageView } from './ConnectionStorageView';
import { Tier1View } from './Tier1View';
import { RoutingTableView } from './RoutingTableView';
import { SnapshotHostsView } from './SnapshotHostsView';

type NetworkInfoViewProps = {
addr: string;
Expand All @@ -29,6 +30,9 @@ export const NetworkInfoView = ({ addr }: NetworkInfoViewProps) => {
<NavLink to="routing_table" className={navLinkClassName}>
Routing Table
</NavLink>
<NavLink to="snapshot_hosts" className={navLinkClassName}>
Snapshot Hosts
</NavLink>
</div>
<Routes>
<Route path="" element={<Navigate to="current" />} />
Expand All @@ -37,6 +41,7 @@ export const NetworkInfoView = ({ addr }: NetworkInfoViewProps) => {
<Route path="connection_storage" element={<ConnectionStorageView addr={addr} />} />
<Route path="tier1" element={<Tier1View addr={addr} />} />
<Route path="routing_table" element={<RoutingTableView addr={addr} />} />
<Route path="snapshot_hosts" element={<SnapshotHostsView addr={addr} />} />
</Routes>
</div>
);
Expand Down
3 changes: 3 additions & 0 deletions tools/debug-ui/src/SnapshotHostsView.scss
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
.snapshot-hosts-view {
margin: 10px;
}
51 changes: 51 additions & 0 deletions tools/debug-ui/src/SnapshotHostsView.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
import { useQuery } from 'react-query';
import { toHumanTime } from './utils';
import { fetchSnapshotHosts } from './api';
import './SnapshotHostsView.scss';

type SnapshotHostsViewProps = {
addr: string;
};

export const SnapshotHostsView = ({ addr }: SnapshotHostsViewProps) => {
const {
data: snapshotHosts,
error,
isLoading,
} = useQuery(['snapshotHosts', addr], () => fetchSnapshotHosts(addr));

if (isLoading) {
return <div>Loading...</div>;
} else if (error) {
return <div className="error">{(error as Error).stack}</div>;
}

const snapshot_hosts = snapshotHosts!.status_response.SnapshotHosts;

return (
<div className="snapshot-hosts-view">
<table>
<thead>
<th>Peer ID</th>
<th>Shards</th>
<th>Epoch Height</th>
<th>Sync Hash</th>
</thead>
<tbody>
{snapshot_hosts.hosts.map(
(host) => {
return (
<tr key={host.peer_id}>
<td>{host.peer_id}</td>
<td>{JSON.stringify(host.shards)}</td>
<td>{host.epoch_height}</td>
<td>{host.sync_hash}</td>
</tr>
);
}
)}
</tbody>
</table>
</div>
);
};
24 changes: 24 additions & 0 deletions tools/debug-ui/src/api.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -330,6 +330,23 @@ export interface RoutingTableResponse {
};
}

export interface SnapshotHostInfoView {
peer_id: string,
sync_hash: string,
epoch_height: number,
shards: number[],
}

export interface SnapshotHostsView {
hosts: SnapshotHostInfoView[],
}

export interface SnapshotHostsResponse {
status_response: {
SnapshotHosts: SnapshotHostsView;
};
}

export type DroppedReason = 'HeightProcessed' | 'TooManyProcessingBlocks';

export type BlockProcessingStatus =
Expand Down Expand Up @@ -441,6 +458,13 @@ export async function fetchRoutingTable(
return await response.json();
}

export async function fetchSnapshotHosts(
addr: string
): Promise<SnapshotHostsResponse> {
const response = await fetch(`http://${addr}/debug/api/snapshot_hosts`);
return await response.json();
}

export async function fetchChainProcessingStatus(
addr: string
): Promise<ChainProcessingStatusResponse> {
Expand Down

0 comments on commit a38478f

Please sign in to comment.