Repository navigation
Java/Janino scripting to replace our interpreter leading to a breaking change of the format - #2209
Merged
Merged
Conversation
* CustomWeighting: ensure Map is ordered * minor * cleaner junit5 assertThrows * one more assertThrow
…nd make speed factor working too
…se-if clauses and maybe a different variable initialization
# Conflicts: # core/src/main/java/com/graphhopper/routing/weighting/custom/CustomWeighting.java
easbar
reviewed
Jan 8, 2021
easbar
reviewed
Jan 8, 2021
easbar
reviewed
Jan 8, 2021
easbar
reviewed
Jan 8, 2021
easbar
reviewed
Jan 8, 2021
…ffers from way geometry length (cherry picked from commit dfee21c)
9 tasks done
easbar
approved these changes
Jan 18, 2021
Member
Author
58 of 71 tasks
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
This change is intended to make custom profiles more powerful and also more readable. It breaks backward compatibility. It uses a very powerful and fast library called Janino that allows on-the-fly compilation of partial expressions, blocks or whole classes written in Java.
With Janino scripting we can satisfy much more use cases. As soon as custom profiles were released there was often the need to combine expressions like "use factor=0.5 only if road_class is primary AND in a certain area" or "surface is unpaved AND road_class is not unclassified". This is now possible. Still I do not think we should use more power of Janino and keep the scripting simple (expressions only).
The big advantage with Janino scripting is now that we do not have the burden in the future to extend or improve our own interpreter. Performance-wise the routing with Janino scripting is at least of the same speed (for preparation it is even faster!). For query time this sounds a bit strange as the compilation takes some time. But when we assume that a custom profile is re-used for future requests of the same profile the class cache helps (see ExpressionBuilder, but the cache can be disabled). Now the most surprising thing for me was that even without the cache the slow down is not much (20% for LM queries? precise numbers needs to be re-done for the most recent commit). Janino is able to compile many expressions and a whole class in only a few milliseconds. And currently we create a cached version automatically for all server-side profiles due to GraphHopper.checkProfilesConsistency, i.e. for them there is no compilation overhead. btw: our cache is not that bad or complex as the cached object for key X is always the same (unlike for DBs with user data).
We have to add Janino as dependency to the core (1MB). I find this acceptable for what we get.
A custom profile in the current master looks like:
with this branch this gets more readable and you do not need to study the docs and still know what it does:
It is slightly more verbose for the enum case, but also more clear.
And now new things like inequality
!=or AND-expressions&&are possible and all other comparison operators (<=,>, ...). See this example:We could even use (whitelisted) methods in the expression like
road.class.ordinal() < 3.The
multiply byoperation can now be>1for server-side profiles. Note that the custom profile in the query still has the limitation offactor<=1.Also areas are still possible and can be combined with other expressions:
The area check is done only if expressionA is true and should be avoided to be called multiple times.
For some use cases (and sometimes for performance) you can use else-if and else:
Such a block is also what happens for a single encoded value in master branch so you can use this for migration.
Technically Janino compiles CustomWeightingHelper at runtime and adds user expressions to it before (in a safe manner).
Most code changes and tests are necessary to make the janino compilation safe regarding attacks from user expressions. Of course it should not be possible to call System.exit, mine crypto currencies or read the file system on the routing servers that could be exposed to the public. Additionally to this work we could think about enabling the SecurityManager (see here) but this only further increases security and would not be the main protection. The main protection comes from the fact that we allow only "simple" expressions like "if" clauses and enforce that there are no assignments, no loops, no lambdas or other stuff happening in user expressions and have a whitelist of variable names and methods. We do this with the help of the Scanner API from Janino and traverse the code (ExpressionVisitor).
Also we want simple and readable user expressions. For that we need to declare and assign variables before like
road_classand create the class variableEncodedValue<RoadClass> roadClassEncand also make constants likeNOmore specific (convert it intoToll.NOif thetollencoded value is used). The user should not need to do that. With the nice API of Janino I found a safe way to mix arbitrary code with the user expressions. First I create Java objects (List<Java.BlockStatement>) from all the user expressions (it is ensured that they are simple) and inject them into the parsed class CustomWeightingHelper (Java.CompilationUnit) containing arbitrary Java code like assignments and the compile this into byte code, load the class and finally instantiate an object of it.To my knowledge and experiments Janino does not use the Metaspace (unlike javaassist) and instead it creates the classes on the heap.