mssf_core/runtime/stateless_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#![deny(non_snake_case)] // this file is safe rust
7
8use std::sync::Arc;
9
10use crate::WString;
11use crate::runtime::executor::BoxedCancelToken;
12use crate::types::ServicePartitionInformation;
13
14/// Stateless service factories are registered with the FabricRuntime by service hosts via
15/// Runtime::register_stateless_service_factory().
16///
17pub trait IStatelessServiceFactory: Send + Sync + 'static {
18 /// Creates a stateless service instance for a particular service. This method is called by Service Fabric.
19 fn create_instance(
20 &self,
21 servicetypename: WString,
22 servicename: crate::types::Uri,
23 initializationdata: &[u8],
24 partitionid: crate::GUID,
25 instanceid: i64,
26 ) -> crate::Result<Box<dyn IStatelessServiceInstance>>;
27}
28
29/// Defines behavior that governs the lifecycle of a stateless service instance, such as startup, initialization, and shutdown.
30#[async_trait::async_trait]
31pub trait IStatelessServiceInstance: Send + Sync + 'static {
32 /// Opens an initialized service instance so that it can be contacted by clients.
33 /// Remarks:
34 /// Opening an instance stateless service indicates that the service is now resolvable
35 /// and discoverable by service clients. The string that is returned is the address of this service instance.
36 /// The address is associated with the service name via Service Fabric naming and returned to
37 /// clients that resolve the service via resolve_service_partition(uri).
38 async fn open(
39 &self,
40 partition: Arc<dyn IStatelessServicePartition>,
41 cancellation_token: BoxedCancelToken,
42 ) -> crate::Result<WString>;
43
44 /// Closes this service instance gracefully when the service instance is being shut down.
45 async fn close(&self, cancellation_token: BoxedCancelToken) -> crate::Result<()>;
46
47 /// Terminates this instance ungracefully with this synchronous method call.
48 /// Remarks:
49 /// Examples of ungraceful termination are network issues resulting in Service Fabric process shutdown and the
50 /// use of ReportFault(FaultType) to report a Permanent fault. When the service instance receives this method,
51 /// it should immediately release and clean up all references and return.
52 fn abort(&self);
53}
54
55/// Abstrction for IFStatelessServicePartition interface
56pub trait IStatelessServicePartition: Send + Sync + 'static {
57 fn get_partition_info(&self) -> crate::Result<ServicePartitionInformation>;
58 /// Reports load for the current replica in the partition.
59 /// Remarks:
60 /// The reported metrics should correspond to those that are provided in the ServiceLoadMetricDescription
61 /// as a part of the ServiceDescription that is used to create the service. Load metrics that are not
62 /// present in the description are ignored. Reporting custom metrics allows Service Fabric to balance
63 /// services that are based on additional custom information.
64 fn report_load(&self, metrics: &[crate::types::LoadMetric]) -> crate::Result<()>;
65 /// Enables the replica to report a fault to the runtime and indicates that it has encountered
66 /// an error from which it cannot recover and must either be restarted or removed.
67 fn report_fault(&self, fault_type: crate::types::FaultType) -> crate::Result<()>;
68 /// Reports the move cost for a replica.
69 /// Remarks:
70 /// Services can report move cost of a replica using this method.
71 /// While the Service Fabric Resource Balances searches for the best balance in the cluster,
72 /// it examines both load information and move cost of each replica.
73 /// Resource balances will prefer to move replicas with lower cost in order to achieve balance.
74 fn report_move_cost(&self, move_cost: crate::types::MoveCost) -> crate::Result<()>;
75 /// Reports current partition health.
76 fn report_partition_health(
77 &self,
78 health_info: &crate::types::HealthInformation,
79 ) -> crate::Result<()>;
80 /// Reports health on the current stateless service instance of the partition.
81 fn report_instance_health(
82 &self,
83 health_info: &crate::types::HealthInformation,
84 ) -> crate::Result<()>;
85}