Skip to content

Commit 74a476e

Browse files
harikrishna-patnaladhslove
authored andcommitted
Fix local storage deletion cases (apache#10231)
* Delete local storage properties in agent.properties during delete pool * Fix stale entry when add local storage failed * Smaller methods * Comment added
1 parent d495ff9 commit 74a476e

File tree

3 files changed

+67
-9
lines changed

3 files changed

+67
-9
lines changed

plugins/hypervisors/kvm/src/main/java/com/cloud/hypervisor/kvm/resource/LibvirtComputingResource.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -459,7 +459,7 @@ public class LibvirtComputingResource extends ServerResourceBase implements Serv
459459

460460
protected static final String LOCAL_STORAGE_PATH = "local.storage.path";
461461
protected static final String LOCAL_STORAGE_UUID = "local.storage.uuid";
462-
protected static final String DEFAULT_LOCAL_STORAGE_PATH = "/var/lib/libvirt/images/";
462+
public static final String DEFAULT_LOCAL_STORAGE_PATH = "/var/lib/libvirt/images";
463463

464464
protected List<String> localStoragePaths = new ArrayList<>();
465465
protected List<String> localStorageUUIDs = new ArrayList<>();

plugins/hypervisors/kvm/src/main/java/com/cloud/hypervisor/kvm/resource/wrapper/LibvirtDeleteStoragePoolCommandWrapper.java

Lines changed: 55 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,28 +22,78 @@
2222
import com.cloud.agent.api.Answer;
2323
import com.cloud.agent.api.DeleteStoragePoolCommand;
2424
import com.cloud.agent.api.to.StorageFilerTO;
25+
import com.cloud.agent.dao.impl.PropertiesStorage;
26+
import com.cloud.agent.properties.AgentProperties;
27+
import com.cloud.agent.properties.AgentPropertiesFileHandler;
2528
import com.cloud.hypervisor.kvm.resource.LibvirtComputingResource;
2629
import com.cloud.hypervisor.kvm.storage.KVMStoragePoolManager;
2730
import com.cloud.resource.CommandWrapper;
2831
import com.cloud.resource.ResourceWrapper;
32+
import com.cloud.storage.Storage;
2933
import com.cloud.utils.exception.CloudRuntimeException;
3034

35+
import java.util.Arrays;
36+
import java.util.HashMap;
37+
import java.util.stream.Collectors;
38+
3139
@ResourceWrapper(handles = DeleteStoragePoolCommand.class)
3240
public final class LibvirtDeleteStoragePoolCommandWrapper extends CommandWrapper<DeleteStoragePoolCommand, Answer, LibvirtComputingResource> {
3341
@Override
3442
public Answer execute(final DeleteStoragePoolCommand command, final LibvirtComputingResource libvirtComputingResource) {
3543
try {
3644
// if getRemoveDatastore() is true, then we are dealing with managed storage and can skip the delete logic here
3745
if (!command.getRemoveDatastore()) {
38-
final StorageFilerTO pool = command.getPool();
39-
final KVMStoragePoolManager storagePoolMgr = libvirtComputingResource.getStoragePoolMgr();
40-
41-
storagePoolMgr.deleteStoragePool(pool.getType(), pool.getUuid());
46+
handleStoragePoolDeletion(command, libvirtComputingResource);
4247
}
43-
4448
return new Answer(command);
4549
} catch (final CloudRuntimeException e) {
4650
return new Answer(command, false, e.toString());
4751
}
4852
}
53+
54+
private void handleStoragePoolDeletion(final DeleteStoragePoolCommand command, final LibvirtComputingResource libvirtComputingResource) {
55+
final StorageFilerTO pool = command.getPool();
56+
final KVMStoragePoolManager storagePoolMgr = libvirtComputingResource.getStoragePoolMgr();
57+
storagePoolMgr.deleteStoragePool(pool.getType(), pool.getUuid());
58+
59+
if (isLocalStorageAndNotHavingDefaultPath(pool, libvirtComputingResource)) {
60+
updateLocalStorageProperties(pool);
61+
}
62+
}
63+
64+
private boolean isLocalStorageAndNotHavingDefaultPath(final StorageFilerTO pool, final LibvirtComputingResource libvirtComputingResource) {
65+
return Storage.StoragePoolType.Filesystem.equals(pool.getType())
66+
&& !libvirtComputingResource.DEFAULT_LOCAL_STORAGE_PATH.equals(pool.getPath());
67+
}
68+
69+
private void updateLocalStorageProperties(final StorageFilerTO pool) {
70+
String localStoragePath = AgentPropertiesFileHandler.getPropertyValue(AgentProperties.LOCAL_STORAGE_PATH);
71+
String localStorageUuid = AgentPropertiesFileHandler.getPropertyValue(AgentProperties.LOCAL_STORAGE_UUID);
72+
73+
String uuidToRemove = pool.getUuid();
74+
String pathToRemove = pool.getPath();
75+
76+
if (localStorageUuid != null && uuidToRemove != null) {
77+
localStorageUuid = Arrays.stream(localStorageUuid.split(","))
78+
.filter(uuid -> !uuid.equals(uuidToRemove))
79+
.collect(Collectors.joining(","));
80+
}
81+
82+
if (localStoragePath != null && pathToRemove != null) {
83+
localStoragePath = Arrays.stream(localStoragePath.split(","))
84+
.filter(path -> !path.equals(pathToRemove))
85+
.collect(Collectors.joining(","));
86+
}
87+
88+
PropertiesStorage agentProperties = new PropertiesStorage();
89+
agentProperties.configure("AgentProperties", new HashMap<String, Object>());
90+
91+
if (localStorageUuid != null) {
92+
agentProperties.persist(AgentProperties.LOCAL_STORAGE_UUID.getName(), localStorageUuid);
93+
}
94+
95+
if (localStoragePath != null) {
96+
agentProperties.persist(AgentProperties.LOCAL_STORAGE_PATH.getName(), localStoragePath);
97+
}
98+
}
4999
}

server/src/main/java/com/cloud/storage/StorageManagerImpl.java

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -808,7 +808,9 @@ public DataStore createLocalStorage(Host host, StoragePoolInfo pInfo) throws Con
808808
if (!(dc.isLocalStorageEnabled() || useLocalStorageForSystemVM)) {
809809
return null;
810810
}
811-
DataStore store;
811+
DataStore store = null;
812+
DataStoreProvider provider = _dataStoreProviderMgr.getDefaultPrimaryDataStoreProvider();
813+
DataStoreLifeCycle lifeCycle = provider.getDataStoreLifeCycle();
812814
try {
813815
String hostAddress = pInfo.getHost();
814816
if (host.getHypervisorType() == Hypervisor.HypervisorType.VMware) {
@@ -834,8 +836,6 @@ public DataStore createLocalStorage(Host host, StoragePoolInfo pInfo) throws Con
834836
}
835837
}
836838

837-
DataStoreProvider provider = _dataStoreProviderMgr.getDefaultPrimaryDataStoreProvider();
838-
DataStoreLifeCycle lifeCycle = provider.getDataStoreLifeCycle();
839839
if (pool == null) {
840840
Map<String, Object> params = new HashMap<>();
841841
String name = pInfo.getName() != null ? pInfo.getName() : createLocalStoragePoolName(host, pInfo);
@@ -865,6 +865,14 @@ public DataStore createLocalStorage(Host host, StoragePoolInfo pInfo) throws Con
865865

866866
} catch (Exception e) {
867867
logger.warn("Unable to setup the local storage pool for " + host, e);
868+
try {
869+
if (store != null) {
870+
logger.debug(String.format("Trying to delete storage pool entry if exists %s", store));
871+
lifeCycle.deleteDataStore(store);
872+
}
873+
} catch (Exception ex) {
874+
logger.debug(String.format("Failed to clean up local storage pool: %s", ex.getMessage()));
875+
}
868876
throw new ConnectionException(true, "Unable to setup the local storage pool for " + host, e);
869877
}
870878

0 commit comments

Comments
 (0)