@Lucien
2016-03-19T16:04:06.000000Z
字数 6088
阅读 715
摘要:
每周我们都能听到这样的说法,“即将有一种新的技术将改变我们的生活”,正如很多时候,我们听到新的创新将要改变行业一样。Fintech新技术正带来一种难以想象的变革。
正文:
每周我们都能听到这样的说法,“即将有一种新的技术将改变我们的生活”,正如很多时候,我们听到新的创新将要改变行业一样。Fintech(金融科技)新技术正带来一种难以想象的变革。就在前不久QCon伦敦大会的开场Keynote上,Adrian Colyer提到了Pony编程语言:
我们都非常了解数据库和分布式系统在过去的5多年里对彼此的影响很深刻这一事实。而现在,我们也渐渐看到一些关于编程语言的有趣工作,这样的编程语言真的能对世界有价值。比如说,从伦敦帝国学院走出来的Pony编程语言,真是个令人着迷的东西。
很幸运有几位认识的人参加到了这款编程语言的设计当中,Sylvan Clebsch在一次关于本机语言追踪的演讲中建议到,Pony在本质上就很适合FinTech系统,因为在FinTech领域不会有人去编写软件,我们只写时间依赖的事件流处理器,对性能的要求很严格,但不需要正式验证。这些处理器通常都是用Java和C++,当然也会使用包括Scala、C、OCaml、Erlang、R和NumPY在内的语言。
Pony是一个开源的、面向对象的基于Actor模型的安全且高性能的本机编程语言,只不过这样的语言在使用LLVM之前就已经被编译了。Pony提供Sublime和Atom编辑插件,同时很快将提供Vim和Visual Studio支持。可使用LLDB和GDB进行调试。同时,Pony也兼容C程序,其编译器可从Pony库中生成C的头文件,这样C/C++程序可方便调用Pony库。
所谓Actor模型,其实是从大家更为了解的Erlang演化而来的,或者是从最近流行的Akka传播过来的,更早则是出现在了1973年Carl Hewitt和其他人员的一篇论文里。Actor模型是将状态管理与异步方法相结合,可以把Actor看作是一个个独立的实体,它们之间是毫无关联的。但是它们可以通过消息来通信。一个Actor收到其他Actor的信息之后可以根据需求作出各种响应。消息的类型可以是任意的,消息的内容也可以是任意的。这点有点像WebService,只提供接口服务,你不必了解是如何实现的。
一个Actor如何处理多个Actor的请求呢?它先建立一个消息队列,每次收到消息后就放入队列,而它每次也从队列中取出消息体来处理。通常我们都设计这个过程是循环的,让Actor可以时刻处理发送来的消息。Clebsch说,在Pony编程语言里,Actor堆序列有独立的垃圾收集(GC,Garbage Collect),这一点不同于Erlang或Akka,也就是说Actor具备垃圾收集功能,在本质上没有手动内存管理。
众多Actor独立的收集堆序列垃圾,而其它的Actor则使用mark-and-don’t-sweep算法。这也就意味着Pony是基于可达图的,不可达存储不会受到任何影响。Actor堆序列的垃圾收集并没有安全点,也没有读写障碍,无卡表标记,不需要压缩。因为不需要压缩,所以也没必要固定的指针。
或者可以这样理解,通过单一的Actor来收集本地堆序列的行为只是在跟踪可达图。同类事件中的相关工作并不是太多,也就是说当一个Actor在收集自己工作集垃圾的时候,震动数量其实是很低的。
Pony actors have no blocking constructs. They are cheap, having an overhead of 240 bytes when compared to an object, or 156 bytes on a 32-bit architecture such as ARM. They also have no CPU overhead when they are not executing working code. As Clebsch put it, “If an actor has no work to perform, it’s not even in a queue anywhere. The runtime has no knowledge of it, of any kind, unless is has pending work to perform”.
Actors pass messages around using message queues which are intrusive, that is messages do not need to be in more than one queue. More controversially the queues are also unbounded since "if the queue was bounded then, when the queue is full, you have to either block or fail,” Clebsch stated. Blocking can introduce deadlocks, whilst failing would require application-specific error handling every time a message is sent. Bounded queues are used to avoid the back pressure problem but, Clebsch argued, whilst unbounded queues move the back pressure problem they don’t make it worse. At the time of writing the Pony runtime does not do anything to provide generalised back pressure. Clebsch told InfoQ
That's not the end of the world: it's pretty easy to write domain-specific back pressure, such as the back pressure in TCPListener, which stops accepting new connections when the open connection count exceeds a specified number.
In the next couple of months, generalised back pressure will land in the runtime. What this does is automatically deprioritise actors that send to "loaded queues".
Fundamentally the actor model is about expressing concurrency, and dealing with hard concurrency problems is the main area for which Pony has been designed. Key to that design is the type system which is data-race free, concurrency-aware and proven sound. According to Clebsch there are no other languages with mutability and a data-race free type system, though Rust achieves the same thing through a combination of its type system and atomic reference counting.
Pony has no null. The type system is built on algebraic data types so, in that sense, it can be considered a functional language. The following example, from the talk slide deck, shows some code to create an order on a very basic order management system.
The ReadSeq[OrderObserver] iso introduces us to one of the most important, and novel, concepts in the type system. Iso (Isolated) is a reference capability which offers a guarantee that is built on deny properties. It is these reference capabilities (rcaps) which make the type system data-race free.
“It's not what you are allowed to do, it's what their existence proves cannot exist anywhere else in your program statically. So isolated says it denies both local and global aliases which can either read from or write to the object. That’s an incredibly powerful deny guarantee. It means that the most anyone other than you can know about this mutable sequence is its address. They can’t read its field or write to its field. That means it is safe to send it to a new actor even though it remains mutable without locks of any kind.” Clebsch said.
Rcaps are type annotations that indicate a level of isolation or immutability:
x: Foo iso // An isolated Foo
x: Foo val // A globally immutable Foo
x: Foo ref // A mutable Foo
x: Foo box // A locally immutable Foo (like C++ const)
x: Foo tag // An opaque Foo
It’s important to note that data-race freedom using rcaps is handled by the compiler during type checking, which means that there isn't non-linear growth in the amount of compiler work to be done as your code base grows. Colyer provides a fantastic summary of the paper that describes this in more detail on his Morning Paper blog.
Rcaps allow isolated (iso), immutable (val), and opaque (tag) objects to be passed by reference between actors, so you need some way of preventing premature collection of objects in messages (where no actor might have a reference) or where they are reachable by other actors. Pony uses a message protocol for this which is described in a paper which has also been written up by Colyer. The approach is analogous to a consensus algorithm, and Colyer draws parallels with the Chandy-Lamport distributed snapshot algorithm. The Pony paper, "Ownership and Reference Counting Based Garbage Collection in the Actor World" – Clebsch et al. 2015, states
When an actor sends, receives, or drops a reference to an object it does not own, it sends protocol-specific messages to the owner. These protocol-specific messages result in the owner updating its (local) reference count.
Pony is still in very early stages, and some significant items, including reflection and hot code loading, are non-trivial and not yet resolved. That said, whilst a recent survey suggested that the vast majority of users are still just checking out the language, but some are further along. For example Sendence, a NY company, has a FinTech product that they are planning to put into production soon.
Pony is an open-source language and contributions are welcome. There is also a Sandbox so you can try it out yourself.
Actor模型在并发编程中已经得到广泛应用。Java虽然还未提供直接的支持,但是一些开源组织已经提供相关的jar包来模拟实现Actor模型,例如:ActiveJava等,有兴趣的同学可以去了解一下源码http://en.wikipedia.org/wiki/Actor_model
查看英文原文:Using the Actor-model Language Pony for FinTech
http://mt.sohu.com/20160217/n437652333.shtml
http://technews.cn/tag/fintech/
http://www.zhihu.com/question/30106265
http://finance.eastmoney.com/news/1373,20160314603873932.html
http://developer.51cto.com/art/200908/141595.htm
http://janeky.iteye.com/blog/1504125