mssf-core 0.7.0

Rust for Azure Service Fabric. Rust safe APIs.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
// ------------------------------------------------------------
// Copyright (c) Microsoft Corporation.  All rights reserved.
// Licensed under the MIT License (MIT). See License.txt in the repo root for license information.
// ------------------------------------------------------------

use std::time::Duration;

use mssf_com::{
    FabricClient::{IFabricHealthClient4, IFabricNodeHealthResult},
    FabricTypes::{
        FABRIC_APPLICATION_HEALTH_REPORT, FABRIC_CLUSTER_HEALTH_QUERY_DESCRIPTION,
        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_QUERY_DESCRIPTION,
        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::{
    mem::{BoxPool, GetRawWithBoxPool},
    runtime::executor::BoxedCancelToken,
    sync::{FabricReceiver, fabric_begin_end_proxy},
    types::{ClusterHealth, HealthReport, NodeHealthQueryDescription, NodeHealthResult},
};

/// 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).
///
/// TODO: Implement full functionality of the HealthClient.
#[derive(Debug, Clone)]
pub struct HealthClient {
    com: IFabricHealthClient4,
}

impl From<IFabricHealthClient4> for HealthClient {
    fn from(value: IFabricHealthClient4) -> Self {
        Self { com: value }
    }
}

impl From<HealthClient> for IFabricHealthClient4 {
    fn from(value: HealthClient) -> Self {
        value.com
    }
}

// Public implementation block
impl HealthClient {
    /// Reports health on a Service Fabric entity. See C# API [here](https://docs.microsoft.com/en-us/dotnet/api/system.fabric.fabricclient.healthclient.reporthealth?view=azure-dotnet).
    ///
    /// Remarks:
    /// When a cluster is secured, the health client needs administrator permission to be able to send the reports.
    /// 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) -> crate::Result<()> {
        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) }
            }
        }.map_err(crate::Error::from)
    }
}

impl HealthClient {
    fn get_node_health_internal(
        &self,
        desc: &FABRIC_NODE_HEALTH_QUERY_DESCRIPTION,
        timeout_milliseconds: u32,
        cancellation_token: Option<BoxedCancelToken>,
    ) -> FabricReceiver<crate::WinResult<IFabricNodeHealthResult>> {
        let com1 = &self.com;
        let com2 = self.com.clone();
        fabric_begin_end_proxy(
            move |callback| unsafe {
                com1.BeginGetNodeHealth2(desc, timeout_milliseconds, callback)
            },
            move |ctx| unsafe { com2.EndGetNodeHealth2(ctx) },
            cancellation_token,
        )
    }

    fn get_cluster_health_internal(
        &self,
        desc: &FABRIC_CLUSTER_HEALTH_QUERY_DESCRIPTION,
        timeout_milliseconds: u32,
        cancellation_token: Option<BoxedCancelToken>,
    ) -> FabricReceiver<crate::WinResult<mssf_com::FabricClient::IFabricClusterHealthResult>> {
        let com1 = &self.com;
        let com2 = self.com.clone();
        fabric_begin_end_proxy(
            move |callback| unsafe {
                com1.BeginGetClusterHealth2(desc, timeout_milliseconds, callback)
            },
            move |ctx| unsafe { com2.EndGetClusterHealth2(ctx) },
            cancellation_token,
        )
    }

    fn get_application_health_internal(
        &self,
        desc: &mssf_com::FabricTypes::FABRIC_APPLICATION_HEALTH_QUERY_DESCRIPTION,
        timeout_milliseconds: u32,
        cancellation_token: Option<BoxedCancelToken>,
    ) -> FabricReceiver<crate::WinResult<mssf_com::FabricClient::IFabricApplicationHealthResult>>
    {
        let com1 = &self.com;
        let com2 = self.com.clone();
        fabric_begin_end_proxy(
            move |callback| unsafe {
                com1.BeginGetApplicationHealth2(desc, timeout_milliseconds, callback)
            },
            move |ctx| unsafe { com2.EndGetApplicationHealth2(ctx) },
            cancellation_token,
        )
    }
    fn get_partition_health_internal(
        &self,
        desc: &mssf_com::FabricTypes::FABRIC_PARTITION_HEALTH_QUERY_DESCRIPTION,
        timeout_milliseconds: u32,
        cancellation_token: Option<BoxedCancelToken>,
    ) -> FabricReceiver<crate::WinResult<mssf_com::FabricClient::IFabricPartitionHealthResult>>
    {
        let com1 = &self.com;
        let com2 = self.com.clone();
        fabric_begin_end_proxy(
            move |callback| unsafe {
                com1.BeginGetPartitionHealth2(desc, timeout_milliseconds, callback)
            },
            move |ctx| unsafe { com2.EndGetPartitionHealth2(ctx) },
            cancellation_token,
        )
    }
    fn get_service_health_internal(
        &self,
        desc: &mssf_com::FabricTypes::FABRIC_SERVICE_HEALTH_QUERY_DESCRIPTION,
        timeout_milliseconds: u32,
        cancellation_token: Option<BoxedCancelToken>,
    ) -> FabricReceiver<crate::WinResult<mssf_com::FabricClient::IFabricServiceHealthResult>> {
        let com1 = &self.com;
        let com2 = self.com.clone();
        fabric_begin_end_proxy(
            move |callback| unsafe {
                com1.BeginGetServiceHealth2(desc, timeout_milliseconds, callback)
            },
            move |ctx| unsafe { com2.EndGetServiceHealth2(ctx) },
            cancellation_token,
        )
    }
    fn get_replica_health_internal(
        &self,
        desc: &mssf_com::FabricTypes::FABRIC_REPLICA_HEALTH_QUERY_DESCRIPTION,
        timeout_milliseconds: u32,
        cancellation_token: Option<BoxedCancelToken>,
    ) -> FabricReceiver<crate::WinResult<mssf_com::FabricClient::IFabricReplicaHealthResult>> {
        let com1 = &self.com;
        let com2 = self.com.clone();
        fabric_begin_end_proxy(
            move |callback| unsafe {
                com1.BeginGetReplicaHealth2(desc, timeout_milliseconds, callback)
            },
            move |ctx| unsafe { com2.EndGetReplicaHealth2(ctx) },
            cancellation_token,
        )
    }
}

