Skip to content

Java/Janino scripting to replace our interpreter leading to a breaking change of the format - #2209

Merged
karussell merged 108 commits into
masterfrom
janino_scripting
Jan 19, 2021
Merged

karussell merged 108 commits into
masterfrom
janino_scripting

Conversation

@karussell

@karussell karussell commented Dec 10, 2020 •

Copy link
Copy Markdown
Member

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:

priority:
  max_height: {"<3.8": 0}

speed_factor:
  road_class:
    motorway: 0.85
    primary: 0.9

max_speed:
  road_class:
    living_street: 2

max_speed_fallback: 80

with this branch this gets more readable and you do not need to study the docs and still know what it does:

priority:
- if: max_height < 3.80
  multiply by: 0

speed:
- if: road_class == MOTORWAY: 
  multiply by: 0.85
- if: road_class == PRIMARY
  multiply by: 0.9
  # if you wish to keep compatibility you apply max_speed after all multiplications, but in general you can use "limit to" at any time
- if: road_class == LIVING_STREET
  limit to: 2
  # replacement for max_speed_fallback which is now implicit
  # it could be also done via the else statement of the last if ... or via:
- if: true
  limit to: 80

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:

priority:
- if: road_class == MOTORWAY && max_speed >= 70
  multiply by: 0.85
- if: road_class != PRIMARY
  multiply by: 0.9

We could even use (whitelisted) methods in the expression like road.class.ordinal() < 3.

The multiply by operation can now be >1 for server-side profiles. Note that the custom profile in the query still has the limitation of factor<=1.

Also areas are still possible and can be combined with other expressions:

expressionA && in_area_custom1: 0.5

areas:
- custom1: <geojson>

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:

priority:
- if: <expressionA>
  multiply by: 0.85
- else if: <expressionB>
  multiply by: 0.6
- else: 
  multiply by: 0.8

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_class and create the class variable EncodedValue<RoadClass> roadClassEnc and also make constants like NO more specific (convert it into Toll.NO if the toll encoded 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.

* CustomWeighting: ensure Map is ordered

* minor

* cleaner junit5 assertThrows

* one more assertThrow
…se-if clauses and maybe a different variable initialization
easbar added 3 commits January 8, 2021 10:18
# Conflicts:
#	core/src/main/java/com/graphhopper/routing/weighting/custom/CustomWeighting.java
Comment thread docs/core/profiles.md Resolved
Comment thread docs/core/profiles.md Resolved
Comment thread docs/core/profiles.md Resolved
Comment thread docs/core/profiles.md Resolved
Comment thread docs/core/profiles.md Resolved
Comment thread docs/core/profiles.md Resolved
Comment thread docs/core/profiles.md Resolved
…ffers from way geometry length

(cherry picked from commit dfee21c)
@karussell
karussell merged commit 13b7d33 into master Jan 19, 2021
@karussell
karussell deleted the janino_scripting branch January 19, 2021 11:22
@karussell

Copy link
Copy Markdown
Member Author

Thanks @easbar for the detailed review!

Some refinements will now happen in follow up issue #2232 before 3.0 release.

@karussell karussell mentioned this pull request Mar 6, 2021
58 of 71 tasks
@karussell karussell changed the title Java/Janino scripting to replace our interpreter Java/Janino scripting to replace our interpreter leading to a breaking change of the format Apr 23, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants