omniscale / imposm3

Imposm imports OpenStreetMap data into PostGIS
http://imposm.org/docs/imposm3/latest/
Apache License 2.0
723 stars 158 forks source link

Retry query on bad connection error #170

Open ear7h opened 6 years ago

ear7h commented 6 years ago

Context

Imports of osm planet can take hours or days and a single error can take down the import process. Something like the database not existing or open makes sense, but a bad connection should result in retries.

[Jul  2 04:04:47] [PostGIS] SQL Error: driver: bad connection in query COPY "import"."osm_buildings" ...

Expected Behavior

On a sql error in the middle of the import, the query should be run again N times (command line flag?) with with an increasing delay between them, or a constant specified amount of time (command line flag?).

Actual Behavior

The process exists.

Possible Fix

The error handling mechanism should differentiate between fatal errors and retry-able errors. And implement a higher order function to take care of this.

func RetryN(n int, fn func(n int) error) error {
    for i := 0; i < n; i ++ {
        err := fn(i)
        if err == nil {
            return nil
        }
        log.Warnf("retry %d, %v", n, err)
    }
}

using this function might be

err := RetryN(argRetry, func(n int) error {
    _, err := sql.Query(.....)
    if err != nil {
        time.Sleep(time.Duration(n) * time.Second)
    }

    return err
}
if err != nil {
    log.Fatal(err)
}

Context

It requires re-import.

Your Environment

olt commented 6 years ago

It's not that easy. Import is all done in a single transaction (with a COPY FROM data stream). If the connection is dropped, the transaction is lost and Imposm can't continue with the import.