impl HealthClient {
    /// Gets the health of a node.
    ///
    /// See C# API [here](https://learn.microsoft.com/en-us/dotnet/api/system.fabric.fabricclient.healthclient.getnodehealthasync?view=azure-dotnet).
    pub async fn get_node_health(
        &self,
        desc: &NodeHealthQueryDescription,
        timeout: Duration,
        cancellation_token: Option<BoxedCancelToken>,
    ) -> crate::Result<NodeHealthResult> {
        let com = {
            let mut pool = BoxPool::new();
            let desc_raw = desc.get_raw_with_pool(&mut pool);
            self.get_node_health_internal(&desc_raw, timeout.as_millis() as u32, cancellation_token)
        }
        .await??;
        Ok(NodeHealthResult::from_com(&com))
    }

    /// Gets the health of the cluster.
    pub async fn get_cluster_health(
        &self,
        desc: &crate::types::ClusterHealthQueryDescription,
        timeout: Duration,
        cancellation_token: Option<BoxedCancelToken>,
    ) -> crate::Result<ClusterHealth> {
        let com = {
            // Build the app policy map
            let mut pool = BoxPool::new();
            let desc_raw = desc.get_raw_with_pool(&mut pool);
            self.get_cluster_health_internal(
                &desc_raw,
                timeout.as_millis() as u32,
                cancellation_token,
            )
        }
        .await??;
        Ok(ClusterHealth::from(&com))
    }

    /// Gets the health of an application.
    pub async fn get_application_health(
        &self,
        desc: &crate::types::ApplicationHealthQueryDescription,
        timeout: Duration,
        cancellation_token: Option<BoxedCancelToken>,
    ) -> crate::Result<crate::types::ApplicationHealth> {
        let com = {
            let mut pool = BoxPool::new();
            let desc_raw = desc.get_raw_with_pool(&mut pool);
            self.get_application_health_internal(
                &desc_raw,
                timeout.as_millis() as u32,
                cancellation_token,
            )
        }
        .await??;
        Ok(crate::types::ApplicationHealth::from(&com))
    }

    pub async fn get_partition_health(
        &self,
        desc: &crate::types::PartitionHealthQueryDescription,
        timeout: Duration,
        cancellation_token: Option<BoxedCancelToken>,
    ) -> crate::Result<crate::types::PartitionHealthResult> {
        let com = {
            let mut pool = BoxPool::new();
            let desc_raw = desc.get_raw_with_pool(&mut pool);
            self.get_partition_health_internal(
                &desc_raw,
                timeout.as_millis() as u32,
                cancellation_token,
            )
        }
        .await??;
        Ok(crate::types::PartitionHealthResult::from(&com))
    }

    /// Gets the health of a service.
    pub async fn get_service_health(
        &self,
        desc: &crate::types::ServiceHealthQueryDescription,
        timeout: Duration,
        cancellation_token: Option<BoxedCancelToken>,
    ) -> crate::Result<crate::types::ServiceHealthResult> {
        let com = {
            let mut pool = BoxPool::new();
            let desc_raw = desc.get_raw_with_pool(&mut pool);
            self.get_service_health_internal(
                &desc_raw,
                timeout.as_millis() as u32,
                cancellation_token,
            )
        }
        .await??;
        Ok(crate::types::ServiceHealthResult::from(&com))
    }

    /// Gets the health of a replica.
    pub async fn get_replica_health(
        &self,
        desc: &crate::types::ReplicaHealthQueryDescription,
        timeout: Duration,
        cancellation_token: Option<BoxedCancelToken>,
    ) -> crate::Result<crate::types::ReplicaHealthResult> {
        let com = {
            let mut pool = BoxPool::new();
            let desc_raw = desc.get_raw_with_pool(&mut pool);
            self.get_replica_health_internal(
                &desc_raw,
                timeout.as_millis() as u32,
                cancellation_token,
            )
        }
        .await??;
        Ok(crate::types::ReplicaHealthResult::from(&com))
    }
}