/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements. See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License. You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.apache.kafka.streams.processor.internals;

import org.apache.kafka.common.TopicPartition;
import org.apache.kafka.common.utils.LogContext;
import org.apache.kafka.streams.processor.TaskId;
import org.apache.kafka.streams.processor.internals.Task.State;

import org.slf4j.Logger;

import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;
import java.util.TreeMap;
import java.util.stream.Collectors;

import static org.apache.kafka.common.utils.Utils.filterMap;
import static org.apache.kafka.common.utils.Utils.union;

/**
 * All tasks contained by the Streams instance.
 *
 * Note that these tasks are shared between the TaskManager (stream thread) and the StateUpdater (restore thread),
 * i.e. all running active tasks are processed by the former and all restoring active tasks and standby tasks are
 * processed by the latter.
 */
class Tasks implements TasksRegistry {
    private final Logger log;

    // note that these two maps may be accessed by concurrent threads and hence
    // should be synchronized when accessed
    private final Map<TaskId, StreamTask> activeTasksPerId = new TreeMap<>();
    private final Map<TaskId, StandbyTask> standbyTasksPerId = new TreeMap<>();

    // Tasks may have been assigned for a NamedTopology that is not yet known by this host. When that occurs we stash
    // these unknown tasks until either the corresponding NamedTopology is added and we can create them at last, or
    // we receive a new assignment and they are revoked from the thread.
    private final Map<TaskId, Set<TopicPartition>> pendingActiveTasksToCreate = new HashMap<>();
    private final Map<TaskId, Set<TopicPartition>> pendingStandbyTasksToCreate = new HashMap<>();
    private final Set<Task> pendingTasksToInit = new HashSet<>();
    private final Set<Task> pendingTasksToClose = new HashSet<>();
    private final Set<TaskId> failedTaskIds = new HashSet<>();

    private final Map<TopicPartition, StreamTask> activeTasksPerPartition = new HashMap<>();

    Tasks(final LogContext logContext) {
        this.log = logContext.logger(getClass());
    }

    @Override
    public void clearPendingTasksToCreate() {
        pendingActiveTasksToCreate.clear();
        pendingStandbyTasksToCreate.clear();
    }

    @Override
    public Map<TaskId, Set<TopicPartition>> drainPendingActiveTasksForTopologies(final Set<String> currentTopologies) {
        final Map<TaskId, Set<TopicPartition>> pendingActiveTasksForTopologies =
            filterMap(pendingActiveTasksToCreate, t -> currentTopologies.contains(t.getKey().topologyName()));

        pendingActiveTasksToCreate.keySet().removeAll(pendingActiveTasksForTopologies.keySet());

        return pendingActiveTasksForTopologies;
    }

    @Override
    public Map<TaskId, Set<TopicPartition>> drainPendingStandbyTasksForTopologies(final Set<String> currentTopologies) {
        final Map<TaskId, Set<TopicPartition>> pendingActiveTasksForTopologies =
            filterMap(pendingStandbyTasksToCreate, t -> currentTopologies.contains(t.getKey().topologyName()));

        pendingStandbyTasksToCreate.keySet().removeAll(pendingActiveTasksForTopologies.keySet());

        return pendingActiveTasksForTopologies;
    }

    @Override
    public void addPendingActiveTasksToCreate(final Map<TaskId, Set<TopicPartition>> pendingTasks) {
        pendingActiveTasksToCreate.putAll(pendingTasks);
    }

    @Override
    public void addPendingStandbyTasksToCreate(final Map<TaskId, Set<TopicPartition>> pendingTasks) {
        pendingStandbyTasksToCreate.putAll(pendingTasks);
    }

    @Override
    public Set<Task> drainPendingTasksToInit() {
        final Set<Task> result = new HashSet<>(pendingTasksToInit);
        pendingTasksToInit.clear();
        return result;
    }

    @Override
    public Set<StreamTask> drainPendingActiveTasksToInit() {
        final Set<StreamTask> result = new HashSet<>();
        final Iterator<Task> iterator = pendingTasksToInit.iterator();
        while (iterator.hasNext()) {
            final Task task = iterator.next();
            if (task.isActive()) {
                result.add((StreamTask) task);
                iterator.remove();
            }
        }
        return result;
    }

    @Override
    public Set<StandbyTask> drainPendingStandbyTasksToInit() {
        final Set<StandbyTask> result = new HashSet<>();
        final Iterator<Task> iterator = pendingTasksToInit.iterator();
        while (iterator.hasNext()) {
            final Task task = iterator.next();
            if (!task.isActive()) {
                result.add((StandbyTask) task);
                iterator.remove();
            }
        }
        return result;
    }

    @Override
    public Set<Task> pendingTasksToInit() {
        return Collections.unmodifiableSet(pendingTasksToInit);
    }

    @Override
    public void addPendingTasksToInit(final Collection<? extends Task> tasks) {
        pendingTasksToInit.addAll(tasks);
    }

    @Override
    public boolean hasPendingTasksToInit() {
        return !pendingTasksToInit.isEmpty();
    }

    @Override
    public Set<Task> pendingTasksToClose() {
        return Collections.unmodifiableSet(pendingTasksToClose);
    }

    @Override
    public void addPendingTasksToClose(final Collection<? extends Task> tasks) {
        pendingTasksToClose.addAll(tasks);
    }

    @Override
    public boolean hasPendingTasksToClose() {
        return !pendingTasksToClose.isEmpty();
    }

    @Override
    public void addActiveTasks(final Collection<StreamTask> newTasks) {
        if (!newTasks.isEmpty()) {
            for (final StreamTask activeTask : newTasks) {
                addActiveTask(activeTask);
            }
        }
    }

    @Override
    public void addStandbyTasks(final Collection<StandbyTask> newTasks) {
        if (!newTasks.isEmpty()) {
            for (final StandbyTask standbyTask : newTasks) {
                addStandbyTask(standbyTask);
            }
        }
    }

    @Override
    public void addTask(final Task task) {
        if (task.isActive()) {
            addActiveTask((StreamTask) task);
        } else {
            addStandbyTask((StandbyTask) task);
        }
    }

    @Override
    public synchronized void addActiveTask(final StreamTask task) {
        final TaskId taskId = task.id();

        if (activeTasksPerId.containsKey(taskId)) {
            throw new IllegalStateException("Attempted to create an active task that we already own: " + taskId);
        }

        if (standbyTasksPerId.containsKey(taskId)) {
            throw new IllegalStateException("Attempted to create an active task while we already own its standby: " + taskId);
        }

        activeTasksPerId.put(taskId, task);
        pendingActiveTasksToCreate.remove(taskId);
        for (final TopicPartition topicPartition : task.inputPartitions()) {
            activeTasksPerPartition.put(topicPartition, task);
        }
    }

    @Override
    public synchronized void addStandbyTask(final StandbyTask task) {
        final TaskId taskId = task.id();

        if (standbyTasksPerId.containsKey(taskId)) {
            throw new IllegalStateException("Attempted to create an standby task that we already own: " + taskId);
        }

        if (activeTasksPerId.containsKey(taskId)) {
            throw new IllegalStateException("Attempted to create an standby task while we already own its active: " + taskId);
        }

        standbyTasksPerId.put(taskId, task);
    }

    @Override
    public void addFailedTask(final Task task) {
        failedTaskIds.add(task.id());
        addTask(task);
    }

    @Override
    public synchronized void removeTask(final Task taskToRemove) {
        final TaskId taskId = taskToRemove.id();

        if (taskToRemove.state() != Task.State.CLOSED && taskToRemove.state() != State.SUSPENDED) {
            throw new IllegalStateException("Attempted to remove a task that is not closed or suspended: " + taskId);
        }

        if (pendingTasksToClose.contains(taskToRemove)) {
            pendingTasksToClose.remove(taskToRemove);
        } else if (taskToRemove.isActive()) {
            if (activeTasksPerId.remove(taskId) == null) {
                throw new IllegalArgumentException("Attempted to remove an active task that is not owned: " + taskId);
            }
            removePartitionsForActiveTask(taskId);
        } else {
            if (standbyTasksPerId.remove(taskId) == null) {
                throw new IllegalArgumentException("Attempted to remove a standby task that is not owned: " + taskId);
            }
        }
        failedTaskIds.remove(taskId);
    }

    @Override
    public synchronized void replaceStandbyWithActive(final StreamTask activeTask) {
        final TaskId taskId = activeTask.id();
        if (standbyTasksPerId.remove(taskId) == null) {
            throw new IllegalStateException("Attempted to convert unknown standby task to stream task: " + taskId);
        }

        activeTasksPerId.put(activeTask.id(), activeTask);
        for (final TopicPartition topicPartition : activeTask.inputPartitions()) {
            activeTasksPerPartition.put(topicPartition, activeTask);
        }
    }

