From 0e81e014da4e135dd2360547785f7b684e097c1f Mon Sep 17 00:00:00 2001 From: zaro Date: Sat, 2 Mar 2013 08:02:55 -0800 Subject: [PATCH] decouple gearman from the gearman configuration This change is to create a new object to store Gearman objects and state info. src/main/java/hudson/plugins/gearman/GearmanProxy.java created to keep Gearman state info. src/main/java/hudson/plugins/gearman/GearmanPluginConfig.java simplied this class by removing the core gearman stuff out to a GearmanProxy.java class src/main/java/hudson/plugins/gearman/Constants.java Use one logger instead of two. updated logger reference in all of the other files in this checkin src/main/java/hudson/plugins/gearman/ProjectListener.java src/main/java/hudson/plugins/gearman/StartJobWorker.java src/main/java/hudson/plugins/gearman/StopJobWorker.java src/main/java/hudson/plugins/gearman/ComputerListenerImpl.java update references to changed class and methods Change-Id: I879cdb8839c8b5437bccf6d7e1602c33eff434a6 --- .../plugins/gearman/ComputerListenerImpl.java | 42 ++--- .../hudson/plugins/gearman/Constants.java | 1 - .../plugins/gearman/ExecutorWorkerThread.java | 2 - .../plugins/gearman/GearmanPluginConfig.java | 163 ++---------------- .../plugins/gearman/GearmanPluginUtil.java | 60 +++++++ .../hudson/plugins/gearman/GearmanProxy.java | 109 +++++------- .../plugins/gearman/ProjectListener.java | 20 +-- .../plugins/gearman/StartJobWorker.java | 2 +- .../hudson/plugins/gearman/StopJobWorker.java | 2 +- 9 files changed, 150 insertions(+), 251 deletions(-) create mode 100644 src/main/java/hudson/plugins/gearman/GearmanPluginUtil.java diff --git a/src/main/java/hudson/plugins/gearman/ComputerListenerImpl.java b/src/main/java/hudson/plugins/gearman/ComputerListenerImpl.java index f4628fb..490fa2e 100644 --- a/src/main/java/hudson/plugins/gearman/ComputerListenerImpl.java +++ b/src/main/java/hudson/plugins/gearman/ComputerListenerImpl.java @@ -37,17 +37,17 @@ public class ComputerListenerImpl extends ComputerListener { // on creation of slave int currNumNodes = Jenkins.getInstance().getNodes().size(); - if (GearmanPluginConfig.numExecutorNodes < currNumNodes) { + if (GearmanProxy.numExecutorNodes < currNumNodes) { Node node = c.getNode(); int slaveExecutors = c.getExecutors().size(); for (int i=0; i gewtHandles; - public static List gmwtHandles; - - public static int numExecutorNodes; /** * Constructor. @@ -67,10 +56,7 @@ public class GearmanPluginConfig extends GlobalConfiguration { public GearmanPluginConfig() { logger.info("--- GearmanPluginConfig Constructor ---"); - gewtHandles = new Stack(); - gmwtHandles = new Stack(); - numExecutorNodes = 0; - + gearmanProxy = new GearmanProxy(); load(); /* @@ -78,53 +64,10 @@ public class GearmanPluginConfig extends GlobalConfiguration { * initialize the launch worker flag to disabled state at jenkins * startup so we are always at a known state */ - this.launchWorker = Constants.GEARMAN_DEFAULT_LAUNCH_WORKER; + GearmanPluginConfig.launchWorker = Constants.GEARMAN_DEFAULT_LAUNCH_WORKER; save(); } - /* - * This method checks whether a connection can be made to a host:port - * - * @param host - * the host name - * - * @param port - * the host port - * - * @param timeout - * the timeout (milliseconds) to try the connection - * - * @return - * true if a socket connection can be established otherwise false - */ - public boolean connectionIsAvailable(String host, int port, int timeout) { - - InetSocketAddress endPoint = new InetSocketAddress(host, port); - Socket socket = new Socket(); - - if (endPoint.isUnresolved()) { - System.out.println("Failure " + endPoint); - } else { - try { - socket.connect(endPoint, timeout); - logger.info("Connection Success: "+endPoint); - return true; - } catch (Exception e) { - logger.info("Connection Failure: "+endPoint+" message: " - +e.getClass().getSimpleName()+" - " - +e.getMessage()); - } finally { - if (socket != null) { - try { - socket.close(); - } catch (Exception e) { - logger.info(e.getMessage()); - } - } - } - } - return false; - } /* * This method runs when user clicks Test Connection button. @@ -137,7 +80,7 @@ public class GearmanPluginConfig extends GlobalConfiguration { @QueryParameter("port") final int port) throws IOException, ServletException { - if (connectionIsAvailable(host, port, 5000)) { + if (GearmanPluginUtil.connectionIsAvailable(host, port, 5000)) { return FormValidation.ok("Success"); } else { return FormValidation.error("Failed: Unable to Connect"); @@ -153,102 +96,22 @@ public class GearmanPluginConfig extends GlobalConfiguration { host = json.getString("host"); port = json.getInt("port"); - /* - * Purpose here is to create a 1:1 mapping of 'gearman worker':'jenkins - * executor' then use the gearman worker to execute builds on that - * jenkins nodes - */ - if (launchWorker && gmwtHandles.isEmpty() && gewtHandles.isEmpty()) { + if (launchWorker) { // check for a valid connection to gearman server - logger.info("--- Check connection to Gearman Server " + getHost() + ":" - + getPort()); - if (!connectionIsAvailable(host, port, 5000)) { - this.launchWorker = false; - throw new RuntimeException( - "Could not get connection to Gearman Server " + getHost() - + ":" + getPort()); + logger.info("--- Check connection to Gearman Server " + host + ":" + + port); + if (!GearmanPluginUtil.connectionIsAvailable(host, port, 5000)) { + GearmanPluginConfig.launchWorker = false; + throw new RuntimeException("Unable to connect to Gearman Server"); } - /* - * Spawn management executor worker. This worker does not need any - * executors. It only needs to work with gearman. - */ - AbstractWorkerThread gwt = null; - gwt = new ManagementWorkerThread(host, port, host); - gwt.registerJobs(); - gwt.start(); - gmwtHandles.add(gwt); + gearmanProxy.init_worker(host, port); - /* - * Spawn executors for the jenkins master Need to treat the master - * differently than slaves because the master is not the same as a - * slave - */ - // first make sure master is enabled (or has executors) - Node masterNode = null; - try { - masterNode = Computer.currentComputer().getNode(); - } catch (NullPointerException npe) { - logger.info("--- Master is offline"); - } catch (Exception e) { - logger.info("--- Can't get Master"); - e.printStackTrace(); - } - - if (masterNode != null) { - Computer computer = masterNode.toComputer(); - int executors = computer.getExecutors().size(); - for (int i = 0; i < executors; i++) { - // create a gearman worker for every executor on the master - gwt = new ExecutorWorkerThread(host, port, "master-exec" - + Integer.toString(i), masterNode); - gwt.registerJobs(); - gwt.start(); - gewtHandles.add(gwt); - } - numExecutorNodes++; - } - - /* - * Spawn executors for the jenkins slaves - */ - List nodes = Jenkins.getInstance().getNodes(); - if (!nodes.isEmpty()) { - for (Node node : nodes) { - Computer computer = node.toComputer(); - // create a gearman worker for every executor on the slave - int slaveExecutors = computer.getExecutors().size(); - for (int i = 0; i < slaveExecutors; i++) { - gwt = new ExecutorWorkerThread(host, port, - node.getNodeName() + "-exec" - + Integer.toString(i), node); - gwt.registerJobs(); - gwt.start(); - gewtHandles.add(gwt); - } - numExecutorNodes++; - } - } + } else { + gearmanProxy.stop_all(); } - // stop gearman workers - if (!launchWorker) { - for (AbstractWorkerThread gewtHandle : gewtHandles) { // stop executors - gewtHandle.stop(); - } - gewtHandles.clear(); - - for (AbstractWorkerThread gmwtHandle : gmwtHandles) { // stop executors - gmwtHandle.stop(); - } - gmwtHandles.clear(); - numExecutorNodes = 0; - } - - int runningExecutors = gmwtHandles.size() + gewtHandles.size(); - logger.info("--- Num of executors running = " + runningExecutors); - req.bindJSON(this, json); save(); return true; diff --git a/src/main/java/hudson/plugins/gearman/GearmanPluginUtil.java b/src/main/java/hudson/plugins/gearman/GearmanPluginUtil.java new file mode 100644 index 0000000..0aee4b2 --- /dev/null +++ b/src/main/java/hudson/plugins/gearman/GearmanPluginUtil.java @@ -0,0 +1,60 @@ +package hudson.plugins.gearman; + +import java.net.InetSocketAddress; +import java.net.Socket; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +public class GearmanPluginUtil { + + private static final Logger logger = LoggerFactory + .getLogger(Constants.PLUGIN_LOGGER_NAME); + + + /* + * This method checks whether a connection can be made to a host:port + * + * @param host + * the host name + * + * @param port + * the host port + * + * @param timeout + * the timeout (milliseconds) to try the connection + * + * @return + * true if a socket connection can be established otherwise false + */ + public static boolean connectionIsAvailable(String host, int port, int timeout) { + + InetSocketAddress endPoint = new InetSocketAddress(host, port); + Socket socket = new Socket(); + + if (endPoint.isUnresolved()) { + System.out.println("Failure " + endPoint); + } else { + try { + socket.connect(endPoint, timeout); + logger.info("Connection Success: "+endPoint); + return true; + } catch (Exception e) { + logger.info("Connection Failure: "+endPoint+" message: " + +e.getClass().getSimpleName()+" - " + +e.getMessage()); + } finally { + if (socket != null) { + try { + socket.close(); + } catch (Exception e) { + logger.info(e.getMessage()); + } + } + } + } + return false; + } + + +} diff --git a/src/main/java/hudson/plugins/gearman/GearmanProxy.java b/src/main/java/hudson/plugins/gearman/GearmanProxy.java index 76c9d33..25f3179 100644 --- a/src/main/java/hudson/plugins/gearman/GearmanProxy.java +++ b/src/main/java/hudson/plugins/gearman/GearmanProxy.java @@ -32,55 +32,49 @@ import org.slf4j.LoggerFactory; public class GearmanProxy { - - private static final Logger logger = LoggerFactory .getLogger(Constants.PLUGIN_LOGGER_NAME); // handles to gearman workers -// public static List gewtHandles; -// public static List gmwtHandles; - public static Stack gewtHandles; - public static Stack gmwtHandles; + public static List gewtHandles; + public static List gmwtHandles; + public static int numExecutorNodes; public GearmanProxy() { - // TODO Auto-generated constructor stub + logger.info("--- GearmanProxy Constructor ---"); -// gewtHandles = new ArrayList(); -// gmwtHandles = new ArrayList(); gewtHandles = new Stack(); gmwtHandles = new Stack(); numExecutorNodes = 0; - } - public void init_worker(String host, int port) { + public void init_worker(String host, int port) throws RuntimeException{ + /* - * Purpose here is to create a 1:1 mapping of 'gearman - * worker':'jenkins executor' then use the gearman worker to execute - * builds on that jenkins nodes + * Purpose here is to create a 1:1 mapping of 'gearman worker':'jenkins + * executor' then use the gearman worker to execute builds on that + * jenkins nodes */ - if (gmwtHandles.isEmpty() && gewtHandles.isEmpty()) { + if (getNumExecutors() == 0) { /* - * Spawn management executor. This worker does not need any - * executors. It only needs to work with gearman. + * Spawn management executor worker. This worker does not need any + * executors. It only needs to work with gearman. */ - AbstractWorkerThread gwt = null; - gwt = new ManagementWorkerThread(host, port, host); + AbstractWorkerThread gwt = new ManagementWorkerThread(host, port, host); gwt.registerJobs(); gwt.start(); gmwtHandles.add(gwt); /* - * 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 master Need to treat the master + * differently than slaves because the master is not the same as a + * slave */ - // make sure master is enabled (or has executors) + // first make sure master is enabled (or has executors) Node masterNode = null; try { masterNode = Computer.currentComputer().getNode(); @@ -94,14 +88,15 @@ public class GearmanProxy { if (masterNode != null) { Computer computer = masterNode.toComputer(); int executors = computer.getExecutors().size(); - for (int i=0; i