mirror of
https://github.com/apache/cloudstack.git
synced 2025-10-26 08:42:29 +01:00
221 to 222 DB upgrade for InstanceGroup. Unit test is included
This commit is contained in:
parent
c013d0a6c6
commit
a50950bbef
@ -89,6 +89,69 @@ public class Upgrade217to22 implements DbUpgrade {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
protected void upgradeInstanceGroups(Connection conn){
|
||||||
|
try {
|
||||||
|
|
||||||
|
//Create instance groups - duplicated names are allowed across accounts
|
||||||
|
PreparedStatement pstmt = conn.prepareStatement("SELECT DISTINCT v.group, v.account_id from vm_instance v where v.group is not null");
|
||||||
|
ResultSet rs = pstmt.executeQuery();
|
||||||
|
ArrayList<Object[]> groups = new ArrayList<Object[]>();
|
||||||
|
while (rs.next()) {
|
||||||
|
Object[] group = new Object[10];
|
||||||
|
group[0] = rs.getString(1); // group name
|
||||||
|
group[1] = rs.getLong(2); // accountId
|
||||||
|
groups.add(group);
|
||||||
|
}
|
||||||
|
rs.close();
|
||||||
|
pstmt.close();
|
||||||
|
|
||||||
|
for (Object[] group : groups) {
|
||||||
|
String groupName = (String)group[0];
|
||||||
|
Long accountId = (Long)group[1];
|
||||||
|
createInstanceGroups(conn, groupName, accountId);
|
||||||
|
}
|
||||||
|
|
||||||
|
//update instance_group_vm_map
|
||||||
|
pstmt = conn.prepareStatement("SELECT g.id, v.id from vm_instance v, instance_group g where g.name=v.group and g.account_id=v.account_id and v.group is not null");
|
||||||
|
rs = pstmt.executeQuery();
|
||||||
|
ArrayList<Object[]> groupVmMaps = new ArrayList<Object[]>();
|
||||||
|
while (rs.next()) {
|
||||||
|
Object[] groupMaps = new Object[10];
|
||||||
|
groupMaps[0] = rs.getLong(1); // vmId
|
||||||
|
groupMaps[1] = rs.getLong(2); // groupId
|
||||||
|
groupVmMaps.add(groupMaps);
|
||||||
|
}
|
||||||
|
rs.close();
|
||||||
|
pstmt.close();
|
||||||
|
|
||||||
|
for (Object[] groupMap : groupVmMaps) {
|
||||||
|
Long groupId = (Long)groupMap[0];
|
||||||
|
Long instanceId = (Long)groupMap[1];
|
||||||
|
createInstanceGroupVmMaps(conn, groupId, instanceId);
|
||||||
|
}
|
||||||
|
} catch (SQLException e) {
|
||||||
|
throw new CloudRuntimeException("Can't update instance groups ", e);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
protected void createInstanceGroups(Connection conn, String groupName, long accountId) throws SQLException{
|
||||||
|
PreparedStatement pstmt = conn.prepareStatement("INSERT INTO instance_group (account_id, name, created) values (?, ?, now()) ");
|
||||||
|
pstmt.setLong(1, accountId);
|
||||||
|
pstmt.setString(2, groupName);
|
||||||
|
pstmt.executeUpdate();
|
||||||
|
pstmt.close();
|
||||||
|
}
|
||||||
|
|
||||||
|
protected void createInstanceGroupVmMaps(Connection conn, long groupId, long instanceId) throws SQLException{
|
||||||
|
PreparedStatement pstmt = conn.prepareStatement("INSERT INTO instance_group_vm_map (group_id, instance_id) values (?, ?) ");
|
||||||
|
pstmt.setLong(1, groupId);
|
||||||
|
pstmt.setLong(2, instanceId);
|
||||||
|
pstmt.executeUpdate();
|
||||||
|
pstmt.close();
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
protected long insertNetwork(Connection conn, String name, String displayText, String trafficType, String broadcastDomainType, String broadcastUri,
|
protected long insertNetwork(Connection conn, String name, String displayText, String trafficType, String broadcastDomainType, String broadcastUri,
|
||||||
String gateway, String cidr, String mode, long networkOfferingId, long dataCenterId, String guruName,
|
String gateway, String cidr, String mode, long networkOfferingId, long dataCenterId, String guruName,
|
||||||
String state, long domainId, long accountId, String dns1, String dns2, String guestType, boolean shared,
|
String state, long domainId, long accountId, String dns1, String dns2, String guestType, boolean shared,
|
||||||
@ -338,6 +401,7 @@ public class Upgrade217to22 implements DbUpgrade {
|
|||||||
upgradeDataCenter(conn);
|
upgradeDataCenter(conn);
|
||||||
// upgradeNetworks(conn);
|
// upgradeNetworks(conn);
|
||||||
upgradeStoragePools(conn);
|
upgradeStoragePools(conn);
|
||||||
|
upgradeInstanceGroups(conn);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
|||||||
@ -55,7 +55,7 @@ public class BasicZone217To223UpgradeTest extends TestCase {
|
|||||||
|
|
||||||
public void test217to22Upgrade() {
|
public void test217to22Upgrade() {
|
||||||
s_logger.debug("Finding sample data from 2.1.7");
|
s_logger.debug("Finding sample data from 2.1.7");
|
||||||
DbTestUtils.executeScript("VersionDaoImplTest/2.1.7/2.1.7_sample_basicZone_noSecurityGroups.sql", false, true);
|
DbTestUtils.executeScript("VersionDaoImplTest/2.1.7/2.1.7_sample_basicZone_SecurityGroups.sql", false, true);
|
||||||
|
|
||||||
Connection conn = Transaction.getStandaloneConnection();
|
Connection conn = Transaction.getStandaloneConnection();
|
||||||
PreparedStatement pstmt;
|
PreparedStatement pstmt;
|
||||||
|
|||||||
@ -0,0 +1,219 @@
|
|||||||
|
/**
|
||||||
|
* 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 <http://www.gnu.org/licenses/>.
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
package com.cloud.upgrade.dao;
|
||||||
|
|
||||||
|
|
||||||
|
import java.sql.Connection;
|
||||||
|
import java.sql.PreparedStatement;
|
||||||
|
import java.sql.ResultSet;
|
||||||
|
import java.sql.SQLException;
|
||||||
|
import java.util.ArrayList;
|
||||||
|
|
||||||
|
import javax.naming.ConfigurationException;
|
||||||
|
|
||||||
|
import junit.framework.TestCase;
|
||||||
|
|
||||||
|
import org.apache.log4j.Logger;
|
||||||
|
import org.junit.After;
|
||||||
|
import org.junit.Before;
|
||||||
|
|
||||||
|
import com.cloud.upgrade.dao.VersionVO.Step;
|
||||||
|
import com.cloud.utils.component.ComponentLocator;
|
||||||
|
import com.cloud.utils.db.DbTestUtils;
|
||||||
|
import com.cloud.utils.db.Transaction;
|
||||||
|
import com.cloud.utils.exception.CloudRuntimeException;
|
||||||
|
|
||||||
|
public class InstanceGroup217To221UpgradeTest extends TestCase {
|
||||||
|
private static final Logger s_logger = Logger.getLogger(InstanceGroup217To221UpgradeTest.class);
|
||||||
|
|
||||||
|
@Override
|
||||||
|
@Before
|
||||||
|
public void setUp() throws Exception {
|
||||||
|
VersionVO version = new VersionVO("2.1.7");
|
||||||
|
version.setStep(Step.Cleanup);
|
||||||
|
DbTestUtils.executeScript("VersionDaoImplTest/clean-db.sql", false, true);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
@After
|
||||||
|
public void tearDown() throws Exception {
|
||||||
|
}
|
||||||
|
|
||||||
|
public void test217to22Upgrade() {
|
||||||
|
s_logger.debug("Finding sample data from 2.1.7");
|
||||||
|
DbTestUtils.executeScript("VersionDaoImplTest/2.1.7/2.1.7_sample_instanceGroups.sql", false, true);
|
||||||
|
|
||||||
|
Connection conn = Transaction.getStandaloneConnection();
|
||||||
|
PreparedStatement pstmt;
|
||||||
|
|
||||||
|
VersionDaoImpl dao = ComponentLocator.inject(VersionDaoImpl.class);
|
||||||
|
|
||||||
|
String version = dao.getCurrentVersion();
|
||||||
|
|
||||||
|
if (!version.equals("2.1.7")) {
|
||||||
|
s_logger.error("Version returned is not 2.1.7 but " + version);
|
||||||
|
} else {
|
||||||
|
s_logger.debug("Basic zone test version is " + version);
|
||||||
|
}
|
||||||
|
|
||||||
|
try {
|
||||||
|
dao.upgrade("2.1.7", "2.2.1");
|
||||||
|
} catch (ConfigurationException e) {
|
||||||
|
s_logger.warn("Exception: ", e);
|
||||||
|
assert false : "The test failed. Check logs";
|
||||||
|
}
|
||||||
|
|
||||||
|
conn = Transaction.getStandaloneConnection();
|
||||||
|
try {
|
||||||
|
|
||||||
|
s_logger.debug("Starting tesing upgrade from 2.1.7 to 2.2.2 for Instance groups...");
|
||||||
|
|
||||||
|
//Version check
|
||||||
|
pstmt = conn.prepareStatement("SELECT version FROM version");
|
||||||
|
ResultSet rs = pstmt.executeQuery();
|
||||||
|
|
||||||
|
if (!rs.next()) {
|
||||||
|
s_logger.error("ERROR: No version selected");
|
||||||
|
} else if (!rs.getString(1).equals("2.2.1")) {
|
||||||
|
s_logger.error("ERROR: VERSION stored is not 2.2.1: " + rs.getString(1));
|
||||||
|
}
|
||||||
|
rs.close();
|
||||||
|
pstmt.close();
|
||||||
|
|
||||||
|
//Check that correct number of instance groups were created
|
||||||
|
Long groupNumberVmInstance = 0L;
|
||||||
|
pstmt = conn.prepareStatement("SELECT DISTINCT v.group, v.account_id from vm_instance v where v.group is not null");
|
||||||
|
rs = pstmt.executeQuery();
|
||||||
|
|
||||||
|
while (rs.next()) {
|
||||||
|
groupNumberVmInstance++;
|
||||||
|
}
|
||||||
|
|
||||||
|
rs.close();
|
||||||
|
pstmt.close();
|
||||||
|
|
||||||
|
Long groupNumber = 0L;
|
||||||
|
pstmt = conn.prepareStatement("SELECT COUNT(*) FROM instance_group");
|
||||||
|
rs = pstmt.executeQuery();
|
||||||
|
|
||||||
|
if (rs.next()) {
|
||||||
|
groupNumber = rs.getLong(1);
|
||||||
|
}
|
||||||
|
|
||||||
|
rs.close();
|
||||||
|
pstmt.close();
|
||||||
|
|
||||||
|
if (groupNumber != groupNumberVmInstance) {
|
||||||
|
s_logger.error("ERROR: instance groups were updated incorrectly. Have " + groupNumberVmInstance + " groups in vm_instance table, and " + groupNumber + " where created in instance_group table. Stopping the test");
|
||||||
|
System.exit(2);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
//For each instance group from vm_instance table check that 1) entry was created in the instance_group table 2) vm to group map exists in instance_group_vm_map table
|
||||||
|
//Check 1)
|
||||||
|
pstmt = conn.prepareStatement("SELECT DISTINCT v.group, v.account_id from vm_instance v where v.group is not null");
|
||||||
|
rs = pstmt.executeQuery();
|
||||||
|
ArrayList<Object[]> groups = new ArrayList<Object[]>();
|
||||||
|
while (rs.next()) {
|
||||||
|
Object[] group = new Object[10];
|
||||||
|
group[0] = rs.getString(1); // group name
|
||||||
|
group[1] = rs.getLong(2); // accountId
|
||||||
|
groups.add(group);
|
||||||
|
}
|
||||||
|
rs.close();
|
||||||
|
pstmt.close();
|
||||||
|
|
||||||
|
for (Object[] group : groups) {
|
||||||
|
String groupName = (String)group[0];
|
||||||
|
Long accountId = (Long)group[1];
|
||||||
|
if (!checkInstanceGroup(conn, groupName, accountId)) {
|
||||||
|
s_logger.error("ERROR: Unable to find group with name " + groupName + " for account id=" + accountId + ", stopping the test");
|
||||||
|
System.exit(2);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
rs.close();
|
||||||
|
pstmt.close();
|
||||||
|
|
||||||
|
//Check 2)
|
||||||
|
pstmt = conn.prepareStatement("SELECT g.id, v.id from vm_instance v, instance_group g where g.name=v.group and g.account_id=v.account_id and v.group is not null");
|
||||||
|
rs = pstmt.executeQuery();
|
||||||
|
ArrayList<Object[]> groupVmMaps = new ArrayList<Object[]>();
|
||||||
|
while (rs.next()) {
|
||||||
|
Object[] groupMaps = new Object[10];
|
||||||
|
groupMaps[0] = rs.getLong(1); // vmId
|
||||||
|
groupMaps[1] = rs.getLong(2); // groupId
|
||||||
|
groupVmMaps.add(groupMaps);
|
||||||
|
}
|
||||||
|
rs.close();
|
||||||
|
pstmt.close();
|
||||||
|
|
||||||
|
for (Object[] groupMap : groupVmMaps) {
|
||||||
|
Long groupId = (Long)groupMap[0];
|
||||||
|
Long instanceId = (Long)groupMap[1];
|
||||||
|
if (!checkInstanceGroupVmMap(conn, groupId, instanceId)) {
|
||||||
|
s_logger.error("ERROR: unable to find instanceGroupVMMap for vm id=" + instanceId + " and group id=" + groupId + ", stopping the test");
|
||||||
|
System.exit(2);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
rs.close();
|
||||||
|
pstmt.close();
|
||||||
|
|
||||||
|
s_logger.debug("Instance group upgrade test is passed");
|
||||||
|
|
||||||
|
} catch (SQLException e) {
|
||||||
|
throw new CloudRuntimeException("Problem testing instance group update", e);
|
||||||
|
} finally {
|
||||||
|
try {
|
||||||
|
conn.close();
|
||||||
|
} catch (SQLException e) {
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
protected boolean checkInstanceGroup(Connection conn, String groupName, long accountId) throws SQLException{
|
||||||
|
|
||||||
|
PreparedStatement pstmt = conn.prepareStatement("SELECT * FROM instance_group WHERE name = ? and account_id = ?");
|
||||||
|
pstmt.setString(1, groupName);
|
||||||
|
pstmt.setLong(2, accountId);
|
||||||
|
ResultSet rs = pstmt.executeQuery();
|
||||||
|
|
||||||
|
if (!rs.next()) {
|
||||||
|
return false;
|
||||||
|
} else {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
protected boolean checkInstanceGroupVmMap(Connection conn, long groupId, long vmId) throws SQLException{
|
||||||
|
|
||||||
|
PreparedStatement pstmt = conn.prepareStatement("SELECT * FROM instance_group_vm_map WHERE group_id = ? and instance_id = ?");
|
||||||
|
pstmt.setLong(1, groupId);
|
||||||
|
pstmt.setLong(2, vmId);
|
||||||
|
ResultSet rs = pstmt.executeQuery();
|
||||||
|
|
||||||
|
if (!rs.next()) {
|
||||||
|
return false;
|
||||||
|
} else {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
@ -4,6 +4,7 @@ DROP TABLE IF EXISTS `cloud`.`ext_lun_alloc`;
|
|||||||
DROP TABLE IF EXISTS `cloud`.`disk_template_ref`;
|
DROP TABLE IF EXISTS `cloud`.`disk_template_ref`;
|
||||||
|
|
||||||
-- Then remove columns
|
-- Then remove columns
|
||||||
|
ALTER TABLE `cloud`.`vm_instance` DROP COLUMN `group`;
|
||||||
|
|
||||||
ALTER TABLE `cloud`.`volumes` DROP COLUMN `mirror_state`;
|
ALTER TABLE `cloud`.`volumes` DROP COLUMN `mirror_state`;
|
||||||
ALTER TABLE `cloud`.`volumes` DROP COLUMN `mirror_vol`;
|
ALTER TABLE `cloud`.`volumes` DROP COLUMN `mirror_vol`;
|
||||||
|
|||||||
@ -1,8 +1,6 @@
|
|||||||
--;
|
--;
|
||||||
-- Schema upgrade from 2.1 to 2.2;
|
-- Schema upgrade from 2.1 to 2.2;
|
||||||
--;
|
--;
|
||||||
|
|
||||||
ALTER TABLE `cloud`.`vm_instance` DROP COLUMN `group`;
|
|
||||||
ALTER TABLE `cloud`.`cluster` ADD COLUMN `guid` varchar(255) UNIQUE DEFAULT NULL;
|
ALTER TABLE `cloud`.`cluster` ADD COLUMN `guid` varchar(255) UNIQUE DEFAULT NULL;
|
||||||
ALTER TABLE `cloud`.`cluster` ADD COLUMN `cluster_type` varchar(64) DEFAULT 'CloudManaged';
|
ALTER TABLE `cloud`.`cluster` ADD COLUMN `cluster_type` varchar(64) DEFAULT 'CloudManaged';
|
||||||
ALTER TABLE `cloud`.`vm_template` ADD COLUMN `hypervisor_type` varchar(32) COMMENT 'hypervisor that the template is belonged to';
|
ALTER TABLE `cloud`.`vm_template` ADD COLUMN `hypervisor_type` varchar(32) COMMENT 'hypervisor that the template is belonged to';
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user