将取消令牌存储在服务结构服务中 [英] store cancellation tokens in service fabric services

本文介绍了将取消令牌存储在服务结构服务中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在服务矩阵有状态服务中实现取消任务功能。

I am trying to achieve cancel tasks feature in service fabric stateful services.

计划正在使用取消令牌将通知传播到链接的线程/任务。

Plan is using cancellation token to propagate notices to linked threads / Tasks.

问题是,尽管有这些长时间运行的任务和线程在等待该信号,但我不确定如何才能根据另一个Web API调用找到正确的取消令牌。

The problem is, while there are these long running tasks and threads waiting for this signal, I am not sure how I can find the right cancellation tokens based on another Web API calls.

我一直在考虑使用可靠的字典,因此即使在尝试使用它之前,我也认为这将陷入僵局,因为cancelingToken无法序列化/反序列化。

I was thinking of using reliable dictionary, then even before trying it out, I assume this will hit deadend because cancellationToken can't be serialized / deserialized.

请帮助我,什么是解决此问题的好方法。

Please help me what could be good solution to solve this issue.

更新(我没有想要创建一个新线程,失去该线程中提到的一些重要上下文,因此在本文中进行更新。)

Update (I didn't want to create a new thread losing some of important contexts mentioned in this thread so updating in this post.)

确认以下链接说明确实显示了可靠的服务和参与者方法,支持取消令牌。但是,典型的用例是直接通过Web API通过用户触发(例如单击刷新,转到另一页面等)接收取消请求。在这种情况下,当先前的http请求仍然存在时,需要接收完全相同的端点长时间运行的任务或卡住。

Confirmed that below link description does show Reliable service and actor methods can support cancellation token. However typical use case would be receiving cancellation request directly via web API with user triggering such as click refresh, go to another page, etc. In such scenario, the exact same end point needs to receive the request while previous http requst is lingering with some long running task or stuck. That is not the scenario in this thread.

来自链接:> https://blogs.msdn.microsoft.com/ azureservicefabric / 2016/02/23 / service-fabric-sdk-v1-5-175-并且采用虚拟机规模集/

CancellationToken对IService / IActor的支持

可靠服务和可靠Actor方法现在支持可通过ActorProxy远程进行的取消令牌和ServiceProxy,可让您实施协作取消。想要取消长期运行的服务或参与者方法的客户端可以发出取消令牌的信号,并且取消意图将传播到参与者/服务方法。然后,该方法可以通过查看其取消令牌参数的状态来确定何时停止执行。

Reliable Service and Reliable Actor methods now support a cancellation token that can be remoted via ActorProxy and ServiceProxy, allowing you to implement cooperative cancellation. Clients that want to cancel a long running service or actor method can signal the cancellation token and that cancellation intent will be propagated to the actor/service method. That method can then determine when to stop execution by looking at the state of its cancellation token argument.

例如,具有可能长期运行的方法的参与者合同可以

For example, an actor’s contract that has a possibly long-running method can be modelled as shown below:

        public interface IPrimeNumberActorInterface : IActor
        {

            Task<ulong> FindNextPrimeNumberAsync
                (ulong previous, CancellationToken cancellationToken);

        }

希望取消方法执行的客户端代码可以进行通信

The client code that wishes to cancel the method execution can communicate its intent by canceling the cancellation token.

推荐答案

CancellationToken & CancellationTokenSource 不可序列化,并且不会跨服务调用或SF中的数据复制流过。它只能用于告诉同一进程中的处理程序某个操作已被取消,并且应该在接收到响应的情况下停止任何处理或忽略任何继续。

CancellationToken & CancellationTokenSource are not serializable and doesn't flow across service calls or Data Replication in SF. It can only be used to tell the handler within the same process that an operation has been cancelled and should stop any processing or ignore any continuation in case a response is received.

如果您希望能够启动和取消另一个服务中的操作,则应将操作分为两个调用。

If you want to be able to start and cancel an operation in another service, you should split the operation in two calls.


  • 第一个将生成要返回给客户端的操作ID,并为此操作创建 CancellationTokenSource 来生成 CancellationToken 传递给在后台运行的Task\Thread

  • 第二个将接收和OperationID,并标识是否存在 CancellationTokenSource 并将其取消,以便提供给任何Task\Thread的令牌可以停止任何处理(如果尚未完成或取消的话)。

  • The first will generate an Operation ID to be returned to the client, and Create a CancellationTokenSource for this operation to generate a CancellationToken to be passed to the Task\Thread running in the background
  • The second will receive and OperationID and identify if a CancellationTokenSource exists and cancel it, so that the token provided to any Task\Thread can stop any processing, if not already completed or cancelled.

将其存储为 Dictionary< Guid,CancellationTokenSource> 在进程运行分区时

You could simply store it as a Dictionary<Guid, CancellationTokenSource> in the process\partition running the task.

如果您正在SF中的多个分区中运行这些任务,并计划将其存储在可靠字典中,则不是一个好主意,因为如前所述,您无法将取消序列化到其他分区。

In case you are running these tasks in multiple partitions in SF, and is planning to store it in a Reliable Dictionary, it is not a good idea, because as said previously, you can't serialize the cancellation to other partitions.

在这种情况下,您可以存储OperationID和PartitionID,这样,当您收到任何分区上的取消请求时,所有分区都知道操作在哪里运行,服务将在运行该操作的可靠字典中进行查找,并将取消操作转发到正确的分区。

In this case you can store the OperationID and the PartitionID, so all partitions know where an operation is running, when you receive a call for cancellation on any of the partitions, the service will lookup in this reliable dictionary where the operation is running and forward the cancellation to the right partition.

这篇关于将取消令牌存储在服务结构服务中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