<br><div class="gmail_quote">On Wed, Oct 17, 2012 at 3:51 PM, Manik Surtani <span dir="ltr"><<a href="mailto:manik@jboss.org" target="_blank">manik@jboss.org</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
<div style="word-wrap:break-word">The problem is that I have 2 code paths:<div><br></div><div>1. Acquiring a lock.</div><div>1.1. Check CHM for lock</div><div>1.2. If lock == null, create new lock, do a putIfAbsent on CHM</div>
<div>1.3. acquire the lock</div><div>1.4. Check (in a loop) if the lock we have acquired is the same as the lock in the CHM, if not, release and retry.</div><div><br></div></div></blockquote><div><br>I assume the retry goes back to 1.2?<br>
<br><br></div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div style="word-wrap:break-word"><div></div><div>2. Releasing the lock.</div><div>2.1. Get lock from CHM</div>
<div>2.2. Release lock.</div><div>2.3. Remove lock from CHM</div><div><br></div></div></blockquote><div><br>I believe you need to remove the lock from the CHM before you release the lock, otherwise a thread can acquire the lock before you've removed it. The release should look something like this:<br>
<br>2.1 Get the lock from CHM<br>2.2. Check if the current thread is the owner, if not throw exception<br>2.3. Remove lock from CHM<br>2.4. Release lock<br><br>It's not very fair, because threads that try to lock this key after we have removed the lock from the CHM have an advantage compared to threads that have been waiting for a while on our lock and now have to acquire the lock, unlock, and try again to lock on a new key. This putIfAbsent+lock+unlock loop may hurt performance in high contention cases as well, and using a reference counting scheme would avoid it in most cases.<br>
<br><br></div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div style="word-wrap:break-word"><div></div><div>The problem is that we may have several competing threads in code path 1. Imagine T1, waiting on 1.3., and T2, who owns the lock, releasing it in 2.2. With some unfortunate timing, I have seen:</div>
<div><br></div><div>* T1 acquires the lock (1.3), does checks (1.4) and leaves code path 1.</div><div>* T2 removes the lock from the CHM (2.3)</div><div>* T3 comes in to code path 1, sees the lock missing from the CHM, creates a new lock acquires it, etc.</div>
<div>* T1 now tries to unlock the lock it thinks it owns. Finds a different lock instance in the CHM. All sorts of problems by this stage.</div><div><br></div><div>I have a working branch where I solved this by:</div><div>
<br></div><div>* Replacing the putIfAbsent in 1.2 with a compute() closure, which means the null check and store of the value is atomic wrt. any other modification on the same key. (with pIA, the null check didn't seem to be atomic?!)</div>
<div>* Replacing 2.1, 2.2 and 2.3 with a computeIfPresent() to release the lock and remove.</div><div><br></div><div>This seems to have solved things, because step 1.4 cannot proceed until any remove operation completes (competing for the same entry space in the map) and process 1 cannot get beyond 1.3 since the lock is still held by the releasing thread in step 2. But I'm still testing further in case of edge cases.</div>
<div><div class="h5"><div><br></div></div></div></div></blockquote><div><br>Not sure about the safety of the computeIfAbsent/computeIfPresent approach, as I don't have any experience with it, but doesn't CHMV8 use unsafe operations that prevent us from using in SecurityManager scenarios?<br>
<br> </div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div style="word-wrap:break-word"><div><div class="h5"><div></div><div><br></div><div><div><div>On 16 Oct 2012, at 16:33, Sanne Grinovero <<a href="mailto:sanne@infinispan.org" target="_blank">sanne@infinispan.org</a>> wrote:</div>
<br><blockquote type="cite"><blockquote type="cite"> But still, since the process of creating, acquiring and adding the lock to the lock map needs to be atomic, and not just atomic but also safe with regards to competing threads (say, an old owner) releasing the lock and removing it from the map (also atomic), I think a concurrent map isn't good enough anymore.<br>
</blockquote><br>are you sure that adding + creating + acquiring needs to be atomic?<br><br>I see several solutions by using the standard CHM; assuming you<br>already checked for Lock existence:<br> - create a Lock, use a putIfAbsent to store it, then *try* to acquire it<br>
- create a Lock, acquire it, putIfAbsent to store it, throw your<br>instance away if you failed to store it and try again by starting over<br>from _get_ to lock on an eventually created new instance.<br><br>For removal of the Lock I assume you're only allowed to do that when<br>
owning it? Which is even simpler.<br> - CHM.remove(), release the returned value;<br>Take care that threads waiting on that removed lock don't assume they<br>acquired it when they get this instance but go through the acquire<br>
routine again, or they might end up owning the wrong instance;<br>basically after a succesfull acquire you'll have to check you didn't<br>get an outdated instance.<br><br>What I don't like of this is that you need to get/put on the same key<br>
multiple times, hashing the key over and over, looking up the same<br>segment again and potentially traversing segment locks for each of<br>these steps: the V8 solution from Jason looks like far more efficient.<br><br>Sanne<br>
<br>On 16 October 2012 11:32, Manik Surtani <<a href="mailto:manik@jboss.org" target="_blank">manik@jboss.org</a>> wrote:<br><blockquote type="cite"><br>On 16 Oct 2012, at 11:03, Dan Berindei <<a href="mailto:dan.berindei@gmail.com" target="_blank">dan.berindei@gmail.com</a>> wrote:<br>
<br>Manik, how about adding a reference count to the lock entry? If there is a<br>waiter on the lock, the reverence count will be > 0 and the owner won't<br>remove the key on unlock.<br><br><br>Then you have a race on reading the counter/removing.<br>
<br><br><br><br>On Tue, Oct 16, 2012 at 3:43 AM, Manik Surtani <<a href="mailto:manik@jboss.org" target="_blank">manik@jboss.org</a>> wrote:<br><blockquote type="cite"><br>Hmm, that actually might just do the trick. Thanks!<br>
<br>On 15 Oct 2012, at 17:46, Jason Greene <<a href="mailto:jason.greene@redhat.com" target="_blank">jason.greene@redhat.com</a>> wrote:<br><br>I think what you are looking for is this:<br><br><br><a href="http://gee.cs.oswego.edu/dl/jsr166/dist/jsr166edocs/jsr166e/ConcurrentHashMapV8.html#computeIfAbsent" target="_blank">http://gee.cs.oswego.edu/dl/jsr166/dist/jsr166edocs/jsr166e/ConcurrentHashMapV8.html#computeIfAbsent</a>(K,<br>
jsr166e.ConcurrentHashMapV8.Fun)<br><br>On Oct 15, 2012, at 11:23 AM, Manik Surtani <<a href="mailto:manik@jboss.org" target="_blank">manik@jboss.org</a>> wrote:<br><br>Guys, after investigating <a href="https://issues.jboss.org/browse/ISPN-2381" target="_blank">https://issues.jboss.org/browse/ISPN-2381</a> and<br>
<a href="https://github.com/infinispan/infinispan/pull/1382" target="_blank">https://github.com/infinispan/infinispan/pull/1382</a>, I've discovered a pretty<br>nasty race condition in our per-entry lock containers (whether<br>
OwnableReentrantLocks or JDK locks for non-transactional caches).<br><br>The problem is that we maintain a lock map, and any given request can<br>acquire a lock, if a lock doesn't exist for a given key, create the lock and<br>
acquire it, and when done, release the lock and remove it from the lock map.<br>There's lots of room for races to occur. The current impl uses a<br>ConcurrentMap, where concurrent operations on the map are used to make sure<br>
locks are not overwritten. But still, since the process of creating,<br>acquiring and adding the lock to the lock map needs to be atomic, and not<br>just atomic but also safe with regards to competing threads (say, an old<br>
owner) releasing the lock and removing it from the map (also atomic), I<br>think a concurrent map isn't good enough anymore.<br><br>The sledgehammer approach is to synchronise on this map for these two<br>operations, but that causes all sorts of suckage. Ideally, I'd just hold on<br>
to the segment lock for the duration of these operations, but these aren't<br>exposed. Extending CHM to expose methods like acquireLockAndGet() and<br>unlockAndRemove() would work perfectly, but again a lot of CHM internals are<br>
private or package protected.<br><br>So my options are: completely re-implement a CHM-like structure, like<br>we've done for BCHM, or perhaps think of a new, specialised structure to<br>contain locks. In terms of contract, I just need a fast way to look up a<br>
value under given a key, efficient put and remove as well. It should be<br>thread-safe (naturally), and allow for an atomic operation (like "get, do<br>work, put").<br><br>Any interesting new data structures on peoples' minds?<br>
<br>Cheers<br>Manik<br>--<br>Manik Surtani<br><a href="mailto:manik@jboss.org" target="_blank">manik@jboss.org</a><br><a href="http://twitter.com/maniksurtani" target="_blank">twitter.com/maniksurtani</a><br><br>Platform Architect, JBoss Data Grid<br>
<a href="http://red.ht/data-grid" target="_blank">http://red.ht/data-grid</a><br><br><br><br>--<br>Manik Surtani<br><a href="mailto:manik@jboss.org" target="_blank">manik@jboss.org</a><br><a href="http://twitter.com/maniksurtani" target="_blank">twitter.com/maniksurtani</a><br>
<br>Platform Architect, JBoss Data Grid<br><a href="http://red.ht/data-grid" target="_blank">http://red.ht/data-grid</a><br><br><br>_______________________________________________<br>infinispan-dev mailing list<br><a href="mailto:infinispan-dev@lists.jboss.org" target="_blank">infinispan-dev@lists.jboss.org</a><br>
<a href="https://lists.jboss.org/mailman/listinfo/infinispan-dev" target="_blank">https://lists.jboss.org/mailman/listinfo/infinispan-dev</a><br></blockquote><br><br>_______________________________________________<br>infinispan-dev mailing list<br>
<a href="mailto:infinispan-dev@lists.jboss.org" target="_blank">infinispan-dev@lists.jboss.org</a><br><a href="https://lists.jboss.org/mailman/listinfo/infinispan-dev" target="_blank">https://lists.jboss.org/mailman/listinfo/infinispan-dev</a><br>
<br><br>--<br>Manik Surtani<br><a href="mailto:manik@jboss.org" target="_blank">manik@jboss.org</a><br><a href="http://twitter.com/maniksurtani" target="_blank">twitter.com/maniksurtani</a><br><br>Platform Architect, JBoss Data Grid<br>
<a href="http://red.ht/data-grid" target="_blank">http://red.ht/data-grid</a><br><br><br>_______________________________________________<br>infinispan-dev mailing list<br><a href="mailto:infinispan-dev@lists.jboss.org" target="_blank">infinispan-dev@lists.jboss.org</a><br>
<a href="https://lists.jboss.org/mailman/listinfo/infinispan-dev" target="_blank">https://lists.jboss.org/mailman/listinfo/infinispan-dev</a><br></blockquote><br>_______________________________________________<br>infinispan-dev mailing list<br>
<a href="mailto:infinispan-dev@lists.jboss.org" target="_blank">infinispan-dev@lists.jboss.org</a><br><a href="https://lists.jboss.org/mailman/listinfo/infinispan-dev" target="_blank">https://lists.jboss.org/mailman/listinfo/infinispan-dev</a><br>
</blockquote></div><br><div>
<span style="border-spacing:0px;text-indent:0px;letter-spacing:normal;font-variant:normal;text-align:-webkit-auto;font-style:normal;font-weight:normal;line-height:normal;border-collapse:separate;text-transform:none;font-size:medium;white-space:normal;font-family:Helvetica;word-spacing:0px"><div style="word-wrap:break-word">
<span style="border-spacing:0px;text-indent:0px;letter-spacing:normal;font-variant:normal;text-align:-webkit-auto;font-style:normal;font-weight:normal;line-height:normal;border-collapse:separate;text-transform:none;font-size:medium;white-space:normal;font-family:Helvetica;word-spacing:0px"><div style="word-wrap:break-word">
<span style="border-spacing:0px;text-indent:0px;letter-spacing:normal;font-variant:normal;font-style:normal;font-weight:normal;line-height:normal;border-collapse:separate;text-transform:none;font-size:medium;white-space:normal;font-family:Helvetica;word-spacing:0px"><div style="word-wrap:break-word">
<div>--</div><div>Manik Surtani</div><div><a href="mailto:manik@jboss.org" target="_blank">manik@jboss.org</a></div><div><a href="http://twitter.com/maniksurtani" target="_blank">twitter.com/maniksurtani</a></div><div><br>
</div><div><div>Platform Architect, JBoss Data Grid</div><div><a href="http://red.ht/data-grid" target="_blank">http://red.ht/data-grid</a></div></div></div></span></div></span></div></span>
</div>
<br></div></div></div></div><br>_______________________________________________<br>
infinispan-dev mailing list<br>
<a href="mailto:infinispan-dev@lists.jboss.org">infinispan-dev@lists.jboss.org</a><br>
<a href="https://lists.jboss.org/mailman/listinfo/infinispan-dev" target="_blank">https://lists.jboss.org/mailman/listinfo/infinispan-dev</a><br></blockquote></div><br>