Skip to main content

mssf_core/runtime/
self_reconfiguring_traits.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
6// self_reconfiguring contains the rs definition of the traits that a user needs
7// to implement for a self-reconfiguring service.
8
9use std::sync::Arc;
10
11use crate::WString;
12use crate::runtime::executor::BoxedCancelToken;
13use crate::types::{
14    SelfReconfiguringConfigurationChangeRequest, SelfReconfiguringConfigurationReport,
15    SelfReconfiguringConfigurationRequest, SelfReconfiguringOpenMode, ServicePartitionInformation,
16};
17
18/// Represents a self-reconfiguring service factory that is responsible for
19/// creating instances of a specific type of self-reconfiguring service.
20/// Self-reconfiguring service factories are registered with the FabricRuntime by
21/// service hosts via `Runtime::register_self_reconfiguring_service_factory()`.
22pub trait ISelfReconfiguringServiceFactory: Send + Sync + 'static {
23    /// Creates a self-reconfiguring service instance for a particular service.
24    /// This method is called by Service Fabric.
25    fn create_instance(
26        &self,
27        servicetypename: WString,
28        servicename: crate::types::Uri,
29        initializationdata: &[u8],
30        partitionid: crate::GUID,
31        instanceid: i64,
32    ) -> crate::Result<Box<dyn ISelfReconfiguringServiceInstance>>;
33}
34
35/// Safe abstraction over the `IFabricSelfReconfiguringServiceInstance` COM
36/// interface that an author implements. Each method corresponds to a method on
37/// that COM interface; `open` and `close` are asynchronous (COM Begin/End), the
38/// rest are synchronous.
39#[async_trait::async_trait]
40pub trait ISelfReconfiguringServiceInstance: Send + Sync + 'static {
41    /// Opens an initialized service instance. The returned string is the address
42    /// of this service instance, which is associated with the service name via
43    /// Service Fabric naming and returned to clients that resolve the service.
44    ///
45    /// `open_mode` is the open mode supplied by Service Fabric.
46    async fn open(
47        &self,
48        partition: Arc<dyn ISelfReconfiguringServicePartition>,
49        open_mode: SelfReconfiguringOpenMode,
50        cancellation_token: BoxedCancelToken,
51    ) -> crate::Result<WString>;
52
53    /// Public documentation for this interface is TBD.
54    fn request_configuration(
55        &self,
56        request: SelfReconfiguringConfigurationRequest,
57    ) -> crate::Result<()>;
58
59    /// Public documentation for this interface is TBD.
60    fn request_configuration_change(
61        &self,
62        change: SelfReconfiguringConfigurationChangeRequest,
63    ) -> crate::Result<()>;
64
65    /// Closes this service instance gracefully when the service instance is being
66    /// shut down.
67    async fn close(&self, cancellation_token: BoxedCancelToken) -> crate::Result<()>;
68
69    /// Terminates this instance ungracefully with this synchronous method call.
70    /// When the service instance receives this method, it should immediately
71    /// release and clean up all references and return.
72    fn abort(&self);
73}
74
75/// Safe abstraction over the `IFabricSelfReconfiguringServicePartition` COM
76/// interface. Each method corresponds to a synchronous method on that interface.
77pub trait ISelfReconfiguringServicePartition: Send + Sync + 'static {
78    /// Provides access to the `ServicePartitionInformation` of the service, which
79    /// contains the partition type and ID.
80    fn get_partition_info(&self) -> crate::Result<ServicePartitionInformation>;
81
82    /// Reports load for the current instance in the partition.
83    fn report_load(&self, metrics: &[crate::types::LoadMetric]) -> crate::Result<()>;
84
85    /// Enables the instance to report a fault to the runtime and indicates that it
86    /// has encountered an error from which it cannot recover and must either be
87    /// restarted or removed.
88    fn report_fault(&self, fault_type: crate::types::FaultType) -> crate::Result<()>;
89
90    /// Reports the move cost for an instance.
91    fn report_move_cost(&self, move_cost: crate::types::MoveCost) -> crate::Result<()>;
92
93    /// Reports health on the current self-reconfiguring service instance of the
94    /// partition.
95    fn report_instance_health(
96        &self,
97        health_info: &crate::types::HealthInformation,
98    ) -> crate::Result<()>;
99
100    /// Reports current partition health.
101    fn report_partition_health(
102        &self,
103        health_info: &crate::types::HealthInformation,
104    ) -> crate::Result<()>;
105
106    /// Public documentation for this interface is TBD.
107    fn report_configuration(
108        &self,
109        report: &SelfReconfiguringConfigurationReport,
110    ) -> crate::Result<()>;
111}