Skip to content

Commit e5ede1f

Browse files
authored
Merge pull request #116 from gh-ca/yco730
Yco730
2 parents b2a77ac + 8533f63 commit e5ede1f

File tree

3 files changed

+309
-1
lines changed

3 files changed

+309
-1
lines changed

dmestore-service/src/main/java/com/huawei/dmestore/mvc/VmfsAccessController.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,7 @@ public ResponseBodyBean unmountVmfs(@RequestBody Map<String, Object> params) {
145145
try {
146146
//用于衡量部分成功情况返回结果
147147
params.put("success", PARTIAL_SUCCESS);
148-
Map<String, Object> map = vmfsAccessService.unmountVmfs(params);
148+
Map<String, Object> map = vmfsAccessService.unmountVmfsNew(params);
149149
if (map.size() == 0) {
150150
return success(null, "unmount vmfs success!");
151151
} else if (Boolean.valueOf(params.get("success").toString())) {

dmestore-service/src/main/java/com/huawei/dmestore/services/VmfsAccessService.java

+3
Original file line numberDiff line numberDiff line change
@@ -244,4 +244,7 @@ List<Map<String, String>> estimateConnectivityOfHostOrHostgroup(String storageId
244244
* @Date 2021/7/8 15:53
245245
*/
246246
List<Map<String, String>> queryMountableVmfsByClusterId(String clusterObjectId, String dataStoreType) throws DmeException;
247+
248+
Map<String, Object> unmountVmfsNew(Map<String, Object> params) throws DmeException;
249+
247250
}

dmestore-service/src/main/java/com/huawei/dmestore/services/VmfsAccessServiceImpl.java

+305
Original file line numberDiff line numberDiff line change
@@ -5700,6 +5700,10 @@ public MountVmfsReturn mountVmfsNew(Map<String, Object> params) throws DmeExcept
57005700
if (!CollectionUtils.isEmpty(mappedHostGroupId)){
57015701
hostGroupIds.removeAll(mappedHostGroupId);
57025702
}
5703+
if(CollectionUtils.isEmpty(hostGroupIds)){
5704+
throw new DmeException("no need mapping hostGroup");
5705+
5706+
}
57035707
//maps.stream().forEach(map -> hostGroupIds.addAll(map.get("nomalCluster")));
57045708
if (!CollectionUtils.isEmpty(hostGroupIds)) {
57055709
//将lun映射给主机组,然后挂载集群
@@ -6633,4 +6637,305 @@ private String addHostToHostGroup(List<String> hostIds, String hostGroupVolumid,
66336637

66346638
return desc;
66356639
}
6640+
6641+
@Override
6642+
public Map<String, Object> unmountVmfsNew(Map<String, Object> params) throws DmeException {
6643+
Map<String, Object> response = new HashMap<>();
6644+
List<String> taskIds = new ArrayList<>();
6645+
List<String> dataStoreObjectIds = null;
6646+
List<String> hostObjIds = new ArrayList<>();
6647+
List<String> clusterObjIds = new ArrayList<>();
6648+
List<String> errorStoreName = new ArrayList<>();
6649+
List<Map<String, String>> boundVmfs = new ArrayList<>();
6650+
Map<String, List<String>> volumeidHostids = new HashMap<>();
6651+
Map<String, List<String>> storeNameHostids = new HashMap<>();
6652+
//add by yc 20211021
6653+
List<DmeVmwareRelation> dmeVmwareRelations = new ArrayList<>();
6654+
//
6655+
//计数操作对象数量
6656+
int count = 0;
6657+
//失败对象计数
6658+
int failCount = 0;
6659+
// 获取存储对应的卷Id并过滤绑掉虚拟机的存储
6660+
try {
6661+
//获取所有vcenter hostid
6662+
if (!CollectionUtils.isEmpty(params)) {
6663+
if (params.get(HOSTIDS) != null && !"".equals(params.get(HOSTIDS))) {
6664+
hostObjIds = (List<String>) params.get(HOSTIDS);
6665+
}
6666+
if (!StringUtils.isEmpty(ToolUtils.getStr(params.get("clusterIds")))) {
6667+
clusterObjIds = (List<String>) params.get("clusterIds");
6668+
for (String clusterObjId : clusterObjIds) {
6669+
String hosts = vcsdkUtils.getHostsOnCluster(clusterObjId);
6670+
if (!StringUtils.isEmpty(hosts)) {
6671+
List<Map<String, String>> list = gson.fromJson(hosts, List.class);
6672+
for (Map<String, String> map : list) {
6673+
String hostId = map.get(HOSTID);
6674+
if (!StringUtils.isEmpty(hostId)) {
6675+
hostObjIds.add(hostId);
6676+
}
6677+
}
6678+
}
6679+
}
6680+
}
6681+
}
6682+
6683+
if (!CollectionUtils.isEmpty(params) && null != params.get(DATASTORE_OBJECT_IDS)) {
6684+
dataStoreObjectIds = (List<String>) params.get(DATASTORE_OBJECT_IDS);
6685+
count = dataStoreObjectIds.size();
6686+
if (dataStoreObjectIds.size() > 0) {
6687+
for (String dsObjectId : dataStoreObjectIds) {
6688+
List<String> temp = new ArrayList<>();
6689+
temp.addAll(hostObjIds);
6690+
DmeVmwareRelation dvr = dmeVmwareRalationDao.getDmeVmwareRelationByDsId(dsObjectId);
6691+
if (dvr == null) {
6692+
scanVmfs();
6693+
dvr = dmeVmwareRalationDao.getDmeVmwareRelationByDsId(dsObjectId);
6694+
}
6695+
//add by yc 20211021
6696+
if (!StringUtils.isEmpty(dvr)) {
6697+
dmeVmwareRelations.add(dvr);
6698+
}
6699+
//
6700+
Map<String, String> map = vcsdkUtils.hasVmOnDatastore2(dsObjectId);
6701+
if (!CollectionUtils.isEmpty(map)) {
6702+
for (Map.Entry<String, String> entry : map.entrySet()) {
6703+
if (temp.contains(entry.getValue())) {
6704+
LOG.error("the vmfs {} contain vm,can not unmount!!!", dvr.getStoreName());
6705+
Map<String, String> boundedMap = new HashMap<>();
6706+
boundedMap.put(dvr.getStoreName(), "The object which mounted " + entry.getKey() + " is being used by the virtual machine !");
6707+
boundVmfs.add(boundedMap);
6708+
temp.remove(entry.getValue());
6709+
if (!CollectionUtils.isEmpty(temp)) {
6710+
storeNameHostids.put(dvr.getStoreName(), temp);
6711+
volumeidHostids.put(dvr.getVolumeId(), temp);
6712+
}
6713+
failCount++;
6714+
continue;
6715+
}
6716+
}
6717+
}
6718+
if (dvr != null && !CollectionUtils.isEmpty(temp)) {
6719+
volumeidHostids.put(dvr.getVolumeId(), temp);
6720+
storeNameHostids.put(dvr.getStoreName(), temp);
6721+
} else {
6722+
LOG.error("The object has already been deleted or is being used by the virtual machine !");
6723+
}
6724+
}
6725+
if (!CollectionUtils.isEmpty(boundVmfs)) {
6726+
response.put("bounded", boundVmfs);
6727+
}
6728+
} else {
6729+
throw new DmeException("The object has already been deleted or has not been completely created or is being used by the virtual machine ,please synchronize vmfs and try again later!");
6730+
}
6731+
}
6732+
6733+
// vcenter卸载存储
6734+
if (!CollectionUtils.isEmpty(storeNameHostids)) {
6735+
List<Map<String, String>> vcErrors = new ArrayList<>();
6736+
for (Map.Entry<String, List<String>> entry : storeNameHostids.entrySet()) {
6737+
Map<String, Object> dsmap = new HashMap<>();
6738+
dsmap.put(NAME_FIELD, entry.getKey());
6739+
Map<String, String> vcError = vcsdkUtils.unmountVmfsOnHost(gson.toJson(dsmap), entry.getValue());
6740+
if (!CollectionUtils.isEmpty(vcError)) {
6741+
vcErrors.add(vcError);
6742+
errorStoreName.add(entry.getKey());
6743+
failCount++;
6744+
}
6745+
}
6746+
if (!CollectionUtils.isEmpty(vcErrors)) {
6747+
response.put("vcError", vcErrors);
6748+
}
6749+
}
6750+
6751+
Map<String, List<String>> unMappingHost = new HashMap<>();
6752+
Map<String, List<String>> unMappingHostGroup = new HashMap<>();
6753+
Map<String, List<Map<String, Object>>> allinitionators = getAllInitionator();
6754+
6755+
getvolumeIdUnMappinginfo(unMappingHost,unMappingHostGroup,allinitionators,dmeVmwareRelations);
6756+
// 解除主机Lun映射
6757+
if (!CollectionUtils.isEmpty(unMappingHost)){
6758+
Map<String, List<String>> needUnmappedMap = getVolumeidMapToHostOrGroup(unMappingHost);
6759+
for (Map.Entry<String, List<String>> entry : needUnmappedMap.entrySet()) {
6760+
Map<String, Object> unmap = new HashMap<>();
6761+
unmap.put(HOST_ID, entry.getKey());
6762+
unmap.put(VOLUMEIDS, entry.getValue());
6763+
LOG.info("start to unmap the host's lun.");
6764+
String taskId = unmountHostGetTaskId2(unmap);
6765+
if (!StringUtils.isEmpty(taskId)){
6766+
taskIds.add(taskId);
6767+
}
6768+
}
6769+
6770+
}
6771+
// 解除集群Lun映射
6772+
if (!CollectionUtils.isEmpty(unMappingHostGroup)){
6773+
Map<String, List<String>> needUnmappedMap = getVolumeidMapToHostOrGroup(unMappingHostGroup);
6774+
for (Map.Entry<String, List<String>> entry : needUnmappedMap.entrySet()) {
6775+
Map<String, Object> unmap = new HashMap<>();
6776+
unmap.put(HOST_GROUP_ID1, entry.getKey());
6777+
unmap.put(VOLUMEIDS, entry.getValue());
6778+
LOG.info("start to unmap the host's lun.");
6779+
String taskId = unmountHostGroupGetTaskId(unmap);
6780+
if (!StringUtils.isEmpty(taskId)){
6781+
taskIds.add(taskId);
6782+
}
6783+
}
6784+
}
6785+
if (!CollectionUtils.isEmpty(hostObjIds)) {
6786+
for (String hostId : hostObjIds) {
6787+
vcsdkUtils.scanDataStore(null, hostId);
6788+
}
6789+
}
6790+
// 获取卸载的任务完成后的状态,默认超时时间10分钟
6791+
if (!CollectionUtils.isEmpty(taskIds)) {
6792+
// 检测任务等待卸载完成后再删除,不用判断是否卸载成功
6793+
List<Map<String, String>> dmeErrors = new ArrayList<>();
6794+
for (String taskId : taskIds) {
6795+
TaskDetailInfoNew taskDetailInfoNew = taskService.queryTaskByIdReturnMainTask(taskId, longTaskTimeOut);
6796+
Map<String, String> dmeError = new HashMap<>();
6797+
if (taskDetailInfoNew != null && taskDetailInfoNew.getStatus() != 3) {
6798+
// 任务部分成功/失败 取得Lun名称
6799+
List<String> name = taskService.getFailNameFromCreateTask(TASKTYPE, taskId, longTaskTimeOut);
6800+
for (String name1 : name) {
6801+
if (!CollectionUtils.isEmpty(errorStoreName) && !errorStoreName.contains(name1)) {
6802+
failCount++;
6803+
}
6804+
}
6805+
if (!CollectionUtils.isEmpty(name) && ToolUtils.getStr(params.get("language")).equals(LANGUAGE_CN)) {
6806+
dmeError.put(name.toString().replace("[", "").replace("]", ""), "DME 错误: " + taskDetailInfoNew.getDetailCn());
6807+
}
6808+
if (!CollectionUtils.isEmpty(name) && ToolUtils.getStr(params.get("language")).equals(LANGUAGE_EN)) {
6809+
dmeError.put(name.toString().replace("[", "").replace("]", ""), "DME ERROR: " + taskDetailInfoNew.getDetailEn());
6810+
}
6811+
}
6812+
if (dmeError.size() != 0) {
6813+
dmeErrors.add(dmeError);
6814+
}
6815+
}
6816+
if (dmeErrors.size() != 0) {
6817+
response.put("dmeError", dmeErrors);
6818+
}
6819+
}
6820+
if (count > failCount) {
6821+
params.put("success", true);
6822+
}
6823+
} catch (Exception e) {
6824+
throw new DmeException(e.getMessage());
6825+
}
6826+
return response;
6827+
}
6828+
6829+
private void getvolumeIdUnMappinginfo(Map<String, List<String>> unMappingHost, Map<String, List<String>> unMappingHostGroup,
6830+
Map<String, List<Map<String, Object>>> allinitionators,
6831+
List<DmeVmwareRelation> dmeVmwareRelations) throws Exception {
6832+
6833+
6834+
if (!CollectionUtils.isEmpty(dmeVmwareRelations)) {
6835+
for (DmeVmwareRelation dmeVmwareRelation : dmeVmwareRelations) {
6836+
//首先获取存储侧,存储挂载的主机的启动器
6837+
List<String> hostidList = new ArrayList<>();
6838+
List<String> hostgroupidList = new ArrayList<>();
6839+
6840+
String storeId = dmeVmwareRelation.getStoreId();
6841+
List<Map<String, String>> mountedHost = vcsdkUtils.getHostsByDsObjectIdNew(storeId, true);
6842+
List<Map<String, Object>> hbasList = new ArrayList<>();
6843+
if (!CollectionUtils.isEmpty(mountedHost)) {
6844+
for (Map<String, String> mountHostInfo : mountedHost) {
6845+
String hostObj = mountHostInfo.get("hostId");
6846+
if (!StringUtils.isEmpty(hostObj)) {
6847+
List<Map<String, Object>> hbas = vcsdkUtils.getHbasByHostObjectId(hostObj);
6848+
if (!CollectionUtils.isEmpty(hbas)) {
6849+
hbasList.addAll(hbas);
6850+
}
6851+
}
6852+
}
6853+
}
6854+
List<String> wwniqns = new ArrayList<>();
6855+
for (Map<String, Object> hba : hbasList) {
6856+
wwniqns.add(ToolUtils.getStr(hba.get(NAME_FIELD)));
6857+
}
6858+
//dme侧获取已经映射的主机和主机组信息
6859+
String volumeid = dmeVmwareRelation.getVolumeId();
6860+
if (!StringUtils.isEmpty(volumeid)) {
6861+
List<Map<String, String>> attachList = findOrientedVolumeMapping(volumeid);
6862+
if (!CollectionUtils.isEmpty(attachList)) {
6863+
Set<String> hostGroupSet = new HashSet<>();
6864+
Set<String> hostIdSet = new HashSet<>();
6865+
6866+
for (Map<String, String> attch : attachList) {
6867+
String hostGroupid = attch.get("attached_host_group");
6868+
if (!StringUtils.isEmpty(hostGroupid)) {
6869+
hostGroupSet.add(hostGroupid);
6870+
} else if (!StringUtils.isEmpty(attch.get(HOST_ID)) || StringUtils.isEmpty(hostGroupid)) {
6871+
hostIdSet.add(attch.get(HOST_ID));
6872+
}
6873+
6874+
}
6875+
//查询主机组下的主机信息
6876+
if (!CollectionUtils.isEmpty(hostGroupSet)) {
6877+
6878+
for (String hostgroupid : hostGroupSet) {
6879+
List<Map<String, Object>> hostInHostGroup = dmeAccessService.getDmeHostInHostGroup(hostgroupid);
6880+
List<String> portNameList = new ArrayList<>();
6881+
if (!CollectionUtils.isEmpty(hostInHostGroup)) {
6882+
for (Map<String, Object> hostinfo : hostInHostGroup) {
6883+
List<Map<String, Object>> hostinitionators = allinitionators.get(hostinfo.get("id"));
6884+
if (!CollectionUtils.isEmpty(hostinitionators)) {
6885+
for (Map<String, Object> inimap : hostinitionators) {
6886+
String portName = ToolUtils.getStr(inimap.get(PORT_NAME));
6887+
if (!StringUtils.isEmpty(portName)) {
6888+
portNameList.add(portName);
6889+
}
6890+
}
6891+
}
6892+
6893+
}
6894+
}
6895+
boolean flag = false;
6896+
for (String wwniqn : wwniqns) {
6897+
if (portNameList.contains(wwniqn)) {
6898+
flag = true;
6899+
break;
6900+
}
6901+
6902+
}
6903+
if (!flag) {
6904+
hostgroupidList.add(hostgroupid);
6905+
}
6906+
// if (!portNameList.containsAll(wwniqns)) {
6907+
// hostgroupidList.add(hostgroupid);
6908+
// }
6909+
}
6910+
}
6911+
if (!CollectionUtils.isEmpty(hostIdSet)) {
6912+
6913+
for (String hostid : hostIdSet) {
6914+
List<String> hostPortNameList = new ArrayList<>();
6915+
List<Map<String, Object>> hostinitionators = allinitionators.get(hostid);
6916+
if (!CollectionUtils.isEmpty(hostinitionators)) {
6917+
for (Map<String, Object> inimap : hostinitionators) {
6918+
String portName = ToolUtils.getStr(inimap.get(PORT_NAME));
6919+
if (!StringUtils.isEmpty(portName)) {
6920+
hostPortNameList.add(portName);
6921+
}
6922+
}
6923+
}
6924+
if (!hostPortNameList.containsAll(wwniqns)) {
6925+
hostidList.add(hostid);
6926+
}
6927+
}
6928+
}
6929+
}
6930+
if (null != unMappingHost) {
6931+
unMappingHost.put(volumeid, hostidList);
6932+
}
6933+
if (null != unMappingHostGroup) {
6934+
unMappingHostGroup.put(volumeid, hostgroupidList);
6935+
}
6936+
}
6937+
}
6938+
}
6939+
}
6940+
66366941
}

0 commit comments

Comments
 (0)