rohangarg commented on code in PR #13476: URL: https://github.com/apache/druid/pull/13476#discussion_r1096965997
########## indexing-service/src/main/java/org/apache/druid/indexing/common/TaskStorageDirTracker.java: ########## @@ -0,0 +1,104 @@ +/* + * 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.druid.indexing.common; + +import com.google.common.annotations.VisibleForTesting; +import org.apache.druid.indexing.common.config.TaskConfig; +import org.apache.druid.java.util.common.ISE; + +import javax.inject.Inject; +import java.io.File; +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +public class TaskStorageDirTracker +{ + private int taskDirIndex = 0; + + private final List<File> baseTaskDirs = new ArrayList<>(); + + private final Map<String, File> taskToTempDirMap = new HashMap<>(); + + @Inject + public TaskStorageDirTracker(final TaskConfig taskConfig) + { + this(taskConfig.getBaseTaskDirPaths()); + } + + @VisibleForTesting + public TaskStorageDirTracker(final List<String> baseTaskDirPaths) + { + for (String baseTaskDirPath : baseTaskDirPaths) { + baseTaskDirs.add(new File(baseTaskDirPath)); + } + } + + public File getBaseTaskDir(String taskId) + { + return getOrSelectTaskDir(taskId); + } + + public File getTaskDir(String taskId) + { + return new File(getBaseTaskDir(taskId), taskId); + } + + public File getTaskWorkDir(String taskId) + { + return new File(getTaskDir(taskId), "work"); + } + + public File getTaskTempDir(String taskId) + { + return new File(getTaskDir(taskId), "temp"); + } + + public List<File> getBaseTaskDirs() + { + return baseTaskDirs; + } + + public synchronized File getOrSelectTaskDir(final String taskId) + { + if (!taskToTempDirMap.containsKey(taskId)) { + addTask(taskId, baseTaskDirs.get(taskDirIndex)); + taskDirIndex = (taskDirIndex + 1) % baseTaskDirs.size(); + } + + return taskToTempDirMap.get(taskId); + } + + public synchronized void addTask(final String taskId, final File taskDir) Review Comment: oh I see another usage in restore - can keep this as is ########## indexing-service/src/test/java/org/apache/druid/indexing/overlord/ForkingTaskRunnerTest.java: ########## @@ -563,4 +576,57 @@ int waitForTaskProcessToComplete(Task task, ProcessHolder processHolder, File lo Assert.assertEquals(1L, (long) forkingTaskRunner.getWorkerFailedTaskCount()); Assert.assertEquals(0L, (long) forkingTaskRunner.getWorkerSuccessfulTaskCount()); } + + @Test + public void testCannotRestoreTasks() throws Exception Review Comment: doubt : does this test verify that a cleanly finished task is removed from the restore list? ########## indexing-service/src/main/java/org/apache/druid/indexing/common/task/HadoopIndexTask.java: ########## @@ -279,14 +281,15 @@ private boolean hadoopJobIdFileExists() private File getHadoopJobIdFile() { - return new File(taskConfig.getTaskDir(getId()), HADOOP_JOB_ID_FILENAME); + return new File(dirTracker.getTaskDir(getId()), HADOOP_JOB_ID_FILENAME); } @Override public TaskStatus runTask(TaskToolbox toolbox) { try { taskConfig = toolbox.getConfig(); + dirTracker = toolbox.getDirTracker(); Review Comment: seems like extra change ########## indexing-service/src/main/java/org/apache/druid/indexing/overlord/BaseRestorableTaskRunner.java: ########## @@ -173,24 +188,55 @@ public Collection<TaskRunnerWorkItem> getKnownTasks() @GuardedBy("tasks") protected void saveRunningTasks() { - final File restoreFile = getRestoreFile(); - final List<String> theTasks = new ArrayList<>(); + final Map<File, List<String>> theTasks = new HashMap<>(); for (TaskRunnerWorkItem forkingTaskRunnerWorkItem : tasks.values()) { - theTasks.add(forkingTaskRunnerWorkItem.getTaskId()); + final String taskId = forkingTaskRunnerWorkItem.getTaskId(); + final File restoreFile = getRestoreFile(taskId); + theTasks.computeIfAbsent(restoreFile, k -> new ArrayList<>()) + .add(taskId); } - try { - Files.createParentDirs(restoreFile); - jsonMapper.writeValue(restoreFile, new TaskRestoreInfo(theTasks)); + for (Map.Entry<File, List<String>> entry : theTasks.entrySet()) { + final File restoreFile = entry.getKey(); + final TaskRestoreInfo taskRestoreInfo = new TaskRestoreInfo(entry.getValue()); + try { + Files.createParentDirs(restoreFile); + jsonMapper.writeValue(restoreFile, taskRestoreInfo); + LOG.debug("Save restore file at [%s] for tasks [%s]", + restoreFile.getAbsoluteFile(), Arrays.toString(theTasks.get(restoreFile).toArray())); + } + catch (Exception e) { + LOG.warn(e, "Failed to save tasks to restore file[%s]. Skipping this save.", restoreFile); + } } - catch (Exception e) { - LOG.warn(e, "Failed to save tasks to restore file[%s]. Skipping this save.", restoreFile); + } + + @Override + public void stop() + { + if (!taskConfig.isRestoreTasksOnRestart()) { + return; + } + + for (File baseDir : dirTracker.getBaseTaskDirs()) { + File restoreFile = new File(baseDir, TASK_RESTORE_FILENAME); + if (restoreFile.exists()) { + try { + TaskRestoreInfo taskRestoreInfo = jsonMapper.readValue(restoreFile, TaskRestoreInfo.class); + for (final String taskId : taskRestoreInfo.getRunningTasks()) { + LOG.info("Saving task[%s] at path[%s] for restore", taskId, baseDir); Review Comment: the log message can be better I think since we more like logging before exit instead of saving task infos -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected] --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
