This commit is contained in:
Mengyi Zhou 2017-01-24 15:40:05 +08:00 committed by vfqq樊琪琦
parent 9d4dc719d2
commit a530f36956
8 changed files with 73 additions and 10 deletions

View file

@ -70,6 +70,9 @@ public class TrafficPolicyResource {
queryRender.readToCommand(cmd); queryRender.readToCommand(cmd);
IdVersion[] searchKeys = trafficPolicyQuery.queryByCommand(cmd, SelectionMode.getMode(mode)); IdVersion[] searchKeys = trafficPolicyQuery.queryByCommand(cmd, SelectionMode.getMode(mode));
if (searchKeys == null) {
searchKeys = trafficPolicyQuery.queryAll(SelectionMode.getMode(mode)).toArray(new IdVersion[]{});
}
List<TrafficPolicy> result = trafficPolicyRepository.list(searchKeys); List<TrafficPolicy> result = trafficPolicyRepository.list(searchKeys);
List<ExtendedView.ExtendedTrafficPolicy> viewArray = new ArrayList<>(result.size()); List<ExtendedView.ExtendedTrafficPolicy> viewArray = new ArrayList<>(result.size());
@ -132,6 +135,13 @@ public class TrafficPolicyResource {
if (extendedView.getTags() != null) { if (extendedView.getTags() != null) {
addTag(p.getId(), extendedView.getTags()); addTag(p.getId(), extendedView.getTags());
} }
try {
if (trafficPolicyQuery.queryByIdAndMode(p.getId(), SelectionMode.ONLINE_EXCLUSIVE).length == 1) {
propertyBox.set("status", "toBeActivated", "policy", p.getId());
}
} catch (Exception ex) {
}
return responseHandler.handle(p, hh.getMediaType()); return responseHandler.handle(p, hh.getMediaType());
} }

View file

@ -11,6 +11,8 @@ public interface ModelValidator<T> {
void validateForActivate(T[] toBeActivatedItems, boolean escapedPathValidation) throws Exception; void validateForActivate(T[] toBeActivatedItems, boolean escapedPathValidation) throws Exception;
void validateForDeactivate(Long[] toBeDeactivatedItems) throws Exception;
void checkVersionForUpdate(T target) throws Exception; void checkVersionForUpdate(T target) throws Exception;
void removable(Long targetId) throws Exception; void removable(Long targetId) throws Exception;

View file

@ -121,11 +121,18 @@ public class DefaultGroupValidator implements GroupValidator {
} }
} }
@Override
public void validateForDeactivate(Long[] toBeDeactivatedItems) throws Exception {
for (RTrafficPolicyGroupDo e : rTrafficPolicyGroupDao.findByGroupsAndPolicyActiveVersion(toBeDeactivatedItems, RTrafficPolicyGroupEntity.READSET_FULL)) {
throw new ValidationException("Group that you try to deactivate is hold by an online traffic policy " + e.getPolicyId() + ".");
}
}
@Override @Override
public void checkVersionForUpdate(Group target) throws Exception { public void checkVersionForUpdate(Group target) throws Exception {
RelGroupStatusDo check = rGroupStatusDao.findByGroup(target.getId(), RGroupStatusEntity.READSET_FULL); RelGroupStatusDo check = rGroupStatusDao.findByGroup(target.getId(), RGroupStatusEntity.READSET_FULL);
if (check == null) { if (check == null) {
throw new ValidationException("Group that you tries to update does not exist."); throw new ValidationException("Group that you try to update does not exist.");
} }
if (check.getOfflineVersion() > target.getVersion()) { if (check.getOfflineVersion() > target.getVersion()) {
throw new ValidationException("Newer version is detected."); throw new ValidationException("Newer version is detected.");
@ -306,7 +313,7 @@ public class DefaultGroupValidator implements GroupValidator {
} else { } else {
// check priority and reformat // check priority and reformat
if (!e.getPriority().equals(insertEntry.getPriority())) { if (!e.getPriority().equals(insertEntry.getPriority())) {
throw new ValidationException("Group that you tries to create/modify may cause path prefix-overlap problem with other entries on virtual-server " + vsId + ". Recommend priority will be " + insertEntry.getPriority() + "."); throw new ValidationException("Group that you try to create/modify may cause path prefix-overlap problem with other entries on virtual-server " + vsId + ". Recommend priority will be " + insertEntry.getPriority() + ".");
} }
e.setPath(insertEntry.getPath()); e.setPath(insertEntry.getPath());
} }

View file

@ -85,6 +85,11 @@ public class DefaultSlbValidator implements SlbValidator {
} }
@Override
public void validateForDeactivate(Long[] toBeDeactivatedItems) throws Exception {
}
@Override @Override
public void checkVersionForUpdate(Slb target) throws Exception { public void checkVersionForUpdate(Slb target) throws Exception {
SlbDo check = slbDao.findById(target.getId(), SlbEntity.READSET_FULL); SlbDo check = slbDao.findById(target.getId(), SlbEntity.READSET_FULL);

View file

@ -29,6 +29,8 @@ public class DefaultTrafficPolicyValidator implements TrafficPolicyValidator {
private RGroupVsDao rGroupVsDao; private RGroupVsDao rGroupVsDao;
@Resource @Resource
private PathValidator pathValidator; private PathValidator pathValidator;
@Resource
private RGroupStatusDao rGroupStatusDao;
@Override @Override
public boolean exists(Long targetId) throws Exception { public boolean exists(Long targetId) throws Exception {
@ -73,6 +75,7 @@ public class DefaultTrafficPolicyValidator implements TrafficPolicyValidator {
for (RelGroupVsDo e : rGroupVsDao.findByVsesAndGroupOfflineVersion(vsIds, RGroupVsEntity.READSET_FULL)) { for (RelGroupVsDo e : rGroupVsDao.findByVsesAndGroupOfflineVersion(vsIds, RGroupVsEntity.READSET_FULL)) {
putArrayEntryValue(gvsListByVsId, e.getVsId(), e); putArrayEntryValue(gvsListByVsId, e.getVsId(), e);
} }
validatePolicyControls(target, groupIds, vsIds, gvsListByVsId);
Map<Long, List<RTrafficPolicyVsDo>> pvsListByVsId = new HashMap<>(); Map<Long, List<RTrafficPolicyVsDo>> pvsListByVsId = new HashMap<>();
for (RTrafficPolicyVsDo e : rTrafficPolicyVsDao.findByVsesAndPolicyVersion(vsIds, RTrafficPolicyVsEntity.READSET_FULL)) { for (RTrafficPolicyVsDo e : rTrafficPolicyVsDao.findByVsesAndPolicyVersion(vsIds, RTrafficPolicyVsEntity.READSET_FULL)) {
@ -84,8 +87,6 @@ public class DefaultTrafficPolicyValidator implements TrafficPolicyValidator {
putArrayEntryValue(policyListByGroupId, e.getGroupId(), e); putArrayEntryValue(policyListByGroupId, e.getGroupId(), e);
} }
validatePolicyControls(target, groupIds, vsIds, gvsListByVsId);
Map<Long, List<PathValidator.LocationEntry>> currentLocationEntriesByVs = new HashMap<>(); Map<Long, List<PathValidator.LocationEntry>> currentLocationEntriesByVs = new HashMap<>();
compareAndBuildCurrentLocationEntries(target, groupIds, vsIds, policyListByGroupId, pvsListByVsId, gvsListByVsId, compareAndBuildCurrentLocationEntries(target, groupIds, vsIds, policyListByGroupId, pvsListByVsId, gvsListByVsId,
escapePathValidation ? null : currentLocationEntriesByVs); escapePathValidation ? null : currentLocationEntriesByVs);
@ -199,6 +200,13 @@ public class DefaultTrafficPolicyValidator implements TrafficPolicyValidator {
policies[i] = e.getId(); policies[i] = e.getId();
hashes[i] = VersionUtils.getHash(e.getId(), e.getVersion()); hashes[i] = VersionUtils.getHash(e.getId(), e.getVersion());
} }
Long[] groupIds = groupLookup.toArray(new Long[groupLookup.size()]);
for (RelGroupStatusDo e : rGroupStatusDao.findByGroups(groupIds, RGroupStatusEntity.READSET_FULL)) {
if (e.getOnlineVersion() < 0) {
throw new ValidationException("Group " + e.getGroupId() + " has not been activated.");
}
}
Long[] vsIds = vsLookup.toArray(new Long[vsLookup.size()]); Long[] vsIds = vsLookup.toArray(new Long[vsLookup.size()]);
Map<Long, List<RelGroupVsDo>> gvsListByVsId = new HashMap<>(); Map<Long, List<RelGroupVsDo>> gvsListByVsId = new HashMap<>();
@ -219,7 +227,7 @@ public class DefaultTrafficPolicyValidator implements TrafficPolicyValidator {
} }
Map<Long, List<RTrafficPolicyGroupDo>> policyListByGroupId = new HashMap<>(); Map<Long, List<RTrafficPolicyGroupDo>> policyListByGroupId = new HashMap<>();
for (RTrafficPolicyGroupDo e : rTrafficPolicyGroupDao.findByGroupsAndPolicyActiveVersion(groupLookup.toArray(new Long[groupLookup.size()]), RTrafficPolicyGroupEntity.READSET_FULL)) { for (RTrafficPolicyGroupDo e : rTrafficPolicyGroupDao.findByGroupsAndPolicyActiveVersion(groupIds, RTrafficPolicyGroupEntity.READSET_FULL)) {
if (Arrays.binarySearch(policies, e.getPolicyId()) >= 0) continue; if (Arrays.binarySearch(policies, e.getPolicyId()) >= 0) continue;
putArrayEntryValue(policyListByGroupId, e.getGroupId(), e); putArrayEntryValue(policyListByGroupId, e.getGroupId(), e);
} }
@ -252,6 +260,11 @@ public class DefaultTrafficPolicyValidator implements TrafficPolicyValidator {
} }
} }
@Override
public void validateForDeactivate(Long[] toBeDeactivatedItems) throws Exception {
}
private <T> void putArrayEntryValue(Map<Long, List<T>> map, Long key, T e) { private <T> void putArrayEntryValue(Map<Long, List<T>> map, Long key, T e) {
List<T> v = map.get(key); List<T> v = map.get(key);
if (v == null) { if (v == null) {
@ -265,12 +278,15 @@ public class DefaultTrafficPolicyValidator implements TrafficPolicyValidator {
Long prev = groupIds[0]; Long prev = groupIds[0];
for (int i = 1; i < groupIds.length; i++) { for (int i = 1; i < groupIds.length; i++) {
if (prev.equals(groupIds[i])) { if (prev.equals(groupIds[i])) {
throw new ValidationException("Traffic policy that you tries to create/modify declares the same group " + prev + " more than once."); throw new ValidationException("Traffic policy that you try to create/modify declares the same group " + prev + " more than once.");
} }
prev = groupIds[i]; prev = groupIds[i];
} }
if (groupIds.length <= 1) { if (groupIds.length <= 1) {
throw new ValidationException("Traffic policy that you tries to create/modify does not have enough traffic-controls."); throw new ValidationException("Traffic policy that you try to create/modify does not have enough traffic-controls.");
}
if (rGroupStatusDao.findByGroups(groupIds, RGroupStatusEntity.READSET_FULL).size() != groupIds.length) {
throw new ValidationException("Traffic policy that you try to create/modify contains group that does not exist.");
} }
int[] versions = new int[groupIds.length]; int[] versions = new int[groupIds.length];
@ -355,7 +371,7 @@ public class DefaultTrafficPolicyValidator implements TrafficPolicyValidator {
public void checkVersionForUpdate(TrafficPolicy target) throws Exception { public void checkVersionForUpdate(TrafficPolicy target) throws Exception {
TrafficPolicyDo d = trafficPolicyDao.findById(target.getId(), TrafficPolicyEntity.READSET_FULL); TrafficPolicyDo d = trafficPolicyDao.findById(target.getId(), TrafficPolicyEntity.READSET_FULL);
if (d == null) { if (d == null) {
throw new ValidationException("Traffic policy that you tries to update does not exist."); throw new ValidationException("Traffic policy that you try to update does not exist.");
} }
if (d.getVersion() > target.getVersion()) { if (d.getVersion() > target.getVersion()) {
throw new ValidationException("Newer version is detected."); throw new ValidationException("Newer version is detected.");
@ -369,7 +385,7 @@ public class DefaultTrafficPolicyValidator implements TrafficPolicyValidator {
public void removable(Long targetId) throws Exception { public void removable(Long targetId) throws Exception {
TrafficPolicyDo d = trafficPolicyDao.findById(targetId, TrafficPolicyEntity.READSET_FULL); TrafficPolicyDo d = trafficPolicyDao.findById(targetId, TrafficPolicyEntity.READSET_FULL);
if (d != null && d.getActiveVersion() > 0) { if (d != null && d.getActiveVersion() > 0) {
throw new ValidationException("Traffic policy that you tried to delete is still active."); throw new ValidationException("Traffic policy that you try to delete is still active.");
} }
} }

View file

@ -43,6 +43,11 @@ public class DefaultVGroupValidator implements VGroupValidator {
} }
@Override
public void validateForDeactivate(Long[] toBeDeactivatedItems) throws Exception {
}
@Override @Override
public void checkVersionForUpdate(Group target) throws Exception { public void checkVersionForUpdate(Group target) throws Exception {
groupModelValidator.checkVersionForUpdate(target); groupModelValidator.checkVersionForUpdate(target);

View file

@ -112,6 +112,11 @@ public class DefaultVirtualServerValidator implements VirtualServerValidator {
} }
@Override
public void validateForDeactivate(Long[] toBeDeactivatedItems) throws Exception {
}
@Override @Override
public void checkVersionForUpdate(VirtualServer target) throws Exception { public void checkVersionForUpdate(VirtualServer target) throws Exception {

View file

@ -1,6 +1,7 @@
package com.ctrip.zeus.service.query.impl; package com.ctrip.zeus.service.query.impl;
import com.ctrip.zeus.dal.core.*; import com.ctrip.zeus.dal.core.*;
import com.ctrip.zeus.executor.impl.ResultHandler;
import com.ctrip.zeus.model.entity.TrafficPolicy; import com.ctrip.zeus.model.entity.TrafficPolicy;
import com.ctrip.zeus.service.model.IdVersion; import com.ctrip.zeus.service.model.IdVersion;
import com.ctrip.zeus.service.model.SelectionMode; import com.ctrip.zeus.service.model.SelectionMode;
@ -130,7 +131,19 @@ public class DefaultTrafficPolicyCriteriaQuery implements TrafficPolicyQuery {
} }
return result; return result;
} }
}).build(IdVersion.class).run(); }).build(IdVersion.class).run(new ResultHandler<IdVersion, IdVersion>() {
@Override
public IdVersion[] handle(Set<IdVersion> result) throws Exception {
if (result == null) return null;
if (result.size() == 0) return new IdVersion[0];
Set<Long> ids = new HashSet<>();
for (IdVersion e : result) {
ids.add(e.getId());
}
result.retainAll(queryByIdsAndMode(ids.toArray(new Long[ids.size()]), mode));
return result.toArray(new IdVersion[result.size()]);
}
});
return result; return result;
} }