Skip to content

Commit 06d4458

Browse files
committed
CID-1256275 regression: resource leak in systemvm update code
1 parent 9239f93 commit 06d4458

File tree

1 file changed

+128
-114
lines changed

1 file changed

+128
-114
lines changed

engine/schema/src/com/cloud/upgrade/dao/Upgrade442to450.java

Lines changed: 128 additions & 114 deletions
Original file line numberDiff line numberDiff line change
@@ -175,132 +175,146 @@ public File[] getCleanupScripts() {
175175
}
176176

177177
private void updateSystemVmTemplates(Connection conn) {
178-
PreparedStatement pstmt = null;
179-
ResultSet rs = null;
180178
s_logger.debug("Updating System Vm template IDs");
181-
try{
182-
//Get all hypervisors in use
183-
Set<Hypervisor.HypervisorType> hypervisorsListInUse = new HashSet<Hypervisor.HypervisorType>();
184-
try {
185-
pstmt = conn.prepareStatement("select distinct(hypervisor_type) from `cloud`.`cluster` where removed is null");
186-
rs = pstmt.executeQuery();
187-
while(rs.next()){
188-
switch (Hypervisor.HypervisorType.getType(rs.getString(1))) {
189-
case XenServer: hypervisorsListInUse.add(Hypervisor.HypervisorType.XenServer);
190-
break;
191-
case KVM: hypervisorsListInUse.add(Hypervisor.HypervisorType.KVM);
192-
break;
193-
case VMware: hypervisorsListInUse.add(Hypervisor.HypervisorType.VMware);
194-
break;
195-
case Hyperv: hypervisorsListInUse.add(Hypervisor.HypervisorType.Hyperv);
196-
break;
197-
case LXC: hypervisorsListInUse.add(Hypervisor.HypervisorType.LXC);
198-
break;
199-
default: // no action on cases Any, BareMetal, None, Ovm, Parralels, Simulator and VirtualBox:
200-
break;
201-
}
179+
//Get all hypervisors in use
180+
Set<Hypervisor.HypervisorType> hypervisorsListInUse = new HashSet<Hypervisor.HypervisorType>();
181+
try (PreparedStatement pstmt = conn.prepareStatement("select distinct(hypervisor_type) from `cloud`.`cluster` where removed is null");
182+
ResultSet rs = pstmt.executeQuery()
183+
) {
184+
while(rs.next()){
185+
switch (Hypervisor.HypervisorType.getType(rs.getString(1))) {
186+
case XenServer: hypervisorsListInUse.add(Hypervisor.HypervisorType.XenServer);
187+
break;
188+
case KVM: hypervisorsListInUse.add(Hypervisor.HypervisorType.KVM);
189+
break;
190+
case VMware: hypervisorsListInUse.add(Hypervisor.HypervisorType.VMware);
191+
break;
192+
case Hyperv: hypervisorsListInUse.add(Hypervisor.HypervisorType.Hyperv);
193+
break;
194+
case LXC: hypervisorsListInUse.add(Hypervisor.HypervisorType.LXC);
195+
break;
196+
default: // no action on cases Any, BareMetal, None, Ovm, Parralels, Simulator and VirtualBox:
197+
break;
202198
}
203-
} catch (SQLException e) {
204-
throw new CloudRuntimeException("Error while listing hypervisors in use", e);
205199
}
200+
} catch (SQLException e) {
201+
s_logger.error("updateSystemVmTemplates:Exception while getting hypervisor types from clusters: "+e.getMessage());
202+
throw new CloudRuntimeException("updateSystemVmTemplates:Exception while getting hypervisor types from clusters", e);
203+
}
206204

207-
Map<Hypervisor.HypervisorType, String> NewTemplateNameList = new HashMap<Hypervisor.HypervisorType, String>(){
208-
{ put(Hypervisor.HypervisorType.XenServer, "systemvm-xenserver-4.5");
209-
put(Hypervisor.HypervisorType.VMware, "systemvm-vmware-4.5");
210-
put(Hypervisor.HypervisorType.KVM, "systemvm-kvm-4.5");
211-
put(Hypervisor.HypervisorType.LXC, "systemvm-lxc-4.5");
212-
put(Hypervisor.HypervisorType.Hyperv, "systemvm-hyperv-4.5");
213-
}
214-
};
215-
216-
Map<Hypervisor.HypervisorType, String> routerTemplateConfigurationNames = new HashMap<Hypervisor.HypervisorType, String>(){
217-
{ put(Hypervisor.HypervisorType.XenServer, "router.template.xen");
218-
put(Hypervisor.HypervisorType.VMware, "router.template.vmware");
219-
put(Hypervisor.HypervisorType.KVM, "router.template.kvm");
220-
put(Hypervisor.HypervisorType.LXC, "router.template.lxc");
221-
put(Hypervisor.HypervisorType.Hyperv, "router.template.hyperv");
222-
}
223-
};
224-
225-
Map<Hypervisor.HypervisorType, String> newTemplateUrl = new HashMap<Hypervisor.HypervisorType, String>(){
226-
{ put(Hypervisor.HypervisorType.XenServer, "http://download.cloud.com/templates/4.5/systemvm64template-4.5-xen.vhd.bz2");
227-
put(Hypervisor.HypervisorType.VMware, "http://download.cloud.com/templates/4.5/systemvm64template-4.5-vmware.ova");
228-
put(Hypervisor.HypervisorType.KVM, "http://download.cloud.com/templates/4.5/systemvm64template-4.5-kvm.qcow2.bz2");
229-
put(Hypervisor.HypervisorType.LXC, "http://download.cloud.com/templates/4.5/systemvm64template-4.5-kvm.qcow2.bz2");
230-
put(Hypervisor.HypervisorType.Hyperv, "http://download.cloud.com/templates/4.5/systemvm64template-4.5-hyperv.vhd.zip");
231-
}
232-
};
233-
234-
Map<Hypervisor.HypervisorType, String> newTemplateChecksum = new HashMap<Hypervisor.HypervisorType, String>(){
235-
{ put(Hypervisor.HypervisorType.XenServer, "2b15ab4401c2d655264732d3fc600241");
236-
put(Hypervisor.HypervisorType.VMware, "3106a79a4ce66cd7f6a7c50e93f2db57");
237-
put(Hypervisor.HypervisorType.KVM, "aa9f501fecd3de1daeb9e2f357f6f002");
238-
put(Hypervisor.HypervisorType.LXC, "aa9f501fecd3de1daeb9e2f357f6f002");
239-
put(Hypervisor.HypervisorType.Hyperv, "70bd30ea02ee9ed67d2c6b85c179cee9");
240-
}
241-
};
242-
243-
for (Map.Entry<Hypervisor.HypervisorType, String> hypervisorAndTemplateName : NewTemplateNameList.entrySet()){
244-
s_logger.debug("Updating " + hypervisorAndTemplateName.getKey() + " System Vms");
245-
try {
246-
//Get 4.5.0 system Vm template Id for corresponding hypervisor
247-
pstmt = conn.prepareStatement("select id from `cloud`.`vm_template` where name = ? and removed is null order by id desc limit 1");
248-
pstmt.setString(1, hypervisorAndTemplateName.getValue());
249-
rs = pstmt.executeQuery();
205+
Map<Hypervisor.HypervisorType, String> NewTemplateNameList = new HashMap<Hypervisor.HypervisorType, String>() {
206+
{
207+
put(Hypervisor.HypervisorType.XenServer, "systemvm-xenserver-4.5");
208+
put(Hypervisor.HypervisorType.VMware, "systemvm-vmware-4.5");
209+
put(Hypervisor.HypervisorType.KVM, "systemvm-kvm-4.5");
210+
put(Hypervisor.HypervisorType.LXC, "systemvm-lxc-4.5");
211+
put(Hypervisor.HypervisorType.Hyperv, "systemvm-hyperv-4.5");
212+
}
213+
};
214+
215+
Map<Hypervisor.HypervisorType, String> routerTemplateConfigurationNames = new HashMap<Hypervisor.HypervisorType, String>() {
216+
{
217+
put(Hypervisor.HypervisorType.XenServer, "router.template.xen");
218+
put(Hypervisor.HypervisorType.VMware, "router.template.vmware");
219+
put(Hypervisor.HypervisorType.KVM, "router.template.kvm");
220+
put(Hypervisor.HypervisorType.LXC, "router.template.lxc");
221+
put(Hypervisor.HypervisorType.Hyperv, "router.template.hyperv");
222+
}
223+
};
224+
225+
Map<Hypervisor.HypervisorType, String> newTemplateUrl = new HashMap<Hypervisor.HypervisorType, String>() {
226+
{
227+
put(Hypervisor.HypervisorType.XenServer, "http://download.cloud.com/templates/4.5/systemvm64template-4.5-xen.vhd.bz2");
228+
put(Hypervisor.HypervisorType.VMware, "http://download.cloud.com/templates/4.5/systemvm64template-4.5-vmware.ova");
229+
put(Hypervisor.HypervisorType.KVM, "http://download.cloud.com/templates/4.5/systemvm64template-4.5-kvm.qcow2.bz2");
230+
put(Hypervisor.HypervisorType.LXC, "http://download.cloud.com/templates/4.5/systemvm64template-4.5-kvm.qcow2.bz2");
231+
put(Hypervisor.HypervisorType.Hyperv, "http://download.cloud.com/templates/4.5/systemvm64template-4.5-hyperv.vhd.zip");
232+
}
233+
};
234+
235+
Map<Hypervisor.HypervisorType, String> newTemplateChecksum = new HashMap<Hypervisor.HypervisorType, String>() {
236+
{
237+
put(Hypervisor.HypervisorType.XenServer, "2b15ab4401c2d655264732d3fc600241");
238+
put(Hypervisor.HypervisorType.VMware, "3106a79a4ce66cd7f6a7c50e93f2db57");
239+
put(Hypervisor.HypervisorType.KVM, "aa9f501fecd3de1daeb9e2f357f6f002");
240+
put(Hypervisor.HypervisorType.LXC, "aa9f501fecd3de1daeb9e2f357f6f002");
241+
put(Hypervisor.HypervisorType.Hyperv, "70bd30ea02ee9ed67d2c6b85c179cee9");
242+
}
243+
};
244+
245+
for (Map.Entry<Hypervisor.HypervisorType, String> hypervisorAndTemplateName : NewTemplateNameList.entrySet()) {
246+
s_logger.debug("Updating " + hypervisorAndTemplateName.getKey() + " System Vms");
247+
try (PreparedStatement pstmt = conn.prepareStatement("select id from `cloud`.`vm_template` where name = ? and removed is null order by id desc limit 1")) {
248+
//Get 4.5.0 system Vm template Id for corresponding hypervisor
249+
long templateId = -1;
250+
pstmt.setString(1, hypervisorAndTemplateName.getValue());
251+
try (ResultSet rs = pstmt.executeQuery()) {
250252
if(rs.next()){
251-
long templateId = rs.getLong(1);
252-
rs.close();
253-
pstmt.close();
254-
pstmt = conn.prepareStatement("update `cloud`.`vm_template` set type='SYSTEM' where id = ?");
255-
pstmt.setLong(1, templateId);
256-
pstmt.executeUpdate();
257-
pstmt.close();
258-
// update templete ID of system Vms
259-
pstmt = conn.prepareStatement("update `cloud`.`vm_instance` set vm_template_id = ? where type <> 'User' and hypervisor_type = ?");
260-
pstmt.setLong(1, templateId);
261-
pstmt.setString(2, hypervisorAndTemplateName.getKey().toString());
262-
pstmt.executeUpdate();
263-
pstmt.close();
264-
// Change value of global configuration parameter router.template.* for the corresponding hypervisor
265-
pstmt = conn.prepareStatement("UPDATE `cloud`.`configuration` SET value = ? WHERE name = ?");
266-
pstmt.setString(1, hypervisorAndTemplateName.getValue());
267-
pstmt.setString(2, routerTemplateConfigurationNames.get(hypervisorAndTemplateName.getKey()));
268-
pstmt.executeUpdate();
269-
pstmt.close();
270-
} else {
271-
rs.close();
272-
pstmt.close();
273-
if (hypervisorsListInUse.contains(hypervisorAndTemplateName.getKey())){
274-
throw new CloudRuntimeException("4.5.0 " + hypervisorAndTemplateName.getKey() + " SystemVm template not found. Cannot upgrade system Vms");
275-
} else {
276-
s_logger.warn("4.5.0 " + hypervisorAndTemplateName.getKey() + " SystemVm template not found. " + hypervisorAndTemplateName.getKey() + " hypervisor is not used, so not failing upgrade");
277-
// Update the latest template URLs for corresponding hypervisor
278-
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");
279-
pstmt.setString(1, newTemplateUrl.get(hypervisorAndTemplateName.getKey()));
280-
pstmt.setString(2, newTemplateChecksum.get(hypervisorAndTemplateName.getKey()));
281-
pstmt.setString(3, hypervisorAndTemplateName.getKey().toString());
282-
pstmt.executeUpdate();
283-
pstmt.close();
284-
}
253+
templateId = rs.getLong(1);
285254
}
286-
} catch (SQLException e) {
287-
throw new CloudRuntimeException("Error while updating "+ hypervisorAndTemplateName.getKey() +" systemVm template", e);
288-
}
289-
}
290-
s_logger.debug("Updating System Vm Template IDs Complete");
291-
} finally {
292-
try {
293-
if (rs != null) {
294-
rs.close();
255+
} catch (SQLException e)
256+
{
257+
s_logger.error("updateSystemVmTemplates:Exception while getting ids of templates: "+e.getMessage());
258+
throw new CloudRuntimeException("updateSystemVmTemplates:Exception while getting ids of templates", e);
295259
}
296260

297-
if (pstmt != null) {
298-
pstmt.close();
261+
// change template type to SYSTEM
262+
if (templateId != -1) {
263+
try(PreparedStatement templ_type_pstmt = conn.prepareStatement("update `cloud`.`vm_template` set type='SYSTEM' where id = ?");)
264+
{
265+
templ_type_pstmt.setLong(1, templateId);
266+
templ_type_pstmt.executeUpdate();
267+
}
268+
catch (SQLException e)
269+
{
270+
s_logger.error("updateSystemVmTemplates:Exception while updating template with id " + templateId + " to be marked as 'system': "+e.getMessage());
271+
throw new CloudRuntimeException("updateSystemVmTemplates:Exception while updating template with id " + templateId + " to be marked as 'system'", e);
272+
}
273+
// update template ID of system Vms
274+
try(PreparedStatement update_templ_id_pstmt = conn.prepareStatement("update `cloud`.`vm_instance` set vm_template_id = ? where type <> 'User' and hypervisor_type = ?");)
275+
{
276+
update_templ_id_pstmt.setLong(1, templateId);
277+
update_templ_id_pstmt.setString(2, hypervisorAndTemplateName.getKey().toString());
278+
update_templ_id_pstmt.executeUpdate();
279+
}catch (Exception e)
280+
{
281+
s_logger.error("updateSystemVmTemplates:Exception while setting template for " + hypervisorAndTemplateName.getKey().toString() + " to " + templateId + ": "+e.getMessage());
282+
throw new CloudRuntimeException("updateSystemVmTemplates:Exception while setting template for " + hypervisorAndTemplateName.getKey().toString() + " to " + templateId, e);
283+
}
284+
// Change value of global configuration parameter router.template.* for the corresponding hypervisor
285+
try(PreparedStatement update_pstmt = conn.prepareStatement("UPDATE `cloud`.`configuration` SET value = ? WHERE name = ?");) {
286+
update_pstmt.setString(1, hypervisorAndTemplateName.getValue());
287+
update_pstmt.setString(2, routerTemplateConfigurationNames.get(hypervisorAndTemplateName.getKey()));
288+
update_pstmt.executeUpdate();
289+
}catch (SQLException e)
290+
{
291+
s_logger.error("updateSystemVmTemplates:Exception while setting " + routerTemplateConfigurationNames.get(hypervisorAndTemplateName.getKey()) + " to " + hypervisorAndTemplateName.getValue() + ": "+e.getMessage());
292+
throw new CloudRuntimeException("updateSystemVmTemplates:Exception while setting " + routerTemplateConfigurationNames.get(hypervisorAndTemplateName.getKey()) + " to " + hypervisorAndTemplateName.getValue(), e);
293+
}
294+
} else {
295+
if (hypervisorsListInUse.contains(hypervisorAndTemplateName.getKey())){
296+
throw new CloudRuntimeException("4.5.0 " + hypervisorAndTemplateName.getKey() + " SystemVm template not found. Cannot upgrade system Vms");
297+
} else {
298+
s_logger.warn("4.5.0 " + hypervisorAndTemplateName.getKey() + " SystemVm template not found. " + hypervisorAndTemplateName.getKey() + " hypervisor is not used, so not failing upgrade");
299+
// Update the latest template URLs for corresponding hypervisor
300+
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");) {
301+
update_templ_url_pstmt.setString(1, newTemplateUrl.get(hypervisorAndTemplateName.getKey()));
302+
update_templ_url_pstmt.setString(2, newTemplateChecksum.get(hypervisorAndTemplateName.getKey()));
303+
update_templ_url_pstmt.setString(3, hypervisorAndTemplateName.getKey().toString());
304+
update_templ_url_pstmt.executeUpdate();
305+
}catch (SQLException e)
306+
{
307+
s_logger.error("updateSystemVmTemplates:Exception while updating 'url' and 'checksum' for hypervisor type " + hypervisorAndTemplateName.getKey().toString() + ": "+e.getMessage());
308+
throw new CloudRuntimeException("updateSystemVmTemplates:Exception while updating 'url' and 'checksum' for hypervisor type " + hypervisorAndTemplateName.getKey().toString(), e);
309+
}
310+
}
299311
}
300312
} catch (SQLException e) {
301-
s_logger.debug("exception while cleaning resources during sytemvm upgrade.", e);
313+
s_logger.error("updateSystemVmTemplates:Exception while getting ids of templates: "+e.getMessage());
314+
throw new CloudRuntimeException("updateSystemVmTemplates:Exception while getting ids of templates", e);
302315
}
303316
}
317+
s_logger.debug("Updating System Vm Template IDs Complete");
304318
}
305319

306320

0 commit comments

Comments
 (0)