Skip to content

Commit 0f525fe

Browse files
authored
GROOVY-11726: compile distinct sources outside the source-cache lock
1 parent df1fe66 commit 0f525fe

2 files changed

Lines changed: 350 additions & 9 deletions

File tree

‎src/main/java/groovy/lang/GroovyClassLoader.java‎

Lines changed: 32 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -365,17 +365,32 @@ public Class parseClass(final GroovyCodeSource codeSource) throws CompilationFai
365365
/**
366366
* Parses the given code source into a Java class. If there is a class file
367367
* for the given code source, then no parsing is done, instead the cached class is returned.
368+
* <p>
369+
* Distinct sources are compiled outside the source-cache lock, so several
370+
* threads may call this method on the same loader at once. The cache is
371+
* consulted again after compilation and only the first result for a key is kept.
368372
*
369373
* @param shouldCacheSource if true then the generated class will be stored in the source cache
370374
* @return the main class defined in the given script
371375
*/
372376
public Class parseClass(final GroovyCodeSource codeSource, final boolean shouldCacheSource) throws CompilationFailedException {
373-
// it's better to cache class instances by the source code
374-
// GCL will load the unique class instance for the same source code
375-
// and avoid occupying Permanent Area/Metaspace repeatedly
377+
// Cache by source text so the same script is not defined again
378+
// and does not occupy Metaspace repeatedly.
376379
String cacheKey = genSourceCacheKey(codeSource);
380+
Class<?> cached = sourceCache.get(cacheKey);
381+
if (cached != null) {
382+
return cached;
383+
}
377384

378-
return sourceCache.getAndPut(cacheKey, key -> doParseClass(codeSource), shouldCacheSource);
385+
// FlexibleCache.getAndPut holds its write lock for the whole provider
386+
// call. Compiling there made every parseClass on this loader wait for
387+
// every other one, including sources that do not share a cache key.
388+
Class<?> parsed = doParseClass(codeSource);
389+
if (!shouldCacheSource || parsed == null) {
390+
return parsed;
391+
}
392+
Class<?> published = sourceCache.getAndPut(cacheKey, key -> parsed, true);
393+
return published != null ? published : parsed;
379394
}
380395

