15

I am wondering, are there any languages allows you to add/delete/update any class on the fly without reloading whole application? (Provided that I can accept some inconveniences like making sure that there is no methods running at the moment + some extra effort to 'migrate' class data members).

Web applications where you replace 1 file and it is used on the next client request is not what I need (like Perl, PHP). Application must be continuously running, and it have some internal state.

Other requirements are

  1. No GIL or similar issues preventing from utilizing SMP
  2. Preferably - existence of JIT-like VM (i.e. where performance is close to native code). Ideal solution would be to be able to reload module in CLang or any other LLVM-based language. It would be just perfect.

About the answers already made:

  • .NET/Java is not suitable - they both have too bulky VM's, and significant part of app will be running on Linux.
  • Erlang - looks like it's possible, but it's terrible for my naked eye, I just cannot look calm at it's if's, case's and strings. Also, I would prefer to avoid transfering bare sources to clients, compiled bytecode would be much better.
BarsMonster
  • 6,483
  • 2
  • 34
  • 47

15 Answers15

16

Erlang was designed to support hot code swapping as one of its high availability features.

geekosaur
  • 59,309
  • 11
  • 123
  • 114
  • 1
    Erlang is indeed a good answer to the question. It even, contrary to the original askers knowledge, compiles to bytecode you can ship, so source is not on client machines. – I GIVE CRAP ANSWERS Mar 21 '11 at 16:40
5

Objective-C might fit the bill. You can use the functions documented here to add new classes and swap method implementations at runtime, and you can load new NSBundles with additional classes or categories on existing classes if additional implementations are required. GNUStep, while not implementing all of the recent Apple additions to the language, does claim to implement these features (see [1] and [2]).

Anomie
  • 92,546
  • 13
  • 126
  • 145
  • Not really. You cannot add instance variables. This is explained by Pieter Schoenmakers in his 1999 thesis. See http://gerbil.org/tom/ – Stephan Eggermont Mar 22 '11 at 09:30
  • You can add instance variables by creating a subclass, of course. For an existing class, you can use `objc_setAssociatedObject` to "fake" it easily enough. – Anomie Mar 22 '11 at 11:26
5

The following are generally considered dynamic languages:

  1. ActionScript
  2. BeanShell
  3. Common Lisp and some other Lisps
  4. Groovy
  5. JavaScript
  6. VBScript
  7. Matlab
  8. Lua
  9. Objective-C
  10. Perl
  11. PHP
  12. Python
  13. Ruby
  14. Smalltalk
  15. Tcl
  16. ...

Some of these languages are supported in the .NET Framework by the Microsoft Dynamic Language Runtime.

Andrei Andrushkevich
  • 9,905
  • 4
  • 31
  • 42
3

What type of application are you trying to write? On what platform?

The question of GUI vs. Server may rule things out as will linux vs. windows.

The following languages are dynamic:

  • Smalltalk
  • Perl
  • JavaScript
  • VBScript
  • Ruby

Modern JavaScript is currently in an arms race to be as fast as possible, so should be pretty quick on any platform.

Ben
  • 34,935
  • 6
  • 74
  • 113
  • 1
    JS doesn't have classes though, so literally speaking... that is not to say prototype-based OOP isn't very neat. –  Mar 12 '11 at 17:51
  • Being dynamic is only 10% of the problem. I don't know a good way of updating class on-the-fly in these languages, so that already created instances would work as usual. Platform is Win/Linux. It's distributed server-client application, no gui. – BarsMonster Mar 12 '11 at 17:54
  • I am not clear from what you wrote: Do you want all instances of a class to change when the class is updated, or do you want only new instances to change. Don't say "both".... :-) Also why can't the client restart periodically to upgrade? Wouldn't that solve the problem at the client end? – Ben Mar 13 '11 at 09:04
  • Both ;-) Client restart would be cheap solution (or something like 'serialize everything and restart, then deserialize'), but I indeed want to have transparent updates on-the-fly. – BarsMonster Mar 14 '11 at 23:56
  • Hey, go with the cheap solution! Why pay more? Do the "Dumbest thing that could possibly work" remember... – Ben Mar 15 '11 at 08:30
  • @Ben: of course both. All smalltalks do 1, Gemstone also does 2. With convert on access. – Stephan Eggermont Mar 21 '11 at 11:58
  • another + for JavaScript, it just rocks! I also agree with the 'dumbest thing taht could work' comment. – Zlatko Mar 21 '11 at 13:03
3

Python can do this. Note the following:

Matt Joiner
  • 112,946
  • 110
  • 377
  • 526
  • I would really need threads, I need all data shared between them, and it's much harder for processes. Also, one cannot safely start 1000 processes. As IronPyton & JPython does not work for me, I've checked PyPy and found out that it also suffers from GIL in order to not break compatibility with existing C libraries. – BarsMonster Mar 17 '11 at 10:25
  • @BarsMonster: Without knowing what the problem you have is, I'll add that your point only stands for CPU-intensive, algorithmic-related data sharing, such as heavy parallel computation, or you're on Windows. You should consider C if this is the case, and wrap your computation in a module for use by higher-level languages. All other cases are only begging for process-based parallelization: http://stackoverflow.com/questions/3609469/what-are-the-thread-limitations-when-working-on-linux-compared-to-processes-for-n/3705919#3705919 – Matt Joiner Mar 17 '11 at 21:51
  • @BarsMonster: What JUST MY correct OPINION has said is quite true. Keep in mind that processes and threads are interchangeable in most Unix schedulers. 1000 processes using the same code segments will get much better treatment from the OS, and removes all need for excessive concurrency primitives. – Matt Joiner Mar 21 '11 at 14:38
