|
24 | 24 | import java.util.HashMap; |
25 | 25 | import java.util.List; |
26 | 26 | import java.util.Map; |
| 27 | +import java.util.Objects; |
27 | 28 | import java.util.TimeZone; |
28 | 29 |
|
29 | 30 | import javax.annotation.PostConstruct; |
30 | 31 | import javax.ejb.Local; |
31 | 32 | import javax.inject.Inject; |
32 | 33 | import javax.persistence.TableGenerator; |
33 | 34 |
|
34 | | -import org.apache.cloudstack.storage.datastore.db.StoragePoolVO; |
35 | | -import org.apache.commons.lang.StringUtils; |
36 | 35 | import org.apache.log4j.Logger; |
37 | 36 | import org.springframework.stereotype.Component; |
38 | 37 |
|
|
50 | 49 | import com.cloud.host.Status; |
51 | 50 | import com.cloud.host.Status.Event; |
52 | 51 | import com.cloud.hypervisor.Hypervisor; |
| 52 | +import com.cloud.hypervisor.Hypervisor.HypervisorType; |
53 | 53 | import com.cloud.info.RunningHostCountInfo; |
54 | 54 | import com.cloud.org.Grouping; |
55 | 55 | import com.cloud.org.Managed; |
@@ -1127,44 +1127,43 @@ public List<HostVO> listByType(Host.Type type) { |
1127 | 1127 | return listBy(sc); |
1128 | 1128 | } |
1129 | 1129 |
|
1130 | | - String sqlFindHostConnectedToStoragePoolToExecuteCommand = "select h.id from storage_pool pool " + " join cluster c on pool.cluster_id = c.id " + " %s " |
1131 | | - + " join host h on h.data_center_id = c.data_center_id and h.hypervisor_type = c.hypervisor_type" |
1132 | | - + " where pool.id = ? and h.status = 'Up' and h.type = 'Routing' and resource_state = '%s' " + " ORDER by rand() limit 1 "; |
| 1130 | + |
| 1131 | + String sqlFindHostInZoneToExecuteCommand = "Select id from host " |
| 1132 | + + " where type = 'Routing' and hypervisor_type = ? and data_center_id = ? and status = 'Up' " |
| 1133 | + + " and resource_state = '%s' " |
| 1134 | + + " ORDER by rand() limit 1"; |
1133 | 1135 |
|
1134 | 1136 | @Override |
1135 | | - public HostVO findHostToOperateOnSnapshotBasedOnStoragePool(StoragePoolVO storagePoolVO) { |
| 1137 | + public HostVO findHostInZoneToExecuteCommand(long zoneId, HypervisorType hypervisorType) { |
1136 | 1138 | try (TransactionLegacy tx = TransactionLegacy.currentTxn()) { |
1137 | | - String sql = createSqlFindHostConnectedToStoragePoolToExecuteCommand(storagePoolVO, false); |
1138 | | - ResultSet rs = executeSqlGetResultSetForMethodFindHostToOperateBasedOnStoragePool(storagePoolVO, tx, sql); |
| 1139 | + String sql = createSqlFindHostToExecuteCommand(false); |
| 1140 | + ResultSet rs = executeSqlGetResultsetForMethodFindHostInZoneToExecuteCommand(hypervisorType, zoneId, tx, sql); |
1139 | 1141 | if (rs.next()) { |
1140 | 1142 | return findById(rs.getLong("id")); |
1141 | 1143 | } |
1142 | | - sql = createSqlFindHostConnectedToStoragePoolToExecuteCommand(storagePoolVO, true); |
1143 | | - rs = executeSqlGetResultSetForMethodFindHostToOperateBasedOnStoragePool(storagePoolVO, tx, sql); |
| 1144 | + sql = createSqlFindHostToExecuteCommand(true); |
| 1145 | + rs = executeSqlGetResultsetForMethodFindHostInZoneToExecuteCommand(hypervisorType, zoneId, tx, sql); |
1144 | 1146 | if (!rs.next()) { |
1145 | | - throw new CloudRuntimeException(String.format("Could not find a host connected to the storage pool [storagepool=%d]. ", storagePoolVO.getId())); |
| 1147 | + throw new CloudRuntimeException(String.format("Could not find a host in zone [zoneId=%d] to operate on. ", zoneId)); |
1146 | 1148 | } |
1147 | 1149 | return findById(rs.getLong("id")); |
1148 | 1150 | } catch (SQLException e) { |
1149 | 1151 | throw new CloudRuntimeException(e); |
1150 | 1152 | } |
1151 | 1153 | } |
1152 | 1154 |
|
1153 | | - private ResultSet executeSqlGetResultSetForMethodFindHostToOperateBasedOnStoragePool(StoragePoolVO storagePoolVO, TransactionLegacy tx, String sql) throws SQLException { |
| 1155 | + private ResultSet executeSqlGetResultsetForMethodFindHostInZoneToExecuteCommand(HypervisorType hypervisorType, long zoneId, TransactionLegacy tx, String sql) throws SQLException { |
1154 | 1156 | PreparedStatement pstmt = tx.prepareAutoCloseStatement(sql); |
1155 | | - pstmt.setLong(1, storagePoolVO.getId()); |
| 1157 | + pstmt.setString(1, Objects.toString(hypervisorType)); |
| 1158 | + pstmt.setLong(2, zoneId); |
1156 | 1159 | return pstmt.executeQuery(); |
1157 | 1160 | } |
1158 | 1161 |
|
1159 | | - private String createSqlFindHostConnectedToStoragePoolToExecuteCommand(StoragePoolVO storagePoolVO, boolean useDisabledHosts) { |
| 1162 | + private String createSqlFindHostToExecuteCommand(boolean useDisabledHosts) { |
1160 | 1163 | String hostResourceStatus = "Enabled"; |
1161 | 1164 | if (useDisabledHosts) { |
1162 | 1165 | hostResourceStatus = "Disabled"; |
1163 | 1166 | } |
1164 | | - String joinForManagedStorage = StringUtils.EMPTY; |
1165 | | - if (storagePoolVO.isManaged()) { |
1166 | | - joinForManagedStorage = "join cluster_details cd on cd.cluster_id = c.id and cd.name = 'supportsResign' and cd.value = 'true'"; |
1167 | | - } |
1168 | | - return String.format(sqlFindHostConnectedToStoragePoolToExecuteCommand, joinForManagedStorage, hostResourceStatus); |
| 1167 | + return String.format(sqlFindHostInZoneToExecuteCommand, hostResourceStatus); |
1169 | 1168 | } |
1170 | 1169 | } |
0 commit comments