Search in sources :

Example 1 with ServerBriefInfo

use of com.dangdang.ddframe.job.lite.lifecycle.domain.ServerBriefInfo in project elastic-job by dangdangdotcom.

the class ServerStatisticsAPIImpl method getServerBriefInfo.

private ServerBriefInfo getServerBriefInfo(final Collection<String> aliveServers, final Collection<String> crashedServers, final String serverIp, final String hostName) {
    ServerBriefInfo result = new ServerBriefInfo();
    result.setServerIp(serverIp);
    result.setServerHostName(hostName);
    result.setStatus(ServerBriefInfo.ServerBriefStatus.getServerBriefStatus(aliveServers, crashedServers, serverIp));
    return result;
}
Also used : ServerBriefInfo(com.dangdang.ddframe.job.lite.lifecycle.domain.ServerBriefInfo)

Example 2 with ServerBriefInfo

use of com.dangdang.ddframe.job.lite.lifecycle.domain.ServerBriefInfo in project elastic-job by dangdangdotcom.

the class ServerStatisticsAPIImplTest method assertGetAllServersBriefInfo.

@Test
public void assertGetAllServersBriefInfo() {
    when(regCenter.getChildrenKeys("/")).thenReturn(Arrays.asList("test_job1", "test_job2"));
    when(regCenter.getChildrenKeys("/test_job1/servers")).thenReturn(Arrays.asList("ip1", "ip2"));
    when(regCenter.getChildrenKeys("/test_job2/servers")).thenReturn(Arrays.asList("ip3", "ip4"));
    when(regCenter.get("/test_job1/servers/ip1/hostName")).thenReturn("host1");
    when(regCenter.get("/test_job1/servers/ip2/hostName")).thenReturn("host2");
    when(regCenter.get("/test_job2/servers/ip3/hostName")).thenReturn("host3");
    when(regCenter.get("/test_job2/servers/ip4/hostName")).thenReturn("host4");
    when(regCenter.isExisted("/test_job1/servers/ip1/shutdown")).thenReturn(false);
    when(regCenter.isExisted("/test_job1/servers/ip1/status")).thenReturn(true);
    when(regCenter.isExisted("/test_job1/servers/ip2/shutdown")).thenReturn(true);
    when(regCenter.isExisted("/test_job2/servers/ip3/shutdown")).thenReturn(false);
    when(regCenter.isExisted("/test_job2/servers/ip3/status")).thenReturn(false);
    when(regCenter.isExisted("/test_job2/servers/ip4/shutdown")).thenReturn(false);
    when(regCenter.isExisted("/test_job2/servers/ip4/status")).thenReturn(true);
    int i = 0;
    for (ServerBriefInfo each : serverStatisticsAPI.getAllServersBriefInfo()) {
        i++;
        assertThat(each.getServerIp(), is("ip" + i));
        assertThat(each.getServerHostName(), is("host" + i));
        switch(i) {
            case 1:
                assertThat(each.getStatus(), is(ServerBriefInfo.ServerBriefStatus.OK));
                break;
            case 2:
                assertThat(each.getStatus(), is(ServerBriefInfo.ServerBriefStatus.ALL_CRASHED));
                break;
            case 3:
                assertThat(each.getStatus(), is(ServerBriefInfo.ServerBriefStatus.ALL_CRASHED));
                break;
            case 4:
                assertThat(each.getStatus(), is(ServerBriefInfo.ServerBriefStatus.OK));
                break;
            default:
                fail();
        }
    }
}
Also used : ServerBriefInfo(com.dangdang.ddframe.job.lite.lifecycle.domain.ServerBriefInfo) Test(org.junit.Test)

Example 3 with ServerBriefInfo

use of com.dangdang.ddframe.job.lite.lifecycle.domain.ServerBriefInfo in project elastic-job by dangdangdotcom.

the class ServerStatisticsAPIImpl method getAllServersBriefInfo.

@Override
public Collection<ServerBriefInfo> getAllServersBriefInfo() {
    Map<String, String> serverHostMap = new HashMap<>();
    Collection<String> aliveServers = new ArrayList<>();
    Collection<String> crashedServers = new ArrayList<>();
    List<String> jobs = regCenter.getChildrenKeys("/");
    for (String jobName : jobs) {
        JobNodePath jobNodePath = new JobNodePath(jobName);
        List<String> servers = regCenter.getChildrenKeys(jobNodePath.getServerNodePath());
        for (String server : servers) {
            serverHostMap.put(server, regCenter.get(jobNodePath.getServerNodePath(server, "hostName")));
            if (!regCenter.isExisted(jobNodePath.getServerNodePath(server, "shutdown")) && regCenter.isExisted(jobNodePath.getServerNodePath(server, "status"))) {
                aliveServers.add(server);
            } else {
                crashedServers.add(server);
            }
        }
    }
    List<ServerBriefInfo> result = new ArrayList<>(serverHostMap.size());
    for (Map.Entry<String, String> entry : serverHostMap.entrySet()) {
        result.add(getServerBriefInfo(aliveServers, crashedServers, entry.getKey(), entry.getValue()));
    }
    Collections.sort(result);
    return result;
}
Also used : HashMap(java.util.HashMap) ServerBriefInfo(com.dangdang.ddframe.job.lite.lifecycle.domain.ServerBriefInfo) ArrayList(java.util.ArrayList) JobNodePath(com.dangdang.ddframe.job.lite.internal.storage.JobNodePath) Map(java.util.Map) HashMap(java.util.HashMap)

Aggregations

ServerBriefInfo (com.dangdang.ddframe.job.lite.lifecycle.domain.ServerBriefInfo)3 JobNodePath (com.dangdang.ddframe.job.lite.internal.storage.JobNodePath)1 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 Map (java.util.Map)1 Test (org.junit.Test)1