Search in sources :

Example 1 with TaskIdFormatException

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

the class StreamThread method cachedTasks.

/**
     * Returns ids of tasks whose states are kept on the local storage.
     */
public Set<TaskId> cachedTasks() {
    // A client could contain some inactive tasks whose states are still kept on the local storage in the following scenarios:
    // 1) the client is actively maintaining standby tasks by maintaining their states from the change log.
    // 2) the client has just got some tasks migrated out of itself to other clients while these task states
    //    have not been cleaned up yet (this can happen in a rolling bounce upgrade, for example).
    HashSet<TaskId> tasks = new HashSet<>();
    File[] stateDirs = stateDirectory.listTaskDirectories();
    if (stateDirs != null) {
        for (File dir : stateDirs) {
            try {
                TaskId id = TaskId.parse(dir.getName());
                // if the checkpoint file exists, the state is valid.
                if (new File(dir, ProcessorStateManager.CHECKPOINT_FILE_NAME).exists())
                    tasks.add(id);
            } catch (TaskIdFormatException e) {
            // there may be some unknown files that sits in the same directory,
            // we should ignore these files instead trying to delete them as well
            }
        }
    }
    return tasks;
}
Also used : TaskId(org.apache.kafka.streams.processor.TaskId) File(java.io.File) HashSet(java.util.HashSet) TaskIdFormatException(org.apache.kafka.streams.errors.TaskIdFormatException)

Example 2 with TaskIdFormatException

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

the class TaskId method parse.

/**
 * @throws TaskIdFormatException if the string is not a valid {@link TaskId}
 */
public static TaskId parse(String string) {
    int index = string.indexOf('_');
    if (index <= 0 || index + 1 >= string.length())
        throw new TaskIdFormatException(string);
    try {
        int topicGroupId = Integer.parseInt(string.substring(0, index));
        int partition = Integer.parseInt(string.substring(index + 1));
        return new TaskId(topicGroupId, partition);
    } catch (Exception e) {
        throw new TaskIdFormatException(string);
    }
}
Also used : TaskIdFormatException(org.apache.kafka.streams.errors.TaskIdFormatException) IOException(java.io.IOException) TaskIdFormatException(org.apache.kafka.streams.errors.TaskIdFormatException)

Example 3 with TaskIdFormatException

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

the class TaskManager method tryToLockAllNonEmptyTaskDirectories.

/**
 * Makes a weak attempt to lock all non-empty task directories in the state dir. We are responsible for computing and
 * reporting the offset sum for any unassigned tasks we obtain the lock for in the upcoming rebalance. Tasks
 * that we locked but didn't own will be released at the end of the rebalance (unless of course we were
 * assigned the task as a result of the rebalance). This method should be idempotent.
 */
private void tryToLockAllNonEmptyTaskDirectories() {
    // Always clear the set at the beginning as we're always dealing with the
    // current set of actually-locked tasks.
    lockedTaskDirectories.clear();
    for (final TaskDirectory taskDir : stateDirectory.listNonEmptyTaskDirectories()) {
        final File dir = taskDir.file();
        final String namedTopology = taskDir.namedTopology();
        try {
            final TaskId id = parseTaskDirectoryName(dir.getName(), namedTopology);
            if (stateDirectory.lock(id)) {
                lockedTaskDirectories.add(id);
                if (!tasks.owned(id)) {
                    log.debug("Temporarily locked unassigned task {} for the upcoming rebalance", id);
                }
            }
        } catch (final TaskIdFormatException e) {
        // ignore any unknown files that sit in the same directory
        }
    }
}
Also used : TaskId(org.apache.kafka.streams.processor.TaskId) TaskDirectory(org.apache.kafka.streams.processor.internals.StateDirectory.TaskDirectory) File(java.io.File) TaskIdFormatException(org.apache.kafka.streams.errors.TaskIdFormatException)

Example 4 with TaskIdFormatException

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

the class StateManagerUtil method parseTaskDirectoryName.

/**
 *  Parse the task directory name (of the form topicGroupId_partition) and construct the TaskId with the
 *  optional namedTopology (may be null)
 *
 *  @throws TaskIdFormatException if the taskIdStr is not a valid {@link TaskId}
 */
static TaskId parseTaskDirectoryName(final String taskIdStr, final String namedTopology) {
    final int index = taskIdStr.indexOf('_');
    if (index <= 0 || index + 1 >= taskIdStr.length()) {
        throw new TaskIdFormatException(taskIdStr);
    }
    try {
        final int topicGroupId = Integer.parseInt(taskIdStr.substring(0, index));
        final int partition = Integer.parseInt(taskIdStr.substring(index + 1));
        return new TaskId(topicGroupId, partition, namedTopology);
    } catch (final Exception e) {
        throw new TaskIdFormatException(taskIdStr);
    }
}
Also used : TaskId(org.apache.kafka.streams.processor.TaskId) TaskIdFormatException(org.apache.kafka.streams.errors.TaskIdFormatException) ProcessorStateException(org.apache.kafka.streams.errors.ProcessorStateException) IOException(java.io.IOException) StreamsException(org.apache.kafka.streams.errors.StreamsException) LockException(org.apache.kafka.streams.errors.LockException) TaskIdFormatException(org.apache.kafka.streams.errors.TaskIdFormatException)

Example 5 with TaskIdFormatException

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

the class TaskManager method cachedTasksIds.

/**
 * Returns ids of tasks whose states are kept on the local storage.
 */
Set<TaskId> cachedTasksIds() {
    // A client could contain some inactive tasks whose states are still kept on the local storage in the following scenarios:
    // 1) the client is actively maintaining standby tasks by maintaining their states from the change log.
    // 2) the client has just got some tasks migrated out of itself to other clients while these task states
    // have not been cleaned up yet (this can happen in a rolling bounce upgrade, for example).
    final HashSet<TaskId> tasks = new HashSet<>();
    final File[] stateDirs = taskCreator.stateDirectory().listTaskDirectories();
    if (stateDirs != null) {
        for (final File dir : stateDirs) {
            try {
                final TaskId id = TaskId.parse(dir.getName());
                // if the checkpoint file exists, the state is valid.
                if (new File(dir, ProcessorStateManager.CHECKPOINT_FILE_NAME).exists()) {
                    tasks.add(id);
                }
            } catch (final TaskIdFormatException e) {
            // there may be some unknown files that sits in the same directory,
            // we should ignore these files instead trying to delete them as well
            }
        }
    }
    return tasks;
}
Also used : TaskId(org.apache.kafka.streams.processor.TaskId) File(java.io.File) HashSet(java.util.HashSet) TaskIdFormatException(org.apache.kafka.streams.errors.TaskIdFormatException)

Aggregations

TaskIdFormatException (org.apache.kafka.streams.errors.TaskIdFormatException)6 TaskId (org.apache.kafka.streams.processor.TaskId)4 File (java.io.File)3 IOException (java.io.IOException)3 HashSet (java.util.HashSet)2 LockException (org.apache.kafka.streams.errors.LockException)1 ProcessorStateException (org.apache.kafka.streams.errors.ProcessorStateException)1 StreamsException (org.apache.kafka.streams.errors.StreamsException)1 TaskDirectory (org.apache.kafka.streams.processor.internals.StateDirectory.TaskDirectory)1