Skip to content

Extract GraphHopperRouter from GraphHopper - #1901

Merged
easbar merged 21 commits into
masterfrom
gh_router
May 28, 2020
Merged

easbar merged 21 commits into
masterfrom
gh_router

Conversation

@easbar

@easbar easbar commented Feb 12, 2020

Copy link
Copy Markdown
Member

Work towards #1895.

The goal here is extracting everything that is related to processing routing requests from GraphHopper so it can be used (and tested) standalone. The idea is that we first need to load the GH data (GraphHopperStorage mostly, but also LocationIndex and LandmarkStorage which are currently not managed by GraphHopperStorage). But this loading has nothing to do with the actual routing. The new GraphHopperRouter here instead uses the loaded data and only deals with performing the routing. There are a few todos left in the code, but I think you already get the idea.

# Conflicts:
#	core/src/main/java/com/graphhopper/GraphHopper.java
@easbar

easbar commented May 27, 2020

Copy link
Copy Markdown
Member Author

I updated this to current master. I also extracted a WeightingFactory here: b11f5ef, because we need a thing that creates weightings not only when handling routing requests, but also during preparation (which is still handled in GraphHopper) (and map-matching and isochrones as well...).

Extracting the GraphHopperRouter is certainly not the end of this 'refactoring' story, but its a good first step (depending on the direction we want to be going). Shall we already merge this?

* This method calculates the alternative path list using the low level Path objects.
*/
public List<Path> calcPaths(GHRequest request, GHResponse ghRsp) {
return createGraphHopperRouter().route(request, ghRsp);

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I basically took this code and put it into a new class...

Comment on lines +69 to +70
private final CHPreparationHandler chPreparationHandler;
private final LMPreparationHandler lmPreparationHandler;

@easbar easbar May 27, 2020 •

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Similar to GraphHopper these PreparationHandlers are concerned with both the 'import' and the 'algorithm creation' (when routing), but they are mostly about preparation and I definitely want to get rid of them here. -> They are gone now :)

@easbar
easbar marked this pull request as ready for review May 27, 2020 19:48

@easbar easbar left a comment

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I took this a bit further now:

  • CH/LMPreprarationHandler contained the disablingAllowed flags, but other than this the preparation handlers were only concerned with preparing the landmarks and CH graphs. So I moved the flags into RoutingConfig. RoutingConfig now contains all (and only) the properties that can be changed between imports.

  • I removed all the setters in GraphHopper that only delegated to setters of RoutingConfig. Instead the same can be done like hopper.getRoutingConfig().setXYZ() now. GraphHopper now is only concerned with importing, loading and storing stuff. Everything related to routing is delegated to GraphHopperRouter which assumes fully loaded data structures. The only things that are still left are the translation map and the path details builder factory (I am not sure yet what to do with these...).

Comment on lines +988 to +991
Map<String, CHGraph> chGraphs = new LinkedHashMap<>();
for (CHProfile chProfile : chPreparationHandler.getCHProfiles()) {
String chGraphName = chPreparationHandler.getPreparation(chProfile.getProfile()).getCHConfig().getName();
chGraphs.put(chProfile.getProfile(), ghStorage.getCHGraph(chGraphName));

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

See my comment about the GraphHopperRouter constructor.

Comment on lines +65 to +68
// todo: these should not be necessary anymore as soon as GraphHopperStorage (or something that replaces) it acts
// like a 'graph database'
private final Map<String, CHGraph> chGraphs;
private final Map<String, LandmarkStorage> landmarks;

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

GraphHopperRouter needs a way to obtain the CH/LM helper structures for a given profile. Once we have it, some kind of 'graph-database' could replace this, but for now I am simply using these maps.

Comment on lines -101 to -103
public final boolean isDisablingAllowed() {
return disablingAllowed || !isEnabled();
}

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This was a bit weird. The default for LM disabling allowed was false, but still this returned true whenever there were some LM profiles. -> I changed the default for lmDisablingAllowed to true in RouterConfig now. Is this ok?

@easbar easbar May 28, 2020 •

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Or should we set it to false now? Seems a bit more simple to me to have both set to false :) (We would probably have to adjust this in a few tests, but what about production?)

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hm I'll leave it as it is for now we can change the default still later if we think its better.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, this could work too. The (hopefully ;)) only requirement for production is that the behaviour of ch.disable=true does not depend on if there are LM profiles or not (as we have clusters with CH and LM but also with CH and without LM). An additional assumption there is that no one uses lm.disable=true and only ch.disable=true/false.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok I think ch.disable=true does not depend on the existence of LM profiles, but anyway for now it seems easier to not change it :)

