From aabe063fe9124b3f8c6fce75da56f09ec0a89ca1 Mon Sep 17 00:00:00 2001 From: Markus Winter Date: Wed, 18 Oct 2023 13:21:35 +0200 Subject: [PATCH] ensure agents are taken offline/online --- .../AbstractDiskSpaceMonitor.java | 34 +----------- .../DiskSpaceMonitorDescriptor.java | 54 ++++++++++++++++++- .../help-freeDiskSpaceThreshold.html | 1 + .../help-freeDiskSpaceWarningThreshold.html | 1 + .../help-freeTempSpaceThreshold.html | 1 + .../help-freeTempSpaceWarningThreshold.html | 1 + 6 files changed, 58 insertions(+), 34 deletions(-) diff --git a/core/src/main/java/hudson/node_monitors/AbstractDiskSpaceMonitor.java b/core/src/main/java/hudson/node_monitors/AbstractDiskSpaceMonitor.java index 02d4a62b9d2f..f9f93b50a6bb 100644 --- a/core/src/main/java/hudson/node_monitors/AbstractDiskSpaceMonitor.java +++ b/core/src/main/java/hudson/node_monitors/AbstractDiskSpaceMonitor.java @@ -6,8 +6,6 @@ import java.util.logging.Logger; import java.util.regex.Matcher; import java.util.regex.Pattern; -import org.kohsuke.accmod.Restricted; -import org.kohsuke.accmod.restrictions.NoExternalUse; import org.kohsuke.stapler.DataBoundSetter; /** @@ -92,38 +90,8 @@ protected long getWarningThresholdBytes(Computer c) { @Override public Object data(Computer c) { - DiskSpace size = markNodeOfflineIfDiskspaceIsTooLow(c); - - // mark online (again), if free space is over threshold - if (size != null && size.size > getThresholdBytes(c) && c.isOffline() && c.getOfflineCause() instanceof DiskSpace) - if (this.getClass().equals(((DiskSpace) c.getOfflineCause()).getTrigger())) - if (getDescriptor().markOnline(c)) { - LOGGER.info(Messages.DiskSpaceMonitor_MarkedOnline(c.getDisplayName())); - } - return size; - } - - /** - * Marks the given node as offline if free disk space is below the configured threshold. - * @param c the node - * @return the free space - * @since 1.521 - */ - @Restricted(NoExternalUse.class) - public DiskSpace markNodeOfflineIfDiskspaceIsTooLow(Computer c) { DiskSpace size = (DiskSpace) super.data(c); - long threshold = getThresholdBytes(c); - if (size != null) { - size.setThreshold(threshold); - long warningThreshold = getWarningThresholdBytes(c); - size.setWarningThreshold(warningThreshold); - if (size.size < threshold) { - size.setTriggered(this.getClass(), true); - if (getDescriptor().markOffline(c, size)) { - LOGGER.warning(Messages.DiskSpaceMonitor_MarkedOffline(c.getDisplayName())); - } - } - } + ((DiskSpaceMonitorDescriptor) getDescriptor()).markNodeOfflineOrOnline(c, size, this); return size; } diff --git a/core/src/main/java/hudson/node_monitors/DiskSpaceMonitorDescriptor.java b/core/src/main/java/hudson/node_monitors/DiskSpaceMonitorDescriptor.java index 1115a028e32a..e3affbd5a573 100644 --- a/core/src/main/java/hudson/node_monitors/DiskSpaceMonitorDescriptor.java +++ b/core/src/main/java/hudson/node_monitors/DiskSpaceMonitorDescriptor.java @@ -25,6 +25,8 @@ package hudson.node_monitors; import hudson.Functions; +import hudson.model.Computer; +import hudson.model.ComputerSet; import hudson.node_monitors.DiskSpaceMonitorDescriptor.DiskSpace; import hudson.remoting.VirtualChannel; import java.io.File; @@ -32,6 +34,8 @@ import java.io.Serializable; import java.text.ParseException; import java.util.Locale; +import java.util.Map; +import java.util.logging.Logger; import jenkins.MasterToSlaveFileCallable; import org.kohsuke.accmod.Restricted; import org.kohsuke.accmod.restrictions.DoNotUse; @@ -46,6 +50,50 @@ * @since 1.520 */ public abstract class DiskSpaceMonitorDescriptor extends AbstractAsyncNodeMonitorDescriptor { + + private static final Logger LOGGER = Logger.getLogger(DiskSpaceMonitorDescriptor.class.getName()); + + @Override + protected Map monitor() throws InterruptedException { + Result base = monitorDetailed(); + Map data = base.getMonitoringData(); + AbstractDiskSpaceMonitor monitor = (AbstractDiskSpaceMonitor) ComputerSet.getMonitors().get(this); + for (Map.Entry e : data.entrySet()) { + Computer c = e.getKey(); + DiskSpace d = e.getValue(); + if (base.getSkipped().contains(c)) { + assert d == null; + continue; + } + if (d == null) { + e.setValue(d = get(c)); + } + markNodeOfflineOrOnline(c, d, monitor); + } + return data; + } + + @Restricted(NoExternalUse.class) + public void markNodeOfflineOrOnline(Computer c, DiskSpace size, AbstractDiskSpaceMonitor monitor) { + if (size != null) { + long threshold = monitor.getThresholdBytes(c); + size.setThreshold(threshold); + long warningThreshold = monitor.getWarningThresholdBytes(c); + size.setWarningThreshold(warningThreshold); + if (size.size <= threshold) { + size.setTriggered(monitor.getClass(), true); + if (markOffline(c, size)) { + LOGGER.warning(Messages.DiskSpaceMonitor_MarkedOffline(c.getDisplayName())); + } + } + if (size.size > threshold && c.isOffline() && c.getOfflineCause() instanceof DiskSpace) + if (monitor.getClass().equals(((DiskSpace) c.getOfflineCause()).getTrigger())) + if (markOnline(c)) { + LOGGER.info(Messages.DiskSpaceMonitor_MarkedOnline(c.getDisplayName())); + } + } + } + /** * Value object that represents the disk space. */ @@ -183,7 +231,11 @@ public static DiskSpace parse(String size) throws ParseException { } } - return new DiskSpace("", (long) (Double.parseDouble(size.trim()) * multiplier)); + try { + return new DiskSpace("", (long) (Double.parseDouble(size.trim()) * multiplier)); + } catch (NumberFormatException nfe) { + throw new ParseException(nfe.getLocalizedMessage(), 0); + } } private static final long serialVersionUID = 2L; diff --git a/core/src/main/resources/hudson/node_monitors/DiskSpaceMonitorNodeProperty/help-freeDiskSpaceThreshold.html b/core/src/main/resources/hudson/node_monitors/DiskSpaceMonitorNodeProperty/help-freeDiskSpaceThreshold.html index 053e104296d8..4e81303bcb73 100644 --- a/core/src/main/resources/hudson/node_monitors/DiskSpaceMonitorNodeProperty/help-freeDiskSpaceThreshold.html +++ b/core/src/main/resources/hudson/node_monitors/DiskSpaceMonitorNodeProperty/help-freeDiskSpaceThreshold.html @@ -4,4 +4,5 @@ is found to have less free disk space than this amount, it will be marked temporarily offline.

