digidem / osm-p2p-db

Peer-to-peer database for OpenStreetMap data
BSD 2-Clause "Simplified" License
237 stars 25 forks source link

Add .close() method #51

Closed danielfdsilva closed 7 years ago

danielfdsilva commented 7 years ago

We have a very specific use case for the osm-p2p-db. The main process that runs the server creates a database. Subsequently, when we try to connect to the database from a different node process, we get a Resource temporarily unavailable because of the LOCK.

Looking through the documentation I wasn't able to find any way to close the db connection. What would be the best way to go about this?

Cc @olafveerman @gmaclennan

gmaclennan commented 7 years ago

Hi @danielfdsilva,

If you're using osm-p2p-db (as opposed to osm-p2p - a simple wrapper) then you have control of the db instances you pass to the constructor, namely the hyperlog, the leveldb for join indexes, and the abstract-chunk-store (used for the kdb spatial index).

Each of these stores has a close() method usually, that takes a callback. You basically need to close each one yourself. It would result in problems trying to do this in osm-p2p-db because you control the data stores that are passed to osm-p2p-db and they could vary with their methods for closing.

This should be documented better somewhere, and I don't know if their is a better solution.

hackergrrl commented 7 years ago

This makes me realize that osm-p2p really ought to have a close() method.

danielfdsilva commented 7 years ago

@gmaclennan I'm actually using osm-p2p, since it prepares all the databases needed to work with osm-p2p-server.

I tried to access the different databases from the instance returned by osm-p2p, but I still get the LOCK error. I'm pretty sure I'm missing some.

I've tried:

db.db.close()
db.log.db.close()
db.kdb.db.close()

Any pointers at which I should be targeting?

hackergrrl commented 7 years ago

Run .close() on:

(Fun caveat: db.kdb.kdb doesn't exist at creation time; you need to wait a few ticks for it to silently set itself up async in the background.)


Looking at how much source code digging was necessary in order for me to figure that out, it seems a bit unreasonable that osm-p2p and osm-p2p-db don't expose close() methods.

danielfdsilva commented 7 years ago

@noffle That seems to have done it! thanks :)

hackergrrl commented 7 years ago

Fixed in 4.2.0 :tada: