Skip to content

Commit 18d9c6d

Browse files
DK101010DK101010
andauthored
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 <[email protected]>
1 parent 5cf8064 commit 18d9c6d

File tree

3 files changed

+43
-1
lines changed

3 files changed

+43
-1
lines changed

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434

3535
import com.cloud.utils.exception.CloudRuntimeException;
3636

37+
3738
public class Upgrade41520to41600 implements DbUpgrade, DbUpgradeSystemVmTemplate {
3839

3940
final static Logger LOG = Logger.getLogger(Upgrade41520to41600.class);

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

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,12 @@
2121
import org.apache.log4j.Logger;
2222

2323
import java.io.InputStream;
24+
import java.math.BigInteger;
2425
import java.sql.Connection;
26+
import java.sql.PreparedStatement;
27+
import java.sql.ResultSet;
28+
import java.sql.SQLException;
29+
import java.util.UUID;
2530

2631
public class Upgrade41610to41700 implements DbUpgrade, DbUpgradeSystemVmTemplate {
2732

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

5762
@Override
5863
public void performDataMigration(Connection conn) {
64+
fixWrongPoolUuid(conn);
5965
}
6066

6167
@Override
@@ -83,4 +89,26 @@ public void updateSystemVmTemplates(Connection conn) {
8389
throw new CloudRuntimeException("Failed to find / register SystemVM template(s)");
8490
}
8591
}
92+
93+
public void fixWrongPoolUuid(Connection conn) {
94+
LOG.debug("Replacement of faulty pool uuids");
95+
try (PreparedStatement pstmt = conn.prepareStatement("SELECT id,uuid FROM storage_pool "
96+
+ "WHERE uuid NOT LIKE \"%-%-%-%\" AND removed IS NULL;"); ResultSet rs = pstmt.executeQuery()) {
97+
PreparedStatement updateStmt = conn.prepareStatement("update storage_pool set uuid = ? where id = ?");
98+
while (rs.next()) {
99+
UUID poolUuid = new UUID(
100+
new BigInteger(rs.getString(2).substring(0, 16), 16).longValue(),
101+
new BigInteger(rs.getString(2).substring(16), 16).longValue()
102+
);
103+
updateStmt.setLong(2, rs.getLong(1));
104+
updateStmt.setString(1, poolUuid.toString());
105+
updateStmt.addBatch();
106+
}
107+
updateStmt.executeBatch();
108+
} catch (SQLException ex) {
109+
String errorMsg = "fixWrongPoolUuid:Exception while updating faulty pool uuids";
110+
LOG.error(errorMsg,ex);
111+
throw new CloudRuntimeException(errorMsg, ex);
112+
}
113+
}
86114
}

server/src/main/java/com/cloud/storage/StorageManagerImpl.java

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -231,6 +231,8 @@
231231
import com.cloud.vm.VMInstanceVO;
232232
import com.cloud.vm.VirtualMachine.State;
233233
import com.cloud.vm.dao.VMInstanceDao;
234+
import java.math.BigInteger;
235+
import java.util.UUID;
234236

235237
@Component
236238
public class StorageManagerImpl extends ManagerBase implements StorageManager, ClusterManagerListener, Configurable {
@@ -1818,7 +1820,7 @@ public void syncDatastoreClusterStoragePool(long datastoreClusterPoolId, List<Mo
18181820

18191821
for (ModifyStoragePoolAnswer childDataStoreAnswer : childDatastoreAnswerList) {
18201822
StoragePoolInfo childStoragePoolInfo = childDataStoreAnswer.getPoolInfo();
1821-
StoragePoolVO dataStoreVO = _storagePoolDao.findPoolByUUID(childStoragePoolInfo.getUuid());
1823+
StoragePoolVO dataStoreVO = getExistingPoolByUuid(childStoragePoolInfo.getUuid());
18221824
if (dataStoreVO == null && childDataStoreAnswer.getPoolType().equalsIgnoreCase("NFS")) {
18231825
List<StoragePoolVO> nfsStoragePools = _storagePoolDao.findPoolsByStorageType(StoragePoolType.NetworkFilesystem.toString());
18241826
for (StoragePoolVO storagePool : nfsStoragePools) {
@@ -1864,6 +1866,17 @@ public void syncDatastoreClusterStoragePool(long datastoreClusterPoolId, List<Mo
18641866
handleRemoveChildStoragePoolFromDatastoreCluster(childDatastoreUUIDs);
18651867
}
18661868

1869+
private StoragePoolVO getExistingPoolByUuid(String uuid){
1870+
if(!uuid.contains("-")){
1871+
UUID poolUuid = new UUID(
1872+
new BigInteger(uuid.substring(0, 16), 16).longValue(),
1873+
new BigInteger(uuid.substring(16), 16).longValue()
1874+
);
1875+
uuid = poolUuid.toString();
1876+
}
1877+
return _storagePoolDao.findByUuid(uuid);
1878+
}
1879+
18671880
private void validateChildDatastoresToBeAddedInUpState(StoragePoolVO datastoreClusterPool, List<ModifyStoragePoolAnswer> childDatastoreAnswerList) {
18681881
for (ModifyStoragePoolAnswer childDataStoreAnswer : childDatastoreAnswerList) {
18691882
StoragePoolInfo childStoragePoolInfo = childDataStoreAnswer.getPoolInfo();

0 commit comments

Comments
 (0)