How can I take a snapshot of a snapshot map of a key value map in Java?

In my application, I have a key-value map that serves as a central repository for storing data that is used to return to a certain state after a failure or reboot (checkpoint).

The application is multi-threaded, and multiple threads can put key-value pairs on this card. One thread is responsible for the regular creation of a checkpoint, i.e. e. serialize the card to persistent storage.

While the control point is being recorded, the map should remain unchanged. It is fairly easy to avoid adding new elements, but what about other threads changing the members of their own objects inside the map?

I can have one object whose monitor is captured at the start of the control point and transfers all write access to any member of the map and their members in the blocks synchronizing this object. It seems very error prone and tiring for me.

I could also make the map private to checkpointer and just put copies of the objects in it. But then I would have to ensure that the copies were deep copies, and I could not automatically update the data on the map, with each change in the presented objects, the senders would have to resend them. This seems like a lot of overhead as well as error prone since I have to remember that you need to resend the code in all the necessary places.

What is an elegant and reliable way to solve this problem?

+4
source share
2 answers

pgras is right that immutability will correct things, but that would also be difficult. You could just block it all, but it could be a performance issue. I can think of two good ideas.

First you need to use ReadWriteLock (this requires 1.5 or newer). Since your breakpoint can acquire a read lock, it can be guaranteed that everything is in order, but when no one reads performance, it should be pretty good. This is still a pretty crude lock, so you can also do # 2 ...

Secondly, to sort it out. Each area of ​​the program can contain its own map (map for GUI files, map for user settings, map for hardware settings, whatever). Everyone will have a castle, and everything will be as usual. When the time came for the checkpoint, the checkpointer would capture ALL locks (so that everything would be consistent), and then do it. The trap is here - you have determined the lock order of locks (say, in alphabetical order), otherwise you will run into deadlocks.

If the cards are orthogonal to each other (updates for one do not require updates for the other to be consistent), then the simplest thing is to redirect updates to the central β€œbackup” card in the checkpointer, unlike something you described.

My biggest question for you would be, is this a problem (performance wise)? Are updates often, or are they rare? This will help advise on something, since my last idea (previous paragraph) may be slow, but it is easy and does not matter.

There is a fantastic book called Java Concurrency in Practice , which is basically a binary version of Java. It discusses how to figure out this material and strategies in order to avoid problems or make them easier to solve. If you are going to make more threads, it is very useful to read.

In fact, if your key values ​​are orthogonal to each other, then everything is very simple. The ConcurrentMap interface (there are features like ConcurrentHashMap ) will solve your problems, as they can make changes atomically, so readers will not see conflicting data. But if you have two (or more) keys that need to be updated at the same time, this will not cover you.

Hope this helps. Access to shared data structures is complex stuff.

+1
source

what about other threads changing members of their own objects inside the map

Here you have a problem :) and it cannot be solved using any Map ...

One solution would be to allow only immutable objects on your map, but this may not be possible for you.

Otherwise, you need to share the lock on all streams that can change the data that your card refers to and block them during the snapshot; but this is the stop of the world approach ...

+2
source

All Articles