From 18d9c6dfc669819ed7489c3f59506a19f66601cc Mon Sep 17 00:00:00 2001 From: DK101010 <57522802+DK101010@users.noreply.github.com> Date: Thu, 21 Apr 2022 15:37:40 +0200 Subject: [PATCH] fix mismatching between db uuids and custom attributes uuids (#5382) * fix mismatching between db uuids and custom attributes uuids during the datastore cluster creation, cloudstack could not recognize the existing primary storage and create a new one because uuid format not equal * remove method call setUuid * add upgrade step to fix faulty pool uuids * adapt method to transform uuid each time * extract error msg * rm unused import * add exception to log error as parameter * adapt sql to fetch wrong uuids * rm spaces * move upgrade code to Upgrade41610to41700 Co-authored-by: DK101010 --- .../upgrade/dao/Upgrade41520to41600.java | 1 + .../upgrade/dao/Upgrade41610to41700.java | 28 +++++++++++++++++++ .../com/cloud/storage/StorageManagerImpl.java | 15 +++++++++- 3 files changed, 43 insertions(+), 1 deletion(-) diff --git a/engine/schema/src/main/java/com/cloud/upgrade/dao/Upgrade41520to41600.java b/engine/schema/src/main/java/com/cloud/upgrade/dao/Upgrade41520to41600.java index b498de7e7c6..05eb2c7df55 100644 --- a/engine/schema/src/main/java/com/cloud/upgrade/dao/Upgrade41520to41600.java +++ b/engine/schema/src/main/java/com/cloud/upgrade/dao/Upgrade41520to41600.java @@ -34,6 +34,7 @@ import org.apache.log4j.Logger; import com.cloud.utils.exception.CloudRuntimeException; + public class Upgrade41520to41600 implements DbUpgrade, DbUpgradeSystemVmTemplate { final static Logger LOG = Logger.getLogger(Upgrade41520to41600.class); diff --git a/engine/schema/src/main/java/com/cloud/upgrade/dao/Upgrade41610to41700.java b/engine/schema/src/main/java/com/cloud/upgrade/dao/Upgrade41610to41700.java index 1f89c75877c..a925e6dc3ef 100644 --- a/engine/schema/src/main/java/com/cloud/upgrade/dao/Upgrade41610to41700.java +++ b/engine/schema/src/main/java/com/cloud/upgrade/dao/Upgrade41610to41700.java @@ -21,7 +21,12 @@ import com.cloud.utils.exception.CloudRuntimeException; import org.apache.log4j.Logger; import java.io.InputStream; +import java.math.BigInteger; import java.sql.Connection; +import java.sql.PreparedStatement; +import java.sql.ResultSet; +import java.sql.SQLException; +import java.util.UUID; public class Upgrade41610to41700 implements DbUpgrade, DbUpgradeSystemVmTemplate { @@ -56,6 +61,7 @@ public class Upgrade41610to41700 implements DbUpgrade, DbUpgradeSystemVmTemplate @Override public void performDataMigration(Connection conn) { + fixWrongPoolUuid(conn); } @Override @@ -83,4 +89,26 @@ public class Upgrade41610to41700 implements DbUpgrade, DbUpgradeSystemVmTemplate throw new CloudRuntimeException("Failed to find / register SystemVM template(s)"); } } + + public void fixWrongPoolUuid(Connection conn) { + LOG.debug("Replacement of faulty pool uuids"); + try (PreparedStatement pstmt = conn.prepareStatement("SELECT id,uuid FROM storage_pool " + + "WHERE uuid NOT LIKE \"%-%-%-%\" AND removed IS NULL;"); ResultSet rs = pstmt.executeQuery()) { + PreparedStatement updateStmt = conn.prepareStatement("update storage_pool set uuid = ? where id = ?"); + while (rs.next()) { + UUID poolUuid = new UUID( + new BigInteger(rs.getString(2).substring(0, 16), 16).longValue(), + new BigInteger(rs.getString(2).substring(16), 16).longValue() + ); + updateStmt.setLong(2, rs.getLong(1)); + updateStmt.setString(1, poolUuid.toString()); + updateStmt.addBatch(); + } + updateStmt.executeBatch(); + } catch (SQLException ex) { + String errorMsg = "fixWrongPoolUuid:Exception while updating faulty pool uuids"; + LOG.error(errorMsg,ex); + throw new CloudRuntimeException(errorMsg, ex); + } + } } diff --git a/server/src/main/java/com/cloud/storage/StorageManagerImpl.java b/server/src/main/java/com/cloud/storage/StorageManagerImpl.java index 3eff3138e38..26f2f41cd0c 100644 --- a/server/src/main/java/com/cloud/storage/StorageManagerImpl.java +++ b/server/src/main/java/com/cloud/storage/StorageManagerImpl.java @@ -231,6 +231,8 @@ import com.cloud.vm.DiskProfile; import com.cloud.vm.VMInstanceVO; import com.cloud.vm.VirtualMachine.State; import com.cloud.vm.dao.VMInstanceDao; +import java.math.BigInteger; +import java.util.UUID; @Component public class StorageManagerImpl extends ManagerBase implements StorageManager, ClusterManagerListener, Configurable { @@ -1818,7 +1820,7 @@ public class StorageManagerImpl extends ManagerBase implements StorageManager, C for (ModifyStoragePoolAnswer childDataStoreAnswer : childDatastoreAnswerList) { StoragePoolInfo childStoragePoolInfo = childDataStoreAnswer.getPoolInfo(); - StoragePoolVO dataStoreVO = _storagePoolDao.findPoolByUUID(childStoragePoolInfo.getUuid()); + StoragePoolVO dataStoreVO = getExistingPoolByUuid(childStoragePoolInfo.getUuid()); if (dataStoreVO == null && childDataStoreAnswer.getPoolType().equalsIgnoreCase("NFS")) { List nfsStoragePools = _storagePoolDao.findPoolsByStorageType(StoragePoolType.NetworkFilesystem.toString()); for (StoragePoolVO storagePool : nfsStoragePools) { @@ -1864,6 +1866,17 @@ public class StorageManagerImpl extends ManagerBase implements StorageManager, C handleRemoveChildStoragePoolFromDatastoreCluster(childDatastoreUUIDs); } + private StoragePoolVO getExistingPoolByUuid(String uuid){ + if(!uuid.contains("-")){ + UUID poolUuid = new UUID( + new BigInteger(uuid.substring(0, 16), 16).longValue(), + new BigInteger(uuid.substring(16), 16).longValue() + ); + uuid = poolUuid.toString(); + } + return _storagePoolDao.findByUuid(uuid); + } + private void validateChildDatastoresToBeAddedInUpState(StoragePoolVO datastoreClusterPool, List childDatastoreAnswerList) { for (ModifyStoragePoolAnswer childDataStoreAnswer : childDatastoreAnswerList) { StoragePoolInfo childStoragePoolInfo = childDataStoreAnswer.getPoolInfo();