Repository: cloudstack Updated Branches: refs/heads/4.5 23de431f9 -> 94e6b7587
CID-1256275 regression: resource leak in systemvm update code (cherry picked from commit 06d4458d0a9de5be7a7bf590678eb4b03989e9a1) Conflicts: engine/schema/src/com/cloud/upgrade/dao/Upgrade442to450.java Project: http://git-wip-us.apache.org/repos/asf/cloudstack/repo Commit: http://git-wip-us.apache.org/repos/asf/cloudstack/commit/94e6b758 Tree: http://git-wip-us.apache.org/repos/asf/cloudstack/tree/94e6b758 Diff: http://git-wip-us.apache.org/repos/asf/cloudstack/diff/94e6b758 Branch: refs/heads/4.5 Commit: 94e6b7587d83b4ac1a0af0953199e0f4bd9340e8 Parents: 23de431 Author: Daan Hoogland <d...@onecht.net> Authored: Mon Jan 12 20:40:01 2015 +0100 Committer: Daan Hoogland <d...@onecht.net> Committed: Mon Jan 12 20:51:51 2015 +0100 ---------------------------------------------------------------------- .../com/cloud/upgrade/dao/Upgrade442to450.java | 260 ++++++++++--------- 1 file changed, 131 insertions(+), 129 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/cloudstack/blob/94e6b758/engine/schema/src/com/cloud/upgrade/dao/Upgrade442to450.java ---------------------------------------------------------------------- diff --git a/engine/schema/src/com/cloud/upgrade/dao/Upgrade442to450.java b/engine/schema/src/com/cloud/upgrade/dao/Upgrade442to450.java index e8a517b..191e022 100644 --- a/engine/schema/src/com/cloud/upgrade/dao/Upgrade442to450.java +++ b/engine/schema/src/com/cloud/upgrade/dao/Upgrade442to450.java @@ -140,9 +140,6 @@ public class Upgrade442to450 implements DbUpgrade { } private void upgradeMemoryOfInternalLoadBalancervmOffering(Connection conn) { - PreparedStatement updatePstmt = null; - PreparedStatement selectPstmt = null; - ResultSet selectResultSet = null; int newRamSize = 256; //256MB long serviceOfferingId = 0; @@ -151,10 +148,9 @@ public class Upgrade442to450 implements DbUpgrade { * We should not update/modify any user-defined offering. */ - try { - selectPstmt = conn.prepareStatement("SELECT id FROM `cloud`.`service_offering` WHERE vm_type='internalloadbalancervm'"); - updatePstmt = conn.prepareStatement("UPDATE `cloud`.`service_offering` SET ram_size=? WHERE id=?"); - selectResultSet = selectPstmt.executeQuery(); + try (PreparedStatement selectPstmt = conn.prepareStatement("SELECT id FROM `cloud`.`service_offering` WHERE vm_type='internalloadbalancervm'"); + PreparedStatement updatePstmt = conn.prepareStatement("UPDATE `cloud`.`service_offering` SET ram_size=? WHERE id=?"); + ResultSet selectResultSet = selectPstmt.executeQuery()){ if(selectResultSet.next()) { serviceOfferingId = selectResultSet.getLong("id"); } @@ -164,19 +160,6 @@ public class Upgrade442to450 implements DbUpgrade { updatePstmt.executeUpdate(); } catch (SQLException e) { throw new CloudRuntimeException("Unable to upgrade ram_size of service offering for internal loadbalancer vm. ", e); - } finally { - try { - if (selectPstmt != null) { - selectPstmt.close(); - } - if (selectResultSet != null) { - selectResultSet.close(); - } - if (updatePstmt != null) { - updatePstmt.close(); - } - } catch (SQLException e) { - } } s_logger.debug("Done upgrading RAM for service offering of internal loadbalancer vm to " + newRamSize); } @@ -192,127 +175,146 @@ public class Upgrade442to450 implements DbUpgrade { } private void updateSystemVmTemplates(Connection conn) { - PreparedStatement pstmt = null; - ResultSet rs = null; s_logger.debug("Updating System Vm template IDs"); - try{ - //Get all hypervisors in use - Set<Hypervisor.HypervisorType> hypervisorsListInUse = new HashSet<Hypervisor.HypervisorType>(); - try { - pstmt = conn.prepareStatement("select distinct(hypervisor_type) from `cloud`.`cluster` where removed is null"); - rs = pstmt.executeQuery(); - while(rs.next()){ - switch (Hypervisor.HypervisorType.getType(rs.getString(1))) { - case XenServer: hypervisorsListInUse.add(Hypervisor.HypervisorType.XenServer); - break; - case KVM: hypervisorsListInUse.add(Hypervisor.HypervisorType.KVM); - break; - case VMware: hypervisorsListInUse.add(Hypervisor.HypervisorType.VMware); - break; - case Hyperv: hypervisorsListInUse.add(Hypervisor.HypervisorType.Hyperv); - break; - case LXC: hypervisorsListInUse.add(Hypervisor.HypervisorType.LXC); - break; - } + //Get all hypervisors in use + Set<Hypervisor.HypervisorType> hypervisorsListInUse = new HashSet<Hypervisor.HypervisorType>(); + try (PreparedStatement pstmt = conn.prepareStatement("select distinct(hypervisor_type) from `cloud`.`cluster` where removed is null"); + ResultSet rs = pstmt.executeQuery() + ) { + while(rs.next()){ + switch (Hypervisor.HypervisorType.getType(rs.getString(1))) { + case XenServer: hypervisorsListInUse.add(Hypervisor.HypervisorType.XenServer); + break; + case KVM: hypervisorsListInUse.add(Hypervisor.HypervisorType.KVM); + break; + case VMware: hypervisorsListInUse.add(Hypervisor.HypervisorType.VMware); + break; + case Hyperv: hypervisorsListInUse.add(Hypervisor.HypervisorType.Hyperv); + break; + case LXC: hypervisorsListInUse.add(Hypervisor.HypervisorType.LXC); + break; + default: // no action on cases Any, BareMetal, None, Ovm, Parralels, Simulator and VirtualBox: + break; } - } catch (SQLException e) { - throw new CloudRuntimeException("Error while listing hypervisors in use", e); } + } catch (SQLException e) { + s_logger.error("updateSystemVmTemplates:Exception while getting hypervisor types from clusters: "+e.getMessage()); + throw new CloudRuntimeException("updateSystemVmTemplates:Exception while getting hypervisor types from clusters", e); + } - Map<Hypervisor.HypervisorType, String> NewTemplateNameList = new HashMap<Hypervisor.HypervisorType, String>(){ - { put(Hypervisor.HypervisorType.XenServer, "systemvm-xenserver-4.5"); - put(Hypervisor.HypervisorType.VMware, "systemvm-vmware-4.5"); - put(Hypervisor.HypervisorType.KVM, "systemvm-kvm-4.5"); - put(Hypervisor.HypervisorType.LXC, "systemvm-lxc-4.5"); - put(Hypervisor.HypervisorType.Hyperv, "systemvm-hyperv-4.5"); - } - }; - - Map<Hypervisor.HypervisorType, String> routerTemplateConfigurationNames = new HashMap<Hypervisor.HypervisorType, String>(){ - { put(Hypervisor.HypervisorType.XenServer, "router.template.xen"); - put(Hypervisor.HypervisorType.VMware, "router.template.vmware"); - put(Hypervisor.HypervisorType.KVM, "router.template.kvm"); - put(Hypervisor.HypervisorType.LXC, "router.template.lxc"); - put(Hypervisor.HypervisorType.Hyperv, "router.template.hyperv"); - } - }; - - Map<Hypervisor.HypervisorType, String> newTemplateUrl = new HashMap<Hypervisor.HypervisorType, String>(){ - { put(Hypervisor.HypervisorType.XenServer, "http://download.cloud.com/templates/4.5/systemvm64template-4.5-xen.vhd.bz2"); - put(Hypervisor.HypervisorType.VMware, "http://download.cloud.com/templates/4.5/systemvm64template-4.5-vmware.ova"); - put(Hypervisor.HypervisorType.KVM, "http://download.cloud.com/templates/4.5/systemvm64template-4.5-kvm.qcow2.bz2"); - put(Hypervisor.HypervisorType.LXC, "http://download.cloud.com/templates/4.5/systemvm64template-4.5-kvm.qcow2.bz2"); - put(Hypervisor.HypervisorType.Hyperv, "http://download.cloud.com/templates/4.5/systemvm64template-4.5-hyperv.vhd.zip"); - } - }; - - Map<Hypervisor.HypervisorType, String> newTemplateChecksum = new HashMap<Hypervisor.HypervisorType, String>(){ - { put(Hypervisor.HypervisorType.XenServer, "2b15ab4401c2d655264732d3fc600241"); - put(Hypervisor.HypervisorType.VMware, "3106a79a4ce66cd7f6a7c50e93f2db57"); - put(Hypervisor.HypervisorType.KVM, "aa9f501fecd3de1daeb9e2f357f6f002"); - put(Hypervisor.HypervisorType.LXC, "aa9f501fecd3de1daeb9e2f357f6f002"); - put(Hypervisor.HypervisorType.Hyperv, "70bd30ea02ee9ed67d2c6b85c179cee9"); - } - }; - - for (Map.Entry<Hypervisor.HypervisorType, String> hypervisorAndTemplateName : NewTemplateNameList.entrySet()){ - s_logger.debug("Updating " + hypervisorAndTemplateName.getKey() + " System Vms"); - try { - //Get 4.5.0 system Vm template Id for corresponding hypervisor - pstmt = conn.prepareStatement("select id from `cloud`.`vm_template` where name = ? and removed is null order by id desc limit 1"); - pstmt.setString(1, hypervisorAndTemplateName.getValue()); - rs = pstmt.executeQuery(); + Map<Hypervisor.HypervisorType, String> NewTemplateNameList = new HashMap<Hypervisor.HypervisorType, String>() { + { + put(Hypervisor.HypervisorType.XenServer, "systemvm-xenserver-4.5"); + put(Hypervisor.HypervisorType.VMware, "systemvm-vmware-4.5"); + put(Hypervisor.HypervisorType.KVM, "systemvm-kvm-4.5"); + put(Hypervisor.HypervisorType.LXC, "systemvm-lxc-4.5"); + put(Hypervisor.HypervisorType.Hyperv, "systemvm-hyperv-4.5"); + } + }; + + Map<Hypervisor.HypervisorType, String> routerTemplateConfigurationNames = new HashMap<Hypervisor.HypervisorType, String>() { + { + put(Hypervisor.HypervisorType.XenServer, "router.template.xen"); + put(Hypervisor.HypervisorType.VMware, "router.template.vmware"); + put(Hypervisor.HypervisorType.KVM, "router.template.kvm"); + put(Hypervisor.HypervisorType.LXC, "router.template.lxc"); + put(Hypervisor.HypervisorType.Hyperv, "router.template.hyperv"); + } + }; + + Map<Hypervisor.HypervisorType, String> newTemplateUrl = new HashMap<Hypervisor.HypervisorType, String>() { + { + put(Hypervisor.HypervisorType.XenServer, "http://download.cloud.com/templates/4.5/systemvm64template-4.5-xen.vhd.bz2"); + put(Hypervisor.HypervisorType.VMware, "http://download.cloud.com/templates/4.5/systemvm64template-4.5-vmware.ova"); + put(Hypervisor.HypervisorType.KVM, "http://download.cloud.com/templates/4.5/systemvm64template-4.5-kvm.qcow2.bz2"); + put(Hypervisor.HypervisorType.LXC, "http://download.cloud.com/templates/4.5/systemvm64template-4.5-kvm.qcow2.bz2"); + put(Hypervisor.HypervisorType.Hyperv, "http://download.cloud.com/templates/4.5/systemvm64template-4.5-hyperv.vhd.zip"); + } + }; + + Map<Hypervisor.HypervisorType, String> newTemplateChecksum = new HashMap<Hypervisor.HypervisorType, String>() { + { + put(Hypervisor.HypervisorType.XenServer, "2b15ab4401c2d655264732d3fc600241"); + put(Hypervisor.HypervisorType.VMware, "3106a79a4ce66cd7f6a7c50e93f2db57"); + put(Hypervisor.HypervisorType.KVM, "aa9f501fecd3de1daeb9e2f357f6f002"); + put(Hypervisor.HypervisorType.LXC, "aa9f501fecd3de1daeb9e2f357f6f002"); + put(Hypervisor.HypervisorType.Hyperv, "70bd30ea02ee9ed67d2c6b85c179cee9"); + } + }; + + for (Map.Entry<Hypervisor.HypervisorType, String> hypervisorAndTemplateName : NewTemplateNameList.entrySet()) { + s_logger.debug("Updating " + hypervisorAndTemplateName.getKey() + " System Vms"); + try (PreparedStatement pstmt = conn.prepareStatement("select id from `cloud`.`vm_template` where name = ? and removed is null order by id desc limit 1")) { + //Get 4.5.0 system Vm template Id for corresponding hypervisor + long templateId = -1; + pstmt.setString(1, hypervisorAndTemplateName.getValue()); + try (ResultSet rs = pstmt.executeQuery()) { if(rs.next()){ - long templateId = rs.getLong(1); - rs.close(); - pstmt.close(); - pstmt = conn.prepareStatement("update `cloud`.`vm_template` set type='SYSTEM' where id = ?"); - pstmt.setLong(1, templateId); - pstmt.executeUpdate(); - pstmt.close(); - // update templete ID of system Vms - pstmt = conn.prepareStatement("update `cloud`.`vm_instance` set vm_template_id = ? where type <> 'User' and hypervisor_type = ?"); - pstmt.setLong(1, templateId); - pstmt.setString(2, hypervisorAndTemplateName.getKey().toString()); - pstmt.executeUpdate(); - pstmt.close(); - // Change value of global configuration parameter router.template.* for the corresponding hypervisor - pstmt = conn.prepareStatement("UPDATE `cloud`.`configuration` SET value = ? WHERE name = ?"); - pstmt.setString(1, hypervisorAndTemplateName.getValue()); - pstmt.setString(2, routerTemplateConfigurationNames.get(hypervisorAndTemplateName.getKey())); - pstmt.executeUpdate(); - pstmt.close(); - } else { - if (hypervisorsListInUse.contains(hypervisorAndTemplateName.getKey())){ - throw new CloudRuntimeException("4.5.0 " + hypervisorAndTemplateName.getKey() + " SystemVm template not found. Cannot upgrade system Vms"); - } else { - s_logger.warn("4.5.0 " + hypervisorAndTemplateName.getKey() + " SystemVm template not found. " + hypervisorAndTemplateName.getKey() + " hypervisor is not used, so not failing upgrade"); - // Update the latest template URLs for corresponding hypervisor - pstmt = conn.prepareStatement("UPDATE `cloud`.`vm_template` SET url = ? , checksum = ? WHERE hypervisor_type = ? AND type = 'SYSTEM' AND removed is null order by id desc limit 1"); - pstmt.setString(1, newTemplateUrl.get(hypervisorAndTemplateName.getKey())); - pstmt.setString(2, newTemplateChecksum.get(hypervisorAndTemplateName.getKey())); - pstmt.setString(3, hypervisorAndTemplateName.getKey().toString()); - pstmt.executeUpdate(); - pstmt.close(); - } + templateId = rs.getLong(1); } - } catch (SQLException e) { - throw new CloudRuntimeException("Error while updating "+ hypervisorAndTemplateName.getKey() +" systemVm template", e); - } - } - s_logger.debug("Updating System Vm Template IDs Complete"); - } finally { - try { - if (rs != null) { - rs.close(); + } catch (SQLException e) + { + s_logger.error("updateSystemVmTemplates:Exception while getting ids of templates: "+e.getMessage()); + throw new CloudRuntimeException("updateSystemVmTemplates:Exception while getting ids of templates", e); } - if (pstmt != null) { - pstmt.close(); + // change template type to SYSTEM + if (templateId != -1) { + try(PreparedStatement templ_type_pstmt = conn.prepareStatement("update `cloud`.`vm_template` set type='SYSTEM' where id = ?");) + { + templ_type_pstmt.setLong(1, templateId); + templ_type_pstmt.executeUpdate(); + } + catch (SQLException e) + { + s_logger.error("updateSystemVmTemplates:Exception while updating template with id " + templateId + " to be marked as 'system': "+e.getMessage()); + throw new CloudRuntimeException("updateSystemVmTemplates:Exception while updating template with id " + templateId + " to be marked as 'system'", e); + } + // update template ID of system Vms + try(PreparedStatement update_templ_id_pstmt = conn.prepareStatement("update `cloud`.`vm_instance` set vm_template_id = ? where type <> 'User' and hypervisor_type = ?");) + { + update_templ_id_pstmt.setLong(1, templateId); + update_templ_id_pstmt.setString(2, hypervisorAndTemplateName.getKey().toString()); + update_templ_id_pstmt.executeUpdate(); + }catch (Exception e) + { + s_logger.error("updateSystemVmTemplates:Exception while setting template for " + hypervisorAndTemplateName.getKey().toString() + " to " + templateId + ": "+e.getMessage()); + throw new CloudRuntimeException("updateSystemVmTemplates:Exception while setting template for " + hypervisorAndTemplateName.getKey().toString() + " to " + templateId, e); + } + // Change value of global configuration parameter router.template.* for the corresponding hypervisor + try(PreparedStatement update_pstmt = conn.prepareStatement("UPDATE `cloud`.`configuration` SET value = ? WHERE name = ?");) { + update_pstmt.setString(1, hypervisorAndTemplateName.getValue()); + update_pstmt.setString(2, routerTemplateConfigurationNames.get(hypervisorAndTemplateName.getKey())); + update_pstmt.executeUpdate(); + }catch (SQLException e) + { + s_logger.error("updateSystemVmTemplates:Exception while setting " + routerTemplateConfigurationNames.get(hypervisorAndTemplateName.getKey()) + " to " + hypervisorAndTemplateName.getValue() + ": "+e.getMessage()); + throw new CloudRuntimeException("updateSystemVmTemplates:Exception while setting " + routerTemplateConfigurationNames.get(hypervisorAndTemplateName.getKey()) + " to " + hypervisorAndTemplateName.getValue(), e); + } + } else { + if (hypervisorsListInUse.contains(hypervisorAndTemplateName.getKey())){ + throw new CloudRuntimeException("4.5.0 " + hypervisorAndTemplateName.getKey() + " SystemVm template not found. Cannot upgrade system Vms"); + } else { + s_logger.warn("4.5.0 " + hypervisorAndTemplateName.getKey() + " SystemVm template not found. " + hypervisorAndTemplateName.getKey() + " hypervisor is not used, so not failing upgrade"); + // Update the latest template URLs for corresponding hypervisor + try(PreparedStatement update_templ_url_pstmt = conn.prepareStatement("UPDATE `cloud`.`vm_template` SET url = ? , checksum = ? WHERE hypervisor_type = ? AND type = 'SYSTEM' AND removed is null order by id desc limit 1");) { + update_templ_url_pstmt.setString(1, newTemplateUrl.get(hypervisorAndTemplateName.getKey())); + update_templ_url_pstmt.setString(2, newTemplateChecksum.get(hypervisorAndTemplateName.getKey())); + update_templ_url_pstmt.setString(3, hypervisorAndTemplateName.getKey().toString()); + update_templ_url_pstmt.executeUpdate(); + }catch (SQLException e) + { + s_logger.error("updateSystemVmTemplates:Exception while updating 'url' and 'checksum' for hypervisor type " + hypervisorAndTemplateName.getKey().toString() + ": "+e.getMessage()); + throw new CloudRuntimeException("updateSystemVmTemplates:Exception while updating 'url' and 'checksum' for hypervisor type " + hypervisorAndTemplateName.getKey().toString(), e); + } + } } } catch (SQLException e) { + s_logger.error("updateSystemVmTemplates:Exception while getting ids of templates: "+e.getMessage()); + throw new CloudRuntimeException("updateSystemVmTemplates:Exception while getting ids of templates", e); } } + s_logger.debug("Updating System Vm Template IDs Complete"); }