3

I have did some research on zero downtime service migration recently. And my solution is not a language-relative one. Here is the idea, we can dump the state of current service, create another process, transfer the connection state description to the new process, finally terminate the old process. As shown in following diagram:

enter image description here

With a well defined abstract service description format and migration protocol, you can migrate any kind of services from one process to another, which means, you could write a server in C++, and migrate the service to the new process written in Python without any disconnection. Of curse, you can migrate your service from the old version to the new version. Adding/Deleting/Updating classes won't be a problem. For more detail, you can reference to my article

Zero-downtime service migration

The difficulty of this kind of technique is that you have to dump the all state of the running service and load them on another process. For most libraries out there you could find, it is hard to get internal state of those classes, which means you may have to do some hack on them, or write your own library. It would be a nightmare to transfer service state for complex services, but for simple services, that's not a big deal.

Fang-Pen Lin
  • 13,420
  • 15
  • 66
  • 96
3

We do this with a Seaside smalltalk webapplication running on the free version of Gemstone. Gemstone has been doing this for the past 20 years or so, so they have everything you need. Some of the high-availability features are not free.

Open source smalltalks don't have the extensive class version/migration gemstone has. The simple 'load a new version and migrate all instances' works with all smalltalks.

Stephan Eggermont
  • 15,847
  • 1
  • 38
  • 65
2

Take a look at Scheme. You can do object-oriented programming in Scheme using very simple extensions, such as the Berkeley extensions. Just extend the code to allow for replacing methods (should be very easy) and you can hot-swap them however you want -- the syntax would still stay simple because, well... it's Scheme. :)

Right now, the code for the classlooks something like:

(define-class (person name)
  (method (greet) (print `Hello!))
  ...)

where person is a lambda. It should be pretty easy to change the define-class macro to make person a list, for example, so that you can add to or remove from it dynamically.

user541686
  • 205,094
  • 128
  • 528
  • 886
2

The Dart VM has great hot code reload support which greatly improves the Flutter development experience.

https://github.com/dart-lang/sdk/wiki/Hot-reload

Günter Zöchbauer
  • 623,577
  • 216
  • 2,003
  • 1,567
1

Java can do this with its debugging interface

http://download.oracle.com/javase/6/docs/platform/jvmti/jvmti.html#RedefineClasses
http://download.oracle.com/javase/6/docs/platform/jvmti/jvmti.html#RetransformClasses

or slightly older:

http://download.oracle.com/javase/1.4.2/docs/guide/jpda/enhancements.html#hotswap

  • This looks very very juicy, the only problem is slight personal incompatibility with bulky JVM :-) – BarsMonster Mar 12 '11 at 18:13
  • It is possible to do hotswap/redeploy even more things using 3rd party utility, http://www.zeroturnaround.com/jrebel/ – mindas Mar 15 '11 at 16:23
1

Depending on the specifics of your project - Javascript might be the answer via Node.js (nodejs.com) which allows you to program an event based server using javascript that is interpreted by the V8 engine.

This approach can be ver efficient compared to traditional web-servers in circumstances where there are many connections at one time, and especially if there is a lot of idling around for the server. This is due to the event based nature of Javascript where the cost of idling is very low.

There are several methods for hot-swapping code using node.js - this should get you started: Node.Js in Erlang style? and https://github.com/kriszyp/nodules

Community
  • 1
  • 1
Billy Moon
  • 57,113
  • 24
  • 136
  • 237
0

Objective-C allows you to hot swap code, and there is a plugin which allows that. I answered similar question about Objective-C here

Community
  • 1
  • 1
Bartosz Hernas
  • 1,130
  • 1
  • 9
  • 17
0

Smalltalk can do it naturally, Common Lisp (CLOS) with a couple of tricks.

SK-logic
  • 9,605
  • 1
  • 23
  • 35
0

You could look through the list at http://en.wikipedia.org/wiki/List_of_programming_languages_by_category#Reflective_languages. One that I don't see mentioned here yet is Lua, which has a reputation as being fast compared to other dynamic languages.

Another strategy might be to look at the academic research. A possible starting point is http://scholar.google.com/scholar?q=ksplice, which is about patching a running linux kernel.

I'm not sure what degree of automation you're looking for. Obviously the general case of seamlessly replacing a running instance of program A with A' is difficult, even with some guarantees on what is allowed to change in A'.

Depending on how the pieces of the program that need to be updated can be grouped and isolated, you could put them in a shared library, and (re-)load the shared library at run time (using e.g. the dlopen family of functions if you're on unix).

Andy
  • 4,789
  • 23
  • 20
0

you should use php for this, i too have a linux server, it is very good with permission to change files like i have this php code to open a text box with an editable on site file,

<?php 
$fn = "test.txt"; //the path to any file

if (isset($_POST['content'])) 
{ 
    $content = stripslashes($_POST['content']); 
    $fp = fopen($fn,"w") or die ("Error opening file in write mode!"); 
    fputs($fp,$content); 
    fclose($fp) or die ("Error closing file!"); 
} 
?> 
<h4>You are editing <?php echo $fn ?> </h4>
<form action="<?php echo $_SERVER["PHP_SELF"] ?>" method="post"> 
    <textarea rows="25" cols="40" name="content"><?php readfile($fn); ?></textarea>
    <br/>
    <input type="submit" value="Save">  
</form>
Trevor Rudolph
  • 1,055
  • 4
  • 18
  • 42