Alibaba task scheduler x supports log service

Function introduction

In the current situation that microservices and containerization are becoming more and more popular, there are too many machines and they do not have permission to log in to the container, which has become a pain point for business students to quickly locate business failures. Visual white screen log tool has increasingly become the rigid demand of enterprises.

Alibaba distributed task scheduling system schedulerx2 The log service of 0 allows the business party to view the business log of each task scheduling (including distributed tasks) on the console without modifying a line of code. It only needs to add a log4j/logback configuration to facilitate troubleshooting.

Use restrictions

Open Professional Edition

The professional version needs to be opened. In the application management, under the advanced configuration, change to the professional version and start the log service, as shown in the following figure

Log save time

The current log can be kept for up to 2 weeks, and the logs that exceed 2 weeks will be cleaned up

Access configuration

Upgrade schedulerx client version

Upgrade the schedulerx client version to a version above 1.4.1.1. Take springboot starter as an example

<dependency>
    <groupId>com.aliyun.schedulerx</groupId>
    <artifactId>schedulerx2-spring-boot-starter</artifactId>
    <version>1.4.1.1</version>
</dependency>

Configure Log Appender collection log service

Log4j2 Appender

log4j2.xml adds an appender named SchedulerxLog4j2Appender

<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="off">
    <Appenders>
        <Console name="Console" target="SYSTEM_OUT">
            <PatternLayout
                pattern="%d{yyyy-MM-dd HH:mm:ss.SSS} [%t] %-5level %logger{36} - %m%n" />
        </Console>
        <SchedulerxLog4j2Appender name="schedulerxLog"
            timeFormat="yyyy-MM-dd'T'HH:mmZ"
            timeZone="UTC"
            ignoreExceptions="true">
        <PatternLayout pattern="%d %-5level [%thread] %logger{0}: %msg"/>
    </SchedulerxLog4j2Appender>
    </Appenders>
    <Loggers>
        <Root level="info">
            <AppenderRef ref="Console" />
        </Root>
        <Logger name="schedulerx" level="info" additivity="false">
            <AppenderRef ref="schedulerxLog" />
        </Logger>
    </Loggers>
</Configuration>

The business code uses the native log4j2 to print the log

package com.hxm.test.processor;

import com.alibaba.schedulerx.worker.domain.JobContext;
import com.alibaba.schedulerx.worker.processor.JavaProcessor;
import com.alibaba.schedulerx.worker.processor.ProcessResult;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.stereotype.Component;


@Component
public class HelloWorldJob3 extends JavaProcessor {
    private static final Logger LOGGER = LogManager.getLogger("schedulerx");
    
    @Override
    public ProcessResult process(JobContext context) throws Exception {
        LOGGER.info("hello HelloWorldJob3");
        return new ProcessResult(true);
    }
    
}

Log4j Appender

log4j. Add an Appender to properties

log4j.appender.schedulerxLog=com.alibaba.schedulerx.worker.log.appender.SchedulerxLog4jAppender

Logback Appender

logback.xml add an Appender

<appender name="schedulerxLog" class="com.alibaba.schedulerx.worker.log.appender.SchedulerxLogbackAppender"> 
    <timeFormat>yyyy-MM-dd'T'HH:mmZ</timeFormat>
    <timeZone>UTC</timeZone>
</appender>

Log output

An example of log output is as follows:

  • ip: the execution machine that prints the log
  • executionId: the execution id of this task instance. The format is ${jobId}_${jobInstanceId}_${taskId}
  • Level: log level
  • Log: log information
  • throwable: if an exception is thrown, this field will be printed

Application scenario and demonstration

Reasons for business failure

SchedulerX2.0's log service can collect task execution logs and exceptions, including service logs. The demo is as follows

1. Create a new task code and service business code, and configure schedulerx to print logs

package com.hxm.test.processor;

import com.alibaba.schedulerx.test.service.TestService;
import com.alibaba.schedulerx.worker.domain.JobContext;
import com.alibaba.schedulerx.worker.processor.JavaProcessor;
import com.alibaba.schedulerx.worker.processor.ProcessResult;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component
public class HelloWorldJob extends JavaProcessor {
    //Collecting logs using schedulerx
    private static final Logger LOGGER = LogManager.getLogger("schedulerx");
    
    @Autowired
    private TestService testService;
    
    @Override
    public ProcessResult process(JobContext context) throws Exception {
        String parameters = context.getJobParameters();
        String tokens[] = parameters.split(" ");
        int a = Integer.valueOf(tokens[0]);
        int b = Integer.valueOf(tokens[1]);
        int c = testService.doDivision(a, b);
        LOGGER.info("testService.doDivision finished, a={}, b={}, c={}", a, b, c);
        if (c < 0) {
            return new ProcessResult(false, "result=" + c);
        }
        return new ProcessResult(true);
    }
    
}
package com.hxm.test.service;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.stereotype.Service;

@Service("testService")
public class TestServiceImpl implements TestService {
    //Collecting logs using schedulerx
    private static final Logger LOGGER = LogManager.getLogger("schedulerx");

    @Override
    public int doDivision(int a, int b) {
        try {
             LOGGER.info("start to do division c = " + a + "/" + b);
             int c = a/b;
             LOGGER.info("c=" + c);
             return c;
        } catch (Exception e) {
            LOGGER.error("", e);
        }
        return -1;
    }

}

2. The console configuration tasks are as follows. According to the code, 1 divided by 0 will throw exceptions

3. After running once, view the log through the task instance list

As shown in the figure above, we can see that the reason for the failure is that TestServiceImpl threw an exception except 0

Query distributed task failure reason

SchedulerX2. The distributed task of 0 is used to run batches. A batch fails to run batches. You want to query which subtask failed. The demo is as follows

1. The new task code is as follows:

package com.hxm.test.processor;

import java.util.ArrayList;
import java.util.List;

import com.alibaba.schedulerx.worker.domain.JobContext;
import com.alibaba.schedulerx.worker.processor.MapJobProcessor;
import com.alibaba.schedulerx.worker.processor.ProcessResult;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

public class TestMapJobProcessor extends MapJobProcessor {
    private static final Logger LOGGER = LogManager.getLogger("schedulerx");
    
    @Override
    public ProcessResult process(JobContext context) throws Exception {
        String taskName = context.getTaskName();
        String parameter = context.getJobParameters();
        int dispatchNum = Integer.valueOf(parameter);
        if (isRootTask(context)) {
            LOGGER.info("start root task");
            List<String> msgList = new ArrayList<>();
            for (int i = 0; i <= dispatchNum; i++) {
                msgList.add("msg_" + i);
            }
            
            return map(msgList, "Level1Dispatch");
        } else if (taskName.equals("Level1Dispatch")) {
            String task = (String)context.getTask();
            if (task.equals("msg_23")) {
                LOGGER.error("msg={}, failed", task);
                return new ProcessResult(false);
            } else {
                LOGGER.info("msg={}, success", task);
            }
            return new ProcessResult(true);
        }
        return new ProcessResult(false);
    }
}

2. Configuration tasks are as follows:

3. After running once, check the log

4. Search keywords to quickly locate the failed machine and reason

Query history log by keyword

Only the last 60 task instance history records are kept. If you want to check the causes of task failure in history, you can query through the log in the left column. It supports searching by task id and keyword, and query time interval

Original link: Alibaba task scheduler x supports log service - Alibaba cloud developer community 

Tags: Java Spring Boot Distribution Middleware

Posted by julien on Fri, 11 Mar 2022 12:12:46 +0530