-
-
Notifications
You must be signed in to change notification settings - Fork 9k
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
[JENKINS-72111] Allow Lifecycle
to load implementations from plugins
#8589
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -506,8 +506,10 @@ protected void reactOnCycle(PluginWrapper q, List<PluginWrapper> cycle) { | |
|
||
// obtain topologically sorted list and overwrite the list | ||
for (PluginWrapper p : cgd.getSorted()) { | ||
if (p.isActive()) | ||
if (p.isActive()) { | ||
activePlugins.add(p); | ||
((UberClassLoader) uberClassLoader).clearCacheMisses(); | ||
} | ||
} | ||
} catch (CycleDetectedException e) { // TODO this should be impossible, since we override reactOnCycle to not throw the exception | ||
stop(); // disable all plugins since classloading from them can lead to StackOverflow | ||
|
@@ -932,9 +934,10 @@ public void dynamicLoad(File arc, boolean removeExisting, @CheckForNull List<Plu | |
// so existing plugins can't be depending on this newly deployed one. | ||
|
||
plugins.add(p); | ||
if (p.isActive()) | ||
if (p.isActive()) { | ||
activePlugins.add(p); | ||
((UberClassLoader) uberClassLoader).loaded.clear(); | ||
((UberClassLoader) uberClassLoader).clearCacheMisses(); | ||
Comment on lines
-937
to
+939
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is for dynamic plugin loading, so already cleared the cache, though also cache hits which seemed unnecessary. |
||
} | ||
|
||
// TODO antimodular; perhaps should have a PluginListener to complement ExtensionListListener? | ||
CustomClassFilter.Contributed.load(); | ||
|
@@ -2385,6 +2388,10 @@ protected Enumeration<URL> findResources(String name) throws IOException { | |
return Collections.enumeration(resources); | ||
} | ||
|
||
void clearCacheMisses() { | ||
loaded.values().removeIf(Optional::isEmpty); | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
// only for debugging purpose | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
/* | ||
* The MIT License | ||
* | ||
* Copyright 2023 CloudBees, Inc. | ||
* | ||
* Permission is hereby granted, free of charge, to any person obtaining a copy | ||
* of this software and associated documentation files (the "Software"), to deal | ||
* in the Software without restriction, including without limitation the rights | ||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
* copies of the Software, and to permit persons to whom the Software is | ||
* furnished to do so, subject to the following conditions: | ||
* | ||
* The above copyright notice and this permission notice shall be included in | ||
* all copies or substantial portions of the Software. | ||
* | ||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | ||
* THE SOFTWARE. | ||
*/ | ||
|
||
package hudson.lifecycle; | ||
|
||
import static org.hamcrest.MatcherAssert.assertThat; | ||
import static org.hamcrest.Matchers.is; | ||
|
||
import java.lang.reflect.Field; | ||
import java.util.logging.Level; | ||
import jenkins.model.Jenkins; | ||
import org.junit.Rule; | ||
import org.junit.Test; | ||
import org.jvnet.hudson.test.JenkinsRule; | ||
import org.jvnet.hudson.test.RealJenkinsRule; | ||
|
||
public final class LifecycleTest { | ||
|
||
@Rule | ||
public RealJenkinsRule rr = new RealJenkinsRule() | ||
.addPlugins("plugins/custom-lifecycle.hpi") | ||
.javaOptions("-Dhudson.lifecycle=test.custom_lifecycle.CustomLifecycle") | ||
.withLogger(Lifecycle.class, Level.FINE); | ||
|
||
@Test | ||
public void definedInPlugin() throws Throwable { | ||
rr.then(LifecycleTest::_definedInPlugin); | ||
} | ||
|
||
private static void _definedInPlugin(JenkinsRule r) throws Throwable { | ||
Class<? extends Lifecycle> type = Jenkins.get().getPluginManager().uberClassLoader | ||
.loadClass("test.custom_lifecycle.CustomLifecycle").asSubclass(Lifecycle.class); | ||
Lifecycle l = Lifecycle.get(); | ||
assertThat(l.getClass(), is(type)); | ||
Field count = type.getField("count"); | ||
assertThat(count.get(l), is(0)); | ||
Lifecycle.get().restart(); | ||
assertThat(count.get(l), is(1)); | ||
} | ||
|
||
} |
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I retained sources in the plugin archive, but for the convenience of reviewers: package test.custom_lifecycle;
import hudson.lifecycle.Lifecycle;
public final class CustomLifecycle extends Lifecycle {
public int count;
@Override
public void restart() {
count++;
}
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I found that
UberClassLoader.loaded
cached class loading misses after plugins were loaded during startup. This would not normally be noticeable, but in this case a call toLifecycle.get
before initializing plugins would cache the miss and it would not subsequently be cleared.