mssf_core/runtime/
stateless_proxy.rs

1// ------------------------------------------------------------
2// Copyright (c) Microsoft Corporation.  All rights reserved.
3// Licensed under the MIT License (MIT). See License.txt in the repo root for license information.
4// ------------------------------------------------------------
5
6use crate::types::{
7    FaultType, HealthInformation, LoadMetric, LoadMetricListRef, MoveCost,
8    ServicePartitionInformation,
9};
10use mssf_com::FabricRuntime::{
11    IFabricStatelessServicePartition, IFabricStatelessServicePartition3,
12};
13use windows_core::Interface;
14// wrap of com interface
15#[derive(Debug, Clone)]
16pub struct StatelessServicePartition {
17    com_impl: IFabricStatelessServicePartition3,
18}
19
20impl StatelessServicePartition {
21    pub fn new(com_impl: IFabricStatelessServicePartition) -> StatelessServicePartition {
22        StatelessServicePartition {
23            com_impl: com_impl.cast().unwrap(),
24        } // cast to the newer version.
25    }
26
27    pub fn get_partition_info(&self) -> crate::Result<ServicePartitionInformation> {
28        let raw = unsafe { self.com_impl.GetPartitionInfo() }?;
29        let raw_ref = unsafe { raw.as_ref().unwrap() };
30        assert!(!raw.is_null());
31        Ok(raw_ref.into())
32    }
33
34    /// Reports load for the current replica in the partition.
35    /// Remarks:
36    /// The reported metrics should correspond to those that are provided in the ServiceLoadMetricDescription
37    /// as a part of the ServiceDescription that is used to create the service. Load metrics that are not
38    /// present in the description are ignored. Reporting custom metrics allows Service Fabric to balance
39    /// services that are based on additional custom information.
40    pub fn report_load(&self, metrics: &[LoadMetric]) -> crate::Result<()> {
41        let metrics_ref = LoadMetricListRef::from_slice(metrics);
42        let raw = metrics_ref.as_raw_slice();
43        unsafe { self.com_impl.ReportLoad(raw) }.map_err(crate::Error::from)
44    }
45
46    /// Enables the replica to report a fault to the runtime and indicates that it has encountered
47    /// an error from which it cannot recover and must either be restarted or removed.
48    pub fn report_fault(&self, fault_type: FaultType) -> crate::Result<()> {
49        unsafe { self.com_impl.ReportFault(fault_type.into()) }.map_err(crate::Error::from)
50    }
51
52    /// Reports the move cost for a replica.
53    /// Remarks:
54    /// Services can report move cost of a replica using this method.
55    /// While the Service Fabric Resource Balances searches for the best balance in the cluster,
56    /// it examines both load information and move cost of each replica.
57    /// Resource balances will prefer to move replicas with lower cost in order to achieve balance.
58    pub fn report_move_cost(&self, move_cost: MoveCost) -> crate::Result<()> {
59        unsafe { self.com_impl.ReportMoveCost(move_cost.into()) }.map_err(crate::Error::from)
60    }
61
62    /// Reports current partition health.
63    pub fn report_partition_health(&self, healthinfo: &HealthInformation) -> crate::Result<()> {
64        let healthinfo_ref = &healthinfo.into();
65        unsafe { self.com_impl.ReportPartitionHealth(healthinfo_ref) }.map_err(crate::Error::from)
66    }
67
68    /// Reports health on the current stateless service instance of the partition.
69    pub fn report_instance_health(&self, healthinfo: &HealthInformation) -> crate::Result<()> {
70        let healthinfo_ref = &healthinfo.into();
71        unsafe { self.com_impl.ReportInstanceHealth(healthinfo_ref) }.map_err(crate::Error::from)
72    }
73}