Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

CLDR-7646 TestCache concurrency issues #3450

Merged
merged 2 commits into from
Jan 11, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 23 additions & 15 deletions tools/cldr-code/src/main/java/org/unicode/cldr/test/TestCache.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,12 @@

import com.google.common.cache.Cache;
import com.google.common.cache.CacheBuilder;
import com.google.common.collect.ImmutableList;
import java.util.ArrayList;
import java.util.List;
import java.util.Map.Entry;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ExecutionException;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.unicode.cldr.test.CheckCLDR.CheckStatus;
import org.unicode.cldr.test.CheckCLDR.Options;
Expand Down Expand Up @@ -60,12 +61,21 @@ public void check(String path, List<CheckStatus> result, String value) {
*/
result.clear();
Pair<String, String> key = new Pair<>(path, value);
List<CheckStatus> cachedResult = pathCache.get(key);
List<CheckStatus> cachedResult =
pathCache.computeIfAbsent(
key,
(Pair<String, String> k) -> {
List<CheckStatus> l = new ArrayList<CheckStatus>();
cc.check(
k.getFirst(),
file.getFullXPath(k.getFirst()),
k.getSecond(),
options,
l);
return l;
});
if (cachedResult != null) {
result.addAll(cachedResult);
} else {
cc.check(path, file.getFullXPath(path), value, options, result);
pathCache.put(key, ImmutableList.copyOf(result));
}
}

Expand All @@ -88,6 +98,7 @@ public List<CheckStatus> getPossibleProblems() {
private Cache<CheckCLDR.Options, TestResultBundle> testResultCache =
CacheBuilder.newBuilder()
.maximumSize(CLDRConfig.getInstance().getProperty("CLDR_TESTCACHE_SIZE", 12))
.concurrencyLevel(1)
.softValues()
.build();

Expand All @@ -96,16 +107,13 @@ public List<CheckStatus> getPossibleProblems() {
private String nameMatcher = ".*";

/** Get the bundle for this test */
public TestResultBundle getBundle(CheckCLDR.Options options) {
TestResultBundle b = testResultCache.getIfPresent(options);
if (b != null) {
logger.finest(() -> this + " Bundle refvalid: " + options);
}
if (b == null) {
// ElapsedTimer et = new ElapsedTimer("New test bundle " + locale + " opt " + options);
b = new TestResultBundle(options);
// System.err.println(et.toString());
testResultCache.put(options, b);
public TestResultBundle getBundle(final CheckCLDR.Options options) {
TestResultBundle b;
try {
b = testResultCache.get(options, () -> new TestResultBundle(options));
} catch (ExecutionException e) {
logger.log(Level.SEVERE, e, () -> "Failed to load " + options);
throw new RuntimeException(e);
}
return b;
}
Expand Down
Loading