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-68371) improve asynchrony of StandardPlannedNodeBuilder #1171

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,35 +1,44 @@
package org.csanchez.jenkins.plugins.kubernetes;

import hudson.Util;
import hudson.util.NamingThreadFactory;
import hudson.model.Descriptor;
import hudson.slaves.NodeProvisioner;

import java.io.IOException;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
import java.util.logging.Logger;

/**
* The default {@link PlannedNodeBuilder} implementation, in case there is other registered.
*/
public class StandardPlannedNodeBuilder extends PlannedNodeBuilder {
private static final Logger LOGGER = Logger.getLogger(StandardPlannedNodeBuilder.class.getName());
private static final int THREAD_POOL_SIZE = Integer.parseInt(System.getProperty("org.csanchez.jenkins.plugins.kubernetes.plannedNodeBuilderThreadPoolSize", "100"));
private static final ExecutorService EXECUTOR_SERVICE = Executors.newFixedThreadPool(THREAD_POOL_SIZE, new NamingThreadFactory(Executors.defaultThreadFactory(), "StandardPlannedNodeBuilderAgent"));

@Override
public NodeProvisioner.PlannedNode build() {
LOGGER.finer("Start build");
long start = System.currentTimeMillis();
KubernetesCloud cloud = getCloud();
PodTemplate t = getTemplate();
CompletableFuture f;
String displayName;
try {
Future f = EXECUTOR_SERVICE.submit(() -> {
long insideThreadStart = System.currentTimeMillis();
LOGGER.fine("Creating agent");
KubernetesSlave agent = KubernetesSlave
.builder()
.podTemplate(cloud.getUnwrappedTemplate(t))
.cloud(cloud)
.build();
displayName = agent.getDisplayName();
f = CompletableFuture.completedFuture(agent);
} catch (IOException | Descriptor.FormException e) {
displayName = null;
f = new CompletableFuture();
f.completeExceptionally(e);
}
return new NodeProvisioner.PlannedNode(Util.fixNull(displayName), f, getNumExecutors());
LOGGER.fine("Created agent in " + (System.currentTimeMillis() - insideThreadStart) + " milliseconds");
return agent;
});
LOGGER.finer("Created future after " + (System.currentTimeMillis() - start) + " milliseconds");
NodeProvisioner.PlannedNode result = new NodeProvisioner.PlannedNode(Util.fixNull("Kubernetes Agent"), f, getNumExecutors());
LOGGER.finer("Exiting build after " + (System.currentTimeMillis() - start) + " milliseconds");
return result;
}
}