Skip to content

Commit

Permalink
Impl FabricClient (#19)
Browse files Browse the repository at this point in the history
Implement safe rust wrapper for fabric client.
Safely wrapped resolve_service_partition and get_node_list.
The API design is modeled after Csharp FabricClient.
Added unit tests for calling these apis with SF onebox and echo app
running locally (and in ci).
#15
  • Loading branch information
youyuanwu authored Apr 22, 2024
1 parent 5a7bf5c commit 4eb498a
Show file tree
Hide file tree
Showing 17 changed files with 823 additions and 31 deletions.
7 changes: 4 additions & 3 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions crates/libs/core/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ tokio = { version = "1", features = ["sync" , "rt-multi-thread", "rt", "macros"]
windows-core = "0.54"
ctrlc = { version = "3.0", features = ["termination"] }
trait-variant = "0.1.1"
bitflags = "2.5.0"

[dev-dependencies]
paste = "1.0"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,11 @@ impl IFabricApplicationManagementClient10Wrap {
pub fn new() -> IFabricApplicationManagementClient10Wrap {
IFabricApplicationManagementClient10Wrap { com : crate :: sync :: CreateLocalClient :: < :: mssf_com :: Microsoft :: ServiceFabric :: FabricCommon :: FabricClient :: IFabricApplicationManagementClient10 > () , }
}
pub fn from_com(
com : :: mssf_com :: Microsoft :: ServiceFabric :: FabricCommon :: FabricClient :: IFabricApplicationManagementClient10,
) -> IFabricApplicationManagementClient10Wrap {
IFabricApplicationManagementClient10Wrap { com }
}
pub fn CreateApplication(
&self,
description: &::mssf_com::Microsoft::ServiceFabric::FABRIC_APPLICATION_DESCRIPTION,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,11 @@ impl Default for IFabricHealthClient4Wrap {
impl IFabricHealthClient4Wrap {
pub fn new() -> IFabricHealthClient4Wrap {
IFabricHealthClient4Wrap { com : crate :: sync :: CreateLocalClient :: < :: mssf_com :: Microsoft :: ServiceFabric :: FabricCommon :: FabricClient :: IFabricHealthClient4 > () , }
}
pub fn from_com(
com: ::mssf_com::Microsoft::ServiceFabric::FabricCommon::FabricClient::IFabricHealthClient4,
) -> IFabricHealthClient4Wrap {
IFabricHealthClient4Wrap { com }
} pub fn GetApplicationHealth (& self , applicationName : & u16 , healthPolicy : & :: mssf_com :: Microsoft :: ServiceFabric :: FABRIC_APPLICATION_HEALTH_POLICY , timeoutMilliseconds : u32) -> crate :: sync :: FabricReceiver < :: windows_core :: Result < :: mssf_com :: Microsoft :: ServiceFabric :: FabricCommon :: FabricClient :: IFabricApplicationHealthResult >>{
let (tx, rx) = crate::sync::oneshot_channel();
let callback = crate::sync::AwaitableCallback2::i_new(move |ctx| {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,11 @@ impl IFabricClusterManagementClient10Wrap {
pub fn new() -> IFabricClusterManagementClient10Wrap {
IFabricClusterManagementClient10Wrap { com : crate :: sync :: CreateLocalClient :: < :: mssf_com :: Microsoft :: ServiceFabric :: FabricCommon :: FabricClient :: IFabricClusterManagementClient10 > () , }
}
pub fn from_com(
com : :: mssf_com :: Microsoft :: ServiceFabric :: FabricCommon :: FabricClient :: IFabricClusterManagementClient10,
) -> IFabricClusterManagementClient10Wrap {
IFabricClusterManagementClient10Wrap { com }
}
pub fn ActivateNode(
&self,
nodeName: ::windows_core::PCWSTR,
Expand Down
7 changes: 7 additions & 0 deletions crates/libs/core/src/client/gen/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
pub mod app;
pub mod health;
pub mod mgmt;
pub mod property;
pub mod query;
pub mod svc;
pub mod svcgp;
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,11 @@ impl IFabricPropertyManagementClient2Wrap {
pub fn new() -> IFabricPropertyManagementClient2Wrap {
IFabricPropertyManagementClient2Wrap { com : crate :: sync :: CreateLocalClient :: < :: mssf_com :: Microsoft :: ServiceFabric :: FabricCommon :: FabricClient :: IFabricPropertyManagementClient2 > () , }
}
pub fn from_com(
com : :: mssf_com :: Microsoft :: ServiceFabric :: FabricCommon :: FabricClient :: IFabricPropertyManagementClient2,
) -> IFabricPropertyManagementClient2Wrap {
IFabricPropertyManagementClient2Wrap { com }
}
pub fn CreateName(
&self,
name: &u16,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,11 @@ impl Default for IFabricQueryClient10Wrap {
impl IFabricQueryClient10Wrap {
pub fn new() -> IFabricQueryClient10Wrap {
IFabricQueryClient10Wrap { com : crate :: sync :: CreateLocalClient :: < :: mssf_com :: Microsoft :: ServiceFabric :: FabricCommon :: FabricClient :: IFabricQueryClient10 > () , }
}
pub fn from_com(
com: ::mssf_com::Microsoft::ServiceFabric::FabricCommon::FabricClient::IFabricQueryClient10,
) -> IFabricQueryClient10Wrap {
IFabricQueryClient10Wrap { com }
} pub fn GetApplicationList (& self , queryDescription : & :: mssf_com :: Microsoft :: ServiceFabric :: FABRIC_APPLICATION_QUERY_DESCRIPTION , timeoutMilliseconds : u32) -> crate :: sync :: FabricReceiver < :: windows_core :: Result < :: mssf_com :: Microsoft :: ServiceFabric :: FabricCommon :: FabricClient :: IFabricGetApplicationListResult >>{
let (tx, rx) = crate::sync::oneshot_channel();
let callback = crate::sync::AwaitableCallback2::i_new(move |ctx| {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,11 @@ impl IFabricServiceManagementClient6Wrap {
pub fn new() -> IFabricServiceManagementClient6Wrap {
IFabricServiceManagementClient6Wrap { com : crate :: sync :: CreateLocalClient :: < :: mssf_com :: Microsoft :: ServiceFabric :: FabricCommon :: FabricClient :: IFabricServiceManagementClient6 > () , }
}
pub fn from_com(
com : :: mssf_com :: Microsoft :: ServiceFabric :: FabricCommon :: FabricClient :: IFabricServiceManagementClient6,
) -> IFabricServiceManagementClient6Wrap {
IFabricServiceManagementClient6Wrap { com }
}
pub fn CreateService(
&self,
description: &::mssf_com::Microsoft::ServiceFabric::FABRIC_SERVICE_DESCRIPTION,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,11 @@ impl IFabricServiceGroupManagementClient4Wrap {
pub fn new() -> IFabricServiceGroupManagementClient4Wrap {
IFabricServiceGroupManagementClient4Wrap { com : crate :: sync :: CreateLocalClient :: < :: mssf_com :: Microsoft :: ServiceFabric :: FabricCommon :: FabricClient :: IFabricServiceGroupManagementClient4 > () , }
}
pub fn from_com(
com : :: mssf_com :: Microsoft :: ServiceFabric :: FabricCommon :: FabricClient :: IFabricServiceGroupManagementClient4,
) -> IFabricServiceGroupManagementClient4Wrap {
IFabricServiceGroupManagementClient4Wrap { com }
}
pub fn CreateServiceGroup(
&self,
description: &::mssf_com::Microsoft::ServiceFabric::FABRIC_SERVICE_GROUP_DESCRIPTION,
Expand Down
88 changes: 81 additions & 7 deletions crates/libs/core/src/client/mod.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,84 @@
pub mod app;
pub mod health;
pub mod mgmt;
pub mod property;
pub mod query;
pub mod svc;
pub mod svcgp;
use mssf_com::FabricCommon::FabricClient::{
IFabricPropertyManagementClient2, IFabricQueryClient10, IFabricServiceManagementClient6,
};
use windows_core::Interface;

use self::{
gen::{property::IFabricPropertyManagementClient2Wrap, query::IFabricQueryClient10Wrap},
query_client::QueryClient,
svc_mgmt_client::ServiceManagementClient,
};

// TODO: make private. Currently exposed for backward compat
pub mod gen;

pub mod query_client;
pub mod svc_mgmt_client;

#[cfg(test)]
mod tests;

// FabricClient safe wrapper
// The design of FabricClient follows from the csharp client:
// https://github.com/microsoft/service-fabric/blob/master/src/prod/src/managed/Api/src/System/Fabric/FabricClient.cs

pub struct FabricClient {
com_property_client: IFabricPropertyManagementClient2,
com_service_client: IFabricServiceManagementClient6,
com_query_client: IFabricQueryClient10,
}

impl Default for FabricClient {
fn default() -> Self {
Self::new()
}
}

impl FabricClient {
pub fn new() -> Self {
let com = crate::sync::CreateLocalClient::<IFabricPropertyManagementClient2>();
Self::from_com(com)
}

// Creates from com directly. This gives the user freedom to create com from
// custom code and pass it in.
// For the final state of FabricClient, this function should be private.
pub fn from_com(com: IFabricPropertyManagementClient2) -> Self {
let com_property_client = com.clone();
let com_service_client = com
.clone()
.cast::<IFabricServiceManagementClient6>()
.unwrap();
let com_query_client = com.clone().cast::<IFabricQueryClient10>().unwrap();
Self {
com_property_client,
com_service_client,
com_query_client,
}
}

// Get the client for managing Fabric Properties in Naming Service
pub fn get_property_manager(&self) -> PropertyManagementClient {
PropertyManagementClient {
_com: self.com_property_client.clone(),
_gen_wrap: IFabricPropertyManagementClient2Wrap::from_com(
self.com_property_client.clone(),
),
}
}

// Get the client for quering SF info.
pub fn get_query_manager(&self) -> QueryClient {
QueryClient::from_com(self.com_query_client.clone())
}

// Get the client for managing service info and lifecycles.
pub fn get_service_manager(&self) -> ServiceManagementClient {
ServiceManagementClient::from_com(self.com_service_client.clone())
}
}

pub struct PropertyManagementClient {
_com: IFabricPropertyManagementClient2,
_gen_wrap: IFabricPropertyManagementClient2Wrap,
}
Loading

0 comments on commit 4eb498a

Please sign in to comment.