xingtanzjr commented on a change in pull request #5294:
URL: https://github.com/apache/iotdb/pull/5294#discussion_r830838159



##########
File path: 
server/src/main/java/org/apache/iotdb/db/mpp/schedule/FragmentInstanceManager.java
##########
@@ -0,0 +1,286 @@
+/*
+ * 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.iotdb.db.mpp.schedule;
+
+import org.apache.iotdb.commons.exception.StartupException;
+import org.apache.iotdb.commons.service.IService;
+import org.apache.iotdb.commons.service.ServiceType;
+import org.apache.iotdb.db.mpp.execution.ExecFragmentInstance;
+import org.apache.iotdb.db.mpp.schedule.queue.IndexedBlockingQueue;
+import org.apache.iotdb.db.mpp.schedule.queue.L1PriorityQueue;
+import org.apache.iotdb.db.mpp.schedule.queue.L2PriorityQueue;
+import org.apache.iotdb.db.mpp.schedule.task.FragmentInstanceTask;
+import org.apache.iotdb.db.mpp.schedule.task.FragmentInstanceTaskStatus;
+
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import java.io.IOException;
+import java.util.ArrayList;
+import java.util.Collections;
+import java.util.HashSet;
+import java.util.List;
+import java.util.Map;
+import java.util.Set;
+import java.util.concurrent.ConcurrentHashMap;
+import java.util.stream.Collectors;
+
+/** the manager of fragment instances scheduling */
+public class FragmentInstanceManager implements IFragmentInstanceManager, 
IService {
+
+  private static final Logger logger = 
LoggerFactory.getLogger(FragmentInstanceManager.class);
+
+  public static IFragmentInstanceManager getInstance() {
+    return InstanceHolder.instance;
+  }
+
+  private final IndexedBlockingQueue<FragmentInstanceTask> readyQueue;
+  private final IndexedBlockingQueue<FragmentInstanceTask> timeoutQueue;
+  private final Set<FragmentInstanceTask> blockedTasks;
+  private final Map<String, Set<FragmentInstanceTask>> queryMap;
+  private final ITaskScheduler scheduler;
+
+  private static final int MAX_CAPACITY = 1000; // TODO: load from config files
+  private static final int WORKER_THREAD_NUM = 4; // TODO: load from config 
files
+  private static final int QUERY_TIMEOUT_MS = 10000; // TODO: load from config 
files or requests
+  private final ThreadGroup workerGroups;
+  private final List<AbstractExecutor> threads;
+
+  public FragmentInstanceManager() {
+    this.readyQueue =
+        new L2PriorityQueue<>(
+            MAX_CAPACITY,
+            new FragmentInstanceTask.SchedulePriorityComparator(),
+            new FragmentInstanceTask());
+    this.timeoutQueue =
+        new L1PriorityQueue<>(
+            MAX_CAPACITY,
+            new FragmentInstanceTask.SchedulePriorityComparator(),
+            new FragmentInstanceTask());
+    this.queryMap = new ConcurrentHashMap<>();
+    this.blockedTasks = Collections.synchronizedSet(new HashSet<>());
+    this.scheduler = new Scheduler();
+    this.workerGroups = new ThreadGroup("ScheduleThreads");
+    this.threads = new ArrayList<>();
+  }
+
+  @Override
+  public void start() throws StartupException {
+    for (int i = 0; i < WORKER_THREAD_NUM; i++) {
+      AbstractExecutor t =
+          new FragmentInstanceTaskExecutor(
+              "Worker-Thread-" + i, workerGroups, readyQueue, scheduler);
+      threads.add(t);
+      t.start();
+    }
+    AbstractExecutor t =
+        new FragmentInstanceTimeoutSentinel(
+            "Sentinel-Thread", workerGroups, timeoutQueue, scheduler);
+    threads.add(t);
+    t.start();
+  }
+
+  @Override
+  public void stop() {
+    this.threads.forEach(
+        t -> {
+          try {
+            t.close();
+          } catch (IOException e) {
+            // Only a field is set, there's no chance to throw an IOException
+          }
+        });
+  }
+
+  @Override
+  public ServiceType getID() {
+    return ServiceType.FRAGMENT_INSTANCE_MANAGER_SERVICE;
+  }
+
+  @Override
+  public void submitFragmentInstances(String queryId, 
List<ExecFragmentInstance> instances) {
+    Set<FragmentInstanceTask> tasks =
+        instances.stream()
+            .map(
+                v ->
+                    new FragmentInstanceTask(v, QUERY_TIMEOUT_MS, 
FragmentInstanceTaskStatus.READY))
+            .collect(Collectors.toSet());
+    queryMap.put(queryId, Collections.synchronizedSet(tasks));
+    for (FragmentInstanceTask task : tasks) {
+      task.lock();
+      try {
+        timeoutQueue.push(task);
+        readyQueue.push(task);
+      } finally {
+        task.unlock();
+      }
+    }
+  }
+
+  @Override
+  public void abortQuery(String queryId) {
+    Set<FragmentInstanceTask> queryRelatedTasks = queryMap.remove(queryId);
+    if (queryRelatedTasks != null) {

Review comment:
       If the tasks for a query is being added to the `readyQueue` on line 129 
and a abortQuery command for this query is triggered at the same time, the 
abort command may only clear part of the tasks.
   
   I am not sure whether this scenario will lead to some unexpected results or 
not...




-- 
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]


Reply via email to