简体   繁体   English

Akka在Java中键入了演员

[英]Akka typed actors in Java

I don't understand why not to use TypedActors in Akka . 我不明白为什么不在Akka使用TypedActors Using reflection (well.. instanceof ) to compensate for the lack of pattern matching in Java is quite ugly. 使用反射(well .. instanceof )来弥补Java中缺少模式匹配是非常难看的。
As far as I understand, TypedActors should be like a gate between the "Akka world" and the "Non Akka world" of your software. 据我所知, TypedActors应该像你的软件的“Akka world”和“Non Akka world”之间的门。 But why won't we just throw all OO principals and just use reflection! 但是,为什么我们不抛弃所有OO主体并仅使用反射!
Why wouldn't you want to use an actor and know exactly what it should respond to? 您为什么不想要使用演员并确切知道它应该响应什么呢? Or for Akka's sake of keeping the actor model, why not create a message hierarchy that uses double-dispatch in order to activate the right method in the actor (and I know you shouldn't pass Actors as parameters and use ActorRef instead). 或者为了保持演员模型的Akka,为什么不创建一个使用双调度的消息层次结构来激活actor中的正确方法(我知道你不应该将Actors作为参数传递并使用ActorRef代替)。
DISCLAIMER: I'm new to Akka and this model, and I haven't wrote a single line of code using Akka , but just reading the documentation is giving me a headache. 免责声明:我是Akka和这个模型的新手,我没有使用Akka写过一行代码,但只是阅读文档让我头疼。

Before we get started : The question is about the deprecated "typed actors" module . 在我们开始之前 :问题是关于已弃用的 “类型化演员”模块 Which will soon be replaced with akka-typed , a far superior take on the problem, which avoids the below explained shortcomings - please do have a look at akka-typed if you're interested in typed actors! 这将很快被akka-typed取代,这是一个更优越的问题,这避免了以下解释的缺点 - 如果你对打字的演员感兴趣,请看看akka-typed!


I'll enumerate a number of downsides of using the typed actors implementation you refer to. 我将列举使用您引用的类型化actor实现的一些弊端。 Please do note however that we have just merged a new akka-typed module, which brings in type safety back to the world of akka actors. 但是请注意,我们刚刚合并了一个新的akka型模块,这将类型安全性带回了akka演员的世界。 For the sake of this post, I will not go in depth into the reasons developing the typed version was such a tough challenge, let's for now answer the question of "why not use the (old) typed actors". 为了这篇文章,我不会深入研究开发打字版本是如此艰难的挑战的原因,让我们现在回答“为什么不使用(旧的)打字演员”的问题。

Firstly , they were never designed to be the core of the toolkit. 首先 ,它们从未被设计为工具包的核心。 They are built on top of the messaging infrastructure Akka provides. 它们建立在Akka提供的消息传递基础架构之上。 Please note that thanks to that messaging infrastructure we're able to achieve location transparency, and Akka's well known performance. 请注意,由于有了该消息传递基础结构,我们能够实现位置透明性以及Akka著名的性能。 They heavily use reflection and JDK proxies to translate to and from methods to message sends. 他们大量使用反射和JDK代理来转换消息发送方法和从消息发送方法转换。 This is very expensive (time wise), and downgrades the performance around 10-fold in contrast to plain Akka Actors, see below for a "ping pong" benchmark (implemented using both styles, sender tells to actor, actor replies - 100.000 times): 这是非常昂贵的(时间方面),并且与普通的Akka Actors相比,将性能降低了大约10倍,参见下面的“乒乓”基准测试(使用两种风格实现,发送者告诉演员,演员回复 - 100.000次) :

Unit = ops/ms
Benchmark                                                Mode   Samples         Mean   Mean error    Units
TellPingPongBenchmark.tell_100000_msgs                   thrpt       20 119973619.810 79577253.299   ops/ms
JdkProxyTypedActorTellPingPongBenchmark.tell_100000_msgs thrpt       20  16697718.988   406179.847   ops/ms

Unit = us/op
Benchmark                                                Mode   Samples         Mean   Mean error    Units
TellPingPongBenchmark.tell_100000_msgs                   sample  133647        1.223        0.916    us/op
JdkProxyTypedActorTellPingPongBenchmark.tell_100000_msgs sample  222869       12.416        0.045    us/op

(Benchmarks are kept in akka/akka-bench-jmh and run using the OpenJDK JMH tool, via the sbt-jmh plugin.) (基准测试保存在akka / akka-bench-jmh中,并使用OpenJDK JMH工具,通过sbt-jmh插件运行。)

Secondly , using methods to abstract over distributed systems is just not a good way of going about it (oh, how I remember RMI... let's not go there again). 其次 ,使用方法来抽象分布式系统并不是一个很好的方法(哦,我记得RMI ......让我们不再去那里)。 Using such "looks like a method" makes you stop thinking about message loss, reordering and all the things which can and do happen in distributed systems. 使用这样的“看起来像方法”使您不必再考虑消息丢失,重新排序以及分布式系统中所有可能发生的事情。 It also encourages (makes it " too easy to do the wrong thing ") using signatures like def getThing(id: Int): Thing - which would generate blocking code - which is horrible for performance! 它还使用def getThing(id: Int): Thing类的签名鼓励(使其“ 容易做错def getThing(id: Int): Thing - 会生成阻塞代码 - 这对性能来说太可怕了! You really do want to stay asynchronous and responsive , which is why you'd end up with loads of futures when trying to work properly with these (proxy based) typed actors. 您确实确实想保持异步和响应能力 ,这就是为什么当尝试与这些(基于代理)类型的actor正常工作时,最终会承受大量期货的原因。

Lastly , you basically lose one of the main Actor capabilities . 最后 ,你基本上失去了一个主要的Actor功能 The 3 canonical operations an Actor can perform are 1) send messages 2) start child actors 3) change it's own behaviour based on received messages (see Carl Hewitt's original paper on the Actor Model ). Actor可以执行的3个规范操作是1)发送消息2)启动子actor 3) 根据收到的消息改变它自己的行为 (参见Carl Hewitt关于Actor模型的原始论文)。 The 3rd capability is used to beautifully model state machines. 第三种功能用于精美地建模状态机。 For example you can say (in plain akka actors) become(active) and then become(allowOnlyPrivileged) , to switch between receive implementations - making finite state machine implementations (we also have a DSL for FSMs ) a joy to work with. 例如,您可以说(在简单的akka​​演员中) become(active)然后become(allowOnlyPrivileged) ,在receive实现之间切换 - 使得有限状态机实现(我们还有用于FSMDSL )是一种乐趣。 You can not express this nicely in JDK proxied typed actors, because you can not change the set of exposed methods. 您无法在JDK代理类型的actor中表达这一点,因为您无法更改公开方法的集合。 This is a major down side once you get into the thinking and modeling using state machines. 一旦您使用状态机进行思考和建模,这是一个重大的缺点。

