get groupId instead of groupname

This commit is contained in:
Mengyi Zhou 2015-06-25 16:05:15 +08:00
parent 9058efed0e
commit 67657a0f60
6 changed files with 55 additions and 34 deletions

View file

@ -119,14 +119,14 @@ public class RollingTrafficStatus {
for (String key : upstreamMap.keySet()) {
Long[] data = upstreamMap.get(key);
String[] hostUpstream = key.split("/");
String hostName, groupName;
hostName = groupName = "";
String hostName, groupId;
hostName = groupId = "";
if (hostUpstream.length > 0) {
hostName = hostUpstream[0];
if (hostUpstream.length > 1)
groupName = hostUpstream[1].replaceFirst("backend_", "");
groupId = hostUpstream[1].replaceFirst("backend_", "");
}
if (groupName.equals(""))
if (groupId.equals(""))
return;
Long upRequests = data[ReqStatusOffset.UpstreamReq.ordinal()];
double upResponseTime = (upRequests == null || upRequests == 0) ? 0 : (double) data[ReqStatusOffset.UpstreamRt.ordinal()] / upRequests;
@ -137,7 +137,7 @@ public class RollingTrafficStatus {
.setBytesOutTotal(data[ReqStatusOffset.BytOutTotal.ordinal()])
.setResponseTime(responseTime)
.setTotalRequests(requests)
.setGroupName(groupName)
.setGroupId(Long.parseLong(groupId))
.setUpRequests(upRequests)
.setUpResponseTime(upResponseTime)
.setUpTries(data[ReqStatusOffset.UpstreamTries.ordinal()])

View file

@ -6,10 +6,12 @@ import com.ctrip.zeus.nginx.entity.ReqStatus;
* Created by zhoumy on 2015/6/25.
*/
public class TrafficStatusHelper {
public static ReqStatus add(ReqStatus origin, ReqStatus delta, String groupName, String hostname) {
public static ReqStatus add(ReqStatus origin, ReqStatus delta, String hostname, Long slbId, Long groupId, String groupName) {
if (origin == null)
return delta.setGroupName(groupName).setHostName(hostname);
return new ReqStatus().setHostName(hostname).setGroupName(groupName)
return delta.setHostName(hostname).setSlbId(slbId)
.setGroupName(groupName).setGroupId(groupId);
return new ReqStatus().setHostName(hostname).setSlbId(slbId)
.setGroupName(groupName).setGroupId(groupId)
.setBytesInTotal(origin.getBytesInTotal() + delta.getBytesInTotal())
.setBytesOutTotal(origin.getBytesOutTotal() + delta.getBytesOutTotal())
.setSuccessCount(origin.getSuccessCount() + delta.getSuccessCount())

View file

@ -12,6 +12,7 @@ import com.ctrip.zeus.nginx.entity.NginxServerStatus;
import com.ctrip.zeus.nginx.entity.ReqStatus;
import com.ctrip.zeus.nginx.entity.TrafficStatus;
import com.ctrip.zeus.service.build.NginxConfService;
import com.ctrip.zeus.service.model.GroupRepository;
import com.ctrip.zeus.service.model.SlbRepository;
import com.ctrip.zeus.service.nginx.NginxService;
import com.ctrip.zeus.nginx.RollingTrafficStatus;
@ -41,6 +42,8 @@ public class NginxServiceImpl implements NginxService {
@Resource
private SlbRepository slbRepository;
@Resource
private GroupRepository groupRepository;
@Resource
private NginxConfService nginxConfService;
@Resource
private NginxServerDao nginxServerDao;
@ -265,10 +268,19 @@ public class NginxServiceImpl implements NginxService {
@Override
public List<ReqStatus> getTrafficStatusBySlb(Long slbId, int count, boolean aggregatedByGroup, boolean aggregatedByMember) throws Exception {
List<ReqStatus> result = getTrafficStatusBySlb(slbId, count);
if (aggregatedByGroup && aggregatedByMember) {
return result;
if (!(aggregatedByGroup && aggregatedByMember)) {
result = aggregateByKey(result, aggregatedByGroup, aggregatedByMember, slbId);
}
return aggregateByKey(result, aggregatedByGroup, aggregatedByMember);
if (aggregatedByGroup) {
for (ReqStatus reqStatus : result) {
Group g = groupRepository.getById(reqStatus.getGroupId());
if (g == null)
reqStatus.setGroupName("Not Found");
else
reqStatus.setGroupName(g.getName());
}
}
return result;
}
private List<ReqStatus> getTrafficStatusBySlb(Long slbId, int count) throws Exception {
@ -285,20 +297,20 @@ public class NginxServiceImpl implements NginxService {
return list;
}
private List<ReqStatus> aggregateByKey(List<ReqStatus> raw, boolean group, boolean member) {
private List<ReqStatus> aggregateByKey(List<ReqStatus> raw, boolean group, boolean member, Long slbId) {
Map<String, ReqStatus> result = new ConcurrentHashMap<>();
for (ReqStatus reqStatus : raw) {
String key = genKey(reqStatus, group, member);
ReqStatus value = result.get(key);
if (group) {
result.put(key, TrafficStatusHelper.add(value, reqStatus, reqStatus.getGroupName(), null));
result.put(key, TrafficStatusHelper.add(value, reqStatus, "", slbId, null, reqStatus.getGroupName()));
continue;
}
if (member) {
result.put(key, TrafficStatusHelper.add(value, reqStatus, null, reqStatus.getHostName()));
result.put(key, TrafficStatusHelper.add(value, reqStatus, reqStatus.getHostName(), slbId, -1L, ""));
continue;
}
result.put(key, TrafficStatusHelper.add(value, reqStatus, null, null));
result.put(key, TrafficStatusHelper.add(value, reqStatus, "", slbId, -1L, ""));
}
return new LinkedList<>(result.values());
}

View file

@ -54,6 +54,8 @@
</entity>
<entity name="req-status">
<element name="host-name" value-type="String" />
<element name="slb-id" value-type="long" />
<element name="group-id" value-type="long" />
<element name="group-name" value-type="String" />
<element name="bytes-in-total" value-type="long" />
<element name="bytes-out-total" value-type="long" />

View file

@ -27,33 +27,33 @@ public class TrafficStatusTest {
@Test
public void testParseReqStatus() {
String[] reqStatues = new String[3];
reqStatues[0] = "localhost/backend_cluster,8348,3738,1,21,21,1,2,3,4,14,21,14,21";
reqStatues[0] = "localhost/103288465,8348,3738,1,21,21,1,2,3,4,14,21,14,21";
reqStatues[1] = "localhost/,2501999,3760318,141,13106,13104,0,2,0,0,13104,0,0,0";
reqStatues[2] = "localhost/backend_upstream,408,128,1,7,7,7,0,0,0,14,6,18,8";
reqStatues[2] = "localhost/923,408,128,1,7,7,7,0,0,0,14,6,18,8";
TrafficStatus trafficStatus = new TrafficStatus();
RollingTrafficStatus obj = new RollingTrafficStatus(0, 0);
Map<String, Long[]> map = obj.parseReqStatusEntries(reqStatues);
RollingTrafficStatus.extractReqStatus(map, trafficStatus);
ReqStatus ref1 = new ReqStatus().setHostName("localhost").setGroupName("cluster")
ReqStatus ref1 = new ReqStatus().setHostName("localhost").setGroupId(103288465L)
.setBytesInTotal(8348L).setBytesOutTotal(3738L)
.setTotalRequests(21L).setResponseTime((double)14/21).setSuccessCount(21L)
.setRedirectionCount(1L).setClientErrCount(2L).setServerErrCount(3L).setUpRequests(21L)
.setUpResponseTime((double)14/21).setUpTries(21L);
ReqStatus ref2 = new ReqStatus().setHostName("localhost").setGroupName("")
ReqStatus ref2 = new ReqStatus().setHostName("localhost").setGroupId(0L)
.setBytesInTotal(2501999L).setBytesOutTotal(3760318L)
.setTotalRequests(13106L).setResponseTime((double) 13104/13106).setSuccessCount(13104L)
.setRedirectionCount(0L).setClientErrCount(2L).setServerErrCount(0L).setUpRequests(0L)
.setUpResponseTime(0.0).setUpTries(0L);
ReqStatus ref3 = new ReqStatus().setHostName("localhost").setGroupName("upstream")
ReqStatus ref3 = new ReqStatus().setHostName("localhost").setGroupId(923L)
.setBytesInTotal(408L).setBytesOutTotal(128L)
.setTotalRequests(7L).setResponseTime((double) 14/7).setSuccessCount(7L)
.setRedirectionCount(7L).setClientErrCount(0L).setServerErrCount(0L).setUpRequests(6L)
.setUpResponseTime((double) 18/6).setUpTries(8L);
// map reorder
assertReqStatusEquals(ref1, trafficStatus.getReqStatuses().get(2));
assertReqStatusEquals(ref2, trafficStatus.getReqStatuses().get(1));
assertReqStatusEquals(ref3, trafficStatus.getReqStatuses().get(0));
Assert.assertEquals(trafficStatus.getReqStatuses().size(), 2);
assertReqStatusEquals(ref1, trafficStatus.getReqStatuses().get(0));
assertReqStatusEquals(ref3, trafficStatus.getReqStatuses().get(1));
}
@Test
@ -100,15 +100,15 @@ public class TrafficStatusTest {
" server accepts handled requests request_time\n" +
" 11 13 15 17\n" +
" Reading: 0 Writing: 1 Waiting: 2";
String rs1 = "localhost/backend_cluster,1200,1358,0,1,2,3,4,5,0,6,7,8,9";
String rs2 = "localhost/backend_cluster,1962,3592,0,21,20,19,18,17,0,16,15,14,13";
String rs3 = "localhost/backend_cluster,4783,7688,0,30,32,34,36,38,0,40,42,44,46";
String rs1 = "localhost/103288465,1200,1358,0,1,2,3,4,5,0,6,7,8,9";
String rs2 = "localhost/103288465,1962,3592,0,21,20,19,18,17,0,16,15,14,13";
String rs3 = "localhost/103288465,4783,7688,0,30,32,34,36,38,0,40,42,44,46";
obj.add(ss1, rs1);
obj.add(ss2, rs2);
obj.add(ss3, rs3);
TrafficStatus ref1 = new TrafficStatus();
ReqStatus reqref1 = new ReqStatus().setHostName("localhost").setGroupName("cluster")
ReqStatus reqref1 = new ReqStatus().setHostName("localhost").setGroupId(103288465L)
.setBytesInTotal(762L).setBytesOutTotal(2234L)
.setTotalRequests(20L).setSuccessCount(18L)
.setRedirectionCount(16L).setClientErrCount(14L).setServerErrCount(12L)
@ -117,11 +117,11 @@ public class TrafficStatusTest {
.setReading(0L).setWriting(1L).setWaiting(1L);
TrafficStatus ref2 = new TrafficStatus();
ReqStatus reqref2 = new ReqStatus().setHostName("localhost").setGroupName("cluster")
ReqStatus reqref2 = new ReqStatus().setHostName("localhost").setGroupId(103288465L)
.setBytesInTotal(2821L).setBytesOutTotal(4096L)
.setTotalRequests(9L).setSuccessCount(12L)
.setRedirectionCount(15L).setClientErrCount(18L).setServerErrCount(21L)
.setResponseTime((double) 24/9).setUpRequests(27L).setUpResponseTime((double) 30/27).setUpTries(33L);
.setResponseTime((double) 24 / 9).setUpRequests(27L).setUpResponseTime((double) 30 / 27).setUpTries(33L);
ref2.setActiveConnections(1L).setAccepts(9L).setHandled(10L).setRequests(11L).setResponseTime((double)12/11)
.setReading(0L).setWriting(1L).setWaiting(2L);
@ -136,22 +136,25 @@ public class TrafficStatusTest {
@Test
public void testAggregateByGroup() {
Date now = new Date();
ReqStatus origin = new ReqStatus().setHostName("123").setGroupName("group")
ReqStatus origin = new ReqStatus().setHostName("123").setSlbId(25L)
.setGroupId(1L).setGroupName("group")
.setBytesInTotal(8348L).setBytesOutTotal(3738L)
.setTotalRequests(21L).setResponseTime((double)14/21).setSuccessCount(21L)
.setRedirectionCount(1L).setClientErrCount(2L).setServerErrCount(3L).setUpRequests(21L)
.setUpResponseTime((double)14/21).setUpTries(21L).setTime(now);
ReqStatus delta = new ReqStatus().setHostName("456").setGroupName("group")
ReqStatus delta = new ReqStatus().setHostName("456").setSlbId(25L)
.setGroupId(1L).setGroupName("group")
.setBytesInTotal(2501999L).setBytesOutTotal(3760318L)
.setTotalRequests(13106L).setResponseTime((double) 13104/13106).setSuccessCount(13104L)
.setRedirectionCount(0L).setClientErrCount(2L).setServerErrCount(0L).setUpRequests(0L)
.setUpResponseTime(0.0).setUpTries(0L).setTime(now);
ReqStatus answer = new ReqStatus().setHostName(null).setGroupName("group")
ReqStatus answer = new ReqStatus().setHostName("").setSlbId(25L)
.setGroupId(1L).setGroupName("group")
.setBytesInTotal(8348L + 2501999L).setBytesOutTotal(3738L + 3760318L)
.setTotalRequests(21L + 13106L).setResponseTime((double) 14 / 21 + (double) 13104 / 13106).setSuccessCount(21L + 13104L)
.setRedirectionCount(1L).setClientErrCount(4L).setServerErrCount(3L).setUpRequests(21L)
.setUpResponseTime((double) 14 / 21 + 0.0).setUpTries(21L).setTime(now);
assertReqStatusEquals(answer, TrafficStatusHelper.add(origin, delta, "group", null));
assertReqStatusEquals(answer, TrafficStatusHelper.add(origin, delta, "", 25L, 1L, "group"));
}
private void testGetDelta(int length, int round) {
@ -167,7 +170,7 @@ public class TrafficStatusTest {
}
private static void assertReqStatusEquals(ReqStatus expected, ReqStatus actual) {
Assert.assertEquals(expected.getHostName() + "/" + expected.getGroupName(), actual.getHostName() + "/" + actual.getGroupName());
Assert.assertEquals(expected.getHostName() + "/" + expected.getGroupId(), actual.getHostName() + "/" + actual.getGroupId());
Assert.assertTrue(expected.getBytesInTotal().longValue() == actual.getBytesInTotal().longValue() &&
expected.getBytesOutTotal().longValue() == actual.getBytesOutTotal().longValue() &&
expected.getClientErrCount().longValue() == actual.getClientErrCount().longValue() &&

View file

@ -78,6 +78,8 @@
</traffic-status>
<req-status>
<host-name>ctrip.com</host-name>
<slb-id>123456789012</slb-id>
<group-id>123456789012</group-id>
<group-name>cluster</group-name>
<bytes-in-total>17915825952</bytes-in-total>
<bytes-out-total>17915825952</bytes-out-total>