381396
/**
@@ -476,11 +491,19 @@ private static void validate(final GroovyCodeSource codeSource) {
476491

477492
private void definePackageInternal(final String className) {
478493
int i = className.lastIndexOf('.');
479-
if (i != -1) {
480-
String packageName = className.substring(0, i);
481-
Package packageDef = getDefinedPackage(packageName);
482-
if (packageDef == null) {
483-
definePackage(packageName, null, null, null, null, null, null, null);
494+
if (i == -1) {
495+
return;
496+
}
497+
String packageName = className.substring(0, i);
498+
if (getDefinedPackage(packageName) != null) {
499+
return;
500+
}
501+
try {
502+
definePackage(packageName, null, null, null, null, null, null, null);
503+
} catch (IllegalArgumentException alreadyDefined) {
504+
// Concurrent parseClass calls can define the same package.
505+
if (getDefinedPackage(packageName) == null) {
506+
throw alreadyDefined;
484507
}
485508
}
486509
}
Lines changed: 318 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,318 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one
3+
* or more contributor license agreements. See the NOTICE file
4+
* distributed with this work for additional information
5+
* regarding copyright ownership. The ASF licenses this file
6+
* to you under the Apache License, Version 2.0 (the
7+
* "License"); you may not use this file except in compliance
8+
* with the License. You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing,
13+
* software distributed under the License is distributed on an
14+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
* KIND, either express or implied. See the License for the
16+
* specific language governing permissions and limitations
17+
* under the License.
18+
*/
19+
package bugs
20+
21+
import org.codehaus.groovy.ast.ClassNode
22+
import org.codehaus.groovy.control.CompilationUnit
23+
import org.codehaus.groovy.control.CompilerConfiguration
24+
import org.codehaus.groovy.control.Phases
25+
import org.codehaus.groovy.control.SourceUnit
26+
import org.codehaus.groovy.runtime.memoize.ConcurrentCommonCache
27+
import org.codehaus.groovy.runtime.memoize.MemoizeCache
28+
import org.junit.jupiter.api.Test
29+
import org.junit.jupiter.api.Timeout
30+
31+
import java.net.URL
32+
import java.security.CodeSource
33+
import java.util.concurrent.CountDownLatch
34+
import java.util.concurrent.Executors
35+
import java.util.concurrent.TimeUnit
36+
import java.util.concurrent.atomic.AtomicInteger
37+
38+
import static org.junit.jupiter.api.Assertions.assertEquals
39+
import static org.junit.jupiter.api.Assertions.assertNotSame
40+
import static org.junit.jupiter.api.Assertions.assertNull
41+
import static org.junit.jupiter.api.Assertions.assertSame
42+
import static org.junit.jupiter.api.Assertions.assertThrows
43+
import static org.junit.jupiter.api.Assertions.assertTrue
44+
45+
/**
46+
* GROOVY-11726: {@code GroovyClassLoader.parseClass(GroovyCodeSource, boolean)}
47+
* of distinct sources must not queue behind one cache lock, a repeated
48+
* source must still resolve to the cached class, and only the first result
49+
* published for a key is kept. Concurrent parses that share a package must
50+
* survive the losing {@code definePackage}.
51+
*/
52+
@Timeout(value = 60, unit = TimeUnit.SECONDS)
53+
final class Groovy11726 {
54+
55+
@Test
56+
void repeatedSourceUsesTheCachedClass() {
57+
def loader = new GroovyClassLoader()
58+
try {
59+
def source = source('class Cached { int n() { 7 } }', 'Cached.groovy')
60+
def first = loader.parseClass(source, true)
61+
def second = loader.parseClass(source, true)
62+
assertSame(first, second)
63+
assertEquals(7, first.getDeclaredMethod('n').invoke(first.getDeclaredConstructor().newInstance()))
64+
} finally {
65+
loader.close()
66+
}
67+
}
68+
69+
@Test
70+
void uncachedParseCompilesAgain() {
71+
def loader = new GroovyClassLoader()
72+
try {
73+
def source = source('class Fresh { int n() { 1 } }', 'Fresh.groovy')
74+
def first = loader.parseClass(source, false)
75+
def second = loader.parseClass(source, false)
76+
assertNotSame(first, second)
77+
assertEquals(first.name, second.name)
78+
} finally {
79+
loader.close()
80+
}
81+
}
82+
83+
@Test
84+
void distinctSourcesCompileConcurrently() {
85+
int threads = 4
86+
def entered = new CountDownLatch(threads)
87+
def active = new AtomicInteger()
88+
def maxActive = new AtomicInteger()
89+
def loader = new GroovyClassLoader() {
90+
@Override
91+
protected CompilationUnit createCompilationUnit(CompilerConfiguration config, CodeSource codeSource) {
92+
CompilationUnit unit = super.createCompilationUnit(config, codeSource)
93+
unit.addPhaseOperation(new CompilationUnit.ISourceUnitOperation() {
94+
@Override
95+
void call(SourceUnit source) {
96+
int now = active.incrementAndGet()
97+
maxActive.updateAndGet { current -> Math.max(current, now) }
98+
entered.countDown()
99+
try {
100+
// A serialized compile never reaches the other threads
101+
// while this one is inside the phase, so the wait expires.
102+
entered.await(2, TimeUnit.SECONDS)
103+
} catch (InterruptedException e) {
104+
Thread.currentThread().interrupt()
105+
} finally {
106+
active.decrementAndGet()
107+
}
108+
}
109+
}, Phases.CONVERSION)
110+
return unit
111+
}
112+
}
113+
def pool = Executors.newFixedThreadPool(threads)
114+
try {
115+
def tasks = (0..<threads).collect { int id ->
116+
pool.submit {
117+
def compiled = loader.parseClass(source("""
118+
package bench.hot
119+
class Job${id} {
120+
int value() { ${id} }
121+
}
122+
""".stripIndent(), "Job${id}.groovy"), true)
123+
assertEquals(id, compiled.getDeclaredMethod('value').invoke(compiled.getDeclaredConstructor().newInstance()))
124+
return compiled
125+
}
126+
}
127+
def classes = tasks.collect { it.get() }
128+
assertEquals(threads, classes*.name.toSet().size())
129+
assertTrue(maxActive.get() > 1, "distinct parseClass calls overlapped ${maxActive.get()} at a time")
130+
} finally {
131+
pool.shutdownNow()
132+
loader.close()
133+
}
134+
}
135+
136+
@Test
137+
void sameSourceCompiledConcurrentlyKeepsTheFirstClass() {
138+
int threads = 2
139+
def entered = new CountDownLatch(threads)
140+
def loader = new GroovyClassLoader() {
141+
@Override
142+
protected CompilationUnit createCompilationUnit(CompilerConfiguration config, CodeSource codeSource) {
143+
CompilationUnit unit = super.createCompilationUnit(config, codeSource)
144+
unit.addPhaseOperation(new CompilationUnit.ISourceUnitOperation() {
145+
@Override
146+
void call(SourceUnit source) {
147+
entered.countDown()
148+
// Both parses must miss the cache. Waiting here, before
149+
// the result is published, is what makes that true.
150+
if (!entered.await(10, TimeUnit.SECONDS)) {
151+
throw new IllegalStateException('the other parse did not enter compilation')
152+
}
153+
}
154+
}, Phases.CONVERSION)
155+
return unit
156+
}
157+
}
158+
def pool = Executors.newFixedThreadPool(threads)
159+
try {
160+
def source = source('class Shared { int n() { 3 } }', 'Shared.groovy')
161+
def tasks = (0..<threads).collect {
162+
pool.submit { loader.parseClass(source, true) }
163+
}
164+
def classes = tasks.collect { it.get() }
165+
assertSame(classes[0], classes[1])
166+
assertSame(classes[0], loader.parseClass(source, true))
167+
assertEquals(3, classes[0].getDeclaredMethod('n').invoke(classes[0].getDeclaredConstructor().newInstance()))
168+
} finally {
169+
pool.shutdownNow()
170+
loader.close()
171+
}
172+
}
173+
174+
@Test
175+
void secondClassInAPackageReusesTheDefinedPackage() {
176+
def loader = new GroovyClassLoader()
177+
try {
178+
def first = loader.parseClass(source('''
179+
package seq.pkg
180+
class SeqA { int n() { 1 } }
181+
'''.stripIndent(), 'SeqA.groovy'), true)
182+
def second = loader.parseClass(source('''
183+
package seq.pkg
184+
class SeqB { int n() { 2 } }
185+
'''.stripIndent(), 'SeqB.groovy'), true)
186+
assertNotSame(first, second)
187+
assertEquals('seq.pkg', first.packageName)
188+
assertEquals('seq.pkg', second.packageName)
189+
assertEquals('seq.pkg', loader.getDefinedPackage('seq.pkg').name)
190+
assertEquals(1, first.getDeclaredMethod('n').invoke(first.getDeclaredConstructor().newInstance()))
191+
assertEquals(2, second.getDeclaredMethod('n').invoke(second.getDeclaredConstructor().newInstance()))
192+
} finally {
193+
loader.close()
194+
}
195+
}
196+
197+
@Test
198+
void concurrentParsesOfOnePackageKeepTheFirstDefinition() {
199+
int threads = 2
200+
def arrived = new CountDownLatch(threads)
201+
def loader = new GroovyClassLoader() {
202+
@Override
203+
protected Package definePackage(String name, String specTitle, String specVersion, String specVendor,
204+
String implTitle, String implVersion, String implVendor, URL sealBase) {
205+
if ('race.pkg' == name) {
206+
arrived.countDown()
207+
// Hold the first caller before the package is inserted so
208+
// the other parse still observes it as missing and both
209+
// call definePackage. The platform then rejects the loser.
210+
if (!arrived.await(10, TimeUnit.SECONDS)) {
211+
throw new IllegalStateException('the other parse did not reach definePackage')
212+
}
213+
}
214+
return super.definePackage(name, specTitle, specVersion, specVendor, implTitle, implVersion, implVendor, sealBase)
215+
}
216+
}
217+
def pool = Executors.newFixedThreadPool(threads)
218+
try {
219+
def tasks = (0..<threads).collect { int id ->
220+
pool.submit {
221+
def compiled = loader.parseClass(source("""
222+
package race.pkg
223+
class Race${id} {
224+
int value() { ${id} }
225+
}
226+
""".stripIndent(), "Race${id}.groovy"), true)
227+
assertEquals(id, compiled.getDeclaredMethod('value').invoke(compiled.getDeclaredConstructor().newInstance()))
228+
return compiled
229+
}
230+
}
231+
def classes = tasks.collect { it.get() }
232+
assertEquals(['race.pkg.Race0', 'race.pkg.Race1'] as Set, classes*.name.toSet())
233+
assertEquals('race.pkg', loader.getDefinedPackage('race.pkg').name)
234+
} finally {
235+
pool.shutdownNow()
236+
loader.close()
237+
}
238+
}
239+
240+
@Test
241+
void packageRejectionIsNotSwallowed() {
242+
def loader = new GroovyClassLoader() {
243+
@Override
244+
protected Package definePackage(String name, String specTitle, String specVersion, String specVendor,
245+
String implTitle, String implVersion, String implVendor, URL sealBase) {
246+
if ('rejected.pkg' == name) {
247+
throw new IllegalArgumentException(name)
248+
}
249+
return super.definePackage(name, specTitle, specVersion, specVendor, implTitle, implVersion, implVendor, sealBase)
250+
}
251+
}
252+
try {
253+
def thrown = assertThrows(IllegalArgumentException) {
254+
loader.parseClass(source('''
255+
package rejected.pkg
256+
class Rejected { }
257+
'''.stripIndent(), 'Rejected.groovy'), true)
258+
}
259+
assertEquals('rejected.pkg', thrown.message)
260+
assertNull(loader.getDefinedPackage('rejected.pkg'))
261+
} finally {
262+
loader.close()
263+
}
264+
}
265+
266+
@Test
267+
void unpublishedClassIsReturnedAndCompiledAgain() {
268+
def cache = new ConcurrentCommonCache<String, Class>() {
269+
@Override
270+
Class getAndPut(String key, MemoizeCache.ValueProvider<? super String, ? extends Class> valueProvider, boolean shouldCache) {
271+
null
272+
}
273+
}
274+
def loader = new GroovyClassLoader(getClass().classLoader, new CompilerConfiguration(), false, cache)
275+
try {
276+
def source = source('class Unstored { int n() { 4 } }', 'Unstored.groovy')
277+
def first = loader.parseClass(source, true)
278+
def second = loader.parseClass(source, true)
279+
assertNotSame(first, second)
280+
assertEquals(first.name, second.name)
281+
assertEquals(4, first.getDeclaredMethod('n').invoke(first.getDeclaredConstructor().newInstance()))
282+
} finally {
283+
loader.close()
284+
}
285+
}
286+
287+
@Test
288+
void parseThatDefinesNoClassIsNotCached() {
289+
def defined = new AtomicInteger()
290+
def loader = new GroovyClassLoader() {
291+
@Override
292+
protected ClassCollector createCollector(CompilationUnit unit, SourceUnit su) {
293+
new ClassCollector(new InnerLoader(this), unit, su) {
294+
@Override
295+
protected Class createClass(byte[] code, ClassNode classNode) {
296+
defined.incrementAndGet()
297+
null
298+
}
299+
}
300+
}
301+
}
302+
try {
303+
def source = source('class NoBytes { }', 'NoBytes.groovy')
304+
assertNull(loader.parseClass(source, true))
305+
assertNull(loader.parseClass(source, true))
306+
// The empty result is not a class the next call can reuse.
307+
assertTrue(defined.get() >= 2, "compilations that produced bytecode: ${defined.get()}")
308+
} finally {
309+
loader.close()
310+
}
311+
}
312+
313+
private static GroovyCodeSource source(String text, String name) {
314+
def source = new GroovyCodeSource(text, name, '/groovy/script')
315+
source.cachable = true
316+
return source
317+
}
318+
}

0 commit comments

Comments
 (0)