实现 Reliable Actors 备份和还原Implement Reliable Actors backup and restore

备注

Azure 建议使用定期备份和还原来配置可靠有状态服务和 Reliable Actors 的数据备份。Azure recommends to use Periodic backup and restore for configuring data backup of Reliable Stateful services and Reliable Actors.

在下面的示例中,自定义执行组件服务通过利用已存在于 ActorService 中的远程侦听器公开备份执行组件数据的方法:In the following example, a custom actor service exposes a method to back up actor data by taking advantage of the remoting listener already present in ActorService:

public interface IMyActorService : IService
{
    Task BackupActorsAsync();
}

class MyActorService : ActorService, IMyActorService
{
    public MyActorService(StatefulServiceContext context, ActorTypeInformation typeInfo, Func<ActorBase> newActor)
        : base(context, typeInfo, newActor)
    { }

    public Task BackupActorsAsync()
    {
        return this.BackupAsync(new BackupDescription(PerformBackupAsync));
    }

    private async Task<bool> PerformBackupAsync(BackupInfo backupInfo, CancellationToken cancellationToken)
    {
        try
        {
           // store the contents of backupInfo.Directory
           return true;
        }
        finally
        {
           Directory.Delete(backupInfo.Directory, recursive: true);
        }
    }
}
public interface MyActorService extends Service
{
    CompletableFuture<?> backupActorsAsync();
}

class MyActorServiceImpl extends ActorService implements MyActorService
{
    public MyActorService(StatefulServiceContext context, ActorTypeInformation typeInfo, Func<FabricActorService, ActorId, ActorBase> newActor)
    {
       super(context, typeInfo, newActor);
    }

    public CompletableFuture backupActorsAsync()
    {
        return this.backupAsync(new BackupDescription((backupInfo, cancellationToken) -> performBackupAsync(backupInfo, cancellationToken)));
    }

    private CompletableFuture<Boolean> performBackupAsync(BackupInfo backupInfo, CancellationToken cancellationToken)
    {
        try
        {
           // store the contents of backupInfo.Directory
           return true;
        }
        finally
        {
           deleteDirectory(backupInfo.Directory)
        }
    }

    void deleteDirectory(File file) {
        File[] contents = file.listFiles();
        if (contents != null) {
            for (File f : contents) {
               deleteDirectory(f);
             }
        }
        file.delete();
    }
}

在本示例中,IMyActorService 是一个实现 IService (C#) 和 Service (Java),并由 MyActorService 实现的远程协定。In this example, IMyActorService is a remoting contract that implements IService (C#) and Service (Java), and is then implemented by MyActorService. 通过添加此远程协定,并通过 ActorServiceProxy 创建远程代理,现在 IMyActorService 的方法也可用于客户端:By adding this remoting contract, methods on IMyActorService are now also available to a client by creating a remoting proxy via ActorServiceProxy:

IMyActorService myActorServiceProxy = ActorServiceProxy.Create<IMyActorService>(
    new Uri("fabric:/MyApp/MyService"), ActorId.CreateRandom());

await myActorServiceProxy.BackupActorsAsync();
MyActorService myActorServiceProxy = ActorServiceProxy.create(MyActorService.class,
    new URI("fabric:/MyApp/MyService"), actorId);

myActorServiceProxy.backupActorsAsync();

有关 Reliable Actors 的详细信息,请阅读以下文章:For more information on Reliable Actors, read the following articles: