Skip to content

Commit 7f2f25d

Browse files
bvbharatJayapal
authored andcommitted
Cloudstack-2150 DB table entries of phisical network is not proper.Shows Duplicate entries Cloudstack-2980 Adding a VLAN range that overlaps with two existing ranges results in inconsistent DB entries
Signed-off-by: Jayapal <jayapal@apache.org>
1 parent 3096261 commit 7f2f25d

2 files changed

Lines changed: 34 additions & 53 deletions

File tree

engine/schema/src/com/cloud/dc/dao/DataCenterVnetDaoImpl.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@ public List<DataCenterVnetVO> findVnet(long dcId, long physicalNetworkId, String
113113

114114
@DB
115115
public void add(long dcId, long physicalNetworkId, int start, int end) {
116-
String insertVnet = "INSERT INTO `cloud`.`op_dc_vnet_alloc` (vnet, data_center_id, physical_network_id) VALUES ( ?, ?, ?)";
116+
String insertVnet = "INSERT IGNORE INTO `cloud`.`op_dc_vnet_alloc` (vnet, data_center_id, physical_network_id) VALUES ( ?, ?, ?)";
117117

118118
Transaction txn = Transaction.currentTxn();
119119
try {

server/src/com/cloud/network/NetworkServiceImpl.java

Lines changed: 33 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -2561,7 +2561,7 @@ public PhysicalNetwork updatePhysicalNetwork(Long id, String networkSpeed, List<
25612561

25622562
if (removeVlan != null){
25632563
List<Integer> tokens = processVlanRange(network,removeVlan);
2564-
boolean result = removeVlanRange(network, tokens.get(0), tokens.get(1));
2564+
removeVlanRange(network, tokens.get(0), tokens.get(1));
25652565
}
25662566

25672567
if (tags != null && tags.size() > 1) {
@@ -2589,7 +2589,6 @@ public PhysicalNetwork updatePhysicalNetwork(Long id, String networkSpeed, List<
25892589
network.setSpeed(networkSpeed);
25902590
}
25912591

2592-
// Vnet range can be extended only
25932592
boolean AddVnet = true;
25942593
List<Pair<Integer, Integer>> vnetsToAdd = new ArrayList<Pair<Integer, Integer>>();
25952594

@@ -2606,7 +2605,6 @@ public PhysicalNetwork updatePhysicalNetwork(Long id, String networkSpeed, List<
26062605
int existingStartVnet = existingRanges.get(j).first();
26072606
int existingEndVnet = existingRanges.get(j).second();
26082607

2609-
// check if vnet is being extended
26102608
if (newStartVnet.intValue() >= existingStartVnet & newEndVnet.intValue() <= existingEndVnet) {
26112609
throw new InvalidParameterValueException("The vlan range you trying to add already exists.");
26122610
}
@@ -2630,80 +2628,63 @@ else if (newStartVnet< existingStartVnet & newEndVnet > existingEndVnet){
26302628
vnetsToAdd.add(new Pair<Integer, Integer>(existingEndVnet+1,newEndVnet));
26312629
existingRanges.get(j).first(newStartVnet);
26322630
existingRanges.get(j).second(newEndVnet);
2631+
AddVnet = false;
26332632
break;
26342633
}
26352634
}
26362635

26372636
}
26382637
if (AddVnet){
2639-
vnetsToAdd.add(new Pair<Integer, Integer>(newStartVnet, newEndVnet));
2640-
existingRanges.add(new Pair<Integer, Integer>(newStartVnet,newEndVnet));
2641-
}
2642-
2643-
Map <Integer,Integer> vnetMap = new HashMap<Integer, Integer>(existingRanges.size());
2644-
Map <Integer, Integer> IndexMap = new HashMap<Integer, Integer>(existingRanges.size());
2645-
for (int i=0; i< existingRanges.size(); i++){
2646-
vnetMap.put(existingRanges.get(i).first(),existingRanges.get(i).second());
2647-
IndexMap.put(existingRanges.get(i).first(),i);
2638+
vnetsToAdd.add(new Pair<Integer, Integer>(newStartVnet, newEndVnet));
2639+
existingRanges.add(new Pair<Integer, Integer>(newStartVnet,newEndVnet));
2640+
j= existingRanges.size()-1;
26482641
}
2649-
26502642
Integer value;
2651-
Integer index;
26522643
String vnetString = "";
2644+
2645+
Integer changed_entry_start = existingRanges.get(j).first();
2646+
Integer changed_entry_end = existingRanges.get(j).second();
26532647
for (int i=0; i < existingRanges.size(); i++){
2654-
value = vnetMap.get((existingRanges.get(i).second()+1));
2655-
if (value != null) {
2656-
vnetMap.remove((existingRanges.get(i).second()+1));
2657-
vnetMap.remove(existingRanges.get(i).first());
2658-
vnetMap.put(existingRanges.get(i).first(),value);
2659-
existingRanges.add(new Pair<Integer,Integer>(existingRanges.get(i).first(),value));
2660-
index = IndexMap.get(existingRanges.get(i).second()+1);
2661-
existingRanges.get(index).first(-1);
2662-
existingRanges.get(index).second(-1);
2663-
existingRanges.get(i).first(-1);
2664-
existingRanges.get(i).second(-1);
2665-
}
2666-
value = vnetMap.get((existingRanges.get(i).second()));
2667-
if (value != null && ( (existingRanges.get(i).second()) != (existingRanges.get(i).first()) )) {
2668-
vnetMap.remove((existingRanges.get(i).second()));
2669-
vnetMap.remove(existingRanges.get(i).first());
2670-
vnetMap.put(existingRanges.get(i).first(),value);
2671-
existingRanges.add(new Pair<Integer,Integer>(existingRanges.get(i).first(),value));
2672-
index = IndexMap.get(existingRanges.get(i).second());
2673-
existingRanges.get(index).first(-1);
2674-
existingRanges.get(index).second(-1);
2648+
if (i !=j && existingRanges.get(i).first()<=changed_entry_end && existingRanges.get(i).second() >= changed_entry_end) {
2649+
existingRanges.get(j).second(existingRanges.get(i).second());
26752650
existingRanges.get(i).first(-1);
26762651
existingRanges.get(i).second(-1);
2652+
} else if ((i !=j && changed_entry_end > existingRanges.get(i).second()) && changed_entry_start <= existingRanges.get(i).first()) {
2653+
existingRanges.get(i).first(-1);
2654+
existingRanges.get(i).second(-1);
2655+
}else if ((i != j ) && changed_entry_end > existingRanges.get(i).second() && changed_entry_start <= existingRanges.get(i).second() && existingRanges.get(i).first() <= changed_entry_start) {
2656+
existingRanges.get(j).first(existingRanges.get(i).first());
2657+
existingRanges.get(i).first(-1);
2658+
existingRanges.get(i).first(-1);
26772659
}
26782660
}
26792661

26802662

2681-
2682-
if (newVnetRangeString != null) {
2683-
for (Pair<Integer,Integer> vnetRange : existingRanges ){
2684-
value=vnetMap.get(vnetRange.first());
2685-
if (value != null){
2686-
vnetString = vnetString+vnetRange.first().toString()+"-"+value.toString()+";";
2687-
}
2688-
}
2689-
if (vnetString.length() > 0 && vnetString.charAt(vnetString.length()-1)==';') {
2690-
vnetString = vnetString.substring(0, vnetString.length()-1);
2691-
}
2692-
network.setVnet(vnetString);
2663+
for (Pair<Integer,Integer> vnetRange : existingRanges ){
2664+
value=vnetRange.first();
2665+
if (value != -1){
2666+
vnetString = vnetString+vnetRange.first().toString()+"-"+vnetRange.second().toString()+";";
2667+
}
26932668
}
2694-
2669+
if (vnetString.length() > 0 && vnetString.charAt(vnetString.length()-1)==';') {
2670+
vnetString = vnetString.substring(0, vnetString.length()-1);
2671+
}
2672+
network.setVnet(vnetString);
2673+
Transaction txn = Transaction.currentTxn();
2674+
txn.start();
26952675
for (Pair<Integer, Integer> vnetToAdd : vnetsToAdd) {
26962676
s_logger.debug("Adding vnet range " + vnetToAdd.first() + "-" + vnetToAdd.second() + " for the physicalNetwork id= " + id + " and zone id=" + network.getDataCenterId()
2697-
+ " as a part of updatePhysicalNetwork call");
2677+
+ " as a part of updatePhysicalNetwork call");
26982678
_dcDao.addVnet(network.getDataCenterId(), network.getId(), vnetToAdd.first(), vnetToAdd.second());
26992679
}
2680+
_physicalNetworkDao.update(id, network);
2681+
txn.commit();
27002682
}
27012683

2702-
_physicalNetworkDao.update(id, network);
2703-
2704-
return network;
2684+
return network;
27052685
}
27062686

2687+
27072688
private List<Integer> processVlanRange(PhysicalNetworkVO network, String removeVlan) {
27082689
Integer StartVnet;
27092690
Integer EndVnet;

0 commit comments

Comments
 (0)