Add global scheduling on module triggers
[lttng-ci.git] / dsl / kernel-lttng-modules.seed.groovy
index 9a633fd38bdb882eb4ba1dd5f2c0ea3d5e047c5e..049ce6824e82728b113396602b40b542787dafae 100644 (file)
@@ -88,12 +88,12 @@ class BasicVersion implements Comparable<BasicVersion> {
 }
 
 def kernelTagCutOff = new BasicVersion("2.6.36", "")
-def modulesBranches = []
+def modulesBranches = ["master", "stable-2.5", "stable-2.6"]
 
 //def modulesBranches = ["master","stable-2.5","stable-2.6", "stable-2.4"]
 
 def linuxURL = "git://git.kernel.org/pub/scm/linux/kernel/git/stable/linux-stable.git"
-def modulesURL = "git://git.lttng.org/lttng-modules.git"
+def modulesURL = "https://github.com/lttng/lttng-modules.git"
 
 // Linux specific variable
 String linuxCheckoutTo = "linux-source"
@@ -234,7 +234,7 @@ if ( result.exitValue() == 0 ) {
                             }
                         }
                         steps {
-                            copyArtifacts("${jobName}/arch=\$arch", "linux-artifact/**", '', false, false) {
+                            copyArtifacts("${jobName}/arch=\$arch,label=kernel", "linux-artifact/**", '', false, false) {
                                 latestSuccessful(true) // Latest successful build
                             }
                             shell(readFileFromWorkspace('lttng-modules/lttng-modules-dsl-master.sh'))
@@ -248,29 +248,34 @@ if ( result.exitValue() == 0 ) {
     // Trigger generations
     def dslTriggerKernel = """\
 import hudson.model.*
+import jenkins.model.*
 import hudson.AbortException
 import hudson.console.HyperlinkNote
 import java.util.concurrent.CancellationException
+import java.util.Random
 
 
+Random random = new Random()
 def jobs = hudson.model.Hudson.instance.items
 def fail = false
-def jobStartWith = "dsl-kernel-"
+def jobStartWithKernel = "KERNELPREFIX"
+def jobStartWithModule = "MODULEPREFIX"
 def toBuild = []
 def counter = 0
+def limitQueue = 4
 
 def anotherBuild
 jobs.each { job ->
-       def jobName = job.getName()
-               if (jobName.startsWith(jobStartWith)) {
-                       counter = counter + 1
-                       def lastBuild = job.getLastBuild()
-                               if (lastBuild == null || lastBuild.result != Result.SUCCESS) {
-                                       toBuild.push(job)
-                               } else {
-                                       println("\\tAlready built")
-                               }
-               }
+  def jobName = job.getName()
+  if (jobName.startsWith(jobStartWithKernel)) {
+    counter = counter + 1
+    def lastBuild = job.getLastBuild()
+    if (lastBuild == null || lastBuild.result != Result.SUCCESS) {
+      toBuild.push(job)
+    } else {
+      println("\tAlready built")
+    }
+  }
 }
 
 println "Kernel total "+ counter
@@ -279,26 +284,37 @@ println "Kernel to build "+ toBuild.size()
 
 def kernelEnabledNode = 0
 hudson.model.Hudson.instance.nodes.each { node ->
-       if (node.getLabelString().contains("kernel")){
-               kernelEnabledNode++
-       }
+  if (node.getLabelString().contains("kernel")){
+    kernelEnabledNode++
+  }
 }
 println "Nb of live kernel enabled build node "+ kernelEnabledNode
 
 def ongoingBuild = []
+def q = jenkins.model.Jenkins.getInstance().getQueue() 
 
 while (toBuild.size() != 0) {
-       if(ongoingBuild.size() <= (kernelEnabledNode.intdiv(2))) {
+  // Throttle the build with both the number of current parent task and queued
+  // task.Look for both kernel and downstream module from previous kernel.
+  def queuedTask = q.getItems().findAll {
+    it.task.getParent().name.startsWith(jobStartWithKernel) ||
+      it.task.getParent().name.startsWith(jobStartWithModule)
+  }
+  if ((ongoingBuild.size() <= kernelEnabledNode.intdiv(2)) && (queuedTask.size() < limitQueue)) {
                def job = toBuild.pop()
                ongoingBuild.push(job.scheduleBuild2(0))
-               println "\\t trigering" + HyperlinkNote.encodeTo('/' + job.url, job.fullDisplayName)
-       } else {
-               ongoingBuild.removeAll{ it.isCancelled() || it.isDone() }
-       }
+               println "\t trigering " + HyperlinkNote.encodeTo('/' + job.url, job.fullDisplayName)
+  } else {
+    println "Currently " + ongoingBuild.size() + " build currently on execution. Limit: " + kernelEnabledNode.intdiv(2)
+    println "Currently " + queuedTask.findAll{it.task.getParent().name.startsWith(jobStartWithModule)}.size() + " module jobs are queued. Limit: " + limitQueue
+    println "Currently " + queuedTask.findAll{it.task.getParent().name.startsWith(jobStartWithKernel)}.size() + " kernel jobs are queued. Limit: " + limitQueue
+    Thread.sleep(random.nextInt(60000))
+    ongoingBuild.removeAll{ it.isCancelled() || it.isDone() }
+  }
 }
 
 if (fail){
-       throw new AbortException("Some job failed")
+  throw new AbortException("Some job failed")
 }
 """
        def dslTriggerModule = """\
@@ -306,43 +322,62 @@ import hudson.model.*
 import hudson.AbortException
 import hudson.console.HyperlinkNote
 import java.util.concurrent.CancellationException
+import java.util.Random
 
 
+Random random = new Random()
 def jobs = hudson.model.Hudson.instance.items
 def fail = false
 def jobStartWith = "JOBPREFIX"
+def toBuild = []
+def counter = 0
+def limitQueue = 4
 
 def anotherBuild
 jobs.each { job ->
-  def jobName = job.getName()
-  if (jobName.startsWith(jobStartWith)) {
-    def lastBuild = job.getLastBuild()
-    if (lastBuild == null) {
-      try {
-        def future = job.scheduleBuild2(0, new Cause.UpstreamCause(build))
-        println "\\tWaiting for the completion of " + HyperlinkNote.encodeTo('/' + job.url, job.fullDisplayName)
-        anotherBuild = future.get()
-      } catch (CancellationException x) {
-        throw new AbortException("\${job.fullDisplayName} aborted.")
-      }
-      println HyperlinkNote.encodeTo('/' + anotherBuild.url, anotherBuild.fullDisplayName) + " completed. Result was " + anotherBuild.result
-
-      build.result = anotherBuild.result
-      if (anotherBuild.result != Result.SUCCESS && anotherBuild.result != Result.UNSTABLE) {
-        // We abort this build right here and now.
-        fail = true
-        println("Build Failed")
-      }
-    } else {
-      println("\\tAlready built")
-    }
-  }
+       def jobName = job.getName()
+       if (jobName.startsWith(jobStartWith)) {
+               counter = counter + 1
+               toBuild.push(job)
+       }
+}
+
+// Get valid node
+def kernelEnabledNode = 0
+hudson.model.Hudson.instance.nodes.each { node ->
+       if (node.getLabelString().contains("kernel")){
+               kernelEnabledNode++
+       }
+}
+
+def ongoingBuild = []
+def q = jenkins.model.Jenkins.getInstance().getQueue()
+
+while (toBuild.size() != 0) {
+       // Throttle the build with both the number of current parent task and queued
+       // task.Look for both kernel and downstream module from previous kernel.
+       def queuedTask = q.getItems().findAll {
+               it.task.getParent().name.startsWith(jobStartWithKernel) ||
+                 it.task.getParent().name.startsWith(jobStartWithModule)
+       }
+       
+       if ((ongoingBuild.size() <= kernelEnabledNode.intdiv(2)) && (queuedTask.size() < limitQueue)) {
+               def job = toBuild.pop()
+               ongoingBuild.push(job.scheduleBuild2(0))
+               println "\\t trigering " + HyperlinkNote.encodeTo('/' + job.url, job.fullDisplayName)
+       } else {
+               Thread.sleep(random.nextInt(60000))
+               ongoingBuild.removeAll{ it.isCancelled() || it.isDone() }
+       }
 }
 
 if (fail){
-  throw new AbortException("Some job failed")
+       throw new AbortException("Some job failed")
 }
 """
+
+       dslTriggerKernel = dslTriggerKernel.replaceAll("KERNELPREFIX", kernelPrefix)
+       dslTriggerKernel = dslTriggerKernel.replaceAll("MODULEPREFIX", modulesPrefix)
     if (isJenkinsInstance) {
         freeStyleJob("dsl-trigger-kernel") {
             steps {
@@ -354,9 +389,10 @@ if (fail){
                }
 
                modulesBranches.each { branch ->
+                       dslTriggerModule = dslTriggerModule.replaceAll("JOBPREFIX",modulesPrefix + separator + branch + separator)
                        freeStyleJob("dsl-trigger-module-${branch}") {
                                steps {
-                                       systemGroovyCommand(dslTriggerModule.replaceAll("JOBPREFIX",modulesPrefix + separator + branch + separator))
+                                       systemGroovyCommand(dslTriggerModule)
                                }
                                triggers {
                                        scm('@daily')
This page took 0.025329 seconds and 4 git commands to generate.