diff --git a/plugins/hypervisors/vmware/src/main/java/com/cloud/hypervisor/vmware/resource/VmwareResource.java b/plugins/hypervisors/vmware/src/main/java/com/cloud/hypervisor/vmware/resource/VmwareResource.java index 711bac10c94..04b9bdca39b 100644 --- a/plugins/hypervisors/vmware/src/main/java/com/cloud/hypervisor/vmware/resource/VmwareResource.java +++ b/plugins/hypervisors/vmware/src/main/java/com/cloud/hypervisor/vmware/resource/VmwareResource.java @@ -77,7 +77,6 @@ import org.apache.commons.collections.CollectionUtils; import org.apache.commons.collections.MapUtils; import org.apache.commons.lang.ArrayUtils; import org.apache.commons.lang.math.NumberUtils; -import org.apache.commons.lang3.StringUtils; import org.apache.logging.log4j.ThreadContext; import org.joda.time.Duration; @@ -293,6 +292,7 @@ import com.cloud.utils.FileUtil; import com.cloud.utils.LogUtils; import com.cloud.utils.NumbersUtil; import com.cloud.utils.Pair; +import com.cloud.utils.StringUtils; import com.cloud.utils.Ternary; import com.cloud.utils.db.DB; import com.cloud.utils.exception.CloudRuntimeException; @@ -394,7 +394,6 @@ public class VmwareResource extends ServerResourceBase implements StoragePoolRes // out an operation protected final int _retry = 24; - protected final int _sleep = 10000; protected final int DefaultDomRSshPort = 3922; protected final int MazCmdMBean = 100; @@ -420,10 +419,10 @@ public class VmwareResource extends ServerResourceBase implements StoragePoolRes protected DiskControllerType _rootDiskController = DiskControllerType.ide; protected ManagedObjectReference _morHyperHost; - protected final static ThreadLocal s_serviceContext = new ThreadLocal(); + protected final static ThreadLocal s_serviceContext = new ThreadLocal<>(); protected String _hostName; - protected List _cmdMBeans = new ArrayList(); + protected List _cmdMBeans = new ArrayList<>(); protected Gson _gson; @@ -434,7 +433,7 @@ public class VmwareResource extends ServerResourceBase implements StoragePoolRes protected VirtualRoutingResource _vrResource; - protected final static HashMap s_powerStatesTable = new HashMap(); + protected final static HashMap s_powerStatesTable = new HashMap<>(); static { s_powerStatesTable.put(VirtualMachinePowerState.POWERED_ON, PowerState.PowerOn); @@ -468,7 +467,7 @@ public class VmwareResource extends ServerResourceBase implements StoragePoolRes @Override public Answer executeRequest(Command cmd) { logCommand(cmd); - Answer answer = null; + Answer answer; ThreadContext.push(getCommandLogTitle(cmd)); try { long cmdSequence = _cmdSequence++; @@ -698,13 +697,13 @@ public class VmwareResource extends ServerResourceBase implements StoragePoolRes String scriptChecksum = lines[1].trim(); String checksum = ChecksumUtil.calculateCurrentChecksum(sysVMName, "vms/cloud-scripts.tgz").trim(); - if (!org.apache.commons.lang3.StringUtils.isEmpty(checksum) && checksum.equals(scriptChecksum) && !cmd.isForced()) { + if (!StringUtils.isEmpty(checksum) && checksum.equals(scriptChecksum) && !cmd.isForced()) { String msg = String.format("No change in the scripts checksum, not patching systemVM %s", sysVMName); logger.info(msg); return new PatchSystemVmAnswer(cmd, msg, lines[0], lines[1]); } - Pair patchResult = null; + Pair patchResult; try { patchResult = SshHelper.sshExecute(controlIp, DefaultDomRSshPort, "root", pemFile, null, "/var/cache/cloud/patch-sysvms.sh", 10000, 10000, 600000); @@ -732,7 +731,7 @@ public class VmwareResource extends ServerResourceBase implements StoragePoolRes private Answer execute(SetupPersistentNetworkCommand cmd) { VmwareHypervisorHost host = getHyperHost(getServiceContext()); - String hostname = null; + String hostname; VmwareContext context = getServiceContext(); HostMO hostMO = new HostMO(context, host.getMor()); @@ -757,7 +756,7 @@ public class VmwareResource extends ServerResourceBase implements StoragePoolRes if (storageNfsVersion != null) return; if (cmd instanceof CopyCommand) { - EnumMap params = new EnumMap( + EnumMap params = new EnumMap<>( VmwareStorageProcessorConfigurableFields.class); examineStorageSubSystemCommandNfsVersion((CopyCommand) cmd, params); params = examineStorageSubSystemCommandFullCloneFlagForVmware((CopyCommand) cmd, params); @@ -796,10 +795,10 @@ public class VmwareResource extends ServerResourceBase implements StoragePoolRes if (destDataStore instanceof PrimaryDataStoreTO) { PrimaryDataStoreTO dest = (PrimaryDataStoreTO) destDataStore; if (dest.isFullCloneFlag() != null) { - paramsCopy.put(VmwareStorageProcessorConfigurableFields.FULL_CLONE_FLAG, dest.isFullCloneFlag().booleanValue()); + paramsCopy.put(VmwareStorageProcessorConfigurableFields.FULL_CLONE_FLAG, dest.isFullCloneFlag()); } if (dest.getDiskProvisioningStrictnessFlag() != null) { - paramsCopy.put(VmwareStorageProcessorConfigurableFields.DISK_PROVISIONING_STRICTNESS, dest.getDiskProvisioningStrictnessFlag().booleanValue()); + paramsCopy.put(VmwareStorageProcessorConfigurableFields.DISK_PROVISIONING_STRICTNESS, dest.getDiskProvisioningStrictnessFlag()); } } } @@ -885,7 +884,7 @@ public class VmwareResource extends ServerResourceBase implements StoragePoolRes try { if (newSize < oldSize) { String errorMsg = String.format("VMware doesn't support shrinking volume from larger size [%s] GB to a smaller size [%s] GB. Can't resize volume of VM [name: %s].", - oldSize / Float.valueOf(ResourceType.bytesToMiB), newSize / Float.valueOf(ResourceType.bytesToMiB), vmName); + oldSize / (float) ResourceType.bytesToMiB, newSize / (float) ResourceType.bytesToMiB, vmName); logger.error(errorMsg); throw new Exception(errorMsg); } else if (newSize == oldSize) { @@ -1023,8 +1022,10 @@ public class VmwareResource extends ServerResourceBase implements StoragePoolRes if (useWorkerVm) { logger.info("Destroy worker VM after volume resize"); - vmMo.detachDisk(vmdkDataStorePath, false); - vmMo.destroy(); + if (vmMo != null) { + vmMo.detachDisk(vmdkDataStorePath, false); + vmMo.destroy(); + } } } catch (Throwable e) { logger.error(String.format("Failed to destroy worker VM [name: %s] due to: [%s].", vmName, e.getMessage()), e); @@ -1116,13 +1117,11 @@ public class VmwareResource extends ServerResourceBase implements StoragePoolRes if (cmd.getOption() != null && cmd.getOption().equals("create")) { String result = networkUsage(cmd.getPrivateIP(), "create", null); - NetworkUsageAnswer answer = new NetworkUsageAnswer(cmd, result, 0L, 0L); - return answer; + return new NetworkUsageAnswer(cmd, result, 0L, 0L); } long[] stats = getNetworkStats(cmd.getPrivateIP(), null); - NetworkUsageAnswer answer = new NetworkUsageAnswer(cmd, "", stats[0], stats[1]); - return answer; + return new NetworkUsageAnswer(cmd, "", stats[0], stats[1]); } protected NetworkUsageAnswer VPCNetworkUsage(NetworkUsageCommand cmd) { @@ -1197,8 +1196,8 @@ public class VmwareResource extends ServerResourceBase implements StoragePoolRes } protected Answer execute(GetAutoScaleMetricsCommand cmd) { - Long bytesSent; - Long bytesReceived; + long bytesSent; + long bytesReceived; if (cmd.isForVpc()) { long[] stats = getVPCNetworkStats(cmd.getPrivateIP(), cmd.getPublicIP(), "get", ""); bytesSent = stats[0]; @@ -1218,14 +1217,14 @@ public class VmwareResource extends ServerResourceBase implements StoragePoolRes switch (metrics.getCounter()) { case NETWORK_RECEIVED_AVERAGE_MBPS: values.add(new VirtualRouterAutoScale.AutoScaleMetricsValue(metrics, VirtualRouterAutoScale.AutoScaleValueType.AGGREGATED_VM_GROUP, - Double.valueOf(bytesReceived) / VirtualRouterAutoScale.MBITS_TO_BYTES)); + (double) bytesReceived / VirtualRouterAutoScale.MBITS_TO_BYTES)); break; case NETWORK_TRANSMIT_AVERAGE_MBPS: values.add(new VirtualRouterAutoScale.AutoScaleMetricsValue(metrics, VirtualRouterAutoScale.AutoScaleValueType.AGGREGATED_VM_GROUP, - Double.valueOf(bytesSent) / VirtualRouterAutoScale.MBITS_TO_BYTES)); + (double) bytesSent / VirtualRouterAutoScale.MBITS_TO_BYTES)); break; case LB_AVERAGE_CONNECTIONS: - values.add(new VirtualRouterAutoScale.AutoScaleMetricsValue(metrics, VirtualRouterAutoScale.AutoScaleValueType.INSTANT_VM, Double.valueOf(lbConnections))); + values.add(new VirtualRouterAutoScale.AutoScaleMetricsValue(metrics, VirtualRouterAutoScale.AutoScaleValueType.INSTANT_VM, (double) lbConnections)); break; } } @@ -1237,7 +1236,7 @@ public class VmwareResource extends ServerResourceBase implements StoragePoolRes public ExecutionResult createFileInVR(String routerIp, String filePath, String fileName, String content) { File keyFile = getSystemVmKeyFile(); try { - SshHelper.scpTo(routerIp, 3922, "root", keyFile, null, filePath, content.getBytes("UTF-8"), fileName, null); + SshHelper.scpTo(routerIp, 3922, "root", keyFile, null, filePath, content.getBytes(StringUtils.getPreferredCharset()), fileName, null); } catch (Exception e) { logger.warn("Fail to create file " + filePath + fileName + " in VR " + routerIp, e); return new ExecutionResult(false, e.getMessage()); @@ -1285,7 +1284,7 @@ public class VmwareResource extends ServerResourceBase implements StoragePoolRes private int findRouterEthDeviceIndex(String domrName, String routerIp, String mac) throws Exception { File keyFile = getSystemVmKeyFile(); logger.info("findRouterEthDeviceIndex. mac: " + mac); - ArrayList skipInterfaces = new ArrayList(Arrays.asList("all", "default", "lo")); + ArrayList skipInterfaces = new ArrayList<>(Arrays.asList("all", "default", "lo")); // when we dynamically plug in a new NIC into virtual router, it may take time to show up in guest OS // we use a waiting loop here as a workaround to synchronize activities in systems @@ -1352,7 +1351,7 @@ public class VmwareResource extends ServerResourceBase implements StoragePoolRes int ethDeviceNum = findRouterEthDeviceIndex(domrName, routerIp, nic.getMac()); nic.setDeviceId(ethDeviceNum); } catch (Exception e) { - String msg = "Prepare SetupGuestNetwork failed due to " + e.toString(); + String msg = "Prepare SetupGuestNetwork failed due to " + e; logger.warn(msg, e); return new ExecutionResult(false, msg); } @@ -1396,7 +1395,7 @@ public class VmwareResource extends ServerResourceBase implements StoragePoolRes int ethDeviceNum = findRouterEthDeviceIndex(routerName, routerIp, pubIp.getVifMacAddress()); pubIp.setNicDevId(ethDeviceNum); } catch (Exception e) { - String msg = "Prepare Ip SNAT failure due to " + e.toString(); + String msg = "Prepare Ip SNAT failure due to " + e; logger.error(msg, e); return new ExecutionResult(false, e.toString()); } @@ -1412,7 +1411,7 @@ public class VmwareResource extends ServerResourceBase implements StoragePoolRes int ethDeviceNum = findRouterEthDeviceIndex(routerName, routerIp, nic.getMac()); nic.setDeviceId(ethDeviceNum); } catch (Exception e) { - String msg = "Prepare SetNetworkACL failed due to " + e.toString(); + String msg = "Prepare SetNetworkACL failed due to " + e; logger.error(msg, e); return new ExecutionResult(false, msg); } @@ -1433,7 +1432,7 @@ public class VmwareResource extends ServerResourceBase implements StoragePoolRes return new PlugNicAnswer(cmd, true, "success"); } catch (Exception e) { logger.error("Unexpected exception: ", e); - return new PlugNicAnswer(cmd, false, "Unable to execute PlugNicCommand due to " + e.toString()); + return new PlugNicAnswer(cmd, false, "Unable to execute PlugNicCommand due to " + e); } } @@ -1480,7 +1479,7 @@ public class VmwareResource extends ServerResourceBase implements StoragePoolRes VirtualDevice nic; Pair networkInfo = prepareNetworkFromNicInfo(vmMo.getRunningHost(), nicTo, false, nicTo.getNetworkSegmentName(), vmType); - String dvSwitchUuid = null; + String dvSwitchUuid; if (VmwareHelper.isDvPortGroup(networkInfo.first())) { ManagedObjectReference dcMor = hyperHost.getHyperHostDatacenter(); DatacenterMO dataCenterMo = new DatacenterMO(context, dcMor); @@ -1542,7 +1541,7 @@ public class VmwareResource extends ServerResourceBase implements StoragePoolRes } Pair networkInfo = prepareNetworkFromNicInfo(vmMo.getRunningHost(), nicTo, false, null, cmd.getVMType()); - String dvSwitchUuid = null; + String dvSwitchUuid; if (VmwareHelper.isDvPortGroup(networkInfo.first())) { ManagedObjectReference dcMor = hyperHost.getHyperHostDatacenter(); DatacenterMO dataCenterMo = new DatacenterMO(context, dcMor); @@ -1561,7 +1560,7 @@ public class VmwareResource extends ServerResourceBase implements StoragePoolRes return new ReplugNicAnswer(cmd, true, "success"); } catch (Exception e) { logger.error("Unexpected exception: ", e); - return new ReplugNicAnswer(cmd, false, "Unable to execute ReplugNicCommand due to " + e.toString()); + return new ReplugNicAnswer(cmd, false, "Unable to execute ReplugNicCommand due to " + e); } } @@ -1602,13 +1601,13 @@ public class VmwareResource extends ServerResourceBase implements StoragePoolRes return new UnPlugNicAnswer(cmd, true, "success"); } catch (Exception e) { logger.error("Unexpected exception: ", e); - return new UnPlugNicAnswer(cmd, false, "Unable to execute unPlugNicCommand due to " + e.toString()); + return new UnPlugNicAnswer(cmd, false, "Unable to execute unPlugNicCommand due to " + e); } } private void plugPublicNic(VirtualMachineMO vmMo, final String vlanId, final IpAddressTO ipAddressTO) throws Exception { // TODO : probably need to set traffic shaping - Pair networkInfo = null; + Pair networkInfo; VirtualSwitchType vSwitchType = VirtualSwitchType.StandardVirtualSwitch; if (_publicTrafficInfo != null) { vSwitchType = _publicTrafficInfo.getVirtualSwitchType(); @@ -1749,7 +1748,7 @@ public class VmwareResource extends ServerResourceBase implements StoragePoolRes ip.setNewNic(addVif); } } catch (Throwable e) { - logger.error("Unexpected exception: " + e.toString() + " will shortcut rest of IPAssoc commands", e); + logger.error("Unexpected exception: " + e + " will shortcut rest of IPAssoc commands", e); return new ExecutionResult(false, e.toString()); } return new ExecutionResult(true, null); @@ -1793,7 +1792,7 @@ public class VmwareResource extends ServerResourceBase implements StoragePoolRes configureNicDevice(vmMo, nicInfo.first(), VirtualDeviceConfigSpecOperation.REMOVE, "unplugNicCommand"); } } catch (Throwable e) { - logger.error("Unexpected exception: " + e.toString() + " will shortcut rest of IPAssoc commands", e); + logger.error("Unexpected exception: " + e + " will shortcut rest of IPAssoc commands", e); return new ExecutionResult(false, e.toString()); } return new ExecutionResult(true, null); @@ -1850,7 +1849,7 @@ public class VmwareResource extends ServerResourceBase implements StoragePoolRes } catch (Exception e) { String msg = "Command failed due to " + VmwareHelper.getExceptionMessage(e); logger.error(msg); - result = new Pair(false, msg); + result = new Pair<>(false, msg); } if (logger.isDebugEnabled()) { logger.debug(script + " execution result: " + result.first().toString()); @@ -1894,7 +1893,7 @@ public class VmwareResource extends ServerResourceBase implements StoragePoolRes } private DiskTO[] validateDisks(DiskTO[] disks) { - List validatedDisks = new ArrayList(); + List validatedDisks = new ArrayList<>(); for (DiskTO vol : disks) { if (vol.getType() != Volume.Type.ISO) { @@ -1968,7 +1967,7 @@ public class VmwareResource extends ServerResourceBase implements StoragePoolRes } } catch (Exception e) { logger.error(String.format("ScaleVmCommand failed due to: [%s].", VmwareHelper.getExceptionMessage(e)), e); - return new ScaleVmAnswer(cmd, false, String.format("Unable to execute ScaleVmCommand due to: [%s].", e.toString())); + return new ScaleVmAnswer(cmd, false, String.format("Unable to execute ScaleVmCommand due to: [%s].", e)); } return new ScaleVmAnswer(cmd, true, null); } @@ -2037,8 +2036,8 @@ public class VmwareResource extends ServerResourceBase implements StoragePoolRes String existingVmName = null; VirtualMachineFileInfo existingVmFileInfo = null; VirtualMachineFileLayoutEx existingVmFileLayout = null; - List existingDatastores = new ArrayList(); - String diskStoragePolicyId = null; + List existingDatastores = new ArrayList<>(); + String diskStoragePolicyId; String vmStoragePolicyId = null; VirtualMachineDefinedProfileSpec diskProfileSpec = null; VirtualMachineDefinedProfileSpec vmProfileSpec = null; @@ -2187,7 +2186,7 @@ public class VmwareResource extends ServerResourceBase implements StoragePoolRes } } tearDownVm(vmMo); - } else if (!hyperHost.createBlankVm(vmNameOnVcenter, vmInternalCSName, vmSpec.getCpus(), vmSpec.getMaxSpeed().intValue(), getReservedCpuMHZ(vmSpec), + } else if (!hyperHost.createBlankVm(vmNameOnVcenter, vmInternalCSName, vmSpec.getCpus(), vmSpec.getMaxSpeed(), getReservedCpuMHZ(vmSpec), vmSpec.getLimitCpuUse(), (int) (vmSpec.getMaxRam() / ResourceType.bytesToMiB), getReservedMemoryMb(vmSpec), guestOsId, rootDiskDataStoreDetails.first(), false, controllerInfo, systemVm)) { throw new Exception("Failed to create VM. vmName: " + vmInternalCSName); @@ -2350,13 +2349,16 @@ public class VmwareResource extends ServerResourceBase implements StoragePoolRes continue; } + VirtualMachineDiskInfo matchingExistingDisk = getMatchingExistingDisk(diskInfoBuilder, vol, hyperHost, context); + Pair volumeDsDetails = getVolumeDatastoreDetails(vol, dataStoresDetails); + syncVolumeDatastoreAndPathForDatastoreCluster(vol, diskInfoBuilder, matchingExistingDisk, volumeDsDetails, diskDatastores, hyperHost, context); + if (deployAsIs && vol.getType() == Volume.Type.ROOT) { rootDiskTO = vol; resizeRootDiskOnVMStart(vmMo, rootDiskTO, hyperHost, context); continue; } - VirtualMachineDiskInfo matchingExistingDisk = getMatchingExistingDisk(diskInfoBuilder, vol, hyperHost, context); String diskController = getDiskController(vmMo, matchingExistingDisk, vol, chosenDiskControllers, deployAsIs); if (DiskControllerType.getType(diskController) == DiskControllerType.ide) { controllerKey = vmMo.getIDEControllerKey(ideUnitNumber); @@ -2365,7 +2367,7 @@ public class VmwareResource extends ServerResourceBase implements StoragePoolRes // Ensure maximum of 2 data volumes over IDE controller, 3 includeing root volume if (vmMo.getNumberOfVirtualDisks() > 3) { throw new CloudRuntimeException("Found more than 3 virtual disks attached to this VM [" + vmMo.getVmName() + "]. Unable to implement the disks over " - + diskController + " controller, as maximum number of devices supported over IDE controller is 4 includeing CDROM device."); + + diskController + " controller, as maximum number of devices supported over IDE controller is 4 including CDROM device."); } } } else { @@ -2385,54 +2387,9 @@ public class VmwareResource extends ServerResourceBase implements StoragePoolRes if (!hasSnapshot) { deviceConfigSpecArray[i] = new VirtualDeviceConfigSpec(); - VolumeObjectTO volumeTO = (VolumeObjectTO) vol.getData(); - DataStoreTO primaryStore = volumeTO.getDataStore(); - Map details = vol.getDetails(); - boolean managed = false; - String iScsiName = null; - - if (details != null) { - managed = Boolean.parseBoolean(details.get(DiskTO.MANAGED)); - iScsiName = details.get(DiskTO.IQN); - } - - String primaryStoreUuid = primaryStore.getUuid(); - // if the storage is managed, iScsiName should not be null - String datastoreName = managed ? VmwareResource.getDatastoreName(iScsiName) : primaryStoreUuid; - Pair volumeDsDetails = dataStoresDetails.get(datastoreName); - - assert (volumeDsDetails != null); - if (volumeDsDetails == null) { - throw new Exception("Primary datastore " + primaryStore.getUuid() + " is not mounted on host."); - } - - if (vol.getDetails().get(DiskTO.PROTOCOL_TYPE) != null && vol.getDetails().get(DiskTO.PROTOCOL_TYPE).equalsIgnoreCase("DatastoreCluster")) { - if (diskInfoBuilder != null && matchingExistingDisk != null) { - String[] diskChain = matchingExistingDisk.getDiskChain(); - if (diskChain != null && diskChain.length > 0) { - DatastoreFile file = new DatastoreFile(diskChain[0]); - if (!file.getFileBaseName().equalsIgnoreCase(volumeTO.getPath())) { - if (logger.isInfoEnabled()) - logger.info("Detected disk-chain top file change on volume: " + volumeTO.getId() + " " + volumeTO.getPath() + " -> " + file.getFileBaseName()); - volumeTO.setPath(file.getFileBaseName()); - } - } - DatastoreMO diskDatastoreMofromVM = getDataStoreWhereDiskExists(hyperHost, context, diskInfoBuilder, vol, diskDatastores); - if (diskDatastoreMofromVM != null) { - String actualPoolUuid = diskDatastoreMofromVM.getCustomFieldValue(CustomFieldConstants.CLOUD_UUID); - if (actualPoolUuid != null && !actualPoolUuid.equalsIgnoreCase(primaryStore.getUuid())) { - volumeDsDetails = new Pair<>(diskDatastoreMofromVM.getMor(), diskDatastoreMofromVM); - if (logger.isInfoEnabled()) - logger.info("Detected datastore uuid change on volume: " + volumeTO.getId() + " " + primaryStore.getUuid() + " -> " + actualPoolUuid); - ((PrimaryDataStoreTO)primaryStore).setUuid(actualPoolUuid); - } - } - } - } - String[] diskChain = syncDiskChain(dcMo, vmMo, vol, matchingExistingDisk, volumeDsDetails.second()); - int deviceNumber = -1; + int deviceNumber; if (controllerKey == vmMo.getIDEControllerKey(ideUnitNumber)) { deviceNumber = ideUnitNumber % VmwareHelper.MAX_ALLOWED_DEVICES_IDE_CONTROLLER; ideUnitNumber++; @@ -2441,6 +2398,7 @@ public class VmwareResource extends ServerResourceBase implements StoragePoolRes scsiUnitNumber++; } + VolumeObjectTO volumeTO = (VolumeObjectTO) vol.getData(); Long maxIops = volumeTO.getIopsWriteRate() + volumeTO.getIopsReadRate(); VirtualDevice device = VmwareHelper.prepareDiskDevice(vmMo, null, controllerKey, diskChain, volumeDsDetails.first(), deviceNumber, i + 1, maxIops); logger.debug(LogUtils.logGsonWithoutException("The following definitions will be used to start the VM: virtual device [%s], volume [%s].", device, volumeTO)); @@ -2508,7 +2466,7 @@ public class VmwareResource extends ServerResourceBase implements StoragePoolRes NiciraNvpApiVersion.logNiciraApiVersion(); - Map nicUuidToDvSwitchUuid = new HashMap(); + Map nicUuidToDvSwitchUuid = new HashMap<>(); for (NicTO nicTo : sortNicsByDeviceId(nics)) { logger.info("Prepare NIC device based on NicTO: " + _gson.toJson(nicTo)); @@ -2572,7 +2530,7 @@ public class VmwareResource extends ServerResourceBase implements StoragePoolRes // // pass boot arguments through machine.id & perform customized options to VMX - ArrayList extraOptions = new ArrayList(); + ArrayList extraOptions = new ArrayList<>(); configBasicExtraOption(extraOptions, vmSpec); if (deployAsIs) { @@ -2678,12 +2636,12 @@ public class VmwareResource extends ServerResourceBase implements StoragePoolRes // Since VM was successfully powered-on, if there was an existing VM in a different cluster that was unregistered, delete all the files associated with it. if (existingVmName != null && existingVmFileLayout != null) { - List vmDatastoreNames = new ArrayList(); + List vmDatastoreNames = new ArrayList<>(); for (DatastoreMO vmDatastore : vmMo.getAllDatastores()) { vmDatastoreNames.add(vmDatastore.getName()); } // Don't delete files that are in a datastore that is being used by the new VM as well (zone-wide datastore). - List skipDatastores = new ArrayList(); + List skipDatastores = new ArrayList<>(); for (DatastoreMO existingDatastore : existingDatastores) { if (vmDatastoreNames.contains(existingDatastore.getName())) { skipDatastores.add(existingDatastore.getName()); @@ -2714,6 +2672,64 @@ public class VmwareResource extends ServerResourceBase implements StoragePoolRes } } + private Pair getVolumeDatastoreDetails(DiskTO vol, HashMap> dataStoresDetails) throws Exception { + boolean managed = false; + String iScsiName = null; + Map details = vol.getDetails(); + if (MapUtils.isNotEmpty(details)) { + managed = Boolean.parseBoolean(details.get(DiskTO.MANAGED)); + iScsiName = details.get(DiskTO.IQN); + } + + VolumeObjectTO volumeTO = (VolumeObjectTO) vol.getData(); + DataStoreTO primaryStore = volumeTO.getDataStore(); + String primaryStoreUuid = primaryStore.getUuid(); + // if the storage is managed, iScsiName should not be null + String datastoreName = managed ? VmwareResource.getDatastoreName(iScsiName) : primaryStoreUuid; + Pair volumeDsDetails = dataStoresDetails.get(datastoreName); + if (volumeDsDetails == null) { + throw new Exception("Primary datastore " + primaryStore.getUuid() + " is not mounted on host."); + } + + return volumeDsDetails; + } + + private void syncVolumeDatastoreAndPathForDatastoreCluster(DiskTO vol, VirtualMachineDiskInfoBuilder diskInfoBuilder, VirtualMachineDiskInfo matchingExistingDisk, + Pair volumeDsDetails, List> diskDatastores, + VmwareHypervisorHost hyperHost, VmwareContext context) throws Exception { + if (vol.getDetails() == null || vol.getDetails().get(DiskTO.PROTOCOL_TYPE) == null || !vol.getDetails().get(DiskTO.PROTOCOL_TYPE).equalsIgnoreCase("DatastoreCluster")) { + return; + } + + if (diskInfoBuilder != null && matchingExistingDisk != null) { + String[] diskChain = matchingExistingDisk.getDiskChain(); + if (diskChain != null && diskChain.length > 0) { + DatastoreFile file = new DatastoreFile(diskChain[0]); + VolumeObjectTO volumeTO = (VolumeObjectTO) vol.getData(); + if (!file.getFileBaseName().equalsIgnoreCase(volumeTO.getPath())) { + if (logger.isInfoEnabled()) { + logger.info("Detected disk-chain top file change on volume: " + volumeTO.getId() + " " + volumeTO.getPath() + " -> " + file.getFileBaseName()); + } + volumeTO.setPath(file.getFileBaseName()); + vol.setPath(file.getFileBaseName()); + } + } + DatastoreMO diskDatastoreMofromVM = getDataStoreWhereDiskExists(hyperHost, context, diskInfoBuilder, vol, diskDatastores); + if (diskDatastoreMofromVM != null) { + String actualPoolUuid = diskDatastoreMofromVM.getCustomFieldValue(CustomFieldConstants.CLOUD_UUID); + VolumeObjectTO volumeTO = (VolumeObjectTO) vol.getData(); + DataStoreTO primaryStore = volumeTO.getDataStore(); + if (actualPoolUuid != null && !actualPoolUuid.equalsIgnoreCase(primaryStore.getUuid())) { + volumeDsDetails = new Pair<>(diskDatastoreMofromVM.getMor(), diskDatastoreMofromVM); + if (logger.isInfoEnabled()) { + logger.info("Detected datastore uuid change on volume: " + volumeTO.getId() + " " + primaryStore.getUuid() + " -> " + actualPoolUuid); + } + ((PrimaryDataStoreTO)primaryStore).setUuid(actualPoolUuid); + } + } + } + } + private boolean powerOnVM(final VirtualMachineMO vmMo, final String vmInternalCSName, final String vmNameOnVcenter) throws Exception { int retry = 20; while (retry-- > 0) { @@ -3069,7 +3085,7 @@ public class VmwareResource extends ServerResourceBase implements StoragePoolRes final Pair vdisk = getVirtualDiskInfo(vmMo, appendFileType(rootDiskTO.getPath(), VMDK_EXTENSION)); assert (vdisk != null); - Long reqSize = 0L; + long reqSize = 0L; final VolumeObjectTO volumeTO = ((VolumeObjectTO) rootDiskTO.getData()); if (volumeTO != null) { reqSize = volumeTO.getSize() / 1024; @@ -3185,8 +3201,8 @@ public class VmwareResource extends ServerResourceBase implements StoragePoolRes * @param vmConfigSpec virtual machine config spec */ protected void modifyVmVideoCardVRamSize(VirtualMachineVideoCard videoCard, VirtualMachineMO vmMo, long svgaVmramSize, VirtualMachineConfigSpec vmConfigSpec) { - if (videoCard.getVideoRamSizeInKB().longValue() != svgaVmramSize) { - logger.info("Video card memory was set " + toHumanReadableSize(videoCard.getVideoRamSizeInKB().longValue()) + " instead of " + toHumanReadableSize(svgaVmramSize)); + if (videoCard.getVideoRamSizeInKB() != svgaVmramSize) { + logger.info("Video card memory was set " + toHumanReadableSize(videoCard.getVideoRamSizeInKB()) + " instead of " + toHumanReadableSize(svgaVmramSize)); configureSpecVideoCardNewVRamSize(videoCard, svgaVmramSize, vmConfigSpec); } } @@ -3265,7 +3281,7 @@ public class VmwareResource extends ServerResourceBase implements StoragePoolRes if (vmMo == null) return; - boolean hasSnapshot = false; + boolean hasSnapshot; hasSnapshot = vmMo.hasSnapshot(); if (!hasSnapshot) vmMo.tearDownDevices(new Class[]{VirtualDisk.class, VirtualEthernetCard.class}); @@ -3368,7 +3384,7 @@ public class VmwareResource extends ServerResourceBase implements StoragePoolRes } else if (_instanceNameFlag && vmSpec.getHostName() != null) { vmNameOnVcenter = vmSpec.getHostName(); } - return new Pair(vmInternalCSName, vmNameOnVcenter); + return new Pair<>(vmInternalCSName, vmNameOnVcenter); } protected void configNestedHVSupport(VirtualMachineMO vmMo, VirtualMachineTO vmSpec, VirtualMachineConfigSpec vmConfigSpec) throws Exception { @@ -3386,7 +3402,7 @@ public class VmwareResource extends ServerResourceBase implements StoragePoolRes if (nestedHvSupported == null) { // nestedHvEnabled property is supported only since VMware 5.1. It's not defined for earlier versions. logger.warn("Hypervisor doesn't support nested virtualization, unable to set config for VM " + vmSpec.getName()); - } else if (nestedHvSupported.booleanValue()) { + } else if (nestedHvSupported) { logger.debug("Hypervisor supports nested virtualization, enabling for VM " + vmSpec.getName()); vmConfigSpec.setNestedHVEnabled(true); } else { @@ -3457,7 +3473,7 @@ public class VmwareResource extends ServerResourceBase implements StoragePoolRes for (NicTO nicTo : sortNicsByDeviceId(vmSpec.getNics())) { if (nicTo.getBroadcastType() == BroadcastDomainType.Lswitch) { // We need to create a port with a unique vlan and pass the key to the nic device - logger.trace("Nic " + nicTo.toString() + " is connected to an NVP logicalswitch"); + logger.trace("Nic " + nicTo + " is connected to an NVP logicalswitch"); VirtualDevice nicVirtualDevice = vmMo.getNicDeviceByIndex(nicIndex); if (nicVirtualDevice == null) { throw new Exception("Failed to find a VirtualDevice for nic " + nicIndex); //FIXME Generic exceptions are bad @@ -3471,7 +3487,7 @@ public class VmwareResource extends ServerResourceBase implements StoragePoolRes String portGroupKey = port.getPortgroupKey(); String dvSwitchUuid = port.getSwitchUuid(); - logger.debug("NIC " + nicTo.toString() + " is connected to dvSwitch " + dvSwitchUuid + " pg " + portGroupKey + " port " + portKey); + logger.debug("NIC " + nicTo + " is connected to dvSwitch " + dvSwitchUuid + " pg " + portGroupKey + " port " + portKey); ManagedObjectReference dvSwitchManager = vmMo.getContext().getVimClient().getServiceContent().getDvSwitchManager(); ManagedObjectReference dvSwitch = vmMo.getContext().getVimClient().getService().queryDvsByUuid(dvSwitchManager, dvSwitchUuid); @@ -3483,7 +3499,7 @@ public class VmwareResource extends ServerResourceBase implements StoragePoolRes List dvPorts = vmMo.getContext().getVimClient().getService().fetchDVPorts(dvSwitch, criteria); DistributedVirtualPort vmDvPort = null; - List usedVlans = new ArrayList(); + List usedVlans = new ArrayList<>(); for (DistributedVirtualPort dvPort : dvPorts) { // Find the port for this NIC by portkey if (portKey.equals(dvPort.getKey())) { @@ -3498,7 +3514,7 @@ public class VmwareResource extends ServerResourceBase implements StoragePoolRes } if (vmDvPort == null) { - throw new Exception("Empty port list from dvSwitch for nic " + nicTo.toString()); + throw new Exception("Empty port list from dvSwitch for nic " + nicTo); } DVPortConfigInfo dvPortConfigInfo = vmDvPort.getConfig(); @@ -3528,13 +3544,13 @@ public class VmwareResource extends ServerResourceBase implements StoragePoolRes dvPortConfigSpec.setSetting(edittedSettings); dvPortConfigSpec.setOperation("edit"); dvPortConfigSpec.setKey(portKey); - List dvPortConfigSpecs = new ArrayList(); + List dvPortConfigSpecs = new ArrayList<>(); dvPortConfigSpecs.add(dvPortConfigSpec); ManagedObjectReference task = vmMo.getContext().getVimClient().getService().reconfigureDVPortTask(dvSwitch, dvPortConfigSpecs); if (!vmMo.getContext().getVimClient().waitForTask(task)) { - throw new Exception("Failed to configure the dvSwitch port for nic " + nicTo.toString()); + throw new Exception("Failed to configure the dvSwitch port for nic " + nicTo); } - logger.debug("NIC " + nicTo.toString() + " connected to vlan " + i); + logger.debug("NIC " + nicTo + " connected to vlan " + i); } else { logger.trace("Port already configured and set to vlan " + vlanId.getVlanId()); } @@ -3595,8 +3611,8 @@ public class VmwareResource extends ServerResourceBase implements StoragePoolRes private Pair getVMDiskInfo(String volumePath, boolean isManaged, String iScsiName, String datastoreUUID, VmwareHypervisorHost hyperHost, VmwareContext context) throws Exception { - String dsName = null; - String diskBackingFileBaseName = null; + String dsName; + String diskBackingFileBaseName; if (isManaged) { // if the storage is managed, iScsiName should not be null @@ -3771,11 +3787,11 @@ public class VmwareResource extends ServerResourceBase implements StoragePoolRes private static NicTO[] sortNicsByDeviceId(NicTO[] nics) { - List listForSort = new ArrayList(); + List listForSort = new ArrayList<>(); for (NicTO nic : nics) { listForSort.add(nic); } - Collections.sort(listForSort, new Comparator() { + Collections.sort(listForSort, new Comparator<>() { @Override public int compare(NicTO arg0, NicTO arg1) { @@ -3794,11 +3810,11 @@ public class VmwareResource extends ServerResourceBase implements StoragePoolRes private static DiskTO[] sortVolumesByDeviceId(DiskTO[] volumes) { - List listForSort = new ArrayList(); + List listForSort = new ArrayList<>(); for (DiskTO vol : volumes) { listForSort.add(vol); } - Collections.sort(listForSort, new Comparator() { + Collections.sort(listForSort, new Comparator<>() { @Override public int compare(DiskTO arg0, DiskTO arg1) { @@ -3834,9 +3850,7 @@ public class VmwareResource extends ServerResourceBase implements StoragePoolRes path = path.substring(startIndex + search.length()); - final String search2 = VMDK_EXTENSION; - - int endIndex = path.indexOf(search2); + int endIndex = path.indexOf(VMDK_EXTENSION); if (endIndex == -1) { return null; @@ -3953,7 +3967,7 @@ public class VmwareResource extends ServerResourceBase implements StoragePoolRes } private Pair getDatastoreThatDiskIsOn(HashMap> dataStoresDetails, DiskTO vol) { - Pair rootDiskDataStoreDetails = null; + Pair rootDiskDataStoreDetails; Map details = vol.getDetails(); boolean managed = false; @@ -4022,7 +4036,7 @@ public class VmwareResource extends ServerResourceBase implements StoragePoolRes String vlanToken = switchDetails.third(); String namePrefix = getNetworkNamePrefix(nicTo); - Pair networkInfo = null; + Pair networkInfo; logger.info("Prepare network on " + switchType + " " + switchName + " with name prefix: " + namePrefix); @@ -4032,7 +4046,7 @@ public class VmwareResource extends ServerResourceBase implements StoragePoolRes _opsTimeout, true, nicTo.getBroadcastType(), nicTo.getUuid(), nicTo.getDetails()); } else { String vlanId = getVlanInfo(nicTo, vlanToken); - String svlanId = null; + String svlanId; boolean pvlannetwork = (getPvlanInfo(nicTo) == null) ? false : true; if (vmType != null && vmType.equals(VirtualMachine.Type.DomainRouter) && pvlannetwork) { // plumb this network to the promiscuous vlan. @@ -4055,7 +4069,7 @@ public class VmwareResource extends ServerResourceBase implements StoragePoolRes TrafficType trafficType = nicTo.getType(); if (!Arrays.asList(supportedTrafficTypes).contains(trafficType)) { - throw new CloudException("Traffic type " + trafficType.toString() + " for nic " + nicTo.toString() + " is not supported."); + throw new CloudException("Traffic type " + trafficType.toString() + " for nic " + nicTo + " is not supported."); } String switchName = null; @@ -4090,12 +4104,12 @@ public class VmwareResource extends ServerResourceBase implements StoragePoolRes if (switchType == VirtualSwitchType.NexusDistributedVirtualSwitch) { if (trafficType == TrafficType.Management || trafficType == TrafficType.Storage) { throw new CloudException( - "Unable to configure NIC " + nicTo.toString() + " as traffic type " + trafficType.toString() + " is not supported over virtual switch type " + switchType + "Unable to configure NIC " + nicTo + " as traffic type " + trafficType + " is not supported over virtual switch type " + switchType + ". Please specify only supported type of virtual switches i.e. {vmwaresvs, vmwaredvs} in physical network traffic label."); } } - return new Ternary(switchName, switchType.toString(), vlanId); + return new Ternary<>(switchName, switchType.toString(), vlanId); } private String getNetworkNamePrefix(NicTO nicTo) throws Exception { @@ -4171,7 +4185,7 @@ public class VmwareResource extends ServerResourceBase implements StoragePoolRes ManagedObjectReference morDs = prepareSecondaryDatastoreOnHost(storeUrl); DatastoreMO dsMo = new DatastoreMO(getServiceContext(), morDs); - return new Pair(String.format("[%s] %s%s", dsMo.getName(), isoPath, isoFileName), morDs); + return new Pair<>(String.format("[%s] %s%s", dsMo.getName(), isoPath, isoFileName), morDs); } protected Answer execute(ReadyCommand cmd) { @@ -4179,7 +4193,7 @@ public class VmwareResource extends ServerResourceBase implements StoragePoolRes VmwareContext context = getServiceContext(); VmwareHypervisorHost hyperHost = getHyperHost(context); - Map hostDetails = new HashMap(); + Map hostDetails = new HashMap<>(); ManagedObjectReference morHost = hyperHost.getMor(); HostMO hostMo = new HostMO(context, morHost); boolean uefiLegacySupported = hostMo.isUefiLegacySupported(); @@ -4230,7 +4244,7 @@ public class VmwareResource extends ServerResourceBase implements StoragePoolRes // getVmNames should return all i-x-y values. List requestedVmNames = cmd.getVmNames(); - List vmNames = new ArrayList(); + List vmNames = new ArrayList<>(); if (requestedVmNames != null) { for (String vmName : requestedVmNames) { @@ -4299,7 +4313,7 @@ public class VmwareResource extends ServerResourceBase implements StoragePoolRes long writeReq = 0; long writeBytes = 0; - final ArrayList perfMetricsIds = new ArrayList(); + final ArrayList perfMetricsIds = new ArrayList<>(); if (diskReadIOPerfCounterInfo != null) { perfMetricsIds.add(VmwareHelper.createPerfMetricId(diskReadIOPerfCounterInfo, diskBusName)); } @@ -4389,7 +4403,7 @@ public class VmwareResource extends ServerResourceBase implements StoragePoolRes VmwareHypervisorHost hyperHost = getHyperHost(getServiceContext()); ManagedObjectReference dcMor = hyperHost.getHyperHostDatacenter(); DatacenterMO dcMo = new DatacenterMO(getServiceContext(), dcMor); - HashMap statEntry = new HashMap(); + HashMap statEntry = new HashMap<>(); for (String chainInfo : cmd.getVolumeUuids()) { if (chainInfo != null) { @@ -4464,7 +4478,7 @@ public class VmwareResource extends ServerResourceBase implements StoragePoolRes if (getVmPowerState(vmMo) != PowerState.PowerOff) { String msg = "Stop VM " + cmd.getVmName() + " Succeed"; - boolean success = false; + boolean success; if (cmd.isForceStop()) { success = vmMo.powerOff(); } else { @@ -4750,7 +4764,7 @@ public class VmwareResource extends ServerResourceBase implements StoragePoolRes } Answer createAnswerForCmd(VirtualMachineMO vmMo, List volumeObjectToList, Command cmd, Map volumeDeviceKey) throws Exception { - List volumeToList = new ArrayList<>(); + List volumeToList; VirtualMachineDiskInfoBuilder diskInfoBuilder = vmMo.getDiskInfoBuilder(); VirtualDisk[] disks = vmMo.getAllDiskDevice(); Answer answer; @@ -4812,7 +4826,7 @@ public class VmwareResource extends ServerResourceBase implements StoragePoolRes } private VirtualMachineMO getVirtualMachineMO(String vmName, VmwareHypervisorHost hyperHost) { - VirtualMachineMO vmMo = null; + VirtualMachineMO vmMo; try { // find VM through datacenter (VM is not at the target host yet) vmMo = hyperHost.findVmOnPeerHyperHost(vmName); @@ -4881,16 +4895,16 @@ public class VmwareResource extends ServerResourceBase implements StoragePoolRes } private Answer migrateVolume(MigrateVolumeCommand cmd) { - Answer answer = null; + Answer answer; String path = cmd.getVolumePath(); VmwareHypervisorHost hyperHost = getHyperHost(getServiceContext()); VirtualMachineMO vmMo = null; - DatastoreMO sourceDsMo = null; + DatastoreMO sourceDsMo; DatastoreMO destinationDsMo = null; - ManagedObjectReference morSourceDS = null; - ManagedObjectReference morDestinationDS = null; - String vmdkDataStorePath = null; + ManagedObjectReference morSourceDS; + ManagedObjectReference morDestinationDS; + String vmdkDataStorePath; boolean isvVolsInvolved = false; String vmName = null; @@ -5047,20 +5061,20 @@ public class VmwareResource extends ServerResourceBase implements StoragePoolRes String vmName = cmd.getAttachedVmName(); - VirtualMachineMO vmMo = null; - VmwareHypervisorHost srcHyperHost = null; + VirtualMachineMO vmMo; + VmwareHypervisorHost srcHyperHost; // OfflineVmwareMigration: ifhost is null ??? if (StringUtils.isBlank(cmd.getAttachedVmName())) { return migrateVolume(cmd); } - ManagedObjectReference morDs = null; - ManagedObjectReference morDc = null; + ManagedObjectReference morDs; + ManagedObjectReference morDc; VirtualMachineRelocateSpec relocateSpec = new VirtualMachineRelocateSpec(); - List diskLocators = new ArrayList(); - VirtualMachineRelocateSpecDiskLocator diskLocator = null; + List diskLocators = new ArrayList<>(); + VirtualMachineRelocateSpecDiskLocator diskLocator; - String tgtDsName = ""; + String tgtDsName; try { srcHyperHost = getHyperHost(getServiceContext()); @@ -5120,9 +5134,9 @@ public class VmwareResource extends ServerResourceBase implements StoragePoolRes // Hence set the existing datastore as target datastore for volumes that are not to be migrated. List> diskDatastores = vmMo.getAllDiskDatastores(); for (Pair diskDatastore : diskDatastores) { - if (diskDatastore.first().intValue() != diskId) { + if (diskDatastore.first() != diskId) { diskLocator = new VirtualMachineRelocateSpecDiskLocator(); - diskLocator.setDiskId(diskDatastore.first().intValue()); + diskLocator.setDiskId(diskDatastore.first()); diskLocator.setDatastore(diskDatastore.second()); diskLocators.add(diskLocator); } @@ -5261,8 +5275,8 @@ public class VmwareResource extends ServerResourceBase implements StoragePoolRes DatastoreMO dsMo = new DatastoreMO(getServiceContext(), morDatastore); HypervisorHostHelper.createBaseFolder(dsMo, hyperHost, pool.getType()); - long capacity = 0; - long available = 0; + long capacity; + long available; List childDatastoresModifyStoragePoolAnswers = new ArrayList<>(); if (pool.getType() == StoragePoolType.DatastoreCluster) { StoragepodMO datastoreClusterMo = new StoragepodMO(getServiceContext(), morDatastore); @@ -5544,7 +5558,7 @@ public class VmwareResource extends ServerResourceBase implements StoragePoolRes * "ManageSnapshotCommand", * "{\"_commandSwitch\":\"-c\",\"_volumePath\":\"i-2-3-KY-ROOT\",\"_snapshotName\":\"i-2-3-KY_i-2-3-KY-ROOT_20101102203827\",\"_snapshotId\":1,\"_vmName\":\"i-2-3-KY\"}" */ - boolean success = false; + boolean success; String cmdSwitch = cmd.getCommandSwitch(); String snapshotOp = "Unsupported snapshot command." + cmdSwitch; if (cmdSwitch.equals(ManageSnapshotCommand.CREATE_SNAPSHOT)) { @@ -5553,8 +5567,8 @@ public class VmwareResource extends ServerResourceBase implements StoragePoolRes snapshotOp = "destroy"; } - String details = "ManageSnapshotCommand operation: " + snapshotOp + " Failed for snapshotId: " + snapshotId; - String snapshotUUID = null; + String details; + String snapshotUUID; // snapshot operation (create or destroy) is handled inside BackupSnapshotCommand(), we just fake // a success return here @@ -5612,7 +5626,7 @@ public class VmwareResource extends ServerResourceBase implements StoragePoolRes } protected Answer execute(CreateVolumeFromSnapshotCommand cmd) { - String details = null; + String details; boolean success = false; String newVolumeName = UUID.randomUUID().toString(); @@ -5660,8 +5674,8 @@ public class VmwareResource extends ServerResourceBase implements StoragePoolRes ManagedObjectReference morDs = HypervisorHostHelper.findDatastoreWithBackwardsCompatibility(hyperHost, cmd.getStorageId()); if (morDs != null) { - long capacity = 0; - long free = 0; + long capacity; + long free; if (cmd.getPooltype() == StoragePoolType.DatastoreCluster) { StoragepodMO datastoreClusterMo = new StoragepodMO(getServiceContext(), morDs); StoragePodSummary summary = datastoreClusterMo.getDatastoreClusterSummary(); @@ -5795,8 +5809,8 @@ public class VmwareResource extends ServerResourceBase implements StoragePoolRes String details = "Unable to find IP Address of VM. "; String vmName = cmd.getVmName(); boolean result = false; - String ip = null; - Answer answer = null; + String ip; + Answer answer; VmwareContext context = getServiceContext(); VmwareHypervisorHost hyperHost = getHyperHost(context); @@ -6083,7 +6097,7 @@ public class VmwareResource extends ServerResourceBase implements StoragePoolRes VirtualMachineMO vmMo = new VirtualMachineMO(hyperHost.getContext(), oc.getObj()); if (!template && isWorker) { - boolean recycle = false; + boolean recycle; recycle = mgr.needRecycle(workerTag); if (recycle) { @@ -6117,7 +6131,7 @@ public class VmwareResource extends ServerResourceBase implements StoragePoolRes @Override public StartupCommand[] initialize() { try { - String hostApiVersion = "4.1"; + String hostApiVersion; VmwareContext context = getServiceContext(); try { VmwareHypervisorHost hyperHost = getHyperHost(context); @@ -6159,7 +6173,7 @@ public class VmwareResource extends ServerResourceBase implements StoragePoolRes } private List initializeLocalStorage() { - List storageCmds = new ArrayList(); + List storageCmds = new ArrayList<>(); VmwareContext context = getServiceContext(); try { @@ -6213,7 +6227,7 @@ public class VmwareResource extends ServerResourceBase implements StoragePoolRes VmwareContext serviceContext = getServiceContext(); Map details = cmd.getHostDetails(); if (details == null) { - details = new HashMap(); + details = new HashMap<>(); } try { @@ -6221,16 +6235,16 @@ public class VmwareResource extends ServerResourceBase implements StoragePoolRes fillHostNetworkInfo(serviceContext, cmd); fillHostDetailsInfo(serviceContext, details); } catch (RuntimeFaultFaultMsg e) { - logger.error("RuntimeFault while retrieving host info: " + e.toString(), e); + logger.error("RuntimeFault while retrieving host info: " + e, e); throw new CloudRuntimeException("RuntimeFault while retrieving host info"); } catch (RemoteException e) { - logger.error("RemoteException while retrieving host info: " + e.toString(), e); + logger.error("RemoteException while retrieving host info: " + e, e); invalidateServiceContext(); throw new CloudRuntimeException("RemoteException while retrieving host info"); } catch (Exception e) { - logger.error("Exception while retrieving host info: " + e.toString(), e); + logger.error("Exception while retrieving host info: " + e, e); invalidateServiceContext(); - throw new CloudRuntimeException("Exception while retrieving host info: " + e.toString()); + throw new CloudRuntimeException("Exception while retrieving host info: " + e); } cmd.setHostDetails(details); @@ -6329,7 +6343,7 @@ public class VmwareResource extends ServerResourceBase implements StoragePoolRes try { return getHostVmStateReport(); } catch (Exception e) { - return new HashMap(); + return new HashMap<>(); } } @@ -6448,7 +6462,7 @@ public class VmwareResource extends ServerResourceBase implements StoragePoolRes // the internal CS name, but the custom field CLOUD_VM_INTERNAL_NAME always stores the internal CS name. ObjectContent[] ocs = hyperHost.getVmPropertiesOnHyperHost(new String[]{"name", "runtime.powerState", "config.template", instanceNameCustomField}); - HashMap newStates = new HashMap(); + HashMap newStates = new HashMap<>(); if (ocs != null && ocs.length > 0) { for (ObjectContent oc : ocs) { List objProps = oc.getPropSet(); @@ -6500,7 +6514,7 @@ public class VmwareResource extends ServerResourceBase implements StoragePoolRes // the internal CS name, but the custom field CLOUD_VM_INTERNAL_NAME always stores the internal CS name. ObjectContent[] ocs = hyperHost.getVmPropertiesOnHyperHost(new String[]{"name", "runtime.powerState", "config.template", instanceNameCustomField}); - HashMap newStates = new HashMap(); + HashMap newStates = new HashMap<>(); if (ocs != null && ocs.length > 0) { for (ObjectContent oc : ocs) { List objProps = oc.getPropSet(); @@ -6541,7 +6555,7 @@ public class VmwareResource extends ServerResourceBase implements StoragePoolRes private HashMap getVmStats(List vmNames) throws Exception { VmwareHypervisorHost hyperHost = getHyperHost(getServiceContext()); - HashMap vmResponseMap = new HashMap(); + HashMap vmResponseMap = new HashMap<>(); ManagedObjectReference perfMgr = getServiceContext().getServiceContent().getPerfManager(); VimPortType service = getServiceContext().getService(); @@ -6603,7 +6617,7 @@ public class VmwareResource extends ServerResourceBase implements StoragePoolRes for (ObjectContent oc : ocs) { List objProps = oc.getPropSet(); if (objProps != null) { - String name = null; + String name; String numberCPUs = null; double maxCpuUsage = 0; String memlimit = null; @@ -6654,7 +6668,7 @@ public class VmwareResource extends ServerResourceBase implements StoragePoolRes double diskReadKbs = 0; double diskWriteKbs = 0; - final ArrayList perfMetricsIds = new ArrayList(); + final ArrayList perfMetricsIds = new ArrayList<>(); if (rxPerfCounterInfo != null) { perfMetricsIds.add(VmwareHelper.createPerfMetricId(rxPerfCounterInfo, "")); } @@ -6781,7 +6795,7 @@ public class VmwareResource extends ServerResourceBase implements StoragePoolRes stats[1] += Long.parseLong(splitResult[i++]); } } catch (Throwable e) { - logger.warn("Unable to parse return from script return of network usage command: " + e.toString(), e); + logger.warn("Unable to parse return from script return of network usage command: " + e, e); } } return stats; @@ -6804,7 +6818,7 @@ public class VmwareResource extends ServerResourceBase implements StoragePoolRes sch.connect(addr); return null; } catch (IOException e) { - logger.info("Could not connect to " + ipAddress + " due to " + e.toString()); + logger.info("Could not connect to " + ipAddress + " due to " + e); if (e instanceof ConnectException) { // if connection is refused because of VM is being started, // we give it more sleep time @@ -6945,21 +6959,21 @@ public class VmwareResource extends ServerResourceBase implements StoragePoolRes Integer intObj = (Integer) params.get("ports.per.dvportgroup"); if (intObj != null) - _portsPerDvPortGroup = intObj.intValue(); + _portsPerDvPortGroup = intObj; logger.info("VmwareResource network configuration info." + " private traffic over vSwitch: " + _privateNetworkVSwitchName + ", public traffic over " + _publicTrafficInfo.getVirtualSwitchType() + " : " + _publicTrafficInfo.getVirtualSwitchName() + ", guest traffic over " + _guestTrafficInfo.getVirtualSwitchType() + " : " + _guestTrafficInfo.getVirtualSwitchName()); Boolean boolObj = (Boolean) params.get("vmware.create.full.clone"); - if (boolObj != null && boolObj.booleanValue()) { + if (boolObj != null && boolObj) { _fullCloneFlag = true; } else { _fullCloneFlag = false; } boolObj = (Boolean) params.get("vm.instancename.flag"); - if (boolObj != null && boolObj.booleanValue()) { + if (boolObj != null && boolObj) { _instanceNameFlag = true; } else { _instanceNameFlag = false; @@ -7024,7 +7038,7 @@ public class VmwareResource extends ServerResourceBase implements StoragePoolRes @Override public VmwareContext getServiceContext(Command cmd) { - VmwareContext context = null; + VmwareContext context; if (s_serviceContext.get() != null) { context = s_serviceContext.get(); String poolKey = VmwareContextPool.composePoolKey(_vCenterAddress, _username); @@ -7210,7 +7224,7 @@ public class VmwareResource extends ServerResourceBase implements StoragePoolRes logger.debug("Looking for file [" + filePath + "] in the classpath."); } if (!keyFile.exists()) { - logger.error("Unable to locate id_rsa.cloud in your setup at " + keyFile.toString()); + logger.error("Unable to locate id_rsa.cloud in your setup at " + keyFile); } return keyFile; } @@ -7293,13 +7307,13 @@ public class VmwareResource extends ServerResourceBase implements StoragePoolRes } VmwareHypervisorHost sourceHyperHost = hypervisorHost; VmwareHypervisorHost targetHyperHost = hostInTargetCluster; - VirtualMachineMO vmMo = null; - ManagedObjectReference morSourceHostDc = null; + VirtualMachineMO vmMo; + ManagedObjectReference morSourceHostDc; VirtualMachineRelocateSpec relocateSpec = new VirtualMachineRelocateSpec(); - List diskLocators = new ArrayList(); - Set mountedDatastoresAtSource = new HashSet(); + List diskLocators = new ArrayList<>(); + Set mountedDatastoresAtSource = new HashSet<>(); List volumeToList = new ArrayList<>(); - Map volumeDeviceKey = new HashMap(); + Map volumeDeviceKey = new HashMap<>(); try { if (sourceHyperHost == null) { @@ -7339,7 +7353,7 @@ public class VmwareResource extends ServerResourceBase implements StoragePoolRes if (StringUtils.isNotBlank(poolUuid)) { VmwareHypervisorHost dsHost = targetHyperHost == null ? sourceHyperHost : targetHyperHost; - ManagedObjectReference morDatastore = null; + ManagedObjectReference morDatastore; morDatastore = getTargetDatastoreMOReference(poolUuid, dsHost); if (morDatastore == null) { String msg = String.format("Unable to find the target datastore: %s on host: %s to execute migration", poolUuid, dsHost.getHyperHostName()); @@ -7388,9 +7402,9 @@ public class VmwareResource extends ServerResourceBase implements StoragePoolRes // Hence set the existing datastore as target datastore for volumes that are not to be migrated. List> diskDatastores = vmMo.getAllDiskDatastores(); for (Pair diskDatastore : diskDatastores) { - if (!volumeDeviceKey.containsValue(diskDatastore.first().intValue())) { + if (!volumeDeviceKey.containsValue(diskDatastore.first())) { VirtualMachineRelocateSpecDiskLocator diskLocator = new VirtualMachineRelocateSpecDiskLocator(); - diskLocator.setDiskId(diskDatastore.first().intValue()); + diskLocator.setDiskId(diskDatastore.first()); diskLocator.setDatastore(diskDatastore.second()); diskLocators.add(diskLocator); } @@ -7629,7 +7643,7 @@ public class VmwareResource extends ServerResourceBase implements StoragePoolRes VmwareContext context = getServiceContext(); VmwareHypervisorHost hyperHost = getHyperHost(context); - ManagedObjectReference morDatastore = null; + ManagedObjectReference morDatastore; int count = 0; List names = new ArrayList<>(); @@ -7738,7 +7752,7 @@ public class VmwareResource extends ServerResourceBase implements StoragePoolRes return new Answer(command, true, "success"); } catch (Exception e) { logger.error("Unexpected exception: ", e); - return new Answer(command, false, "Unable to execute PrepareForBackupRestorationCommand due to " + e.toString()); + return new Answer(command, false, "Unable to execute PrepareForBackupRestorationCommand due to " + e); } } diff --git a/plugins/storage/volume/linstor/CHANGELOG.md b/plugins/storage/volume/linstor/CHANGELOG.md index 7e9d754b9f6..2abda3ebc50 100644 --- a/plugins/storage/volume/linstor/CHANGELOG.md +++ b/plugins/storage/volume/linstor/CHANGELOG.md @@ -5,6 +5,11 @@ All notable changes to Linstor CloudStack plugin will be documented in this file The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/), and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html). +## [2025-05-07] + +### Added +- Implemented storage/volume stats + ## [2025-03-13] ### Fixed diff --git a/plugins/storage/volume/linstor/src/main/java/org/apache/cloudstack/storage/datastore/driver/LinstorPrimaryDataStoreDriverImpl.java b/plugins/storage/volume/linstor/src/main/java/org/apache/cloudstack/storage/datastore/driver/LinstorPrimaryDataStoreDriverImpl.java index 87dad560f29..574e5ddcfea 100644 --- a/plugins/storage/volume/linstor/src/main/java/org/apache/cloudstack/storage/datastore/driver/LinstorPrimaryDataStoreDriverImpl.java +++ b/plugins/storage/volume/linstor/src/main/java/org/apache/cloudstack/storage/datastore/driver/LinstorPrimaryDataStoreDriverImpl.java @@ -28,11 +28,11 @@ import com.linbit.linstor.api.model.ResourceDefinition; import com.linbit.linstor.api.model.ResourceDefinitionCloneRequest; import com.linbit.linstor.api.model.ResourceDefinitionCloneStarted; import com.linbit.linstor.api.model.ResourceDefinitionCreate; - import com.linbit.linstor.api.model.ResourceDefinitionModify; import com.linbit.linstor.api.model.ResourceGroup; import com.linbit.linstor.api.model.ResourceGroupSpawn; import com.linbit.linstor.api.model.ResourceMakeAvailable; +import com.linbit.linstor.api.model.ResourceWithVolumes; import com.linbit.linstor.api.model.Snapshot; import com.linbit.linstor.api.model.SnapshotRestore; import com.linbit.linstor.api.model.VolumeDefinition; @@ -134,6 +134,9 @@ public class LinstorPrimaryDataStoreDriverImpl implements PrimaryDataStoreDriver @Inject private HostDao _hostDao; + private long volumeStatsLastUpdate = 0L; + private final Map> volumeStats = new HashMap<>(); + public LinstorPrimaryDataStoreDriverImpl() { } @@ -403,9 +406,9 @@ public class LinstorPrimaryDataStoreDriverImpl implements PrimaryDataStoreDriver } } - private String getRscGrp(StoragePoolVO storagePoolVO) { - return storagePoolVO.getUserInfo() != null && !storagePoolVO.getUserInfo().isEmpty() ? - storagePoolVO.getUserInfo() : "DfltRscGrp"; + private String getRscGrp(StoragePool storagePool) { + return storagePool.getUserInfo() != null && !storagePool.getUserInfo().isEmpty() ? + storagePool.getUserInfo() : "DfltRscGrp"; } /** @@ -618,7 +621,7 @@ public class LinstorPrimaryDataStoreDriverImpl implements PrimaryDataStoreDriver */ private void updateRscGrpIfNecessary(DevelopersApi api, String rscName, String tgtRscGrp) throws ApiException { List rscDfns = api.resourceDefinitionList( - Collections.singletonList(rscName), null, null, null); + Collections.singletonList(rscName), false, null, null, null); if (rscDfns != null && !rscDfns.isEmpty()) { ResourceDefinition rscDfn = rscDfns.get(0); @@ -648,7 +651,7 @@ public class LinstorPrimaryDataStoreDriverImpl implements PrimaryDataStoreDriver private void deleteTemplateForProps( DevelopersApi api, String rscName) throws ApiException { List rdList = api.resourceDefinitionList( - Collections.singletonList(rscName), null, null, null); + Collections.singletonList(rscName), false, null, null, null); if (CollectionUtils.isNotEmpty(rdList)) { ResourceDefinitionModify rdm = new ResourceDefinitionModify(); @@ -1506,22 +1509,77 @@ public class LinstorPrimaryDataStoreDriverImpl implements PrimaryDataStoreDriver @Override public boolean canProvideStorageStats() { - return false; + return true; } @Override public Pair getStorageStats(StoragePool storagePool) { - return null; + logger.debug(String.format("Requesting storage stats: %s", storagePool)); + return LinstorUtil.getStorageStats(storagePool.getHostAddress(), getRscGrp(storagePool)); } @Override public boolean canProvideVolumeStats() { - return false; + return LinstorConfigurationManager.VolumeStatsCacheTime.value() > 0; + } + + /** + * Updates the cache map containing current allocated size data. + * @param api Linstor Developers api object + */ + private void fillVolumeStatsCache(DevelopersApi api) { + try { + logger.trace("Start volume stats cache update"); + List resources = api.viewResources( + Collections.emptyList(), + Collections.emptyList(), + Collections.emptyList(), + null, + null, + null); + + List rscDfns = api.resourceDefinitionList( + Collections.emptyList(), true, null, null, null); + + HashMap resSizeMap = new HashMap<>(); + for (ResourceDefinition rscDfn : rscDfns) { + if (CollectionUtils.isNotEmpty(rscDfn.getVolumeDefinitions())) { + resSizeMap.put(rscDfn.getName(), rscDfn.getVolumeDefinitions().get(0).getSizeKib() * 1024); + } + } + + HashMap allocSizeMap = new HashMap<>(); + for (ResourceWithVolumes rsc : resources) { + if (!LinstorUtil.isRscDiskless(rsc) && !rsc.getVolumes().isEmpty()) { + long allocatedBytes = allocSizeMap.getOrDefault(rsc.getName(), 0L); + allocSizeMap.put(rsc.getName(), Math.max(allocatedBytes, rsc.getVolumes().get(0).getAllocatedSizeKib() * 1024)); + } + } + + volumeStats.clear(); + for (Map.Entry entry : allocSizeMap.entrySet()) { + Long reserved = resSizeMap.getOrDefault(entry.getKey(), 0L); + Pair volStat = new Pair<>(entry.getValue(), reserved); + volumeStats.put(entry.getKey(), volStat); + } + volumeStatsLastUpdate = System.currentTimeMillis(); + logger.trace("Done volume stats cache update: {}", volumeStats.size()); + } catch (ApiException e) { + logger.error("Unable to fetch Linstor resources: {}", e.getBestMessage()); + } } @Override public Pair getVolumeStats(StoragePool storagePool, String volumeId) { - return null; + final DevelopersApi api = LinstorUtil.getLinstorAPI(storagePool.getHostAddress()); + synchronized (volumeStats) { + long invalidateCacheTime = volumeStatsLastUpdate + + LinstorConfigurationManager.VolumeStatsCacheTime.value() * 1000; + if (invalidateCacheTime < System.currentTimeMillis()) { + fillVolumeStatsCache(api); + } + return volumeStats.get(LinstorUtil.RSC_PREFIX + volumeId); + } } @Override diff --git a/plugins/storage/volume/linstor/src/main/java/org/apache/cloudstack/storage/datastore/util/LinstorConfigurationManager.java b/plugins/storage/volume/linstor/src/main/java/org/apache/cloudstack/storage/datastore/util/LinstorConfigurationManager.java index 90ebf30f7cd..85a0804dbab 100644 --- a/plugins/storage/volume/linstor/src/main/java/org/apache/cloudstack/storage/datastore/util/LinstorConfigurationManager.java +++ b/plugins/storage/volume/linstor/src/main/java/org/apache/cloudstack/storage/datastore/util/LinstorConfigurationManager.java @@ -24,7 +24,14 @@ public class LinstorConfigurationManager implements Configurable public static final ConfigKey BackupSnapshots = new ConfigKey<>(Boolean.class, "lin.backup.snapshots", "Advanced", "true", "Backup Linstor primary storage snapshots to secondary storage (deleting ps snapshot)", true, ConfigKey.Scope.Global, null); - public static final ConfigKey[] CONFIG_KEYS = new ConfigKey[] { BackupSnapshots }; + public static final ConfigKey VolumeStatsCacheTime = new ConfigKey<>("Advanced", Integer.class, + "lin.volumes.stats.cachetime", "300", + "Cache time of volume stats for Linstor volumes. 0 to disable volume stats", + false); + + public static final ConfigKey[] CONFIG_KEYS = new ConfigKey[] { + BackupSnapshots, VolumeStatsCacheTime + }; @Override public String getConfigComponentName() diff --git a/plugins/storage/volume/linstor/src/main/java/org/apache/cloudstack/storage/datastore/util/LinstorUtil.java b/plugins/storage/volume/linstor/src/main/java/org/apache/cloudstack/storage/datastore/util/LinstorUtil.java index 87b31d70554..40aca2fdd45 100644 --- a/plugins/storage/volume/linstor/src/main/java/org/apache/cloudstack/storage/datastore/util/LinstorUtil.java +++ b/plugins/storage/volume/linstor/src/main/java/org/apache/cloudstack/storage/datastore/util/LinstorUtil.java @@ -196,6 +196,30 @@ public class LinstorUtil { } } + public static Pair getStorageStats(String linstorUrl, String rscGroupName) { + DevelopersApi linstorApi = getLinstorAPI(linstorUrl); + try { + List storagePools = LinstorUtil.getRscGroupStoragePools(linstorApi, rscGroupName); + + long capacity = storagePools.stream() + .filter(sp -> sp.getProviderKind() != ProviderKind.DISKLESS) + .mapToLong(sp -> sp.getTotalCapacity() != null ? sp.getTotalCapacity() : 0L) + .sum() * 1024; // linstor uses kiB + + long used = storagePools.stream() + .filter(sp -> sp.getProviderKind() != ProviderKind.DISKLESS) + .mapToLong(sp -> sp.getTotalCapacity() != null && sp.getFreeCapacity() != null ? + sp.getTotalCapacity() - sp.getFreeCapacity() : 0L) + .sum() * 1024; // linstor uses Kib + LOGGER.debug( + String.format("Linstor(%s;%s): storageStats -> %d/%d", linstorUrl, rscGroupName, capacity, used)); + return new Pair<>(capacity, used); + } catch (ApiException apiEx) { + LOGGER.error(apiEx.getMessage()); + throw new CloudRuntimeException(apiEx.getBestMessage(), apiEx); + } + } + /** * Check if any resource of the given name is InUse on any host. * @@ -304,7 +328,7 @@ public class LinstorUtil { public static List getRDListStartingWith(DevelopersApi api, String startWith) throws ApiException { - List rscDfns = api.resourceDefinitionList(null, null, null, null); + List rscDfns = api.resourceDefinitionList(null, false, null, null, null); return rscDfns.stream() .filter(rscDfn -> rscDfn.getName().toLowerCase().startsWith(startWith.toLowerCase())) @@ -387,7 +411,7 @@ public class LinstorUtil { */ public static ResourceDefinition findResourceDefinition(DevelopersApi api, String rscName, String rscGrpName) throws ApiException { - List rscDfns = api.resourceDefinitionList(null, null, null, null); + List rscDfns = api.resourceDefinitionList(null, false, null, null, null); List rdsStartingWith = rscDfns.stream() .filter(rscDfn -> rscDfn.getName().toLowerCase().startsWith(rscName.toLowerCase())) @@ -403,4 +427,8 @@ public class LinstorUtil { return rd.orElseGet(() -> rdsStartingWith.get(0)); } + + public static boolean isRscDiskless(ResourceWithVolumes rsc) { + return rsc.getFlags() != null && rsc.getFlags().contains(ApiConsts.FLAG_DISKLESS); + } } diff --git a/pom.xml b/pom.xml index daf9c15e01b..1f163c6b1fb 100644 --- a/pom.xml +++ b/pom.xml @@ -175,7 +175,7 @@ 10.1 2.6.6 0.6.0 - 0.6.0 + 0.6.1 0.10.2 3.4.4_1 4.0.1 diff --git a/server/src/main/java/com/cloud/alert/AlertManagerImpl.java b/server/src/main/java/com/cloud/alert/AlertManagerImpl.java index db2ed4927d5..f35a0664a85 100644 --- a/server/src/main/java/com/cloud/alert/AlertManagerImpl.java +++ b/server/src/main/java/com/cloud/alert/AlertManagerImpl.java @@ -19,6 +19,7 @@ package com.cloud.alert; import java.io.UnsupportedEncodingException; import java.text.DecimalFormat; import java.util.ArrayList; +import java.util.Arrays; import java.util.Date; import java.util.HashMap; import java.util.HashSet; @@ -88,41 +89,55 @@ import com.cloud.utils.Pair; import com.cloud.utils.component.ManagerBase; import com.cloud.utils.concurrency.NamedThreadFactory; import com.cloud.utils.db.SearchCriteria; +import org.jetbrains.annotations.Nullable; public class AlertManagerImpl extends ManagerBase implements AlertManager, Configurable { protected Logger logger = LogManager.getLogger(AlertManagerImpl.class.getName()); + public static final List ALERTS = Arrays.asList(AlertType.ALERT_TYPE_HOST + , AlertType.ALERT_TYPE_USERVM + , AlertType.ALERT_TYPE_DOMAIN_ROUTER + , AlertType.ALERT_TYPE_CONSOLE_PROXY + , AlertType.ALERT_TYPE_SSVM + , AlertType.ALERT_TYPE_STORAGE_MISC + , AlertType.ALERT_TYPE_MANAGEMENT_NODE + , AlertType.ALERT_TYPE_RESOURCE_LIMIT_EXCEEDED + , AlertType.ALERT_TYPE_UPLOAD_FAILED + , AlertType.ALERT_TYPE_OOBM_AUTH_ERROR + , AlertType.ALERT_TYPE_HA_ACTION + , AlertType.ALERT_TYPE_CA_CERT); + private static final long INITIAL_CAPACITY_CHECK_DELAY = 30L * 1000L; // Thirty seconds expressed in milliseconds. private static final DecimalFormat DfPct = new DecimalFormat("###.##"); private static final DecimalFormat DfWhole = new DecimalFormat("########"); @Inject - private AlertDao _alertDao; + AlertDao _alertDao; @Inject protected StorageManager _storageMgr; @Inject protected CapacityManager _capacityMgr; @Inject - private CapacityDao _capacityDao; + CapacityDao _capacityDao; @Inject - private DataCenterDao _dcDao; + DataCenterDao _dcDao; @Inject - private HostPodDao _podDao; + HostPodDao _podDao; @Inject - private ClusterDao _clusterDao; + ClusterDao _clusterDao; @Inject - private IPAddressDao _publicIPAddressDao; + IPAddressDao _publicIPAddressDao; @Inject - private DataCenterIpAddressDao _privateIPAddressDao; + DataCenterIpAddressDao _privateIPAddressDao; @Inject - private PrimaryDataStoreDao _storagePoolDao; + PrimaryDataStoreDao _storagePoolDao; @Inject - private ConfigurationDao _configDao; + ConfigurationDao _configDao; @Inject - private ResourceManager _resourceMgr; + ResourceManager _resourceMgr; @Inject - private ConfigurationManager _configMgr; + ConfigurationManager _configMgr; @Inject protected ConfigDepot _configDepot; @Inject @@ -138,7 +153,7 @@ public class AlertManagerImpl extends ManagerBase implements AlertManager, Confi private double _vlanCapacityThreshold = 0.75; private double _directNetworkPublicIpCapacityThreshold = 0.75; private double _localStorageCapacityThreshold = 0.75; - Map _capacityTypeThresholdMap = new HashMap(); + Map _capacityTypeThresholdMap = new HashMap<>(); private final ExecutorService _executor; @@ -402,18 +417,15 @@ public class AlertManagerImpl extends ManagerBase implements AlertManager, Confi private void createOrUpdateVlanCapacity(long dcId, AllocationState capacityState) { SearchCriteria capacitySC = _capacityDao.createSearchCriteria(); - - List capacities = _capacityDao.search(capacitySC, null); - capacitySC = _capacityDao.createSearchCriteria(); capacitySC.addAnd("dataCenterId", SearchCriteria.Op.EQ, dcId); capacitySC.addAnd("capacityType", SearchCriteria.Op.EQ, Capacity.CAPACITY_TYPE_VLAN); - capacities = _capacityDao.search(capacitySC, null); + List capacities = _capacityDao.search(capacitySC, null); int totalVlans = _dcDao.countZoneVlans(dcId, false); int allocatedVlans = _dcDao.countZoneVlans(dcId, true); CapacityState vlanCapacityState = (capacityState == AllocationState.Disabled) ? CapacityState.Disabled : CapacityState.Enabled; - if (capacities.size() == 0) { + if (capacities.isEmpty()) { CapacityVO newVlanCapacity = new CapacityVO(null, dcId, null, null, allocatedVlans, totalVlans, Capacity.CAPACITY_TYPE_VLAN); newVlanCapacity.setCapacityState(vlanCapacityState); _capacityDao.persist(newVlanCapacity); @@ -430,16 +442,13 @@ public class AlertManagerImpl extends ManagerBase implements AlertManager, Confi public void createOrUpdateIpCapacity(Long dcId, Long podId, short capacityType, AllocationState capacityState) { SearchCriteria capacitySC = _capacityDao.createSearchCriteria(); - - List capacities = _capacityDao.search(capacitySC, null); - capacitySC = _capacityDao.createSearchCriteria(); capacitySC.addAnd("podId", SearchCriteria.Op.EQ, podId); capacitySC.addAnd("dataCenterId", SearchCriteria.Op.EQ, dcId); capacitySC.addAnd("capacityType", SearchCriteria.Op.EQ, capacityType); int totalIPs; int allocatedIPs; - capacities = _capacityDao.search(capacitySC, null); + List capacities = _capacityDao.search(capacitySC, null); if (capacityType == Capacity.CAPACITY_TYPE_PRIVATE_IP) { totalIPs = _privateIPAddressDao.countIPs(podId, dcId, false); allocatedIPs = _privateIPAddressDao.countIPs(podId, dcId, true); @@ -452,7 +461,7 @@ public class AlertManagerImpl extends ManagerBase implements AlertManager, Confi } CapacityState ipCapacityState = (capacityState == AllocationState.Disabled) ? CapacityState.Disabled : CapacityState.Enabled; - if (capacities.size() == 0) { + if (capacities.isEmpty()) { CapacityVO newPublicIPCapacity = new CapacityVO(null, dcId, podId, null, allocatedIPs, totalIPs, capacityType); newPublicIPCapacity.setCapacityState(ipCapacityState); _capacityDao.persist(newPublicIPCapacity); @@ -477,7 +486,7 @@ public class AlertManagerImpl extends ManagerBase implements AlertManager, Confi int total = usedTotal.second(); int allocated = usedTotal.first(); CapacityState state = (capacityState == AllocationState.Disabled) ? CapacityState.Disabled : CapacityState.Enabled; - if (capacities.size() == 0) { + if (capacities.isEmpty()) { CapacityVO capacityVO = new CapacityVO(null, dcId, null, null, allocated, total, capacityType); capacityVO.setCapacityState(state); _capacityDao.persist(capacityVO); @@ -524,13 +533,12 @@ public class AlertManagerImpl extends ManagerBase implements AlertManager, Confi // Generate Alerts for Zone Level capacities for (DataCenterVO dc : dataCenterList) { for (Short capacityType : dataCenterCapacityTypes) { - List capacity = new ArrayList(); - capacity = _capacityDao.findCapacityBy(capacityType.intValue(), dc.getId(), null, null); + List capacity = _capacityDao.findCapacityBy(capacityType.intValue(), dc.getId(), null, null); if (capacityType == Capacity.CAPACITY_TYPE_SECONDARY_STORAGE) { capacity.add(getUsedStats(capacityType, dc.getId(), null, null)); } - if (capacity == null || capacity.size() == 0) { + if (capacity == null || capacity.isEmpty()) { continue; } double totalCapacity = capacity.get(0).getTotalCapacity(); @@ -545,7 +553,7 @@ public class AlertManagerImpl extends ManagerBase implements AlertManager, Confi for (HostPodVO pod : podList) { for (Short capacityType : podCapacityTypes) { List capacity = _capacityDao.findCapacityBy(capacityType.intValue(), pod.getDataCenterId(), pod.getId(), null); - if (capacity == null || capacity.size() == 0) { + if (capacity == null || capacity.isEmpty()) { continue; } double totalCapacity = capacity.get(0).getTotalCapacity(); @@ -559,11 +567,10 @@ public class AlertManagerImpl extends ManagerBase implements AlertManager, Confi // Generate Alerts for Cluster Level capacities for (ClusterVO cluster : clusterList) { for (Short capacityType : clusterCapacityTypes) { - List capacity = new ArrayList(); - capacity = _capacityDao.findCapacityBy(capacityType.intValue(), cluster.getDataCenterId(), null, cluster.getId()); + List capacity = _capacityDao.findCapacityBy(capacityType.intValue(), cluster.getDataCenterId(), null, cluster.getId()); // cpu and memory allocated capacity notification threshold can be defined at cluster level, so getting the value if they are defined at cluster level - double threshold = 0; + double threshold; switch (capacityType) { case Capacity.CAPACITY_TYPE_STORAGE: capacity.add(getUsedStats(capacityType, cluster.getDataCenterId(), cluster.getPodId(), cluster.getId())); @@ -581,7 +588,7 @@ public class AlertManagerImpl extends ManagerBase implements AlertManager, Confi default: threshold = _capacityTypeThresholdMap.get(capacityType); } - if (capacity == null || capacity.size() == 0) { + if (capacity == null || capacity.isEmpty()) { continue; } @@ -697,7 +704,7 @@ public class AlertManagerImpl extends ManagerBase implements AlertManager, Confi private List getCapacityTypesAtZoneLevel() { - List dataCenterCapacityTypes = new ArrayList(); + List dataCenterCapacityTypes = new ArrayList<>(); dataCenterCapacityTypes.add(Capacity.CAPACITY_TYPE_VIRTUAL_NETWORK_PUBLIC_IP); dataCenterCapacityTypes.add(Capacity.CAPACITY_TYPE_DIRECT_ATTACHED_PUBLIC_IP); dataCenterCapacityTypes.add(Capacity.CAPACITY_TYPE_SECONDARY_STORAGE); @@ -709,7 +716,7 @@ public class AlertManagerImpl extends ManagerBase implements AlertManager, Confi private List getCapacityTypesAtPodLevel() { - List podCapacityTypes = new ArrayList(); + List podCapacityTypes = new ArrayList<>(); podCapacityTypes.add(Capacity.CAPACITY_TYPE_PRIVATE_IP); return podCapacityTypes; @@ -717,7 +724,7 @@ public class AlertManagerImpl extends ManagerBase implements AlertManager, Confi private List getCapacityTypesAtClusterLevel() { - List clusterCapacityTypes = new ArrayList(); + List clusterCapacityTypes = new ArrayList<>(); clusterCapacityTypes.add(Capacity.CAPACITY_TYPE_CPU); clusterCapacityTypes.add(Capacity.CAPACITY_TYPE_MEMORY); clusterCapacityTypes.add(Capacity.CAPACITY_TYPE_STORAGE); @@ -748,19 +755,11 @@ public class AlertManagerImpl extends ManagerBase implements AlertManager, Confi public void sendAlert(AlertType alertType, DataCenter dataCenter, Pod pod, Cluster cluster, String subject, String content) throws MessagingException, UnsupportedEncodingException { - logger.warn(String.format("alertType=[%s] dataCenter=[%s] pod=[%s] cluster=[%s] message=[%s].", alertType, dataCenter, pod, cluster, subject)); - AlertVO alert = null; Long clusterId = cluster == null ? null : cluster.getId(); Long podId = pod == null ? null : pod.getId(); long dcId = dataCenter == null ? 0L : dataCenter.getId(); - if ((alertType != AlertManager.AlertType.ALERT_TYPE_HOST) && (alertType != AlertManager.AlertType.ALERT_TYPE_USERVM) - && (alertType != AlertManager.AlertType.ALERT_TYPE_DOMAIN_ROUTER) && (alertType != AlertManager.AlertType.ALERT_TYPE_CONSOLE_PROXY) - && (alertType != AlertManager.AlertType.ALERT_TYPE_SSVM) && (alertType != AlertManager.AlertType.ALERT_TYPE_STORAGE_MISC) - && (alertType != AlertManager.AlertType.ALERT_TYPE_MANAGEMENT_NODE) && (alertType != AlertManager.AlertType.ALERT_TYPE_RESOURCE_LIMIT_EXCEEDED) - && (alertType != AlertManager.AlertType.ALERT_TYPE_UPLOAD_FAILED) && (alertType != AlertManager.AlertType.ALERT_TYPE_OOBM_AUTH_ERROR) - && (alertType != AlertManager.AlertType.ALERT_TYPE_HA_ACTION) && (alertType != AlertManager.AlertType.ALERT_TYPE_CA_CERT)) { - alert = _alertDao.getLastAlert(alertType.getType(), dcId, podId, clusterId); - } + logger.warn(String.format("alertType=[%s] dataCenterId=[%s] podId=[%s] clusterId=[%s] message=[%s].", alertType, dcId, podId, clusterId, subject)); + AlertVO alert = getAlertForTrivialAlertType(alertType, dcId, podId, clusterId); if (alert == null) { AlertVO newAlert = new AlertVO(); @@ -802,6 +801,15 @@ public class AlertManagerImpl extends ManagerBase implements AlertManager, Confi } + @Nullable + private AlertVO getAlertForTrivialAlertType(AlertType alertType, long dataCenterId, Long podId, Long clusterId) { + AlertVO alert = null; + if (!ALERTS.contains(alertType)) { + alert = _alertDao.getLastAlert(alertType.getType(), dataCenterId, podId, clusterId); + } + return alert; + } + protected void sendMessage(SMTPMailProperties mailProps) { _executor.execute(new Runnable() { @Override diff --git a/server/src/main/java/com/cloud/network/NetworkServiceImpl.java b/server/src/main/java/com/cloud/network/NetworkServiceImpl.java index 2e5308b5bd8..ccafc2da258 100644 --- a/server/src/main/java/com/cloud/network/NetworkServiceImpl.java +++ b/server/src/main/java/com/cloud/network/NetworkServiceImpl.java @@ -2909,6 +2909,12 @@ public class NetworkServiceImpl extends ManagerBase implements NetworkService, C Account callerAccount = _accountMgr.getActiveAccountById(user.getAccountId()); _accountMgr.checkAccess(callerAccount, AccessType.OperateEntry, true, network); if (!network.isRedundant() && makeRedundant) { + NetworkOffering networkOffering = _entityMgr.findById(NetworkOffering.class, network.getNetworkOfferingId()); + Map sourceNatCapabilities = getNetworkOfferingServiceCapabilities(networkOffering, Service.SourceNat); + String isRedundantRouterSupported = sourceNatCapabilities.get(Capability.RedundantRouter); + if (!Boolean.parseBoolean(isRedundantRouterSupported)) { + throw new InvalidParameterValueException(String.format("Redundant router is not supported by the network offering %s", networkOffering)); + } network.setRedundant(true); if (!_networksDao.update(network.getId(), network)) { throw new CloudRuntimeException("Failed to update network into a redundant one, please try again");