Skip to content

Commit

Permalink
创建vmfs,主机集群列表展示速度较慢问题
Browse files Browse the repository at this point in the history
  • Loading branch information
lianqiang-git committed Apr 21, 2022
1 parent 3a81b5e commit 256de0c
Show file tree
Hide file tree
Showing 6 changed files with 210 additions and 2 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.huawei.dmestore.model;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

/**
Expand All @@ -14,7 +15,7 @@ public class ClusterTree implements Serializable {
private String clusterId;
private String clusterName;
private boolean flag = true;
private List<ClusterTree> children;
private List<ClusterTree> children = new ArrayList<>();

public ClusterTree(String clusterId, String clusterName, List<ClusterTree> children) {
this.clusterId = clusterId;
Expand Down Expand Up @@ -58,11 +59,13 @@ public void setFlag(boolean flag) {
this.flag = flag;
}


@Override
public String toString() {
return "ClusterTree{" +
"clusterId='" + clusterId + '\'' +
", clusterName='" + clusterName + '\'' +
", flag=" + flag +
", children=" + children +
'}';
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
package com.huawei.dmestore.model;

import java.util.ArrayList;
import java.util.List;

public class EsxiInstanceTree {

private List<String> hostIds = new ArrayList<>();
private List<String> hostIdsOnCluster = new ArrayList<>();
private List<String> clusterIds = new ArrayList<>();
private List<String> hostIdsIndependence = new ArrayList<>();
//集群信息
private List<ClusterTree> clusters = new ArrayList<>();
//主机信息
private List<ClusterTree> hosts = new ArrayList<>();
public EsxiInstanceTree() {
}

public List<String> getHostIds() {
return hostIds;
}

public void setHostIds(List<String> hostIds) {
this.hostIds = hostIds;
}

public List<String> getHostIdsOnCluster() {
return hostIdsOnCluster;
}

public void setHostIdsOnCluster(List<String> hostIdsOnCluster) {
this.hostIdsOnCluster = hostIdsOnCluster;
}

public List<String> getClusterIds() {
return clusterIds;
}

public void setClusterIds(List<String> clusterIds) {
this.clusterIds = clusterIds;
}

public List<String> getHostIdsIndependence() {
return hostIdsIndependence;
}

public void setHostIdsIndependence(List<String> hostIdsIndependence) {
this.hostIdsIndependence = hostIdsIndependence;
}

public List<ClusterTree> getClusters() {
return clusters;
}

public void setClusters(List<ClusterTree> clusters) {
this.clusters = clusters;
}

public List<ClusterTree> getHosts() {
return hosts;
}

public void setHosts(List<ClusterTree> hosts) {
this.hosts = hosts;
}

@Override
public String toString() {
return "EsxiInstanceTree{" +
"hostIds=" + hostIds +
", hostIdsOnCluster=" + hostIdsOnCluster +
", clusterIds=" + clusterIds +
", hostIdsIndependence=" + hostIdsIndependence +
", clusters=" + clusters +
", hosts=" + hosts +
'}';
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -294,7 +294,7 @@ public ResponseBodyBean getClustersByDsObjectIdNew(@RequestParam("dataStoreObjec
public ResponseBodyBean listIndependenceHosts() {
String failureStr = "";
try {
return success(vmwareAccessService.listHostsAndClusterReturnTree());
return success(vmwareAccessService.listHostsAndClusterReturnTree1());
} catch (DmeException e) {
LOG.error("list vmware host failure:", e);
failureStr = "list vmware host failure:" + e.toString();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -139,5 +139,7 @@ List<Map<String, String>> getMountDataStoresByClusterObjectId(String clusterObje
*/
List<ClusterTree> listHostsAndClusterReturnTree() throws DmeException;

List<ClusterTree> listHostsAndClusterReturnTree1() throws DmeException;

List<Map<String, String>> getNoMountedHostByDsObj(List<Map<String, String>> lists,String datastoreObjectId) throws DmeException;
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import com.huawei.dmestore.exception.DmeSqlException;
import com.huawei.dmestore.exception.VcenterException;
import com.huawei.dmestore.model.ClusterTree;
import com.huawei.dmestore.model.EsxiInstanceTree;
import com.huawei.dmestore.utils.ToolUtils;
import com.huawei.dmestore.utils.VCSDKUtils;

