Search in sources :

Example 1 with TaskAssignmentException

use of org.apache.kafka.streams.errors.TaskAssignmentException in project kafka by apache.

the class StreamPartitionAssignor method onAssignment.

/**
     * @throws TaskAssignmentException if there is no task id for one of the partitions specified
     */
@Override
public void onAssignment(Assignment assignment) {
    List<TopicPartition> partitions = new ArrayList<>(assignment.partitions());
    Collections.sort(partitions, PARTITION_COMPARATOR);
    AssignmentInfo info = AssignmentInfo.decode(assignment.userData());
    this.standbyTasks = info.standbyTasks;
    this.activeTasks = new HashMap<>();
    // could be duplicated if one task has more than one assigned partitions
    if (partitions.size() != info.activeTasks.size()) {
        throw new TaskAssignmentException(String.format("stream-thread [%s] Number of assigned partitions %d is not equal to the number of active taskIds %d" + ", assignmentInfo=%s", streamThread.getName(), partitions.size(), info.activeTasks.size(), info.toString()));
    }
    for (int i = 0; i < partitions.size(); i++) {
        TopicPartition partition = partitions.get(i);
        TaskId id = info.activeTasks.get(i);
        Set<TopicPartition> assignedPartitions = activeTasks.get(id);
        if (assignedPartitions == null) {
            assignedPartitions = new HashSet<>();
            activeTasks.put(id, assignedPartitions);
        }
        assignedPartitions.add(partition);
    }
    this.partitionsByHostState = info.partitionsByHost;
    final Collection<Set<TopicPartition>> values = partitionsByHostState.values();
    final Map<TopicPartition, PartitionInfo> topicToPartitionInfo = new HashMap<>();
    for (Set<TopicPartition> value : values) {
        for (TopicPartition topicPartition : value) {
            topicToPartitionInfo.put(topicPartition, new PartitionInfo(topicPartition.topic(), topicPartition.partition(), null, new Node[0], new Node[0]));
        }
    }
    metadataWithInternalTopics = Cluster.empty().withPartitions(topicToPartitionInfo);
}
Also used : TaskAssignmentException(org.apache.kafka.streams.errors.TaskAssignmentException) TaskId(org.apache.kafka.streams.processor.TaskId) HashSet(java.util.HashSet) Set(java.util.Set) HashMap(java.util.HashMap) Node(org.apache.kafka.common.Node) ArrayList(java.util.ArrayList) AssignmentInfo(org.apache.kafka.streams.processor.internals.assignment.AssignmentInfo) TopicPartition(org.apache.kafka.common.TopicPartition) PartitionInfo(org.apache.kafka.common.PartitionInfo)

Example 2 with TaskAssignmentException

use of org.apache.kafka.streams.errors.TaskAssignmentException in project apache-kafka-on-k8s by banzaicloud.

the class AssignmentInfo method encode.

/**
 * @throws TaskAssignmentException if method fails to encode the data, e.g., if there is an
 * IO exception during encoding
 */
public ByteBuffer encode() {
    final ByteArrayOutputStream baos = new ByteArrayOutputStream();
    try (final DataOutputStream out = new DataOutputStream(baos)) {
        switch(usedVersion) {
            case 1:
                encodeVersionOne(out);
                break;
            case 2:
                encodeVersionTwo(out);
                break;
            default:
                throw new IllegalStateException("Unknown metadata version: " + usedVersion + "; latest supported version: " + LATEST_SUPPORTED_VERSION);
        }
        out.flush();
        out.close();
        return ByteBuffer.wrap(baos.toByteArray());
    } catch (final IOException ex) {
        throw new TaskAssignmentException("Failed to encode AssignmentInfo", ex);
    }
}
Also used : TaskAssignmentException(org.apache.kafka.streams.errors.TaskAssignmentException) DataOutputStream(java.io.DataOutputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException)

Example 3 with TaskAssignmentException

use of org.apache.kafka.streams.errors.TaskAssignmentException in project apache-kafka-on-k8s by banzaicloud.

the class AssignmentInfo method decode.

/**
 * @throws TaskAssignmentException if method fails to decode the data or if the data version is unknown
 */
