Skip to content

Commit 5a3ae15

Browse files
authored
upgrade: check systemvm template before db changes (#4582)
* Upgrade: check systemvm template before db changes * Upgrade: move some codes to a separated method * #4582 add txn.commit()
1 parent ba43825 commit 5a3ae15

3 files changed

Lines changed: 60 additions & 3 deletions

File tree

engine/schema/src/main/java/com/cloud/upgrade/DatabaseUpgradeChecker.java

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
import org.apache.log4j.Logger;
3535

3636
import com.cloud.upgrade.dao.DbUpgrade;
37+
import com.cloud.upgrade.dao.DbUpgradeSystemVmTemplate;
3738
import com.cloud.upgrade.dao.Upgrade217to218;
3839
import com.cloud.upgrade.dao.Upgrade218to22;
3940
import com.cloud.upgrade.dao.Upgrade218to224DomainVlans;
@@ -235,11 +236,42 @@ DbUpgrade[] calculateUpgradePath(final CloudStackVersion dbVersion, final CloudS
235236

236237
}
237238

239+
private void updateSystemVmTemplates(DbUpgrade[] upgrades) {
240+
for (int i = upgrades.length - 1; i >= 0; i--) {
241+
DbUpgrade upgrade = upgrades[i];
242+
if (upgrade instanceof DbUpgradeSystemVmTemplate) {
243+
TransactionLegacy txn = TransactionLegacy.open("Upgrade");
244+
txn.start();
245+
try {
246+
Connection conn;
247+
try {
248+
conn = txn.getConnection();
249+
} catch (SQLException e) {
250+
String errorMessage = "Unable to upgrade the database";
251+
s_logger.error(errorMessage, e);
252+
throw new CloudRuntimeException(errorMessage, e);
253+
}
254+
((DbUpgradeSystemVmTemplate)upgrade).updateSystemVmTemplates(conn);
255+
txn.commit();
256+
break;
257+
} catch (CloudRuntimeException e) {
258+
String errorMessage = "Unable to upgrade the database";
259+
s_logger.error(errorMessage, e);
260+
throw new CloudRuntimeException(errorMessage, e);
261+
} finally {
262+
txn.close();
263+
}
264+
}
265+
}
266+
}
267+
238268
protected void upgrade(CloudStackVersion dbVersion, CloudStackVersion currentVersion) {
239269
s_logger.info("Database upgrade must be performed from " + dbVersion + " to " + currentVersion);
240270

241271
final DbUpgrade[] upgrades = calculateUpgradePath(dbVersion, currentVersion);
242272

273+
updateSystemVmTemplates(upgrades);
274+
243275
for (DbUpgrade upgrade : upgrades) {
244276
VersionVO version;
245277
s_logger.debug("Running upgrade " + upgrade.getClass().getSimpleName() + " to upgrade from " + upgrade.getUpgradableVersionRange()[0] + "-" + upgrade
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
// Licensed to the Apache Software Foundation (ASF) under one
2+
// or more contributor license agreements. See the NOTICE file
3+
// distributed with this work for additional information
4+
// regarding copyright ownership. The ASF licenses this file
5+
// to you under the Apache License, Version 2.0 (the
6+
// "License"); you may not use this file except in compliance
7+
// with the License. You may obtain a copy of the License at
8+
//
9+
// http://www.apache.org/licenses/LICENSE-2.0
10+
//
11+
// Unless required by applicable law or agreed to in writing,
12+
// software distributed under the License is distributed on an
13+
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14+
// KIND, either express or implied. See the License for the
15+
// specific language governing permissions and limitations
16+
// under the License.
17+
18+
package com.cloud.upgrade.dao;
19+
20+
import java.sql.Connection;
21+
22+
public interface DbUpgradeSystemVmTemplate {
23+
24+
void updateSystemVmTemplates(Connection conn);
25+
}

engine/schema/src/main/java/com/cloud/upgrade/dao/Upgrade41400to41500.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@
3535
import com.cloud.hypervisor.Hypervisor;
3636
import com.cloud.utils.exception.CloudRuntimeException;
3737

38-
public class Upgrade41400to41500 implements DbUpgrade {
38+
public class Upgrade41400to41500 implements DbUpgrade, DbUpgradeSystemVmTemplate {
3939

4040
final static Logger LOG = Logger.getLogger(Upgrade41400to41500.class);
4141

@@ -67,12 +67,12 @@ public InputStream[] getPrepareScripts() {
6767

6868
@Override
6969
public void performDataMigration(Connection conn) {
70-
updateSystemVmTemplates(conn);
7170
addRolePermissionsForNewReadOnlyAndSupportRoles(conn);
7271
}
7372

73+
@Override
7474
@SuppressWarnings("serial")
75-
private void updateSystemVmTemplates(final Connection conn) {
75+
public void updateSystemVmTemplates(final Connection conn) {
7676
LOG.debug("Updating System Vm template IDs");
7777
final Set<Hypervisor.HypervisorType> hypervisorsListInUse = new HashSet<Hypervisor.HypervisorType>();
7878
try (PreparedStatement pstmt = conn.prepareStatement("select distinct(hypervisor_type) from `cloud`.`cluster` where removed is null"); ResultSet rs = pstmt.executeQuery()) {

0 commit comments

Comments
 (0)