Use of Task Manager Task Management

Task Management Class

Because this Task Manager is used in many places in Nacos, we need to first understand what this class is for, so that we can easily read the source code later without any effort.

First come to the conclusion:
Task Manager can be seen as a set of tasks to be executed. Task Manager is used to handle tasks that must be executed successfully in a single-threaded way to ensure that tasks must be successfully processed. If the execution fails, tasks will be re-placed in the set for the next consumption.

AbstractTask

AbstractTask is an abstract class that continues all tasks that need to be performed; this class mainly provides the data and methods needed to perform tasks; for example

   /* The interval between two processing of a task in milliseconds*/
    private long taskInterval;
    /*The last time a task was processed, expressed in milliseconds*/
    private long lastProcessTime;
/* TaskManager To determine whether the Task needs to be processed at present, the subclass can implement its own logic with the Override function
     */
    public boolean shouldProcess() {
        return (System.currentTimeMillis() - this.lastProcessTime >= this.taskInterval);
    }

TaskProcessor Task Processor

TaskProcessor is the task processor interface. It has a method

boolean process(String taskType, AbstractTask task);

It is used to execute the corresponding AbstractTask task class; different task types can implement their own execution task logic;

TaskManager Task Management Class

Task Manager is a task management class.
It contains two attributes, AbstractTask for consumption and TaskProcessor for task execution.

/**Task to be consumed AbstractTask**/
private final ConcurrentHashMap<String, AbstractTask> tasks = new ConcurrentHashMap<String, AbstractTask>();
/**Task Processor for Task AbstractTask**/
 private final ConcurrentHashMap<String, TaskProcessor> taskProcessors =new ConcurrentHashMap<String, TaskProcessor>();

If the corresponding task executor is not found in taskProcessors, then there is a default executor in it that will execute.

 /**Default Executor**/
 private TaskProcessor defaultTaskProcessor;

use case

An important function of the Nacos configuration center module is to dump all data into the disk in the database at initialization time and at intervals; Dump is a task class AbstractTask; as we said above
AbstractTask is an object of information bearing, which mainly provides TaskProcessor with the data needed for execution; let's look at DumpTask;

DumpTask


DumpTask defines its own attributes; look at other attributes such as DumpAllTask, DumpAllBetaTask


These two task classes define only TASK_ID
Since there is a DumpTask task class, there must be a corresponding task processor class DumpProcessor.

DumpProcessor

DumpProcessor is the executor of the DumpTask task; the method in the executor

public boolean process(String taskType, AbstractTask task)

The code is too long to be analyzed here. I'll write a special article about the operation of Dump (Todo... The main operation in it is to save the configuration file to the local disk and cache md5.

The task executors corresponding to DumpAllTask and DumpAllBetaTask tasks are DumpAllProcessor and DumpAllBetaProcessor.

Where the DumpAllTask task triggers execution

Above is the definition of DumpAllTask and the definition of DumpAllTaskProcessor executor; how is it triggered when defined?

DumpService initializes Dump configuration information

This class is a service class specializing in Dump configuration information; the Dump All mentioned above is called here; let's look at its main methods;

    @PostConstruct
    public void init() {
        DumpAllProcessor dumpAllProcessor = new DumpAllProcessor(this);
      /**At the time of the new TaskManager class, a thread dedicated to executing tasks started, but no task Task was added at this time.**/
       dumpAllTaskMgr = new TaskManager( "com.alibaba.nacos.server.DumpAllTaskManager");
dumpAllTaskMgr.setDefaultTaskProcessor(dumpAllProcessor);
      Runnable dumpAll = new Runnable() {
            @Override
            public void run() {
dumpAllTaskMgr.addTask(DumpAllTask.TASK_ID, new DumpAllTask());
            }
        };
            /**Perform DumpAll operations every 10 minutes**/
      TimerTaskService.scheduleWithFixedDelay(dumpAll, initialDelay, DUMP_ALL_INTERVAL_IN_MINUTE,
                TimeUnit.MINUTES);
    }

DumpService calls this init method back at initialization time.
1. First, a new DumpAllProcessor executor is created.
2. new Task Manager Task Manager Task Manager Task Manager Task Manager Task Manager Task Manager Task Manager Task Manager Task Manager Task Manager Task Manager Task Manager Task Manager Task Manager Task Manager Task Manager Task
3. Set the default executor of the task manager to DumpAllProcessor;
4. The task of adding a DumpAllTask to Task Manager is executed every 10 minutes; once added, the process method is executed by the thread processingThread in Task Manager.

Tags: Database

Posted on Mon, 26 Aug 2019 23:27:08 -0700 by Alidad