Skip to content

Commit ee8b1c8

Browse files
committed
refactor: move grpc handlers to their own files
1 parent 0803d2a commit ee8b1c8

26 files changed

+1562
-1521
lines changed

mgmtd/src/grpc.rs

Lines changed: 45 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ use crate::license::LicensedFeature;
66
use crate::types::{ResolveEntityId, SqliteEnumExt};
77
use anyhow::{Context as AContext, Result, anyhow, bail};
88
use protobuf::{beegfs as pb, management as pm};
9-
use rusqlite::{OptionalExtension, Transaction, TransactionBehavior, params};
9+
use rusqlite::{OptionalExtension, Row, Transaction, TransactionBehavior, named_params, params};
1010
use shared::grpc::*;
1111
use shared::impl_grpc_handler;
1212
use shared::run_state::RunStateHandle;
@@ -20,13 +20,28 @@ use std::pin::Pin;
2020
use tonic::transport::{Identity, Server, ServerTlsConfig};
2121
use tonic::{Code, Request, Response, Status};
2222

23-
mod buddy_group;
24-
mod license;
25-
mod misc;
26-
mod node;
27-
mod pool;
28-
mod quota;
29-
mod target;
23+
mod common;
24+
25+
mod assign_pool;
26+
mod create_buddy_group;
27+
mod create_pool;
28+
mod delete_buddy_group;
29+
mod delete_node;
30+
mod delete_pool;
31+
mod delete_target;
32+
mod get_buddy_groups;
33+
mod get_license;
34+
mod get_nodes;
35+
mod get_pools;
36+
mod get_quota_limits;
37+
mod get_quota_usage;
38+
mod get_targets;
39+
mod mirror_root_inode;
40+
mod set_alias;
41+
mod set_default_quota_limits;
42+
mod set_quota_limits;
43+
mod set_target_state;
44+
mod start_resync;
3045