Expand All @@ -14,6 +15,7 @@

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.util.ObjectUtils;
Expand Down Expand Up @@ -512,6 +514,8 @@ public List<ClusterTree> listHostsAndClusterReturnTree() throws DmeException {
List<String> clusteridList = null;
try {
// 取得vcenter中的所有host。
EsxiInstanceTree instanceTree = vcsdkUtils.getInstanceTree();

String hostListStr = vcsdkUtils.getAllHosts();
clusteridList = vcsdkUtils.getAllClusterIds();
if (!StringUtils.isEmpty(hostListStr)) {
Expand All @@ -529,6 +533,20 @@ public List<ClusterTree> listHostsAndClusterReturnTree() throws DmeException {
if (!CollectionUtils.isEmpty(hostList)) {
temp = getHostTempList(hostList);
//todo 查询主机是否属于集群,如果属于集群就不返回
if (!CollectionUtils.isEmpty(clusteridList)) {
for(String clusterid : clusteridList){
List<String> hosts = vcsdkUtils.getHostidsOnCluster(clusterid);
for (Map<String, String> hostidMap : hostList) {
if (!CollectionUtils.isEmpty(hosts) && hosts.contains(hostidMap.get(HOST_ID))) {
temp.remove(hostidMap.get(HOST_ID));
}
if (temp.size() == 0) {
break;
}
}
}
}

for (Map<String, String> hostidMap : hostList) {
if (!CollectionUtils.isEmpty(clusteridList)) {
for (String clusterid : clusteridList) {
Expand Down Expand Up @@ -577,6 +595,38 @@ public List<ClusterTree> listHostsAndClusterReturnTree() throws DmeException {
return treeList;
}

@Override
public List<ClusterTree> listHostsAndClusterReturnTree1() throws DmeException {
LOG.info("Create VMFS --> Start querying the host list!");
long startTime = System.currentTimeMillis();
EsxiInstanceTree instanceTree = new EsxiInstanceTree();
try {
instanceTree = vcsdkUtils.getInstanceTree();
// 取得独立主机id
if (instanceTree.getHostIds().size() > 0) {
for (String hostId : instanceTree.getHostIds()) {
if (!instanceTree.getHostIdsOnCluster().contains(hostId)) {
instanceTree.getHostIdsIndependence().add(hostId);
}
}
}
List<ClusterTree> clusters = instanceTree.getClusters();
if (instanceTree.getHosts().size() > 0) {
for (ClusterTree host : instanceTree.getHosts()) {
if (instanceTree.getHostIdsIndependence().contains(host.getClusterId())) {
instanceTree.getClusters().add(host);
}
}
}
long endTime = System.currentTimeMillis();
LOG.info("Create VMFS --> End host list query , time consuming: " + (endTime - startTime)+ " ms.");
} catch (Exception e) {
LOG.error("list hosts And clusters return tree error:", e);
throw new DmeException(e.getMessage());
}
return instanceTree.getClusters();
}

@Override
public List<Map<String, String>> getNoMountedHostByDsObj(List<Map<String, String>> lists,String datastoreObjectId) throws DmeException {
List<Map<String, String>> latlists = new ArrayList<>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
import com.huawei.dmestore.entity.VCenterInfo;
import com.huawei.dmestore.exception.DmeException;
import com.huawei.dmestore.exception.VcenterException;
import com.huawei.dmestore.model.ClusterTree;
import com.huawei.dmestore.model.EsxiInstanceTree;
import com.huawei.dmestore.model.UpHostVnicRequestBean;
import com.huawei.vmware.VcConnectionHelpers;
import com.huawei.vmware.autosdk.SessionHelper;
Expand Down Expand Up @@ -440,6 +442,77 @@ public String getHostsByDsObjectId(String dataStoreObjectId) throws VcenterExcep
return getHostsByDsObjectId(dataStoreObjectId, false);
}

/**
* 查询所有主机,集群,集群上主机的信息
* @return
*/
public EsxiInstanceTree getInstanceTree() throws Exception {
EsxiInstanceTree esxiInstanceTree = new EsxiInstanceTree();

List<String> clusterIds = new ArrayList<>();
//集群上主机id
List<String> hostIdsOnCluster = new ArrayList<>();
//独立主机id
List<String> hostIds = new ArrayList();
try{
VmwareContext[] vmwareContexts = vcConnectionHelpers.getAllContext();
for (VmwareContext vmwareContext : vmwareContexts) {
RootFsMo rootFsMo = rootVmwareMoFactory.build(vmwareContext, vmwareContext.getRootFolder());
List<Pair<ManagedObjectReference, String>> cls = rootFsMo.getAllClusterOnRootFs();
if (cls != null && cls.size() > 0) {
for (Pair<ManagedObjectReference, String> cl : cls) {
ClusterTree cluster = new ClusterTree();
ClusterMo cl1 = clusterVmwareMoFactory.build(vmwareContext, cl.first());
String objectId = vcConnectionHelpers.mor2ObjectId(cl1.getMor(), vmwareContext.getServerAddress());
clusterIds.add(objectId);
cluster.setClusterId(objectId);
cluster.setClusterName(cl1.getName());

String serverguid = vcConnectionHelpers.objectId2Serverguid(objectId);
VmwareContext context = vcConnectionHelpers.getServerContext(serverguid);
//获取所有集群上的主机
List<Pair<ManagedObjectReference, String>> clusterHosts = cl1.getClusterHosts();
if (clusterHosts != null && clusterHosts.size() > 0) {
for (Pair<ManagedObjectReference, String> host : clusterHosts) {
ClusterTree cluster1 = new ClusterTree();
HostMo host1 = hostVmwareFactory.build(context, host.first());
String hostId = vcConnectionHelpers.mor2ObjectId(host1.getMor(), context.getServerAddress());
if (!StringUtils.isEmpty(hostId)) {
cluster1.setClusterId(hostId);
cluster1.setClusterName(host1.getName());
hostIdsOnCluster.add(hostId);
}
cluster.getChildren().add(cluster1);
}
esxiInstanceTree.getClusters().add(cluster);
}
//获取所有独立主机
List<Pair<ManagedObjectReference, String>> hosts = rootFsMo.getAllHostOnRootFs();
if (hosts != null && hosts.size() > 0) {
for (Pair<ManagedObjectReference, String> host : hosts) {
ClusterTree cluster1 = new ClusterTree();
HostMo host1 = hostVmwareFactory.build(context, host.first());
String hostId = vcConnectionHelpers.mor2ObjectId(host1.getMor(), context.getServerAddress());
if (!hostIdsOnCluster.contains(hostId)&&!hostIds.contains(hostId)) {
hostIds.add(hostId);
cluster1.setClusterId(hostId);
cluster1.setClusterName(host1.getName());
esxiInstanceTree.getHosts().add(cluster1);
}
}
}
}
}
}
esxiInstanceTree.setClusterIds(clusterIds);
esxiInstanceTree.setHostIds(hostIds);
esxiInstanceTree.setHostIdsOnCluster(hostIdsOnCluster);
}catch(Exception e){
throw new VcenterException(e.getMessage());
}
return esxiInstanceTree;
}

/**
* 得到所有主机的ID与name, boolean mount 是否已经挂载了当前存储的主机
*
Expand Down Expand Up @@ -1030,6 +1103,7 @@ public String getHostsOnCluster(String clusterObjectId) throws VcenterException
map.put(HOST_NAME, host1.getName());
lists.add(map);
}

}
if (lists.size() > 0) {
listStr = gson.toJson(lists);
Expand Down Expand Up @@ -1096,6 +1170,7 @@ public List<String> getHostidsOnCluster(String clusterObjectId) throws VcenterEx
}
return hostIds;
}

/**
* 得到指定集群下的所有主机,以及指定主机所属集群下的所有主机 20200918objectId
*
Expand Down

0 comments on commit 256de0c

Please sign in to comment.