Skip to content

Commit 2934295

Browse files
committed
Use configDepot.isNewConfig() to populate volume.allocation.algorithm
1 parent 5c10036 commit 2934295

File tree

3 files changed

+18
-67
lines changed

3 files changed

+18
-67
lines changed

engine/orchestration/src/main/java/org/apache/cloudstack/engine/orchestration/VolumeOrchestrator.java

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@
3838
import javax.inject.Inject;
3939
import javax.naming.ConfigurationException;
4040

41+
import com.cloud.deploy.DeploymentClusterPlanner;
4142
import com.cloud.exception.ResourceAllocationException;
4243
import com.cloud.storage.DiskOfferingVO;
4344
import com.cloud.storage.VMTemplateVO;
@@ -73,6 +74,7 @@
7374
import org.apache.cloudstack.framework.config.ConfigDepot;
7475
import org.apache.cloudstack.framework.config.ConfigKey;
7576
import org.apache.cloudstack.framework.config.Configurable;
77+
import org.apache.cloudstack.framework.config.dao.ConfigurationDao;
7678
import org.apache.cloudstack.framework.jobs.AsyncJobManager;
7779
import org.apache.cloudstack.framework.jobs.impl.AsyncJobVO;
7880
import org.apache.cloudstack.resourcedetail.DiskOfferingDetailVO;
@@ -257,6 +259,10 @@ public enum UserVmCloneType {
257259
StoragePoolHostDao storagePoolHostDao;
258260
@Inject
259261
DiskOfferingDao diskOfferingDao;
262+
@Inject
263+
ConfigDepot configDepot;
264+
@Inject
265+
ConfigurationDao configurationDao;
260266

261267
@Inject
262268
protected SnapshotHelper snapshotHelper;
@@ -2033,6 +2039,18 @@ public boolean configure(String name, Map<String, Object> params) throws Configu
20332039
return true;
20342040
}
20352041

