diff --git a/server/src/com/cloud/network/NetworkManagerImpl.java b/server/src/com/cloud/network/NetworkManagerImpl.java index 2490abc6fcd..8ea4c001ac2 100755 --- a/server/src/com/cloud/network/NetworkManagerImpl.java +++ b/server/src/com/cloud/network/NetworkManagerImpl.java @@ -1212,6 +1212,7 @@ public class NetworkManagerImpl implements NetworkManager, NetworkService, Manag profile = new NicProfile(nic, network, broadcastUri, isolationUri, networkRate); guru.reserve(profile, network, vmProfile, dest, context); nic.setIp4Address(profile.getIp4Address()); + nic.setAddressFormat(profile.getFormat()); nic.setIp6Address(profile.getIp6Address()); nic.setMacAddress(profile.getMacAddress()); nic.setIsolationUri(profile.getIsolationUri()); @@ -1220,7 +1221,7 @@ public class NetworkManagerImpl implements NetworkManager, NetworkService, Manag nic.setState(Nic.State.Reserved); nic.setNetmask(profile.getNetmask()); nic.setGateway(profile.getGateway()); - nic.setAddressFormat(profile.getFormat()); + if (profile.getStrategy() != null) { nic.setReservationStrategy(profile.getStrategy()); } diff --git a/server/src/com/cloud/network/guru/DirectPodBasedNetworkGuru.java b/server/src/com/cloud/network/guru/DirectPodBasedNetworkGuru.java index 6acf6236bf6..6284ce6f2c4 100644 --- a/server/src/com/cloud/network/guru/DirectPodBasedNetworkGuru.java +++ b/server/src/com/cloud/network/guru/DirectPodBasedNetworkGuru.java @@ -122,7 +122,6 @@ public class DirectPodBasedNetworkGuru extends DirectNetworkGuru { nic.setBroadcastUri(URI.create("vlan://" + Vlan.UNTAGGED)); nic.setBroadcastType(BroadcastDomainType.Native); } - nic.setFormat(AddressFormat.Ip4); nic.setReservationId(String.valueOf(ip.getVlanTag())); nic.setMacAddress(ip.getMacAddress()); } diff --git a/server/src/com/cloud/upgrade/dao/Upgrade218to22.java b/server/src/com/cloud/upgrade/dao/Upgrade218to22.java index 2f8d8f44a96..b2607cba6a1 100644 --- a/server/src/com/cloud/upgrade/dao/Upgrade218to22.java +++ b/server/src/com/cloud/upgrade/dao/Upgrade218to22.java @@ -29,16 +29,21 @@ import java.sql.Types; import java.util.ArrayList; import java.util.HashMap; import java.util.List; +import java.util.Map; import java.util.Properties; import java.util.TimeZone; import java.util.UUID; import org.apache.log4j.Logger; +import com.cloud.consoleproxy.ConsoleProxyManager; import com.cloud.event.EventTypes; import com.cloud.event.EventVO; import com.cloud.event.UsageEventVO; +import com.cloud.network.router.VirtualNetworkApplianceManager; +import com.cloud.storage.secondary.SecondaryStorageVmManager; import com.cloud.utils.DateUtil; +import com.cloud.utils.NumbersUtil; import com.cloud.utils.exception.CloudRuntimeException; import com.cloud.utils.net.NetUtils; import com.cloud.utils.script.Script; @@ -258,7 +263,7 @@ public class Upgrade218to22 implements DbUpgrade { pstmt.close(); } - insertNic(conn, guestNetworkId, domrId, running, guestMac, guestIp, guestNetmask, "Start", gateway, vnet, "DirectPodBasedNetworkGuru", true, 0, "Dhcp", null); + insertNic(conn, guestNetworkId, domrId, running, guestMac, guestIp, guestNetmask, "Create", gateway, vnet, "DirectPodBasedNetworkGuru", true, 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", @@ -332,7 +337,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, "Dhcp", null); + insertNic(conn, publicNetworkId, ssvmId, running, publicMac, publicIp, publicNetmask, "Create", gateway, publicVlan, "DirectPodBasedNetworkGuru", true, 2, "Dhcp", null); } else { insertNic(conn, publicNetworkId, ssvmId, running, publicMac, publicIp, publicNetmask, "Create", gateway, publicVlan, "PublicNetworkGuru", true, 2, "Static", null); @@ -413,7 +418,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, "Dhcp", null); + insertNic(conn, publicNetworkId, cpId, running, publicMac, publicIp, publicNetmask, "Create", gateway, publicVlan, "DirectPodBasedNetworkGuru", true, 2, "Dhcp", null); } else { insertNic(conn, publicNetworkId, cpId, running, publicMac, publicIp, publicNetmask, "Create", gateway, publicVlan, "PublicNetworkGuru", true, 2, "Static", null); } @@ -530,7 +535,7 @@ public class Upgrade218to22 implements DbUpgrade { count++; } } - insertNic(conn, networkId, (Long) vm[0], running, (String) vm[1], (String) vm[2], (String) vm[3], "Start", gateway, vnet, "DirectPodBasedNetworkGuru", true, 0, "Dhcp", null); + insertNic(conn, networkId, (Long) vm[0], running, (String) vm[1], (String) vm[2], (String) vm[3], "Create", gateway, vnet, "DirectPodBasedNetworkGuru", true, 0, "Dhcp", null); } pstmt = conn.prepareStatement("SELECT state FROM vm_instance WHERE id=?"); @@ -1377,6 +1382,172 @@ public class Upgrade218to22 implements DbUpgrade { } } + private void upgradeHostMemoryCapacityInfo(Connection conn) { + try { + // count user_vm memory info (M Bytes) + PreparedStatement pstmt = conn + .prepareStatement("select h.id, sum(s.ram_size) from host h, vm_instance v, service_offering s where h.type='Routing' and v.state='Running' and v.`type`='User' and v.host_id=h.id and v.service_offering_id = s.id group by h.id"); + + ResultSet rs = pstmt.executeQuery(); + Map hostUsedMemoryInfo = new HashMap(); + while (rs.next()) { + hostUsedMemoryInfo.put(rs.getLong(1), rs.getLong(2)); + } + rs.close(); + pstmt.close(); + + int proxyRamSize = NumbersUtil.parseInt(getConfigValue(conn, "consoleproxy.ram.size"), ConsoleProxyManager.DEFAULT_PROXY_VM_RAMSIZE); + int domrRamSize = NumbersUtil.parseInt(getConfigValue(conn, "router.ram.size"), VirtualNetworkApplianceManager.DEFAULT_ROUTER_VM_RAMSIZE); + int ssvmRamSize = NumbersUtil.parseInt(getConfigValue(conn, "secstorage.vm.ram.size"), SecondaryStorageVmManager.DEFAULT_SS_VM_RAMSIZE); + + pstmt = conn + .prepareStatement("select h.id, count(v.id) from host h, vm_instance v where h.type='Routing' and v.state='Running' and v.`type`='ConsoleProxy' and v.host_id=h.id group by h.id"); + rs = pstmt.executeQuery(); + while (rs.next()) { + if (hostUsedMemoryInfo.get(rs.getLong(1)) != null) { + Long usedMem = hostUsedMemoryInfo.get(rs.getLong(1)); + hostUsedMemoryInfo.put(rs.getLong(1), rs.getLong(2) * proxyRamSize + usedMem); + } else { + hostUsedMemoryInfo.put(rs.getLong(1), rs.getLong(2) * proxyRamSize); + } + } + rs.close(); + pstmt.close(); + + pstmt = conn + .prepareStatement("select h.id, count(v.id) from host h, vm_instance v where h.type='Routing' and v.state='Running' and v.`type`='DomainRouter' and v.host_id=h.id group by h.id"); + rs = pstmt.executeQuery(); + while (rs.next()) { + if (hostUsedMemoryInfo.get(rs.getLong(1)) != null) { + Long usedMem = hostUsedMemoryInfo.get(rs.getLong(1)); + hostUsedMemoryInfo.put(rs.getLong(1), rs.getLong(2) * domrRamSize + usedMem); + } else { + hostUsedMemoryInfo.put(rs.getLong(1), rs.getLong(2) * domrRamSize); + } + } + rs.close(); + pstmt.close(); + + pstmt = conn + .prepareStatement("select h.id, count(v.id) from host h, vm_instance v where h.type='Routing' and v.state='Running' and v.`type`='SecondaryStorageVm' and v.host_id=h.id group by h.id"); + rs = pstmt.executeQuery(); + while (rs.next()) { + if (hostUsedMemoryInfo.get(rs.getLong(1)) != null) { + Long usedMem = hostUsedMemoryInfo.get(rs.getLong(1)); + hostUsedMemoryInfo.put(rs.getLong(1), rs.getLong(2) * ssvmRamSize + usedMem); + } else { + hostUsedMemoryInfo.put(rs.getLong(1), rs.getLong(2) * ssvmRamSize); + } + } + rs.close(); + pstmt.close(); + + for (Map.Entry entry : hostUsedMemoryInfo.entrySet()) { + pstmt = conn.prepareStatement("update op_host_capacity set used_capacity=? where host_id=? and capacity_type=0"); + pstmt.setLong(1, entry.getValue() * 1024 * 1024); + pstmt.setLong(2, entry.getKey()); + + pstmt.executeUpdate(); + } + + } catch (SQLException e) { + throw new CloudRuntimeException("Can't upgrade host capacity info ", e); + } + } + + private void upgradeHostCpuCapacityInfo(Connection conn) { + try { + // count user_vm memory info (M Bytes) + PreparedStatement pstmt = conn + .prepareStatement("select h.id, sum(s.speed*s.cpu) from host h, vm_instance v, service_offering s where h.type='Routing' and v.state='Running' and v.`type`='User' and v.host_id=h.id and v.service_offering_id = s.id group by h.id"); + + ResultSet rs = pstmt.executeQuery(); + Map hostUsedCpuInfo = new HashMap(); + while (rs.next()) { + hostUsedCpuInfo.put(rs.getLong(1), rs.getLong(2)); + } + rs.close(); + pstmt.close(); + + int proxyCpuMhz = NumbersUtil.parseInt(getConfigValue(conn, "consoleproxy.cpu.mhz"), ConsoleProxyManager.DEFAULT_PROXY_VM_CPUMHZ); + int domrCpuMhz = NumbersUtil.parseInt(getConfigValue(conn, "router.cpu.mhz"), VirtualNetworkApplianceManager.DEFAULT_ROUTER_CPU_MHZ); + int ssvmCpuMhz = NumbersUtil.parseInt(getConfigValue(conn, "secstorage.vm.cpu.mhz"), SecondaryStorageVmManager.DEFAULT_SS_VM_CPUMHZ); + + pstmt = conn + .prepareStatement("select h.id, count(v.id) from host h, vm_instance v where h.type='Routing' and v.state='Running' and v.`type`='ConsoleProxy' and v.host_id=h.id group by h.id"); + + rs = pstmt.executeQuery(); + while (rs.next()) { + if (hostUsedCpuInfo.get(rs.getLong(1)) != null) { + Long usedCpuMhz = hostUsedCpuInfo.get(rs.getLong(1)); + hostUsedCpuInfo.put(rs.getLong(1), rs.getLong(2) * proxyCpuMhz + usedCpuMhz); + } else { + hostUsedCpuInfo.put(rs.getLong(1), rs.getLong(2) * proxyCpuMhz); + } + } + rs.close(); + pstmt.close(); + + pstmt = conn + .prepareStatement("select h.id, count(v.id) from host h, vm_instance v where h.type='Routing' and v.state='Running' and v.`type`='DomainRouter' and v.host_id=h.id group by h.id"); + rs = pstmt.executeQuery(); + while (rs.next()) { + if (hostUsedCpuInfo.get(rs.getLong(1)) != null) { + Long usedCpuMhz = hostUsedCpuInfo.get(rs.getLong(1)); + hostUsedCpuInfo.put(rs.getLong(1), rs.getLong(2) * domrCpuMhz + usedCpuMhz); + } else { + hostUsedCpuInfo.put(rs.getLong(1), rs.getLong(2) * domrCpuMhz); + } + } + rs.close(); + pstmt.close(); + + pstmt = conn + .prepareStatement("select h.id, count(v.id) from host h, vm_instance v where h.type='Routing' and v.state='Running' and v.`type`='SecondaryStorageVm' and v.host_id=h.id group by h.id"); + rs = pstmt.executeQuery(); + while (rs.next()) { + if (hostUsedCpuInfo.get(rs.getLong(1)) != null) { + Long usedCpuMhz = hostUsedCpuInfo.get(rs.getLong(1)); + hostUsedCpuInfo.put(rs.getLong(1), rs.getLong(2) * ssvmCpuMhz + usedCpuMhz); + } else { + hostUsedCpuInfo.put(rs.getLong(1), rs.getLong(2) * ssvmCpuMhz); + } + } + rs.close(); + pstmt.close(); + + for (Map.Entry entry : hostUsedCpuInfo.entrySet()) { + pstmt = conn.prepareStatement("update op_host_capacity set used_capacity=? where host_id=? and capacity_type=1"); + pstmt.setLong(1, entry.getValue()); + pstmt.setLong(2, entry.getKey()); + + pstmt.executeUpdate(); + } + } catch (SQLException e) { + throw new CloudRuntimeException("Can't upgrade host capacity info ", e); + } + } + + private String getConfigValue(Connection conn, String name) { + try { + // count user_vm memory info (M Bytes) + PreparedStatement pstmt = conn.prepareStatement("select value from configuration where name=?"); + pstmt.setString(1, name); + ResultSet rs = pstmt.executeQuery(); + + String val = null; + if (rs.next()) { + val = rs.getString(1); + } + rs.close(); + pstmt.close(); + + return val; + } catch (SQLException e) { + throw new CloudRuntimeException("Can't upgrade host capacity info ", e); + } + } + private void migrateEvents(Connection conn) { try { PreparedStatement pstmt1 = conn.prepareStatement("SHOW DATABASES LIKE 'cloud_usage'"); @@ -1733,6 +1904,8 @@ public class Upgrade218to22 implements DbUpgrade { upgradeInstanceGroups(conn); upgradePortForwardingRules(conn); upgradeLoadBalancingRules(conn); + upgradeHostMemoryCapacityInfo(conn); + upgradeHostCpuCapacityInfo(conn); migrateEvents(conn); // Update hypervisor type for user vm to be consistent with original 2.2.4 pstmt = conn.prepareStatement("UPDATE vm_instance SET hypervisor_type='XenServer' WHERE hypervisor_type='xenserver'"); diff --git a/server/test/com/cloud/upgrade/Sanity220To224UpgradeTest.java b/server/test/com/cloud/upgrade/Sanity220To224UpgradeTest.java new file mode 100644 index 00000000000..2fa2436662e --- /dev/null +++ b/server/test/com/cloud/upgrade/Sanity220To224UpgradeTest.java @@ -0,0 +1,94 @@ +/** + * Copyright (C) 2010 Cloud.com, Inc. All rights reserved. + * + * This software is licensed under the GNU General Public License v3 or later. + * + * It is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or any later version. + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + * + */ +package com.cloud.upgrade; + +import java.sql.Connection; +import java.sql.PreparedStatement; +import java.sql.ResultSet; +import java.sql.SQLException; + +import junit.framework.TestCase; + +import org.apache.log4j.Logger; +import org.junit.After; +import org.junit.Before; + +import com.cloud.upgrade.dao.VersionDaoImpl; +import com.cloud.utils.component.ComponentLocator; +import com.cloud.utils.db.DbTestUtils; +import com.cloud.utils.db.Transaction; + +public class Sanity220To224UpgradeTest extends TestCase { + private static final Logger s_logger = Logger.getLogger(Sanity220To224UpgradeTest.class); + + @Override + @Before + public void setUp() throws Exception { + DbTestUtils.executeScript("PreviousDatabaseSchema/clean-db.sql", false, true); + } + + @Override + @After + public void tearDown() throws Exception { + } + + public void test217to22Upgrade() throws SQLException { + s_logger.debug("Finding sample data from 2.2.1"); + DbTestUtils.executeScript("/home/alena/Downloads/before/cloud_report_dump_220_April_26th_1521hrs.sql", false, true); + + Connection conn; + PreparedStatement pstmt; + ResultSet rs; + + VersionDaoImpl dao = ComponentLocator.inject(VersionDaoImpl.class); + DatabaseUpgradeChecker checker = ComponentLocator.inject(DatabaseUpgradeChecker.class); + + String version = dao.getCurrentVersion(); + + if (!version.equals("2.2.1")) { + s_logger.error("Version returned is not 2.2.1 but " + version); + } else { + s_logger.debug("Sanity 2.2.1 to 2.2.4 test version is " + version); + } + + checker.upgrade("2.2.1", "2.2.4"); + + conn = Transaction.getStandaloneConnection(); + try { + s_logger.debug("Starting tesing upgrade from 2.2.1 to 2.2.4..."); + + // Version check + pstmt = conn.prepareStatement("SELECT version FROM version"); + rs = pstmt.executeQuery(); + + if (!rs.next()) { + s_logger.error("ERROR: No version selected"); + } else if (!rs.getString(1).equals("2.2.4")) { + s_logger.error("ERROR: VERSION stored is not 2.2.4: " + rs.getString(1)); + } + rs.close(); + pstmt.close(); + + s_logger.debug("Sanity 2.2.1 to 2.2.4 DB upgrade test passed"); + + } finally { + conn.close(); + } + } + +} diff --git a/setup/db/db/schema-222to224.sql b/setup/db/db/schema-222to224.sql index f6edb325e32..96567e61704 100644 --- a/setup/db/db/schema-222to224.sql +++ b/setup/db/db/schema-222to224.sql @@ -130,6 +130,9 @@ UPDATE `cloud`.`service_offering` s, `cloud`.`disk_offering` d SET s.ha_enabled= UPDATE `cloud`.`data_center` SET is_security_group_enabled=1 WHERE networktype='Basic'; +UPDATE `cloud`.`host` SET resource='com.cloud.hypervisor.xen.resource.XenServer56FP1Resource' WHERE resource='com.cloud.hypervisor.xen.resource.XenServer56FP1PremiumResource'; + UPDATE `cloud`.`nics` SET ip_type='Ip4'; +UPDATE `cloud`.`nics` SET broadcast_uri='vlan://untagged', isolation_uri='ec2://untagged', strategy='Create' where reserver_name='DirectPodBasedNetworkGuru';