Service Fabric Reliable Actors を列挙する
Reliable Actors サービスでは、クライアントは、サービスがホストしているアクターに関するメタデータを列挙できます。 アクター サービスはパーティション分割されたステートフル サービスであるため、列挙はパーティションごとに実行されます。 各パーティションには多数のアクターが含まれる可能性があるため、列挙はページングされた結果のセットとして返されます。 ページはすべてのページが読み取られるまでループされます。 次の例は、アクター サービスの 1 つのパーティションに含まれるすべてのアクティブ アクターの一覧を作成する方法を示しています。
IActorService actorServiceProxy = ActorServiceProxy.Create(
new Uri("fabric:/MyApp/MyService"), partitionKey);
ContinuationToken continuationToken = null;
List<ActorInformation> activeActors = new List<ActorInformation>();
do
{
PagedResult<ActorInformation> page = await actorServiceProxy.GetActorsAsync(continuationToken, cancellationToken);
activeActors.AddRange(page.Items.Where(x => x.IsActive));
continuationToken = page.ContinuationToken;
}
while (continuationToken != null);
ActorService actorServiceProxy = ActorServiceProxy.create(
new URI("fabric:/MyApp/MyService"), partitionKey);
ContinuationToken continuationToken = null;
List<ActorInformation> activeActors = new ArrayList<ActorInformation>();
do
{
PagedResult<ActorInformation> page = actorServiceProxy.getActorsAsync(continuationToken);
while(ActorInformation x: page.getItems())
{
if(x.isActive()){
activeActors.add(x);
}
}
continuationToken = page.getContinuationToken();
}
while (continuationToken != null);
上記のコードでは、特定のパーティション内のすべてのアクターが取得されますが、各パーティションにわたってすべてのアクター (アクティブまたは非アクティブ) の ID を照会する必要が生じることがあります。 これは非常に面倒な作業であるため、例外的に行う必要があります。
次の例では、サービスのパーティションに対してクエリを実行し、上記の例と組み合わせて各パーティションを反復処理して、Service Fabric アプリケーション全体でサービス内のすべてのアクティブ アクターと非アクティブ アクターの一覧を生成する方法を示しています。
var serviceName = new Uri("fabric:/MyApp/MyService");
//As the FabricClient is expensive to create, it should be shared as much as possible
FabricClient fabricClient = new();
//List each of the service's partitions
ServicePartitionList partitions = await fabricClient.QueryManager.GetPartitionListAsync(serviceName);
List<Guid> actorIds = new();
foreach(var partition in partitions)
{
//Retrieve the partition information
Int64RangePartitionInformation partitionInformation = (Int64RangePartitionInformation)partition.PartitionInformation; //Actors are restricted to the uniform Int64 scheme per https://learn.microsoft.com/azure/service-fabric/service-fabric-reliable-actors-introduction#distribution-and-failover
IActorService actorServiceProxy = ActorServiceProxy.Create(serviceName, partitionInformation.LowKey);
ContinuationToken? continuationToken = null;
do
{
var page = await actorServiceProxy.GetActorsAsync(continuationToken, cancellationToken);
actorIds.AddRange(page.Items.Select(actor => actor.ActorId.GetGuidId());
continuationToken = page.ContinuationToken;
} while (continuationToken != null);
}
return actorIds;