rp- commented on a change in pull request #4994: URL: https://github.com/apache/cloudstack/pull/4994#discussion_r626596161
########## File path: plugins/storage/volume/linstor/src/main/java/org/apache/cloudstack/storage/datastore/driver/LinstorPrimaryDataStoreDriverImpl.java ########## @@ -0,0 +1,420 @@ +// Licensed to the Apache Software Foundation (ASF) under one +// or more contributor license agreements. See the NOTICE file +// distributed with this work for additional information +// regarding copyright ownership. The ASF licenses this file +// to you under the Apache License, Version 2.0 (the +// "License"); you may not use this file except in compliance +// with the License. You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, +// software distributed under the License is distributed on an +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +// KIND, either express or implied. See the License for the +// specific language governing permissions and limitations +// under the License. +package org.apache.cloudstack.storage.datastore.driver; + +import com.linbit.linstor.api.ApiCallChecker; +import com.linbit.linstor.api.ApiException; +import com.linbit.linstor.api.DevelopersApi; +import com.linbit.linstor.api.model.ApiCallRc; +import com.linbit.linstor.api.model.ApiCallRcList; +import com.linbit.linstor.api.model.ResourceGroupSpawn; +import com.linbit.linstor.api.model.ResourceWithVolumes; +import com.linbit.linstor.api.model.VolumeDefinitionModify; + +import javax.inject.Inject; +import java.util.Collections; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +import com.cloud.agent.api.Answer; +import com.cloud.agent.api.to.DataObjectType; +import com.cloud.agent.api.to.DataStoreTO; +import com.cloud.agent.api.to.DataTO; +import com.cloud.host.Host; +import com.cloud.storage.ResizeVolumePayload; +import com.cloud.storage.StoragePool; +import com.cloud.storage.VolumeVO; +import com.cloud.storage.dao.VolumeDao; +import com.cloud.utils.Pair; +import com.cloud.utils.exception.CloudRuntimeException; +import org.apache.cloudstack.engine.subsystem.api.storage.ChapInfo; +import org.apache.cloudstack.engine.subsystem.api.storage.CopyCommandResult; +import org.apache.cloudstack.engine.subsystem.api.storage.CreateCmdResult; +import org.apache.cloudstack.engine.subsystem.api.storage.DataObject; +import org.apache.cloudstack.engine.subsystem.api.storage.DataStore; +import org.apache.cloudstack.engine.subsystem.api.storage.DataStoreCapabilities; +import org.apache.cloudstack.engine.subsystem.api.storage.PrimaryDataStoreDriver; +import org.apache.cloudstack.engine.subsystem.api.storage.SnapshotInfo; +import org.apache.cloudstack.engine.subsystem.api.storage.TemplateInfo; +import org.apache.cloudstack.engine.subsystem.api.storage.VolumeInfo; +import org.apache.cloudstack.framework.async.AsyncCompletionCallback; +import org.apache.cloudstack.storage.command.CommandResult; +import org.apache.cloudstack.storage.datastore.db.PrimaryDataStoreDao; +import org.apache.cloudstack.storage.datastore.db.StoragePoolVO; +import org.apache.cloudstack.storage.datastore.util.LinstorUtil; +import org.apache.cloudstack.storage.volume.VolumeObject; +import org.apache.log4j.Logger; + +public class LinstorPrimaryDataStoreDriverImpl implements PrimaryDataStoreDriver { + private static final Logger s_logger = Logger.getLogger(LinstorPrimaryDataStoreDriverImpl.class); + @Inject + private PrimaryDataStoreDao _storagePoolDao; + @Inject + private VolumeDao _volumeDao; + + public LinstorPrimaryDataStoreDriverImpl() + { + } + + @Override + public Map<String, String> getCapabilities() + { + Map<String, String> mapCapabilities = new HashMap<>(); + + // Linstor can create a volume from a volume via snapshot restore, but this would only work + // if a LVM-thin storage pool is used, TBH it would also work with ZFS pools, but there you always + // have to keep in mind the child-parent snapshot relationship + // So we will rely on a plain copy volume mechanism which is more robust for now + mapCapabilities.put(DataStoreCapabilities.CAN_CREATE_VOLUME_FROM_VOLUME.toString(), Boolean.FALSE.toString()); + + // for now, technically possible + mapCapabilities.put(DataStoreCapabilities.STORAGE_SYSTEM_SNAPSHOT.toString(), Boolean.FALSE.toString()); + // CAN_CREATE_VOLUME_FROM_SNAPSHOT see note from CAN_CREATE_VOLUME_FROM_VOLUME + mapCapabilities.put(DataStoreCapabilities.CAN_CREATE_VOLUME_FROM_SNAPSHOT.toString(), Boolean.FALSE.toString()); + mapCapabilities.put(DataStoreCapabilities.CAN_REVERT_VOLUME_TO_SNAPSHOT.toString(), Boolean.FALSE.toString()); + + mapCapabilities.put(DataStoreCapabilities.VOLUME_SNAPSHOT_QUIESCEVM.toString(), Boolean.FALSE.toString()); + + return mapCapabilities; + } + + @Override + public DataTO getTO(DataObject data) + { + return null; + } + + @Override + public DataStoreTO getStoreTO(DataStore store) + { + return null; + } + + @Override + public ChapInfo getChapInfo(DataObject dataObject) + { + return null; + } + + @Override + public boolean grantAccess(DataObject dataObject, Host host, DataStore dataStore) + { + return false; + } + + @Override + public void revokeAccess(DataObject dataObject, Host host, DataStore dataStore) + { + } + + @Override + public long getUsedBytes(StoragePool storagePool) + { + return 0; + } + + @Override + public long getUsedIops(StoragePool storagePool) + { + return 0; + } + + @Override + public long getDataObjectSizeIncludingHypervisorSnapshotReserve(DataObject dataObject, StoragePool pool) + { + return dataObject.getSize(); + } + + @Override + public long getBytesRequiredForTemplate(TemplateInfo templateInfo, StoragePool storagePool) + { + return 0; + } + + private void deleteResourceDefinition(DataStore dataStore, String rscDefName) + { + StoragePoolVO storagePool = _storagePoolDao.findById(dataStore.getId()); + DevelopersApi linstorApi = LinstorUtil.getLinstorAPI(storagePool.getHostAddress()); + + try + { + ApiCallRcList answers = linstorApi.resourceDefinitionDelete(rscDefName); + if (ApiCallChecker.hasError(answers)) + { + for (ApiCallRc answer : answers) + { + s_logger.error(answer.getMessage()); + } + throw new CloudRuntimeException("Linstor: Unable to delete resource definition: " + rscDefName); + } + } catch (ApiException apiEx) + { + s_logger.error("Linstor: ApiEx - " + apiEx.getMessage()); + throw new CloudRuntimeException(apiEx); + } + } + + @Override + public void deleteAsync( + DataStore dataStore, + DataObject dataObject, + AsyncCompletionCallback<CommandResult> callback) + { + s_logger.debug("deleteAsync: " + dataObject.getType() + ";" + dataObject.getId()); + String errMsg = null; + + StoragePoolVO storagePool = _storagePoolDao.findById(dataStore.getId()); + + if (dataObject.getType() == DataObjectType.VOLUME) + { + VolumeInfo volumeInfo = (VolumeInfo) dataObject; + + long storagePoolId = dataStore.getId(); + final String rscName = "cs-" + volumeInfo.getPath(); + deleteResourceDefinition(dataStore, rscName); + + long usedBytes = storagePool.getUsedBytes(); + long capacityIops = storagePool.getCapacityIops(); + + usedBytes -= volumeInfo.getSize(); + if (volumeInfo.getMaxIops() != null) + capacityIops += volumeInfo.getMaxIops(); + + storagePool.setUsedBytes(Math.max(0, usedBytes)); + storagePool.setCapacityIops(Math.max(0, capacityIops)); + + _storagePoolDao.update(storagePoolId, storagePool); + } else + { + errMsg = "Invalid DataObjectType (" + dataObject.getType() + ") passed to deleteAsync"; + } + + CommandResult result = new CommandResult(); + + result.setResult(errMsg); + + callback.complete(result); + } + + private String createResource(VolumeInfo vol, StoragePoolVO storagePoolVO) + { + DevelopersApi linstorApi = LinstorUtil.getLinstorAPI(storagePoolVO.getHostAddress()); + final String rscGrp = storagePoolVO.getUserInfo() != null && !storagePoolVO.getUserInfo().isEmpty() ? + storagePoolVO.getUserInfo() : "DfltRscGrp"; + + ResourceGroupSpawn rscGrpSpawn = new ResourceGroupSpawn(); + final String rscName = "cs-" + vol.getUuid(); + rscGrpSpawn.setResourceDefinitionName(rscName); + rscGrpSpawn.addVolumeSizesItem(vol.getSize() / 1024); + + try + { + s_logger.debug("Linstor: Spawn resource " + rscName); + ApiCallRcList answers = linstorApi.resourceGroupSpawn(rscGrp, rscGrpSpawn); + if (ApiCallChecker.hasError(answers)) + { + for (ApiCallRc answer : answers) + { + s_logger.error(answer.getMessage()); + } + throw new CloudRuntimeException("Linstor: Unable to spawn resource."); + } + + List<ResourceWithVolumes> resources = linstorApi.viewResources( + Collections.emptyList(), + Collections.singletonList(rscName), + Collections.emptyList(), + null, + null, + null); + if (!resources.isEmpty() && !resources.get(0).getVolumes().isEmpty()) + { + s_logger.info("Linstor: Created drbd device: " + resources.get(0).getVolumes().get(0).getDevicePath()); + return resources.get(0).getVolumes().get(0).getDevicePath(); + } else + { + s_logger.error("Linstor: viewResources didn't return resources or volumes."); + throw new CloudRuntimeException("Linstor: viewResources didn't return resources or volumes."); + } + } catch (ApiException apiEx) + { + s_logger.error("Linstor: ApiEx - " + apiEx.getMessage()); + throw new CloudRuntimeException(apiEx); + } + } + + @Override + public void createAsync(DataStore dataStore, DataObject vol, AsyncCompletionCallback<CreateCmdResult> callback) + { + String devPath = null; + String errMsg = null; + StoragePoolVO storagePool = _storagePoolDao.findById(dataStore.getId()); + + try + { + switch (vol.getType()) + { + case VOLUME: + VolumeInfo volumeInfo = (VolumeInfo) vol; + VolumeVO volume = _volumeDao.findById(volumeInfo.getId()); + s_logger.debug("createAsync - creating volume"); + devPath = createResource((VolumeInfo) vol, storagePool); + volume.setFolder("/dev/"); + volume.setPoolId(storagePool.getId()); + volume.setUuid(vol.getUuid()); + volume.setPath(vol.getUuid()); + + _volumeDao.update(volume.getId(), volume); + break; + case SNAPSHOT: + s_logger.debug("createAsync - creating snapshot"); + break; + case TEMPLATE: + s_logger.debug("createAsync - creating template"); + break; Review comment: Hmm I wouldn't completely remove the cases, but I added an errMsg, so the callback gets an error answer, If I would remove the cases, it would return an InvalidDataobject error, which it isn't -- 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]
