|
29 | 29 | import org.apache.log4j.Logger; |
30 | 30 | import org.springframework.stereotype.Component; |
31 | 31 |
|
| 32 | +import com.cloud.exception.InvalidParameterValueException; |
32 | 33 | import com.cloud.hypervisor.Hypervisor.HypervisorType; |
33 | 34 | import com.cloud.server.ResourceTag.TaggedResourceType; |
| 35 | +import com.cloud.storage.ScopeType; |
34 | 36 | import com.cloud.storage.Storage.ImageFormat; |
35 | 37 | import com.cloud.storage.Volume; |
36 | 38 | import com.cloud.storage.Volume.Event; |
@@ -70,7 +72,9 @@ public class VolumeDaoImpl extends GenericDaoBase<VolumeVO, Long> implements Vol |
70 | 72 | // need to account for zone-wide primary storage where storage_pool has |
71 | 73 | // null-value pod and cluster, where hypervisor information is stored in |
72 | 74 | // storage_pool |
73 | | - protected static final String SELECT_HYPERTYPE_FROM_VOLUME = "SELECT s.hypervisor, c.hypervisor_type from volumes v, storage_pool s, cluster c where v.pool_id = s.id and s.cluster_id = c.id and v.id = ?"; |
| 75 | + protected static final String SELECT_HYPERTYPE_FROM_CLUSTER_VOLUME = "SELECT c.hypervisor_type from volumes v, storage_pool s, cluster c where v.pool_id = s.id and s.cluster_id = c.id and v.id = ?"; |
| 76 | + protected static final String SELECT_HYPERTYPE_FROM_ZONE_VOLUME = "SELECT s.hypervisor from volumes v, storage_pool s where v.pool_id = s.id and v.id = ?"; |
| 77 | + protected static final String SELECT_POOLSCOPE = "SELECT s.scope from storage_pool s, volumes v where s.id = v.pool_id and v.id = ?"; |
74 | 78 |
|
75 | 79 | private static final String ORDER_POOLS_NUMBER_OF_VOLUMES_FOR_ACCOUNT = "SELECT pool.id, SUM(IF(vol.state='Ready' AND vol.account_id = ?, 1, 0)) FROM `cloud`.`storage_pool` pool LEFT JOIN `cloud`.`volumes` vol ON pool.id = vol.pool_id WHERE pool.data_center_id = ? " |
76 | 80 | + " AND pool.pod_id = ? AND pool.cluster_id = ? " + " GROUP BY pool.id ORDER BY 2 ASC "; |
@@ -234,25 +238,29 @@ public HypervisorType getHypervisorType(long volumeId) { |
234 | 238 | /* lookup from cluster of pool */ |
235 | 239 | Transaction txn = Transaction.currentTxn(); |
236 | 240 | PreparedStatement pstmt = null; |
237 | | - |
| 241 | + String sql = null; |
238 | 242 | try { |
239 | | - String sql = SELECT_HYPERTYPE_FROM_VOLUME; |
240 | | - pstmt = txn.prepareAutoCloseStatement(sql); |
241 | | - pstmt.setLong(1, volumeId); |
242 | | - ResultSet rs = pstmt.executeQuery(); |
243 | | - if (rs.next()) { |
244 | | - String hypervisor; |
245 | | - if (rs.getString(1) != null) |
246 | | - hypervisor = rs.getString(1); |
247 | | - else |
248 | | - hypervisor = rs.getString(2); |
249 | | - return HypervisorType.getType(hypervisor); |
| 243 | + ScopeType scope = getVolumeStoragePoolScope(volumeId); |
| 244 | + if (scope != null ) { |
| 245 | + if (scope == ScopeType.CLUSTER) |
| 246 | + sql = SELECT_HYPERTYPE_FROM_CLUSTER_VOLUME; |
| 247 | + else if (scope == ScopeType.ZONE) |
| 248 | + sql = SELECT_HYPERTYPE_FROM_ZONE_VOLUME; |
| 249 | + |
| 250 | + pstmt = txn.prepareAutoCloseStatement(sql); |
| 251 | + pstmt.setLong(1, volumeId); |
| 252 | + ResultSet rs = pstmt.executeQuery(); |
| 253 | + if (rs.next()) { |
| 254 | + if (rs.getString(1) != null) { |
| 255 | + return HypervisorType.getType(rs.getString(1)); |
| 256 | + } |
| 257 | + } |
250 | 258 | } |
251 | 259 | return HypervisorType.None; |
252 | 260 | } catch (SQLException e) { |
253 | | - throw new CloudRuntimeException("DB Exception on: " + SELECT_HYPERTYPE_FROM_VOLUME, e); |
| 261 | + throw new CloudRuntimeException("DB Exception on: " + sql, e); |
254 | 262 | } catch (Throwable e) { |
255 | | - throw new CloudRuntimeException("Caught: " + SELECT_HYPERTYPE_FROM_VOLUME, e); |
| 263 | + throw new CloudRuntimeException("Caught: " + sql, e); |
256 | 264 | } |
257 | 265 | } |
258 | 266 |
|
@@ -494,4 +502,33 @@ public boolean remove(Long id) { |
494 | 502 | txn.commit(); |
495 | 503 | return result; |
496 | 504 | } |
| 505 | + |
| 506 | + @Override |
| 507 | + public ScopeType getVolumeStoragePoolScope(long volumeId) { |
| 508 | + // finding the storage scope where the volume is present |
| 509 | + Transaction txn = Transaction.currentTxn(); |
| 510 | + PreparedStatement pstmt = null; |
| 511 | + |
| 512 | + try { |
| 513 | + String sql = SELECT_POOLSCOPE; |
| 514 | + pstmt = txn.prepareAutoCloseStatement(sql); |
| 515 | + pstmt.setLong(1, volumeId); |
| 516 | + ResultSet rs = pstmt.executeQuery(); |
| 517 | + if (rs.next()) { |
| 518 | + String scope = rs.getString(1); |
| 519 | + if (scope != null) { |
| 520 | + try { |
| 521 | + return Enum.valueOf(ScopeType.class, scope.toUpperCase()); |
| 522 | + } catch (Exception e) { |
| 523 | + throw new InvalidParameterValueException("invalid scope for pool " + scope); |
| 524 | + } |
| 525 | + } |
| 526 | + } |
| 527 | + } catch (SQLException e) { |
| 528 | + throw new CloudRuntimeException("DB Exception on: " + SELECT_POOLSCOPE, e); |
| 529 | + } catch (Throwable e) { |
| 530 | + throw new CloudRuntimeException("Caught: " + SELECT_POOLSCOPE, e); |
| 531 | + } |
| 532 | + return null; |
| 533 | + } |
497 | 534 | } |
0 commit comments