Unverified Commit dce403d0 authored by Yichao Yang's avatar Yichao Yang Committed by GitHub
Browse files

[Improvement-3369][api] Introduce logger service interface for clear code (#3437)

* [Improvement][api] Introduce logger service interface for clear code

* Remove the code smell
parent f7f07608
Loading
Loading
Loading
Loading
+17 −8
Original line number Diff line number Diff line
@@ -17,25 +17,34 @@
package org.apache.dolphinscheduler.api.controller;


import static org.apache.dolphinscheduler.api.enums.Status.DOWNLOAD_TASK_INSTANCE_LOG_FILE_ERROR;
import static org.apache.dolphinscheduler.api.enums.Status.QUERY_TASK_INSTANCE_LOG_ERROR;

import org.apache.dolphinscheduler.api.exceptions.ApiException;
import org.apache.dolphinscheduler.api.service.LoggerService;
import org.apache.dolphinscheduler.api.utils.Result;
import org.apache.dolphinscheduler.common.Constants;
import org.apache.dolphinscheduler.dao.entity.User;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.bind.annotation.RestController;

import static org.apache.dolphinscheduler.api.enums.Status.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import springfox.documentation.annotations.ApiIgnore;


/**
@@ -70,7 +79,7 @@ public class LoggerController extends BaseController {
    @GetMapping(value = "/detail")
    @ResponseStatus(HttpStatus.OK)
    @ApiException(QUERY_TASK_INSTANCE_LOG_ERROR)
    public Result queryLog(@ApiIgnore @RequestAttribute(value = Constants.SESSION_USER) User loginUser,
    public Result<String> queryLog(@ApiIgnore @RequestAttribute(value = Constants.SESSION_USER) User loginUser,
                           @RequestParam(value = "taskInstanceId") int taskInstanceId,
                           @RequestParam(value = "skipLineNum") int skipNum,
                           @RequestParam(value = "limit") int limit) {
+3 −2
Original line number Diff line number Diff line
@@ -18,17 +18,18 @@ package org.apache.dolphinscheduler.api.exceptions;

import org.apache.dolphinscheduler.api.enums.Status;
import org.apache.dolphinscheduler.api.utils.Result;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestControllerAdvice;
import org.springframework.web.method.HandlerMethod;

/**
 * Exception Handler
 */
@ControllerAdvice
@RestControllerAdvice
@ResponseBody
public class ApiExceptionHandler {

+20 −107
Original line number Diff line number Diff line
@@ -16,45 +16,12 @@
 */
package org.apache.dolphinscheduler.api.service;

import java.nio.charset.StandardCharsets;
import javax.annotation.PreDestroy;
import org.apache.commons.lang.ArrayUtils;
import org.apache.dolphinscheduler.api.enums.Status;
import org.apache.dolphinscheduler.api.utils.Result;
import org.apache.dolphinscheduler.common.Constants;
import org.apache.dolphinscheduler.common.utils.StringUtils;
import org.apache.dolphinscheduler.dao.entity.TaskInstance;
import org.apache.dolphinscheduler.remote.utils.Host;
import org.apache.dolphinscheduler.service.log.LogClientService;
import org.apache.dolphinscheduler.service.process.ProcessService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * log service
 */
@Service
public class LoggerService {

  private static final Logger logger = LoggerFactory.getLogger(LoggerService.class);

  private static final String LOG_HEAD_FORMAT = "[LOG-PATH]: %s, [HOST]:  %s%s";

  @Autowired
  private ProcessService processService;

  private final LogClientService logClient;

  public LoggerService() {
    logClient = new LogClientService();
  }

  @PreDestroy
  public void close() {
    logClient.close();
  }
public interface LoggerService {

    /**
     * view log
@@ -64,36 +31,7 @@ public class LoggerService {
     * @param limit limit
     * @return log string data
     */
  public Result queryLog(int taskInstId, int skipLineNum, int limit) {

    TaskInstance taskInstance = processService.findTaskInstanceById(taskInstId);

    if (taskInstance == null || StringUtils.isBlank(taskInstance.getHost())) {
      return Result.error(Status.TASK_INSTANCE_NOT_FOUND);
    }

    String host = getHost(taskInstance.getHost());

    Result result = new Result(Status.SUCCESS.getCode(), Status.SUCCESS.getMsg());

    logger.info("log host : {} , logPath : {} , logServer port : {}", host, taskInstance.getLogPath(),
            Constants.RPC_PORT);

    StringBuilder log = new StringBuilder();
    if (skipLineNum == 0) {
      String head = String.format(LOG_HEAD_FORMAT,
          taskInstance.getLogPath(),
          host,
          Constants.SYSTEM_LINE_SEPARATOR);
      log.append(head);
    }

    log.append(logClient
        .rollViewLog(host, Constants.RPC_PORT, taskInstance.getLogPath(), skipLineNum, limit));

    result.setData(log);
    return result;
  }
    Result<String> queryLog(int taskInstId, int skipLineNum, int limit);


    /**
@@ -102,31 +40,6 @@ public class LoggerService {
     * @param taskInstId task instance id
     * @return log byte array
     */
  public byte[] getLogBytes(int taskInstId) {
    TaskInstance taskInstance = processService.findTaskInstanceById(taskInstId);
    if (taskInstance == null || StringUtils.isBlank(taskInstance.getHost())) {
      throw new RuntimeException("task instance is null or host is null");
    }
    String host = getHost(taskInstance.getHost());
    byte[] head = String.format(LOG_HEAD_FORMAT,
        taskInstance.getLogPath(),
        host,
        Constants.SYSTEM_LINE_SEPARATOR).getBytes(StandardCharsets.UTF_8);
    return ArrayUtils.addAll(head,
        logClient.getLogBytes(host, Constants.RPC_PORT, taskInstance.getLogPath()));
  }
    byte[] getLogBytes(int taskInstId);


  /**
   * get host
   *
   * @param address address
   * @return old version return true ,otherwise return false
   */
  private String getHost(String address) {
    if (Host.isOldVersion(address)) {
      return address;
    }
    return Host.of(address).getIp();
  }
}
+146 −0
Original line number Diff line number Diff line
/*
 * 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.dolphinscheduler.api.service.impl;

import org.apache.dolphinscheduler.api.enums.Status;
import org.apache.dolphinscheduler.api.exceptions.ServiceException;
import org.apache.dolphinscheduler.api.service.LoggerService;
import org.apache.dolphinscheduler.api.utils.Result;
import org.apache.dolphinscheduler.common.Constants;
import org.apache.dolphinscheduler.common.utils.StringUtils;
import org.apache.dolphinscheduler.dao.entity.TaskInstance;
import org.apache.dolphinscheduler.remote.utils.Host;
import org.apache.dolphinscheduler.service.log.LogClientService;
import org.apache.dolphinscheduler.service.process.ProcessService;

import org.apache.commons.lang.ArrayUtils;

import java.nio.charset.StandardCharsets;
import java.util.Objects;

import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * log service
 */
@Service
public class LoggerServiceImpl implements LoggerService {

    private static final Logger logger = LoggerFactory.getLogger(LoggerServiceImpl.class);

    private static final String LOG_HEAD_FORMAT = "[LOG-PATH]: %s, [HOST]:  %s%s";

    @Autowired
    private ProcessService processService;

    private LogClientService logClient;

    @PostConstruct
    public void init() {
        if (Objects.isNull(this.logClient)) {
            this.logClient = new LogClientService();
        }
    }

    @PreDestroy
    public void close() {
        if (Objects.nonNull(this.logClient) && this.logClient.isRunning()) {
            logClient.close();
        }
    }

    /**
     * view log
     *
     * @param taskInstId task instance id
     * @param skipLineNum skip line number
     * @param limit limit
     * @return log string data
     */
    @SuppressWarnings("unchecked")
    public Result<String> queryLog(int taskInstId, int skipLineNum, int limit) {

        TaskInstance taskInstance = processService.findTaskInstanceById(taskInstId);

        if (taskInstance == null || StringUtils.isBlank(taskInstance.getHost())) {
            return Result.error(Status.TASK_INSTANCE_NOT_FOUND);
        }

        String host = getHost(taskInstance.getHost());

        Result<String> result = new Result<>(Status.SUCCESS.getCode(), Status.SUCCESS.getMsg());

        logger.info("log host : {} , logPath : {} , logServer port : {}", host, taskInstance.getLogPath(),
                Constants.RPC_PORT);

        StringBuilder log = new StringBuilder();
        if (skipLineNum == 0) {
            String head = String.format(LOG_HEAD_FORMAT,
                    taskInstance.getLogPath(),
                    host,
                    Constants.SYSTEM_LINE_SEPARATOR);
            log.append(head);
        }

        log.append(logClient
                .rollViewLog(host, Constants.RPC_PORT, taskInstance.getLogPath(), skipLineNum, limit));

        result.setData(log.toString());
        return result;
    }


    /**
     * get log size
     *
     * @param taskInstId task instance id
     * @return log byte array
     */
    public byte[] getLogBytes(int taskInstId) {
        TaskInstance taskInstance = processService.findTaskInstanceById(taskInstId);
        if (taskInstance == null || StringUtils.isBlank(taskInstance.getHost())) {
            throw new ServiceException("task instance is null or host is null");
        }
        String host = getHost(taskInstance.getHost());
        byte[] head = String.format(LOG_HEAD_FORMAT,
                taskInstance.getLogPath(),
                host,
                Constants.SYSTEM_LINE_SEPARATOR).getBytes(StandardCharsets.UTF_8);
        return ArrayUtils.addAll(head,
                logClient.getLogBytes(host, Constants.RPC_PORT, taskInstance.getLogPath()));
    }


    /**
     * get host
     *
     * @param address address
     * @return old version return true ,otherwise return false
     */
    private String getHost(String address) {
        if (Boolean.TRUE.equals(Host.isOldVersion(address))) {
            return address;
        }
        return Host.of(address).getIp();
    }
}
+29 −15
Original line number Diff line number Diff line
@@ -17,10 +17,14 @@
package org.apache.dolphinscheduler.api.service;

import org.apache.dolphinscheduler.api.enums.Status;
import org.apache.dolphinscheduler.api.service.impl.LoggerServiceImpl;
import org.apache.dolphinscheduler.api.utils.Result;
import org.apache.dolphinscheduler.dao.entity.TaskInstance;
import org.apache.dolphinscheduler.service.process.ProcessService;

import org.junit.After;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.InjectMocks;
@@ -32,16 +36,21 @@ import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

@RunWith(MockitoJUnitRunner.class)
@PrepareForTest({LoggerService.class})
@PrepareForTest({LoggerServiceImpl.class})
public class LoggerServiceTest {

    private static final Logger logger = LoggerFactory.getLogger(LoggerServiceTest.class);

    @InjectMocks
    private LoggerService loggerService;
    private LoggerServiceImpl loggerService;
    @Mock
    private ProcessService processService;

    @Before
    public void init() {
        this.loggerService.init();
    }


    @Test
    public void testQueryDataSourceList() {
@@ -100,4 +109,9 @@ public class LoggerServiceTest {

    }

    @After
    public void close() {
        this.loggerService.close();
    }

}
 No newline at end of file
Loading