Dapr - actors之构建块(1)
The actor model originated in 1973. It was proposed by Carl Hewitt as a conceptual model of concurrent computation, a form of computing in which several computations are executed at the same time. Highly parallel computers weren't yet available at that time, but the more recent advancements of multi-core CPUs and distributed systems have made the actor model popular.
Actor 模型 起源于Carl Hewitt 在 1973 年提出的作为并发计算的概念模型,这种形式的计算会同时执行多个计算。 当时并没有高度并行的计算机,但多核 Cpu 和分布式系统的最新进步使得Actor 模型 变得流行。
In the actor model, the actor is an independent unit of compute and state. Actors are completely isolated from each other and they will never share memory. Actors communicate with each other using messages. When an actor receives a message, it can change its internal state, and send messages to other (possibly new) actors.
在Actor 模型中,Actor 是一个计算和状态独立的单元。 Actors 完全彼此隔离,它们永远不会共享内存。 Actors 使用消息相互通信。 当一个Actor 收到消息时,它可以更改其内部状态,并将消息发送到其他 (可能是新的) Actors。
The reason why the actor model makes writing concurrent systems easier is that it provides a turn-based (or single-threaded) access model. Multiple actors can run at the same time, but each actor will process received messages one at a time. This means that you can be sure that at most one thread is active inside an actor at any time. That makes writing correct concurrent and parallel systems much easier.
Actor模型使得编写并发系统变得更简单的,它提供了基于 turn-based 的 (或单线程) 访问模型。 多个Actors可以同时运行,但每个Actor 一次只处理一个接收的消息。 这意味着,在任何时候,都可以确保在Actors 中最多有一个线程处于活动状态。 这使得编写正确的并发系统和并行系统变得更加容易。
What it solves
它解决了什么问题
Actor model implementations are usually tied to a specific language or platform. With the Dapr actors building block however, you can leverage the actor model from any language or platform.
Actor 模型的实现通常绑定到特定语言或平台。 使用 Dapr Actor 构建块可以从任何语言或平台 来使用 Actor 模型。
Dapr's implementation is based on the virtual actor pattern introduced by Project "Orleans". With the virtual actor pattern, you don't need to explicitly create actors. Actors are activated implicitly and placed on a node in the cluster the first time a message is sent to the actor. When not executing operations, actors are silently unloaded from memory. If a node fails, Dapr automatically moves activated actors to healthy nodes. Besides sending messages between actors, the Dapr actor model also support scheduling future work using timers and reminders.
Dapr 的实现基于 项目 "奥尔良" 中引入的虚拟Actor模式。 对于虚拟Actor模式,不需要显式的创建Actor。 第一次将消息发送到Actor时,Actor将被隐式激活并放置在群集中的节点上。 当不执行操作时,Actor 会以静默方式从内存中卸载。 如果某个节点出现故障,Dapr 会自动将激活的Actor 移到正常的节点。 除了在Actor之间发送消息以外,Dapr Actor模型还支持使用计时器和提醒调度将来的工作。
While the actor model can provide great benefits, it's important to carefully consider the actor design. For example, having many clients call the same actor will result in poor performance because the actor operations execute serially. Here are some criteria to check if a scenario is a good fit for Dapr actors:
虽然Actor模型 提供了很大的优势,但必须仔细考虑Actor的设计。 例如,如果多个客户端调用相同的Actor,则会导致性能不佳,因为Actor 操作会按顺序执行。 下面的检查清单是是否适用于 Dapr Actor的一些标准:
- Your problem space involves concurrency. Without actors, you'd have to introduce explicit locking mechanisms in your code.
- 问题空间涉及并发性。 如果没有Actor,则需要在代码中引入显式锁定机制。
- Your problem space can be partitioned into small, independent, and isolated units of state and logic.
- 可以将问题空间分区为小、独立和隔离的状态和逻辑单元。
- You don't need low-latency reads of the actor state. Low-latency reads cannot be guaranteed because actor operations execute serially.
- 不需要低延迟的读取Actor 状态。 因为Actor 操作是按顺序执行,不能保证低延迟读取。
- You don't need to query state across a set of actors. Querying across actors is inefficient because each actor's state needs to be read individually and can introduce unpredictable latencies.
- 不需要在一组Actor 之间查询状态。 跨Actor 的查询效率低下,因为每个Actor 的状态都需要单独读取,并且可能会导致不可预测的延迟。
One design pattern that fits these criteria quite well is the orchestration-based saga or process manager design pattern. A saga manages a sequence of steps that must be taken to reach some outcome. The saga (or process manager) maintains the current state of the sequence and triggers the next step. If a step fails, the saga can execute compensating actions. Actors make it easy to deal with concurrency in the saga and to keep track of the current state. The eShopOnDapr reference application uses the saga pattern and Dapr actors to implement the Ordering process.
满足这些条件的一种设计模式非常好,就是 基于业务流程的 saga 或 流程管理器 设计模式。 Saga 管理必须执行的一系列步骤才能达到某些结果。 Saga (或进程管理器) 维护序列的当前状态,并触发下一步。 如果一个步骤失败,saga 可以执行补偿操作。 利用Actor,可以轻松处理 saga 中的并发,并跟踪当前状态。 EShopOnDapr 参考应用程序使用 saga 模式和 Dapr Actor来实现排序过程。
- 点赞
- 收藏
- 关注作者
评论(0)