add sync group status

This commit is contained in:
Mengyi Zhou 2015-11-30 15:23:02 +08:00
parent d9e98a0298
commit 4f3a452f20
3 changed files with 38 additions and 31 deletions

View file

@ -30,6 +30,8 @@ import javax.ws.rs.core.Context;
import javax.ws.rs.core.HttpHeaders;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
@ -302,26 +304,28 @@ public class GroupResource {
}
@GET
@Path("/group/upgradeAll")
@Path("/group/syncStatus")
@Produces({MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML})
public Response upgradeAll(@Context HttpHeaders hh,
@Context HttpServletRequest request) throws Exception {
List<Long> groupIds = groupRepository.portGroupRel();
@Context HttpServletRequest request,
@QueryParam("groupId") Long groupId) throws Exception {
if (groupId != null) {
groupRepository.syncMemberStatus(groupRepository.getById(groupId));
return responseHandler.handle("Successfully synced group status.", hh.getMediaType());
}
List<Long> failedIds = new ArrayList<>();
Set<Long> groupIds = groupCriteriaQuery.queryAll();
for (Group group : groupRepository.list(groupIds.toArray(new Long[groupIds.size()]))) {
try {
groupRepository.syncMemberStatus(group);
} catch (Exception ex) {
failedIds.add(group.getId());
}
}
if (groupIds.size() == 0)
return responseHandler.handle("Successfully ported all group relations.", hh.getMediaType());
return responseHandler.handle("Successfully synced all group statuses.", hh.getMediaType());
else
return responseHandler.handle("Error occurs when porting group relations on id " + Joiner.on(',').join(groupIds) + ".", hh.getMediaType());
}
@GET
@Path("/group/upgrade")
@Produces({MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML})
public Response upgradeSingle(@Context HttpHeaders hh,
@Context HttpServletRequest request,
@QueryParam("groupId") Long groupId) throws Exception {
groupRepository.portGroupRel(groupId);
return responseHandler.handle("Successfully ported group relations.", hh.getMediaType());
return responseHandler.handle("Error occurs when syncing group statuses on id " + Joiner.on(',').join(groupIds) + ".", hh.getMediaType());
}

View file

@ -31,10 +31,7 @@ public interface GroupRepository extends Repository {
List<Group> listGroupsByGroupServer(String groupServerIp) throws Exception;
@Deprecated
List<Long> portGroupRel() throws Exception;
@Deprecated
void portGroupRel(Long groupId) throws Exception;
void syncMemberStatus(Group group) throws Exception;
@Deprecated
Group get(String groupName) throws Exception;

View file

@ -7,6 +7,7 @@ import com.ctrip.zeus.service.model.handler.GroupSync;
import com.ctrip.zeus.service.model.handler.GroupValidator;
import com.ctrip.zeus.service.model.handler.VGroupValidator;
import com.ctrip.zeus.service.query.GroupCriteriaQuery;
import com.ctrip.zeus.service.status.StatusService;
import org.springframework.stereotype.Repository;
import javax.annotation.Resource;
@ -32,6 +33,8 @@ public class GroupRepositoryImpl implements GroupRepository {
private GroupValidator groupModelValidator;
@Resource
private VGroupValidator vGroupValidator;
@Resource
private StatusService statusService;
@Override
public List<Group> list(Long[] ids) throws Exception {
@ -68,6 +71,7 @@ public class GroupRepositoryImpl implements GroupRepository {
autoFiller.autofill(group);
hideVirtualValue(group);
groupEntityManager.add(group, false);
syncMemberStatus(group);
return group;
}
@ -89,6 +93,7 @@ public class GroupRepositoryImpl implements GroupRepository {
autoFiller.autofill(group);
hideVirtualValue(group);
groupEntityManager.update(group);
syncMemberStatus(group);
return group;
}
@ -107,6 +112,7 @@ public class GroupRepositoryImpl implements GroupRepository {
@Override
public int delete(Long groupId) throws Exception {
groupModelValidator.removable(groupId);
statusService.cleanGroupServerStatus(groupId);
return groupEntityManager.delete(groupId);
}
@ -123,17 +129,16 @@ public class GroupRepositoryImpl implements GroupRepository {
}
@Override
public List<Long> portGroupRel() throws Exception {
Set<Long> groupIds = groupCriteriaQuery.queryAll();
List<Group> groups = list(groupIds.toArray(new Long[groupIds.size()]));
Group[] batch = groups.toArray(new Group[groups.size()]);
return groupEntityManager.port(batch);
}
@Override
public void portGroupRel(Long groupId) throws Exception {
Group group = getById(groupId);
groupEntityManager.port(group);
public void syncMemberStatus(Group group) throws Exception {
Long[] vsIds = new Long[group.getGroupVirtualServers().size()];
for (int i = 0; i < vsIds.length; i++) {
vsIds[i] = group.getGroupVirtualServers().get(i).getVirtualServer().getId();
}
String[] ips = new String[group.getGroupServers().size()];
for (int i = 0; i < ips.length; i++) {
ips[i] = group.getGroupServers().get(i).getIp();
}
statusService.groupServerStatusInit(group.getId(), vsIds, ips);
}
@Override
@ -150,4 +155,5 @@ public class GroupRepositoryImpl implements GroupRepository {
private void hideVirtualValue(Group group) {
group.setVirtual(null);
}
}