Skip to content

ArrayIndexOutOfBoundsException encountered with flushing properties to disk with large number of StringEncodedValues. #3102

Closed

Description

@kbrackney

Describe the bug

ArrayIndexOutOfBoundsException encountered with flushing properties to disk when using a StringEncodedValue with large number of entries (768K distinct values). Graphhopper is being used as an embedded Java library.

A custom tag representing the closest road network weather station was included for each way in the OSM datafile and encoded as a StringEncodedValue.

        GraphHopper hopper = new GraphHopper();
        hopper.init(config);

        log.debug("Loading data from source: {}", source);
        hopper.setOSMFile(source);       
        hopper.setImportRegistry(new DefaultImportRegistry() {
            @Override
            public ImportUnit createImportUnit(String name) {

                ImportUnit importUnit = super.createImportUnit(name);
               
                if (WeatherStation.KEY.equals(name))
                    return ImportUnit.create(name, props -> WeatherStation.create(),
                            (lookup, props) -> new WeatherStationParser(
                                lookup.getStringEncodedValue(WeatherStation.KEY)));

                return importUnit;
            }
        });

For a small network, the encoded value works and is retrieved just fine. However, for a large graph representing North America, the import of the OSM file fails when StorableProperties are being flushed to disk.

Expected behavior

Graph is serialized to graph-cache directory.

System Information
Operating system:
macOS 14.6.1
JVM:
openjdk 21.0.5 2024-10-15
OpenJDK Runtime Environment Homebrew (build 21.0.5)
OpenJDK 64-Bit Server VM Homebrew (build 21.0.5, mixed mode, sharing)

GraphHopper:
version 10.0|2024-11-04T16:04:43Z (9,23,7,5,2,9)

Screenshots & Logs

Caused by: java.lang.ArrayIndexOutOfBoundsException: Index 1 out of bounds for length 1
        at com.graphhopper.storage.RAMDataAccess.setBytes(RAMDataAccess.java:241) ~[graphhopper-core-10.0.jar:na]
        at com.graphhopper.storage.StorableProperties.flush(StorableProperties.java:71) ~[graphhopper-core-10.0.jar:na]
        at com.graphhopper.GraphHopper.flush(GraphHopper.java:1423) ~[graphhopper-core-10.0.jar:na]
        at com.graphhopper.GraphHopper.process(GraphHopper.java:831) ~[graphhopper-core-10.0.jar:na]
        at com.graphhopper.GraphHopper.importOrLoad(GraphHopper.java:774) ~[graphhopper-core-10.0.jar:na]

Error occurs here:

Activity

  1. kbrackney commented on Jan 11, 2025

    @kbrackney
    Author

    Code to encode the tag ref:weather as encoded value weather

    src/main/java/ai/moove/routing/ev/WeatherStation.java

    package ai.moove.routing.ev;
    
    import com.graphhopper.routing.ev.StringEncodedValue;
    
    import lombok.Data;
    
    @Data
    public class WeatherStation {
    
        public static final String KEY = "weather";
    
        private final String name;
    
        public static StringEncodedValue create() {
            return new StringEncodedValue(KEY, 1000000, false);
        }
    
    }
    

    src/main/java/ai/moove/routing/util/parsers/WeatherStationParser.java

    package ai.moove.routing.util.parsers;
    
    import com.graphhopper.reader.ReaderWay;
    import com.graphhopper.routing.ev.EdgeIntAccess;
    import com.graphhopper.routing.ev.StringEncodedValue;
    import com.graphhopper.routing.util.parsers.TagParser;
    import com.graphhopper.storage.IntsRef;
    
    public class WeatherStationParser implements TagParser {
    
        public static final String KEY = "ref:weather";
    
        private final StringEncodedValue weatherEnc;
    
        public WeatherStationParser(StringEncodedValue weatherEnc) {
            this.weatherEnc = weatherEnc;
        }
    
        @Override
        public void handleWayTags(int edgeId, EdgeIntAccess edgeIntAccess, ReaderWay way, IntsRef relationFlags) {
            String station = way.getTag(KEY, "");
            weatherEnc.setString(false, edgeId, edgeIntAccess, station);
        }
     
    }
    
  2. kbrackney commented on Feb 6, 2025

    @kbrackney
    Author

    Any comment on this as its occurring in a low level function to flush properties to disk?

    Caused by: java.lang.ArrayIndexOutOfBoundsException: Index 1 out of bounds for length 1
            at com.graphhopper.storage.RAMDataAccess.setBytes(RAMDataAccess.java:241) ~[graphhopper-core-10.0.jar:na]
            at com.graphhopper.storage.StorableProperties.flush(StorableProperties.java:71) ~[graphhopper-core-10.0.jar:na]
            at com.graphhopper.GraphHopper.flush(GraphHopper.java:1423) ~[graphhopper-core-10.0.jar:na]
            at com.graphhopper.GraphHopper.process(GraphHopper.java:831) ~[graphhopper-core-10.0.jar:na]
            at com.graphhopper.GraphHopper.importOrLoad(GraphHopper.java:774) ~[graphhopper-core-10.0.jar:na]
    
  3. otbutz commented on Feb 7, 2025

    @otbutz
    Contributor

    // TODO at the moment the size is limited to da.segmentSize() !
    byte[] bytes = sw.toString().getBytes(UTF_CS);
    da.setBytes(0, bytes, bytes.length);
    da.flush();

    That TODO looks suspicious 😉

  4. kbrackney commented on Feb 15, 2025

    @kbrackney
    Author

    @otbutz thanks for the extra pair of eyes; missed that StorableProperties used a separate segment size hard coded in the constructor.

    Would it be advisable that StorableProperties be updated to leverage the same graph.dataaccess.segment_size configuration parameter and default to the local value if not provided (or set to -1)?

  5. otbutz commented on Feb 25, 2025

    @otbutz
    Contributor

    I'm running into the same problem now, as GraphHopper is unable to complete the import when a large number of CH profiles are configured 🙈

  6. easbar commented on Feb 25, 2025

    @easbar
    Member

    to complete the import when a large number of CH profiles are configured

    How many?

  7. otbutz commented on Feb 25, 2025

    @otbutz
    Contributor

    36 profiles

  8. karussell commented on Feb 25, 2025

    @karussell
    Member

    Is it fixed if you use:

    int segmentSize = 1 << 17;

    (instead of 1 << 15)

  9. otbutz commented on Feb 25, 2025

    @otbutz
    Contributor

    @karussell I'd have to rerun the import, but a big enough segment size should work. I'd propose #3129 as a proper fix.

  10. otbutz commented on Feb 25, 2025

    @otbutz
    Contributor

    Rerun the import using #3129 and the resulting properties file is 64KB large as it requires two DataAccess segments. Judging from the properties.txt the encoded values are the main culprit as each profile also creates a subnetwork EV.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions