|
34 | 34 | */ |
35 | 35 | public class NIOLockProvider implements LockProvider { |
36 | 36 |
|
| 37 | + public static final int DEFAULT_WAIT_BEFORE_RETRY = 20; |
37 | 38 | public static Logger LOGGER = Logging.getLogger(NIOLockProvider.class.getName()); |
38 | 39 |
|
39 | 40 | private final String root; |
40 | 41 |
|
41 | 42 | /** The wait to occur in case the lock cannot be acquired */ |
42 | 43 | private final int waitBeforeRetry; |
43 | 44 |
|
44 | | - /** max lock attempts */ |
45 | | - private final int maxLockAttempts; |
| 45 | + private final int timeoutSeconds; |
46 | 46 |
|
47 | 47 | MemoryLockProvider memoryProvider = new MemoryLockProvider(); |
48 | 48 |
|
49 | 49 | public NIOLockProvider(DefaultStorageFinder storageFinder) throws ConfigurationException { |
50 | 50 | this(storageFinder.getDefaultPath()); |
51 | 51 | } |
52 | 52 |
|
53 | | - public NIOLockProvider(DefaultStorageFinder storageFinder, int waitBeforeRetry, int maxLockAttempts) |
| 53 | + public NIOLockProvider(DefaultStorageFinder storageFinder, int waitBeforeRetry, int timeoutSeconds) |
54 | 54 | throws ConfigurationException { |
55 | 55 | this.root = storageFinder.getDefaultPath(); |
56 | 56 | this.waitBeforeRetry = waitBeforeRetry; |
57 | | - this.maxLockAttempts = maxLockAttempts; |
| 57 | + this.timeoutSeconds = timeoutSeconds; |
58 | 58 | } |
59 | 59 |
|
60 | | - public NIOLockProvider(String root) throws ConfigurationException { |
| 60 | + public NIOLockProvider(String root) { |
61 | 61 | this.root = root; |
62 | | - this.waitBeforeRetry = 20; |
63 | | - this.maxLockAttempts = 120 * 1000 / waitBeforeRetry; |
| 62 | + this.waitBeforeRetry = DEFAULT_WAIT_BEFORE_RETRY; |
| 63 | + this.timeoutSeconds = GWC_LOCK_TIMEOUT; |
| 64 | + } |
| 65 | + |
| 66 | + public NIOLockProvider(String root, int timeoutSeconds) { |
| 67 | + this.root = root; |
| 68 | + this.waitBeforeRetry = DEFAULT_WAIT_BEFORE_RETRY; |
| 69 | + this.timeoutSeconds = timeoutSeconds; |
64 | 70 | } |
65 | 71 |
|
66 | 72 | @Override |
67 | | - @SuppressWarnings({"PMD.CloseResource", "PMD.UseTryWithResources"}) |
| 73 | + @SuppressWarnings({"PMD.CloseResource"}) |
68 | 74 | // complex but seemingly correct resource handling |
69 | 75 | public LockProvider.Lock getLock(final String lockKey) throws GeoWebCacheException { |
70 | | - File file = null; |
71 | 76 | // first off, synchronize among threads in the same jvm (the nio locks won't lock |
72 | 77 | // threads in the same JVM) |
73 | 78 | final LockProvider.Lock memoryLock = memoryProvider.getLock(lockKey); |
74 | | - // then synch up between different processes |
| 79 | + final File file = getFile(lockKey); |
| 80 | + |
| 81 | + // Track these to ensure cleanup on failure |
| 82 | + FileOutputStream currFos = null; |
| 83 | + FileLock currLock = null; |
| 84 | + |
| 85 | + if (LOGGER.isLoggable(Level.FINE)) |
| 86 | + LOGGER.fine("Mapped lock key " + lockKey + " to lock file " + file + ". Attempting to lock on it."); |
75 | 87 | try { |
76 | | - file = getFile(lockKey); |
77 | | - FileOutputStream currFos = null; |
78 | | - FileLock currLock = null; |
79 | | - try { |
80 | | - // try to lock |
81 | | - int count = 0; |
82 | | - while (currLock == null && count < maxLockAttempts) { |
83 | | - // the file output stream can also fail to be acquired due to the |
84 | | - // other nodes deleting the file |
85 | | - try { |
86 | | - currFos = new FileOutputStream(file); |
| 88 | + long lockTimeoutMs = timeoutSeconds * 1000L; |
| 89 | + long startTime = System.currentTimeMillis(); |
87 | 90 |
|
88 | | - currLock = currFos.getChannel().lock(); |
89 | | - } catch (OverlappingFileLockException | IOException e) { |
| 91 | + while (currLock == null && (System.currentTimeMillis() - startTime) < lockTimeoutMs) { |
| 92 | + try { |
| 93 | + currFos = new FileOutputStream(file); |
| 94 | + currLock = currFos.getChannel().tryLock(); |
| 95 | + |
| 96 | + if (currLock == null) { |
90 | 97 | IOUtils.closeQuietly(currFos); |
91 | | - try { |
92 | | - Thread.sleep(waitBeforeRetry); |
93 | | - } catch (InterruptedException ie) { |
94 | | - Thread.currentThread().interrupt(); |
95 | | - // ok, moving on |
96 | | - } |
| 98 | + Thread.sleep(waitBeforeRetry); |
97 | 99 | } |
98 | | - count++; |
99 | | - } |
100 | | - |
101 | | - // verify we managed to get the FS lock |
102 | | - if (count >= maxLockAttempts) { |
103 | | - throw new GeoWebCacheException( |
104 | | - "Failed to get a lock on key " + lockKey + " after " + count + " attempts"); |
105 | | - } |
106 | | - |
107 | | - if (LOGGER.isLoggable(Level.FINE)) { |
108 | | - LOGGER.fine("Lock " |
109 | | - + lockKey |
110 | | - + " acquired by thread " |
111 | | - + Thread.currentThread().getName() |
112 | | - + " on file " |
113 | | - + file); |
| 100 | + } catch (OverlappingFileLockException | IOException | InterruptedException e) { |
| 101 | + IOUtils.closeQuietly(currFos); |
| 102 | + if (e instanceof InterruptedException) { |
| 103 | + Thread.currentThread().interrupt(); |
| 104 | + break; |
| 105 | + } |
| 106 | + Thread.sleep(waitBeforeRetry); |
114 | 107 | } |
| 108 | + } |
115 | 109 |
|
116 | | - // store the results in a final variable for the inner class to use |
117 | | - final FileOutputStream fos = currFos; |
118 | | - final FileLock lock = currLock; |
119 | | - |
120 | | - // nullify so that we don't close them, the locking occurred as expected |
121 | | - currFos = null; |
122 | | - currLock = null; |
| 110 | + if (currLock == null) { |
| 111 | + throw new IllegalStateException("Failed to get lock on " + lockKey + " after " + lockTimeoutMs + "ms"); |
| 112 | + } |
123 | 113 |
|
124 | | - final File lockFile = file; |
125 | | - return new LockProvider.Lock() { |
| 114 | + if (LOGGER.isLoggable(Level.FINE)) { |
| 115 | + LOGGER.fine("Lock " |
| 116 | + + lockKey |
| 117 | + + " acquired by thread " |
| 118 | + + Thread.currentThread().getId() |
| 119 | + + " on file " |
| 120 | + + file); |
| 121 | + } |
126 | 122 |
|
127 | | - boolean released; |
| 123 | + final FileOutputStream finalFos = currFos; |
| 124 | + final FileLock finalLock = currLock; |
128 | 125 |
|
129 | | - @Override |
130 | | - public void release() throws GeoWebCacheException { |
131 | | - if (released) { |
132 | | - return; |
133 | | - } |
| 126 | + return new LockProvider.Lock() { |
| 127 | + boolean released; |
134 | 128 |
|
135 | | - try { |
136 | | - released = true; |
137 | | - if (!lock.isValid()) { |
138 | | - // do not crap out, locks usage in GWC is only there to prevent |
139 | | - // duplication of work |
140 | | - if (LOGGER.isLoggable(Level.FINE)) { |
141 | | - LOGGER.fine( |
142 | | - "Lock key " |
143 | | - + lockKey |
144 | | - + " for releasing lock is unkonwn, it means " |
145 | | - + "this lock was never acquired, or was released twice. " |
146 | | - + "Current thread is: " |
147 | | - + Thread.currentThread().getName() |
148 | | - + ". " |
149 | | - + "Are you running two GWC instances in the same JVM using NIO locks? " |
150 | | - + "This case is not supported and will generate exactly this error message"); |
151 | | - return; |
152 | | - } |
| 129 | + @Override |
| 130 | + public void release() throws GeoWebCacheException { |
| 131 | + if (released) return; |
| 132 | + try { |
| 133 | + released = true; |
| 134 | + if (finalLock.isValid()) { |
| 135 | + finalLock.release(); |
| 136 | + IOUtils.closeQuietly(finalFos); |
| 137 | + file.delete(); // Proper place for deletion |
| 138 | + |
| 139 | + if (LOGGER.isLoggable(Level.FINE)) { |
| 140 | + LOGGER.fine(String.format( |
| 141 | + "Lock %s mapped onto %s released by thread %d", |
| 142 | + lockKey, file, Thread.currentThread().getId())); |
153 | 143 | } |
154 | | - try { |
155 | | - lock.release(); |
156 | | - IOUtils.closeQuietly(fos); |
157 | | - lockFile.delete(); |
158 | | - |
159 | | - if (LOGGER.isLoggable(Level.FINE)) { |
160 | | - LOGGER.fine("Lock " |
161 | | - + lockKey |
162 | | - + " on file " |
163 | | - + lockFile |
164 | | - + " released by thread " |
165 | | - + Thread.currentThread().getName()); |
166 | | - } |
167 | | - } catch (IOException e) { |
168 | | - throw new GeoWebCacheException( |
169 | | - "Failure while trying to release lock for key " + lockKey, e); |
| 144 | + } else { |
| 145 | + // do not crap out, locks usage is only there to prevent duplication |
| 146 | + // of work |
| 147 | + if (LOGGER.isLoggable(Level.FINE)) { |
| 148 | + LOGGER.fine(String.format( |
| 149 | + "Lock key %s for releasing lock is unknown, it means this lock was never" |
| 150 | + + " acquired, or was released twice. Current thread is: %d. Are you" |
| 151 | + + " running two instances in the same JVM using NIO locks? This case is" |
| 152 | + + " not supported and will generate exactly this error message", |
| 153 | + lockKey, Thread.currentThread().getId())); |
170 | 154 | } |
171 | | - } finally { |
172 | | - memoryLock.release(); |
173 | 155 | } |
| 156 | + |
| 157 | + } catch (IOException e) { |
| 158 | + throw new IllegalStateException("Failure releasing lock " + lockKey, e); |
| 159 | + } finally { |
| 160 | + memoryLock.release(); |
174 | 161 | } |
175 | | - }; |
176 | | - } finally { |
| 162 | + } |
| 163 | + }; |
| 164 | + } catch (Exception e) { |
| 165 | + // If we get here, acquisition failed or timed out |
| 166 | + if (currLock != null) { |
177 | 167 | try { |
178 | | - if (currLock != null) { |
179 | | - currLock.release(); |
180 | | - } |
181 | | - IOUtils.closeQuietly(currFos); |
182 | | - file.delete(); |
183 | | - } finally { |
184 | | - memoryLock.release(); |
| 168 | + currLock.release(); |
| 169 | + } catch (IOException ignored) { |
185 | 170 | } |
186 | 171 | } |
187 | | - } catch (IOException e) { |
188 | | - throw new GeoWebCacheException("Failure while trying to get lock for key " + lockKey, e); |
| 172 | + IOUtils.closeQuietly(currFos); |
| 173 | + memoryLock.release(); // Must release memory lock on failure |
| 174 | + throw (e instanceof RuntimeException) ? (RuntimeException) e : new IllegalStateException(e); |
189 | 175 | } |
| 176 | + // Note: No finally block deleting the file here, it's done in the returned lock |
190 | 177 | } |
191 | 178 |
|
192 | 179 | private File getFile(String lockKey) { |
193 | 180 | File locks = new File(root, "lockfiles"); |
194 | 181 | locks.mkdirs(); |
195 | | - String sha1 = DigestUtils.sha1Hex(lockKey); |
| 182 | + // cryptographically strong and has a chance of collision around 10^-59 |
| 183 | + String sha1 = DigestUtils.sha256Hex(lockKey); |
196 | 184 | return new File(locks, sha1 + ".lck"); |
197 | 185 | } |
198 | 186 | } |
0 commit comments