diff --git a/src/main/java/com/ctrip/zeus/restful/resource/StatusResource.java b/src/main/java/com/ctrip/zeus/restful/resource/StatusResource.java index faa17598..1b75fc35 100644 --- a/src/main/java/com/ctrip/zeus/restful/resource/StatusResource.java +++ b/src/main/java/com/ctrip/zeus/restful/resource/StatusResource.java @@ -12,7 +12,6 @@ import com.ctrip.zeus.service.model.SelectionMode; import com.ctrip.zeus.service.query.CriteriaQueryFactory; import com.ctrip.zeus.service.query.GroupCriteriaQuery; import com.ctrip.zeus.service.query.QueryEngine; -import com.ctrip.zeus.service.query.SlbCriteriaQuery; import com.ctrip.zeus.service.status.GroupStatusService; import com.ctrip.zeus.status.entity.GroupStatus; import com.ctrip.zeus.status.entity.GroupStatusList; @@ -42,8 +41,6 @@ public class StatusResource { @Resource private GroupCriteriaQuery groupCriteriaQuery; @Resource - private SlbCriteriaQuery slbCriteriaQuery; - @Resource private ResponseHandler responseHandler; @Resource private GlobalJobDao globalJobDao; @@ -57,15 +54,14 @@ public class StatusResource { public Response allGroupStatusInSlb(@Context HttpServletRequest request, @Context HttpHeaders hh, @TrimmedQueryParam("mode") final String mode, @Context UriInfo uriInfo) throws Exception { - List statusList = null; QueryEngine queryRender = new QueryEngine(QueryParamRender.extractRawQueryParam(uriInfo), "group", SelectionMode.getMode(mode)); queryRender.init(true); IdVersion[] searchKeys = queryRender.run(criteriaQueryFactory); Set groupIds = new HashSet<>(); - for (IdVersion idv : searchKeys){ + for (IdVersion idv : searchKeys) { groupIds.add(idv.getId()); } - statusList = groupStatusService.getOfflineGroupsStatus(groupIds); + List statusList = groupStatusService.getOfflineGroupsStatus(groupIds); GroupStatusList result = new GroupStatusList(); for (GroupStatus groupStatus : statusList) { result.addGroupStatus(groupStatus); @@ -79,41 +75,25 @@ public class StatusResource { @Authorize(name = "getGroupStatus", uriGroupHint = -1) public Response groupStatus(@Context HttpServletRequest request, @Context HttpHeaders hh, @QueryParam("groupId") Long groupId, - @QueryParam("groupName") String groupName, - @QueryParam("slbId") Long slbId, - @QueryParam("slbName") String slbName) throws Exception { - GroupStatus statusResult = null; - + @QueryParam("groupName") String groupName) throws Exception { if (groupId == null) { if (groupName != null) { groupId = groupCriteriaQuery.queryByName(groupName); } } - if (null == groupId) { - throw new ValidationException("Group Id or Name not found!"); + if (groupId == null) { + throw new ValidationException("Cannot find group by groupName " + groupName + "."); } - if (slbId == null) { - if (slbName != null) { - slbId = slbCriteriaQuery.queryByName(slbName); - } - } - if (null == slbId) { - GroupStatus status = groupStatusService.getOfflineGroupStatus(groupId); - if (status != null) { - statusResult = status; - } else { - throw new ValidationException("Not Found Group Status In Slb!"); - } - } else { - GroupStatus res = groupStatusService.getOfflineGroupStatus(groupId, slbId); - if (res != null) { - statusResult = res; - } else { - throw new ValidationException("Not Found Group Status!"); - } + IdVersion[] check = groupCriteriaQuery.queryByIdAndMode(groupId, SelectionMode.REDUNDANT); + if (check.length == 0) { + throw new ValidationException("Cannot find group by groupId " + groupId + "."); } - return responseHandler.handle(statusResult, hh.getMediaType()); + GroupStatus status = groupStatusService.getOfflineGroupStatus(groupId); + if (status == null) { + throw new ValidationException("Not Found Group Status In Slb!"); + } + return responseHandler.handle(status, hh.getMediaType()); } @GET diff --git a/src/main/java/com/ctrip/zeus/service/build/conf/HealthCheckConf.java b/src/main/java/com/ctrip/zeus/service/build/conf/HealthCheckConf.java deleted file mode 100644 index 9db5fc19..00000000 --- a/src/main/java/com/ctrip/zeus/service/build/conf/HealthCheckConf.java +++ /dev/null @@ -1,94 +0,0 @@ -package com.ctrip.zeus.service.build.conf; - -import com.ctrip.zeus.model.entity.Group; -import com.ctrip.zeus.model.entity.HealthCheck; -import com.ctrip.zeus.model.entity.VirtualServer; -import com.ctrip.zeus.service.build.ConfigHandler; -import com.ctrip.zeus.util.AssertUtils; -import org.springframework.stereotype.Component; - -import javax.annotation.Resource; - -/** - * @author:xingchaowang - * @date: 3/8/2015. - */ -@Component("healthCheckConf") -public class HealthCheckConf { - - @Resource - ConfigHandler configHandler; - - public String generate(VirtualServer vs, Group group) throws Exception { - Long vsId = vs.getId(); - Long groupId = group.getId(); - Long slbId = vs.getSlbId(); - - if (!configHandler.getEnable("upstream.healthCheck", slbId, vsId, groupId, true)) { - return ""; - } - - HealthCheck h = group.getHealthCheck(); - if (h == null) { - return ""; - } - AssertUtils.assertNotNull(h.getIntervals(), "Group HealthCheck Intervals config is null!"); - AssertUtils.assertNotNull(h.getFails(), "Group HealthCheck Fails config is null!"); - AssertUtils.assertNotNull(h.getPasses(), "Group HealthCheck Passes config is null!"); - AssertUtils.assertNotNull(h.getUri(), "Group HealthCheck Uri config is null!"); - - StringBuilder b = new StringBuilder(128); - - String healthCheckTimeout = configHandler.getStringValue("upstream.healthCheck.timeOut", null, vsId, groupId, "2000"); - String sslAspx = configHandler.getStringValue("upstream.healthCheck.ssl.aspx", null, vsId, groupId, "/SlbHealthCheck.aspx"); - - if (group.getSsl() && configHandler.getEnable("upstream.healthCheck.sslHello", null, vsId, groupId, false)) { - b.append(" check interval=").append(h.getIntervals()) - .append(" rise=").append(h.getPasses()) - .append(" fall=").append(h.getFails()) - .append(" timeout=").append(h.getTimeout() == null ? healthCheckTimeout : h.getTimeout()) - .append(" type=ssl_hello").append(";\n"); - } else if (group.getSsl() && h.getUri().equalsIgnoreCase(sslAspx)) { - b.append(" check interval=").append(h.getIntervals()) - .append(" rise=").append(h.getPasses()) - .append(" fall=").append(h.getFails()) - .append(" timeout=").append(h.getTimeout() == null ? healthCheckTimeout : h.getTimeout()); - b.append(" port=").append(80); - b.append(" type=http default_down=false").append(";\n") - .append(" check_keepalive_requests 100").append(";\n") - .append(" check_http_send \"") - .append("GET ").append(h.getUri()).append(" HTTP/1.0\\r\\n") - .append("Connection:keep-alive\\r\\n"); - b.append("UserAgent:SLB_HealthCheck").append("\\r\\n\\r\\n\"").append(";\n") - .append(" check_http_expect_alive http_2xx http_3xx").append(";\n"); - } else if (group.getSsl()) { - b.append(" check interval=").append(h.getIntervals()) - .append(" rise=").append(h.getPasses()) - .append(" fall=").append(h.getFails()) - .append(" timeout=").append(h.getTimeout() == null ? healthCheckTimeout : h.getTimeout()); - b.append(" port=").append(80); - b.append(" type=http default_down=false").append(";\n") - .append(" check_keepalive_requests 100").append(";\n") - .append(" check_http_send \"") - .append("GET ").append(h.getUri()).append(" HTTP/1.1\\r\\n") - .append("Connection:keep-alive\\r\\n") - .append("Host:").append(vs.getDomains().get(0).getName().trim()).append("\\r\\n"); - b.append("UserAgent:SLB_HealthCheck").append("\\r\\n\\r\\n\"").append(";\n") - .append(" check_http_expect_alive http_2xx http_3xx").append(";\n"); - }else { - b.append(" check interval=").append(h.getIntervals()) - .append(" rise=").append(h.getPasses()) - .append(" fall=").append(h.getFails()) - .append(" timeout=").append(h.getTimeout() == null ? healthCheckTimeout : h.getTimeout()); - b.append(" type=http default_down=false").append(";\n") - .append(" check_keepalive_requests 100").append(";\n") - .append(" check_http_send \"") - .append("GET ").append(h.getUri()).append(" HTTP/1.1\\r\\n") - .append("Connection:keep-alive\\r\\n") - .append("Host:").append(vs.getDomains().get(0).getName().trim()).append("\\r\\n"); - b.append("User-Agent:SLB_HealthCheck").append("\\r\\n\\r\\n\"").append(";\n") - .append(" check_http_expect_alive http_2xx http_3xx").append(";\n"); - } - return b.toString(); - } -} diff --git a/src/main/java/com/ctrip/zeus/service/build/conf/UpstreamsConf.java b/src/main/java/com/ctrip/zeus/service/build/conf/UpstreamsConf.java index ad73e04c..091d0f11 100644 --- a/src/main/java/com/ctrip/zeus/service/build/conf/UpstreamsConf.java +++ b/src/main/java/com/ctrip/zeus/service/build/conf/UpstreamsConf.java @@ -20,8 +20,6 @@ import java.util.*; public class UpstreamsConf { @Resource ConfigHandler configHandler; - @Resource - HealthCheckConf healthCheckConf; public static final String UpstreamPrefix = "backend_"; @@ -113,8 +111,6 @@ public class UpstreamsConf { confWriter.writeCommand("keepalive_timeout", configHandler.getStringValue("upstream.keepAlive.timeout", slbId, vsId, groupId, "110") + "s"); } - // This module is to be abandoned. - confWriter.getStringBuilder().append(healthCheckConf.generate(vs, group)); confWriter.writeUpstreamEnd(); } diff --git a/src/main/java/com/ctrip/zeus/service/status/GroupStatusService.java b/src/main/java/com/ctrip/zeus/service/status/GroupStatusService.java index 1b61e815..fe5ae289 100644 --- a/src/main/java/com/ctrip/zeus/service/status/GroupStatusService.java +++ b/src/main/java/com/ctrip/zeus/service/status/GroupStatusService.java @@ -16,32 +16,35 @@ import java.util.Set; * Time: 10:54 AM */ public interface GroupStatusService { - /** - * Find all online groups status - * @return status list - * @throws Exception - */ - List getAllOnlineGroupsStatus() throws Exception; - - /** - * Find all online group status in the specific slb cluster - * @param slbId - * @return status list - * @throws Exception - */ - List getOnlineGroupsStatusBySlbId(Long slbId) throws Exception; - /** - * Find online group status by groupId - * @param groups groupId - * @param slbId slbId + * Find all online groups status + * * @return status list * @throws Exception */ - List getOnlineGroupsStatus(Map groups , Long slbId) throws Exception; + List getAllOnlineGroupsStatus() throws Exception; + + /** + * Find all online group status in the specific slb cluster + * + * @param slbId + * @return status list + * @throws Exception + */ + List getOnlineGroupsStatusBySlbId(Long slbId) throws Exception; + + /** + * Find online group status by groupId + * + * @param groups groupId + * @return status list + * @throws Exception + */ + List getOnlineGroupsStatus(Map groups) throws Exception; /** * Find all online group status by groupIds and slb id + * * @param groupId groupId * @return status list * @throws Exception @@ -50,6 +53,7 @@ public interface GroupStatusService { /** * Find all online groups status + * * @return status list * @throws Exception */ @@ -57,6 +61,7 @@ public interface GroupStatusService { /** * Find all online group status in the specific slb cluster + * * @param slbId * @return status list * @throws Exception @@ -65,33 +70,27 @@ public interface GroupStatusService { /** * Find online group status by groupId + * * @param groupId groupId * @return status list * @throws Exception */ GroupStatus getOfflineGroupStatus(Long groupId) throws Exception; - /** + /** * Find online group status by groupId + * * @param groupIds groupIds * @return status list * @throws Exception */ List getOfflineGroupsStatus(Set groupIds) throws Exception; - /** - * Find online group status by groupId - * @param groupId groupId - * @return status list - * @throws Exception - */ - GroupStatus getOfflineGroupStatus(Long groupId,Long slbId) throws Exception; - /** * Find all online group status by groupIds and slb id - * @param slbId slbId + * * @return status list * @throws Exception */ - List getOfflineGroupsStatus(Map groups ,Map onlineGroups , Long slbId) throws Exception; + List getOfflineGroupsStatus(Map groups, Map onlineGroups) throws Exception; } diff --git a/src/main/java/com/ctrip/zeus/service/status/impl/GroupStatusServiceImpl.java b/src/main/java/com/ctrip/zeus/service/status/impl/GroupStatusServiceImpl.java index 744ca609..b900a81e 100644 --- a/src/main/java/com/ctrip/zeus/service/status/impl/GroupStatusServiceImpl.java +++ b/src/main/java/com/ctrip/zeus/service/status/impl/GroupStatusServiceImpl.java @@ -1,18 +1,13 @@ package com.ctrip.zeus.service.status.impl; import com.ctrip.zeus.dal.core.ConfSlbActiveDao; -import com.ctrip.zeus.exceptions.ValidationException; import com.ctrip.zeus.model.entity.Group; import com.ctrip.zeus.model.entity.GroupServer; -import com.ctrip.zeus.model.entity.GroupVirtualServer; -import com.ctrip.zeus.model.entity.VirtualServer; -import com.ctrip.zeus.service.build.ConfigHandler; import com.ctrip.zeus.service.model.*; import com.ctrip.zeus.service.query.GroupCriteriaQuery; import com.ctrip.zeus.service.query.SlbCriteriaQuery; import com.ctrip.zeus.service.query.VirtualServerCriteriaQuery; import com.ctrip.zeus.service.status.GroupStatusService; -import com.ctrip.zeus.service.status.HealthCheckStatusService; import com.ctrip.zeus.service.status.StatusOffset; import com.ctrip.zeus.service.status.StatusService; import com.ctrip.zeus.status.entity.GroupServerStatus; @@ -51,10 +46,6 @@ public class GroupStatusServiceImpl implements GroupStatusService { ConfSlbActiveDao confSlbActiveDao; @Resource EntityFactory entityFactory; - @Resource - private HealthCheckStatusService healthCheckStatusService; - @Resource - private ConfigHandler configHandler; private Logger LOGGER = LoggerFactory.getLogger(GroupStatusServiceImpl.class); @@ -87,7 +78,7 @@ public class GroupStatusServiceImpl implements GroupStatusService { if (groups.getOnlineMapping() == null || groups.getOnlineMapping().size() == 0) { return result; } - result = getOnlineGroupsStatus(groups.getOnlineMapping(), slbId); + result = getOnlineGroupsStatus(groups.getOnlineMapping()); return result; } @@ -99,39 +90,18 @@ public class GroupStatusServiceImpl implements GroupStatusService { if (groups.getOfflineMapping() == null || groups.getOfflineMapping().size() == 0) { return result; } - result = getOfflineGroupsStatus(groups.getOfflineMapping(), groups.getOnlineMapping(), slbId); + result = getOfflineGroupsStatus(groups.getOfflineMapping(), groups.getOnlineMapping()); return result; } @Override public GroupStatus getOnlineGroupStatus(Long groupId) throws Exception { GroupStatus result = null; - ModelStatusMapping map = entityFactory.getGroupsByIds(new Long[]{groupId}); - if (map.getOnlineMapping() == null || map.getOnlineMapping().size() == 0) { + ModelStatusMapping groupMap = entityFactory.getGroupsByIds(new Long[]{groupId}); + if (groupMap.getOnlineMapping().size() == 0) { return result; } - List vsId = new ArrayList<>(); - for (GroupVirtualServer gvs : map.getOnlineMapping().get(groupId).getGroupVirtualServers()) { - vsId.add(gvs.getVirtualServer().getId()); - } - ModelStatusMapping vsMap = entityFactory.getVsesByIds(vsId.toArray(new Long[]{})); - if (vsMap.getOnlineMapping() == null || vsMap.getOnlineMapping().size() == 0) { - return result; - } - Long slbId = null; - for (Long vid : vsMap.getOnlineMapping().keySet()) { - Long tmp = vsMap.getOnlineMapping().get(vid).getSlbId(); - if (tmp != null) { - slbId = tmp; - if (configHandler.getEnable("healthy.operation.active", slbId, null, null, false)) { - break; - } - } - if (slbId == null) { - throw new ValidationException("Not found slbId for Group Id:" + groupId); - } - } - List list = getOnlineGroupsStatus(map.getOnlineMapping(), slbId); + List list = getOnlineGroupsStatus(groupMap.getOnlineMapping()); if (list.size() > 0) { result = list.get(0); } @@ -141,32 +111,12 @@ public class GroupStatusServiceImpl implements GroupStatusService { @Override public GroupStatus getOfflineGroupStatus(Long groupId) throws Exception { GroupStatus result = null; - ModelStatusMapping map = entityFactory.getGroupsByIds(new Long[]{groupId}); - if (map.getOfflineMapping() == null || map.getOfflineMapping().size() == 0) { + ModelStatusMapping groupMap = entityFactory.getGroupsByIds(new Long[]{groupId}); + if (groupMap.getOfflineMapping().size() == 0) { return result; } - List vsId = new ArrayList<>(); - for (GroupVirtualServer gvs : map.getOfflineMapping().get(groupId).getGroupVirtualServers()) { - vsId.add(gvs.getVirtualServer().getId()); - } - ModelStatusMapping vsMap = entityFactory.getVsesByIds(vsId.toArray(new Long[]{})); - if (vsMap.getOfflineMapping() == null || vsMap.getOfflineMapping().size() == 0) { - return result; - } - Long slbId = null; - for (Long vid : vsMap.getOfflineMapping().keySet()) { - Long tmp = vsMap.getOfflineMapping().get(vid).getSlbId(); - if (tmp != null) { - slbId = tmp; - if (configHandler.getEnable("healthy.operation.active", slbId, null, null, false)) { - break; - } - } - } - if (slbId == null) { - throw new ValidationException("Not found slbId for Group Id:" + groupId); - } - List list = getOfflineGroupsStatus(map.getOfflineMapping(), map.getOnlineMapping(), slbId); + + List list = getOfflineGroupsStatus(groupMap.getOfflineMapping(), groupMap.getOnlineMapping()); if (list.size() > 0) { result = list.get(0); } @@ -180,41 +130,24 @@ public class GroupStatusServiceImpl implements GroupStatusService { if (map.getOfflineMapping() == null || map.getOfflineMapping().size() == 0) { return result; } - result = getOfflineGroupsStatus(map.getOfflineMapping(), map.getOnlineMapping(), -1L); + result = getOfflineGroupsStatus(map.getOfflineMapping(), map.getOnlineMapping()); return result; } @Override - public GroupStatus getOfflineGroupStatus(Long groupId, Long slbId) throws Exception { - GroupStatus result = null; - ModelStatusMapping map = entityFactory.getGroupsByIds(new Long[]{groupId}); - if (map.getOfflineMapping() == null || map.getOfflineMapping().size() == 0) { - return result; - } - List list = getOfflineGroupsStatus(map.getOfflineMapping(), map.getOnlineMapping(), slbId); - if (list.size() > 0) { - result = list.get(0); - } - return result; - } - - @Override - public List getOnlineGroupsStatus(Map groups, Long slbId) throws Exception { + public List getOnlineGroupsStatus(Map groups) throws Exception { List res = new ArrayList<>(); - GroupStatus status = null; - boolean healthyActivateFlag = healthyOpsActivate.get() && configHandler.getEnable("healthy.operation.active", slbId, null, null, false); + GroupStatus status = new GroupStatus(); Map> memberStatus = statusService.fetchGroupServerStatus(groups.keySet().toArray(new Long[]{})); Set allDownServers = statusService.findAllDownServers(); for (Group group : groups.values()) { Long groupId = group.getId(); - status = new GroupStatus(); status.setGroupId(groupId); - status.setSlbId(slbId); status.setGroupName(group.getName()); status.setActivated(true); - List groupServerList = group.getGroupServers();//groupRepository.listGroupServersByGroup(groupId); + List groupServerList = group.getGroupServers(); for (GroupServer gs : groupServerList) { GroupServerStatus groupServerStatus = new GroupServerStatus(); groupServerStatus.setIp(gs.getIp()); @@ -229,7 +162,7 @@ public class GroupStatusServiceImpl implements GroupStatusService { boolean pullIn = memberStatus.get(key).get(StatusOffset.PULL_OPS); boolean raise = memberStatus.get(key).get(StatusOffset.HEALTHY); boolean up = false; - if (healthyActivateFlag) { + if (healthyOpsActivate.get()) { up = memberUp && serverUp && pullIn && raise; } else { if (memberUp && serverUp && pullIn) { @@ -253,19 +186,16 @@ public class GroupStatusServiceImpl implements GroupStatusService { } @Override - public List getOfflineGroupsStatus(Map groups, Map onlineGroups, Long slbId) throws Exception { + public List getOfflineGroupsStatus(Map groups, Map onlineGroups) throws Exception { List res = new ArrayList<>(); - GroupStatus status = null; - boolean healthyActivateFlag = healthyOpsActivate.get() && configHandler.getEnable("healthy.operation.active", slbId, null, null, false); Map> memberStatus = statusService.fetchGroupServerStatus(groups.keySet().toArray(new Long[]{})); Set allDownServers = statusService.findAllDownServers(); for (Group group : groups.values()) { + GroupStatus status = new GroupStatus(); Long groupId = group.getId(); - status = new GroupStatus(); status.setGroupId(groupId); - status.setSlbId(slbId); status.setGroupName(group.getName()); status.setActivated(onlineGroups.containsKey(groupId)); @@ -299,7 +229,7 @@ public class GroupStatusServiceImpl implements GroupStatusService { boolean raise = memberStatus.get(key).get(StatusOffset.HEALTHY); boolean up = false; NextStatus nextStatus = NextStatus.Online; - if (healthyActivateFlag) { + if (healthyOpsActivate.get()) { up = memberUp && serverUp && pullIn && raise; } else { if (memberUp && serverUp && pullIn) {