Search in sources :

Example 1 with Status

use of org.apache.dubbo.common.status.Status in project dubbo by alibaba.

the class LoadStatusChecker method check.

@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);
}
Also used : Status(org.apache.dubbo.common.status.Status) Method(java.lang.reflect.Method) OperatingSystemMXBean(java.lang.management.OperatingSystemMXBean)

Example 2 with Status

use of org.apache.dubbo.common.status.Status in project dubbo by alibaba.

the class StatusUtils method getSummaryStatus.

public static Status getSummaryStatus(Map<String, Status> statuses) {
    Level level = Level.OK;
    StringBuilder msg = new StringBuilder();
    for (Map.Entry<String, Status> entry : statuses.entrySet()) {
        String key = entry.getKey();
        Status status = entry.getValue();
        Level l = status.getLevel();
        if (Level.ERROR.equals(l)) {
            level = Level.ERROR;
            if (msg.length() > 0) {
                msg.append(",");
            }
            msg.append(key);
        } else if (Level.WARN.equals(l)) {
            if (!Level.ERROR.equals(level)) {
                level = Level.WARN;
            }
            if (msg.length() > 0) {
                msg.append(",");
            }
            msg.append(key);
        }
    }
    return new Status(level, msg.toString());
}
Also used : Status(org.apache.dubbo.common.status.Status) Level(org.apache.dubbo.common.status.Status.Level) Map(java.util.Map)

Example 3 with Status

use of org.apache.dubbo.common.status.Status in project dubbo by alibaba.

the class ThreadPoolStatusCheckerTest method statusWarnTest.

@Test
public void statusWarnTest() {
    int activeCount = 1;
    int maximumPoolSize = 2;
    String portKey = "8888";
    mockThreadPoolExecutor(activeCount, maximumPoolSize, portKey);
    Status status = threadPoolStatusChecker.check();
    Assertions.assertEquals(status.getLevel(), Status.Level.WARN);
    Assertions.assertEquals(status.getMessage(), "Pool status:WARN, max:" + maximumPoolSize + ", core:0, largest:0, active:" + activeCount + ", task:0, service port: 8888");
    destroy(portKey);
}
Also used : Status(org.apache.dubbo.common.status.Status) Test(org.junit.jupiter.api.Test)

Example 4 with Status

use of org.apache.dubbo.common.status.Status in project dubbo by alibaba.

the class SpringStatusChecker method check.

@Override
public Status check() {
    ApplicationContext context = null;
    for (ApplicationContext c : SpringExtensionFactory.getContexts()) {
        // issue : https://github.com/apache/dubbo/issues/3615
        if (c instanceof GenericWebApplicationContext) {
            // ignore GenericXmlApplicationContext
            continue;
        }
        if (c != null) {
            context = c;
            break;
        }
    }
    if (context == null) {
        return new Status(Status.Level.UNKNOWN);
    }
    Status.Level level;
    if (context instanceof Lifecycle) {
        if (((Lifecycle) context).isRunning()) {
            level = Status.Level.OK;
        } else {
            level = Status.Level.ERROR;
        }
    } else {
        level = Status.Level.UNKNOWN;
    }
    StringBuilder buf = new StringBuilder();
    try {
        Class<?> cls = context.getClass();
        Method method = null;
        while (cls != null && method == null) {
            try {
                method = cls.getDeclaredMethod("getConfigLocations", new Class<?>[0]);
            } catch (NoSuchMethodException t) {
                cls = cls.getSuperclass();
            }
        }
        if (method != null) {
            ReflectUtils.makeAccessible(method);
            String[] configs = (String[]) method.invoke(context, new Object[0]);
            if (configs != null && configs.length > 0) {
                for (String config : configs) {
                    if (buf.length() > 0) {
                        buf.append(",");
                    }
                    buf.append(config);
                }
            }
        }
    } catch (Throwable t) {
        logger.warn(t.getMessage(), t);
    }
    return new Status(level, buf.toString());
}
Also used : Status(org.apache.dubbo.common.status.Status) Lifecycle(org.springframework.context.Lifecycle) Method(java.lang.reflect.Method) GenericWebApplicationContext(org.springframework.web.context.support.GenericWebApplicationContext) ApplicationContext(org.springframework.context.ApplicationContext) GenericWebApplicationContext(org.springframework.web.context.support.GenericWebApplicationContext)

Example 5 with Status

use of org.apache.dubbo.common.status.Status in project dubbo by alibaba.

the class RegistryStatusChecker method check.

@Override
public Status check() {
    Collection<Registry> registries = AbstractRegistryFactory.getRegistries();
    if (registries.isEmpty()) {
        return new Status(Status.Level.UNKNOWN);
    }
    Status.Level level = Status.Level.OK;
    StringBuilder buf = new StringBuilder();
    for (Registry registry : registries) {
        if (buf.length() > 0) {
            buf.append(",");
        }
        buf.append(registry.getUrl().getAddress());
        if (!registry.isAvailable()) {
            level = Status.Level.ERROR;
            buf.append("(disconnected)");
        } else {
            buf.append("(connected)");
        }
    }
    return new Status(level, buf.toString());
}
Also used : Status(org.apache.dubbo.common.status.Status) Registry(org.apache.dubbo.registry.Registry)

Aggregations

Status (org.apache.dubbo.common.status.Status)27 Test (org.junit.jupiter.api.Test)19 HashMap (java.util.HashMap)7 DataSource (javax.sql.DataSource)4 Connection (java.sql.Connection)3 Map (java.util.Map)3 Registry (org.apache.dubbo.registry.Registry)3 Matchers.containsString (org.hamcrest.Matchers.containsString)3 Matchers.isEmptyOrNullString (org.hamcrest.Matchers.isEmptyOrNullString)3 Method (java.lang.reflect.Method)2 StatusChecker (org.apache.dubbo.common.status.StatusChecker)2 RegistryStatusChecker (org.apache.dubbo.registry.status.RegistryStatusChecker)2 ApplicationContext (org.springframework.context.ApplicationContext)2 GenericWebApplicationContext (org.springframework.web.context.support.GenericWebApplicationContext)2 OperatingSystemMXBean (java.lang.management.OperatingSystemMXBean)1 DatabaseMetaData (java.sql.DatabaseMetaData)1 ResultSet (java.sql.ResultSet)1 ArrayList (java.util.ArrayList)1 List (java.util.List)1 ExecutorService (java.util.concurrent.ExecutorService)1