diff --git a/core/src/main/java/hudson/model/ComputerSet.java b/core/src/main/java/hudson/model/ComputerSet.java index ca341080a9..809c213281 100644 --- a/core/src/main/java/hudson/model/ComputerSet.java +++ b/core/src/main/java/hudson/model/ComputerSet.java @@ -23,10 +23,14 @@ */ package hudson.model; +import hudson.BulkChange; +import hudson.DescriptorExtensionList; import hudson.Util; -import hudson.slaves.NodeDescriptor; +import hudson.XmlFile; import hudson.model.Descriptor.FormException; import hudson.node_monitors.NodeMonitor; +import hudson.slaves.NodeDescriptor; +import hudson.util.DescribableList; import hudson.util.FormValidation; import org.kohsuke.stapler.QueryParameter; import org.kohsuke.stapler.StaplerRequest; @@ -36,11 +40,17 @@ import org.kohsuke.stapler.export.ExportedBean; import javax.servlet.ServletException; import static javax.servlet.http.HttpServletResponse.SC_BAD_REQUEST; +import java.io.File; import java.io.IOException; import java.text.ParseException; import java.util.AbstractList; -import java.util.ArrayList; import java.util.List; +import java.util.Map; +import java.util.HashMap; +import java.util.TreeMap; +import java.util.Comparator; +import java.util.logging.Level; +import java.util.logging.Logger; /** * Serves as the top of {@link Computer}s in the URL hierarchy. @@ -51,15 +61,29 @@ import java.util.List; */ @ExportedBean public final class ComputerSet extends AbstractModelObject { - private static final List monitors; + /** + * This is the owner that persists {@link #monitors}. + */ + private static final Saveable MONITORS_OWNER = new Saveable() { + public void save() throws IOException { + getConfigFile().write(monitors); + } + }; + + private static final DescribableList> monitors + = new DescribableList>(MONITORS_OWNER); @Exported public String getDisplayName() { return "nodes"; } + /** + * @deprecated as of 1.301 + * Use {@link #getMonitors()}. + */ public static List get_monitors() { - return monitors; + return monitors.toList(); } @Exported(name="computer",inline=true) @@ -67,6 +91,29 @@ public final class ComputerSet extends AbstractModelObject { return Hudson.getInstance().getComputers(); } + /** + * Exposing {@link NodeMonitor#all()} for Jelly binding. + */ + public DescriptorExtensionList> getNodeMonitorDescriptors() { + return NodeMonitor.all(); + } + + public static DescribableList> getMonitors() { + return monitors; + } + + /** + * Returns a subset pf {@link #getMonitors()} that are {@linkplain NodeMonitor#isIgnored() not ignored}. + */ + public static Map,NodeMonitor> getNonIgnoredMonitors() { + Map,NodeMonitor> r = new HashMap, NodeMonitor>(); + for (NodeMonitor m : monitors) { + if(!m.isIgnored()) + r.put(m.getDescriptor(),m); + } + return r; + } + /** * Gets all the slave names. */ @@ -262,6 +309,37 @@ public final class ComputerSet extends AbstractModelObject { } } + /** + * Accepts submission from the configuration page. + */ + public final synchronized void doConfigSubmit( StaplerRequest req, StaplerResponse rsp ) throws IOException, ServletException { + BulkChange bc = new BulkChange(MONITORS_OWNER); + try { + Hudson.getInstance().checkPermission(Hudson.ADMINISTER); + monitors.rebuild(req,req.getSubmittedForm(),getNodeMonitorDescriptors()); + + // add in the rest of instances are ignored instances + for (Descriptor d : NodeMonitor.all()) + if(monitors.get(d)==null) { + NodeMonitor i = createDefaultInstance(d, true); + if(i!=null) + monitors.add(i); + } + rsp.sendRedirect2("."); + } catch (FormException e) { + sendError(e,req,rsp); + } finally { + bc.commit(); + } + } + + /** + * {@link NodeMonitor}s are persisted in this file. + */ + private static XmlFile getConfigFile() { + return new XmlFile(new File(Hudson.getInstance().getRootDir(),"nodeMonitors.xml")); + } + public Api getApi() { return new Api(this); } @@ -271,15 +349,44 @@ public final class ComputerSet extends AbstractModelObject { */ public static void initialize() {} + private static final Logger LOGGER = Logger.getLogger(ComputerSet.class.getName()); + static { - // create all instances - ArrayList r = new ArrayList(); - for (Descriptor d : NodeMonitor.all()) - try { - r.add(d.newInstance(null,null)); - } catch (FormException e) { - // so far impossible. TODO: report + try { + DescribableList> r + = new DescribableList>(Saveable.NOOP); + + // load persisted monitors + XmlFile xf = getConfigFile(); + if(xf.exists()) { + DescribableList> persisted = + (DescribableList>) xf.read(); + r.replaceBy(persisted.toList()); } - monitors = r; + + // if we have any new monitors, let's add them + for (Descriptor d : NodeMonitor.all()) + if(r.get(d)==null) { + NodeMonitor i = createDefaultInstance(d,false); + if(i!=null) + r.add(i); + } + monitors.replaceBy(r.toList()); + } catch (IOException e) { + LOGGER.log(Level.WARNING, "Failed to instanciate NodeMonitors",e); + } + } + + private static NodeMonitor createDefaultInstance(Descriptor d, boolean ignored) { + try { + NodeMonitor nm = d.clazz.newInstance(); + nm.setIgnored(ignored); + return nm; + } catch (InstantiationException e) { + LOGGER.log(Level.SEVERE, "Failed to instanciate "+d.clazz,e); + } catch (IllegalAccessException e) { + LOGGER.log(Level.SEVERE, "Failed to instanciate "+d.clazz,e); + } + return null; } } diff --git a/core/src/main/java/hudson/node_monitors/AbstractNodeMonitorDescriptor.java b/core/src/main/java/hudson/node_monitors/AbstractNodeMonitorDescriptor.java index e04364e13f..d049ee97a7 100644 --- a/core/src/main/java/hudson/node_monitors/AbstractNodeMonitorDescriptor.java +++ b/core/src/main/java/hudson/node_monitors/AbstractNodeMonitorDescriptor.java @@ -26,6 +26,8 @@ package hudson.node_monitors; import hudson.model.Computer; import hudson.model.Descriptor; import hudson.model.Hudson; +import hudson.model.ComputerSet; +import hudson.model.AdministrativeMonitor; import hudson.triggers.Trigger; import hudson.triggers.SafeTimerTask; @@ -118,6 +120,34 @@ public abstract class AbstractNodeMonitorDescriptor extends Descriptor + * This also allows them to disable the monitoring if they don't like it. + * + * @author Kohsuke Kawaguchi + * @since 1.301 + */ +@Extension +public class MonitorMarkedNodeOffline extends AdministrativeMonitor { + public boolean active = false; + + public boolean isActivated() { + return active; + } +} diff --git a/core/src/main/java/hudson/node_monitors/NodeMonitor.java b/core/src/main/java/hudson/node_monitors/NodeMonitor.java index 7c1b70d199..4151f346d5 100644 --- a/core/src/main/java/hudson/node_monitors/NodeMonitor.java +++ b/core/src/main/java/hudson/node_monitors/NodeMonitor.java @@ -27,6 +27,7 @@ import hudson.ExtensionPoint; import hudson.Functions; import hudson.DescriptorExtensionList; import hudson.Extension; +import hudson.tasks.Publisher; import hudson.scm.RepositoryBrowser; import hudson.model.Computer; import hudson.model.ComputerSet; @@ -39,6 +40,8 @@ import hudson.util.DescriptorList; import java.util.logging.Level; import java.util.logging.Logger; import java.util.List; +import java.io.Serializable; + import org.kohsuke.stapler.export.Exported; import org.kohsuke.stapler.export.ExportedBean; @@ -52,13 +55,24 @@ import org.kohsuke.stapler.export.ExportedBean; * Invoked from {@link ComputerSet} index.jelly to render a column. * The {@link NodeMonitor} instance is accessible through the "from" variable. * Also see {@link #getColumnCaption()}. + * + *
config.jelly (optional)
+ *
+ * Configuration fragment to be displayed in {@code http://server/hudson/computer/configure}. + * Used for configuring the threshold for taking nodes offline. * * + *

Persistence

+ *

+ * {@link NodeMonitor}s are persisted via XStream. + * * @author Kohsuke Kawaguchi * @since 1.123 */ @ExportedBean public abstract class NodeMonitor implements ExtensionPoint, Describable { + private volatile boolean ignored; + /** * Returns the name of the column to be added to {@link ComputerSet} index.jelly. * @@ -100,6 +114,27 @@ public abstract class NodeMonitor implements ExtensionPoint, Describable + * Many {@link NodeMonitor}s implement a logic that if the value goes above/below + * a threshold, the slave will be marked offline as a preventive measure. + * This flag controls that. + * + *

+ * Unlike {@link Publisher}, where the absence of an instance indicates that it's disengaged, + * in {@link NodeMonitor} this boolean flag is used to indicate the disengagement, so that + * monitors work in opt-out basis. + */ + public boolean isIgnored() { + return ignored; + } + + public void setIgnored(boolean ignored) { + this.ignored = ignored; + } + /** * All registered {@link NodeMonitor}s. * @deprecated as of 1.286. diff --git a/core/src/main/java/hudson/node_monitors/ResponseTimeMonitor.java b/core/src/main/java/hudson/node_monitors/ResponseTimeMonitor.java index 9d2e134eb7..94d5e76c62 100644 --- a/core/src/main/java/hudson/node_monitors/ResponseTimeMonitor.java +++ b/core/src/main/java/hudson/node_monitors/ResponseTimeMonitor.java @@ -66,14 +66,8 @@ public class ResponseTimeMonitor extends NodeMonitor { d = new Data(old,-1L); } - if(d.hasTooManyTimeouts()) { - // TODO: this scheme should be generalized, so that Hudson can remember why it's marking the node - // as offline, as well as allowing the user to force Hudson to use it. - if(!c.isTemporarilyOffline()) { - LOGGER.warning(Messages.ResponseTimeMonitor_MarkedOffline(c.getName())); - c.setTemporarilyOffline(true); - } - } + if(d.hasTooManyTimeouts() && markOffline(c)) + LOGGER.warning(Messages.ResponseTimeMonitor_MarkedOffline(c.getName())); return d; } diff --git a/core/src/main/resources/hudson/model/ComputerSet/configure.jelly b/core/src/main/resources/hudson/model/ComputerSet/configure.jelly new file mode 100644 index 0000000000..ac60dddb2d --- /dev/null +++ b/core/src/main/resources/hudson/model/ComputerSet/configure.jelly @@ -0,0 +1,47 @@ + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/core/src/main/resources/hudson/model/ComputerSet/sidepanel.jelly b/core/src/main/resources/hudson/model/ComputerSet/sidepanel.jelly index 1c3b1a2a16..0acbf62641 100644 --- a/core/src/main/resources/hudson/model/ComputerSet/sidepanel.jelly +++ b/core/src/main/resources/hudson/model/ComputerSet/sidepanel.jelly @@ -31,6 +31,7 @@ THE SOFTWARE. + diff --git a/core/src/main/resources/hudson/node_monitors/MonitorMarkedNodeOffline/message.jelly b/core/src/main/resources/hudson/node_monitors/MonitorMarkedNodeOffline/message.jelly new file mode 100644 index 0000000000..f3d975c7c5 --- /dev/null +++ b/core/src/main/resources/hudson/node_monitors/MonitorMarkedNodeOffline/message.jelly @@ -0,0 +1,32 @@ + + + +

+
+ ${%blurb(rootURL)} + + +
+ \ No newline at end of file diff --git a/core/src/main/resources/hudson/node_monitors/MonitorMarkedNodeOffline/message.properties b/core/src/main/resources/hudson/node_monitors/MonitorMarkedNodeOffline/message.properties new file mode 100644 index 0000000000..f2e420e575 --- /dev/null +++ b/core/src/main/resources/hudson/node_monitors/MonitorMarkedNodeOffline/message.properties @@ -0,0 +1,3 @@ +blurb=Hudson took some slaves offline because their key health metrics went below a threshold. \ + If you don''t want Hudson to do this, \ + change the setting. \ No newline at end of file diff --git a/test/src/test/java/hudson/model/ComputerSetTest.java b/test/src/test/java/hudson/model/ComputerSetTest.java index 0e292119a4..830be38b68 100644 --- a/test/src/test/java/hudson/model/ComputerSetTest.java +++ b/test/src/test/java/hudson/model/ComputerSetTest.java @@ -25,6 +25,7 @@ package hudson.model; import org.jvnet.hudson.test.HudsonTestCase; import org.jvnet.hudson.test.Bug; +import com.gargoylesoftware.htmlunit.html.HtmlForm; /** * @author Kohsuke Kawaguchi @@ -36,4 +37,13 @@ public class ComputerSetTest extends HudsonTestCase { createSlave(); client.goTo("computer"); } + + /** + * Tests the basic UI behavior of the node monitoring + */ + public void testConfiguration() throws Exception { + HudsonTestCase.WebClient client = new WebClient(); + HtmlForm form = client.goTo("computer/configure").getFormByName("config"); + submit(form); + } }