Skip to content

Commit 287a472

Browse files
committed
Merge branch '4.19'
2 parents cf1428d + 6e6a276 commit 287a472

24 files changed

+602
-386
lines changed

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

Lines changed: 99 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,7 @@ public class DatabaseUpgradeChecker implements SystemIntegrityChecker {
130130
protected static Logger LOGGER = LogManager.getLogger(DatabaseUpgradeChecker.class);
131131
private final DatabaseVersionHierarchy hierarchy;
132132
private static final String VIEWS_DIRECTORY = Paths.get("META-INF", "db", "views").toString();
133+
private static final String PROCEDURES_DIRECTORY = Paths.get("META-INF", "db", "procedures").toString();
133134

134135
@Inject
135136
VersionDao _dao;
@@ -298,83 +299,120 @@ private void updateSystemVmTemplates(DbUpgrade[] upgrades) {
298299
}
299300

300301
protected void upgrade(CloudStackVersion dbVersion, CloudStackVersion currentVersion) {
302+
executeProcedureScripts();
303+
final DbUpgrade[] upgrades = executeUpgrades(dbVersion, currentVersion);
304+
305+
executeViewScripts();
306+
updateSystemVmTemplates(upgrades);
307+
}
308+
309+
protected void executeProcedureScripts() {
310+
LOGGER.info(String.format("Executing Stored Procedure scripts that are under resource directory [%s].", PROCEDURES_DIRECTORY));
311+
List<String> filesPathUnderViewsDirectory = FileUtil.getFilesPathsUnderResourceDirectory(PROCEDURES_DIRECTORY);
312+
313+
try (TransactionLegacy txn = TransactionLegacy.open("execute-procedure-scripts")) {
314+
Connection conn = txn.getConnection();
315+
316+
for (String filePath : filesPathUnderViewsDirectory) {
317+
LOGGER.debug(String.format("Executing PROCEDURE script [%s].", filePath));
318+
319+
InputStream viewScript = Thread.currentThread().getContextClassLoader().getResourceAsStream(filePath);
320+
runScript(conn, viewScript);
321+
}
322+
323+
LOGGER.info(String.format("Finished execution of PROCEDURE scripts that are under resource directory [%s].", PROCEDURES_DIRECTORY));
324+
} catch (SQLException e) {
325+
String message = String.format("Unable to execute PROCEDURE scripts due to [%s].", e.getMessage());
326+
LOGGER.error(message, e);
327+
throw new CloudRuntimeException(message, e);
328+
}
329+
}
330+
331+
private DbUpgrade[] executeUpgrades(CloudStackVersion dbVersion, CloudStackVersion currentVersion) {
301332
LOGGER.info("Database upgrade must be performed from " + dbVersion + " to " + currentVersion);
302333

303334
final DbUpgrade[] upgrades = calculateUpgradePath(dbVersion, currentVersion);
304335

305336
for (DbUpgrade upgrade : upgrades) {
306-
VersionVO version;
307-
LOGGER.debug("Running upgrade " + upgrade.getClass().getSimpleName() + " to upgrade from " + upgrade.getUpgradableVersionRange()[0] + "-" + upgrade
308-
.getUpgradableVersionRange()[1] + " to " + upgrade.getUpgradedVersion());
309-
TransactionLegacy txn = TransactionLegacy.open("Upgrade");
310-
txn.start();
311-
try {
312-
Connection conn;
313-
try {
314-
conn = txn.getConnection();
315-
} catch (SQLException e) {
316-
String errorMessage = "Unable to upgrade the database";
317-
LOGGER.error(errorMessage, e);
318-
throw new CloudRuntimeException(errorMessage, e);
319-
}
320-
InputStream[] scripts = upgrade.getPrepareScripts();
321-
if (scripts != null) {
322-
for (InputStream script : scripts) {
323-
runScript(conn, script);
324-
}
325-
}
326-
327-
upgrade.performDataMigration(conn);
328-
329-
version = new VersionVO(upgrade.getUpgradedVersion());
330-
version = _dao.persist(version);
337+
VersionVO version = executeUpgrade(upgrade);
338+
executeUpgradeCleanup(upgrade, version);
339+
}
340+
return upgrades;
341+
}
331342

332-
txn.commit();
333-
} catch (CloudRuntimeException e) {
343+
private VersionVO executeUpgrade(DbUpgrade upgrade) {
344+
VersionVO version;
345+
LOGGER.debug("Running upgrade " + upgrade.getClass().getSimpleName() + " to upgrade from " + upgrade.getUpgradableVersionRange()[0] + "-" + upgrade
346+
.getUpgradableVersionRange()[1] + " to " + upgrade.getUpgradedVersion());
347+
TransactionLegacy txn = TransactionLegacy.open("Upgrade");
348+
txn.start();
349+
try {
350+
Connection conn;
351+
try {
352+
conn = txn.getConnection();
353+
} catch (SQLException e) {
334354
String errorMessage = "Unable to upgrade the database";
335355
LOGGER.error(errorMessage, e);
336356
throw new CloudRuntimeException(errorMessage, e);
337-
} finally {
338-
txn.close();
357+
}
358+
InputStream[] scripts = upgrade.getPrepareScripts();
359+
if (scripts != null) {
360+
for (InputStream script : scripts) {
361+
runScript(conn, script);
362+
}
339363
}
340364

341-
// Run the corresponding '-cleanup.sql' script
342-
txn = TransactionLegacy.open("Cleanup");
343-
try {
344-
LOGGER.info("Cleanup upgrade " + upgrade.getClass().getSimpleName() + " to upgrade from " + upgrade.getUpgradableVersionRange()[0] + "-" + upgrade
345-
.getUpgradableVersionRange()[1] + " to " + upgrade.getUpgradedVersion());
365+
upgrade.performDataMigration(conn);
346366

347-
txn.start();
348-
Connection conn;
349-
try {
350-
conn = txn.getConnection();
351-
} catch (SQLException e) {
352-
LOGGER.error("Unable to cleanup the database", e);
353-
throw new CloudRuntimeException("Unable to cleanup the database", e);
354-
}
367+
version = new VersionVO(upgrade.getUpgradedVersion());
368+
version = _dao.persist(version);
355369

356-
InputStream[] scripts = upgrade.getCleanupScripts();
357-
if (scripts != null) {
358-
for (InputStream script : scripts) {
359-
runScript(conn, script);
360-
LOGGER.debug("Cleanup script " + upgrade.getClass().getSimpleName() + " is executed successfully");
361-
}
362-
}
363-
txn.commit();
370+
txn.commit();
371+
} catch (CloudRuntimeException e) {
372+
String errorMessage = "Unable to upgrade the database";
373+
LOGGER.error(errorMessage, e);
374+
throw new CloudRuntimeException(errorMessage, e);
375+
} finally {
376+
txn.close();
377+
}
378+
return version;
379+
}
364380

365-
txn.start();
366-
version.setStep(Step.Complete);
367-
version.setUpdated(new Date());
368-
_dao.update(version.getId(), version);
369-
txn.commit();
370-
LOGGER.debug("Upgrade completed for version " + version.getVersion());
371-
} finally {
372-
txn.close();
381+
private void executeUpgradeCleanup(DbUpgrade upgrade, VersionVO version) {
382+
TransactionLegacy txn;
383+
// Run the corresponding '-cleanup.sql' script
384+
txn = TransactionLegacy.open("Cleanup");
385+
try {
386+
LOGGER.info("Cleanup upgrade " + upgrade.getClass().getSimpleName() + " to upgrade from " + upgrade.getUpgradableVersionRange()[0] + "-" + upgrade
387+
.getUpgradableVersionRange()[1] + " to " + upgrade.getUpgradedVersion());
388+
389+
txn.start();
390+
Connection conn;
391+
try {
392+
conn = txn.getConnection();
393+
} catch (SQLException e) {
394+
LOGGER.error("Unable to cleanup the database", e);
395+
throw new CloudRuntimeException("Unable to cleanup the database", e);
373396
}
374-
}
375397

376-
executeViewScripts();
377-
updateSystemVmTemplates(upgrades);
398+
InputStream[] scripts = upgrade.getCleanupScripts();
399+
if (scripts != null) {
400+
for (InputStream script : scripts) {
401+
runScript(conn, script);
402+
LOGGER.debug("Cleanup script " + upgrade.getClass().getSimpleName() + " is executed successfully");
403+
}
404+
}
405+
txn.commit();
406+
407+
txn.start();
408+
version.setStep(Step.Complete);
409+
version.setUpdated(new Date());
410+
_dao.update(version.getId(), version);
411+
txn.commit();
412+
LOGGER.debug("Upgrade completed for version " + version.getVersion());
413+
} finally {
414+
txn.close();
415+
}
378416
}
379417

380418
protected void executeViewScripts() {

engine/schema/src/main/java/org/apache/cloudstack/affinity/dao/AffinityGroupDao.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,4 +38,7 @@ public interface AffinityGroupDao extends GenericDao<AffinityGroupVO, Long> {
3838
AffinityGroupVO findByAccountAndType(Long accountId, String string);
3939

4040
AffinityGroupVO findDomainLevelGroupByType(Long domainId, String string);
41+
42+
List<AffinityGroupVO> listByIds(List<Long> ids, boolean exclusive);
43+
4144
}

engine/schema/src/main/java/org/apache/cloudstack/affinity/dao/AffinityGroupDaoImpl.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
import com.cloud.utils.db.SearchCriteria;
3232

3333
public class AffinityGroupDaoImpl extends GenericDaoBase<AffinityGroupVO, Long> implements AffinityGroupDao {
34+
private SearchBuilder<AffinityGroupVO> IdsSearch;
3435
private SearchBuilder<AffinityGroupVO> AccountIdSearch;
3536
private SearchBuilder<AffinityGroupVO> AccountIdNameSearch;
3637
private SearchBuilder<AffinityGroupVO> AccountIdNamesSearch;
@@ -47,6 +48,10 @@ public AffinityGroupDaoImpl() {
4748

4849
@PostConstruct
4950
protected void init() {
51+
IdsSearch = createSearchBuilder();
52+
IdsSearch.and("idIn", IdsSearch.entity().getId(), SearchCriteria.Op.IN);
53+
IdsSearch.done();
54+
5055
AccountIdSearch = createSearchBuilder();
5156
AccountIdSearch.and("accountId", AccountIdSearch.entity().getAccountId(), SearchCriteria.Op.EQ);
5257
AccountIdSearch.done();
@@ -158,4 +163,11 @@ public AffinityGroupVO findDomainLevelGroupByType(Long domainId, String type) {
158163
sc.setJoinParameters("domainTypeSearch", "domainId", domainId);
159164
return findOneBy(sc);
160165
}
166+
167+
@Override
168+
public List<AffinityGroupVO> listByIds(List<Long> ids, boolean exclusive) {
169+
SearchCriteria<AffinityGroupVO> sc = IdsSearch.create();
170+
sc.setParameters("idIn", ids.toArray());
171+
return lockRows(sc, null, exclusive);
172+
}
161173
}
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
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+
-- PR#4699 Drop the procedure `ADD_GUEST_OS_AND_HYPERVISOR_MAPPING` if it already exist.
19+
DROP PROCEDURE IF EXISTS `cloud`.`ADD_GUEST_OS_AND_HYPERVISOR_MAPPING`;
20+
21+
-- PR#4699 Create the procedure `ADD_GUEST_OS_AND_HYPERVISOR_MAPPING` to add guest_os and guest_os_hypervisor mapping.
22+
CREATE PROCEDURE `cloud`.`ADD_GUEST_OS_AND_HYPERVISOR_MAPPING` (
23+
IN guest_os_category_id bigint(20) unsigned,
24+
IN guest_os_display_name VARCHAR(255),
25+
IN guest_os_hypervisor_hypervisor_type VARCHAR(32),
26+
IN guest_os_hypervisor_hypervisor_version VARCHAR(32),
27+
IN guest_os_hypervisor_guest_os_name VARCHAR(255)
28+
)
29+
BEGIN
30+
INSERT INTO cloud.guest_os (uuid, category_id, display_name, created)
31+
SELECT UUID(), guest_os_category_id, guest_os_display_name, now()
32+
FROM DUAL
33+
WHERE not exists( SELECT 1
34+
FROM cloud.guest_os
35+
WHERE cloud.guest_os.category_id = guest_os_category_id
36+
AND cloud.guest_os.display_name = guest_os_display_name)
37+
38+
; INSERT INTO cloud.guest_os_hypervisor (uuid, hypervisor_type, hypervisor_version, guest_os_name, guest_os_id, created)
39+
SELECT UUID(), guest_os_hypervisor_hypervisor_type, guest_os_hypervisor_hypervisor_version, guest_os_hypervisor_guest_os_name, guest_os.id, now()
40+
FROM cloud.guest_os
41+
WHERE guest_os.category_id = guest_os_category_id
42+
AND guest_os.display_name = guest_os_display_name
43+
AND NOT EXISTS (SELECT 1
44+
FROM cloud.guest_os_hypervisor as hypervisor
45+
WHERE hypervisor_type = guest_os_hypervisor_hypervisor_type
46+
AND hypervisor_version = guest_os_hypervisor_hypervisor_version
47+
AND hypervisor.guest_os_id = guest_os.id
48+
AND hypervisor.guest_os_name = guest_os_hypervisor_guest_os_name)
49+
;END;
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
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+
-- in cloud
19+
DROP PROCEDURE IF EXISTS `cloud`.`IDEMPOTENT_ADD_COLUMN`;
20+
CREATE PROCEDURE `cloud`.`IDEMPOTENT_ADD_COLUMN` (
21+
IN in_table_name VARCHAR(200),
22+
IN in_column_name VARCHAR(200),
23+
IN in_column_definition VARCHAR(1000)
24+
)
25+
BEGIN
26+
27+
DECLARE CONTINUE HANDLER FOR 1060 BEGIN END; SET @ddl = CONCAT('ALTER TABLE ', in_table_name); SET @ddl = CONCAT(@ddl, ' ', 'ADD COLUMN') ; SET @ddl = CONCAT(@ddl, ' ', in_column_name); SET @ddl = CONCAT(@ddl, ' ', in_column_definition); PREPARE stmt FROM @ddl; EXECUTE stmt; DEALLOCATE PREPARE stmt; END;
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
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+
DROP PROCEDURE IF EXISTS `cloud`.`IDEMPOTENT_ADD_KEY`;
19+
20+
CREATE PROCEDURE `cloud`.`IDEMPOTENT_ADD_KEY` (
21+
IN in_index_name VARCHAR(200)
22+
, IN in_table_name VARCHAR(200)
23+
, IN in_key_definition VARCHAR(1000)
24+
)
25+
BEGIN
26+
27+
DECLARE CONTINUE HANDLER FOR 1061 BEGIN END; SET @ddl = CONCAT('ALTER TABLE ', in_table_name); SET @ddl = CONCAT(@ddl, ' ', ' ADD KEY ') ; SET @ddl = CONCAT(@ddl, ' ', in_index_name); SET @ddl = CONCAT(@ddl, ' ', in_key_definition); PREPARE stmt FROM @ddl; EXECUTE stmt; DEALLOCATE PREPARE stmt; END;
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
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+
-- Idempotent ADD UNIQUE INDEX
19+
DROP PROCEDURE IF EXISTS `cloud_usage`.`IDEMPOTENT_ADD_UNIQUE_INDEX`;
20+
CREATE PROCEDURE `cloud_usage`.`IDEMPOTENT_ADD_UNIQUE_INDEX` (
21+
IN in_table_name VARCHAR(200)
22+
, IN in_index_name VARCHAR(200)
23+
, IN in_index_definition VARCHAR(1000)
24+
)
25+
BEGIN
26+
DECLARE CONTINUE HANDLER FOR 1061 BEGIN END; SET @ddl = CONCAT('ALTER TABLE ', in_table_name); SET @ddl = CONCAT(@ddl, ' ', 'ADD UNIQUE INDEX ', in_index_name); SET @ddl = CONCAT(@ddl, ' ', in_index_definition); PREPARE stmt FROM @ddl; EXECUTE stmt; DEALLOCATE PREPARE stmt; END;
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
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+
-- Idempotent ADD UNIQUE KEY
19+
DROP PROCEDURE IF EXISTS `cloud`.`IDEMPOTENT_ADD_UNIQUE_KEY`;
20+
CREATE PROCEDURE `cloud`.`IDEMPOTENT_ADD_UNIQUE_KEY` (
21+
IN in_table_name VARCHAR(200)
22+
, IN in_key_name VARCHAR(200)
23+
, IN in_key_definition VARCHAR(1000)
24+
)
25+
BEGIN
26+
DECLARE CONTINUE HANDLER FOR 1061 BEGIN END; SET @ddl = CONCAT('ALTER TABLE ', in_table_name); SET @ddl = CONCAT(@ddl, ' ', 'ADD UNIQUE KEY ', in_key_name); SET @ddl = CONCAT(@ddl, ' ', in_key_definition); PREPARE stmt FROM @ddl; EXECUTE stmt; DEALLOCATE PREPARE stmt; END;

0 commit comments

Comments
 (0)