if (!hopper.getCHPreparationHandler().getNodeBasedCHConfigs().isEmpty()) {
CHConfig chConfig = hopper.getCHPreparationHandler().getNodeBasedCHConfigs().get(0);
CHGraph lg = g.getCHGraph(chConfig);
CHGraph lg = g.getCHGraph(chConfig.getName());

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I changed these, because getting a CH graph should be done by passing its name, not the entire CHConfig (which I want to get rid of anyway).

this.chGraph = ghStorage.getCHGraph(chConfig);
chGraph = ghStorage.getCHGraph(chConfig.getName());
if (chGraph == null)
throw new IllegalArgumentException("There is no CH graph '" + chConfig.getName() + "', existing: " + ghStorage.getCHGraphNames());

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we throw this inside ghStorage.getCHGraph?

@easbar easbar May 28, 2020 •

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good question :) Previously there was an IllegalStateException inside getCHGraph(), but what about the case where we get some profile via the web UI and use this to get the CH graph? In this case we do not want a 500 error, so we would have to catch the IllegalStateException and convert it into a (web api friendly(ier)) IllegalArgumentException, but this very ugly. So we could change this into an IllegalArgumentException instead, but this only means that the concerns of the web layer further propagate into the core (especially if the error message contains web layer stuff). I thought returning null is a cleaner (layer independent) way to communicate that the corresponding CHGraph does not exist. I would even use a (checked) CHGraphDoesNotExistException to make this very explicit, but I know you don't like it and we had this discussion before :) Or we use a dedicated but unchecked exception...

To summarize:

  • The IllegalStateException used before seems very wrong to me, because it does not even allow asking GraphHopperStorage for a wrong CHGraph name. You have to know it before calling the method, but if this shall turn into something like a 'graph-database' there should be a clean way to find out something does not exist.
  • Using IllegalArgumentException is slightly better, especially because we use this quite frequently so far. But what about the error message? Its hard to make this independent from the layer the method is used in. And catching and converting an IllegalArgumentException also seems wrong. In this case it would be better to use a dedicated exception (does not matter much if its checked or not).
  • Using null leaves it up to the calling code how to handle the exceptional case (this is what I decided to do here)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah. Thanks for these insights. So we fetch getCHGraph to know if a ch graph exists? Couldn't we do this with the profile map or when is it required?

but I know you don't like it and we had this discussion before :)

Haha. Yeah. But every rule can have its exceptions ;)

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So we fetch getCHGraph to know if a ch graph exists?

No, we need the actual graph (here and in other places).

Haha. Yeah. But every rule can have its exceptions ;)

No I think there is no need for this. Its a matter of style and since we aren't using checked exceptions there is no real need to start doing it. Sorry mentioning checked/unchecked was a bit distracting here. This is more about error handling between different parts of the code base. And I guess the question was whether we should use return values like null, or standard exceptions (like IllegalArgument) or dedicated exceptions (like CHGraphDoesNotExist) to indicate such exceptional conditions.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Using IllegalArgumentException is slightly better, especially because we use this quite frequently so far. But what about the error message? Its hard to make this independent from the layer the method is used in.

Are there currently different exception handling cases for ghStorage.getCHGraph==null where we have different messages?

(Because if there is a bug then the now unchecked cases like in createRouter will throw a NPE)

@easbar easbar May 28, 2020 •

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are there currently different exception handling cases for ghStorage.getCHGraph==null where we have different messages?

We don't know what GraphHopperStorage might be used for so we cannot know what a good error message would be. Actually I think we do not want to know this because GraphHopperStorage is not meant to be used for one particular use-case. This is kind of my point here, it should be up to the caller of the method what to do if the CH graph does not exist. In case the CH graph does not exist and we use the result we get an NPE yes, but before we got an IllegalStateException (I do not think either one is better). Using null forces the caller to handle it unless she is sure this case cannot happen (compared to a checked exceptions where we also have to handle it (and the compiler tells us) if we are sure we do not need to). Ok, the NPE will not produce a useful error message, so this would be an advantage of using a dedicated exception.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

like in createRouter

In createRouter there cannot be an NPE, because we use a profile name that we got from GraphHopperStorage (but yes this is hard to see and not so easy to reason about).

@easbar easbar May 28, 2020 •

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah but maybe we are talking about two different things... Have a look at Router#getAlgorithmFactory

   CHGraph chGraph = chGraphs.get(profile);
            if (chGraph == null)
                throw new IllegalArgumentException("Cannot find CH preparation for the requested profile: '" + profile + "'" +
                        "\nYou can try disabling CH using " + Parameters.CH.DISABLE + "=true" +
                        "\navailable CH profiles: " + chGraphs.keySet());

Here we get a profile name from the user and throw an error if this profile name does not work with CH. The same error message would not make sense in GraphHopperStorage#getCHGraph. But to make this even more complicated: This code no longer even takes the CH graph from GraphHopperStorage (but it did before), because now I have this Map<String, CHGraph> (see the comment in the Router constructor if you wonder why).

So I could also revert the change in getCHGraph and throw an exception there again, but still my point is that it should be possible to find out a CH graph does not exist in a clean way (without catching IllegalStateException).

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

But I can see why you want to prevent NPE, so my next suggestion would be an (unchecked) CHGraphDoesNotExistException.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So I could also revert the change in getCHGraph and throw an exception there again

ok

But I can see why you want to prevent NPE, so my next suggestion would be an (unchecked) CHGraphDoesNotExistException

Let's keep it like it is :) (returning null)

@easbar
easbar merged commit bcf033f into master May 28, 2020
@easbar
easbar deleted the gh_router branch May 28, 2020 15:04
@karussell karussell added this to the 2.0 milestone May 28, 2020
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.

2 participants