    @Override
    public boolean updateActiveTaskInputPartitions(final StreamTask task, final Set<TopicPartition> topicPartitions) {
        final boolean requiresUpdate = !task.inputPartitions().equals(topicPartitions);
        if (requiresUpdate) {
            log.debug("Update task {} inputPartitions: current {}, new {}", task, task.inputPartitions(), topicPartitions);
            for (final TopicPartition inputPartition : task.inputPartitions()) {
                activeTasksPerPartition.remove(inputPartition);
            }
            for (final TopicPartition topicPartition : topicPartitions) {
                activeTasksPerPartition.put(topicPartition, task);
            }
        }

        return requiresUpdate;
    }

    private void removePartitionsForActiveTask(final TaskId taskId) {
        final Set<TopicPartition> toBeRemoved = activeTasksPerPartition.entrySet().stream()
            .filter(e -> e.getValue().id().equals(taskId))
            .map(Map.Entry::getKey)
            .collect(Collectors.toSet());
        toBeRemoved.forEach(activeTasksPerPartition::remove);
    }

    @Override
    public synchronized void clear() {
        pendingTasksToInit.clear();
        pendingTasksToClose.clear();
        pendingActiveTasksToCreate.clear();
        pendingStandbyTasksToCreate.clear();
        activeTasksPerId.clear();
        standbyTasksPerId.clear();
        activeTasksPerPartition.clear();
        failedTaskIds.clear();
    }

    @Override
    public StreamTask activeInitializedTasksForInputPartition(final TopicPartition partition) {
        return activeTasksPerPartition.get(partition);
    }

    private synchronized Task getTask(final TaskId taskId) {
        if (activeTasksPerId.containsKey(taskId)) {
            return activeTasksPerId.get(taskId);
        }
        if (standbyTasksPerId.containsKey(taskId)) {
            return standbyTasksPerId.get(taskId);
        }
        return null;
    }

    @Override
    public Task initializedTask(final TaskId taskId) {
        final Task task = getTask(taskId);

        if (task != null)
            return task;
        else
            throw new IllegalStateException("Task unknown: " + taskId);
    }

    @Override
    public Collection<Task> initializedTasks(final Collection<TaskId> taskIds) {
        final Set<Task> tasks = new HashSet<>();
        for (final TaskId taskId : taskIds) {
            tasks.add(initializedTask(taskId));
        }
        return tasks;
    }

    @Override
    public synchronized Collection<TaskId> activeInitializedTaskIds() {
        return Collections.unmodifiableCollection(activeTasksPerId.keySet());
    }

    @Override
    public synchronized Collection<StreamTask> activeInitializedTasks() {
        return Collections.unmodifiableCollection(activeTasksPerId.values());
    }

    @Override
    public synchronized Collection<StandbyTask> standbyInitializedTasks() {
        return Collections.unmodifiableCollection(standbyTasksPerId.values());
    }

    /**
     * All tasks returned by any of the getters are read-only and should NOT be modified;
     * and the returned task could be modified by other threads concurrently
     */
    @Override
    public synchronized Set<Task> allInitializedTasks() {
        return union(HashSet::new, new HashSet<>(activeTasksPerId.values()), new HashSet<>(standbyTasksPerId.values()));
    }

    @Override
    public synchronized Set<Task> allNonFailedInitializedTasks() {
        final Set<Task> nonFailedActiveTasks = activeTasksPerId.values().stream()
            .filter(task -> !failedTaskIds.contains(task.id()))
            .collect(Collectors.toSet());
        final Set<Task> nonFailedStandbyTasks = standbyTasksPerId.values().stream()
            .filter(task -> !failedTaskIds.contains(task.id()))
            .collect(Collectors.toSet());
        return union(HashSet::new, nonFailedActiveTasks, nonFailedStandbyTasks);
    }

    @Override
    public synchronized Set<TaskId> allInitializedTaskIds() {
        return union(HashSet::new, activeTasksPerId.keySet(), standbyTasksPerId.keySet());
    }

    @Override
    public synchronized Map<TaskId, Task> allInitializedTasksPerId() {
        final Map<TaskId, Task> ret = new HashMap<>();
        ret.putAll(activeTasksPerId);
        ret.putAll(standbyTasksPerId);
        return ret;
    }

    @Override
    public boolean containsInitialized(final TaskId taskId) {
        return getTask(taskId) != null;
    }

    Map<TaskId, Set<TopicPartition>> pendingActiveTasksToCreate() {
        return pendingActiveTasksToCreate;
    }

    Map<TaskId, Set<TopicPartition>> pendingStandbyTasksToCreate() {
        return pendingStandbyTasksToCreate;
    }
}