3146
/// Management gRPC service implementation struct
3247
#[derive(Debug)]
@@ -43,8 +58,9 @@ impl pm::management_server::Management for ManagementService {
4358
// Example: Implement pm::management_server::Management::set_alias using the impl_grpc_handler
4459
// macro
4560
impl_grpc_handler! {
46-
// <the function to implement (as defined by the trait)> => <the actual, custom handler function to call>,
47-
set_alias => misc::set_alias,
61+
// the function to implement (as defined by the trait) as well as the handler to call (must
62+
// be named the same and in a submodule named the same),
63+
set_alias,
4864
// <request message passed to the fn impl (as defined by the trait)> => <response message,
4965
// returned by the fn impl (as defined by the trait)>,
5066
pm::SetAliasRequest => pm::SetAliasResponse,
@@ -53,102 +69,102 @@ impl pm::management_server::Management for ManagementService {
5369
}
5470

5571
impl_grpc_handler! {
56-
get_nodes => node::get,
72+
get_nodes,
5773
pm::GetNodesRequest => pm::GetNodesResponse,
5874
"Get nodes"
5975
}
6076
impl_grpc_handler! {
61-
delete_node => node::delete,
77+
delete_node,
6278
pm::DeleteNodeRequest => pm::DeleteNodeResponse,
6379
"Delete node"
6480
}
6581

6682
impl_grpc_handler! {
67-
get_targets => target::get,
83+
get_targets,
6884
pm::GetTargetsRequest => pm::GetTargetsResponse,
6985
"Get targets"
7086
}
7187
impl_grpc_handler! {
72-
delete_target => target::delete,
88+
delete_target,
7389
pm::DeleteTargetRequest => pm::DeleteTargetResponse,
7490
"Delete target"
7591
}
7692
impl_grpc_handler! {
77-
set_target_state => target::set_state,
93+
set_target_state,
7894
pm::SetTargetStateRequest => pm::SetTargetStateResponse,
7995
"Set target state"
8096
}
8197

8298
impl_grpc_handler! {
83-
get_pools => pool::get,
99+
get_pools,
84100
pm::GetPoolsRequest => pm::GetPoolsResponse,
85101
"Get pools"
86102
}
87103
impl_grpc_handler! {
88-
create_pool => pool::create,
104+
create_pool,
89105
pm::CreatePoolRequest => pm::CreatePoolResponse,
90106
"Create pool"
91107
}
92108
impl_grpc_handler! {
93-
assign_pool => pool::assign,
109+
assign_pool,
94110
pm::AssignPoolRequest => pm::AssignPoolResponse,
95111
"Assign pool"
96112
}
97113
impl_grpc_handler! {
98-
delete_pool => pool::delete,
114+
delete_pool,
99115
pm::DeletePoolRequest => pm::DeletePoolResponse,
100116
"Delete pool"
101117
}
102118

103119
impl_grpc_handler! {
104-
get_buddy_groups => buddy_group::get,
120+
get_buddy_groups,
105121
pm::GetBuddyGroupsRequest => pm::GetBuddyGroupsResponse,
106122
"Get buddy groups"
107123
}
108124
impl_grpc_handler! {
109-
create_buddy_group => buddy_group::create,
125+
create_buddy_group,
110126
pm::CreateBuddyGroupRequest => pm::CreateBuddyGroupResponse,
111127
"Create buddy group"
112128
}
113129
impl_grpc_handler! {
114-
delete_buddy_group => buddy_group::delete,
130+
delete_buddy_group,
115131
pm::DeleteBuddyGroupRequest => pm::DeleteBuddyGroupResponse,
116132
"Delete buddy group"
117133
}
118134
impl_grpc_handler! {
119-
mirror_root_inode => buddy_group::mirror_root_inode,
135+
mirror_root_inode,
120136
pm::MirrorRootInodeRequest => pm::MirrorRootInodeResponse,
121137
"Mirror root inode"
122138
}
123139
impl_grpc_handler! {
124-
start_resync => buddy_group::start_resync,
140+
start_resync,
125141
pm::StartResyncRequest => pm::StartResyncResponse,
126142
"Start resync"
127143
}
128144

129145
impl_grpc_handler! {
130-
set_default_quota_limits => quota::set_default_quota_limits,
146+
set_default_quota_limits,
131147
pm::SetDefaultQuotaLimitsRequest => pm::SetDefaultQuotaLimitsResponse,
132148
"Set default quota limits"
133149
}
134150
impl_grpc_handler! {
135-
set_quota_limits => quota::set_quota_limits,
151+
set_quota_limits,
136152
pm::SetQuotaLimitsRequest => pm::SetQuotaLimitsResponse,
137153
"Set quota limits"
138154
}
139155
impl_grpc_handler! {
140-
get_quota_limits => quota::get_quota_limits,
156+
get_quota_limits,
141157
pm::GetQuotaLimitsRequest => STREAM(GetQuotaLimitsStream, pm::GetQuotaLimitsResponse),
142158
"Get quota limits"
143159
}
144160
impl_grpc_handler! {
145-
get_quota_usage => quota::get_quota_usage,
161+
get_quota_usage,
146162
pm::GetQuotaUsageRequest => STREAM(GetQuotaUsageStream, pm::GetQuotaUsageResponse),
147163
"Get quota usage"
148164
}
149165

150166
impl_grpc_handler! {
151-
get_license => license::get,
167+
get_license,
152168
pm::GetLicenseRequest => pm::GetLicenseResponse,
153169
"Get license"
154170
}

mgmtd/src/grpc/assign_pool.rs

Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
use super::*;
2+
use shared::bee_msg::storage_pool::RefreshStoragePools;
3+
4+
/// Assigns a pool to a list of targets and buddy groups.
5+
pub(crate) async fn assign_pool(
6+
app: &impl AppExt,
7+
req: pm::AssignPoolRequest,
8+
) -> Result<pm::AssignPoolResponse> {
9+
app.fail_on_missing_license(LicensedFeature::Storagepool)?;
10+
app.fail_on_pre_shutdown()?;
11+
12+
let pool: EntityId = required_field(req.pool)?.try_into()?;
13+
14+
let pool = app
15+
.write_tx(move |tx| {
16+
let pool = pool.resolve(tx, EntityType::Pool)?;
17+
do_assign(tx, pool.num_id().try_into()?, req.targets, req.buddy_groups)?;
18+
Ok(pool)
19+
})
20+
.await?;
21+
22+
log::info!("Pool assigned: {pool}");
23+
24+
app.send_notifications(
25+
&[NodeType::Meta, NodeType::Storage],
26+
&RefreshStoragePools { ack_id: "".into() },
27+
)
28+
.await;
29+
30+
Ok(pm::AssignPoolResponse {
31+
pool: Some(pool.into()),
32+
})
33+
}
34+
35+
/// Do the actual assign work
36+
pub(super) fn do_assign(
37+
tx: &Transaction,
38+
pool_id: PoolId,
39+
targets: Vec<pb::EntityIdSet>,
40+
groups: Vec<pb::EntityIdSet>,
41+
) -> Result<()> {
42+
// Target being part of a buddy group can not be assigned individually
43+
let mut check_group_membership = tx.prepare_cached(sql!(
44+
"SELECT COUNT(*) FROM storage_buddy_groups AS g
45+
INNER JOIN targets AS p_t ON p_t.target_id = g.p_target_id AND p_t.node_type = g.node_type
46+
INNER JOIN targets AS s_t ON s_t.target_id = g.s_target_id AND s_t.node_type = g.node_type
47+
WHERE p_t.target_uid = ?1 OR s_t.target_uid = ?1"
48+
))?;
49+
50+
let mut assign_target = tx.prepare_cached(sql!(
51+
"UPDATE targets SET pool_id = ?1 WHERE target_uid = ?2"
52+
))?;
53+
54+
// Do the checks and assign for each target in the given list. This is expensive, but shouldn't
55+
// matter as this command should only be run occasionally and not with very large lists of
56+
// targets.
57+
for t in targets {
58+
let eid = EntityId::try_from(t)?;
59+
let target = eid.resolve(tx, EntityType::Target)?;
60+
if check_group_membership.query_row([target.uid], |row| row.get::<_, i64>(0))? > 0 {
61+
bail!("Target {eid} can't be assigned directly as it's part of a buddy group");
62+
}
63+
64+
assign_target.execute(params![pool_id, target.uid])?;
65+
}
66+
67+
let mut assign_group = tx.prepare_cached(sql!(
68+
"UPDATE buddy_groups SET pool_id = ?1 WHERE group_uid = ?2"
69+
))?;
70+
71+
// Targets being part of buddy groups are auto-assigned to the new pool
72+
let mut assign_grouped_targets = tx.prepare_cached(sql!(
73+
"UPDATE targets SET pool_id = ?1
74+
FROM (
75+
SELECT p_t.target_uid AS p_uid, s_t.target_uid AS s_uid FROM buddy_groups AS g
76+
INNER JOIN targets AS p_t ON p_t.target_id = g.p_target_id AND p_t.node_type = g.node_type
77+
INNER JOIN targets AS s_t ON s_t.target_id = g.s_target_id AND s_t.node_type = g.node_type
78+
WHERE group_uid = ?2
79+
)
80+
WHERE target_uid IN (p_uid, s_uid)"
81+
))?;
82+
83+
// Assign each group and their targets to the new pool
84+
for g in groups {
85+
let eid = EntityId::try_from(g)?;
86+
let group = eid.resolve(tx, EntityType::BuddyGroup)?;
87+
88+
assign_group.execute(params![pool_id, group.uid])?;
89+
assign_grouped_targets.execute(params![pool_id, group.uid])?;
90+
}
91+
92+
Ok(())
93+
}

0 commit comments

Comments
 (0)