Repository navigation
Description
Hi there. This is Hao Chen from Ant Financial.
Recently we had some internal discussions on improving the Java API. Before making any change, we'd like to open a wider discussion here on the possible options.
If you're interested in using Ray on Java, your feedbacks are very much valuable. Thanks in advance!
Option 1: Ray.call (Current implementation)
Let's say we have the following method
class Echo {
public static int echo(int x) { return x; }
}To invoke this method remotely on ray, we can do
RayObject<Integer> res = Ray.call(Echo::echo, 1);
System.out.println(res.get());Problems
- Compared with Python API (
echo.remote(1)), this way is less natural, and is somewhat against Java convention. - Its internal implementation is a little bit hacky. (The TLDR explanation is that the implementation uses byte-code manipulation to change function's behavior at runtime. I'm not gonna to go too deeply because I'd like to make this discussion more focused on users' perspective.)
A Potential solution
Java comes with a Annotation Processor API that allows you to generate new source files at compile time.
Note:
- it can only generate new files, cannot modify existing files (*).
- This is done by Java compiler, doesn't need any support from editors or IDEs.
Option 2: EchoRemote
If you annotate the original class with @RayRemote, the annotation processor will generate a remote version:
class EchoRemote {
public static RayObject<Integer> echo(int x) { ... }
public static RayObject<Integer> echo(RayObject<Integer> x) { ... }
}Then you'll use it like this:
RayObject<Integer> res = EchoRemote.echo(1);
System.out.println(res);Here's a prototype of this idea: https://github.com/raulchen/ray_annotation_test
Original file: https://github.com/raulchen/ray_annotation_test/blob/master/annotation_test/src/main/java/test/Adder.java
Call-site: https://github.com/raulchen/ray_annotation_test/blob/master/annotation_test/src/main/java/Main.java
Pros:
- API looks more natural and elegant.
- Implementation is cleaner, safer and easier to debug.
Con
- Some of us don't like the fact that the class name is different.
Option 3: x.y.z.remote.Echo
One way to avoid changing class name would be moving the new class to a different package. Let's say the original package name is x.y.z, the new package will be x.y.z.remote. However,
- If you want to use both classes in the same file, you'll need to write full names, because Java doesn't support name alias.
- Although the names are the same, but the classes are different. It could cause a bug if one wants to compare the classes.
Option N
We'd like to hear your voice if you have a better solution :)
(*): Technically, modifying existing code is possible, but it needs to use internal API, requires considerable amount of work, and has some effects (related discussion can be found at https://stackoverflow.com/questions/3852091/is-it-safe-to-use-project-lombok). Comparing the benefits and drawbacks, we don't want to list it as an option here.
Activity
raulchen commentedon May 31, 2018
cc @robertnishihara @imzhenyu @eric-jj
pcmoritz commentedon May 31, 2018
Both option 2 and option 3 are clearly better than what is currently there, I prefer option 2 because it seems more convenient for the user (not having to deal with the naming clashes). If the generation of new names is documented, it should be fine, but I'm curious what others think about this.
I'm super glad that we can get rid of the bytecode rewrite :)
ericl commentedon May 31, 2018
Option 2 looks pretty good. If I understand correctly, it's generating a thin wrapper class that delegates to the original right, so there shouldn't be any class name issues.
I assume actors would work the same way?
robertnishihara commentedon May 31, 2018
Does option 2 generate 2^N overloaded functions for each method, where N is the number of arguments?
istoica commentedon Jun 1, 2018
I prefer option 2 as well. Even if the class name is different, the rule change is natural: ClassName -> RemoteClassName, which I don't think is bad.
One important aspect to consider in all these cases is the easy of debugging. I guess option 2 scores pretty well here as well, as it requires no support from IDE and the user will very quickly figure out what the annotation is doing over the hood.
Also, it would be great to give an example for actors as well.
raulchen commentedon Jun 1, 2018
Thanks for the feedbacks :)
@ericl @istoica:
The generated class will be a very thin wrapper that simply calls another
submitTaskfunction.Actors should work very similarly. My current thought is to have another
@RayActorannotation, which generates only non-static methods, whereas@RayRemoteonly generates static methods.@robertnishihara:
Unfortunately, yes. I didn't think of a better solution for this issue in Java.
(Actually, in terms of this point, option 2 is worse than option 1, because it generates 2^N methods for each class.)
To mitigate this issue, my plan is to leave the choice to the users. The annotation will come with an option that let the user choose 1) generate 2^N versions, or 2) only generate RayObject parameters. If the users choose 2), they'll need to wrap their parameters before calling the remote functions. E.g.,
Echo.echo(RayObject.of(1)). This is not ideal, but looks acceptable to me.istoica commentedon Jun 1, 2018
@raulchen Regarding the option to let user choose whether to generate all 2^N call versions maybe not do it initially? Let's first see how bad it is, what is the typical number of arguments people are passing. Also, if you add the option, I think the default should be generating the 2^N versions.
Using @RayActor instead of @rayremote would make the API a bit inconsistent with the Python one. Anyway, please provide a simple example for classes/actors as well. You could use the counter example we are using in Python to illustrate actors, e.g.,
becoming
raulchen commentedon Jun 1, 2018
@istoica Sure. Let's say the original class is
If user annotate it with
@RayActor, the following class will be generated:Then user can use this like this:
PS1: the reason why there're 2 different annotations is because both annotations decorate classes (Java doesn't have standalone functions).
PS2: it makes sense to use 2^N as default. It's also fine to not provide the option first.
istoica commentedon Jun 1, 2018
Thanks @raulchen. I think that give all constraints this is a fine solution.
We should also come up with the API for C++ just to make sure we minimize the inconsistencies across Python, Java, and C++ APIs.
robertnishihara commentedon Jun 1, 2018
Thanks @raulchen. Question, why
RayObject.of(1)instead ofRay.put(1)?raulchen commentedon Jun 2, 2018
@robertnishihara
Ray.putis also fine here, but may not be the best API for parameters.Because
Pay.putlooks more like putting the object into the global object store. But here, this1doesn't have to be in the global object store, because it can be passed by value.Here's my proposal about the
RayObjectAPI:RayObjectis just an abstract interface that has agetmethod:It has 2 sub-classes:
RayGlobalObjectrepresents an object that is in the global object store. It has an UniqueID, and may or may not be locally available.RayLocalObjectis for objects that will only be needed locally, for example, parameters.RayGlobalObjectcan be attained viaRay.put,RayLocalObjectcan be attained viaRay.param. (Ray.paramis the same as the aboveRayObject.of, but this name should be better because of consistency withRay.put)Thus, users can use either
Ray.putorRay.param, depending on whether they want to put the object in global object store or not.How does this sound to you?
Update:
RayGlobalObject, but we can declare it asRayObjectfor now in case we'll have return-by-value in the future;Ray.paramcan also return aRayGlobalObjectif the object is too large to be passed by value;RayRefObject/RayValueObject, (I have slight preference forRayGlobalObject/RayLocalObject);imzhenyu commentedon Jun 2, 2018
Nice discussion, would like to provide more context for making the best decision here.
Option 1:
Option 2 is good in terms of removing binary rewrite and being consistent with what we have now in Python. However, it has a major drawback that it introduces some 'from-no-where' types that does not preserve the type completeness during development (i.e., an IDE plugin may be confused and fire errors as the remote types are not found anywhere).
Option 3 is better but as discussed, it may cause potential bugs at runtime (esp. when we have third party libraries that depend on the same set of classes).
istoica commentedon Jun 2, 2018
@imzhenyu good points. A few questions:
Thanks.
14 remaining items
raulchen commentedon Jun 6, 2018
@mehrdadn thanks for your feedback.
We tried dynamic proxy as well. But it didn't work very well, because:
mehrdadn commentedon Jun 6, 2018
@raulchen Ah yeah, you're right... now that I'm looking more carefully at how proxies work in Java it seems like their entirely goal is to keep the signatures matching which doesn't help. :\
I think I'm also a fan of Option 2 then. I guess a more idiomatic Java version of it would generate an interface for the user rather than directly giving back a class, but I'm honestly neither sure if it can get much more idiomatic than this, nor what exactly that might buy anyone.
For addressing the 2^N issue though, could you do something like this and figure it out at call-time?
The performance impact should be acceptable I think?
raulchen commentedon Jun 6, 2018
@mehrdadn regarding your suggestion of using Object as arguments, I'm not concerned about perf, but I think it's not ideal to sacrifice type safety.
robertnishihara commentedon Jun 8, 2018
@raulchen I've added mine. It sounds like there is no perfect choice right now, but I assume it will be possible to iterate on the decision if we come up with better ideas.
imzhenyu commentedon Jun 9, 2018
@robertnishihara yes, and in fact I guess we can converge soon as some people in Ant have had some new findings about remote lambdas which may fix the performance issue. Besides, we also have some new requirements (getting annotation information efficiently) which will change the reasoning a little bit that can result a consensus easier. Will have the feedback here once there are some more in-depth results.
robertnishihara commentedon Jun 9, 2018
@imzhenyu interesting, please share the performance numbers for remote lambdas when they are available! What was the issue?
raulchen commentedon Jun 10, 2018
First, Lambda serialization is actually cacheable.
Java compiles each appearance of a method reference to one single instance.
It means, if we do
Ray.callwill get 2 different instances.If we do
Ray.callwill always get the same instance.Thus, to cache serialization, we can simply use the instance as the key. To cache deserialization, we can just use the serialized binary as the key.
I did a benchmark on my Macbook Pro.
And the serialized binary is ~600 bytes.
Conclusion:
With cache, the perf of remote lambda is totally okay.
If we remove binary rewriting and only use lambda, Java worker code can be significantly sanitized. Then I think Option 1 is also okay to me.
Note: the approach @imzhenyu mentioned above (proposed by @mylinyuzhi) is actually a method to get the info of the original method. Considering the perf of cache is already very good, I think this is not very necessary to use it for transferring functions. But it can be a good approach to get meta data of annotations at runtime.
robertnishihara commentedon Jun 10, 2018
Thanks @raulchen, that sounds good! Is this something that would only be used with Solution 1? Or would it also be used in conjunction with solution 2?
raulchen commentedon Jun 11, 2018
@robertnishihara yep, it will only be used for solution 1.
raulchen commentedon Jun 13, 2018
@mylinyyuzhi just implemented the above-mentioned solution in #2245.
This PR removes binary rewrite, and significantly simplified Java code base (without sacrificing perf, see commit message).
I think, then, Option 1 should be an okay solution for everybody.
I'll close this issue once #2245 is merged.
istoica commentedon Jun 13, 2018
Is this the Ray.call() solution?
This solution is still hard to read as Ray.call() doesn't match the syntax of the class declaration like solution 2 does, but still ok.
It would be great to ask the opinion of other Java developers outside Ant regarding solution 1 vs 2.
raulchen commentedon Jun 13, 2018
@istoica
yes, that is still
Ray.call.I share the opinion with you that option 2 looks more idiomatic. But option 1 seems to be the only one that everybody would be happy to accept.
Also, I'll keep this issue open to see if we'll hear more voices outside the current group.
istoica commentedon Jun 13, 2018
Well, my main point was about process. In general with APIs we should get feedback from a larger poll of developers because they are the ones we are building the APIs for.
Also, I know that everyone is familiar with this piece about "How to design a good API and why it matters?" but I always find it useful to re-read it ;-): https://static.googleusercontent.com/media/research.google.com/en//pubs/archive/32713.pdf
(And, in general, the best solution is not the one which everyone is least happy about; in general, the best solution is the one that excel in a particular dimension.)
Anyway, let's continue to monitor this and get feedback from other developers.
robertnishihara commentedon Aug 6, 2018
Closing because I think this was done in #2245.