Skip to content

Commit

Permalink
Merge pull request #117 from gh-ca/yco730
Browse files Browse the repository at this point in the history
Yco730
  • Loading branch information
andrewliu83 authored Oct 22, 2021
2 parents afa4468 + 8533f63 commit c2233f4
Show file tree
Hide file tree
Showing 3 changed files with 309 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ public ResponseBodyBean unmountVmfs(@RequestBody Map<String, Object> params) {
try {
//用于衡量部分成功情况返回结果
params.put("success", PARTIAL_SUCCESS);
Map<String, Object> map = vmfsAccessService.unmountVmfs(params);
Map<String, Object> map = vmfsAccessService.unmountVmfsNew(params);
if (map.size() == 0) {
return success(null, "unmount vmfs success!");
} else if (Boolean.valueOf(params.get("success").toString())) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -244,4 +244,7 @@ List<Map<String, String>> estimateConnectivityOfHostOrHostgroup(String storageId
* @Date 2021/7/8 15:53
*/
List<Map<String, String>> queryMountableVmfsByClusterId(String clusterObjectId, String dataStoreType) throws DmeException;

Map<String, Object> unmountVmfsNew(Map<String, Object> params) throws DmeException;

}
Original file line number Diff line number Diff line change
Expand Up @@ -5700,6 +5700,10 @@ public MountVmfsReturn mountVmfsNew(Map<String, Object> params) throws DmeExcept
if (!CollectionUtils.isEmpty(mappedHostGroupId)){
hostGroupIds.removeAll(mappedHostGroupId);
}
if(CollectionUtils.isEmpty(hostGroupIds)){
throw new DmeException("no need mapping hostGroup");

}
//maps.stream().forEach(map -> hostGroupIds.addAll(map.get("nomalCluster")));
if (!CollectionUtils.isEmpty(hostGroupIds)) {
//将lun映射给主机组,然后挂载集群
Expand Down Expand Up @@ -6633,4 +6637,305 @@ private String addHostToHostGroup(List<String> hostIds, String hostGroupVolumid,

return desc;
}

@Override
public Map<String, Object> unmountVmfsNew(Map<String, Object> params) throws DmeException {
Map<String, Object> response = new HashMap<>();
List<String> taskIds = new ArrayList<>();
List<String> dataStoreObjectIds = null;
List<String> hostObjIds = new ArrayList<>();
List<String> clusterObjIds = new ArrayList<>();
List<String> errorStoreName = new ArrayList<>();
List<Map<String, String>> boundVmfs = new ArrayList<>();
Map<String, List<String>> volumeidHostids = new HashMap<>();
Map<String, List<String>> storeNameHostids = new HashMap<>();
//add by yc 20211021
List<DmeVmwareRelation> dmeVmwareRelations = new ArrayList<>();
//
//计数操作对象数量
int count = 0;
//失败对象计数
int failCount = 0;
// 获取存储对应的卷Id并过滤绑掉虚拟机的存储
try {
//获取所有vcenter hostid
if (!CollectionUtils.isEmpty(params)) {
if (params.get(HOSTIDS) != null && !"".equals(params.get(HOSTIDS))) {
hostObjIds = (List<String>) params.get(HOSTIDS);
}
if (!StringUtils.isEmpty(ToolUtils.getStr(params.get("clusterIds")))) {
clusterObjIds = (List<String>) params.get("clusterIds");
for (String clusterObjId : clusterObjIds) {
String hosts = vcsdkUtils.getHostsOnCluster(clusterObjId);
if (!StringUtils.isEmpty(hosts)) {
List<Map<String, String>> list = gson.fromJson(hosts, List.class);
for (Map<String, String> map : list) {
String hostId = map.get(HOSTID);
if (!StringUtils.isEmpty(hostId)) {
hostObjIds.add(hostId);
}
}
}
}
}
}

if (!CollectionUtils.isEmpty(params) && null != params.get(DATASTORE_OBJECT_IDS)) {
dataStoreObjectIds = (List<String>) params.get(DATASTORE_OBJECT_IDS);
count = dataStoreObjectIds.size();
if (dataStoreObjectIds.size() > 0) {
for (String dsObjectId : dataStoreObjectIds) {
List<String> temp = new ArrayList<>();
temp.addAll(hostObjIds);
DmeVmwareRelation dvr = dmeVmwareRalationDao.getDmeVmwareRelationByDsId(dsObjectId);
if (dvr == null) {
scanVmfs();
dvr = dmeVmwareRalationDao.getDmeVmwareRelationByDsId(dsObjectId);
}
//add by yc 20211021
if (!StringUtils.isEmpty(dvr)) {
dmeVmwareRelations.add(dvr);
}
//
Map<String, String> map = vcsdkUtils.hasVmOnDatastore2(dsObjectId);
if (!CollectionUtils.isEmpty(map)) {
for (Map.Entry<String, String> entry : map.entrySet()) {
if (temp.contains(entry.getValue())) {
LOG.error("the vmfs {} contain vm,can not unmount!!!", dvr.getStoreName());
Map<String, String> boundedMap = new HashMap<>();
boundedMap.put(dvr.getStoreName(), "The object which mounted " + entry.getKey() + " is being used by the virtual machine !");
boundVmfs.add(boundedMap);
temp.remove(entry.getValue());
if (!CollectionUtils.isEmpty(temp)) {
storeNameHostids.put(dvr.getStoreName(), temp);
volumeidHostids.put(dvr.getVolumeId(), temp);
}
failCount++;
continue;
}
}
}
if (dvr != null && !CollectionUtils.isEmpty(temp)) {
volumeidHostids.put(dvr.getVolumeId(), temp);
storeNameHostids.put(dvr.getStoreName(), temp);
} else {
LOG.error("The object has already been deleted or is being used by the virtual machine !");
}
}
if (!CollectionUtils.isEmpty(boundVmfs)) {
response.put("bounded", boundVmfs);
}
} else {
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!");
}
}

// vcenter卸载存储
if (!CollectionUtils.isEmpty(storeNameHostids)) {
List<Map<String, String>> vcErrors = new ArrayList<>();
for (Map.Entry<String, List<String>> entry : storeNameHostids.entrySet()) {
Map<String, Object> dsmap = new HashMap<>();
dsmap.put(NAME_FIELD, entry.getKey());
Map<String, String> vcError = vcsdkUtils.unmountVmfsOnHost(gson.toJson(dsmap), entry.getValue());
if (!CollectionUtils.isEmpty(vcError)) {
vcErrors.add(vcError);
errorStoreName.add(entry.getKey());
failCount++;
}
}
if (!CollectionUtils.isEmpty(vcErrors)) {
response.put("vcError", vcErrors);
}
}

Map<String, List<String>> unMappingHost = new HashMap<>();
Map<String, List<String>> unMappingHostGroup = new HashMap<>();
Map<String, List<Map<String, Object>>> allinitionators = getAllInitionator();

getvolumeIdUnMappinginfo(unMappingHost,unMappingHostGroup,allinitionators,dmeVmwareRelations);
// 解除主机Lun映射
if (!CollectionUtils.isEmpty(unMappingHost)){
Map<String, List<String>> needUnmappedMap = getVolumeidMapToHostOrGroup(unMappingHost);
for (Map.Entry<String, List<String>> entry : needUnmappedMap.entrySet()) {
Map<String, Object> unmap = new HashMap<>();
unmap.put(HOST_ID, entry.getKey());
unmap.put(VOLUMEIDS, entry.getValue());
LOG.info("start to unmap the host's lun.");
String taskId = unmountHostGetTaskId2(unmap);
if (!StringUtils.isEmpty(taskId)){
taskIds.add(taskId);
}
}

}
// 解除集群Lun映射
if (!CollectionUtils.isEmpty(unMappingHostGroup)){
Map<String, List<String>> needUnmappedMap = getVolumeidMapToHostOrGroup(unMappingHostGroup);
for (Map.Entry<String, List<String>> entry : needUnmappedMap.entrySet()) {
Map<String, Object> unmap = new HashMap<>();
unmap.put(HOST_GROUP_ID1, entry.getKey());
unmap.put(VOLUMEIDS, entry.getValue());
LOG.info("start to unmap the host's lun.");
String taskId = unmountHostGroupGetTaskId(unmap);
if (!StringUtils.isEmpty(taskId)){
taskIds.add(taskId);
}
}
}
if (!CollectionUtils.isEmpty(hostObjIds)) {
for (String hostId : hostObjIds) {
vcsdkUtils.scanDataStore(null, hostId);
}
}
// 获取卸载的任务完成后的状态,默认超时时间10分钟
if (!CollectionUtils.isEmpty(taskIds)) {
// 检测任务等待卸载完成后再删除,不用判断是否卸载成功
List<Map<String, String>> dmeErrors = new ArrayList<>();
for (String taskId : taskIds) {
TaskDetailInfoNew taskDetailInfoNew = taskService.queryTaskByIdReturnMainTask(taskId, longTaskTimeOut);
Map<String, String> dmeError = new HashMap<>();
if (taskDetailInfoNew != null && taskDetailInfoNew.getStatus() != 3) {
// 任务部分成功/失败 取得Lun名称
List<String> name = taskService.getFailNameFromCreateTask(TASKTYPE, taskId, longTaskTimeOut);
for (String name1 : name) {
if (!CollectionUtils.isEmpty(errorStoreName) && !errorStoreName.contains(name1)) {
failCount++;
}
}
if (!CollectionUtils.isEmpty(name) && ToolUtils.getStr(params.get("language")).equals(LANGUAGE_CN)) {
dmeError.put(name.toString().replace("[", "").replace("]", ""), "DME 错误: " + taskDetailInfoNew.getDetailCn());
}
if (!CollectionUtils.isEmpty(name) && ToolUtils.getStr(params.get("language")).equals(LANGUAGE_EN)) {
dmeError.put(name.toString().replace("[", "").replace("]", ""), "DME ERROR: " + taskDetailInfoNew.getDetailEn());
}
}
if (dmeError.size() != 0) {
dmeErrors.add(dmeError);
}
}
if (dmeErrors.size() != 0) {
response.put("dmeError", dmeErrors);
}
}
if (count > failCount) {
params.put("success", true);
}
} catch (Exception e) {
throw new DmeException(e.getMessage());
}
return response;
}

private void getvolumeIdUnMappinginfo(Map<String, List<String>> unMappingHost, Map<String, List<String>> unMappingHostGroup,
Map<String, List<Map<String, Object>>> allinitionators,
List<DmeVmwareRelation> dmeVmwareRelations) throws Exception {


if (!CollectionUtils.isEmpty(dmeVmwareRelations)) {
for (DmeVmwareRelation dmeVmwareRelation : dmeVmwareRelations) {
//首先获取存储侧,存储挂载的主机的启动器
List<String> hostidList = new ArrayList<>();
List<String> hostgroupidList = new ArrayList<>();

String storeId = dmeVmwareRelation.getStoreId();
List<Map<String, String>> mountedHost = vcsdkUtils.getHostsByDsObjectIdNew(storeId, true);
List<Map<String, Object>> hbasList = new ArrayList<>();
if (!CollectionUtils.isEmpty(mountedHost)) {
for (Map<String, String> mountHostInfo : mountedHost) {
String hostObj = mountHostInfo.get("hostId");
if (!StringUtils.isEmpty(hostObj)) {
List<Map<String, Object>> hbas = vcsdkUtils.getHbasByHostObjectId(hostObj);
if (!CollectionUtils.isEmpty(hbas)) {
hbasList.addAll(hbas);
}
}
}
}
List<String> wwniqns = new ArrayList<>();
for (Map<String, Object> hba : hbasList) {
wwniqns.add(ToolUtils.getStr(hba.get(NAME_FIELD)));
}
//dme侧获取已经映射的主机和主机组信息
String volumeid = dmeVmwareRelation.getVolumeId();
if (!StringUtils.isEmpty(volumeid)) {
List<Map<String, String>> attachList = findOrientedVolumeMapping(volumeid);
if (!CollectionUtils.isEmpty(attachList)) {
Set<String> hostGroupSet = new HashSet<>();
Set<String> hostIdSet = new HashSet<>();

for (Map<String, String> attch : attachList) {
String hostGroupid = attch.get("attached_host_group");
if (!StringUtils.isEmpty(hostGroupid)) {
hostGroupSet.add(hostGroupid);
} else if (!StringUtils.isEmpty(attch.get(HOST_ID)) || StringUtils.isEmpty(hostGroupid)) {
hostIdSet.add(attch.get(HOST_ID));
}

}
//查询主机组下的主机信息
if (!CollectionUtils.isEmpty(hostGroupSet)) {

for (String hostgroupid : hostGroupSet) {
List<Map<String, Object>> hostInHostGroup = dmeAccessService.getDmeHostInHostGroup(hostgroupid);
List<String> portNameList = new ArrayList<>();
if (!CollectionUtils.isEmpty(hostInHostGroup)) {
for (Map<String, Object> hostinfo : hostInHostGroup) {
List<Map<String, Object>> hostinitionators = allinitionators.get(hostinfo.get("id"));
if (!CollectionUtils.isEmpty(hostinitionators)) {
for (Map<String, Object> inimap : hostinitionators) {
String portName = ToolUtils.getStr(inimap.get(PORT_NAME));
if (!StringUtils.isEmpty(portName)) {
portNameList.add(portName);
}
}
}

}
}
boolean flag = false;
for (String wwniqn : wwniqns) {
if (portNameList.contains(wwniqn)) {
flag = true;
break;
}

}
if (!flag) {
hostgroupidList.add(hostgroupid);
}
// if (!portNameList.containsAll(wwniqns)) {
// hostgroupidList.add(hostgroupid);
// }
}
}
if (!CollectionUtils.isEmpty(hostIdSet)) {

for (String hostid : hostIdSet) {
List<String> hostPortNameList = new ArrayList<>();
List<Map<String, Object>> hostinitionators = allinitionators.get(hostid);
if (!CollectionUtils.isEmpty(hostinitionators)) {
for (Map<String, Object> inimap : hostinitionators) {
String portName = ToolUtils.getStr(inimap.get(PORT_NAME));
if (!StringUtils.isEmpty(portName)) {
hostPortNameList.add(portName);
}
}
}
if (!hostPortNameList.containsAll(wwniqns)) {
hostidList.add(hostid);
}
}
}
}
if (null != unMappingHost) {
unMappingHost.put(volumeid, hostidList);
}
if (null != unMappingHostGroup) {
unMappingHostGroup.put(volumeid, hostgroupidList);
}
}
}
}
}

}

0 comments on commit c2233f4

Please sign in to comment.