寻找具有热代码交换功能的编程平台
我目前正在集思广益,思考如何在程序运行时升级它。 (不是在调试“生产”系统时。)
但它需要的一件事是实际将更改的源代码或编译的字节代码提交到正在运行的进程中。
伪代码
var method = typeof(MyClass).GetMethod("Method1");
var content = //get it from a database (bytecode or source code)
SELECT content FROM methods WHERE id=? AND version=?
method.SetContent(content);
首先,我希望实现系统能够在没有面向对象的复杂性的情况下工作。 这导致了以下要求:
- 更改函数的源代码或字节代码
- 删除函数
- 添加新函数
- 更改函数的签名
使用.NET(和其他),我可以通过 IoC 注入一个类,从而可以更改源代码。 但加载会很麻烦,因为所有内容都必须位于程序集中或通过 Emit 创建。 也许使用 Java 这会更容易? 我认为整个类加载器是可替换的。
借助 JavaScript,我可以实现许多目标。 只需评估一个新函数 (MyMethod_V25) 并将其分配给 MyClass.prototype.MyMethod。 我认为人们也可以用“del”以某种方式删除功能
哪个通用平台可以处理这样的事情?
I'm currently brainstorming over the idea how to upgrade a program while it is running. (Not while debugging, a "production" system.)
But one thing that is required for it, is to actually submit the changed source code or compiled byte code into the running process.
Pseudo Code
var method = typeof(MyClass).GetMethod("Method1");
var content = //get it from a database (bytecode or source code)
SELECT content FROM methods WHERE id=? AND version=?
method.SetContent(content);
At first, I want to achieve the system to work without the complexity of object-orientation. That leads to the following requirements:
- change source code or byte code of function
- drop functions
- add new functions
- change the signature of a function
With .NET (and others) I could inject a class via an IoC and could thus change the source code. But the loading would be cumbersome, because everything has to be in an Assembly or created via Emit.
Maybe with Java this would be easier? The whole ClassLoader is replacable, I think.
With JavaScript I could achieve many of the goals.
Simply eval a new function (MyMethod_V25) and assign it to MyClass.prototype.MyMethod.
I think one can also drop functions somehow with "del"
Which general-purpose platform can handle such things?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
我认为任何基于图像的语言都会支持这一点。 我知道 Common Lisp 可以,因为它可能是部署 Lisp Web 应用程序的最常见方法之一,但我怀疑它在 Smalltalk 中的工作原理几乎相同。
I think any image-based language would support this. I know Common Lisp does, since it's probably one of the most common ways to deploy Lisp web apps, but I suspect it would work pretty much the same in, say, Smalltalk.
大多数动态语言都具有此功能。 看看 Ruby:您可以在运行时修改现有方法等。 当 IronRuby 推出时,您也可以在 .Net 平台中执行此操作。
Most of dynamic languages have this capability. Take a look at Ruby: you can modify existing methods etc. at runtime. When IronRuby is out, you will be able to do this also in .Net platform.
我的印象是,现在 Erlang 作为一种具有这种能力的语言是非常明显的。 也就是说,我的岳父(在我看来是一位大师级程序员)告诉我,他在一个有点旧的平台上实现了热插拔代码——他们现在称之为 z/OS(之前的 OS/390)的汇编器。 )。
就我个人而言,我一直在寻找在 Java 领域实现这一目标的方法,目前我的绝大多数专业工作都是在 Java 领域完成的。 在 Javaland,提供热卸载的最广为人知的工作(据我所知)是 OSGi 联盟。 也就是说,由于一些常见 Java 库的架构(例如:JDBC
DriverManager
),该解决方案必然涉及一些类加载器魔法。 如果您选择采用 OSGI 路线,您的代码可能需要进行大量审核和测试,以确保其可用于 OSGi 架构。作为实现热插拔代码的替代方法,也许您可以使用可能更简单的请求队列机制来实现一个似乎具有此功能的系统。 例如,如果您需要热插拔系统中处理大型后端请求的部分,为什么不通过中介发送这些请求,该中介可以将它们分派到正在运行的后端组件,并将它们累积在队列中(如果后端组件正在运行)。组件坏了? 这可能允许您独立于系统的其余部分升级后端组件,而无需重新部署,正如我们在行业中所说的“整个shebang”。
My impression is that right now Erlang is very visible as a language which has this capability. That said, my father-in-law (a master programmer, in my opinion) has told me that he's implemented hot-swappable code on a somewhat older platform -- assembler for what they now call z/OS (OS/390 before that).
Personally, I've been looking for ways to do this in the Java space, where the vast majority of my professional work is currently done. In Javaland, the best publicized effort to provide hot-unloading (as far as I know) is the work done by the OSGi Alliance. That said, this solution necessarily involves some classloader magic because of how some common Java libraries are architected (example: JDBC
DriverManager
). If you choose to go down the OSGI route, your code will likely require extensive auditing and testing to ensure that it will be usable with the OSGi architecture.As an alternative to implementing hot-swappable code, perhaps you could implement a system which appears to have this capability using the potentially simpler mechanism of request queueing. For example, if you need to hot-swap the piece of your system which processes large, backend requests, why not send these requests through an intermediary which can dispatch them to the backend component if it is running and accumulate them in a queue if the component is down? This might allow you to upgrade the backend component independently of the rest of your system without redeploying as we say in the industry "the whole shebang".
使用 JavaScript 就可以做到。 令人惊奇的是,Google 的 V8 引擎是开源的,并且很容易实现到任何 C++ 程序中。
http://code.google.com/p/v8/
当然,您会编写一些库来公开功能并从 JavaScript 内部加载脚本。 这取决于您想做什么。
With JavaScript it can be done. Whats amazing is that Google's V8 engine is open source and is easy to implement into any C++ program.
http://code.google.com/p/v8/
Of course you will have to write a bit of a library to have functionality exposed and loading of the script from inside the JavaScript. It will depend on what you are wanting to do.
Erlang 已经被提及,它使用显式的“同步点”,其中正在运行的例程可以通过使用“?MODULE:routine()”进行“自我”调用来显式更新自身。
这是另一件需要记住的重要事情:您不仅需要虚拟机中能够替换正在运行的代码,而且正在运行的代码还需要一种方法来响应此类更新并进行相应调整。
您可能还想查看UpgradeJ这是一种专门针对这种需求(代码热交换)而设计的语言。
Erlang was already mentioned, and it uses explicit "synchronization points" where a running routine may explicitly update itself by doing a "self" call using "?MODULE:routine()."
This is another important thing to keep in mind: you don't just need the capability in the VM to replace running code, but the running code also needs a way to respond to such updates and adjust accordingly.
You may also want to look into UpgradeJ which is a language that was specifically designed with this requirement (code hot swapping) mind.
Erlang 可以完成您想要的一切,并且它不依赖于任何附加库(除非您将 OTP 算在该类别中)或编码解决方法。 它可以在重新加载过程中维护状态(即命令式语言中的“变量值”),并且不需要缓冲或重新传输事务。 如果您以 OTP 风格编写代码,那么您可以让应用程序程序员编写简单的顺序代码,这些代码将具有一定程度的并行执行能力,并支持热重载,而所有这一切都无需他们担心如何执行的细节完成。 它就是有效的。
Erlang can do everything you are looking for, and it doesn't rely on any bolt-on libraries (unless you count OTP in that category) or coding work-arounds. It can maintain state (i.e. "values of variables" in imperative-language-speak) across the reload, and does not require transactions to be buffered or retransmitted. If you write your code in the OTP style then you can have your application programmers write straightforward sequential code that will have some degree of ability to execute in parallel, and to support hot reload, and all this without themhaving to worry about the details of how it's done. It just works.
对于.NET,这是通过托管可扩展性框架完美实现的。 该框架是在 .NET 4 中引入的。您可以构建可扩展的应用程序。 它可以做 IoC 容器可以做的事情,尽管它可以做更多的事情。 它可以发现先验未知的功能。 此外,您可以更新功能,但您事先并不知道该功能是否会更新。 换句话说,它是一个模块化应用程序的框架,无论您是想要热交换代码块并想要提供不同的功能,还是更新已经存在的功能。
With .NET this is perfectly implemented via the Managed Extensibility Framework. This framework is introduced in .NET 4. You can build extensible applications. It does what an IoC container could do, although it can do on thing more. It can discover functionality which does not know a priori. Additionally you can update functionality, you do not know that is going to be updated a priori. In other words, it is a framework for modular applications, whether you want to hot swap code chunks and want to provide different functionality, or update already existing.
在 Java 中,您有 OSGi 项目,它可以方便地升级和更改应用程序的模块,而无需接触其他模块。
如果您不介意学习一些不同的东西,Erlang 编程语言就是从头开始设计的这种类型的应用程序心里。
In Java, you have the OSGi project, which facilitates upgrading and changing modules of your application without touching other modules.
If you don't mind learning something different, the Erlang programming language was designed from the ground up with this type of application in mind.