Skip to content
This repository has been archived by the owner on Mar 31, 2023. It is now read-only.

How to use Fenzo

David Gross edited this page Jul 19, 2015 · 6 revisions

Use the Sample Framework as a Guide

You can use our sample Fenzo-aware Mesos framework as a template for your first framework. This sample framework is implemented as the com.netflix.fenzo.samples.SampleFramework Java class.

Build a Task Scheduler

The first thing it does is to build Fenzo’s TaskScheduler object:

scheduler = new TaskScheduler.Builder()
            .withLeaseOfferExpirySecs(1000000000)
            .withLeaseRejectAction(new Action1<VirtualMachineLease>() {
               @Override
               public void call(VirtualMachineLease lease) {
                  mesosSchedulerDriverRef.get().declineOffer(lease.getOffer().getId());
               }
            })
            .build();

Start the Framework

After you construct the Framework, you set it into motion with the start() method. The sample framework then periodically collects resource offers from Mesos into a queue, leasesQueue and makes the pending tasks available in taskQueue. Then, the framework can schedule currently pending tasks this way:

taskQueue.drainTo(newTaskRequests);
newTaskRequests.add(0, taskRequest);
for(TaskRequest request: newTaskRequests)
  pendingTasksMap.put(request.getId(), request);
leasesQueue.drainTo(newLeases);
SchedulingResult schedulingResult = scheduler.scheduleOnce(new ArrayList<>(pendingTasksMap.values()), newLeases);

Launch the Assigned Tasks

The resulting object, schedulingResult contains a map of task assignment results, among other information about the schedule run. You can use this assignment map to launch the assigned tasks.

Map<String,VMAssignmentResult> resultMap = schedulingResult.getResultMap();
if(!resultMap.isEmpty()) {
  for(VMAssignmentResult result: resultMap.values()) {
    List<VirtualMachineLease> leasesUsed = result.getLeasesUsed();
    List<Protos.TaskInfo> taskInfos = new ArrayList<>();
    for(TaskAssignmentResult t: result.getTasksAssigned()) {
      taskInfos.add(getTaskInfo(leasesUsed, t.getTaskId()));
      // notify persistence: task moving from pending to launched state
      pendingTasksMap.remove(t.getTaskId());
      launchedTasks.put(t.getTaskId(), leasesUsed.get(0).hostname());
      // inform scheduler that this task assignment is committed
      scheduler.getTaskAssigner().call(t.getRequest(), leasesUsed.get(0).hostname());
    }
    List<Protos.OfferID> offerIDs = new ArrayList<>();
    for(VirtualMachineLease l: leasesUsed)
      offerIDs.add(l.getOffer().getId());
    mesosSchedulerDriver.launchTasks(offerIDs, taskInfos);
  }
}

Notify the Scheduler of Assigns and UnAssigns of Tasks

The Fenzo scheduler maintains state of running tasks in order to achieve scheduling optimizations. This state is triggered by two calls, one to mark that a task assignment is being launched, and another to mark that an assigned task is no longer assigned (for example, the task was completed or lost or killed). The state is made available to plugins that are registered for custom fitness calculators as well as constraint evaluators.

When you launch a task, call the scheduler’s getTaskAssigner().call() method to indicate the task is assigned.

scheduler.getTaskAssigner().call(taskAssignmentResult.getRequest(), vmAssignmentResult.getLeasesUsed().get(0).hostname());

When a task ends (or fails or is lost), call the scheduler’s getTaskUnassigner().call() method to indicate the task is no longer assigned:

scheduler.getTaskUnAssigner().call(taskId, launchedTasks.get(taskId));

Using Fitness Calculators

You can modify the framework when you build it so that it will use one of Fenzo’s built-in fitness calculators to achieve bin packing, for example:

scheduler = new TaskScheduler.Builder()
    ...
    .withFitnessCalculator(BinPackingFitnessCalculators.cpuMemBinPacker)
    ...

Or, you can define a custom fitness calculator class that implements the VMTaskFitnessCalculator interface.