Skip to content

Commit

Permalink
Use stack pointer instead of heap allocation with type conversion
Browse files Browse the repository at this point in the history
  • Loading branch information
OscarTHZhang-msft committed Jan 8, 2025
1 parent 1933227 commit 9ce4da8
Show file tree
Hide file tree
Showing 2 changed files with 158 additions and 1,940 deletions.
163 changes: 156 additions & 7 deletions crates/libs/core/src/client/health_client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,24 @@
// Licensed under the MIT License (MIT). See License.txt in the repo root for license information.
// ------------------------------------------------------------

use mssf_com::FabricClient::IFabricHealthClient4;
use mssf_com::{
FabricClient::IFabricHealthClient4,
FabricTypes::{
FABRIC_APPLICATION_HEALTH_REPORT, FABRIC_CLUSTER_HEALTH_REPORT,
FABRIC_DEPLOYED_APPLICATION_HEALTH_REPORT, FABRIC_DEPLOYED_SERVICE_PACKAGE_HEALTH_REPORT,
FABRIC_HEALTH_INFORMATION, FABRIC_HEALTH_REPORT, FABRIC_HEALTH_REPORT_KIND_APPLICATION,
FABRIC_HEALTH_REPORT_KIND_CLUSTER, FABRIC_HEALTH_REPORT_KIND_DEPLOYED_APPLICATION,
FABRIC_HEALTH_REPORT_KIND_DEPLOYED_SERVICE_PACKAGE, FABRIC_HEALTH_REPORT_KIND_INVALID,
FABRIC_HEALTH_REPORT_KIND_NODE, FABRIC_HEALTH_REPORT_KIND_PARTITION,
FABRIC_HEALTH_REPORT_KIND_SERVICE, FABRIC_HEALTH_REPORT_KIND_STATEFUL_SERVICE_REPLICA,
FABRIC_HEALTH_REPORT_KIND_STATELESS_SERVICE_INSTANCE, FABRIC_NODE_HEALTH_REPORT,
FABRIC_PARTITION_HEALTH_REPORT, FABRIC_SERVICE_HEALTH_REPORT,
FABRIC_STATEFUL_SERVICE_REPLICA_HEALTH_REPORT,
FABRIC_STATELESS_SERVICE_INSTANCE_HEALTH_REPORT, FABRIC_URI,
},
};

use crate::types::{FabricHealthReportWrapper, HealthReport};
use crate::types::HealthReport;

