1 /*
2 *
3 * Licensed to the Apache Software Foundation (ASF) under one
4 * or more contributor license agreements. See the NOTICE file
5 * distributed with this work for additional information
6 * regarding copyright ownership. The ASF licenses this file
7 * to you under the Apache License, Version 2.0 (the
8 * "License"); you may not use this file except in compliance
9 * with the License. You may obtain a copy of the License at
10 *
11 * http://www.apache.org/licenses/LICENSE-2.0
12 *
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 */
19 package org.apache.hadoop.hbase.util;
20
21 import java.io.IOException;
22 import java.io.InterruptedIOException;
23 import java.util.concurrent.ConcurrentHashMap;
24 import java.util.concurrent.ConcurrentMap;
25
26 import org.apache.hadoop.hbase.classification.InterfaceAudience;
27
28 import com.google.common.annotations.VisibleForTesting;
29
30 /**
31 * Allows multiple concurrent clients to lock on a numeric id with a minimal
32 * memory overhead. The intended usage is as follows:
33 *
34 * <pre>
35 * IdLock.Entry lockEntry = idLock.getLockEntry(id);
36 * try {
37 * // User code.
38 * } finally {
39 * idLock.releaseLockEntry(lockEntry);
40 * }</pre>
41 */
42 @InterfaceAudience.Private
43 public class IdLock {
44
45 /** An entry returned to the client as a lock object */
46 public static class Entry {
47 private final long id;
48 private int numWaiters;
49 private boolean isLocked = true;
50
51 private Entry(long id) {
52 this.id = id;
53 }
54
55 public String toString() {
56 return "id=" + id + ", numWaiter=" + numWaiters + ", isLocked="
57 + isLocked;
58 }
59 }
60
61 private ConcurrentMap<Long, Entry> map =
62 new ConcurrentHashMap<Long, Entry>();
63
64 /**
65 * Blocks until the lock corresponding to the given id is acquired.
66 *
67 * @param id an arbitrary number to lock on
68 * @return an "entry" to pass to {@link #releaseLockEntry(Entry)} to release
69 * the lock
70 * @throws IOException if interrupted
71 */
72 public Entry getLockEntry(long id) throws IOException {
73 Entry entry = new Entry(id);
74 Entry existing;
75 while ((existing = map.putIfAbsent(entry.id, entry)) != null) {
76 synchronized (existing) {
77 if (existing.isLocked) {
78 ++existing.numWaiters; // Add ourselves to waiters.
79 while (existing.isLocked) {
80 try {
81 existing.wait();
82 } catch (InterruptedException e) {
83 --existing.numWaiters; // Remove ourselves from waiters.
84 // HBASE-21292/HBASE-22706
85 // There is a rare case that interrupting and the lock owner thread call
86 // releaseLockEntry at the same time. Since the owner thread found there
87 // still one waiting, it won't remove the entry from the map. If the interrupted
88 // thread is the last one waiting on the lock, and since an exception is thrown,
89 // the 'existing' entry will stay in the map forever. Later threads which try to
90 // get this lock will stuck in a infinite loop because
91 // existing = map.putIfAbsent(entry.id, entry)) != null and existing.isLocked=false.
92 if (!existing.isLocked && existing.numWaiters == 0) {
93 map.remove(existing.id);
94 }
95 throw new InterruptedIOException(
96 "Interrupted waiting to acquire sparse lock");
97 }
98 }
99
100 --existing.numWaiters; // Remove ourselves from waiters.
101 existing.isLocked = true;
102 return existing;
103 }
104 // If the entry is not locked, it might already be deleted from the
105 // map, so we cannot return it. We need to get our entry into the map
106 // or get someone else's locked entry.
107 }
108 }
109 return entry;
110 }
111
112 /**
113 * Must be called in a finally block to decrease the internal counter and
114 * remove the monitor object for the given id if the caller is the last
115 * client.
116 *
117 * @param entry the return value of {@link #getLockEntry(long)}
118 */
119 public void releaseLockEntry(Entry entry) {
120 synchronized (entry) {
121 entry.isLocked = false;
122 if (entry.numWaiters > 0) {
123 entry.notify();
124 } else {
125 map.remove(entry.id);
126 }
127 }
128 }
129
130 /** For testing */
131 void assertMapEmpty() {
132 assert map.size() == 0;
133 }
134
135 @VisibleForTesting
136 public void waitForWaiters(long id, int numWaiters) throws InterruptedException {
137 for (Entry entry;;) {
138 entry = map.get(id);
139 if (entry != null) {
140 synchronized (entry) {
141 if (entry.numWaiters >= numWaiters) {
142 return;
143 }
144 }
145 }
146 Thread.sleep(100);
147 }
148 }
149 }