public class TaskScheduler
extends java.lang.Object
scheduleOnce()
method with a list of task requests and a list of new resource
lease offers, and that method will return a set of task assignments.
The TaskScheduler
stores any unused lease offers and will apply them during future calls to
scheduleOnce()
until a time expires, which is defined by the lease offer expiry time that you set
when you build the TaskScheduler
(the default is 10 seconds). Upon reaching the expiry time, the
TaskScheduler
rejects expired resource lease offers by invoking the action you supplied then you
built the TaskScheduler
.
Note that when you launch a task that has been scheduled by the TaskScheduler
, you should call
the task assigner action available from the getTaskAssigner()
method. When that
task completes, you should call the task unassigner action available from the
getTaskUnAssigner()
method. These actions make the TaskScheduler
keep
track of launched tasks. The TaskScheduler
then makes these tracked tasks available to its
scheduling optimization functions.
Do not call the scheduler concurrently. The scheduler assigns tasks in the order that they are received in a
particular list. It checks each task against available resources until it finds a match.
You create your TaskScheduler
by means of the TaskScheduler.Builder
. It provides methods with
which you can adjust the scheduler's autoscaling rules, fitness calculators, and so forth.
Modifier and Type | Class and Description |
---|---|
static class |
TaskScheduler.Builder
The Builder is how you construct a
TaskScheduler object with particular characteristics. |
Modifier and Type | Method and Description |
---|---|
void |
addOrReplaceAutoScaleRule(AutoScaleRule rule)
Add a new autoscale rule to those used by this scheduler.
|
void |
addOrReplaceResAllocs(ResAllocs resAllocs)
Add a new resource allocation, or replace an existing one of the same name.
|
void |
disableVM(java.lang.String hostname,
long durationMillis)
Disable the virtual machine with the specified hostname.
|
boolean |
disableVMByVMId(java.lang.String vmID,
long durationMillis)
Disable the virtual machine with the specified ID.
|
void |
enableVM(java.lang.String hostname)
Enable the VM with the specified host name.
|
void |
expireAllLeases()
Call this method to instruct the task scheduler to reject all of the unused offers it is currently
holding.
|
void |
expireAllLeases(java.lang.String hostname)
Call this method to instruct the task scheduler to reject all of the unused offers it is currently
holding that concern resources offered by the host with the name
hostname . |
boolean |
expireAllLeasesByVMId(java.lang.String vmId)
Call this method to instruct the task scheduler to reject all of the unused offers it is currently
holding that concern resources offered by the host with the ID,
vmId . |
void |
expireLease(java.lang.String leaseId)
Call this method to instruct the task scheduler to reject a particular resource offer.
|
java.util.Collection<AutoScaleRule> |
getAutoScaleRules()
Get the autoscale rules currently registered with the scheduler.
|
java.util.Map<java.lang.String,ResAllocs> |
getResAllocs()
Get the current mapping of resource allocations registered with the scheduler.
|
java.util.Map<java.lang.String,java.util.Map<VMResource,java.lang.Double[]>> |
getResourceStatus()
Returns the state of resources on all known hosts.
|
Action2<TaskRequest,java.lang.String> |
getTaskAssigner()
Get the task assigner action.
|
TaskTracker |
getTaskTracker() |
Action2<java.lang.String,java.lang.String> |
getTaskUnAssigner()
Get the task unassigner action.
|
java.util.List<VirtualMachineCurrentState> |
getVmCurrentStates()
Returns the current state of all known hosts.
|
void |
removeAutoScaleRule(java.lang.String ruleName)
Remove the autoscale rule associated with the given name from those used by the scheduler.
|
void |
removeResAllocs(java.lang.String groupName)
Remove a resource allocation associated with the specified name.
|
SchedulingResult |
scheduleOnce(java.util.List<? extends TaskRequest> requests,
java.util.List<VirtualMachineLease> newLeases)
Schedule a list of task requests by using any newly-added resource leases in addition to any
previously-unused leases.
|
void |
setActiveVmGroupAttributeName(java.lang.String attributeName)
Set how the scheduler determines to which group the VM (host) belongs.
|
void |
setActiveVmGroups(java.util.List<java.lang.String> vmGroups)
Set the list of VM group names that are active.
|
void |
setAutoscalerCallback(Action1<AutoScaleAction> callback)
Set the autoscale call back action.
|
void |
shutdown()
Mark task scheduler as shutdown and shutdown any thread pool executors created.
|
public void setAutoscalerCallback(Action1<AutoScaleAction> callback) throws java.lang.IllegalStateException
callback
- the callback to invoke for autoscale actionsjava.lang.IllegalStateException
- if no autoscaler was establishedpublic TaskTracker getTaskTracker()
public java.util.Map<java.lang.String,ResAllocs> getResAllocs()
public void addOrReplaceResAllocs(ResAllocs resAllocs)
resAllocs
- the resource allocation to add or replacepublic void removeResAllocs(java.lang.String groupName)
groupName
- the name of the resource allocation to removepublic java.util.Collection<AutoScaleRule> getAutoScaleRules()
public void addOrReplaceAutoScaleRule(AutoScaleRule rule)
rule
- the autoscale rule to addpublic void removeAutoScaleRule(java.lang.String ruleName)
ruleName
- name of the autoscale rule to removepublic SchedulingResult scheduleOnce(java.util.List<? extends TaskRequest> requests, java.util.List<VirtualMachineLease> newLeases) throws java.lang.IllegalStateException
You cannot add a lease object with an Id equal to that of a stored lease object; scheduleOnce()
will throw an IllegalStateException
. Upon throwing this exception, if you provided multiple
leases in the newLeases
argument, the state of internally maintained list of unused leases will
be in an indeterminate state - some of the leases may have been successfully added.
The task scheduler rejects any expired leases before scheduling begins. Then, it combines all leases of a host to determine the total available resources on the host. The scheduler then tries each task request, in the order that they appear in the given list, for assignment against the available hosts until successful. For each task the scheduler returns either a successful assignment result, or, a set of assignment failures.
After the scheduler evaluates all assignments, it will reject remaining leases if they are unused and their offer time is further in the past than lease expiration interval. This prevents the scheduler from hoarding leases. If you provided an autoscaler, the scheduler then launches autoscale evaluation to run asynchronously, which runs each registered autoscale rule based on its policy.
The successful assignments contain hosts to which tasks have been successfully assigned and the offers for that
host that were used for the assignments. Fenzo removes those offers from its internal state. Normally, you
would use those offers to launch the tasks. For any reason if you do not launch those tasks, you must either
reject the offers to Mesos, or, re-add them to Fenzo with the next call to scheduleOnce(List, List)
.
Otherwise, those offers would be "leaked out".
Unexpected exceptions may arise during scheduling, for example, due to uncaught exceptions in user provided plugins. The scheduling routine stops upon catching any unexpected exceptions. These exceptions are surfaced to you in one or both of two ways.
SchedulingResult.getExceptions()
. In this case, no assignments would have been made.IllegalStateException
with its cause set to the uncaught exception. In this
case the internal state of Fenzo will be undefined.requests
- a list of task requests to match with resources, in their given ordernewLeases
- new resource leases from hosts that the scheduler can use along with any previously
ununsed leasesSchedulingResult
object that contains a task assignment results map and other summariesjava.lang.IllegalStateException
- if you call this method concurrently, or, if you try to add an existing lease
again, or, if there was unexpected exception during the scheduling iteration, or, if using
TaskSchedulingService
, which will instead invoke scheduling from within. Unexpected exceptions
can arise from uncaught exceptions in user defined plugins. It is also thrown if the scheduler has been shutdown
via the shutdown()
method.public java.util.Map<java.lang.String,java.util.Map<VMResource,java.lang.Double[]>> getResourceStatus() throws java.lang.IllegalStateException
java.lang.IllegalStateException
- if called concurrently with scheduleOnce(List, List)
or if called when
using a TaskSchedulingService
.public java.util.List<VirtualMachineCurrentState> getVmCurrentStates() throws java.lang.IllegalStateException
java.lang.IllegalStateException
- if called concurrently with scheduleOnce(List, List)
or if called when
using a TaskSchedulingService
.public void expireLease(java.lang.String leaseId) throws java.lang.IllegalStateException
leaseId
- the lease ID of the lease to expirejava.lang.IllegalStateException
- if the scheduler is shutdown via the isShutdown
method.public void expireAllLeases(java.lang.String hostname) throws java.lang.IllegalStateException
hostname
.hostname
- the name of the host whose leases you want to expirejava.lang.IllegalStateException
- if the scheduler is shutdown via the isShutdown
method.public boolean expireAllLeasesByVMId(java.lang.String vmId) throws java.lang.IllegalStateException
vmId
.vmId
- the ID of the host whose leases you want to expiretrue
if the given ID matches a known host, false
otherwise.java.lang.IllegalStateException
- if the scheduler is shutdown via the isShutdown
method.public void expireAllLeases() throws java.lang.IllegalStateException
java.lang.IllegalStateException
- if the scheduler is shutdown via the isShutdown
method.public Action2<TaskRequest,java.lang.String> getTaskAssigner() throws java.lang.IllegalStateException
getTaskAssigner().call()
method in order to notify Fenzo that the task has actually been deployed
on a host. Pass two arguments to this call method: the TaskRequest
object for the task assigned and
the hostname.
In addition, in your framework's task completion callback that you supply to Mesos, you must call your
task scheduler's getTaskUnassigner().call()
method to notify Fenzo that the
task is no longer assigned.
Some scheduling optimizers need to know not only which tasks are waiting to be scheduled and which hosts have resource offers available, but also which tasks have previously been assigned and are currently running on hosts. These two methods help Fenzo provide this information to these scheduling optimizers.
Note that you may not call the task assigner action concurrently with
scheduleOnce()
. If you do so, the task assigner
action will throw an IllegalStateException
.
java.lang.IllegalStateException
- if the scheduler is shutdown via the isShutdown
method.public Action2<java.lang.String,java.lang.String> getTaskUnAssigner() throws java.lang.IllegalStateException
call()
method to unassign an assignment you
have previously set for each task that completes so that internal state is maintained correctly. Pass two
String arguments to this call method: the taskId and the hostname.
For each task you assign and launch, you must call your task scheduler's
getTaskAssigner().call()
method in order to notify Fenzo that the task has
actually been deployed on a host.
In addition, in your framework's task completion callback that you supply to Mesos, you must call your
task scheduler's getTaskUnassigner().call()
method to notify Fenzo that the
task is no longer assigned.
Some scheduling optimizers need to know not only which tasks are waiting to be scheduled and which hosts have resource offers available, but also which tasks have previously been assigned and are currently running on hosts. These two methods help Fenzo provide this information to these scheduling optimizers.
This method is safe to be called concurrently with other calls to TaskScheduler
. The tasks to be
unassigned are stored internally and actually unassigned at the beginning of the next scheduling iteration,
that is, the next time scheduleOnce(List, List)
is called.
java.lang.IllegalStateException
- if the scheduler is shutdown via the isShutdown
method.public void disableVM(java.lang.String hostname, long durationMillis) throws java.lang.IllegalStateException
hostname
- the name of the host to disabledurationMillis
- the length of time, starting from now, in milliseconds, during which the host will
be disabledjava.lang.IllegalStateException
- if the scheduler is shutdown via the isShutdown
method.public boolean disableVMByVMId(java.lang.String vmID, long durationMillis) throws java.lang.IllegalStateException
vmID
- the ID of the host to disabledurationMillis
- the length of time, starting from now, in milliseconds, during which the host will
be disabledtrue
if the ID matches a known VM, false
otherwise.java.lang.IllegalStateException
- if the scheduler is shutdown via the isShutdown
method.public void enableVM(java.lang.String hostname) throws java.lang.IllegalStateException
hostname
- the name of the host to enablejava.lang.IllegalStateException
- if the scheduler is shutdown via the isShutdown
method.public void setActiveVmGroupAttributeName(java.lang.String attributeName)
attributeName
- the name of the attribute that determines a VM's grouppublic void setActiveVmGroups(java.util.List<java.lang.String> vmGroups)
vmGroups
- a list of VM group names that the scheduler is to consider to be enabled, or null
if the scheduler is to consider every group to be enabledpublic void shutdown()