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

[JENKINS-43496] - Add handling of the null Node#createComputer() result. #2922

Merged
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
21 changes: 18 additions & 3 deletions core/src/main/java/hudson/model/AbstractCIBase.java
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.annotation.CheckForNull;
import javax.annotation.Nonnull;

import jenkins.model.Configuration;

Expand Down Expand Up @@ -125,7 +126,18 @@ private void updateComputer(Node n, Map<String,Computer> byNameMap, Set<Computer
} else {
// we always need Computer for the master as a fallback in case there's no other Computer.
if(n.getNumExecutors()>0 || n==Jenkins.getInstance()) {
computers.put(n, c = n.createComputer());
try {
c = n.createComputer();
} catch(RuntimeException ex) { // Just in case there is a bogus extension
LOGGER.log(Level.WARNING, "Error retrieving computer for node " + n.getNodeName() + ", continuing", ex);
}
if (c == null) {
LOGGER.log(Level.WARNING, "Cannot create computer for node {0}, the {1}#createComputer() method returned null. Skipping this node",
new Object[]{n.getNodeName(), n.getClass().getName()});
return;
}

computers.put(n, c);
if (!n.isHoldOffLaunchUntilSave() && automaticSlaveLaunch) {
RetentionStrategy retentionStrategy = c.getRetentionStrategy();
if (retentionStrategy != null) {
Expand All @@ -136,8 +148,11 @@ private void updateComputer(Node n, Map<String,Computer> byNameMap, Set<Computer
c.connect(true);
}
}
used.add(c);
} else {
// TODO: Maybe it should be allowed, but we would just get NPE in the original logic before JENKINS-43496
LOGGER.log(Level.WARNING, "Node {0} has not executors. Cannot udate the Computer instance for it", n.getNodeName());
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

… has no … update its …

}
used.add(c);
}
}

Expand Down Expand Up @@ -184,7 +199,7 @@ public void run() {
byName.put(node.getNodeName(),c);
}

Set<Computer> used = new HashSet<Computer>(old.size());
Set<Computer> used = new HashSet<>(old.size());

updateComputer(AbstractCIBase.this, byName, used, automaticSlaveLaunch);
for (Node s : getNodes()) {
Expand Down
8 changes: 8 additions & 0 deletions core/src/main/java/hudson/model/Node.java
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
import hudson.security.ACL;
import hudson.security.AccessControlled;
import hudson.security.Permission;
import hudson.slaves.Cloud;
import hudson.slaves.ComputerListener;
import hudson.slaves.NodeDescriptor;
import hudson.slaves.NodeProperty;
Expand All @@ -65,6 +66,8 @@
import net.sf.json.JSONObject;
import org.acegisecurity.Authentication;
import org.jvnet.localizer.Localizable;
import org.kohsuke.accmod.Restricted;
import org.kohsuke.accmod.restrictions.NoExternalUse;
import org.kohsuke.stapler.BindInterceptor;
import org.kohsuke.stapler.Stapler;
import org.kohsuke.stapler.StaplerRequest;
Expand Down Expand Up @@ -214,8 +217,13 @@ public final VirtualChannel getChannel() {

/**
* Creates a new {@link Computer} object that acts as the UI peer of this {@link Node}.
*
* Nobody but {@link Jenkins#updateComputerList()} should call this method.
* @return Created instance of the computer.
* Can be {@code null} if the {@link Node} implementation does not support it (e.g. {@link Cloud} agent).
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this actually true? A Cloud agent would ultimately be a Slave, and that is now documented to return non-null—which seems to contradict your statement that some implementations do return non-null.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed. Slave#createLauncher() now can create nulls

*/
@CheckForNull
@Restricted(NoExternalUse.class)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Won't this cause compilation failures for sub-classes? (and if it doesn't then won't the subclass method be invocable anyway, in which case the annotation is useless

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agreed

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@stephenc @jglick My intention was to reflect the Nobody but {@link Jenkins#updateComputerList()} should call this method. comment in Javadoc. If you know a better annotation for it, just let me know. Not sure if I can dedicate time to writing another restriction if it is missing, so I will just remove it in such case

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@oleg-nenashev ah, were you looking for ProtectedExternally then?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@jglick Yep, it should work better in this case

protected abstract Computer createComputer();

/**
Expand Down
2 changes: 2 additions & 0 deletions core/src/main/java/hudson/model/Slave.java
Original file line number Diff line number Diff line change
Expand Up @@ -306,6 +306,8 @@ public Callable<ClockDifference,IOException> getClockDifferenceCallable() {
return new GetClockDifference1();
}

@Override
@Nonnull
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is not true in the case of GoogleSlave, from a quick search.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree. Better to just remove since the parent Javadoc describes the null case in a portable way

public Computer createComputer() {
return new SlaveComputer(this);
}
Expand Down
6 changes: 4 additions & 2 deletions core/src/main/java/hudson/slaves/RetentionStrategy.java
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
import java.util.concurrent.TimeUnit;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.annotation.Nonnull;

/**
* Controls when to take {@link Computer} offline, bring it back online, or even to destroy it.
Expand All @@ -57,7 +58,7 @@ public abstract class RetentionStrategy<T extends Computer> extends AbstractDesc
* rechecked earlier or later that this!
*/
@GuardedBy("hudson.model.Queue.lock")
public abstract long check(T c);
public abstract long check(@Nonnull T c);

/**
* This method is called to determine whether manual launching of the agent is allowed at this point in time.
Expand Down Expand Up @@ -92,9 +93,10 @@ public boolean isAcceptingTasks(T c) {
* The default implementation of this method delegates to {@link #check(Computer)},
* but this allows {@link RetentionStrategy} to distinguish the first time invocation from the rest.
*
* @param c Computer instance
* @since 1.275
*/
public void start(final T c) {
public void start(final @Nonnull T c) {
Queue.withLock(new Runnable() {
@Override
public void run() {
Expand Down
2 changes: 2 additions & 0 deletions core/src/main/java/jenkins/model/Jenkins.java
Original file line number Diff line number Diff line change
Expand Up @@ -3051,6 +3051,8 @@ public LabelAtom getSelfLabel() {
return getLabelAtom("master");
}

@Override
@Nonnull
public Computer createComputer() {
return new Hudson.MasterComputer();
}
Expand Down