From a8a0a70629f366ff3b79e04fba5eee37ff95e50e Mon Sep 17 00:00:00 2001 From: alena Date: Wed, 13 Apr 2011 17:48:49 -0700 Subject: [PATCH] bug 9433: update network_id (if 0 or NULL) in user_statistics table status 9433: resolved fixed --- .../com/cloud/upgrade/dao/Upgrade218to22.java | 23 ++- .../cloud/upgrade/dao/Upgrade222to224.java | 146 ++++++++++++------ 2 files changed, 114 insertions(+), 55 deletions(-) diff --git a/server/src/com/cloud/upgrade/dao/Upgrade218to22.java b/server/src/com/cloud/upgrade/dao/Upgrade218to22.java index 6edb221ad30..61ccfc973be 100644 --- a/server/src/com/cloud/upgrade/dao/Upgrade218to22.java +++ b/server/src/com/cloud/upgrade/dao/Upgrade218to22.java @@ -258,7 +258,7 @@ public class Upgrade218to22 implements DbUpgrade { pstmt.close(); } - insertNic(conn, guestNetworkId, domrId, running, guestMac, guestIp, guestNetmask, "Start", gateway, vnet, "DirectPodBasedNetworkGuru", false, 0, "Static", null); + insertNic(conn, guestNetworkId, domrId, running, guestMac, guestIp, guestNetmask, "Start", gateway, vnet, "DirectPodBasedNetworkGuru", false, 0, "Dhcp", null); } else { insertNic(conn, publicNetworkId, domrId, running, publicMac, publicIp, publicNetmask, "Create", gateway, publicVlan, "PublicNetworkGuru", true, 2, "Static", null); long controlNicId = insertNic(conn, controlNetworkId, domrId, running, privateMac, privateIp, privateNetmask, "Start", "169.254.0.1", null, "ControlNetworkGuru", false, 1, "Static", @@ -271,7 +271,7 @@ public class Upgrade218to22 implements DbUpgrade { pstmt.executeUpdate(); pstmt.close(); } - insertNic(conn, guestNetworkId, domrId, running, guestMac, guestIp, guestNetmask, "Start", null, vnet, "ExternalGuestNetworkGuru", false, 0, "Static", null); + insertNic(conn, guestNetworkId, domrId, running, guestMac, guestIp, guestNetmask, "Start", null, vnet, "ExternalGuestNetworkGuru", false, 0, "Dhcp", null); } } @@ -334,7 +334,7 @@ public class Upgrade218to22 implements DbUpgrade { pstmt.close(); if (zoneType.equalsIgnoreCase("Basic")) { - insertNic(conn, publicNetworkId, ssvmId, running, publicMac, publicIp, publicNetmask, "Start", gateway, publicVlan, "DirectPodBasedNetworkGuru", true, 2, "Static", null); + insertNic(conn, publicNetworkId, ssvmId, running, publicMac, publicIp, publicNetmask, "Start", gateway, publicVlan, "DirectPodBasedNetworkGuru", true, 2, "Dhcp", null); } else { insertNic(conn, publicNetworkId, ssvmId, running, publicMac, publicIp, publicNetmask, "Create", gateway, publicVlan, "PublicNetworkGuru", true, 2, "Static", null); @@ -415,7 +415,7 @@ public class Upgrade218to22 implements DbUpgrade { pstmt.close(); if (zoneType.equalsIgnoreCase("Basic")) { - insertNic(conn, publicNetworkId, cpId, running, publicMac, publicIp, publicNetmask, "Start", gateway, publicVlan, "DirectPodBasedNetworkGuru", true, 2, "Static", null); + insertNic(conn, publicNetworkId, cpId, running, publicMac, publicIp, publicNetmask, "Start", gateway, publicVlan, "DirectPodBasedNetworkGuru", true, 2, "Dhcp", null); } else { insertNic(conn, publicNetworkId, cpId, running, publicMac, publicIp, publicNetmask, "Create", gateway, publicVlan, "PublicNetworkGuru", true, 2, "Static", null); } @@ -1037,6 +1037,21 @@ public class Upgrade218to22 implements DbUpgrade { Long accountId = rs.getLong(2); // account id Long dataCenterId = rs.getLong(3); // zone id + pstmt = conn.prepareStatement("SELECT networktype from data_center where id=?"); + pstmt.setLong(1, dataCenterId); + + ResultSet dcSet = pstmt.executeQuery(); + + if (!dcSet.next()) { + throw new CloudRuntimeException("Unable to get data_center information as a part of user_statistics update"); + } + + String dataCenterType = dcSet.getString(1); + + if (dataCenterType.equalsIgnoreCase("basic")) { + accountId = 1L; + } + pstmt = conn.prepareStatement("SELECT id from vm_instance where account_id=? AND data_center_id=? AND type='DomainRouter'"); pstmt.setLong(1, accountId); pstmt.setLong(2, dataCenterId); diff --git a/server/src/com/cloud/upgrade/dao/Upgrade222to224.java b/server/src/com/cloud/upgrade/dao/Upgrade222to224.java index 18cff6b95d6..d780bd37a4e 100644 --- a/server/src/com/cloud/upgrade/dao/Upgrade222to224.java +++ b/server/src/com/cloud/upgrade/dao/Upgrade222to224.java @@ -24,15 +24,18 @@ import java.sql.ResultSet; import java.sql.SQLException; import java.util.HashMap; +import org.apache.log4j.Logger; + import com.cloud.capacity.Capacity; import com.cloud.utils.exception.CloudRuntimeException; import com.cloud.utils.script.Script; public class Upgrade222to224 implements DbUpgrade { + final static Logger s_logger = Logger.getLogger(Upgrade222to224.class); @Override public String[] getUpgradableVersionRange() { - return new String[] {"2.2.2", "2.2.3"}; + return new String[] { "2.2.2", "2.2.3" }; } @Override @@ -51,15 +54,16 @@ public class Upgrade222to224 implements DbUpgrade { if (script == null) { throw new CloudRuntimeException("Unable to find db/schema-222to224.sql"); } - + return new File[] { new File(script) }; } @Override public void performDataMigration(Connection conn) { - updateClusterIdInOpHostCapacity(conn); - updateGuestOsType(conn); - updateNicsWithMode(conn); + updateClusterIdInOpHostCapacity(conn); + updateGuestOsType(conn); + updateNicsWithMode(conn); + updateUserStatsWithNetwork(conn); } @Override @@ -68,10 +72,10 @@ public class Upgrade222to224 implements DbUpgrade { if (file == null) { throw new CloudRuntimeException("Unable to find the upgrade script, schema-222to224-cleanup.sql"); } - - return new File[] {new File(file)}; + + return new File[] { new File(file) }; } - + private void updateGuestOsType(Connection conn) { try { PreparedStatement pstmt = conn.prepareStatement("SELECT id FROM `cloud`.`guest_os` WHERE `display_name`='CentOS 5.3 (64-bit)'"); @@ -80,73 +84,73 @@ public class Upgrade222to224 implements DbUpgrade { if (rs.next()) { osId = rs.getLong(1); } - + if (osId != null) { pstmt = conn.prepareStatement("UPDATE `cloud`.`vm_template` SET `guest_os_id`=? WHERE id=2"); pstmt.setLong(1, osId); pstmt.executeUpdate(); } - - }catch (SQLException e) { + + } catch (SQLException e) { throw new CloudRuntimeException("Unable to update the guest os type for default template as a part of 222 to 224 upgrade", e); } } - private void updateClusterIdInOpHostCapacity(Connection conn){ + private void updateClusterIdInOpHostCapacity(Connection conn) { PreparedStatement pstmt = null; ResultSet rs = null; PreparedStatement pstmtUpdate = null; try { - //Host and Primary storage capacity types + // Host and Primary storage capacity types pstmt = conn.prepareStatement("SELECT host_id, capacity_type FROM op_host_capacity WHERE capacity_type IN (0,1,2,3)"); rs = pstmt.executeQuery(); while (rs.next()) { - long hostId = rs.getLong(1); - short capacityType = rs.getShort(2); - String updateSQLPrefix = "Update op_host_capacity set cluster_id = (select cluster_id from "; - String updateSQLSuffix = " where id = ? ) where host_id = ?"; - String tableName = "host"; - switch(capacityType){ - case Capacity.CAPACITY_TYPE_MEMORY: - case Capacity.CAPACITY_TYPE_CPU: - tableName = "host"; - break; - case Capacity.CAPACITY_TYPE_STORAGE: - case Capacity.CAPACITY_TYPE_STORAGE_ALLOCATED: - tableName = "storage_pool"; - break; - } - pstmtUpdate = conn.prepareStatement(updateSQLPrefix + tableName + updateSQLSuffix); - pstmtUpdate.setLong(1, hostId); - pstmtUpdate.setLong(2, hostId); - pstmtUpdate.executeUpdate(); - pstmtUpdate.close(); + long hostId = rs.getLong(1); + short capacityType = rs.getShort(2); + String updateSQLPrefix = "Update op_host_capacity set cluster_id = (select cluster_id from "; + String updateSQLSuffix = " where id = ? ) where host_id = ?"; + String tableName = "host"; + switch (capacityType) { + case Capacity.CAPACITY_TYPE_MEMORY: + case Capacity.CAPACITY_TYPE_CPU: + tableName = "host"; + break; + case Capacity.CAPACITY_TYPE_STORAGE: + case Capacity.CAPACITY_TYPE_STORAGE_ALLOCATED: + tableName = "storage_pool"; + break; + } + pstmtUpdate = conn.prepareStatement(updateSQLPrefix + tableName + updateSQLSuffix); + pstmtUpdate.setLong(1, hostId); + pstmtUpdate.setLong(2, hostId); + pstmtUpdate.executeUpdate(); + pstmtUpdate.close(); } } catch (SQLException e) { throw new CloudRuntimeException("Unable to update the cluster Ids in Op_Host_capacity table", e); - }finally{ - if(pstmtUpdate != null){ - try{ - pstmtUpdate.close(); - }catch (SQLException e) { + } finally { + if (pstmtUpdate != null) { + try { + pstmtUpdate.close(); + } catch (SQLException e) { } } - if(rs != null){ - try{ - rs.close(); - }catch (SQLException e) { + if (rs != null) { + try { + rs.close(); + } catch (SQLException e) { } - } - if(pstmt != null){ - try{ - pstmt.close(); - }catch (SQLException e) { + } + if (pstmt != null) { + try { + pstmt.close(); + } catch (SQLException e) { } } } } - + private void updateNicsWithMode(Connection conn) { try { HashMap nicNetworkMaps = new HashMap(); @@ -155,7 +159,7 @@ public class Upgrade222to224 implements DbUpgrade { if (rs.next()) { nicNetworkMaps.put(rs.getLong(1), rs.getLong(2)); } - + for (Long nic : nicNetworkMaps.keySet()) { pstmt = conn.prepareStatement("SELECT mode FROM networks WHERE id=?"); pstmt.setLong(1, nicNetworkMaps.get(nic)); @@ -170,9 +174,49 @@ public class Upgrade222to224 implements DbUpgrade { } rs.close(); pstmt.close(); - - }catch (SQLException e) { + + } catch (SQLException e) { throw new CloudRuntimeException("Unable to update the Mode field for nics as a part of 222 to 224 upgrade", e); } } + + private void updateUserStatsWithNetwork(Connection conn) { + try { + PreparedStatement pstmt = conn.prepareStatement("SELECT id, device_id FROM user_statistics WHERE network_id=0 or network_id is NULL"); + s_logger.debug("Query is " + pstmt); + ResultSet rs = pstmt.executeQuery(); + + while (rs.next()) { + Long id = rs.getLong(1); + Long instanceId = rs.getLong(2); + + pstmt = conn.prepareStatement("SELECT network_id FROM nics WHERE instance_id=? AND mode='Dhcp'"); + pstmt.setLong(1, instanceId); + s_logger.debug("Query is " + pstmt); + ResultSet rs1 = pstmt.executeQuery(); + + if (!rs1.next()) { + throw new CloudRuntimeException("Failed to update user_statistics table as a part of 222 to 224 upgrade: couldn't get network_id from nics table"); + } + + Long networkId = rs1.getLong(1); + + if (networkId != null) { + pstmt = conn.prepareStatement("UPDATE user_statistics SET network_id=? where id=?"); + pstmt.setLong(1, networkId); + pstmt.setLong(2, id); + pstmt.executeUpdate(); + } + + } + + s_logger.debug("Successfully update user_statistics table with network_ids as a part of 222 to 224 upgrade"); + rs.close(); + pstmt.close(); + + } catch (SQLException e) { + throw new CloudRuntimeException("Unable to update the Mode field for nics as a part of 222 to 224 upgrade", e); + } + } + }