Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
JENKINS-11255 Pooled port allocation support (+ implement ResourceAct…
…ivity to queue builds instead of blocking after start).
- Loading branch information
pepov
committed
Feb 3, 2013
1 parent
93925c5
commit 283b53e
Showing
14 changed files
with
354 additions
and
32 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
25 changes: 25 additions & 0 deletions
25
src/main/java/org/jvnet/hudson/plugins/port_allocator/Pool.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
package org.jvnet.hudson.plugins.port_allocator; | ||
|
||
/** | ||
* Represents a port pool. | ||
* | ||
* @author pepov | ||
*/ | ||
public class Pool { | ||
|
||
public String name; | ||
public String ports; | ||
|
||
public int[] getPortsAsInt() { | ||
|
||
String[] portsItemsAsString = ports.split(","); | ||
|
||
int[] portsItems = new int[portsItemsAsString.length]; | ||
|
||
for (int i = 0; i < portsItemsAsString.length; i++) { | ||
portsItems[i] = Integer.parseInt(portsItemsAsString[i]); | ||
} | ||
|
||
return portsItems; | ||
} | ||
} |
7 changes: 7 additions & 0 deletions
7
src/main/java/org/jvnet/hudson/plugins/port_allocator/PoolNotDefinedException.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
package org.jvnet.hudson.plugins.port_allocator; | ||
|
||
/** | ||
* @author pepov | ||
*/ | ||
public class PoolNotDefinedException extends Throwable { | ||
} |
32 changes: 32 additions & 0 deletions
32
src/main/java/org/jvnet/hudson/plugins/port_allocator/PooledPort.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
package org.jvnet.hudson.plugins.port_allocator; | ||
|
||
import java.io.IOException; | ||
|
||
/** | ||
* Represents a port, that has been allocated from a pool. | ||
* | ||
* Holds a PortAllocationManager to trigger cleanup. | ||
* | ||
* @author pepov | ||
*/ | ||
public class PooledPort extends Port { | ||
|
||
private int selectedPort; | ||
private PortAllocationManager manager; | ||
|
||
public PooledPort(PooledPortType portType, int selectedPort, PortAllocationManager manager) { | ||
super(portType); | ||
this.selectedPort = selectedPort; | ||
this.manager = manager; | ||
} | ||
|
||
@Override | ||
public int get() { | ||
return selectedPort; | ||
} | ||
|
||
@Override | ||
public void cleanUp() throws IOException, InterruptedException { | ||
manager.free(selectedPort); | ||
} | ||
} |
102 changes: 102 additions & 0 deletions
102
src/main/java/org/jvnet/hudson/plugins/port_allocator/PooledPortType.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,102 @@ | ||
package org.jvnet.hudson.plugins.port_allocator; | ||
|
||
import hudson.Extension; | ||
import hudson.Launcher; | ||
import hudson.model.AbstractBuild; | ||
import hudson.model.BuildListener; | ||
import hudson.util.ListBoxModel; | ||
import net.sf.json.JSONObject; | ||
import org.kohsuke.stapler.DataBoundConstructor; | ||
import org.kohsuke.stapler.StaplerRequest; | ||
|
||
import java.io.IOException; | ||
|
||
/** | ||
* Port type for representing a pool of ports used concurrently by parallel jobs | ||
* | ||
* @author pepov | ||
*/ | ||
public class PooledPortType extends PortType { | ||
|
||
private int[] pool; | ||
|
||
@DataBoundConstructor | ||
public PooledPortType(String name) { | ||
super(name); | ||
} | ||
|
||
/** | ||
* Try to allocate one free port from the given pool. | ||
* Wait for a short period if no free port is available, then try again. | ||
*/ | ||
@Override | ||
public Port allocate( | ||
AbstractBuild<?, ?> build, | ||
final PortAllocationManager manager, | ||
int prefPort, | ||
Launcher launcher, | ||
BuildListener buildListener | ||
) throws IOException, InterruptedException { | ||
|
||
try { | ||
while (true) { | ||
|
||
Pool pool = PortAllocator.DESCRIPTOR.getPoolByName(name); | ||
|
||
synchronized (pool) { | ||
for (int port : pool.getPortsAsInt()) { | ||
if (manager.isFree(port)) { | ||
manager.allocate(build, port); | ||
return new PooledPort(this, port, manager); | ||
} | ||
} | ||
pool.wait(500); | ||
} | ||
} | ||
} catch (PoolNotDefinedException e) { | ||
throw new RuntimeException("Undefined pool: " + name); | ||
} | ||
} | ||
|
||
@Override | ||
public DescriptorImpl getDescriptor() { | ||
return DescriptorImpl.INSTANCE; | ||
} | ||
|
||
@Extension | ||
public static final class DescriptorImpl extends PortTypeDescriptor { | ||
|
||
public DescriptorImpl() { | ||
super(PooledPortType.class); | ||
} | ||
|
||
public PooledPortType newInstance(StaplerRequest req, JSONObject formData) throws FormException { | ||
|
||
if ("".equals(formData.getString("name"))) { | ||
throw new FormException( | ||
"Unable to setup port allocator for an undefined pool!" + | ||
" Please configure pools in the global configuration settings!", | ||
"name" | ||
); | ||
} | ||
|
||
return new PooledPortType( | ||
formData.getString("name") | ||
); | ||
} | ||
|
||
public String getDisplayName() { | ||
return "Pooled TCP port"; | ||
} | ||
|
||
public ListBoxModel doFillNameItems() { | ||
ListBoxModel model = new ListBoxModel(); | ||
for (Pool p : PortAllocator.DESCRIPTOR.getPools()) { | ||
model.add(p.name, p.name); | ||
} | ||
return model; | ||
} | ||
|
||
public static final DescriptorImpl INSTANCE = new DescriptorImpl(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.