Skip to content

Commit

Permalink
Use workspace dependencies when appropriate. Clean up windows crate u…
Browse files Browse the repository at this point in the history
…sage. (#71)

Code clean up.
  • Loading branch information
youyuanwu authored Sep 27, 2024
1 parent ed020ab commit 923ce46
Show file tree
Hide file tree
Showing 42 changed files with 261 additions and 350 deletions.
5 changes: 0 additions & 5 deletions Cargo.lock

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

12 changes: 11 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,14 @@ members = [
[workspace.dependencies]
tracing = { version = "0.1", features = ["log"] }
tracing-subscriber = { version = "0.3", default-features = false, features = ["fmt"]}
tokio-util = "0.7"
tokio-util = "0.7"
ctrlc = { version = "3.0", features = ["termination"] }
tokio = { version = "1", features = ["full"] }
windows = {version = "0.57", features = ["implement"]}
windows-core = "0.57"
trait-variant = "0.1"
serde = "*"
serde_derive = "*"
# crates in this repo
mssf-com = { path = "./crates/libs/com" }
mssf-core = { path = "./crates/libs/core", default-features = true}
2 changes: 1 addition & 1 deletion crates/libs/core/src/error/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
// Licensed under the MIT License (MIT). See License.txt in the repo root for license information.
// ------------------------------------------------------------

use super::HRESULT;
use crate::HRESULT;
use mssf_com::FabricTypes::{
FABRIC_ERROR_CODE, FABRIC_E_OPERATION_NOT_COMPLETE, FABRIC_E_OPERATION_NOT_SUPPORTED,
};
Expand Down
5 changes: 4 additions & 1 deletion crates/libs/core/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,4 +33,7 @@ pub mod sync;
pub mod types;

// re-export some windows types
pub use windows_core::{Error, Result, GUID, HRESULT, HSTRING, PCWSTR};
pub use windows_core::{w, Error, Interface, Result, GUID, HRESULT, HSTRING, PCWSTR};
// Note cannot re-export windows_core::implement because the macro using it has hard coded mod name.

pub use windows::Win32::Foundation::BOOLEAN;
13 changes: 5 additions & 8 deletions crates/libs/core/src/runtime/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
// Licensed under the MIT License (MIT). See License.txt in the repo root for license information.
// ------------------------------------------------------------

use crate::BOOLEAN;
use crate::{error::FabricErrorCode, HSTRING};
use mssf_com::{
FabricRuntime::IFabricConfigurationPackage,
FabricTypes::{
Expand All @@ -11,8 +13,6 @@ use mssf_com::{
FABRIC_CONFIGURATION_SECTION_LIST,
},
};
use windows::Win32::Foundation::{BOOLEAN, E_POINTER};
use windows_core::HSTRING;

use crate::{
iter::{FabricIter, FabricListAccessor},
Expand Down Expand Up @@ -92,10 +92,7 @@ impl ConfigurationPackage {
HSTRINGWrap::from(raw).into()
}

pub fn get_section(
&self,
section_name: &HSTRING,
) -> windows_core::Result<ConfigurationSection> {
pub fn get_section(&self, section_name: &HSTRING) -> crate::Result<ConfigurationSection> {
let raw = unsafe { self.com.GetSection(section_name) }?;
let raw_ref = unsafe { raw.as_ref() };
match raw_ref {
Expand All @@ -104,15 +101,15 @@ impl ConfigurationPackage {
res.owner = Some(self.com.clone());
Ok(res)
}
None => Err(E_POINTER.into()),
None => Err(FabricErrorCode::ArgumentNull.into()),
}
}

pub fn get_value(
&self,
section_name: &HSTRING,
parameter_name: &HSTRING,
) -> windows_core::Result<(HSTRING, bool)> {
) -> crate::Result<(HSTRING, bool)> {
let mut is_encrypted: BOOLEAN = Default::default();
let raw = unsafe {
self.com.GetValue(
Expand Down
6 changes: 3 additions & 3 deletions crates/libs/core/src/runtime/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
// Licensed under the MIT License (MIT). See License.txt in the repo root for license information.
// ------------------------------------------------------------

use crate::{Error, HRESULT, HSTRING};
use mssf_com::FabricCommon::FabricGetLastErrorMessage;
use windows_core::{Error, HRESULT, HSTRING};

// Fills the error info as string for better debugging.
// SF has separate last error set and get from windows.
Expand All @@ -29,14 +29,14 @@ pub fn fill_fabric_error(e: Error) -> Error {
#[cfg(test)]
#[cfg(windows)] // linux error propagate is not working yet
mod test {
use crate::{Error, HSTRING};
use mssf_com::FabricTypes::FABRIC_E_GATEWAY_NOT_REACHABLE;
use windows_core::{Error, HSTRING};

#[test]
fn test_win_error() {
let s = HSTRING::from("MyError");
let e = Error::new(
windows_core::HRESULT(FABRIC_E_GATEWAY_NOT_REACHABLE.0),
crate::HRESULT(FABRIC_E_GATEWAY_NOT_REACHABLE.0),
s.clone().to_string(),
);
assert_eq!(e.message(), s);
Expand Down
18 changes: 9 additions & 9 deletions crates/libs/core/src/runtime/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,14 @@
// Licensed under the MIT License (MIT). See License.txt in the repo root for license information.
// ------------------------------------------------------------

use crate::{Error, Interface, HSTRING, PCWSTR};
use mssf_com::{
FabricRuntime::{
FabricCreateRuntime, FabricGetActivationContext, IFabricCodePackageActivationContext,
IFabricRuntime,
},
FabricTypes::FABRIC_ENDPOINT_RESOURCE_DESCRIPTION,
};
use windows_core::{Error, Interface, HSTRING, PCWSTR};

#[cfg(feature = "tokio_async")]
use mssf_com::FabricCommon::{IFabricAsyncOperationCallback, IFabricAsyncOperationContext};
Expand Down Expand Up @@ -45,13 +45,13 @@ pub mod store_proxy;
pub mod store_types;

// creates fabric runtime
pub fn create_com_runtime() -> ::windows_core::Result<IFabricRuntime> {
pub fn create_com_runtime() -> crate::Result<IFabricRuntime> {
let rawruntime = unsafe { FabricCreateRuntime(&IFabricRuntime::IID)? };
let runtime = unsafe { IFabricRuntime::from_raw(rawruntime) };
Ok(runtime)
}

pub fn get_com_activation_context() -> ::windows_core::Result<IFabricCodePackageActivationContext> {
pub fn get_com_activation_context() -> crate::Result<IFabricCodePackageActivationContext> {
let raw_activation_ctx =
unsafe { FabricGetActivationContext(&IFabricCodePackageActivationContext::IID)? };

Expand All @@ -62,11 +62,11 @@ pub fn get_com_activation_context() -> ::windows_core::Result<IFabricCodePackage

#[derive(Debug)]
pub struct EndpointResourceDesc {
pub name: ::windows_core::HSTRING,
pub protocol: ::windows_core::HSTRING,
pub r#type: ::windows_core::HSTRING,
pub name: HSTRING,
pub protocol: HSTRING,
pub r#type: HSTRING,
pub port: u32,
pub certificate_name: ::windows_core::HSTRING,
pub certificate_name: HSTRING,
//pub Reserved: *mut ::core::ffi::c_void,
}

Expand Down Expand Up @@ -95,7 +95,7 @@ impl ActivationContext {
pub fn get_endpoint_resource(
&self,
serviceendpointresourcename: &HSTRING,
) -> Result<EndpointResourceDesc, Error> {
) -> crate::Result<EndpointResourceDesc> {
let rs = unsafe {
self.com_impl.GetServiceEndpointResource(PCWSTR::from_raw(
serviceendpointresourcename.as_ptr(),
Expand All @@ -109,7 +109,7 @@ impl ActivationContext {
pub fn get_configuration_package(
&self,
configpackagename: &HSTRING,
) -> windows_core::Result<ConfigurationPackage> {
) -> crate::Result<ConfigurationPackage> {
let c = unsafe { self.com_impl.GetConfigurationPackage(configpackagename) }?;
Ok(ConfigurationPackage::from_com(c))
}
Expand Down
6 changes: 3 additions & 3 deletions crates/libs/core/src/runtime/node_context.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
use std::time::Duration;

use crate::{Interface, HSTRING};
use mssf_com::FabricRuntime::{
FabricBeginGetNodeContext, FabricEndGetNodeContext, FabricGetNodeContext,
IFabricNodeContextResult, IFabricNodeContextResult2,
};
use windows_core::{Interface, HSTRING};

use crate::{
strings::HSTRINGWrap,
Expand Down Expand Up @@ -43,14 +43,14 @@ impl NodeContext {
pub async fn get(
timeout: Duration,
cancellation_token: Option<CancellationToken>,
) -> ::windows_core::Result<Self> {
) -> crate::Result<Self> {
let com = get_com_node_context(timeout.as_millis().try_into().unwrap(), cancellation_token)
.await??;
Ok(Self::from(&com))
}

// Get the node context synchronously
pub fn get_sync() -> ::windows_core::Result<Self> {
pub fn get_sync() -> crate::Result<Self> {
let raw = unsafe { FabricGetNodeContext() }?;
assert!(!raw.is_null());
let com = unsafe { IFabricNodeContextResult::from_raw(raw) };
Expand Down
6 changes: 3 additions & 3 deletions crates/libs/core/src/runtime/runtime_wrapper.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ impl<E> Runtime<E>
where
E: Executor,
{
pub fn create(rt: E) -> ::windows_core::Result<Runtime<E>> {
pub fn create(rt: E) -> crate::Result<Runtime<E>> {
let com = create_com_runtime()?;
Ok(Runtime { com_impl: com, rt })
}
Expand All @@ -30,7 +30,7 @@ where
&self,
servicetypename: &HSTRING,
factory: F,
) -> windows_core::Result<()>
) -> crate::Result<()>
where
F: StatelessServiceFactory + 'static,
{
Expand All @@ -47,7 +47,7 @@ where
&self,
servicetypename: &HSTRING,
factory: impl StatefulServiceFactory + 'static,
) -> windows_core::Result<()> {
) -> crate::Result<()> {
let rt_cp = self.rt.clone();
let bridge: IFabricStatefulServiceFactory =
StatefulServiceFactoryBridge::create(factory, rt_cp).into();
Expand Down
39 changes: 18 additions & 21 deletions crates/libs/core/src/runtime/stateful.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@

// stateful contains rs definition of stateful traits that user needs to implement

use crate::{GUID, HSTRING};
use mssf_com::FabricRuntime::IFabricStatefulServicePartition;
use windows_core::{Error, HSTRING};

use crate::sync::CancellationToken;
use crate::types::{LoadMetric, LoadMetricListRef, ReplicaRole};
Expand All @@ -23,9 +23,9 @@ pub trait StatefulServiceFactory {
servicetypename: &HSTRING,
servicename: &HSTRING,
initializationdata: &[u8],
partitionid: &::windows::core::GUID,
partitionid: &GUID,
replicaid: i64,
) -> Result<impl StatefulServiceReplica, Error>;
) -> crate::Result<impl StatefulServiceReplica>;
}

/// Defines behavior that governs the lifecycle of a replica, such as startup, initialization, role changes, and shutdown.
Expand All @@ -41,7 +41,7 @@ pub trait LocalStatefulServiceReplica: Send + Sync + 'static {
openmode: OpenMode,
partition: &StatefulServicePartition,
cancellation_token: CancellationToken,
) -> windows::core::Result<impl PrimaryReplicator>;
) -> crate::Result<impl PrimaryReplicator>;

/// Changes the role of the service replica to one of the ReplicaRole.
/// Returns the service’s new connection address that is to be associated with the replica via Service Fabric Naming.
Expand All @@ -54,10 +54,10 @@ pub trait LocalStatefulServiceReplica: Send + Sync + 'static {
&self,
newrole: ReplicaRole,
cancellation_token: CancellationToken,
) -> ::windows_core::Result<HSTRING>;
) -> crate::Result<HSTRING>;

/// Closes the service replica gracefully when it is being shut down.
async fn close(&self, cancellation_token: CancellationToken) -> windows::core::Result<()>;
async fn close(&self, cancellation_token: CancellationToken) -> crate::Result<()>;

/// Ungracefully terminates the service replica.
/// Remarks: Network issues resulting in Service Fabric process shutdown
Expand Down Expand Up @@ -95,14 +95,14 @@ impl From<&IFabricStatefulServicePartition> for StatefulServicePartition {
/// TODO: replicator has no public documentation
#[trait_variant::make(Replicator: Send)]
pub trait LocalReplicator: Send + Sync + 'static {
async fn open(&self, cancellation_token: CancellationToken) -> ::windows_core::Result<HSTRING>; // replicator address
async fn close(&self, cancellation_token: CancellationToken) -> ::windows_core::Result<()>;
async fn open(&self, cancellation_token: CancellationToken) -> crate::Result<HSTRING>; // replicator address
async fn close(&self, cancellation_token: CancellationToken) -> crate::Result<()>;
async fn change_role(
&self,
epoch: &Epoch,
role: &ReplicaRole,
cancellation_token: CancellationToken,
) -> ::windows_core::Result<()>;
) -> crate::Result<()>;

/// (TODO: This doc is from IStateProvider but not Replicator.)
/// Indicates to a replica that the configuration of a replica set has changed due to
Expand All @@ -116,9 +116,9 @@ pub trait LocalReplicator: Send + Sync + 'static {
&self,
epoch: &Epoch,
cancellation_token: CancellationToken,
) -> ::windows_core::Result<()>;
fn get_current_progress(&self) -> ::windows_core::Result<i64>;
fn get_catch_up_capability(&self) -> ::windows_core::Result<i64>;
) -> crate::Result<()>;
fn get_current_progress(&self) -> crate::Result<i64>;
fn get_catch_up_capability(&self) -> crate::Result<i64>;
fn abort(&self);
}

Expand All @@ -128,28 +128,25 @@ pub trait LocalPrimaryReplicator: Replicator {
// SF calls this to indicate that possible data loss has occurred (write quorum loss),
// returns is isStateChanged. If true, SF will re-create other secondaries.
// The default SF impl might be a pass through to the state provider.
async fn on_data_loss(
&self,
cancellation_token: CancellationToken,
) -> ::windows_core::Result<u8>;
async fn on_data_loss(&self, cancellation_token: CancellationToken) -> crate::Result<u8>;
fn update_catch_up_replica_set_configuration(
&self,
currentconfiguration: &ReplicaSetConfig,
previousconfiguration: &ReplicaSetConfig,
) -> ::windows_core::Result<()>;
) -> crate::Result<()>;
async fn wait_for_catch_up_quorum(
&self,
catchupmode: ReplicaSetQuarumMode,
cancellation_token: CancellationToken,
) -> ::windows_core::Result<()>;
) -> crate::Result<()>;
fn update_current_replica_set_configuration(
&self,
currentconfiguration: &ReplicaSetConfig,
) -> ::windows_core::Result<()>;
) -> crate::Result<()>;
async fn build_replica(
&self,
replica: &ReplicaInfo,
cancellation_token: CancellationToken,
) -> ::windows_core::Result<()>;
fn remove_replica(&self, replicaid: i64) -> ::windows_core::Result<()>;
) -> crate::Result<()>;
fn remove_replica(&self, replicaid: i64) -> crate::Result<()>;
}
Loading

0 comments on commit 923ce46

Please sign in to comment.