Search in sources :

Example 1 with Instance

use of org.apache.pulsar.functions.proto.Function.Instance in project incubator-pulsar by apache.

the class RoundRobinScheduler method schedule.

@Override
public List<Assignment> schedule(List<Instance> unassignedFunctionInstances, List<Assignment> currentAssignments, List<String> workers) {
    Map<String, List<Assignment>> workerIdToAssignment = new HashMap<>();
    for (String workerId : workers) {
        workerIdToAssignment.put(workerId, new LinkedList<>());
    }
    for (Assignment existingAssignment : currentAssignments) {
        workerIdToAssignment.get(existingAssignment.getWorkerId()).add(existingAssignment);
    }
    for (Instance unassignedFunctionInstance : unassignedFunctionInstances) {
        String workerId = findNextWorker(workerIdToAssignment);
        Assignment newAssignment = Assignment.newBuilder().setInstance(unassignedFunctionInstance).setWorkerId(workerId).build();
        workerIdToAssignment.get(workerId).add(newAssignment);
    }
    List<Assignment> assignments = workerIdToAssignment.entrySet().stream().flatMap(entry -> entry.getValue().stream()).collect(Collectors.toList());
    return assignments;
}
Also used : Assignment(org.apache.pulsar.functions.proto.Function.Assignment) List(java.util.List) Map(java.util.Map) HashMap(java.util.HashMap) Assignment(org.apache.pulsar.functions.proto.Function.Assignment) Instance(org.apache.pulsar.functions.proto.Function.Instance) LinkedList(java.util.LinkedList) Collectors(java.util.stream.Collectors) HashMap(java.util.HashMap) Instance(org.apache.pulsar.functions.proto.Function.Instance) List(java.util.List) LinkedList(java.util.LinkedList)

Aggregations

HashMap (java.util.HashMap)1 LinkedList (java.util.LinkedList)1 List (java.util.List)1 Map (java.util.Map)1 Collectors (java.util.stream.Collectors)1 Assignment (org.apache.pulsar.functions.proto.Function.Assignment)1 Instance (org.apache.pulsar.functions.proto.Function.Instance)1