-
Notifications
You must be signed in to change notification settings - Fork 529
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Tony L. Kerz
authored and
Tony L. Kerz
committed
Oct 9, 2015
1 parent
4d4e37a
commit 17ad52c
Showing
4 changed files
with
126 additions
and
13 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
35 changes: 35 additions & 0 deletions
35
src/main/scala/org/apache/mesos/chronos/scheduler/mesos/ConstraintChecker.scala
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,35 @@ | ||
package org.apache.mesos.chronos.scheduler.mesos | ||
|
||
import org.apache.mesos.Protos | ||
import org.apache.mesos.chronos.scheduler.jobs.constraints.Constraint | ||
import java.util.logging.Logger | ||
import scala.collection.JavaConverters._ | ||
|
||
/** | ||
* Helper for checking resource offer against job constraints | ||
* | ||
* @author tony kerz (anthony.kerz@gmail.com) | ||
*/ | ||
class ConstraintChecker { | ||
private[this] val log = Logger.getLogger(getClass.getName) | ||
val Hostname = "hostname" | ||
|
||
def checkConstraints(offer: Protos.Offer, constraints: Seq[Constraint]): Boolean = { | ||
var attributes = offer.getAttributesList.asScala | ||
|
||
if (!attributes.exists(attr => attr.getName == Hostname)) { | ||
log.fine(s"adding hostname-attribute=${offer.getHostname} to offer=${offer}") | ||
val hostnameText = Protos.Value.Text.newBuilder().setValue(offer.getHostname).build() | ||
val hostnameAttribute = Protos.Attribute.newBuilder().setName(Hostname).setText(hostnameText).setType(Protos.Value.Type.TEXT).build() | ||
attributes = offer.getAttributesList.asScala :+ hostnameAttribute | ||
} | ||
|
||
constraints.foreach { c => | ||
if (!c.matches(attributes)) { | ||
return false | ||
} | ||
} | ||
true | ||
} | ||
|
||
} |
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
85 changes: 85 additions & 0 deletions
85
src/test/scala/org/apache/mesos/chronos/scheduler/mesos/ConstraintCheckerSpec.scala
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,85 @@ | ||
package org.apache.mesos.chronos.scheduler.mesos | ||
|
||
import mesosphere.mesos.protos._ | ||
import org.apache.mesos.chronos.scheduler.jobs.constraints.ConstraintSpecHelper | ||
import org.apache.mesos.Protos | ||
import java.util.logging.Logger | ||
import org.specs2.mock.Mockito | ||
import org.specs2.mutable.SpecificationWithJUnit | ||
import org.apache.mesos.chronos.scheduler.jobs.constraints.LikeConstraint | ||
import org.apache.mesos.chronos.scheduler.jobs.constraints.EqualsConstraint | ||
import mesosphere.mesos.protos.Implicits._ | ||
import org.specs2.specification.BeforeEach | ||
|
||
/** | ||
* spec for testing ConstraintChecker | ||
* | ||
* @author tony kerz (anthony.kerz@gmail.com) | ||
*/ | ||
class ConstraintCheckerSpec extends SpecificationWithJUnit | ||
with Mockito | ||
with ConstraintSpecHelper | ||
{ | ||
isolated | ||
|
||
val offer = Protos.Offer.newBuilder() | ||
.setId(OfferID("1")) | ||
.setFrameworkId(FrameworkID("chronos")) | ||
.setSlaveId(SlaveID("slave-1")) | ||
.setHostname("slave.one.com") | ||
.addAttributes(createTextAttribute("rack", "rack-1")) | ||
.build() | ||
|
||
val offerWithHostname = Protos.Offer.newBuilder() | ||
.setId(OfferID("1")) | ||
.setFrameworkId(FrameworkID("chronos")) | ||
.setSlaveId(SlaveID("slave-1")) | ||
.setHostname("slave.one.com") | ||
.addAttributes(createTextAttribute("hostname", "slave.explicit.com")) | ||
.build() | ||
|
||
val constraintChecker = new ConstraintChecker() | ||
|
||
"check constraints" should { | ||
|
||
"be true when equal" in { | ||
val constraints = Seq(EqualsConstraint("rack", "rack-1")) | ||
constraintChecker.checkConstraints(offer, constraints) must beTrue | ||
} | ||
|
||
"be false when not equal" in { | ||
val constraints = Seq(EqualsConstraint("rack", "rack-2")) | ||
constraintChecker.checkConstraints(offer, constraints) must beFalse | ||
} | ||
|
||
"be true when like" in { | ||
val constraints = Seq(LikeConstraint("rack", "rack-[1-3]")) | ||
constraintChecker.checkConstraints(offer, constraints) must beTrue | ||
} | ||
|
||
"be false when not like" in { | ||
val constraints = Seq(LikeConstraint("rack", "rack-[2-3]")) | ||
constraintChecker.checkConstraints(offer, constraints) must beFalse | ||
} | ||
|
||
"be true when hostname equal" in { | ||
val constraints = Seq(EqualsConstraint("hostname", "slave.one.com")) | ||
constraintChecker.checkConstraints(offer, constraints) must beTrue | ||
} | ||
|
||
"be false when hostname not equal" in { | ||
val constraints = Seq(EqualsConstraint("hostname", "slave.two.com")) | ||
constraintChecker.checkConstraints(offer, constraints) must beFalse | ||
} | ||
|
||
"be false when hostname explicitly set to something else and not equal" in { | ||
val constraints = Seq(EqualsConstraint("hostname", "slave.one.com")) | ||
constraintChecker.checkConstraints(offerWithHostname, constraints) must beFalse | ||
} | ||
|
||
"be true when hostname explicitly set to something else and equal" in { | ||
val constraints = Seq(EqualsConstraint("hostname", "slave.explicit.com")) | ||
constraintChecker.checkConstraints(offerWithHostname, constraints) must beTrue | ||
} | ||
} | ||
} |