Fix problems when reusing artifacts that are part of the build by disabling URL caching and using more atomic operations on the CliPluginRealmCache

This commit is contained in:
Guillaume Nodet
2020-10-15 20:19:22 +02:00
parent ee17eed282
commit 2cd6b46d5a
3 changed files with 14 additions and 79 deletions

View File

@@ -15,6 +15,7 @@
*/
package org.jboss.fuse.mvnd.common;
import java.io.File;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLClassLoader;
@@ -25,6 +26,10 @@ import java.util.stream.Stream;
public class ServerMain {
public static void main(String[] args) throws Exception {
// Disable URL caching so that the JVM does not try to cache resources
// loaded from jars that are built by a previous run
new File("txt").toURI().toURL().openConnection().setDefaultUseCaches(false);
final String uidStr = Environment.DAEMON_UID.systemProperty().orFail().asString();
final Path mvndHome = Environment.MVND_HOME.systemProperty().orFail().asPath();
URL[] classpath = Stream.concat(

View File

@@ -436,7 +436,6 @@ public class DaemonMavenCli {
}
eventSpyDispatcher = container.lookup(EventSpyDispatcher.class);
eventSpyDispatcher.getEventSpies().add(realmCache.asEventSpy());
maven = container.lookup(Maven.class);

View File

@@ -20,11 +20,9 @@ package org.jboss.fuse.mvnd.plugin;
import java.io.File;
import java.io.IOException;
import java.net.URL;
import java.nio.file.FileSystems;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.StandardWatchEventKinds;
import java.nio.file.WatchEvent;
import java.nio.file.WatchEvent.Kind;
@@ -34,7 +32,6 @@ import java.nio.file.attribute.BasicFileAttributes;
import java.nio.file.attribute.FileTime;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;
import java.util.Locale;
import java.util.Map;
@@ -49,9 +46,6 @@ import javax.inject.Named;
import javax.inject.Singleton;
import org.apache.maven.RepositoryUtils;
import org.apache.maven.artifact.Artifact;
import org.apache.maven.eventspy.EventSpy;
import org.apache.maven.execution.MavenExecutionRequest;
import org.apache.maven.execution.MavenExecutionResult;
import org.apache.maven.model.Plugin;
import org.apache.maven.plugin.PluginRealmCache;
import org.apache.maven.project.MavenProject;
@@ -434,50 +428,6 @@ public class CliPluginRealmCache
private static final Logger log = LoggerFactory.getLogger(CliPluginRealmCache.class);
protected final Map<Key, ValidableCacheRecord> cache = new ConcurrentHashMap<>();
private final RecordValidator watcher;
private final EventSpy eventSpy = new EventSpy() {
private Path multiModuleProjectDirectory;
@Override
public void onEvent(Object event) throws Exception {
try {
if (event instanceof MavenExecutionRequest) {
/* Store the multiModuleProjectDirectory path */
multiModuleProjectDirectory = ((MavenExecutionRequest) event).getMultiModuleProjectDirectory().toPath();
} else if (event instanceof MavenExecutionResult) {
/* Evict the entries refering to jars under multiModuleProjectDirectory */
final Iterator<Entry<Key, ValidableCacheRecord>> i = cache.entrySet().iterator();
while (i.hasNext()) {
final Entry<Key, ValidableCacheRecord> entry = i.next();
final ValidableCacheRecord record = entry.getValue();
for (URL url : record.getRealm().getURLs()) {
if (url.getProtocol().equals("file")) {
final Path path = Paths.get(url.toURI());
if (path.startsWith(multiModuleProjectDirectory)) {
log.debug(
"Removing PluginRealmCache entry {} because it refers to an artifact in the build tree {}",
entry.getKey(), path);
record.dispose();
i.remove();
break;
}
}
}
}
}
} catch (Exception e) {
log.warn("Could not notify CliPluginRealmCache", e);
}
}
@Override
public void init(Context context) throws Exception {
}
@Override
public void close() throws Exception {
}
};
public CliPluginRealmCache() {
final String osName = System.getProperty("os.name").toLowerCase(Locale.ROOT);
@@ -494,27 +444,20 @@ public class CliPluginRealmCache
public CacheRecord get(Key key) {
watcher.validateRecords();
ValidableCacheRecord record = cache.get(key);
if (record != null && !record.isValid()) {
record.dispose();
record = null;
cache.remove(key);
}
return record;
return cache.computeIfPresent(key, (k, r) -> {
if (!r.isValid()) {
r.dispose();
return null;
} else {
return r;
}
});
}
public CacheRecord put(Key key, ClassRealm pluginRealm, List<Artifact> pluginArtifacts) {
Objects.requireNonNull(pluginRealm, "pluginRealm cannot be null");
Objects.requireNonNull(pluginArtifacts, "pluginArtifacts cannot be null");
if (cache.containsKey(key)) {
throw new IllegalStateException("Duplicate plugin realm for plugin " + key);
}
ValidableCacheRecord record = watcher.newRecord(pluginRealm, pluginArtifacts);
cache.put(key, record);
return record;
return cache.computeIfAbsent(key, k -> watcher.newRecord(pluginRealm, pluginArtifacts));
}
public void flush() {
@@ -524,14 +467,6 @@ public class CliPluginRealmCache
cache.clear();
}
protected static int pluginHashCode(Plugin plugin) {
return CliCacheUtils.pluginHashCode(plugin);
}
protected static boolean pluginEquals(Plugin a, Plugin b) {
return CliCacheUtils.pluginEquals(a, b);
}
public void register(MavenProject project, Key key, CacheRecord record) {
// default cache does not track plugin usage
}
@@ -540,8 +475,4 @@ public class CliPluginRealmCache
flush();
}
public EventSpy asEventSpy() {
return eventSpy;
}
}