Skip to content

Commit 5c10036

Browse files
committed
Add upgrade patch for volume.allocation.algorithm
1 parent c3006bf commit 5c10036

File tree

2 files changed

+67
-0
lines changed

2 files changed

+67
-0
lines changed

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

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

@@ -60,6 +61,7 @@ public InputStream[] getPrepareScripts() {
6061
@Override
6162
public void performDataMigration(Connection conn) {
6263
migrateConfigurationScopeToBitmask(conn);
64+
migrateVolumeAllocationAlgorithm(conn);
6365
}
6466

6567
@Override
@@ -118,4 +120,38 @@ protected void migrateExistingConfigurationScopeValues(Connection conn) {
118120
throw new CloudRuntimeException(String.format("Failed to migrate existing configuration scope values to bitmask due to: %s", e.getMessage()));
119121
}
120122
}
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+
}
121157
}

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

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

19+
import static org.mockito.Mockito.doNothing;
1920
import static org.mockito.Mockito.when;
2021

2122
import java.sql.Connection;
2223
import java.sql.PreparedStatement;
24+
import java.sql.ResultSet;
2325
import java.sql.SQLException;
2426

27+
import org.junit.Before;
2528
import org.junit.Test;
2629
import org.junit.runner.RunWith;
2730
import org.mockito.Mock;
@@ -37,9 +40,24 @@ public class Upgrade42010to42100Test {
3740
@Spy
3841
Upgrade42010to42100 upgrade;
3942

43+
@Mock
44+
private PreparedStatement mockPreparedStatement;
45+
@Mock
46+
private ResultSet mockResultSet;
4047
@Mock
4148
private Connection conn;
4249

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+
4361
@Test
4462
public void testPerformDataMigration() throws SQLException {
4563
try (MockedStatic<DbUpgradeUtils> ignored = Mockito.mockStatic(DbUpgradeUtils.class)) {
@@ -70,4 +88,17 @@ public void testPerformDataMigration() throws SQLException {
7088
}
7189
}
7290
}
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+
}
73104
}

0 commit comments

Comments
 (0)