I'm trying to extend the Clojure language to extend ACI-guaranteed refs to ACID-guaranteed drefs (durable refs). The API is to simply to call (dref key value)
, where key
is a String of the key to be used in the underlying data store (BDB JE in my current implementation), and value
is the Object that the dref should be initialized to. If key
already exists in the DB, the stored value is used instead.
Multiple drefs can be created with the same key, and they need to be synchronized, i.e. if one dref with key "A" participates in a transaction where it is written or read with an (ensure)
, all other drefs with key "A" must be transactionally synchronized: read-locks and write-locks must be used to impose ordering on transactions involving those drefs. In a larger sense, although there may be more than one in-memory dref with the same key, all of those drefs with that key are a single logical object.
For obvious reasons, it's much easier to simply ensure that this single logical dref is implemented with a single concrete in-memory dref. That way there's nothing to synchronize. How do I do this?
The obvious answer is to use an object pool keyed on key. Then Clojure will call the static getInstance(key,value)
method to retrieve from the pool if it exists, and create it and populate the pool if not. The problem with this approach is that there's no easy way to get Clojure to release the object when it's done. Memory-leak city. I have to ensure that any object with strong references to it will not be collected, and that they exist in the pool. It would be disastrous if the pool loses references to logical drefs that are still in use, since another process could create a new dref with the same key, and it wouldn't be transactionally safe with the other dref with the same key.
So I need some version of the WeakHashMap
or something using not-strong references (I would prefer SoftReference
s for a little more reluctance by the GC). So:
- If I use a
HashMap<String,SoftReference<DRef>>
, how do I ensure that the map will evict entries if the value of the entry (SoftReference) is collected? Some sort of daemon thread? - How do I make the pool thread-safe for the GC? Or do I not have to worry about that since the GC is operating at the
SoftReference
level and my daemon thread would be the one operating at theMap
level? - On a related note, how do I make sure that the daemon thread is running? Is there any way that it can stop without throwing an exception that will crash the entire JVM if uncaught? If so, how do I monitor and start a new one if needed?