Skip to content

Commit

Permalink
Support fabric client 'remove replica' api (#59)
Browse files Browse the repository at this point in the history
Support fabric client remove replica api.
Added test in Echo app where the app instance gets removed.
  • Loading branch information
youyuanwu authored Aug 12, 2024
1 parent 898e95d commit d6657c5
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 15 deletions.
44 changes: 36 additions & 8 deletions crates/libs/core/src/client/svc_mgmt_client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,21 +10,21 @@ use mssf_com::{
FabricTypes::{
FABRIC_PARTITION_KEY_TYPE, FABRIC_PARTITION_KEY_TYPE_INT64,
FABRIC_PARTITION_KEY_TYPE_INVALID, FABRIC_PARTITION_KEY_TYPE_NONE,
FABRIC_PARTITION_KEY_TYPE_STRING, FABRIC_RESOLVED_SERVICE_ENDPOINT,
FABRIC_RESTART_REPLICA_DESCRIPTION, FABRIC_SERVICE_ENDPOINT_ROLE,
FABRIC_SERVICE_PARTITION_KIND, FABRIC_SERVICE_PARTITION_KIND_INT64_RANGE,
FABRIC_SERVICE_PARTITION_KIND_INVALID, FABRIC_SERVICE_PARTITION_KIND_NAMED,
FABRIC_SERVICE_PARTITION_KIND_SINGLETON, FABRIC_SERVICE_ROLE_INVALID,
FABRIC_SERVICE_ROLE_STATEFUL_PRIMARY, FABRIC_SERVICE_ROLE_STATEFUL_SECONDARY,
FABRIC_SERVICE_ROLE_STATELESS, FABRIC_URI,
FABRIC_PARTITION_KEY_TYPE_STRING, FABRIC_REMOVE_REPLICA_DESCRIPTION,
FABRIC_RESOLVED_SERVICE_ENDPOINT, FABRIC_RESTART_REPLICA_DESCRIPTION,
FABRIC_SERVICE_ENDPOINT_ROLE, FABRIC_SERVICE_PARTITION_KIND,
FABRIC_SERVICE_PARTITION_KIND_INT64_RANGE, FABRIC_SERVICE_PARTITION_KIND_INVALID,
FABRIC_SERVICE_PARTITION_KIND_NAMED, FABRIC_SERVICE_PARTITION_KIND_SINGLETON,
FABRIC_SERVICE_ROLE_INVALID, FABRIC_SERVICE_ROLE_STATEFUL_PRIMARY,
FABRIC_SERVICE_ROLE_STATEFUL_SECONDARY, FABRIC_SERVICE_ROLE_STATELESS, FABRIC_URI,
},
};
use windows_core::{HSTRING, PCWSTR};

use crate::{
iter::{FabricIter, FabricListAccessor},
sync::{fabric_begin_end_proxy, FabricReceiver},
types::RestartReplicaDescription,
types::{RemoveReplicaDescription, RestartReplicaDescription},
};

// Service Management Client
Expand Down Expand Up @@ -74,6 +74,21 @@ impl ServiceManagementClient {
move |ctx| unsafe { com2.EndRestartReplica(ctx) },
)
}

fn remove_replica_internal(
&self,
desc: &FABRIC_REMOVE_REPLICA_DESCRIPTION,
timeout_milliseconds: u32,
) -> FabricReceiver<crate::Result<()>> {
let com1 = &self.com;
let com2 = self.com.clone();
fabric_begin_end_proxy(
move |callback| unsafe {
com1.BeginRemoveReplica(desc, timeout_milliseconds, callback)
},
move |ctx| unsafe { com2.EndRemoveReplica(ctx) },
)
}
}

// public implementation block
Expand Down Expand Up @@ -122,6 +137,19 @@ impl ServiceManagementClient {
self.restart_replica_internal(&raw, timeout.as_millis() as u32)
.await
}

/// This API gives a running replica the chance to cleanup its state and be gracefully shutdown.
/// WARNING: There are no safety checks performed when this API is used.
/// Incorrect use of this API can lead to data loss for stateful services.
pub async fn remove_replica(
&self,
desc: &RemoveReplicaDescription,
timeout: Duration,
) -> crate::Result<()> {
let raw: FABRIC_REMOVE_REPLICA_DESCRIPTION = desc.into();
self.remove_replica_internal(&raw, timeout.as_millis() as u32)
.await
}
}

