EMsnap commented on code in PR #9139:
URL: https://github.com/apache/inlong/pull/9139#discussion_r1374240975


##########
inlong-agent/agent-core/src/main/java/org/apache/inlong/agent/core/task/file/TaskManager.java:
##########
@@ -0,0 +1,449 @@
+/*
+ * 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.inlong.agent.core.task.file;
+
+import org.apache.inlong.agent.common.AbstractDaemon;
+import org.apache.inlong.agent.common.AgentThreadFactory;
+import org.apache.inlong.agent.conf.AgentConfiguration;
+import org.apache.inlong.agent.conf.TaskProfile;
+import org.apache.inlong.agent.constant.AgentConstants;
+import org.apache.inlong.agent.core.task.TaskAction;
+import org.apache.inlong.agent.db.Db;
+import org.apache.inlong.agent.db.RocksDbImp;
+import org.apache.inlong.agent.db.TaskProfileDb;
+import org.apache.inlong.agent.plugin.file.Task;
+import org.apache.inlong.agent.utils.AgentUtils;
+import org.apache.inlong.agent.utils.ThreadUtils;
+import org.apache.inlong.common.enums.TaskStateEnum;
+
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import java.util.List;
+import java.util.Map;
+import java.util.concurrent.BlockingQueue;
+import java.util.concurrent.ConcurrentHashMap;
+import java.util.concurrent.LinkedBlockingQueue;
+import java.util.concurrent.SynchronousQueue;
+import java.util.concurrent.ThreadPoolExecutor;
+import java.util.concurrent.TimeUnit;
+
+import static 
org.apache.inlong.agent.constant.AgentConstants.DEFAULT_JOB_NUMBER_LIMIT;
+import static org.apache.inlong.agent.constant.AgentConstants.JOB_NUMBER_LIMIT;
+import static org.apache.inlong.agent.constant.TaskConstants.TASK_STATE;
+
+/**
+ * handle the task config from manager, including add, delete, update etc.
+ * the task config is store in both db and memory.
+ */
+public class TaskManager extends AbstractDaemon {
+
+    private static final Logger LOGGER = 
LoggerFactory.getLogger(TaskManager.class);
+    public static final int CONFIG_QUEUE_CAPACITY = 1;
+    public static final int CORE_THREAD_SLEEP_TIME = 1000;
+    private static final int ACTION_QUEUE_CAPACITY = 100000;
+    // task basic db
+    private final Db taskBasicDb;
+    // instance basic db
+    private final Db instanceBasicDb;
+    // task in db
+    private final TaskProfileDb taskDb;
+    // task in memory
+    private final ConcurrentHashMap<String, Task> taskMap;
+    // task config from manager.
+    private final BlockingQueue<List<TaskProfile>> configQueue;
+    // task thread pool;
+    private final ThreadPoolExecutor runningPool;
+    // tasks which are not accepted by running pool.
+    private final BlockingQueue<Task> pendingTasks;
+    private final int taskMaxLimit;
+    private final AgentConfiguration agentConf;
+    // instance profile queue.
+    private final BlockingQueue<TaskAction> actionQueue;
+
+    /**
+     * Init task manager.
+     */
+    public TaskManager() {
+        this.agentConf = AgentConfiguration.getAgentConf();
+        this.taskBasicDb = initDb(
+                agentConf.get(AgentConstants.AGENT_ROCKS_DB_PATH, 
AgentConstants.AGENT_LOCAL_DB_PATH_TASK));
+        this.instanceBasicDb = initDb(
+                agentConf.get(AgentConstants.AGENT_ROCKS_DB_PATH, 
AgentConstants.AGENT_LOCAL_DB_PATH_INSTANCE));
+        taskDb = new TaskProfileDb(taskBasicDb);
+        this.runningPool = new ThreadPoolExecutor(
+                0, Integer.MAX_VALUE,
+                60L, TimeUnit.SECONDS,
+                new SynchronousQueue<>(),
+                new AgentThreadFactory("task-manager-running-pool"));
+        taskMap = new ConcurrentHashMap<>();
+        taskMaxLimit = agentConf.getInt(JOB_NUMBER_LIMIT, 
DEFAULT_JOB_NUMBER_LIMIT);
+        pendingTasks = new LinkedBlockingQueue<>(taskMaxLimit);
+        configQueue = new LinkedBlockingQueue<>(CONFIG_QUEUE_CAPACITY);
+        actionQueue = new LinkedBlockingQueue<>(ACTION_QUEUE_CAPACITY);
+    }
+
+    /**
+     * init db by class name
+     *
+     * @return db
+     */
+    public static Db initDb(String childPath) {
+        try {
+            return new RocksDbImp(childPath);
+        } catch (Exception ex) {
+            throw new UnsupportedClassVersionError(ex.getMessage());
+        }
+    }
+
+    public void submitTaskProfiles(List<TaskProfile> taskProfiles) {
+        if (taskProfiles == null) {
+            return;
+        }
+        while (configQueue.size() != 0) {
+            configQueue.poll();
+        }
+        configQueue.add(taskProfiles);
+    }
+
+    public boolean submitAction(TaskAction action) {
+        if (action == null) {
+            return false;
+        }
+        return actionQueue.offer(action);
+    }
+
+    /**
+     * thread for core thread.
+     *
+     * @return runnable profile.
+     */
+    private Runnable coreThread() {
+        return () -> {
+            Thread.currentThread().setName("task-manager-core");
+            while (isRunnable()) {
+                try {
+                    AgentUtils.silenceSleepInMs(CORE_THREAD_SLEEP_TIME);
+                    dealWithConfigQueue(configQueue);
+                    dealWithActionQueue(actionQueue);
+                } catch (Throwable ex) {
+                    LOGGER.error("exception caught", ex);
+                    ThreadUtils.threadThrowableHandler(Thread.currentThread(), 
ex);
+                }
+            }
+        };
+    }
+
+    private void dealWithConfigQueue(BlockingQueue<List<TaskProfile>> queue) {
+        List<TaskProfile> dataConfigs = queue.poll();
+        if (dataConfigs == null) {
+            return;
+        }
+        keepPaceWithManager(dataConfigs);
+        keepPaceWithDb();
+    }
+
+    private void dealWithActionQueue(BlockingQueue<TaskAction> queue) {
+        while (isRunnable()) {
+            try {
+                TaskAction action = queue.poll();
+                if (action == null) {
+                    break;
+                }
+                TaskProfile profile = action.getProfile();
+                switch (action.getActionType()) {
+                    case FINISH:
+                        LOGGER.info("test123 deal finish action, taskId {}", 
profile.getTaskId());

Review Comment:
   what the meaning of test123 



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