2042+
@Override
2043+
public boolean start() {
2044+
if (configDepot.isNewConfig(VolumeOrchestrationService.VolumeAllocationAlgorithm)) {
2045+
String vmAllocationAlgo = DeploymentClusterPlanner.VmAllocationAlgorithm.value();
2046+
if (com.cloud.utils.StringUtils.isNotEmpty(vmAllocationAlgo) && !"random".equalsIgnoreCase(vmAllocationAlgo)) {
2047+
logger.debug("Updating value for configuration: {} to {}", VolumeOrchestrationService.VolumeAllocationAlgorithm.key(), vmAllocationAlgo);
2048+
configurationDao.update(VolumeOrchestrationService.VolumeAllocationAlgorithm.key(), vmAllocationAlgo);
2049+
}
2050+
}
2051+
return true;
2052+
}
2053+
20362054
private void cleanupVolumeDuringAttachFailure(Long volumeId, Long vmId) {
20372055
VolumeVO volume = _volsDao.findById(volumeId);
20382056
if (volume == null) {

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

Lines changed: 0 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@
2323
import java.io.InputStream;
2424
import java.sql.Connection;
2525
import java.sql.PreparedStatement;
26-
import java.sql.ResultSet;
2726
import java.sql.SQLException;
2827
import java.util.List;
2928

@@ -61,7 +60,6 @@ public InputStream[] getPrepareScripts() {
6160
@Override
6261
public void performDataMigration(Connection conn) {
6362
migrateConfigurationScopeToBitmask(conn);
64-
migrateVolumeAllocationAlgorithm(conn);
6563
}
6664

6765
@Override
@@ -120,38 +118,4 @@ protected void migrateExistingConfigurationScopeValues(Connection conn) {
120118
throw new CloudRuntimeException(String.format("Failed to migrate existing configuration scope values to bitmask due to: %s", e.getMessage()));
121119
}
122120
}
123-
124-
void migrateVolumeAllocationAlgorithm(Connection conn) {
125-
ResultSet rs = null;
126-
PreparedStatement pstmt = null;
127-
try {
128-
String vmAllocationAlgo = "random";
129-
pstmt = conn.prepareStatement("SELECT value FROM `cloud`.`configuration` where name = 'vm.allocation.algorithm'");
130-
rs = pstmt.executeQuery();
131-
if (rs.next()) {
132-
vmAllocationAlgo = rs.getString(1);
133-
}
134-
rs.close();
135-
pstmt.close();
136-
137-
if (!"random".equalsIgnoreCase(vmAllocationAlgo)) {
138-
pstmt = conn.prepareStatement("UPDATE `cloud`.`configuration` SET value = ? WHERE name = 'volume.allocation.algorithm'");
139-
pstmt.setString(1, vmAllocationAlgo);
140-
pstmt.executeUpdate();
141-
}
142-
} catch (SQLException e) {
143-
throw new CloudRuntimeException("Unable to migrate the volume.allocation.algorithm", e);
144-
} finally {
145-
try {
146-
if (rs != null) {
147-
rs.close();
148-
}
149-
if (pstmt != null) {
150-
pstmt.close();
151-
}
152-
} catch (SQLException e) {
153-
logger.info("[ignored]",e);
154-
}
155-
}
156-
}
157121
}

engine/schema/src/test/java/com/cloud/upgrade/dao/Upgrade42010to42100Test.java

Lines changed: 0 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -16,15 +16,12 @@
1616
// under the License.
1717
package com.cloud.upgrade.dao;
1818

19-
import static org.mockito.Mockito.doNothing;
2019
import static org.mockito.Mockito.when;
2120

2221
import java.sql.Connection;
2322
import java.sql.PreparedStatement;
24-
import java.sql.ResultSet;
2523
import java.sql.SQLException;
2624

27-
import org.junit.Before;
2825
import org.junit.Test;
2926
import org.junit.runner.RunWith;
3027
import org.mockito.Mock;
@@ -40,24 +37,9 @@ public class Upgrade42010to42100Test {
4037
@Spy
4138
Upgrade42010to42100 upgrade;
4239

43-
@Mock
44-
private PreparedStatement mockPreparedStatement;
45-
@Mock
46-
private ResultSet mockResultSet;
4740
@Mock
4841
private Connection conn;
4942

50-
51-
@Before
52-
public void setup() throws Exception {
53-
when(conn.prepareStatement("SELECT value FROM `cloud`.`configuration` where name = 'vm.allocation.algorithm'"))
54-
.thenReturn(mockPreparedStatement);
55-
when(mockPreparedStatement.executeQuery()).thenReturn(mockResultSet);
56-
when(mockResultSet.next()).thenReturn(true);
57-
when(mockResultSet.getString(1)).thenReturn("random");
58-
}
59-
60-
6143
@Test
6244
public void testPerformDataMigration() throws SQLException {
6345
try (MockedStatic<DbUpgradeUtils> ignored = Mockito.mockStatic(DbUpgradeUtils.class)) {
@@ -88,17 +70,4 @@ public void testPerformDataMigration() throws SQLException {
8870
}
8971
}
9072
}
91-
92-
@Test
93-
public void testPerformDataMigrationShouldUpdateVolumeAlgorithm() throws SQLException {
94-
when(mockResultSet.getString(1)).thenReturn("userdispersing");
95-
96-
PreparedStatement updateStmt = Mockito.mock(PreparedStatement.class);
97-
when(conn.prepareStatement("UPDATE `cloud`.`configuration` SET value = ? WHERE name = 'volume.allocation.algorithm'"))
98-
.thenReturn(updateStmt);
99-
doNothing().when(upgrade).migrateConfigurationScopeToBitmask(conn);
100-
upgrade.performDataMigration(conn);
101-
Mockito.verify(updateStmt).setString(1, "userdispersing");
102-
Mockito.verify(updateStmt).executeUpdate();
103-
}
10473
}

0 commit comments

Comments
 (0)