// see ComFabricClient.cpp for conversion details in cpp
Expand Down
26 changes: 23 additions & 3 deletions crates/libs/core/src/types/client/replica.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,10 @@ use mssf_com::{
FABRIC_QUERY_SERVICE_REPLICA_STATUS, FABRIC_QUERY_SERVICE_REPLICA_STATUS_DOWN,
FABRIC_QUERY_SERVICE_REPLICA_STATUS_DROPPED, FABRIC_QUERY_SERVICE_REPLICA_STATUS_INBUILD,
FABRIC_QUERY_SERVICE_REPLICA_STATUS_INVALID, FABRIC_QUERY_SERVICE_REPLICA_STATUS_READY,
FABRIC_QUERY_SERVICE_REPLICA_STATUS_STANDBY, FABRIC_RESTART_REPLICA_DESCRIPTION,
FABRIC_SERVICE_KIND_STATEFUL, FABRIC_SERVICE_KIND_STATELESS,
FABRIC_SERVICE_REPLICA_QUERY_DESCRIPTION, FABRIC_SERVICE_REPLICA_QUERY_RESULT_ITEM,
FABRIC_QUERY_SERVICE_REPLICA_STATUS_STANDBY, FABRIC_REMOVE_REPLICA_DESCRIPTION,
FABRIC_RESTART_REPLICA_DESCRIPTION, FABRIC_SERVICE_KIND_STATEFUL,
FABRIC_SERVICE_KIND_STATELESS, FABRIC_SERVICE_REPLICA_QUERY_DESCRIPTION,
FABRIC_SERVICE_REPLICA_QUERY_RESULT_ITEM,
FABRIC_STATEFUL_SERVICE_REPLICA_QUERY_RESULT_ITEM,
FABRIC_STATELESS_SERVICE_INSTANCE_QUERY_RESULT_ITEM,
},
Expand Down Expand Up @@ -197,3 +198,22 @@ impl From<&RestartReplicaDescription> for FABRIC_RESTART_REPLICA_DESCRIPTION {
}
}
}

// FABRIC_REMOVE_REPLICA_DESCRIPTION
pub struct RemoveReplicaDescription {
pub node_name: HSTRING,
pub partition_id: GUID,
pub replica_or_instance_id: i64,
// TODO: support force flag
}

impl From<&RemoveReplicaDescription> for FABRIC_REMOVE_REPLICA_DESCRIPTION {
fn from(value: &RemoveReplicaDescription) -> Self {
Self {
NodeName: PCWSTR(value.node_name.as_ptr()),
PartitionId: value.partition_id,
ReplicaOrInstanceId: value.replica_or_instance_id,
Reserved: std::ptr::null_mut(),
}
}
}
17 changes: 13 additions & 4 deletions crates/samples/echomain/src/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@ use std::time::Duration;
use mssf_core::{
client::FabricClient,
types::{
QueryServiceReplicaStatus, ServicePartition, ServicePartitionInformation,
ServicePartitionQueryDescription, ServicePartitionStatus, ServiceReplicaQueryDescription,
ServiceReplicaQueryResult,
QueryServiceReplicaStatus, RemoveReplicaDescription, ServicePartition,
ServicePartitionInformation, ServicePartitionQueryDescription, ServicePartitionStatus,
ServiceReplicaQueryDescription, ServiceReplicaQueryResult,
},
GUID, HSTRING,
};
Expand Down Expand Up @@ -67,5 +67,14 @@ async fn test_fabric_client() {
);
assert_ne!(stateless_replica.node_name, HSTRING::new());

// TODO: stateless restart should use remove-replica api.
// Restart the stateless instance by removing it.
let mgmt = fc.get_service_manager();
let desc = RemoveReplicaDescription {
node_name: stateless_replica.node_name,
partition_id: single.id,
replica_or_instance_id: stateless_replica.instance_id,
};
mgmt.remove_replica(&desc, timeout)
.await
.expect("Failed to remove replica");
}

0 comments on commit d6657c5

Please sign in to comment.