Skip to content

Commit

Permalink
1. remove duplicate refresh
Browse files Browse the repository at this point in the history
2. ResourcePool DAO impr
  • Loading branch information
Xiaoliang.Li committed Jul 3, 2015
1 parent 3d78416 commit 2c7052b
Show file tree
Hide file tree
Showing 8 changed files with 124 additions and 33 deletions.
Expand Up @@ -450,6 +450,9 @@ public Long createCluster(ClusterCreate createSpec) throws Exception {
if (vcClusters == null || vcClusters.isEmpty()) {
throw ClusterConfigException.NO_RESOURCE_POOL_ADDED();
}

this.resMgr.refreshVcResources();

// validate accessibility
validateDatastore(dsNames, vcClusters);
validateNetworkAccessibility(createSpec.getName(), createSpec.getNetworkNames(), vcClusters);
Expand Down Expand Up @@ -719,6 +722,9 @@ public Long resumeClusterCreation(String clusterName) throws Exception {
if (vcClusters.isEmpty()) {
throw ClusterConfigException.NO_DATASTORE_ADDED();
}

this.resMgr.refreshVcResources();

// validate accessibility
validateDatastore(dsNames, vcClusters);
validateNetworkAccessibility(cluster.getName(), cluster.fetchNetworkNameList(), vcClusters);
Expand Down Expand Up @@ -955,6 +961,8 @@ public Long resizeCluster(String clusterName, String nodeGroupName,
throw ClusterConfigException.NO_DATASTORE_ADDED();
}

this.resMgr.refreshVcResources();

// validate accessibility
validateDatastore(dsNames, vcClusters);
validateNetworkAccessibility(cluster.getName(), cluster.fetchNetworkNameList(), vcClusters);
Expand Down
Expand Up @@ -1383,7 +1383,8 @@ public List<BaseNode> getPlacementPlan(ClusterCreate clusterSpec,
List<VcCluster> clusters = resMgr.getAvailableClusters();
AuAssert.check(clusters != null && clusters.size() != 0);
for (VcCluster cl : clusters) {
VcResourceUtils.refreshDatastore(cl);
//refresh once at beginning of cluster create/resume/resize
// VcResourceUtils.refreshDatastore(cl);
container.addResource(cl);
}

Expand Down
Expand Up @@ -181,6 +181,11 @@ public boolean isNetworkAccessibleByCluster(List<String> networkList,
return false;
}

@Override
public void refreshVcResources() {

}

@Mock
@Override
public void refreshResourcePool() {
Expand Down
Expand Up @@ -31,6 +31,8 @@ public interface IResourcePoolDAO extends IBaseDAO<VcResourcePoolEntity> {

List<VcResourcePoolEntity> findAllOrderByClusterName();

List<String> findAllClusterName();

String getNameByClusterAndRp(String vcCluster, String vcRp);

VcResourcePoolEntity findByClusterAndRp(String vcCluster, String vcRp);
Expand Down
Expand Up @@ -19,12 +19,16 @@
import java.util.Map;

import org.apache.log4j.Logger;
import org.hibernate.Criteria;
import org.hibernate.Session;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Projections;
import org.hibernate.criterion.Restrictions;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import com.vmware.bdd.dal.IResourcePoolDAO;
import com.vmware.bdd.entity.ClusterEntity;
import com.vmware.bdd.entity.VcResourcePoolEntity;
import com.vmware.bdd.exception.BddException;

Expand Down Expand Up @@ -60,6 +64,17 @@ public List<VcResourcePoolEntity> findAllOrderByClusterName() {
return this.findByCriteria(new Order[] { order }, null, null);
}

@Override
public List<String> findAllClusterName() {
Session session = getSessionFactory().getCurrentSession();

Criteria criteria = session.createCriteria(VcResourcePoolEntity.class);

criteria.setProjection(Projections.distinct(Projections.property("vcCluster")));

return criteria.list();
}

@Override
@Transactional(readOnly = true)
public String getNameByClusterAndRp(String vcCluster, String vcRp) {
Expand Down
Expand Up @@ -234,4 +234,8 @@ List<String> filterHostsByNetwork(List<String> networkList,
boolean isNetworkAccessibleByCluster(List<String> networkList,
List<com.vmware.bdd.spectypes.VcCluster> clusters);

/**
* refresh host, datastores, networks
*/
void refreshVcResources();
}
Expand Up @@ -204,6 +204,10 @@ public List<VcDatastore> getDatastoreByName(String dsName)
return findDSInVC(datastores);
}

public void refreshVcResources() {
VcResourceUtils.refreshVcResources();
}

@Override
public void refreshDatastore() {
List<VcCluster> vcClusters = VcResourceUtils.getClusters();
Expand Down Expand Up @@ -447,38 +451,24 @@ public List<VcHost> getHostsByClusterName(String clusterName)
*/
@Override
public List<VcCluster> getAvailableClusters() throws VcProviderException {
final List<VcResourcePoolEntity> allRPEntities =
rpDao.findAllOrderByClusterName();
List<VcCluster> clusters =
VcContext.inVcSessionDo(new VcSession<List<VcCluster>>() {
List<VcCluster> result = new ArrayList<VcCluster>();

@Override
protected List<VcCluster> body() throws Exception {
List<VcCluster> vcClusters = VcResourceUtils.getClusters();
for (VcResourcePoolEntity rpEntity : allRPEntities) {
boolean added = false;
for (VcCluster vcCluster : result) {
if (rpEntity.getVcCluster().equals(vcCluster.getName())) {
added = true;
break;
}
}
if (added) {
continue;
}
for (VcCluster vcCluster : vcClusters) {
if (rpEntity.getVcCluster().equals(vcCluster.getName())) {
result.add(vcCluster);
break;
}
}
}
return result;
final HashSet<String> clusterNameFromRps = new HashSet<>(rpDao.findAllClusterName());

return VcContext.inVcSessionDo(new VcSession<List<VcCluster>>() {

@Override
protected List<VcCluster> body() throws Exception {
List<VcCluster> results = new ArrayList<>();

for (VcCluster vcCluster : VcResourceUtils.getClusters()) {
if (clusterNameFromRps.contains(vcCluster.getName())) {
results.add(vcCluster);
}
}

return results;
}

});
return clusters;
});
}


Expand Down Expand Up @@ -527,7 +517,8 @@ public Map<String, String> getCloudProviderAttributes() {
@Override
public List<String> filterHostsByNetwork(List<String> networkList,
List<com.vmware.bdd.spectypes.VcCluster> clusters) {
refreshNetwork();
// refresh all resource when at beginning of creating/resizing/resuming cluster
//refreshNetwork();
Set<String> networkNames = new HashSet<String>();
networkNames.addAll(networkList);
Set<String> portGroupNames = new HashSet<String>();
Expand Down Expand Up @@ -566,7 +557,8 @@ public List<String> filterHostsByNetwork(List<String> networkList,
@Override
public boolean isNetworkAccessibleByCluster(List<String> networkList,
List<com.vmware.bdd.spectypes.VcCluster> clusters) {
refreshNetwork();
// refresh all resource when at beginning of creating/resizing/resuming cluster
// refreshNetwork();

Set<String> portGroupNames = new HashSet<String>();
for (String networkName : networkList) {
Expand Down
Expand Up @@ -22,9 +22,11 @@
import java.util.Map;
import java.util.Set;

import org.apache.commons.collections.CollectionUtils;
import org.apache.log4j.Logger;

import com.vmware.aurora.global.Configuration;
import com.vmware.aurora.util.AuAssert;
import com.vmware.aurora.vc.MoUtil;
import com.vmware.aurora.vc.VcCache;
import com.vmware.aurora.vc.VcCluster;
Expand Down Expand Up @@ -418,6 +420,68 @@ protected Void body() {
});
}


public static void refreshVcResources() {
List<VcCluster> vcClusters = getClusters();
AuAssert.check(CollectionUtils.isNotEmpty(vcClusters), "no VC clusters found.");

for (final VcCluster vcCluster : vcClusters) {
VcContext.inVcSessionDo(new VcSession<Void>() {
@Override
protected Void body() {
try {
vcCluster.update();
} catch (Exception e) {
logger.info("failed to update cluster " + vcCluster.getName()
+ ", ignore this error.", e);
}

List<VcNetwork> networks = vcCluster.getAllNetworks();
if (networks != null) {
for (VcNetwork network : networks) {
try {
network.update();
} catch (Exception e) {
logger.info(
"failed to update network " + network.getName()
+ ", ignore this error.", e);
}
}
}

List<VcDatastore> dss = vcCluster.getAllDatastores();
if (dss != null) {
for (VcDatastore ds : dss) {
try {
ds.update();
} catch (Exception e) {
logger.info("failed to update datastore " + ds.getName()
+ ", ignore this error.", e);
}
}
}
try {
List<VcHost> hosts = vcCluster.getHosts();
if (hosts != null) {
for (VcHost host : hosts) {
try {
host.update();
} catch (Exception e) {
logger.info("failed to update host " + host.getName()
+ ", ignore this error.", e);
}
}
}
} catch (Exception e) {
logger.info("failed to get host list on cluster " + vcCluster.getName()
+ ", ignore this error.", e);
}
return null;
}
});
}
}

public static void refreshResourcePool(final VcCluster cl) {
VcContext.inVcSessionDo(new VcSession<Void>() {
@Override
Expand Down

0 comments on commit 2c7052b

Please sign in to comment.