public static AssignmentInfo decode(final ByteBuffer data) {
    // ensure we are at the beginning of the ByteBuffer
    data.rewind();
    try (final DataInputStream in = new DataInputStream(new ByteBufferInputStream(data))) {
        // decode used version
        final int usedVersion = in.readInt();
        final AssignmentInfo assignmentInfo = new AssignmentInfo(usedVersion);
        switch(usedVersion) {
            case 1:
                decodeVersionOneData(assignmentInfo, in);
                break;
            case 2:
                decodeVersionTwoData(assignmentInfo, in);
                break;
            default:
                TaskAssignmentException fatalException = new TaskAssignmentException("Unable to decode subscription data: " + "used version: " + usedVersion + "; latest supported version: " + LATEST_SUPPORTED_VERSION);
                log.error(fatalException.getMessage(), fatalException);
                throw fatalException;
        }
        return assignmentInfo;
    } catch (final IOException ex) {
        throw new TaskAssignmentException("Failed to decode AssignmentInfo", ex);
    }
}
Also used : TaskAssignmentException(org.apache.kafka.streams.errors.TaskAssignmentException) ByteBufferInputStream(org.apache.kafka.common.utils.ByteBufferInputStream) IOException(java.io.IOException) DataInputStream(java.io.DataInputStream)

Example 4 with TaskAssignmentException

use of org.apache.kafka.streams.errors.TaskAssignmentException in project apache-kafka-on-k8s by banzaicloud.

the class SubscriptionInfo method decode.

/**
 * @throws TaskAssignmentException if method fails to decode the data
 */
public static SubscriptionInfo decode(final ByteBuffer data) {
    // ensure we are at the beginning of the ByteBuffer
    data.rewind();
    // decode used version
    final int usedVersion = data.getInt();
    final SubscriptionInfo subscriptionInfo = new SubscriptionInfo(usedVersion);
    switch(usedVersion) {
        case 1:
            decodeVersionOneData(subscriptionInfo, data);
            break;
        case 2:
            decodeVersionTwoData(subscriptionInfo, data);
            break;
        default:
            TaskAssignmentException fatalException = new TaskAssignmentException("Unable to decode subscription data: " + "used version: " + usedVersion + "; latest supported version: " + LATEST_SUPPORTED_VERSION);
            log.error(fatalException.getMessage(), fatalException);
            throw fatalException;
    }
    return subscriptionInfo;
}
Also used : TaskAssignmentException(org.apache.kafka.streams.errors.TaskAssignmentException)

Example 5 with TaskAssignmentException

use of org.apache.kafka.streams.errors.TaskAssignmentException in project apache-kafka-on-k8s by banzaicloud.

the class StreamsPartitionAssignor method processVersionOneAssignment.

private void processVersionOneAssignment(final AssignmentInfo info, final List<TopicPartition> partitions, final Map<TaskId, Set<TopicPartition>> activeTasks) {
    // could be duplicated if one task has more than one assigned partitions
    if (partitions.size() != info.activeTasks().size()) {
        throw new TaskAssignmentException(String.format("%sNumber of assigned partitions %d is not equal to the number of active taskIds %d" + ", assignmentInfo=%s", logPrefix, partitions.size(), info.activeTasks().size(), info.toString()));
    }
    for (int i = 0; i < partitions.size(); i++) {
        final TopicPartition partition = partitions.get(i);
        final TaskId id = info.activeTasks().get(i);
        Set<TopicPartition> assignedPartitions = activeTasks.get(id);
        if (assignedPartitions == null) {
            assignedPartitions = new HashSet<>();
            activeTasks.put(id, assignedPartitions);
        }
        assignedPartitions.add(partition);
    }
}
Also used : TaskAssignmentException(org.apache.kafka.streams.errors.TaskAssignmentException) TaskId(org.apache.kafka.streams.processor.TaskId) TopicPartition(org.apache.kafka.common.TopicPartition)

Aggregations

TaskAssignmentException (org.apache.kafka.streams.errors.TaskAssignmentException)19 TaskId (org.apache.kafka.streams.processor.TaskId)6 HashMap (java.util.HashMap)5 IOException (java.io.IOException)4 HashSet (java.util.HashSet)4 Set (java.util.Set)4 TopicPartition (org.apache.kafka.common.TopicPartition)4 TopicsInfo (org.apache.kafka.streams.processor.internals.InternalTopologyBuilder.TopicsInfo)4 Test (org.junit.Test)4 ArrayList (java.util.ArrayList)3 Map (java.util.Map)3 UUID (java.util.UUID)3 ByteArrayOutputStream (java.io.ByteArrayOutputStream)2 DataInputStream (java.io.DataInputStream)2 DataOutputStream (java.io.DataOutputStream)2 List (java.util.List)2 SortedSet (java.util.SortedSet)2 TreeMap (java.util.TreeMap)2 TreeSet (java.util.TreeSet)2 UUID.randomUUID (java.util.UUID.randomUUID)2