/// Provides functionality to perform health related operations, like report and query health.
/// See C# API [here](https://docs.microsoft.com/en-us/dotnet/api/system.fabric.fabricclient.healthclient?view=azure-dotnet).
Expand All @@ -29,10 +44,144 @@ impl HealthClient {
/// Read more about [connecting to a cluster using the FabricClient APIs](https://learn.microsoft.com/en-us/azure/service-fabric/service-fabric-connect-to-secure-cluster).
/// For more information about health reporting, see [Service Fabric health monitoring](https://learn.microsoft.com/en-us/azure/service-fabric/service-fabric-health-introduction).
pub fn report_health(&self, health_report: &HealthReport) -> windows_core::Result<()> {
// The following call will construct a FabricHealthReportWrapper from the HealthReport.
// It does a few heap memory allocations, but it is necessary to pass the health_report to the COM API.
// The memory allocations are then freed by the FabricHealthReportWrapper when it goes out of scope.
let health_report_wrapper = FabricHealthReportWrapper::from(health_report);
unsafe { self.com.ReportHealth(&health_report_wrapper.inner.unwrap()) }
match health_report {
HealthReport::Invalid => {
let fabric_health_report = FABRIC_HEALTH_REPORT {
Kind: FABRIC_HEALTH_REPORT_KIND_INVALID,
Value: std::ptr::null_mut(),
};
unsafe { self.com.ReportHealth(&fabric_health_report) }
}
HealthReport::StatefulServiceReplica(health_report) => {
let fabric_health_info =
FABRIC_HEALTH_INFORMATION::from(&health_report.health_information);
let fabric_health_report_value = FABRIC_STATEFUL_SERVICE_REPLICA_HEALTH_REPORT {
PartitionId: health_report.partition_id,
ReplicaId: health_report.replica_id,
HealthInformation: &fabric_health_info,
Reserved: std::ptr::null_mut(),
};
let fabric_health_report = FABRIC_HEALTH_REPORT {
Kind: FABRIC_HEALTH_REPORT_KIND_STATEFUL_SERVICE_REPLICA,
Value: &fabric_health_report_value as *const _ as *mut _,
};
unsafe { self.com.ReportHealth(&fabric_health_report) }
}
HealthReport::StatelessServiceInstance(health_report) => {
let fabric_health_info =
FABRIC_HEALTH_INFORMATION::from(&health_report.health_information);
let fabric_health_report_value = FABRIC_STATELESS_SERVICE_INSTANCE_HEALTH_REPORT {
PartitionId: health_report.partition_id,
InstanceId: health_report.instance_id,
HealthInformation: &fabric_health_info,
Reserved: std::ptr::null_mut(),
};
let fabric_health_report = FABRIC_HEALTH_REPORT {
Kind: FABRIC_HEALTH_REPORT_KIND_STATELESS_SERVICE_INSTANCE,
Value: &fabric_health_report_value as *const _ as *mut _,
};
unsafe { self.com.ReportHealth(&fabric_health_report) }
}
HealthReport::Partition(health_report) => {
let fabric_health_info =
FABRIC_HEALTH_INFORMATION::from(&health_report.health_information);
let fabric_health_report_value = FABRIC_PARTITION_HEALTH_REPORT {
PartitionId: health_report.partition_id,
HealthInformation: &fabric_health_info,
Reserved: std::ptr::null_mut(),
};
let fabric_health_report = FABRIC_HEALTH_REPORT {
Kind: FABRIC_HEALTH_REPORT_KIND_PARTITION,
Value: &fabric_health_report_value as *const _ as *mut _,
};
unsafe { self.com.ReportHealth(&fabric_health_report) }
}
HealthReport::Node(health_report) => {
let fabric_health_info =
FABRIC_HEALTH_INFORMATION::from(&health_report.health_information);
let fabric_health_report_value = FABRIC_NODE_HEALTH_REPORT {
NodeName: health_report.node_name.as_pcwstr(),
HealthInformation: &fabric_health_info,
Reserved: std::ptr::null_mut(),
};
let fabric_health_report = FABRIC_HEALTH_REPORT {
Kind: FABRIC_HEALTH_REPORT_KIND_NODE,
Value: &fabric_health_report_value as *const _ as *mut _,
};
unsafe { self.com.ReportHealth(&fabric_health_report) }
}
HealthReport::Service(health_report) => {
let fabric_health_info =
FABRIC_HEALTH_INFORMATION::from(&health_report.health_information);
let fabric_health_report_value = FABRIC_SERVICE_HEALTH_REPORT {
ServiceName: FABRIC_URI(health_report.service_name.as_ptr() as *mut u16),
HealthInformation: &fabric_health_info,
Reserved: std::ptr::null_mut(),
};
let fabric_health_report = FABRIC_HEALTH_REPORT {
Kind: FABRIC_HEALTH_REPORT_KIND_SERVICE,
Value: &fabric_health_report_value as *const _ as *mut _,
};
unsafe { self.com.ReportHealth(&fabric_health_report) }
}
HealthReport::Application(health_report) => {
let fabric_health_info =
FABRIC_HEALTH_INFORMATION::from(&health_report.health_information);
let fabric_health_report_value = FABRIC_APPLICATION_HEALTH_REPORT {
ApplicationName: FABRIC_URI(health_report.application_name.as_ptr() as *mut u16),
HealthInformation: &fabric_health_info,
Reserved: std::ptr::null_mut(),
};
let fabric_health_report = FABRIC_HEALTH_REPORT {
Kind: FABRIC_HEALTH_REPORT_KIND_APPLICATION,
Value: &fabric_health_report_value as *const _ as *mut _,
};
unsafe { self.com.ReportHealth(&fabric_health_report) }
}
HealthReport::DeployedApplication(health_report) => {
let fabric_health_info =
FABRIC_HEALTH_INFORMATION::from(&health_report.health_information);
let fabric_health_report_value = FABRIC_DEPLOYED_APPLICATION_HEALTH_REPORT {
ApplicationName: FABRIC_URI(health_report.application_name.as_ptr() as *mut u16),
NodeName: health_report.node_name.as_pcwstr(),
HealthInformation: &fabric_health_info,
Reserved: std::ptr::null_mut(),
};
let fabric_health_report = FABRIC_HEALTH_REPORT {
Kind: FABRIC_HEALTH_REPORT_KIND_DEPLOYED_APPLICATION,
Value: &fabric_health_report_value as *const _ as *mut _,
};
unsafe { self.com.ReportHealth(&fabric_health_report) }
}
HealthReport::DeployedServicePackage(health_report) => {
let fabric_health_info =
FABRIC_HEALTH_INFORMATION::from(&health_report.health_information);
let fabric_health_report_value = FABRIC_DEPLOYED_SERVICE_PACKAGE_HEALTH_REPORT {
ApplicationName: FABRIC_URI(health_report.application_name.as_ptr() as *mut u16),
ServiceManifestName: health_report.service_manifest_name.as_pcwstr(),
NodeName: health_report.node_name.as_pcwstr(),
HealthInformation: &fabric_health_info,
Reserved: std::ptr::null_mut(),
};
let fabric_health_report = FABRIC_HEALTH_REPORT {
Kind: FABRIC_HEALTH_REPORT_KIND_DEPLOYED_SERVICE_PACKAGE,
Value: &fabric_health_report_value as *const _ as *mut _,
};
unsafe { self.com.ReportHealth(&fabric_health_report) }
}
HealthReport::Cluster(health_report) => {
let fabric_health_info =
FABRIC_HEALTH_INFORMATION::from(&health_report.health_information);
let fabric_health_report_value = FABRIC_CLUSTER_HEALTH_REPORT {
HealthInformation: &fabric_health_info,
Reserved: std::ptr::null_mut(),
};
let fabric_health_report = FABRIC_HEALTH_REPORT {
Kind: FABRIC_HEALTH_REPORT_KIND_CLUSTER,
Value: &fabric_health_report_value as *const _ as *mut _,
};
unsafe { self.com.ReportHealth(&fabric_health_report) }
}
}
}
}
Loading

0 comments on commit 9ce4da8

Please sign in to comment.