A New Hope (Episode 1): Please do have a look at the upcoming akka-typed module authored by Roland Kuhn (preview to be included in the 2.4 release soon), I'm pretty sure you'll like what you'll find there typesafety wise. 新希望 (第1集):请看看即将推出的由Roland Kuhn撰写的akka类型模块 (预览很快将包含在2.4版本中),我很确定你会喜欢你会发现的安全明智。 And also, that implementation will eventually be even faster than the current untyped actors (omitting impl details here as the answer got pretty long already - short version: basically we'll remove a load of allocations thanks to the new implementation). 此外,该实现最终将比当前的无类型演员更快(省略了impl细节,因为答案已经很久了 - 短版本:基本上我们将删除大量分配,这要归功于新的实现)。

I hope you'll enjoy this thorough answer. 我希望你会喜欢这个彻底的答案。 Feel free to ask follow up questions in comments here or on akka-user - our official mailing list. 欢迎在此处或在akka用户 (我们的官方邮件列表)上的评论中提出后续问题。 Happy Hakking! 哈克!

Typed Actors为您提供了根据域名定义的静态合约 - 您可以将其消息命名(将被委托给底层实施并异步执行)在您的域中有意义的操作,避免在您的域中使用反射part(TypedActors使用JDK Proxies,所以仍然有反射,你只需要担心它,并且你根据传递给活动对象/类型的actor及其的参数获得类型检查文档很清楚,但是我知道对于那些基于角色的并发性而言,新的示例总是有帮助的,所以如果您仍然感到麻烦,请随时提出其他问题/意见。

But do you guys realice that you have a huge number of companies where they don't have the expertise developers, but a big Infra to scale horizontally as much as we need, so performance not always is the best “go for it” but instead be responsive, Message driven, elastic and resilient, which right now thanks to typed actors we have, being used by developers that don't know anything about Akka or Reactive Programing. 但是,你们是否意识到您拥有大量没有专门知识开发人员的公司,而是一个可以根据需要进行水平扩展的大型基础设施,因此性能并非总是最佳的选择,而是具有响应性,消息驱动,弹性和弹性,现在感谢我们拥有的类型演员,被不熟悉Akka或Reactive Programing的开发人员使用。

Don't get me wrong, I'm use pure Akka typed in my day by day, but for delivery teams we have this framework that use typed actors and our consumers use as POJO without know that they are coding in a reactive system. 别误会,我每天都在使用纯Akka类型,但是对于交付团队来说,我们拥有使用类型化actor的框架,而我们的使用者则用作POJO,却不知道他们在反应式系统中进行编码。 And that's awesome feature. 这是一个很棒的功能。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM