update query and sync modules

This commit is contained in:
Mengyi Zhou 2015-05-26 19:42:10 +08:00
parent 1e00043661
commit f08bf83890
13 changed files with 299 additions and 312 deletions

View file

@ -13,18 +13,18 @@ public interface ArchiveService {
int archiveSlb(Slb slb) throws Exception; int archiveSlb(Slb slb) throws Exception;
int archiveGroup(Group app) throws Exception; int archiveGroup(Group app) throws Exception;
int deleteSlbArchive(String slbName) throws Exception; int deleteSlbArchive(long slbId) throws Exception;
int deleteGroupArchive(String appName) throws Exception; int deleteGroupArchive(long groupId) throws Exception;
Slb getSlb(String name, int version) throws Exception; Slb getSlb(long slbId, int version) throws Exception;
Group getGroup(String name, int version) throws Exception; Group getGroup(long groupId, int version) throws Exception;
Slb getMaxVersionSlb(String name) throws Exception; Slb getMaxVersionSlb(long slbId) throws Exception;
Group getMaxVersionGroup(String name) throws Exception; Group getMaxVersionGroup(long groupId) throws Exception;
List<Slb> getAllSlb(String name) throws Exception; List<Slb> getAllSlb(long slbId) throws Exception;
List<Group> getAllGroup(String name) throws Exception; List<Group> getAllGroup(long groupId) throws Exception;
Archive getLatestGroupArchive(String appName) throws Exception; Archive getLatestSlbArchive(long slbId) throws Exception;
Archive getLatestSlbArchive(String slbName) throws Exception; Archive getLatestGroupArchive(long groupId) throws Exception;
} }

View file

@ -62,12 +62,12 @@ public interface SlbRepository extends Repository {
void update(Slb slb) throws Exception; void update(Slb slb) throws Exception;
/** /**
* delete the slb by its name * delete the slb by its primary id
* @param slbName the slb name * @param slbId the slb primary id
* @return the number of rows deleted * @return the number of rows deleted
* @throws Exception * @throws Exception
*/ */
int delete(String slbName) throws Exception; int delete(long slbId) throws Exception;
/** /**
* get the server list managed by the given slb * get the server list managed by the given slb

View file

@ -10,9 +10,9 @@ import org.unidal.dal.jdbc.DalException;
* @date: 3/7/2015. * @date: 3/7/2015.
*/ */
public interface GroupSync { public interface GroupSync {
GroupDo add(Group app) throws DalException, ValidationException; GroupDo add(Group group) throws DalException, ValidationException;
GroupDo update(Group app) throws DalException, ValidationException; GroupDo update(Group group) throws DalException, ValidationException;
int delete(String name) throws DalException; int delete(long groupId) throws DalException;
} }

View file

@ -17,5 +17,5 @@ public interface SlbSync {
SlbDo update(Slb slb) throws DalException, ValidationException; SlbDo update(Slb slb) throws DalException, ValidationException;
int delete(String slbName) throws DalException, ValidationException; int delete(long slbId) throws DalException, ValidationException;
} }

View file

@ -18,15 +18,15 @@ import java.util.List;
@Component("groupQuery") @Component("groupQuery")
public class GroupQueryImpl implements GroupQuery { public class GroupQueryImpl implements GroupQuery {
@Resource @Resource
private GroupDao appDao; private GroupDao groupDao;
@Resource @Resource
private GroupHealthCheckDao appHealthCheckDao; private GroupHealthCheckDao groupHealthCheckDao;
@Resource @Resource
private GroupLoadBalancingMethodDao appLoadBalancingMethodDao; private GroupLoadBalancingMethodDao groupLoadBalancingMethodDao;
@Resource @Resource
private GroupServerDao appServerDao; private GroupServerDao groupServerDao;
@Resource @Resource
private GroupSlbDao appSlbDao; private GroupSlbDao groupSlbDao;
@Resource @Resource
private SlbDao slbDao; private SlbDao slbDao;
@Resource @Resource
@ -39,31 +39,31 @@ public class GroupQueryImpl implements GroupQuery {
@Override @Override
public Group get(String name) throws DalException { public Group get(String name) throws DalException {
GroupDo d = appDao.findByName(name, GroupEntity.READSET_FULL); GroupDo d = groupDao.findByName(name, GroupEntity.READSET_FULL);
return createGroup(d); return createGroup(d);
} }
@Override @Override
public Group getById(long id) throws DalException { public Group getById(long id) throws DalException {
List<GroupDo> list = appDao.findAllByIds(new long[]{id}, GroupEntity.READSET_FULL); List<GroupDo> list = groupDao.findAllByIds(new long[]{id}, GroupEntity.READSET_FULL);
if (list.size() == 0) if (list.size() == 0)
return null; return null;
return createGroup(list.get(0)); return createGroup(list.get(0));
} }
@Override @Override
public Group getByAppId(String appId) throws DalException { public Group getByAppId(String groupId) throws DalException {
GroupDo d = appDao.findByAppId(appId, GroupEntity.READSET_FULL); GroupDo d = groupDao.findByAppId(groupId, GroupEntity.READSET_FULL);
return createGroup(d); return createGroup(d);
} }
@Override @Override
public List<Group> getAll() throws DalException { public List<Group> getAll() throws DalException {
List<Group> list = new ArrayList<>(); List<Group> list = new ArrayList<>();
for (GroupDo d : appDao.findAll(GroupEntity.READSET_FULL)) { for (GroupDo d : groupDao.findAll(GroupEntity.READSET_FULL)) {
Group app = createGroup(d); Group group = createGroup(d);
if (app != null) if (group != null)
list.add(app); list.add(group);
} }
return list; return list;
} }
@ -71,107 +71,107 @@ public class GroupQueryImpl implements GroupQuery {
@Override @Override
public List<Group> getLimit(long fromId, int maxCount) throws DalException { public List<Group> getLimit(long fromId, int maxCount) throws DalException {
List<Group> list = new ArrayList<>(); List<Group> list = new ArrayList<>();
for (GroupDo d : appDao.findLimit(fromId, maxCount, GroupEntity.READSET_FULL)) { for (GroupDo d : groupDao.findLimit(fromId, maxCount, GroupEntity.READSET_FULL)) {
Group app = createGroup(d); Group group = createGroup(d);
if (app != null) if (group != null)
list.add(app); list.add(group);
} }
return list; return list;
} }
@Override @Override
public List<Group> getBySlbAndVirtualServer(String slbName, String virtualServerName) throws DalException { public List<Group> getBySlbAndVirtualServer(String slbName, String virtualServerName) throws DalException {
List<GroupSlbDo> l = appSlbDao.findAllBySlbAndVirtualServer(slbName, virtualServerName, GroupSlbEntity.READSET_FULL); long slbId = slbDao.findByName(slbName, SlbEntity.READSET_FULL).getId();
long vsId = slbVirtualServerDao.findBySlbAndName(slbId, virtualServerName, SlbVirtualServerEntity.READSET_FULL).getId();
List<GroupSlbDo> l = groupSlbDao.findAllBySlbAndVirtualServer(slbId, vsId, GroupSlbEntity.READSET_FULL);
int size = l.size(); int size = l.size();
String[] names = new String[size]; long[] names = new long[size];
for (int i = 0; i < size; i++) { for (int i = 0; i < size; i++) {
names[i] = l.get(i).getGroupName(); names[i] = l.get(i).getId();
} }
List<Group> list = new ArrayList<>(); List<Group> list = new ArrayList<>();
for (GroupDo d : appDao.findAllByNames(names, GroupEntity.READSET_FULL)) { for (GroupDo d : groupDao.findAllByIds(names, GroupEntity.READSET_FULL)) {
list.add(createGroup(d)); list.add(createGroup(d));
} }
return list; return list;
} }
@Override @Override
public List<String> getByGroupServer(String appServerIp) throws DalException { public List<String> getByGroupServer(String groupServerIp) throws DalException {
List<String> appNames = new ArrayList<>(); List<String> groupNames = new ArrayList<>();
for (GroupServerDo asd : appServerDao.findAllByIp(appServerIp, GroupServerEntity.READSET_FULL)) { for (GroupServerDo asd : groupServerDao.findAllByIp(groupServerIp, GroupServerEntity.READSET_FULL)) {
GroupDo d = appDao.findByPK(asd.getGroupId(), GroupEntity.READSET_FULL); GroupDo d = groupDao.findByPK(asd.getGroupId(), GroupEntity.READSET_FULL);
appNames.add(d.getName()); groupNames.add(d.getName());
} }
return appNames; return groupNames;
} }
@Override @Override
public List<String> getGroupServersByGroup(String appName) throws DalException { public List<String> getGroupServersByGroup(String groupName) throws DalException {
GroupDo d = appDao.findByName(appName, GroupEntity.READSET_FULL); GroupDo d = groupDao.findByName(groupName, GroupEntity.READSET_FULL);
if (d == null) if (d == null)
return null; return null;
List<String> appServers = new ArrayList<>(); List<String> groupServers = new ArrayList<>();
for (GroupServerDo asd : appServerDao.findAllByGroup(d.getId(), GroupServerEntity.READSET_FULL)) { for (GroupServerDo asd : groupServerDao.findAllByGroup(d.getId(), GroupServerEntity.READSET_FULL)) {
appServers.add(asd.getIp()); groupServers.add(asd.getIp());
} }
return appServers; return groupServers;
} }
@Override @Override
public List<GroupServer> listGroupServersByGroup(String appName) throws DalException { public List<GroupServer> listGroupServersByGroup(String groupName) throws DalException {
GroupDo d = appDao.findByName(appName, GroupEntity.READSET_FULL); GroupDo d = groupDao.findByName(groupName, GroupEntity.READSET_FULL);
if (d == null) if (d == null)
return null; return null;
List<GroupServer> appServers = new ArrayList<>(); List<GroupServer> groupServers = new ArrayList<>();
for (GroupServerDo asd : appServerDao.findAllByGroup(d.getId(), GroupServerEntity.READSET_FULL)) { for (GroupServerDo asd : groupServerDao.findAllByGroup(d.getId(), GroupServerEntity.READSET_FULL)) {
appServers.add(C.toGroupServer(asd)); groupServers.add(C.toGroupServer(asd));
} }
return appServers; return groupServers;
} }
private Group createGroup(GroupDo d) throws DalException { private Group createGroup(GroupDo d) throws DalException {
if (d == null) if (d == null)
return null; return null;
Group app = C.toGroup(d); Group group = C.toGroup(d);
cascadeQuery(d, app); cascadeQuery(d, group);
return app; return group;
} }
private void cascadeQuery(GroupDo d, Group app) throws DalException { private void cascadeQuery(GroupDo d, Group group) throws DalException {
queryGroupSlbs(d.getName(), app); queryGroupSlbs(d.getName(), group);
queryGroupHealthCheck(d.getId(), app); queryGroupHealthCheck(d.getId(), group);
queryLoadBalancingMethod(d.getId(), app); queryLoadBalancingMethod(d.getId(), group);
queryGroupServers(d.getId(), app); queryGroupServers(d.getId(), group);
} }
private void queryGroupSlbs(String appName, Group app) throws DalException { private void queryGroupSlbs(String groupName, Group group) throws DalException {
List<GroupSlbDo> list = appSlbDao.findAllByGroup(appName, GroupSlbEntity.READSET_FULL); long groupId = groupDao.findByName(groupName, GroupEntity.READSET_FULL).getId();
List<GroupSlbDo> list = groupSlbDao.findAllByGroup(groupId, GroupSlbEntity.READSET_FULL);
for (GroupSlbDo d : list) { for (GroupSlbDo d : list) {
GroupSlb e = C.toGroupSlb(d); GroupSlb e = C.toGroupSlb(d);
app.addGroupSlb(e); group.addGroupSlb(e);
querySlbVips(d.getSlbName(), e); e.setSlbName(slbDao.findById(d.getSlbId(), SlbEntity.READSET_FULL).getName());
queryVirtualServer(d.getSlbName(), d.getSlbVirtualServerName(), e); querySlbVips(d.getSlbId(), e);
queryVirtualServer(d.getSlbVirtualServerId(), e);
} }
} }
private void querySlbVips(String slbName, GroupSlb appSlb) throws DalException { private void querySlbVips(long slbId, GroupSlb groupSlb) throws DalException {
SlbDo sd = slbDao.findByName(slbName, SlbEntity.READSET_FULL); List<SlbVipDo> list = slbVipDao.findAllBySlb(slbId, SlbVipEntity.READSET_FULL);
List<SlbVipDo> list = slbVipDao.findAllBySlb(sd.getId(), SlbVipEntity.READSET_FULL);
for (SlbVipDo d : list) { for (SlbVipDo d : list) {
Vip e = C.toVip(d); Vip e = C.toVip(d);
appSlb.addVip(e); groupSlb.addVip(e);
} }
} }
private void queryVirtualServer(String slbName, String slbVirtualServerName, GroupSlb appSlb) throws DalException { private void queryVirtualServer(long slbVirtualServerId, GroupSlb groupSlb) throws DalException {
SlbDo slbDo = slbDao.findByName(slbName, SlbEntity.READSET_FULL); SlbVirtualServerDo d = slbVirtualServerDao.findByPK(slbVirtualServerId, SlbVirtualServerEntity.READSET_FULL);
SlbVirtualServerDo d = slbVirtualServerDao.findAllBySlbAndName(slbDo.getId(), slbVirtualServerName, SlbVirtualServerEntity.READSET_FULL);
appSlb.setSlbName(slbDo.getName());
if (d == null) if (d == null)
return; return;
VirtualServer e = C.toVirtualServer(d); VirtualServer e = C.toVirtualServer(d);
appSlb.setVirtualServer(e); groupSlb.setVirtualServer(e);
querySlbDomains(d.getId(), e); querySlbDomains(d.getId(), e);
} }
@ -183,27 +183,27 @@ public class GroupQueryImpl implements GroupQuery {
} }
} }
private void queryGroupHealthCheck(long appKey, Group app) throws DalException { private void queryGroupHealthCheck(long groupKey, Group group) throws DalException {
GroupHealthCheckDo d = appHealthCheckDao.findByGroup(appKey, GroupHealthCheckEntity.READSET_FULL); GroupHealthCheckDo d = groupHealthCheckDao.findByGroup(groupKey, GroupHealthCheckEntity.READSET_FULL);
if (d == null) if (d == null)
return; return;
HealthCheck e = C.toHealthCheck(d); HealthCheck e = C.toHealthCheck(d);
app.setHealthCheck(e); group.setHealthCheck(e);
} }
private void queryLoadBalancingMethod(long appKey, Group app) throws DalException { private void queryLoadBalancingMethod(long groupKey, Group group) throws DalException {
GroupLoadBalancingMethodDo d = appLoadBalancingMethodDao.findByGroup(appKey, GroupLoadBalancingMethodEntity.READSET_FULL); GroupLoadBalancingMethodDo d = groupLoadBalancingMethodDao.findByGroup(groupKey, GroupLoadBalancingMethodEntity.READSET_FULL);
if (d == null) if (d == null)
return; return;
LoadBalancingMethod e = C.toLoadBalancingMethod(d); LoadBalancingMethod e = C.toLoadBalancingMethod(d);
app.setLoadBalancingMethod(e); group.setLoadBalancingMethod(e);
} }
private void queryGroupServers(long appKey, Group app) throws DalException { private void queryGroupServers(long groupKey, Group group) throws DalException {
List<GroupServerDo> list = appServerDao.findAllByGroup(appKey, GroupServerEntity.READSET_FULL); List<GroupServerDo> list = groupServerDao.findAllByGroup(groupKey, GroupServerEntity.READSET_FULL);
for (GroupServerDo d : list) { for (GroupServerDo d : list) {
GroupServer e = C.toGroupServer(d); GroupServer e = C.toGroupServer(d);
app.addGroupServer(e); group.addGroupServer(e);
} }
} }
} }

View file

@ -24,138 +24,137 @@ import java.util.Map;
@Component("groupSync") @Component("groupSync")
public class GroupSyncImpl implements GroupSync { public class GroupSyncImpl implements GroupSync {
@Resource @Resource
private GroupDao appDao; private GroupDao groupDao;
@Resource @Resource
private GroupHealthCheckDao appHealthCheckDao; private GroupHealthCheckDao groupHealthCheckDao;
@Resource @Resource
private GroupLoadBalancingMethodDao appLoadBalancingMethodDao; private GroupLoadBalancingMethodDao groupLoadBalancingMethodDao;
@Resource @Resource
private GroupServerDao appServerDao; private GroupServerDao groupServerDao;
@Resource @Resource
private GroupSlbDao appSlbDao; private GroupSlbDao groupSlbDao;
@Resource @Resource
private SlbDao slbDao; private SlbDao slbDao;
@Resource
private SlbVirtualServerDao slbVirtualServerDao;
Logger logger = LoggerFactory.getLogger(this.getClass()); Logger logger = LoggerFactory.getLogger(this.getClass());
@Override @Override
public GroupDo add(Group app) throws DalException, ValidationException { public GroupDo add(Group group) throws DalException, ValidationException {
validate(app); validate(group);
GroupDo d= C.toGroupDo(app); GroupDo d= C.toGroupDo(group);
d.setCreatedTime(new Date()); d.setCreatedTime(new Date());
d.setVersion(1); d.setVersion(1);
appDao.insert(d); groupDao.insert(d);
cascadeSync(d, app); cascadeSync(d, group);
return d; return d;
} }
@Override @Override
public GroupDo update(Group app) throws DalException, ValidationException { public GroupDo update(Group group) throws DalException, ValidationException {
validate(app); validate(group);
GroupDo check = appDao.findByName(app.getName(), GroupEntity.READSET_FULL); GroupDo check = groupDao.findByName(group.getName(), GroupEntity.READSET_FULL);
if (check.getVersion() > app.getVersion()) if (check.getVersion() > group.getVersion())
throw new ValidationException("Newer Group version is detected."); throw new ValidationException("Newer Group version is detected.");
GroupDo d= C.toGroupDo(app); GroupDo d= C.toGroupDo(group).setId(group.getId());
appDao.updateByName(d, GroupEntity.UPDATESET_FULL); groupDao.updateById(d, GroupEntity.UPDATESET_FULL);
GroupDo updated = appDao.findByName(app.getName(), GroupEntity.READSET_FULL); GroupDo updated = groupDao.findByName(group.getName(), GroupEntity.READSET_FULL);
d.setId(updated.getId());
d.setVersion(updated.getVersion()); d.setVersion(updated.getVersion());
cascadeSync(d, app); cascadeSync(d, group);
return d; return d;
} }
@Override @Override
public int delete(String name) throws DalException { public int delete(long groupId) throws DalException {
GroupDo d = appDao.findByName(name, GroupEntity.READSET_FULL); groupSlbDao.deleteByGroup(new GroupSlbDo().setGroupId(groupId));
if (d == null) groupServerDao.deleteByGroup(new GroupServerDo().setGroupId(groupId));
return 0; groupHealthCheckDao.deleteByGroup(new GroupHealthCheckDo().setGroupId(groupId));
appSlbDao.deleteByGroup(new GroupSlbDo().setGroupName(d.getName())); groupLoadBalancingMethodDao.deleteByGroup(new GroupLoadBalancingMethodDo().setGroupId(groupId));
appServerDao.deleteByGroup(new GroupServerDo().setGroupId(d.getId())); return groupDao.deleteById(new GroupDo().setId(groupId));
appHealthCheckDao.deleteByGroup(new GroupHealthCheckDo().setGroupId(d.getId()));
appLoadBalancingMethodDao.deleteByGroup(new GroupLoadBalancingMethodDo().setGroupId(d.getId()));
return appDao.deleteByName(d);
} }
private void validate(Group app) throws DalException, ValidationException { private void validate(Group group) throws DalException, ValidationException {
if (app == null) { if (group == null) {
throw new ValidationException("Group with null value cannot be persisted."); throw new ValidationException("Group with null value cannot be persisted.");
} }
if (!validateSlb(app)) if (!validateSlb(group))
throw new ValidationException("Group with invalid slb data cannot be persisted."); throw new ValidationException("Group with invalid slb data cannot be persisted.");
} }
private boolean validateSlb(Group app) throws DalException { private boolean validateSlb(Group group) throws DalException {
if (app.getGroupSlbs().size() == 0) if (group.getGroupSlbs().size() == 0)
return false; return false;
for (GroupSlb as : app.getGroupSlbs()) { for (GroupSlb as : group.getGroupSlbs()) {
if (slbDao.findByName(as.getSlbName(), SlbEntity.READSET_FULL) == null) if (slbDao.findByName(as.getSlbName(), SlbEntity.READSET_FULL) == null)
return false; return false;
} }
return true; return true;
} }
private void cascadeSync(GroupDo d, Group app) throws DalException { private void cascadeSync(GroupDo d, Group group) throws DalException {
syncGroupSlbs(app.getName(), app.getGroupSlbs()); syncGroupSlbs(group.getId(), group.getGroupSlbs());
syncGroupHealthCheck(d.getId(), app.getHealthCheck()); syncGroupHealthCheck(d.getId(), group.getHealthCheck());
syncLoadBalancingMethod(d.getId(), app.getLoadBalancingMethod()); syncLoadBalancingMethod(d.getId(), group.getLoadBalancingMethod());
syncGroupServers(d.getId(), app.getGroupServers()); syncGroupServers(d.getId(), group.getGroupServers());
} }
private void syncGroupSlbs(String appName, List<GroupSlb> appSlbs) throws DalException { private void syncGroupSlbs(long groupId, List<GroupSlb> groupSlbs) throws DalException {
List<GroupSlbDo> oldList = appSlbDao.findAllByGroup(appName, GroupSlbEntity.READSET_FULL); List<GroupSlbDo> oldList = groupSlbDao.findAllByGroup(groupId, GroupSlbEntity.READSET_FULL);
Map<String, GroupSlbDo> oldMap = Maps.uniqueIndex(oldList, new Function<GroupSlbDo, String>() { Map<String, GroupSlbDo> oldMap = Maps.uniqueIndex(oldList, new Function<GroupSlbDo, String>() {
@Override @Override
public String apply(GroupSlbDo input) { public String apply(GroupSlbDo input) {
return input.getGroupName() + input.getSlbName() + input.getSlbVirtualServerName(); return input.getGroupId() + "" + input.getSlbVirtualServerId();
} }
}); });
//Update existed if necessary, and insert new ones. //Update existed if necessary, and insert new ones.
for (GroupSlb e : appSlbs) { for (GroupSlb e : groupSlbs) {
GroupSlbDo old = oldMap.get(appName + e.getSlbName() + e.getVirtualServer().getName()); long slbId = slbDao.findByName(e.getSlbName(), SlbEntity.READSET_FULL).getId();
long vsId = slbVirtualServerDao.findBySlbAndName(slbId, e.getVirtualServer().getName(), SlbVirtualServerEntity.READSET_FULL).getId();
GroupSlbDo old = oldMap.get(groupId + "" + vsId);
if (old != null) { if (old != null) {
oldList.remove(old); oldList.remove(old);
} }
appSlbDao.insert(C.toGroupSlbDo(e) groupSlbDao.insert(C.toGroupSlbDo(e)
.setGroupName(appName) .setGroupId(groupId)
.setCreatedTime(new Date())); .setCreatedTime(new Date()));
} }
//Remove unused ones. //Remove unused ones.
for (GroupSlbDo d : oldList) { for (GroupSlbDo d : oldList) {
appSlbDao.deleteByPK(new GroupSlbDo().setId(d.getId())); groupSlbDao.deleteByPK(new GroupSlbDo().setId(d.getId()));
} }
} }
private void syncGroupHealthCheck(long appKey, HealthCheck healthCheck) throws DalException { private void syncGroupHealthCheck(long groupKey, HealthCheck healthCheck) throws DalException {
if (healthCheck == null) { if (healthCheck == null) {
logger.info("No health check method is found when adding/updating app with id " + appKey); logger.info("No health check method is found when adding/updating group with id " + groupKey);
return; return;
} }
appHealthCheckDao.insert(C.toGroupHealthCheckDo(healthCheck) groupHealthCheckDao.insert(C.toGroupHealthCheckDo(healthCheck)
.setGroupId(appKey) .setGroupId(groupKey)
.setCreatedTime(new Date())); .setCreatedTime(new Date()));
} }
private void syncLoadBalancingMethod(long appKey, LoadBalancingMethod loadBalancingMethod) throws DalException { private void syncLoadBalancingMethod(long groupKey, LoadBalancingMethod loadBalancingMethod) throws DalException {
if (loadBalancingMethod == null) if (loadBalancingMethod == null)
return; return;
appLoadBalancingMethodDao.insert(C.toGroupLoadBalancingMethodDo(loadBalancingMethod) groupLoadBalancingMethodDao.insert(C.toGroupLoadBalancingMethodDo(loadBalancingMethod)
.setGroupId(appKey) .setGroupId(groupKey)
.setCreatedTime(new Date())); .setCreatedTime(new Date()));
} }
private void syncGroupServers(long appKey, List<GroupServer> appServers) throws DalException { private void syncGroupServers(long groupKey, List<GroupServer> groupServers) throws DalException {
if (appServers == null || appServers.size() == 0) { if (groupServers == null || groupServers.size() == 0) {
logger.warn("No app server is given when adding/update app with id " + appKey); logger.warn("No group server is given when adding/update group with id " + groupKey);
return; return;
} }
List<GroupServerDo> oldList = appServerDao.findAllByGroup(appKey, GroupServerEntity.READSET_FULL); List<GroupServerDo> oldList = groupServerDao.findAllByGroup(groupKey, GroupServerEntity.READSET_FULL);
Map<String, GroupServerDo> oldMap = Maps.uniqueIndex(oldList, new Function<GroupServerDo, String>() { Map<String, GroupServerDo> oldMap = Maps.uniqueIndex(oldList, new Function<GroupServerDo, String>() {
@Override @Override
public String apply(GroupServerDo input) { public String apply(GroupServerDo input) {
@ -164,19 +163,19 @@ public class GroupSyncImpl implements GroupSync {
}); });
//Update existed if necessary, and insert new ones. //Update existed if necessary, and insert new ones.
for (GroupServer e : appServers) { for (GroupServer e : groupServers) {
GroupServerDo old = oldMap.get(appKey + e.getIp()); GroupServerDo old = oldMap.get(groupKey + e.getIp());
if (old != null) { if (old != null) {
oldList.remove(old); oldList.remove(old);
} }
appServerDao.insert(C.toGroupServerDo(e) groupServerDao.insert(C.toGroupServerDo(e)
.setGroupId(appKey) .setGroupId(groupKey)
.setCreatedTime(new Date())); .setCreatedTime(new Date()));
} }
//Remove unused ones. //Remove unused ones.
for (GroupServerDo d : oldList) { for (GroupServerDo d : oldList) {
appServerDao.deleteByPK(new GroupServerDo().setId(d.getId())); groupServerDao.deleteByPK(new GroupServerDo().setId(d.getId()));
} }
} }
} }

View file

@ -67,64 +67,31 @@ public class SlbQueryImpl implements SlbQuery {
@Override @Override
public List<Slb> getByGroupServer(String groupServerIp) throws DalException { public List<Slb> getByGroupServer(String groupServerIp) throws DalException {
List<GroupServerDo> asvrDoList = groupServerDao.findAllByIp(groupServerIp, GroupServerEntity.READSET_FULL); List<GroupServerDo> gslist = groupServerDao.findAllByIp(groupServerIp, GroupServerEntity.READSET_FULL);
if (asvrDoList.size() == 0) if (gslist.size() == 0)
return null; return null;
long[] groupIds = new long[asvrDoList.size()]; long[] groupIds = new long[gslist.size()];
int i = 0; int i = 0;
for (GroupServerDo asd : asvrDoList) { for (GroupServerDo gsd : gslist) {
groupIds[i++] = asd.getGroupId(); groupIds[i++] = gsd.getGroupId();
} }
List<GroupDo> adList = groupDao.findAllByIds(groupIds, GroupEntity.READSET_FULL); List<GroupSlbDo> list = groupSlbDao.findAllByGroups(groupIds, GroupSlbEntity.READSET_FULL);
if (adList.size() == 0) return getAllByGroupSlbs(list);
return null;
String[] groupNames = new String[adList.size()];
int j = 0;
for (GroupDo ad : adList) {
groupNames[j++] = ad.getName();
}
List<GroupSlbDo> aslbDoList = groupSlbDao.findAllByGroups(groupNames, GroupSlbEntity.READSET_FULL);
if (aslbDoList.size() == 0)
return null;
List<String> slbNames = new ArrayList<>();
for (GroupSlbDo asd : aslbDoList) {
if (slbNames.contains(asd.getSlbName()))
continue;
slbNames.add(asd.getSlbName());
}
List<Slb> list = new ArrayList<>();
for (String sn : slbNames) {
Slb slb = get(sn);
if (slb == null)
continue;
list.add(slb);
}
return list;
} }
@Override @Override
public List<Slb> getByGroupNames(String[] groupNames) throws DalException { public List<Slb> getByGroupNames(String[] groupNames) throws DalException {
List<GroupSlbDo> asdList = groupSlbDao.findAllByGroups(groupNames, GroupSlbEntity.READSET_FULL); List<GroupDo> glist = groupDao.findAllByNames(groupNames, GroupEntity.READSET_FULL);
if (asdList.size() == 0) if (glist.size() == 0)
return null; return null;
long[] groupIds = new long[glist.size()];
List<String> slbNames = new ArrayList<>(); int i = 0;
for (GroupSlbDo asd : asdList) { for (GroupDo gd : glist) {
if (slbNames.contains(asd.getSlbName())) groupIds[i++] = gd.getId();
continue;
slbNames.add(asd.getSlbName());
} }
List<Slb> list = new ArrayList<>(); List<GroupSlbDo> list = groupSlbDao.findAllByGroups(groupIds, GroupSlbEntity.READSET_FULL);
for (String sn : slbNames) { return getAllByGroupSlbs(list);
Slb slb = get(sn);
if (slb == null)
continue;
list.add(slb);
}
return list;
} }
@Override @Override
@ -149,22 +116,14 @@ public class SlbQueryImpl implements SlbQuery {
@Override @Override
public List<String> getGroupServersBySlb(String slbName) throws DalException { public List<String> getGroupServersBySlb(String slbName) throws DalException {
List<GroupSlbDo> aslbDoList = groupSlbDao.findAllBySlb(slbName, GroupSlbEntity.READSET_FULL); long slbId = slbDao.findByName(slbName, SlbEntity.READSET_FULL).getId();
if (aslbDoList.size() == 0) List<GroupSlbDo> gslbDoList = groupSlbDao.findAllBySlb(slbId, GroupSlbEntity.READSET_FULL);
return null; if (gslbDoList.size() == 0)
String[] groupNames = new String[aslbDoList.size()];
int i = 0;
for (GroupSlbDo asd : aslbDoList) {
groupNames[i++] = asd.getGroupName();
}
List<GroupDo> adList = groupDao.findAllByNames(groupNames, GroupEntity.READSET_FULL);
if (adList.size() == 0)
return null; return null;
List<GroupServerDo> asvrDoList = new ArrayList<>(); List<GroupServerDo> asvrDoList = new ArrayList<>();
for (GroupDo ad : adList) { for (GroupSlbDo gsd : gslbDoList) {
asvrDoList.addAll(groupServerDao.findAllByGroup(ad.getId(), GroupServerEntity.READSET_FULL)); asvrDoList.addAll(groupServerDao.findAllByGroup(gsd.getGroupId(), GroupServerEntity.READSET_FULL));
} }
if (asvrDoList.size() == 0) if (asvrDoList.size() == 0)
return null; return null;
@ -181,14 +140,18 @@ public class SlbQueryImpl implements SlbQuery {
@Override @Override
public List<GroupSlb> getGroupSlbsByGroups(String[] groupNames) throws DalException { public List<GroupSlb> getGroupSlbsByGroups(String[] groupNames) throws DalException {
List<GroupSlb> list = new ArrayList<>(); List<GroupSlb> list = new ArrayList<>();
for (GroupSlbDo asd : groupSlbDao.findAllByGroups(groupNames, GroupSlbEntity.READSET_FULL)) { List<GroupDo> glist = groupDao.findAllByNames(groupNames, GroupEntity.READSET_FULL);
long[] groupIds = new long[glist.size()];
for (int i = 0; i < groupNames.length; i++) {
groupIds[i] = glist.get(i).getId();
}
for (GroupSlbDo asd : groupSlbDao.findAllByGroups(groupIds, GroupSlbEntity.READSET_FULL)) {
GroupSlb as = C.toGroupSlb(asd); GroupSlb as = C.toGroupSlb(asd);
list.add(as); list.add(as);
SlbDo sd = slbDao.findByName(as.getSlbName(), SlbEntity.READSET_FULL); SlbVirtualServerDo svsd = slbVirtualServerDao.findByPK(asd.getSlbVirtualServerId(), SlbVirtualServerEntity.READSET_FULL);
SlbVirtualServerDo svsd = slbVirtualServerDao.findAllBySlbAndName(sd.getId(), asd.getSlbVirtualServerName(), SlbVirtualServerEntity.READSET_FULL);
if (svsd != null) if (svsd != null)
as.setVirtualServer(C.toVirtualServer(svsd)); as.setVirtualServer(C.toVirtualServer(svsd));
querySlbVips(sd.getId(), as); querySlbVips(svsd.getSlbId(), as);
} }
return list; return list;
} }
@ -196,18 +159,31 @@ public class SlbQueryImpl implements SlbQuery {
@Override @Override
public List<GroupSlb> getGroupSlbsBySlb(String slbName) throws DalException { public List<GroupSlb> getGroupSlbsBySlb(String slbName) throws DalException {
List<GroupSlb> list = new ArrayList<>(); List<GroupSlb> list = new ArrayList<>();
for (GroupSlbDo asd : groupSlbDao.findAllBySlb(slbName, GroupSlbEntity.READSET_FULL)) { long slbId = slbDao.findByName(slbName, SlbEntity.READSET_FULL).getId();
for (GroupSlbDo asd : groupSlbDao.findAllBySlb(slbId, GroupSlbEntity.READSET_FULL)) {
GroupSlb as = C.toGroupSlb(asd); GroupSlb as = C.toGroupSlb(asd);
list.add(as); list.add(as);
SlbDo sd = slbDao.findByName(slbName, SlbEntity.READSET_FULL); SlbVirtualServerDo svsd = slbVirtualServerDao.findByPK(asd.getSlbVirtualServerId(), SlbVirtualServerEntity.READSET_FULL);
SlbVirtualServerDo svsd = slbVirtualServerDao.findAllBySlbAndName(sd.getId(), asd.getSlbVirtualServerName(), SlbVirtualServerEntity.READSET_FULL);
if (svsd != null) if (svsd != null)
as.setVirtualServer(C.toVirtualServer(svsd)); as.setVirtualServer(C.toVirtualServer(svsd));
querySlbVips(sd.getId(), as); querySlbVips(svsd.getSlbId(), as);
} }
return list; return list;
} }
private List<Slb> getAllByGroupSlbs(List<GroupSlbDo> list) throws DalException {
if (list.size() == 0)
return null;
List<Slb> l = new ArrayList<>();
for (GroupSlbDo d : list) {
Slb slb = getById(d.getSlbId());
if (slb == null)
continue;
l.add(slb);
}
return l;
}
private Slb createSlb(SlbDo d) throws DalException { private Slb createSlb(SlbDo d) throws DalException {
if (d == null) if (d == null)
return null; return null;

View file

@ -56,8 +56,8 @@ public class SlbSyncImpl implements SlbSync {
SlbDo check = slbDao.findByName(slb.getName(), SlbEntity.READSET_FULL); SlbDo check = slbDao.findByName(slb.getName(), SlbEntity.READSET_FULL);
if (check.getVersion() > slb.getVersion()) if (check.getVersion() > slb.getVersion())
throw new ValidationException("Newer Slb version is detected."); throw new ValidationException("Newer Slb version is detected.");
SlbDo d = C.toSlbDo(slb); SlbDo d = C.toSlbDo(slb).setId(slb.getId());
slbDao.updateByName(d, SlbEntity.UPDATESET_FULL); slbDao.updateById(d, SlbEntity.UPDATESET_FULL);
SlbDo updated = slbDao.findByName(d.getName(), SlbEntity.READSET_FULL); SlbDo updated = slbDao.findByName(d.getName(), SlbEntity.READSET_FULL);
d.setId(updated.getId()); d.setId(updated.getId());
@ -67,8 +67,8 @@ public class SlbSyncImpl implements SlbSync {
} }
@Override @Override
public int delete(String slbName) throws DalException, ValidationException { public int delete(long slbId) throws DalException, ValidationException {
SlbDo d = slbDao.findByName(slbName, SlbEntity.READSET_FULL); SlbDo d = slbDao.findById(slbId, SlbEntity.READSET_FULL);
if (d == null) if (d == null)
return 0; return 0;
if(removable(d)) { if(removable(d)) {
@ -79,7 +79,7 @@ public class SlbSyncImpl implements SlbSync {
} }
return slbDao.deleteByPK(d); return slbDao.deleteByPK(d);
} }
throw new ValidationException(slbName + " cannot be deleted. Dependency exists"); throw new ValidationException(d.getName() + " cannot be deleted. Dependency exists");
} }
private void validate(Slb slb) throws ValidationException { private void validate(Slb slb) throws ValidationException {
@ -92,7 +92,7 @@ public class SlbSyncImpl implements SlbSync {
} }
private boolean removable(SlbDo d) throws DalException { private boolean removable(SlbDo d) throws DalException {
List<GroupSlbDo> list = appSlbDao.findAllBySlb(d.getName(), GroupSlbEntity.READSET_FULL); List<GroupSlbDo> list = appSlbDao.findAllBySlb(d.getId(), GroupSlbEntity.READSET_FULL);
if (list.size() == 0) if (list.size() == 0)
return true; return true;
return false; return false;

View file

@ -26,65 +26,64 @@ public class ArchiveServiceImpl implements ArchiveService {
@Resource @Resource
private ArchiveSlbDao archiveSlbDao; private ArchiveSlbDao archiveSlbDao;
@Resource @Resource
private ArchiveGroupDao archiveGroupDao; private ArchiveGroupDao archiveGroupDao;
@Override @Override
public int archiveSlb(Slb slb) throws Exception { public int archiveSlb(Slb slb) throws Exception {
String content = String.format(Slb.XML, slb); String content = String.format(Slb.XML, slb);
ArchiveSlbDo d = new ArchiveSlbDo().setName(slb.getName()).setContent(content).setVersion(slb.getVersion()).setCreatedTime(new Date()).setDataChangeLastTime(new Date()); ArchiveSlbDo d = new ArchiveSlbDo().setSlbId(slb.getId()).setContent(content).setVersion(slb.getVersion()).setCreatedTime(new Date()).setDataChangeLastTime(new Date());
archiveSlbDao.insert(d); archiveSlbDao.insert(d);
return d.getVersion(); return d.getVersion();
} }
@Override @Override
public int archiveGroup(Group app) throws Exception { public int archiveGroup(Group group) throws Exception {
String content = String.format(Group.XML, app); String content = String.format(Group.XML, group);
ArchiveGroupDo d = new ArchiveGroupDo().setName(app.getName()).setContent(content).setVersion(app.getVersion()).setCreatedTime(new Date()).setDataChangeLastTime(new Date()); ArchiveGroupDo d = new ArchiveGroupDo().setGroupId(group.getId()).setContent(content).setVersion(group.getVersion()).setCreatedTime(new Date()).setDataChangeLastTime(new Date());
archiveGroupDao.insert(d); archiveGroupDao.insert(d);
return d.getVersion(); return d.getVersion();
} }
@Override @Override
public int deleteSlbArchive(String slbName) throws Exception { public int deleteSlbArchive(long slbId) throws Exception {
ArchiveSlbDo d = new ArchiveSlbDo().setName(slbName); ArchiveSlbDo d = new ArchiveSlbDo().setSlbId(slbId);
return archiveSlbDao.deleteBySlb(d); return archiveSlbDao.deleteBySlb(d);
} }
@Override @Override
public int deleteGroupArchive(String appName) throws Exception { public int deleteGroupArchive(long groupId) throws Exception {
ArchiveGroupDo d = new ArchiveGroupDo().setName(appName); ArchiveGroupDo d = new ArchiveGroupDo().setGroupId(groupId);
return archiveGroupDao.deleteByGroup(d); return archiveGroupDao.deleteByGroup(d);
} }
@Override @Override
public Slb getSlb(String name, int version) throws Exception { public Slb getSlb(long slbId, int version) throws Exception {
String content = archiveSlbDao.findByNameAndVersion(name, version, ArchiveSlbEntity.READSET_FULL).getContent(); String content = archiveSlbDao.findBySlbAndVersion(slbId, version, ArchiveSlbEntity.READSET_FULL).getContent();
return DefaultSaxParser.parseEntity(Slb.class, content); return DefaultSaxParser.parseEntity(Slb.class, content);
} }
@Override @Override
public Group getGroup(String name, int version) throws Exception { public Group getGroup(long groupId, int version) throws Exception {
String content = archiveGroupDao.findByNameAndVersion(name, version, ArchiveGroupEntity.READSET_FULL).getContent(); String content = archiveGroupDao.findByGroupAndVersion(groupId, version, ArchiveGroupEntity.READSET_FULL).getContent();
return DefaultSaxParser.parseEntity(Group.class, content); return DefaultSaxParser.parseEntity(Group.class, content);
} }
@Override @Override
public Slb getMaxVersionSlb(String name) throws Exception { public Slb getMaxVersionSlb(long slbId) throws Exception {
String content = archiveSlbDao.findMaxVersionByName(name, ArchiveSlbEntity.READSET_FULL).getContent(); String content = archiveSlbDao.findMaxVersionBySlb(slbId, ArchiveSlbEntity.READSET_FULL).getContent();
return DefaultSaxParser.parseEntity(Slb.class, content); return DefaultSaxParser.parseEntity(Slb.class, content);
} }
@Override @Override
public Group getMaxVersionGroup(String name) throws Exception { public Group getMaxVersionGroup(long groupId) throws Exception {
String content = archiveGroupDao.findMaxVersionByName(name, ArchiveGroupEntity.READSET_FULL).getContent(); String content = archiveGroupDao.findMaxVersionByGroup(groupId, ArchiveGroupEntity.READSET_FULL).getContent();
return DefaultSaxParser.parseEntity(Group.class, content); return DefaultSaxParser.parseEntity(Group.class, content);
} }
@Override @Override
public List<Slb> getAllSlb(String name) throws Exception { public List<Slb> getAllSlb(long slbId) throws Exception {
List<ArchiveSlbDo> l = archiveSlbDao.findAllByName(name, ArchiveSlbEntity.READSET_FULL); List<ArchiveSlbDo> l = archiveSlbDao.findAllBySlb(slbId, ArchiveSlbEntity.READSET_FULL);
List<Slb> list = new ArrayList<>(); List<Slb> list = new ArrayList<>();
for (ArchiveSlbDo d : l) { for (ArchiveSlbDo d : l) {
list.add(DefaultSaxParser.parseEntity(Slb.class, d.getContent())); list.add(DefaultSaxParser.parseEntity(Slb.class, d.getContent()));
@ -93,8 +92,8 @@ public class ArchiveServiceImpl implements ArchiveService {
} }
@Override @Override
public List<Group> getAllGroup(String name) throws Exception { public List<Group> getAllGroup(long groupId) throws Exception {
List<ArchiveGroupDo> l = archiveGroupDao.findAllByName(name, ArchiveGroupEntity.READSET_FULL); List<ArchiveGroupDo> l = archiveGroupDao.findAllByGroup(groupId, ArchiveGroupEntity.READSET_FULL);
List<Group> list = new ArrayList<>(); List<Group> list = new ArrayList<>();
for (ArchiveGroupDo d : l) { for (ArchiveGroupDo d : l) {
list.add(DefaultSaxParser.parseEntity(Group.class, d.getContent())); list.add(DefaultSaxParser.parseEntity(Group.class, d.getContent()));
@ -103,15 +102,14 @@ public class ArchiveServiceImpl implements ArchiveService {
} }
@Override @Override
public Archive getLatestGroupArchive(String appName) throws Exception { public Archive getLatestSlbArchive(long slbId) throws Exception {
ArchiveGroupDo aad = archiveGroupDao.findMaxVersionByName(appName, ArchiveGroupEntity.READSET_FULL); ArchiveSlbDo asd = archiveSlbDao.findMaxVersionBySlb(slbId, ArchiveSlbEntity.READSET_FULL);
return C.toGroupArchive(aad);
}
@Override
public Archive getLatestSlbArchive(String slbName) throws Exception {
ArchiveSlbDo asd = archiveSlbDao.findMaxVersionByName(slbName, ArchiveSlbEntity.READSET_FULL);
return C.toSlbArchive(asd); return C.toSlbArchive(asd);
} }
@Override
public Archive getLatestGroupArchive(long groupId) throws Exception {
ArchiveGroupDo aad = archiveGroupDao.findMaxVersionByGroup(groupId, ArchiveGroupEntity.READSET_FULL);
return C.toGroupArchive(aad);
}
} }

View file

@ -92,7 +92,7 @@ public class SlbRepositoryImpl implements SlbRepository {
for (SlbServer slbServer : slb.getSlbServers()) { for (SlbServer slbServer : slb.getSlbServers()) {
nginxServerDao.insert(new NginxServerDo() nginxServerDao.insert(new NginxServerDo()
.setIp(slbServer.getIp()) .setIp(slbServer.getIp())
.setSlbName(slb.getName()) .setSlbId(slb.getId())
.setVersion(0) .setVersion(0)
.setCreatedTime(new Date())); .setCreatedTime(new Date()));
} }
@ -107,16 +107,16 @@ public class SlbRepositoryImpl implements SlbRepository {
for (SlbServer slbServer : slb.getSlbServers()) { for (SlbServer slbServer : slb.getSlbServers()) {
nginxServerDao.insert(new NginxServerDo() nginxServerDao.insert(new NginxServerDo()
.setIp(slbServer.getIp()) .setIp(slbServer.getIp())
.setSlbName(slb.getName()) .setSlbId(slb.getId())
.setVersion(0) .setVersion(0)
.setCreatedTime(new Date())); .setCreatedTime(new Date()));
} }
} }
@Override @Override
public int delete(String slbName) throws Exception { public int delete(long slbId) throws Exception {
int count = slbSync.delete(slbName); int count = slbSync.delete(slbId);
archiveService.deleteSlbArchive(slbName); archiveService.deleteSlbArchive(slbId);
return count; return count;
} }

View file

@ -15,24 +15,25 @@
]]> ]]>
</statement> </statement>
</query> </query>
<query name="update-by-name" type="UPDATE"> <query name="update-by-id" type="UPDATE">
<param name="name"/> <param name="id"/>
<statement> <statement>
<![CDATA[ <![CDATA[
UPDATE <TABLE/> UPDATE <TABLE/>
SET <FIELD name='app-id'/> = ${app-id}, SET <FIELD name='name'/> = ${name},
<FIELD name='app-id'/> = ${app-id},
<FIELD name='ssl'/> = ${ssl}, <FIELD name='ssl'/> = ${ssl},
version = version + 1 version = version + 1
WHERE <FIELD name='name'/> = ${name} WHERE <FIELD name='id'/> = ${id}
]]> ]]>
</statement> </statement>
</query> </query>
<query name="delete-by-name" type="DELETE"> <query name="delete-by-id" type="DELETE">
<param name="name"/> <param name="name"/>
<statement> <statement>
<![CDATA[ <![CDATA[
DELETE FROM <TABLE/> DELETE FROM <TABLE/>
WHERE <FIELD name='name'/> = ${name} WHERE <FIELD name='id'/> = ${id}
]]> ]]>
</statement> </statement>
</query> </query>
@ -56,6 +57,16 @@
]]> ]]>
</statement> </statement>
</query> </query>
<query name="find-by-id" type="SELECT">
<param name="id"/>
<statement>
<![CDATA[
SELECT <FIELDS/>
FROM <TABLE/>
WHERE <FIELD name='id'/> = ${id}
]]>
</statement>
</query>
<query name="find-by-name" type="SELECT"> <query name="find-by-name" type="SELECT">
<param name="name"/> <param name="name"/>
<statement> <statement>
@ -66,7 +77,7 @@
]]> ]]>
</statement> </statement>
</query> </query>
<query name="find-by-appId" type="SELECT"> <query name="find-by-app-id" type="SELECT">
<param name="app-id"/> <param name="app-id"/>
<statement> <statement>
<![CDATA[ <![CDATA[
@ -225,7 +236,7 @@
<entity name="group-slb" table="group_slb" alias="as2" do-class="GroupSlbDo"> <entity name="group-slb" table="group_slb" alias="as2" do-class="GroupSlbDo">
<member name="created-time" field="created_time" value-type="Date" insert-expr="now()"/> <member name="created-time" field="created_time" value-type="Date" insert-expr="now()"/>
<member name="data-change-last-time" field="DataChange_LastTime" value-type="Date" nullable="false" insert-expr="now()" update-expr="now()"/> <member name="data-change-last-time" field="DataChange_LastTime" value-type="Date" nullable="false" insert-expr="now()" update-expr="now()"/>
<var name="group-names" value-type="String[]"/> <var name="group-ids" value-type="long[]"/>
<query-defs> <query-defs>
<query name="insert" type="INSERT"> <query name="insert" type="INSERT">
<statement> <statement>
@ -233,9 +244,9 @@
INSERT INTO <TABLE/>(<FIELDS/>) INSERT INTO <TABLE/>(<FIELDS/>)
VALUES(<VALUES/>) VALUES(<VALUES/>)
ON DUPLICATE KEY UPDATE ON DUPLICATE KEY UPDATE
group_name = values(group_name), group_id = values(group_id),
slb_name = values(slb_name), slb_id = values(slb_id),
slb_virtual_server_name = values(slb_virtual_server_name), slb_virtual_server_id = values(slb_virtual_server_id),
path = values(path), path = values(path),
rewrite = values(rewrite), rewrite = values(rewrite),
priority = values(priority) priority = values(priority)
@ -243,53 +254,53 @@
</statement> </statement>
</query> </query>
<query name="delete-by-group" type="DELETE"> <query name="delete-by-group" type="DELETE">
<param name="group-name"/> <param name="group-id"/>
<statement> <statement>
<![CDATA[ <![CDATA[
DELETE FROM <TABLE/> DELETE FROM <TABLE/>
WHERE <FIELD name='group-name'/> = ${group-name} WHERE <FIELD name='group-id'/> = ${group-id}
]]> ]]>
</statement> </statement>
</query> </query>
<query name="find-all-by-group" type="SELECT" multiple="true"> <query name="find-all-by-group" type="SELECT" multiple="true">
<param name="group-name"/> <param name="group-id"/>
<statement> <statement>
<![CDATA[ <![CDATA[
SELECT <FIELDS/> SELECT <FIELDS/>
FROM <TABLE/> FROM <TABLE/>
WHERE <FIELD name='group-name'/> = ${group-name} WHERE <FIELD name='group-id'/> = ${group-id}
]]> ]]>
</statement> </statement>
</query> </query>
<query name="find-all-by-groups" type="SELECT" multiple="true"> <query name="find-all-by-groups" type="SELECT" multiple="true">
<param name="group-names"/> <param name="group-ids"/>
<statement> <statement>
<![CDATA[ <![CDATA[
SELECT <FIELDS/> SELECT <FIELDS/>
FROM <TABLE/> FROM <TABLE/>
WHERE <FIELD name='group-name'/> in <IN> ${group-names} </IN> WHERE <FIELD name='group-id'/> in <IN> ${group-ids} </IN>
]]> ]]>
</statement> </statement>
</query> </query>
<query name="find-all-by-slb" type="SELECT" multiple="true"> <query name="find-all-by-slb" type="SELECT" multiple="true">
<param name="slb-name"/> <param name="slb-id"/>
<statement> <statement>
<![CDATA[ <![CDATA[
SELECT <FIELDS/> SELECT <FIELDS/>
FROM <TABLE/> FROM <TABLE/>
WHERE <FIELD name='slb-name'/> = ${slb-name} WHERE <FIELD name='slb-id'/> = ${slb-id}
]]> ]]>
</statement> </statement>
</query> </query>
<query name="find-all-by-slb-and-virtual-server" type="SELECT" multiple="true"> <query name="find-all-by-slb-and-virtual-server" type="SELECT" multiple="true">
<param name="slb-name"/> <param name="slb-id"/>
<param name="slb-virtual-server-name"/> <param name="slb-virtual-server-id"/>
<statement> <statement>
<![CDATA[ <![CDATA[
SELECT <FIELDS/> SELECT <FIELDS/>
FROM <TABLE/> FROM <TABLE/>
WHERE <FIELD name='slb-name'/> = ${slb-name} WHERE <FIELD name='slb-id'/> = ${slb-id}
AND <FIELD name='slb-virtual-server-name'/> = ${slb-virtual-server-name} AND <FIELD name='slb-virtual-server-id'/> = ${slb-virtual-server-id}
]]> ]]>
</statement> </statement>
</query> </query>
@ -349,12 +360,13 @@
]]> ]]>
</statement> </statement>
</query> </query>
<query name="update-by-name" type="UPDATE"> <query name="update-by-id" type="UPDATE">
<param name="name"/> <param name="id"/>
<statement> <statement>
<![CDATA[ <![CDATA[
UPDATE <TABLE/> UPDATE <TABLE/>
SET <FIELD name='nginx-bin'/> = ${nginx-bin}, SET <FIELD name='name'/> = ${name},
<FIELD name='nginx-bin'/> = ${nginx-bin},
<FIELD name='nginx-conf'/> = ${nginx-conf}, <FIELD name='nginx-conf'/> = ${nginx-conf},
<FIELD name='nginx-worker-processes'/> = ${nginx-worker-processes}, <FIELD name='nginx-worker-processes'/> = ${nginx-worker-processes},
<FIELD name='status'/> = ${status}, <FIELD name='status'/> = ${status},
@ -568,7 +580,7 @@
]]> ]]>
</statement> </statement>
</query> </query>
<query name="find-all-by-slb-and-name" type="SELECT"> <query name="find-by-slb-and-name" type="SELECT">
<param name="slb-id"/> <param name="slb-id"/>
<param name="name"/> <param name="name"/>
<statement> <statement>
@ -585,45 +597,45 @@
<entity name="archive-group" table="archive_group" alias="aa" do-class="ArchiveGroupDo"> <entity name="archive-group" table="archive_group" alias="aa" do-class="ArchiveGroupDo">
<query-defs> <query-defs>
<query name="delete-by-group" type="DELETE"> <query name="delete-by-group" type="DELETE">
<param name="name"/> <param name="group-id"/>
<statement> <statement>
<![CDATA[ <![CDATA[
DELETE FROM <TABLE/> DELETE FROM <TABLE/>
WHERE <FIELD name='name'/> = ${name} WHERE <FIELD name='group-id'/> = ${group-id}
]]> ]]>
</statement> </statement>
</query> </query>
<query name="find-max-version-by-name" type="SELECT"> <query name="find-max-version-by-group" type="SELECT">
<param name="name"/> <param name="group-id"/>
<statement> <statement>
<![CDATA[ <![CDATA[
SELECT <FIELDS/> SELECT <FIELDS/>
FROM <TABLE/> FROM <TABLE/>
WHERE <FIELD name='name'/> = ${name} WHERE <FIELD name='group-id'/> = ${group-id}
ORDER BY <FIELD name='version'/> DESC ORDER BY <FIELD name='version'/> DESC
LIMIT 1 LIMIT 1
]]> ]]>
</statement> </statement>
</query> </query>
<query name="find-by-name-and-version" type="SELECT"> <query name="find-by-group-and-version" type="SELECT">
<param name="name"/> <param name="group-id"/>
<param name="version"/> <param name="version"/>
<statement> <statement>
<![CDATA[ <![CDATA[
SELECT <FIELDS/> SELECT <FIELDS/>
FROM <TABLE/> FROM <TABLE/>
WHERE <FIELD name='name'/> = ${version} WHERE <FIELD name='group-id'/> = ${group-id}
AND <FIELD name='version'/> = ${version} AND <FIELD name='version'/> = ${version}
]]> ]]>
</statement> </statement>
</query> </query>
<query name="find-all-by-name" type="SELECT" multiple="true"> <query name="find-all-by-group" type="SELECT" multiple="true">
<param name="name"/> <param name="group-id"/>
<statement> <statement>
<![CDATA[ <![CDATA[
SELECT <FIELDS/> SELECT <FIELDS/>
FROM <TABLE/> FROM <TABLE/>
WHERE <FIELD name='name'/> = ${name} WHERE <FIELD name='group-id'/> = ${group-id}
]]> ]]>
</statement> </statement>
</query> </query>
@ -632,45 +644,45 @@
<entity name="archive-slb" table="archive_slb" alias="as3" do-class="ArchiveSlbDo"> <entity name="archive-slb" table="archive_slb" alias="as3" do-class="ArchiveSlbDo">
<query-defs> <query-defs>
<query name="delete-by-slb" type="DELETE"> <query name="delete-by-slb" type="DELETE">
<param name="name"/> <param name="slb-id"/>
<statement> <statement>
<![CDATA[ <![CDATA[
DELETE FROM <TABLE/> DELETE FROM <TABLE/>
WHERE <FIELD name='name'/> = ${name} WHERE <FIELD name='slb-id'/> = ${slb-id}
]]> ]]>
</statement> </statement>
</query> </query>
<query name="find-max-version-by-name" type="SELECT"> <query name="find-max-version-by-slb" type="SELECT">
<param name="name"/> <param name="slb-id"/>
<statement> <statement>
<![CDATA[ <![CDATA[
SELECT <FIELDS/> SELECT <FIELDS/>
FROM <TABLE/> FROM <TABLE/>
WHERE <FIELD name='name'/> = ${name} WHERE <FIELD name='slb-id'/> = ${slb-id}
ORDER BY <FIELD name='version'/> DESC ORDER BY <FIELD name='version'/> DESC
LIMIT 1 LIMIT 1
]]> ]]>
</statement> </statement>
</query> </query>
<query name="find-by-name-and-version" type="SELECT"> <query name="find-by-slb-and-version" type="SELECT">
<param name="name"/> <param name="slb-id"/>
<param name="version"/> <param name="version"/>
<statement> <statement>
<![CDATA[ <![CDATA[
SELECT <FIELDS/> SELECT <FIELDS/>
FROM <TABLE/> FROM <TABLE/>
WHERE <FIELD name='name'/> = ${version} WHERE <FIELD name='slb-id'/> = ${slb-id}
AND <FIELD name='version'/> = ${version} AND <FIELD name='version'/> = ${version}
]]> ]]>
</statement> </statement>
</query> </query>
<query name="find-all-by-name" type="SELECT" multiple="true"> <query name="find-all-by-slb" type="SELECT" multiple="true">
<param name="name"/> <param name="slb-id"/>
<statement> <statement>
<![CDATA[ <![CDATA[
SELECT <FIELDS/> SELECT <FIELDS/>
FROM <TABLE/> FROM <TABLE/>
WHERE <FIELD name='name'/> = ${name} WHERE <FIELD name='slb-id'/> = ${slb-id}
]]> ]]>
</statement> </statement>
</query> </query>

View file

@ -20,6 +20,7 @@
<entity-ref name="dy-upstream-ops-data" /> <entity-ref name="dy-upstream-ops-data" />
</entity> </entity>
<entity name="slb"> <entity name="slb">
<attribute name="id" value-type="int" />
<attribute name="name" value-type="String" /> <attribute name="name" value-type="String" />
<attribute name="version" value-type="int" /> <attribute name="version" value-type="int" />
<element name="nginx-bin" value-type="String" /> <element name="nginx-bin" value-type="String" />
@ -53,6 +54,7 @@
<entity-ref name="slb" type="list" names="slbs" xml-indent="true" /> <entity-ref name="slb" type="list" names="slbs" xml-indent="true" />
</entity> </entity>
<entity name="group"> <entity name="group">
<attribute name="id" value-type="int" />
<attribute name="name" value-type="String" /> <attribute name="name" value-type="String" />
<attribute name="app-id" value-type="String" /> <attribute name="app-id" value-type="String" />
<attribute name="version" value-type="int" /> <attribute name="version" value-type="int" />

View file

@ -1,5 +1,5 @@
<model> <model>
<slb name="default" version="1"> <slb id="111111" name="default" version="1">
<nginx-bin>/usr/local/nginx/bin</nginx-bin> <nginx-bin>/usr/local/nginx/bin</nginx-bin>
<nginx-conf>/usr/local/nginx/conf</nginx-conf> <nginx-conf>/usr/local/nginx/conf</nginx-conf>
<nginx-worker-processes>1</nginx-worker-processes> <nginx-worker-processes>1</nginx-worker-processes>
@ -40,7 +40,7 @@
<vip ip="192.168.1.1"/> <vip ip="192.168.1.1"/>
<group name="gateway" app-id="app921822" version="1" ssl="false"> <group id="111111" name="gateway" app-id="app921822" version="1" ssl="false">
<group-slbs> <group-slbs>
<group-slb priority="0"> <group-slb priority="0">
<group-name>gateway</group-name> <group-name>gateway</group-name>