Talk about dubbo’s StatusChecker

  dubbo

Order

This article mainly studies dubbo’s StatusChecker

Status

dubbo-2.7.2/dubbo-common/src/main/java/org/apache/dubbo/common/status/Status.java

public class Status {

    private final Level level;
    private final String message;
    private final String description;

    public Status(Level level) {
        this(level, null, null);
    }

    public Status(Level level, String message) {
        this(level, message, null);
    }

    public Status(Level level, String message, String description) {
        this.level = level;
        this.message = message;
        this.description = description;
    }

    public Level getLevel() {
        return level;
    }

    public String getMessage() {
        return message;
    }

    public String getDescription() {
        return description;
    }

    /**
     * Level
     */
    public enum Level {
        /**
         * OK
         */
        OK,

        /**
         * WARN
         */
        WARN,

        /**
         * ERROR
         */
        ERROR,

        /**
         * UNKNOWN
         */
        UNKNOWN
    }

}
  • Status defines three attributes, namely level, message and description; ; Among them, level has four levels: OK, WARN, ERROR and UNKNOWN.

StatusChecker

dubbo-2.7.2/dubbo-common/src/main/java/org/apache/dubbo/common/status/StatusChecker.java

@SPI
public interface StatusChecker {

    /**
     * check status
     *
     * @return status
     */
    Status check();

}
  • StatusChecker defines the check interface and returns status.

LoadStatusChecker

dubbo-2.7.2/dubbo-common/src/main/java/org/apache/dubbo/common/status/support/LoadStatusChecker.java

@Activate
public class LoadStatusChecker implements StatusChecker {

    @Override
    public Status check() {
        OperatingSystemMXBean operatingSystemMXBean = ManagementFactory.getOperatingSystemMXBean();
        double load;
        try {
            Method method = OperatingSystemMXBean.class.getMethod("getSystemLoadAverage", new Class<?>[0]);
            load = (Double) method.invoke(operatingSystemMXBean, new Object[0]);
            if (load == -1) {
                com.sun.management.OperatingSystemMXBean bean =
                        (com.sun.management.OperatingSystemMXBean) operatingSystemMXBean;
                load = bean.getSystemCpuLoad();
            }
        } catch (Throwable e) {
            load = -1;
        }
        int cpu = operatingSystemMXBean.getAvailableProcessors();
        return new Status(load < 0 ? Status.Level.UNKNOWN : (load < cpu ? Status.Level.OK : Status.Level.WARN),
                (load < 0 ? "" : "load:" + load + ",") + "cpu:" + cpu);
    }

}
  • LoadStatusChecker implements the StatusChecker interface. its check method reads load from the getSystemLoadAverage method of operatingSystemMXBean, and then calculates load according to the number of cpu

MemoryStatusChecker

dubbo-2.7.2/dubbo-common/src/main/java/org/apache/dubbo/common/status/support/MemoryStatusChecker.java

@Activate
public class MemoryStatusChecker implements StatusChecker {

    @Override
    public Status check() {
        Runtime runtime = Runtime.getRuntime();
        long freeMemory = runtime.freeMemory();
        long totalMemory = runtime.totalMemory();
        long maxMemory = runtime.maxMemory();
        boolean ok = (maxMemory - (totalMemory - freeMemory) > 2048); // Alarm when spare memory < 2M
        String msg = "max:" + (maxMemory / 1024 / 1024) + "M,total:"
                + (totalMemory / 1024 / 1024) + "M,used:" + ((totalMemory / 1024 / 1024) - (freeMemory / 1024 / 1024)) + "M,free:" + (freeMemory / 1024 / 1024) + "M";
        return new Status(ok ? Status.Level.OK : Status.Level.WARN, msg);
    }

}
  • MemoryStatusChecker implemented the StatusChecker interface. its check method obtains freeMemory, totalMemory, maxMemory from the Runtime. whenmaxMemory - (totalMemory - freeMemory) > 2048Return OK, otherwise return WARN.

Summary

  • The StatusChecker defines the check interface and returns Status; ; Status defines three attributes, namely level, message and description; ; Among them, level has four levels: OK, WARN, ERROR and UNKNOWN.
  • LoadStatusChecker implements the StatusChecker interface. its check method reads load from the getSystemLoadAverage method of operatingSystemMXBean, and then calculates load according to the number of cpu
  • MemoryStatusChecker implemented the StatusChecker interface. its check method obtains freeMemory, totalMemory, maxMemory from the Runtime. whenmaxMemory - (totalMemory - freeMemory) > 2048Return OK, otherwise return WARN.

doc