Skip to content

Commit

Permalink
Set the build number explicitly
Browse files Browse the repository at this point in the history
This binds MavenModules to exact MavenModuleSet.
  • Loading branch information
jyrkiput committed May 17, 2012
1 parent 124706a commit 0d076e6
Show file tree
Hide file tree
Showing 8 changed files with 55 additions and 21 deletions.
4 changes: 4 additions & 0 deletions core/src/main/java/hudson/model/AbstractBuild.java
Expand Up @@ -168,6 +168,10 @@ protected AbstractBuild(P project, File buildDir) throws IOException {
super(project, buildDir);
}

protected AbstractBuild(P project, Integer buildNumber) throws IOException {
super(project, buildNumber);
}

public final P getProject() {
return getParent();
}
Expand Down
15 changes: 12 additions & 3 deletions core/src/main/java/hudson/model/AbstractProject.java
Expand Up @@ -925,11 +925,14 @@ public void removeRun(R run) {

// keep track of the previous time we started a build
private transient long lastBuildStartTime;


protected synchronized R newBuild() throws IOException {
return newBuild(null);
}
/**
* Creates a new build of this project for immediate execution.
*/
protected synchronized R newBuild() throws IOException {
protected synchronized R newBuild(Integer buildNumber) throws IOException {
// make sure we don't start two builds in the same second
// so the build directories will be different too
long timeSinceLast = System.currentTimeMillis() - lastBuildStartTime;
Expand All @@ -941,7 +944,13 @@ protected synchronized R newBuild() throws IOException {
}
lastBuildStartTime = System.currentTimeMillis();
try {
R lastBuild = getBuildClass().getConstructor(getClass()).newInstance(this);
R lastBuild = null;
if(buildNumber != null) {
lastBuild = getBuildClass().getConstructor(getClass(), Integer.class).newInstance(this, buildNumber);
}
if(lastBuild == null) {
lastBuild = getBuildClass().getConstructor(getClass()).newInstance(this);
}
builds.put(lastBuild);
return lastBuild;
} catch (InstantiationException e) {
Expand Down
5 changes: 5 additions & 0 deletions core/src/main/java/hudson/model/Run.java
Expand Up @@ -259,6 +259,11 @@ protected Run(JobT job) throws IOException {
this.number = project.assignBuildNumber();
}

protected Run(JobT job, Integer buildNumber) throws IOException {
this(job, new GregorianCalendar());
this.number = buildNumber;
}

/**
* Constructor for creating a {@link Run} object in
* an arbitrary state.
Expand Down
2 changes: 1 addition & 1 deletion core/src/main/java/hudson/model/RunMap.java
Expand Up @@ -50,7 +50,7 @@
public final class RunMap<R extends Run<?,R>> extends AbstractMap<Integer,R> implements SortedMap<Integer,R> {
// copy-on-write map
private transient volatile SortedMap<Integer,R> builds =
new TreeMap<Integer,R>(COMPARATOR);
Collections.synchronizedSortedMap(new TreeMap<Integer,R>(COMPARATOR));

/**
* Read-only view of this map.
Expand Down
Expand Up @@ -54,6 +54,10 @@ public AbstractMavenBuild(P project, File buildDir) throws IOException {
super(project, buildDir);
}

public AbstractMavenBuild(P job, Integer buildNumber) throws IOException {
super(job, buildNumber);
}

@Override
public EnvVars getEnvironment(TaskListener log) throws IOException, InterruptedException {
EnvVars envs = super.getEnvironment(log);
Expand Down
3 changes: 3 additions & 0 deletions maven-plugin/src/main/java/hudson/maven/MavenBuild.java
Expand Up @@ -99,6 +99,9 @@ public class MavenBuild extends AbstractMavenBuild<MavenModule,MavenBuild> {
public MavenBuild(MavenModule job) throws IOException {
super(job);
}
public MavenBuild(MavenModule job, Integer buildNumber) throws IOException {
super(job, buildNumber);
}

public MavenBuild(MavenModule job, Calendar timestamp) {
super(job, timestamp);
Expand Down
4 changes: 4 additions & 0 deletions maven-plugin/src/main/java/hudson/maven/MavenModule.java
Expand Up @@ -319,6 +319,10 @@ protected MavenBuild newBuild() throws IOException {
return super.newBuild();
}

protected MavenBuild newBuild(int buildNumber) throws IOException {
return super.newBuild(buildNumber);
}

public ModuleName getModuleName() {
return moduleName;
}
Expand Down
39 changes: 22 additions & 17 deletions maven-plugin/src/main/java/hudson/maven/MavenModuleSetBuild.java
Expand Up @@ -269,7 +269,10 @@ public Map<MavenModule,List<MavenBuild>> getModuleBuilds() {

for (MavenModule m : mods) {
List<MavenBuild> builds = new ArrayList<MavenBuild>();
MavenBuild b = m.getNearestBuild(number);
MavenBuild b = m.getBuildByNumber(number);
if( b == null ) {
b = m.getNearestBuild(number);
}
while(b!=null && b.getNumber()<end) {
builds.add(b);
b = b.getNextBuild();
Expand Down Expand Up @@ -493,17 +496,18 @@ public Fingerprint.RangeSet getDownstreamRelationship(@SuppressWarnings("rawtype
*/
/*package*/ void notifyModuleBuild(MavenBuild newBuild) {
try {
// update module set build number
getParent().updateNextBuildNumber();
synchronized(notifyModuleBuildLock) {
// update module set build number
//getParent().updateNextBuildNumber();

// update actions
Map<MavenModule, List<MavenBuild>> moduleBuilds = getModuleBuilds();
// update actions
Map<MavenModule, List<MavenBuild>> moduleBuilds = getModuleBuilds();

// actions need to be replaced atomically especially
// given that two builds might complete simultaneously.
// use a separate lock object since this synchronized block calls into plugins,
// which in turn can access other MavenModuleSetBuild instances, which will result in a dead lock.

// actions need to be replaced atomically especially
// given that two builds might complete simultaneously.
// use a separate lock object since this synchronized block calls into plugins,
// which in turn can access other MavenModuleSetBuild instances, which will result in a dead lock.
synchronized(notifyModuleBuildLock) {
boolean modified = false;

List<Action> actions = getActions();
Expand Down Expand Up @@ -532,13 +536,14 @@ public Fingerprint.RangeSet getDownstreamRelationship(@SuppressWarnings("rawtype
save();
getProject().updateTransientActions();
}
}

// symlink to this module build
String moduleFsName = newBuild.getProject().getModuleName().toFileSystemName();
Util.createSymlink(getRootDir(),
"../../modules/"+ moduleFsName +"/builds/"+newBuild.getId() /*ugly!*/,
moduleFsName, StreamTaskListener.NULL);

// symlink to this module build
String moduleFsName = newBuild.getProject().getModuleName().toFileSystemName();
Util.createSymlink(getRootDir(),
"../../modules/"+ moduleFsName +"/builds/"+newBuild.getId() /*ugly!*/,
moduleFsName, StreamTaskListener.NULL);
}
} catch (IOException e) {
LOGGER.log(Level.WARNING,"Failed to update "+this,e);
} catch (InterruptedException e) {
Expand Down Expand Up @@ -661,7 +666,7 @@ protected Result doRun(final BuildListener listener) throws Exception {
}

for (MavenModule m : project.sortedActiveModules) {
MavenBuild mb = m.newBuild();
MavenBuild mb = m.newBuild(number);
// JENKINS-8418
mb.setBuiltOnStr( getBuiltOnStr() );
// Check if incrementalBuild is selected and that there are changes -
Expand Down

0 comments on commit 0d076e6

Please sign in to comment.