Skip to content

Commit

Permalink
Replace 'master' label by 'built-in'
Browse files Browse the repository at this point in the history
This is required from Jenkins 2.319.1 and beyond. The built-in node
running on the Jenkins controller now has the label "built-in".

Updated all related variables and comments along the way.
  • Loading branch information
hashar committed Jan 11, 2022
1 parent 49df30f commit 6ae1bc0
Show file tree
Hide file tree
Showing 14 changed files with 86 additions and 85 deletions.
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
Overview
========
This plugin uses Gearman to support multiple Jenkins masters.
This plugin uses Gearman to support multiple Jenkins controllers.

More informations can be found at https://plugins.jenkins.io/gearman-plugin/

History
=======

The plugin has been originally written by Khai Do (@zaro0508) for the OpenStack
CI infrastructure. They needed a way to scale Jenkins to multiple masters and
went with the Gearman protocol since some were familiar with it. OpenStack
CI infrastructure. They needed a way to scale Jenkins to multiple controllers
and went with the Gearman protocol since some were familiar with it. OpenStack
eventually replaced Jenkins with Ansible and the plugin.

The Gooddata company forked it and notably added support for Jenkins Pipeline
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ public void onOffline(Computer c) {
@Override
public void onOnline(Computer c, TaskListener listener) throws IOException,
InterruptedException {
// gets called when master goes into online state
// gets called when built-in node goes into online state
// gets called when existing slave re-connects
// gets called when new slave goes into online state
logger.debug("---- " + ComputerListenerImpl.class.getName() + ":"
Expand All @@ -101,7 +101,7 @@ public void onOnline(Computer c, TaskListener listener) throws IOException,

@Override
public void onTemporarilyOffline(Computer c, OfflineCause cause) {
// fired when master or slave goes into temporary offline state
// fired when built-in node or slave goes into temporary offline state
logger.debug("---- " + ComputerListenerImpl.class.getName() + ":"
+ " onTemporarilyOffline computer " + c);
// update functions only when gearman-plugin is enabled
Expand All @@ -115,7 +115,7 @@ public void onTemporarilyOffline(Computer c, OfflineCause cause) {

@Override
public void onTemporarilyOnline(Computer c) {
// fired when master or slave goes into temporary online state
// fired when built-in or slave goes into temporary online state
logger.debug("---- " + ComputerListenerImpl.class.getName() + ":"
+ " onTemporarilyOnline computer " + c);
// update functions only when gearman-plugin is enabled
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,39 +40,39 @@ public class CustomGearmanFunctionFactory extends DefaultGearmanFunctionFactory
private final GearmanProject project;
private final Computer computer;
private final String theClass;
private final String masterName;
private final String builtInName;
private final MyGearmanWorkerImpl worker;

private static final org.slf4j.Logger LOG = LoggerFactory.getLogger(
Constants.GEARMAN_WORKER_LOGGER_NAME);

public CustomGearmanFunctionFactory(String functionName, String className,
GearmanProject project, Computer computer,
String masterName,
String builtInName,
MyGearmanWorkerImpl worker) {
super(functionName, className);
this.theClass = className;
this.project = project;
this.computer = computer;
this.masterName = masterName;
this.builtInName = builtInName;
this.worker = worker;
}


@Override
public GearmanFunction getFunction() {
return createFunctionInstance(theClass, project, computer, masterName,
return createFunctionInstance(theClass, project, computer, builtInName,
worker);
}

private static GearmanFunction createFunctionInstance(String className, GearmanProject project, Computer computer, String masterName, MyGearmanWorkerImpl worker) {
private static GearmanFunction createFunctionInstance(String className, GearmanProject project, Computer computer, String builtInName, MyGearmanWorkerImpl worker) {

GearmanFunction f = null;
try {

Class<?> c = Class.forName(className);
Constructor<?> con = c.getConstructor(new Class[]{GearmanProject.class, Computer.class, String.class, MyGearmanWorkerImpl.class});
Object o = con.newInstance(new Object[] {project, computer, masterName, worker});
Object o = con.newInstance(new Object[] {project, computer, builtInName, worker});

if (o instanceof GearmanFunction) {
f = (GearmanFunction) o;
Expand Down
12 changes: 6 additions & 6 deletions src/main/java/hudson/plugins/gearman/ExecutorWorkerThread.java
Original file line number Diff line number Diff line change
Expand Up @@ -45,17 +45,17 @@ public class ExecutorWorkerThread extends AbstractWorkerThread{
.getLogger(Constants.PLUGIN_LOGGER_NAME);

private final Computer computer;
private final String masterName;
private final String builtInName;

private HashMap<String,GearmanFunctionFactory> functionMap;

// constructor
public ExecutorWorkerThread(String host, int port, String name,
Computer computer, String masterName,
Computer computer, String builtInName,
AvailabilityMonitor availability) {
super(host, port, name, availability);
this.computer = computer;
this.masterName = masterName;
this.builtInName = builtInName;
}

@Override
Expand Down Expand Up @@ -126,7 +126,7 @@ public void registerJobs() {
String jobFunctionName = "build:" + projectName;
newFunctionMap.put(jobFunctionName, new CustomGearmanFunctionFactory(
jobFunctionName, StartJobWorker.class.getName(),
project, computer, this.masterName, worker));
project, computer, this.builtInName, worker));
}
} else { // register "build:$projectName:$label" if this
// node matches a node from the project label
Expand All @@ -145,15 +145,15 @@ public void registerJobs() {
// register without label (i.e. "build:$projectName")
newFunctionMap.put(jobFunctionName, new CustomGearmanFunctionFactory(
jobFunctionName, StartJobWorker.class.getName(),
project, computer, this.masterName, worker));
project, computer, this.builtInName, worker));
// iterate over the intersection of project and node labels
for (LabelAtom labelAtom : nodeProjectLabelAtoms) {
jobFunctionName = "build:" + projectName
+ ":" + labelAtom.getDisplayName();
// register with label (i.e. "build:$projectName:$label")
newFunctionMap.put(jobFunctionName, new CustomGearmanFunctionFactory(
jobFunctionName, StartJobWorker.class.getName(),
project, computer, this.masterName, worker));
project, computer, this.builtInName, worker));
}
}
}
Expand Down
12 changes: 6 additions & 6 deletions src/main/java/hudson/plugins/gearman/GearmanPluginUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -42,21 +42,21 @@ public class GearmanPluginUtil {
.getLogger(Constants.PLUGIN_LOGGER_NAME);

/*
* This method returns the real computer name. Master computer
* by default has an empty string for the name. But you
* need to use "master" to tell jenkins to do stuff,
* namely like schedule a build.
* This method returns the real computer name.
* Built-in node * by default has an empty string for the name. But you
* need to use "built-in" to tell jenkins to do stuff, namely like schedule a build.
*
* @param Computer
* The computer to lookup
*
* @return
* "master" for the master computer or assigned name of the slave computer
* "built-in" for the built-in computer running on the controller or
* assigned name of the slave computer
*/
public static String getRealName(Computer computer) {

if (Jenkins.getInstance().getComputer("") == computer) {
return "master";
return "built-in";
} else {
return computer.getName();
}
Expand Down
47 changes: 24 additions & 23 deletions src/main/java/hudson/plugins/gearman/GearmanProxy.java
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ public class GearmanProxy {
// handles to gearman workers
private final List<ExecutorWorkerThread> gewtHandles;
private final List<ManagementWorkerThread> gmwtHandles;
private final String masterName;
private final String builtInName;

// Singleton instance
public static synchronized GearmanProxy getInstance() {
Expand All @@ -66,25 +66,25 @@ private GearmanProxy() {
gewtHandles = Collections.synchronizedList(new ArrayList<ExecutorWorkerThread>());
gmwtHandles = Collections.synchronizedList(new ArrayList<ManagementWorkerThread>());

Computer master = null;
Computer builtInNode = null;
String hostname = Constants.GEARMAN_DEFAULT_EXECUTOR_NAME;
// query Jenkins for master's name
// query Jenkins for built-in name
try {
master = Jenkins.get().getComputer("");
hostname = master.getHostName();
builtInNode = Jenkins.get().getComputer("");
hostname = builtInNode.getHostName();
} catch (Exception e) {
logger.warn("Exception while getting hostname", e);
}
// master node may not be enabled so get masterName from system
if (master == null) {
// Built-in node may not be enabled so get builtInName from system
if (builtInNode == null) {
try {
hostname = java.net.InetAddress.getLocalHost().getHostName();
} catch (UnknownHostException e) {
logger.warn("Exception while getting hostname", e);
}
}

masterName = hostname;
builtInName = hostname;
}

/*
Expand Down Expand Up @@ -112,22 +112,23 @@ public void initWorkers() {
createManagementWorker();

/*
* Spawn executors for the jenkins master Need to treat the master
* differently than slaves because the master is not the same as a
* slave
* Spawn executors for the Jenkins built-in node.
*
* We need to treat the built-in node differently than slaves because
* the built-in is not the same as a slave
*/
// first make sure master is enabled (or has executors)
Node masterNode = null;
// first make sure built-in node is enabled (or has executors)
Node builtInNode = null;
try {
masterNode = Jenkins.get().getComputer("").getNode();
builtInNode = Jenkins.get().getComputer("").getNode();
} catch (NullPointerException npe) {
logger.info("---- Master is offline");
logger.info("---- Built-in node is offline");
} catch (Exception e) {
logger.warn("Exception while finding master", e);
logger.warn("Exception while finding built-in node", e);
}

if (masterNode != null) {
Computer computer = masterNode.toComputer();
if (builtInNode != null) {
Computer computer = builtInNode.toComputer();
if (computer != null) {
createExecutorWorkersOnNode(computer);
}
Expand Down Expand Up @@ -165,8 +166,8 @@ public void createManagementWorker() {
gwt = new ManagementWorkerThread(
GearmanPluginConfig.get().getHost(),
GearmanPluginConfig.get().getPort(),
masterName + "_manager",
masterName, new NoopAvailabilityMonitor());
builtInName + "_manager",
builtInName, new NoopAvailabilityMonitor());
gmwtHandles.add(gwt);
gwt.start();
}
Expand Down Expand Up @@ -195,15 +196,15 @@ public void createExecutorWorkersOnNode(Computer computer) {
String nodeName = null;

nodeName = GearmanPluginUtil.getRealName(computer);
if (nodeName == "master") {
nodeName = masterName;
if (nodeName == "built-in") {
nodeName = builtInName;
}

ExecutorWorkerThread ewt = new ExecutorWorkerThread(
GearmanPluginConfig.get().getHost(),
GearmanPluginConfig.get().getPort(),
nodeName+"_exec-"+Integer.toString(i),
computer, masterName, availability);
computer, builtInName, availability);

ewt.start();
gewtHandles.add(ewt);
Expand Down
10 changes: 5 additions & 5 deletions src/main/java/hudson/plugins/gearman/ManagementWorkerThread.java
Original file line number Diff line number Diff line change
Expand Up @@ -39,11 +39,11 @@ public class ManagementWorkerThread extends AbstractWorkerThread{
.getLogger(Constants.PLUGIN_LOGGER_NAME);

private boolean registered = false;
private final String masterName;
private final String builtInName;

public ManagementWorkerThread(String host, int port, String name, String masterName, AvailabilityMonitor availability){
public ManagementWorkerThread(String host, int port, String name, String builtInName, AvailabilityMonitor availability){
super(host, port, name, availability);
this.masterName = masterName;
this.builtInName = builtInName;
}

/**
Expand All @@ -63,9 +63,9 @@ public void registerJobs(){
if (!registered) {
Set<GearmanFunctionFactory> functionSet = new HashSet<GearmanFunctionFactory>();

functionSet.add(new DefaultGearmanFunctionFactory("stop:"+masterName,
functionSet.add(new DefaultGearmanFunctionFactory("stop:"+builtInName,
StopJobWorker.class.getName()));
functionSet.add(new DefaultGearmanFunctionFactory("set_description:"+masterName,
functionSet.add(new DefaultGearmanFunctionFactory("set_description:"+builtInName,
SetDescriptionWorker.class.getName()));

updateJobs(functionSet);
Expand Down
8 changes: 4 additions & 4 deletions src/main/java/hudson/plugins/gearman/StartJobWorker.java
Original file line number Diff line number Diff line change
Expand Up @@ -60,14 +60,14 @@ public class StartJobWorker extends AbstractGearmanFunction {

Computer computer;
GearmanProject project;
String masterName;
String builtInName;
MyGearmanWorkerImpl worker;

public StartJobWorker(GearmanProject project, Computer computer, String masterName,
public StartJobWorker(GearmanProject project, Computer computer, String builtInName,
MyGearmanWorkerImpl worker) {
this.project = project;
this.computer = computer;
this.masterName = masterName;
this.builtInName = builtInName;
this.worker = worker;
}

Expand All @@ -76,7 +76,7 @@ private String buildStatusData(Run<?, ?> build) {

data.put("name", project.getJob().getName());
data.put("number", build.getNumber());
data.put("manager", masterName);
data.put("manager", builtInName);
data.put("worker", this.worker.getWorkerID());

String rootUrl = Jenkins.getInstance().getRootUrl();
Expand Down
4 changes: 2 additions & 2 deletions src/main/resources/index.jelly
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<?jelly escape-by-default='true'?>
<div>
This plugin uses Gearman to support multiple Jenkins masters.
</div>
This plugin uses Gearman to support multiple Jenkins controllers.
</div>

0 comments on commit 6ae1bc0

Please sign in to comment.