Search in sources :

Example 1 with CircularIterator

use of org.apache.kafka.common.utils.CircularIterator in project kafka by apache.

the class WorkerCoordinator method performTaskAssignment.

private Map<String, ByteBuffer> performTaskAssignment(String leaderId, long maxOffset, Map<String, ConnectProtocol.WorkerState> memberConfigs) {
    Map<String, List<String>> connectorAssignments = new HashMap<>();
    Map<String, List<ConnectorTaskId>> taskAssignments = new HashMap<>();
    // Perform round-robin task assignment. Assign all connectors and then all tasks because assigning both the
    // connector and its tasks can lead to very uneven distribution of work in some common cases (e.g. for connectors
    // that generate only 1 task each; in a cluster of 2 or an even # of nodes, only even nodes will be assigned
    // connectors and only odd nodes will be assigned tasks, but tasks are, on average, actually more resource
    // intensive than connectors).
    List<String> connectorsSorted = sorted(configSnapshot.connectors());
    CircularIterator<String> memberIt = new CircularIterator<>(sorted(memberConfigs.keySet()));
    for (String connectorId : connectorsSorted) {
        String connectorAssignedTo = memberIt.next();
        log.trace("Assigning connector {} to {}", connectorId, connectorAssignedTo);
        List<String> memberConnectors = connectorAssignments.get(connectorAssignedTo);
        if (memberConnectors == null) {
            memberConnectors = new ArrayList<>();
            connectorAssignments.put(connectorAssignedTo, memberConnectors);
        }
        memberConnectors.add(connectorId);
    }
    for (String connectorId : connectorsSorted) {
        for (ConnectorTaskId taskId : sorted(configSnapshot.tasks(connectorId))) {
            String taskAssignedTo = memberIt.next();
            log.trace("Assigning task {} to {}", taskId, taskAssignedTo);
            List<ConnectorTaskId> memberTasks = taskAssignments.get(taskAssignedTo);
            if (memberTasks == null) {
                memberTasks = new ArrayList<>();
                taskAssignments.put(taskAssignedTo, memberTasks);
            }
            memberTasks.add(taskId);
        }
    }
    this.leaderState = new LeaderState(memberConfigs, connectorAssignments, taskAssignments);
    return fillAssignmentsAndSerialize(memberConfigs.keySet(), ConnectProtocol.Assignment.NO_ERROR, leaderId, memberConfigs.get(leaderId).url(), maxOffset, connectorAssignments, taskAssignments);
}
Also used : ConnectorTaskId(org.apache.kafka.connect.util.ConnectorTaskId) HashMap(java.util.HashMap) CircularIterator(org.apache.kafka.common.utils.CircularIterator) ArrayList(java.util.ArrayList) List(java.util.List)

Example 2 with CircularIterator

use of org.apache.kafka.common.utils.CircularIterator in project kafka by apache.

the class RoundRobinAssignor method assign.

@Override
public Map<String, List<TopicPartition>> assign(Map<String, Integer> partitionsPerTopic, Map<String, List<String>> subscriptions) {
    Map<String, List<TopicPartition>> assignment = new HashMap<>();
    for (String memberId : subscriptions.keySet()) assignment.put(memberId, new ArrayList<TopicPartition>());
    CircularIterator<String> assigner = new CircularIterator<>(Utils.sorted(subscriptions.keySet()));
    for (TopicPartition partition : allPartitionsSorted(partitionsPerTopic, subscriptions)) {
        final String topic = partition.topic();
        while (!subscriptions.get(assigner.peek()).contains(topic)) assigner.next();
        assignment.get(assigner.next()).add(partition);
    }
    return assignment;
}
Also used : HashMap(java.util.HashMap) TopicPartition(org.apache.kafka.common.TopicPartition) CircularIterator(org.apache.kafka.common.utils.CircularIterator) ArrayList(java.util.ArrayList) List(java.util.List) ArrayList(java.util.ArrayList)

Aggregations

ArrayList (java.util.ArrayList)2 HashMap (java.util.HashMap)2 List (java.util.List)2 CircularIterator (org.apache.kafka.common.utils.CircularIterator)2 TopicPartition (org.apache.kafka.common.TopicPartition)1 ConnectorTaskId (org.apache.kafka.connect.util.ConnectorTaskId)1