As already ask in the Headline:
How can i check, if an folder contains a valid badgerdb, and how can i check if it’s already open?
Background:
I am programming a microservice and it uses badgerdb to store various information. The service runs as a single instance with a fixed NFS. So far it works. Unfortunately, when redeploying via Kubernetes, the new service is then started as a second instance and the service tries to access the database. Of course, that doesn’t work. When opening the DB, an error occurs, correctly. Unfortunately, there is no way to react properly to this error, except by parsing the message. But I would prefer to know in advance whether a directory contains a database and whether it is already opened by another instance.
If a folder contains a valid badgerDB?
Valid badger db could mean a lot of things. It could mean that all SST files are valid. It could mean all SST files are present. It could mean various different metadata files are valid and present. I am not sure all of these things could be verified.
But in general, I can suggest that you can read the MANIFEST file. (It’s a binary file, so you will have to write code). Manifest file is supposed to contain list of the SST files. Then you can check if all the SST files are present.
If it is already open
If a badger db is using it, then you will see a LOCK file in the folder.
Result: well, it works somehow. The problem is not actually recognizing whether a folder contains a BadgerDB, but whether it is currently opened by another process.
It works quite reliably under Windows, but not under Linux. Unfortunately, the test for the existence of the LOCK file is very unreliable. In our scenario, it happens again and again that the LOCK file is still there, even if the corresponding service has been dead for a long time. Opening the file with os.O_EXCL does not help under Linux. Moreover, in my opinion, it is not actually the task of the calling application to test access at file level to determine whether a database is in use or not. I think it would be better (and much more far-sighted) if this were part of the BadgerDB API, e.g. as badger.IsLocked(fs) and badger.IsDBPath(fs) or something similar.
Is there a possibility of implementing this as a feature?
Could I know what is your use case? We never had this issue come up before. We can definitely implement this as a feature, but it might take some time.
@laylamalik using LOCK file in the badger directory could help you figure out if a badger is already running there or not. However it seems like it’s not being deleted properly after the badger is killed.