jihoonson commented on a change in pull request #8088: Add intermediary data 
server for shuffle
URL: https://github.com/apache/incubator-druid/pull/8088#discussion_r304107067
 
 

 ##########
 File path: 
indexing-service/src/main/java/org/apache/druid/indexing/worker/IntermediaryDataManager.java
 ##########
 @@ -0,0 +1,270 @@
+/*
+ * 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.worker;
+
+import com.google.common.collect.Iterators;
+import com.google.common.io.Files;
+import com.google.inject.Inject;
+import org.apache.commons.io.FileUtils;
+import org.apache.druid.client.indexing.IndexingServiceClient;
+import org.apache.druid.client.indexing.TaskStatus;
+import org.apache.druid.guice.ManageLifecycle;
+import org.apache.druid.indexing.common.config.TaskConfig;
+import org.apache.druid.indexing.worker.config.WorkerConfig;
+import org.apache.druid.java.util.common.DateTimes;
+import org.apache.druid.java.util.common.IOE;
+import org.apache.druid.java.util.common.concurrent.Execs;
+import org.apache.druid.java.util.common.lifecycle.LifecycleStart;
+import org.apache.druid.java.util.common.lifecycle.LifecycleStop;
+import org.apache.druid.java.util.common.logger.Logger;
+import org.apache.druid.segment.loading.StorageLocation;
+import org.apache.druid.timeline.DataSegment;
+import org.joda.time.DateTime;
+import org.joda.time.Interval;
+import org.joda.time.Period;
+
+import java.io.File;
+import java.io.IOException;
+import java.util.Arrays;
+import java.util.Collections;
+import java.util.HashMap;
+import java.util.HashSet;
+import java.util.Iterator;
+import java.util.List;
+import java.util.Map;
+import java.util.Map.Entry;
+import java.util.Set;
+import java.util.concurrent.ConcurrentHashMap;
+import java.util.concurrent.ScheduledExecutorService;
+import java.util.concurrent.TimeUnit;
+import java.util.stream.Collectors;
+
+/**
+ * This class manages intermediary segments for data shuffle between native 
parallel index tasks.
+ * In native parallel indexing, phase 1 tasks store segment files in local 
storage of middleManagers
+ * and phase 2 tasks read those files via HTTP.
+ *
+ * The directory where segment files are placed is structured as
+ * {@link 
StorageLocation#path}/supervisorTaskId/startTimeOfSegment/endTimeOfSegment/partitionIdOfSegment.
+ *
+ * This class provides interfaces to store, find, and remove segment files.
+ * It also has a self-cleanup mechanism to clean up stale segment files. It 
periodically checks the last access time
+ * per supervisorTask and removes its all segment files if the supervisorTask 
is not running anymore.
+ */
+@ManageLifecycle
+public class IntermediaryDataManager
+{
+  private static final Logger log = new Logger(IntermediaryDataManager.class);
+
+  private final long intermediaryPartitionDiscoveryPeriodSec;
+  private final long intermediaryPartitionCleanupPeriodSec;
+  private final Period intermediaryPartitionTimeout;
+  private final List<StorageLocation> intermediarySegmentsLocations;
+  private final IndexingServiceClient indexingServiceClient;
+
+  // supervisorTaskId -> time to check supervisorTask status
+  // This time is initialized when a new supervisorTask is found and updated 
whenever a partition is accessed for
+  // the supervisor.
+  private final ConcurrentHashMap<String, DateTime> supervisorTaskCheckTimes = 
new ConcurrentHashMap<>();
+
+  // supervisorTaskId -> cyclic iterator of storage locations
+  private final Map<String, Iterator<StorageLocation>> locationIterators = new 
HashMap<>();
+
+  // The overlord is supposed to send a cleanup request as soon as the 
supervisorTask is finished in parallel indexing,
+  // but middleManager or indexer could miss the request. This executor is to 
automatically clean up unused intermediary
+  // partitions.
+  private ScheduledExecutorService supervisorTaskChecker;
+
+  @Inject
+  public IntermediaryDataManager(
+      WorkerConfig workerConfig,
+      TaskConfig taskConfig,
+      IndexingServiceClient indexingServiceClient
+  )
+  {
+    this.intermediaryPartitionDiscoveryPeriodSec = 
workerConfig.getIntermediaryPartitionDiscoveryPeriodSec();
+    this.intermediaryPartitionCleanupPeriodSec = 
workerConfig.getIntermediaryPartitionCleanupPeriodSec();
+    this.intermediaryPartitionTimeout = 
workerConfig.getIntermediaryPartitionTimeout();
+    this.intermediarySegmentsLocations = taskConfig
+        .getIntermediarySegmentsLocations()
+        .stream()
+        .map(config -> new StorageLocation(config.getPath(), 
config.getMaxSize(), config.getFreeSpacePercent()))
+        .collect(Collectors.toList());
+    this.indexingServiceClient = indexingServiceClient;
+  }
+
+  @LifecycleStart
+  public void start()
+  {
+    supervisorTaskChecker = 
Execs.scheduledSingleThreaded("intermediary-data-manager-%d");
+    // Discover partitions for new supervisorTasks
+    supervisorTaskChecker.scheduleAtFixedRate(
+        () -> {
+          for (StorageLocation location : intermediarySegmentsLocations) {
+            final File[] dirsPerSupervisorTask = 
location.getPath().listFiles();
+            if (dirsPerSupervisorTask != null) {
+              for (File supervisorTaskDir : dirsPerSupervisorTask) {
+                final String supervisorTaskId = supervisorTaskDir.getName();
+                supervisorTaskCheckTimes.computeIfAbsent(
+                    supervisorTaskId,
+                    k -> DateTimes.nowUtc().plus(intermediaryPartitionTimeout)
+                );
+              }
+            }
+          }
+        },
+        intermediaryPartitionDiscoveryPeriodSec,
+        intermediaryPartitionDiscoveryPeriodSec,
+        TimeUnit.SECONDS
+    );
+    // Check supervisorTask status if its partitions have not been accessed in 
timeout.
+    // Delete partitions if the supervisorTask is already finished.
+    // Note that the overlord sends a cleanup request when a supervisorTask is 
finished. The below check is to trigger
+    // the self-cleanup for when middleManager misses the cleanup request from 
the overlord.
+    supervisorTaskChecker.scheduleAtFixedRate(
+        () -> {
+          final DateTime now = DateTimes.nowUtc();
+          final Set<String> expiredSupervisorTasks = new HashSet<>();
+          for (Entry<String, DateTime> entry : 
supervisorTaskCheckTimes.entrySet()) {
+            final String supervisorTaskId = entry.getKey();
+            final DateTime checkTime = entry.getValue();
+            if (checkTime.isAfter(now)) {
+              expiredSupervisorTasks.add(supervisorTaskId);
+            }
+          }
+
+          final Map<String, TaskStatus> taskStatuses = 
indexingServiceClient.getTaskStatuses(expiredSupervisorTasks);
+          RuntimeException exception = null;
+          for (Entry<String, TaskStatus> entry : taskStatuses.entrySet()) {
+            final String supervisorTaskId = entry.getKey();
+            final TaskStatus status = entry.getValue();
+            if (status.getStatusCode().isComplete()) {
+              try {
+                deletePartitions(supervisorTaskId);
+              }
+              catch (IOException e) {
+                if (exception == null) {
+                  exception = new RuntimeException(e);
+                } else {
+                  exception.addSuppressed(e);
+                }
+              }
+            }
+          }
+
+          if (exception != null) {
+            throw exception;
 
 Review comment:
   Forgot to fix this before commit. Thanks.

----------------------------------------------------------------
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.
 
For queries about this service, please contact Infrastructure at:
us...@infra.apache.org


With regards,
Apache Git Services

---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscr...@druid.apache.org
For additional commands, e-mail: commits-h...@druid.apache.org

Reply via email to