Skip to content

Commit

Permalink
Constraints refactor to introduce a specific models package (#68)
Browse files Browse the repository at this point in the history
* refactor the package organization to create a separate package for models

* Refactor the test folder and add license headers
  • Loading branch information
abhi18av authored Jul 15, 2024
1 parent 3e30508 commit e6d3dd3
Show file tree
Hide file tree
Showing 16 changed files with 301 additions and 220 deletions.

This file was deleted.

This file was deleted.

This file was deleted.

34 changes: 19 additions & 15 deletions plugins/nf-nomad/src/main/nextflow/nomad/config/NomadJobOpts.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,10 @@ package nextflow.nomad.config

import groovy.transform.CompileStatic
import groovy.util.logging.Slf4j
import nextflow.nomad.models.JobAffinity
import nextflow.nomad.models.JobConstraint
import nextflow.nomad.models.JobConstraints
import nextflow.nomad.models.JobVolume


/**
Expand All @@ -37,11 +41,11 @@ class NomadJobOpts{
String region
String namespace
String dockerVolume
VolumeSpec[] volumeSpec
AffinitySpec affinitySpec
ConstraintSpec constraintSpec
JobVolume[] volumeSpec
JobAffinity affinitySpec
JobConstraint constraintSpec

ConstraintsSpec constraintsSpec
JobConstraints constraintsSpec

NomadJobOpts(Map nomadJobOpts, Map<String,String> env=null){
assert nomadJobOpts!=null
Expand Down Expand Up @@ -74,10 +78,10 @@ class NomadJobOpts{
this.constraintsSpec = parseConstraints(nomadJobOpts)
}

VolumeSpec[] parseVolumes(Map nomadJobOpts){
List<VolumeSpec> ret = []
JobVolume[] parseVolumes(Map nomadJobOpts){
List<JobVolume> ret = []
if( nomadJobOpts.volume && nomadJobOpts.volume instanceof Closure){
def volumeSpec = new VolumeSpec()
def volumeSpec = new JobVolume()
def closure = (nomadJobOpts.volume as Closure)
def clone = closure.rehydrate(volumeSpec, closure.owner, closure.thisObject)
clone.resolveStrategy = Closure.DELEGATE_FIRST
Expand All @@ -89,7 +93,7 @@ class NomadJobOpts{
if( nomadJobOpts.volumes && nomadJobOpts.volumes instanceof List){
nomadJobOpts.volumes.each{ closure ->
if( closure instanceof Closure){
def volumeSpec = new VolumeSpec()
def volumeSpec = new JobVolume()
def clone = closure.rehydrate(volumeSpec, closure.owner, closure.thisObject)
clone.resolveStrategy = Closure.DELEGATE_FIRST
clone()
Expand All @@ -108,13 +112,13 @@ class NomadJobOpts{
throw new IllegalArgumentException("No more than a workdir volume allowed")
}

return ret as VolumeSpec[]
return ret as JobVolume[]
}

AffinitySpec parseAffinity(Map nomadJobOpts) {
JobAffinity parseAffinity(Map nomadJobOpts) {
if (nomadJobOpts.affinity && nomadJobOpts.affinity instanceof Closure) {
log.info "affinity config will be deprecated, use affinities closure instead"
def affinitySpec = new AffinitySpec()
def affinitySpec = new JobAffinity()
def closure = (nomadJobOpts.affinity as Closure)
def clone = closure.rehydrate(affinitySpec, closure.owner, closure.thisObject)
clone.resolveStrategy = Closure.DELEGATE_FIRST
Expand All @@ -126,10 +130,10 @@ class NomadJobOpts{
}
}

ConstraintSpec parseConstraint(Map nomadJobOpts){
JobConstraint parseConstraint(Map nomadJobOpts){
if (nomadJobOpts.constraint && nomadJobOpts.constraint instanceof Closure) {
log.info "constraint config will be deprecated, use constraints closure instead"
def constraintSpec = new ConstraintSpec()
def constraintSpec = new JobConstraint()
def closure = (nomadJobOpts.constraint as Closure)
def clone = closure.rehydrate(constraintSpec, closure.owner, closure.thisObject)
clone.resolveStrategy = Closure.DELEGATE_FIRST
Expand All @@ -141,9 +145,9 @@ class NomadJobOpts{
}
}

ConstraintsSpec parseConstraints(Map nomadJobOpts){
JobConstraints parseConstraints(Map nomadJobOpts){
if (nomadJobOpts.constraints && nomadJobOpts.constraints instanceof Closure) {
def constraintsSpec = new ConstraintsSpec()
def constraintsSpec = new JobConstraints()
def closure = (nomadJobOpts.constraints as Closure)
def clone = closure.rehydrate(constraintsSpec, closure.owner, closure.thisObject)
clone.resolveStrategy = Closure.DELEGATE_FIRST
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
package nextflow.nomad.executor

import io.nomadproject.client.model.Constraint
import nextflow.nomad.config.ConstraintAttrSpec
import nextflow.nomad.config.ConstraintNodeSpec
import nextflow.nomad.config.ConstraintsSpec
import nextflow.nomad.models.JobConstraintsAttr
import nextflow.nomad.models.JobConstraintsNode
import nextflow.nomad.models.JobConstraints

class ConstraintsBuilder {

protected static List<Constraint> constraintsSpecToList(ConstraintsSpec spec){
protected static List<Constraint> constraintsSpecToList(JobConstraints spec){
def constraints = [] as List<Constraint>
if( spec?.nodeSpecs ){
def nodes = spec.nodeSpecs
Expand All @@ -24,7 +24,7 @@ class ConstraintsBuilder {
return constraints
}

protected static List<Constraint> nodeConstraints(ConstraintNodeSpec nodeSpec){
protected static List<Constraint> nodeConstraints(JobConstraintsNode nodeSpec){
def ret = [] as List<Constraint>
if( nodeSpec.id ){
ret.add new Constraint()
Expand Down Expand Up @@ -65,7 +65,7 @@ class ConstraintsBuilder {
ret
}

protected static List<Constraint> attrConstraints(ConstraintAttrSpec nodeSpec) {
protected static List<Constraint> attrConstraints(JobConstraintsAttr nodeSpec) {
def ret = [] as List<Constraint>
if (nodeSpec.arch) {
ret.add new Constraint()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,9 @@ import groovy.util.logging.Slf4j
import io.nomadproject.client.ApiClient
import io.nomadproject.client.api.JobsApi
import io.nomadproject.client.model.*
import nextflow.nomad.config.ConstraintsSpec
import nextflow.nomad.models.JobConstraints
import nextflow.nomad.config.NomadConfig
import nextflow.nomad.config.VolumeSpec
import nextflow.nomad.models.JobVolume
import nextflow.processor.TaskRun
import nextflow.util.MemoryUnit
import nextflow.exception.ProcessSubmitException
Expand Down Expand Up @@ -136,7 +136,7 @@ class NomadService implements Closeable{
if( config.jobOpts().volumeSpec ) {
taskGroup.volumes = [:]
config.jobOpts().volumeSpec.eachWithIndex { volumeSpec , idx->
if (volumeSpec && volumeSpec.type == VolumeSpec.VOLUME_CSI_TYPE) {
if (volumeSpec && volumeSpec.type == JobVolume.VOLUME_CSI_TYPE) {
taskGroup.volumes["vol_${idx}".toString()] = new VolumeRequest(
type: volumeSpec.type,
source: volumeSpec.name,
Expand All @@ -146,7 +146,7 @@ class NomadService implements Closeable{
)
}

if (volumeSpec && volumeSpec.type == VolumeSpec.VOLUME_HOST_TYPE) {
if (volumeSpec && volumeSpec.type == JobVolume.VOLUME_HOST_TYPE) {
taskGroup.volumes["vol_${idx}".toString()] = new VolumeRequest(
type: volumeSpec.type,
source: volumeSpec.name,
Expand Down Expand Up @@ -183,7 +183,7 @@ class NomadService implements Closeable{

volumes(task, taskDef, workingDir)
affinity(task, taskDef)
constrain(task, taskDef)
constraint(task, taskDef)
constraints(task, taskDef)

return taskDef
Expand Down Expand Up @@ -235,7 +235,7 @@ class NomadService implements Closeable{
taskDef
}

protected Task constrain(TaskRun task, Task taskDef){
protected Task constraint(TaskRun task, Task taskDef){
if( config.jobOpts().constraintSpec ){
def constraint = new Constraint()
if(config.jobOpts().constraintSpec.attribute){
Expand Down Expand Up @@ -264,7 +264,7 @@ class NomadService implements Closeable{
if( task.processor?.config?.get(TaskDirectives.CONSTRAINTS) &&
task.processor?.config?.get(TaskDirectives.CONSTRAINTS) instanceof Closure) {
Closure closure = task.processor?.config?.get(TaskDirectives.CONSTRAINTS) as Closure
ConstraintsSpec constraintsSpec = ConstraintsSpec.parse(closure)
JobConstraints constraintsSpec = JobConstraints.parse(closure)
def list = ConstraintsBuilder.constraintsSpecToList(constraintsSpec)
constraints.addAll(list)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,13 @@
* limitations under the License.
*/

package nextflow.nomad.config
package nextflow.nomad.models
/**
* Nomad Job Affinity Spec
*
* @author Jorge Aguilera <[email protected]>
*/
class AffinitySpec{
class JobAffinity {

private String attribute
private String operator
Expand All @@ -44,22 +44,22 @@ class AffinitySpec{
return weight
}

AffinitySpec attribute(String attribute){
JobAffinity attribute(String attribute){
this.attribute=attribute
this
}

AffinitySpec operator(String operator){
JobAffinity operator(String operator){
this.operator = operator
this
}

AffinitySpec value(String value){
JobAffinity value(String value){
this.value = value
this
}

AffinitySpec weight(int weight){
JobAffinity weight(int weight){
this.weight = weight
this
}
Expand Down
Loading

0 comments on commit e6d3dd3

Please sign in to comment.