Skip to content
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@

import com.cloud.utils.exception.CloudRuntimeException;


public class Upgrade41520to41600 implements DbUpgrade, DbUpgradeSystemVmTemplate {

final static Logger LOG = Logger.getLogger(Upgrade41520to41600.class);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,12 @@
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 {

Expand Down Expand Up @@ -56,6 +61,7 @@ public InputStream[] getPrepareScripts() {

@Override
public void performDataMigration(Connection conn) {
fixWrongPoolUuid(conn);
}

@Override
Expand Down Expand Up @@ -83,4 +89,26 @@ public void updateSystemVmTemplates(Connection conn) {
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);
}
}
}
15 changes: 14 additions & 1 deletion server/src/main/java/com/cloud/storage/StorageManagerImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -230,6 +230,8 @@
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 {
Expand Down Expand Up @@ -1798,7 +1800,7 @@ public void syncDatastoreClusterStoragePool(long datastoreClusterPoolId, List<Mo

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<StoragePoolVO> nfsStoragePools = _storagePoolDao.findPoolsByStorageType(StoragePoolType.NetworkFilesystem.toString());
for (StoragePoolVO storagePool : nfsStoragePools) {
Expand Down Expand Up @@ -1844,6 +1846,17 @@ public void syncDatastoreClusterStoragePool(long datastoreClusterPoolId, List<Mo
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<ModifyStoragePoolAnswer> childDatastoreAnswerList) {
for (ModifyStoragePoolAnswer childDataStoreAnswer : childDatastoreAnswerList) {
StoragePoolInfo childStoragePoolInfo = childDataStoreAnswer.getPoolInfo();
Expand Down