Set to 0 to disable this check on this agent.

+

Set to empty to use the globally defined value.

diff --git a/core/src/main/resources/hudson/node_monitors/DiskSpaceMonitorNodeProperty/help-freeDiskSpaceWarningThreshold.html b/core/src/main/resources/hudson/node_monitors/DiskSpaceMonitorNodeProperty/help-freeDiskSpaceWarningThreshold.html index 208af0ffdfa1..625b5525de0e 100644 --- a/core/src/main/resources/hudson/node_monitors/DiskSpaceMonitorNodeProperty/help-freeDiskSpaceWarningThreshold.html +++ b/core/src/main/resources/hudson/node_monitors/DiskSpaceMonitorNodeProperty/help-freeDiskSpaceWarningThreshold.html @@ -4,4 +4,5 @@ is found to have less free disk space than this amount, a warning will be shown for it on the computer overview page, but it will not be taken offline.

Set to 0 to disable this warning on this agent.

+

Set to empty to use the globally defined value.

diff --git a/core/src/main/resources/hudson/node_monitors/DiskSpaceMonitorNodeProperty/help-freeTempSpaceThreshold.html b/core/src/main/resources/hudson/node_monitors/DiskSpaceMonitorNodeProperty/help-freeTempSpaceThreshold.html index e73c662c13d5..56d8a35febab 100644 --- a/core/src/main/resources/hudson/node_monitors/DiskSpaceMonitorNodeProperty/help-freeTempSpaceThreshold.html +++ b/core/src/main/resources/hudson/node_monitors/DiskSpaceMonitorNodeProperty/help-freeTempSpaceThreshold.html @@ -4,4 +4,5 @@ is found to have less free temp space than this amount, it will be marked temporarily offline.

Set to 0 to disable this check on this agent.

+

Set to empty to use the globally defined value.

diff --git a/core/src/main/resources/hudson/node_monitors/DiskSpaceMonitorNodeProperty/help-freeTempSpaceWarningThreshold.html b/core/src/main/resources/hudson/node_monitors/DiskSpaceMonitorNodeProperty/help-freeTempSpaceWarningThreshold.html index 35818274f546..102843f4413c 100644 --- a/core/src/main/resources/hudson/node_monitors/DiskSpaceMonitorNodeProperty/help-freeTempSpaceWarningThreshold.html +++ b/core/src/main/resources/hudson/node_monitors/DiskSpaceMonitorNodeProperty/help-freeTempSpaceWarningThreshold.html @@ -4,4 +4,5 @@ is found to have less free temp space than this amount, a warning will be shown for it on the computer overview page, but it will not be taken offline.

Set to 0 to disable this warning on this agent.

+

Set to empty to use the globally defined value.