sureshanaparti commented on a change in pull request #4871:
URL: https://github.com/apache/cloudstack/pull/4871#discussion_r603900447
##########
File path: server/src/main/java/com/cloud/storage/StorageManagerImpl.java
##########
@@ -1633,6 +1640,223 @@ public PrimaryDataStoreInfo
cancelPrimaryStorageForMaintenance(CancelPrimaryStor
return
(PrimaryDataStoreInfo)_dataStoreMgr.getDataStore(primaryStorage.getId(),
DataStoreRole.Primary);
}
+ @Override
+ @ActionEvent(eventType = EventTypes.EVENT_SYNC_STORAGE_POOL,
eventDescription = "synchronising storage pool with management server", async =
true)
+ public StoragePool syncStoragePool(SyncStoragePoolCmd cmd) {
+ Long poolId = cmd.getPoolId();
+ StoragePoolVO pool = _storagePoolDao.findById(poolId);
+
+ if (pool == null) {
+ String msg = "Unable to obtain lock on the storage pool record
while syncing storage pool with management server";
+ s_logger.error(msg);
+ throw new InvalidParameterValueException(msg);
+ }
+
+ if (!pool.getPoolType().equals(StoragePoolType.DatastoreCluster)) {
+ throw new
InvalidParameterValueException(String.format("SyncStoragePool API is currently
supported only for storage type of datastore cluster"));
+ }
+
+ if (!pool.getStatus().equals(StoragePoolStatus.Up)) {
+ throw new InvalidParameterValueException(String.format("Primary
storage with id %s is not ready for syncing, as the status is %s",
pool.getUuid(), pool.getStatus().toString()));
+ }
+
+ // find the host
+ List<Long> poolIds = new ArrayList<>();
+ poolIds.add(poolId);
+ List<Long> hosts =
_storagePoolHostDao.findHostsConnectedToPools(poolIds);
+ if (hosts.size() > 0) {
+ Long hostId = hosts.get(0);
+ ModifyStoragePoolCommand modifyStoragePoolCommand = new
ModifyStoragePoolCommand(true, pool);
+ final Answer answer = _agentMgr.easySend(hostId,
modifyStoragePoolCommand);
+
+ if (answer == null) {
+ throw new CloudRuntimeException("Unable to get an answer to
the modify storage pool command" + pool.getId());
+ }
+
+ if (!answer.getResult()) {
+ String msg = "Unable to process ModifyStoragePoolCommand for "
+ poolId + " to the host" + hostId;
+ throw new CloudRuntimeException("Unable establish connection
from storage head to storage pool " + pool.getId() + " due to " +
answer.getDetails() +
+ pool.getId());
+ }
+
+ assert (answer instanceof ModifyStoragePoolAnswer) : "Well, now
why won't you actually return the ModifyStoragePoolAnswer when it's
ModifyStoragePoolCommand? Pool=" +
+ pool.getId() + "Host=" + hostId;
+ ModifyStoragePoolAnswer mspAnswer = (ModifyStoragePoolAnswer)
answer;
+ StoragePoolVO poolVO = _storagePoolDao.findById(poolId);
+ updateStoragePoolHostVOAndBytes(poolVO, hostId, mspAnswer);
+ syncDatastoreClusterStoragePool(poolId,
mspAnswer.getDatastoreClusterChildren(), hostId);
+ for (ModifyStoragePoolAnswer childDataStoreAnswer :
mspAnswer.getDatastoreClusterChildren()) {
+ StoragePoolInfo childStoragePoolInfo =
childDataStoreAnswer.getPoolInfo();
+ StoragePoolVO dataStoreVO =
_storagePoolDao.findPoolByUUID(childStoragePoolInfo.getUuid());
+ for (Long host : hosts) {
+ updateStoragePoolHostVOAndBytes(dataStoreVO, host,
childDataStoreAnswer);
+ }
+ }
+
+ }
+ return (PrimaryDataStoreInfo) _dataStoreMgr.getDataStore(pool.getId(),
DataStoreRole.Primary);
+ }
+
+ public void syncDatastoreClusterStoragePool(long datastoreClusterPoolId,
List<ModifyStoragePoolAnswer> childDatastoreAnswerList, long hostId) {
+ StoragePoolVO datastoreClusterPool =
_storagePoolDao.findById(datastoreClusterPoolId);
+ List<String> storageTags =
_storagePoolTagsDao.getStoragePoolTags(datastoreClusterPoolId);
+ List<StoragePoolVO> childDatastores =
_storagePoolDao.listChildStoragePoolsInDatastoreCluster(datastoreClusterPoolId);
+ Set<String> childDatastoreUUIDs = new HashSet<>();
+ for (StoragePoolVO childDatastore : childDatastores) {
+ childDatastoreUUIDs.add(childDatastore.getUuid());
+ }
+
+ for (ModifyStoragePoolAnswer childDataStoreAnswer :
childDatastoreAnswerList) {
+ StoragePoolInfo childStoragePoolInfo =
childDataStoreAnswer.getPoolInfo();
+ StoragePoolVO dataStoreVO =
_storagePoolDao.findPoolByUUID(childStoragePoolInfo.getUuid());
+ if (dataStoreVO != null) {
+ if (dataStoreVO.getParent() != datastoreClusterPoolId) {
+ s_logger.debug(String.format("Storage pool %s with uuid %s
is found to be under datastore cluster %s at vCenter, " +
+ "so moving the storage pool to be a child
storage pool under the datastore cluster in CloudStack management server",
+ childStoragePoolInfo.getName(),
childStoragePoolInfo.getUuid(), datastoreClusterPool.getName()));
+ dataStoreVO.setParent(datastoreClusterPoolId);
+ _storagePoolDao.update(dataStoreVO.getId(), dataStoreVO);
+ if (CollectionUtils.isNotEmpty(storageTags)) {
+
storageTags.addAll(_storagePoolTagsDao.getStoragePoolTags(dataStoreVO.getId()));
+ } else {
+ storageTags =
_storagePoolTagsDao.getStoragePoolTags(dataStoreVO.getId());
+ }
+ if (CollectionUtils.isNotEmpty(storageTags)) {
+ if (s_logger.isDebugEnabled()) {
+ s_logger.debug("Updating Storage Pool Tags to :" +
storageTags);
+ }
+ _storagePoolTagsDao.persist(dataStoreVO.getId(),
storageTags);
+ }
+ } else {
+ // This is to find datastores which are removed from
datastore cluster.
+ // The final set childDatastoreUUIDs contains the UUIDs of
child datastores which needs to be removed from datastore cluster
+ childDatastoreUUIDs.remove(childStoragePoolInfo.getUuid());
+ }
+ } else {
+ dataStoreVO = createChildDatastoreVO(datastoreClusterPool,
childDataStoreAnswer);
+ }
+ updateStoragePoolHostVOAndBytes(dataStoreVO, hostId,
childDataStoreAnswer);
+ }
+
+ handleRemoveChildStoragePoolFromDatastoreCluster(childDatastoreUUIDs);
+ }
+
+ private StoragePoolVO createChildDatastoreVO(StoragePoolVO
datastoreClusterPool, ModifyStoragePoolAnswer childDataStoreAnswer) {
+ StoragePoolInfo childStoragePoolInfo =
childDataStoreAnswer.getPoolInfo();
+ List<String> storageTags =
_storagePoolTagsDao.getStoragePoolTags(datastoreClusterPool.getId());
+
+ StoragePoolVO dataStoreVO = new StoragePoolVO();
+
dataStoreVO.setStorageProviderName(datastoreClusterPool.getStorageProviderName());
+ dataStoreVO.setHostAddress(childStoragePoolInfo.getHost());
+ dataStoreVO.setPoolType(Storage.StoragePoolType.PreSetup);
+ dataStoreVO.setPath(childStoragePoolInfo.getHostPath());
+ dataStoreVO.setPort(datastoreClusterPool.getPort());
+ dataStoreVO.setName(childStoragePoolInfo.getName());
+ dataStoreVO.setUuid(childStoragePoolInfo.getUuid());
+ dataStoreVO.setDataCenterId(datastoreClusterPool.getDataCenterId());
+ dataStoreVO.setPodId(datastoreClusterPool.getPodId());
+ dataStoreVO.setClusterId(datastoreClusterPool.getClusterId());
+ dataStoreVO.setStatus(StoragePoolStatus.Up);
+ dataStoreVO.setUserInfo(datastoreClusterPool.getUserInfo());
+ dataStoreVO.setManaged(datastoreClusterPool.isManaged());
+ dataStoreVO.setCapacityIops(datastoreClusterPool.getCapacityIops());
+
dataStoreVO.setCapacityBytes(childDataStoreAnswer.getPoolInfo().getCapacityBytes());
+
dataStoreVO.setUsedBytes(childDataStoreAnswer.getPoolInfo().getCapacityBytes()
- childDataStoreAnswer.getPoolInfo().getAvailableBytes());
+ dataStoreVO.setHypervisor(datastoreClusterPool.getHypervisor());
+ dataStoreVO.setScope(datastoreClusterPool.getScope());
+ dataStoreVO.setParent(datastoreClusterPool.getId());
+
+ Map<String, String> details = new HashMap<>();
+
if(org.apache.commons.lang.StringUtils.isNotEmpty(childDataStoreAnswer.getPoolType()))
{
+ details.put("pool_type", childDataStoreAnswer.getPoolType());
+ }
+ _storagePoolDao.persist(dataStoreVO, details, storageTags);
+ return dataStoreVO;
+ }
+
+ private void handleRemoveChildStoragePoolFromDatastoreCluster(Set<String>
childDatastoreUUIDs) {
+
+ for (String childDatastoreUUID : childDatastoreUUIDs) {
+ StoragePoolVO dataStoreVO =
_storagePoolDao.findPoolByUUID(childDatastoreUUID);
+ List<VolumeVO> allVolumes =
_volumeDao.findByPoolId(dataStoreVO.getId());
+
+ for (VolumeVO volume : allVolumes) {
+ VMInstanceVO vmInstance =
_vmInstanceDao.findById(volume.getInstanceId());
+ if (vmInstance == null) {
+ continue;
Review comment:
ok, thanks for the confirmation @harikrishna-patnala
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
For queries about this service, please contact Infrastructure at:
[email protected]