Merge branch 'master' of ssh://git.cloud.com/var/lib/git/cloudstack-oss

This commit is contained in:
root 2011-10-11 16:48:39 +05:30
commit b5cec5c957
5 changed files with 82 additions and 12 deletions

View File

@ -843,12 +843,18 @@ public class ApiResponseHelper implements ResponseGenerator {
Long instanceId = volume.getInstanceId(); Long instanceId = volume.getInstanceId();
if (instanceId != null && volume.getState() != Volume.State.Destroy) { if (instanceId != null && volume.getState() != Volume.State.Destroy) {
VMInstanceVO vm = ApiDBUtils.findVMInstanceById(instanceId); VMInstanceVO vm = ApiDBUtils.findVMInstanceById(instanceId);
volResponse.setVirtualMachineId(vm.getId()); if(vm != null){
volResponse.setVirtualMachineName(vm.getHostName()); volResponse.setVirtualMachineId(vm.getId());
UserVm userVm = ApiDBUtils.findUserVmById(vm.getId()); volResponse.setVirtualMachineName(vm.getHostName());
if (userVm != null) { UserVm userVm = ApiDBUtils.findUserVmById(vm.getId());
volResponse.setVirtualMachineDisplayName(userVm.getDisplayName()); if (userVm != null) {
volResponse.setVirtualMachineState(vm.getState().toString()); volResponse.setVirtualMachineDisplayName(userVm.getDisplayName());
volResponse.setVirtualMachineState(vm.getState().toString());
} else {
s_logger.error("User Vm with Id: "+instanceId+" does not exist for volume "+volume.getId());
}
} else {
s_logger.error("Vm with Id: "+instanceId+" does not exist for volume "+volume.getId());
} }
} }

View File

@ -0,0 +1,36 @@
/**
* Copyright (C) 2011 Cloud.com, Inc. All rights reserved.
*
* This software is licensed under the GNU General Public License v3 or later.
*
* It is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or any later version.
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
package com.cloud.vm.dao;
import javax.ejb.Local;
import org.apache.log4j.Logger;
@Local(value = { UserVmDao.class })
public class RandomlyIncreasingVMInstanceDaoImpl extends UserVmDaoImpl {
public static final Logger s_logger = Logger.getLogger(RandomlyIncreasingVMInstanceDaoImpl.class);
@Override
public <K> K getNextInSequence(final Class<K> clazz, final String name) {
return getRandomlyIncreasingNextInSequence(clazz, name);
}
}

View File

@ -236,5 +236,7 @@ public interface GenericDao<T, ID extends Serializable> {
boolean lockInLockTable(String id, int seconds); boolean lockInLockTable(String id, int seconds);
boolean unlockFromLockTable(String id); boolean unlockFromLockTable(String id);
public <K> K getRandomlyIncreasingNextInSequence(Class<K> clazz, String name);
} }

View File

@ -182,9 +182,11 @@ public abstract class GenericDaoBase<T, ID extends Serializable> implements Gene
Type t = getClass().getGenericSuperclass(); Type t = getClass().getGenericSuperclass();
if (t instanceof ParameterizedType) { if (t instanceof ParameterizedType) {
_entityBeanType = (Class<T>)((ParameterizedType)t).getActualTypeArguments()[0]; _entityBeanType = (Class<T>)((ParameterizedType)t).getActualTypeArguments()[0];
} else { } else if (((Class<?>)t).getGenericSuperclass() instanceof ParameterizedType) {
_entityBeanType = (Class<T>)((ParameterizedType)((Class<?>)t).getGenericSuperclass()).getActualTypeArguments()[0]; _entityBeanType = (Class<T>)((ParameterizedType)((Class<?>)t).getGenericSuperclass()).getActualTypeArguments()[0];
} else {
_entityBeanType = (Class<T>)((ParameterizedType)
( (Class<?>)((Class<?>)t).getGenericSuperclass()).getGenericSuperclass()).getActualTypeArguments()[0];
} }
s_daoMaps.put(_entityBeanType, this); s_daoMaps.put(_entityBeanType, this);
@ -291,6 +293,14 @@ public abstract class GenericDaoBase<T, ID extends Serializable> implements Gene
return s_seqFetcher.getNextSequence(clazz, tg); return s_seqFetcher.getNextSequence(clazz, tg);
} }
@Override @DB(txn=false)
public <K> K getRandomlyIncreasingNextInSequence(final Class<K> clazz, final String name) {
final TableGenerator tg = _tgs.get(name);
assert (tg != null) : "Couldn't find Table generator using " + name;
return s_seqFetcher.getRandomNextSequence(clazz, tg);
}
@Override @DB(txn=false) @Override @DB(txn=false)
public List<T> lockRows(final SearchCriteria<T> sc, final Filter filter, final boolean exclusive) { public List<T> lockRows(final SearchCriteria<T> sc, final Filter filter, final boolean exclusive) {
return search(sc, filter, exclusive, false); return search(sc, filter, exclusive, false);

View File

@ -20,6 +20,7 @@ package com.cloud.utils.db;
import java.sql.PreparedStatement; import java.sql.PreparedStatement;
import java.sql.ResultSet; import java.sql.ResultSet;
import java.sql.SQLException; import java.sql.SQLException;
import java.util.Random;
import java.util.concurrent.Callable; import java.util.concurrent.Callable;
import java.util.concurrent.ExecutorService; import java.util.concurrent.ExecutorService;
import java.util.concurrent.Future; import java.util.concurrent.Future;
@ -46,13 +47,22 @@ import com.cloud.utils.concurrency.NamedThreadFactory;
public class SequenceFetcher { public class SequenceFetcher {
private final static Logger s_logger = Logger.getLogger(SequenceFetcher.class); private final static Logger s_logger = Logger.getLogger(SequenceFetcher.class);
ExecutorService _executors; ExecutorService _executors;
private final static Random random = new Random();
public <T> T getNextSequence(Class<T> clazz, TableGenerator tg) { public <T> T getNextSequence(Class<T> clazz, TableGenerator tg) {
return getNextSequence(clazz, tg, null); return getNextSequence(clazz, tg, null, false);
} }
public <T> T getNextSequence(Class<T> clazz, TableGenerator tg, Object key) { public <T> T getNextSequence(Class<T> clazz, TableGenerator tg, Object key) {
Future<T> future = _executors.submit(new Fetcher<T>(clazz, tg, key)); return getNextSequence(clazz, tg, key, false);
}
public <T> T getRandomNextSequence(Class<T> clazz, TableGenerator tg) {
return getNextSequence(clazz, tg, null, true);
}
public <T> T getNextSequence(Class<T> clazz, TableGenerator tg, Object key, boolean isRandom) {
Future<T> future = _executors.submit(new Fetcher<T>(clazz, tg, key, isRandom));
try { try {
return future.get(); return future.get();
} catch (Exception e) { } catch (Exception e) {
@ -74,11 +84,13 @@ public class SequenceFetcher {
TableGenerator _tg; TableGenerator _tg;
Class<T> _clazz; Class<T> _clazz;
Object _key; Object _key;
boolean isRandom = false;
protected Fetcher(Class<T> clazz, TableGenerator tg, Object key) { protected Fetcher(Class<T> clazz, TableGenerator tg, Object key, boolean isRandom) {
_tg = tg; _tg = tg;
_clazz = clazz; _clazz = clazz;
_key = key; _key = key;
this.isRandom = isRandom;
} }
@Override @SuppressWarnings("unchecked") @Override @SuppressWarnings("unchecked")
@ -103,7 +115,11 @@ public class SequenceFetcher {
sql.append(" WHERE ").append(_tg.pkColumnName()).append("=?"); sql.append(" WHERE ").append(_tg.pkColumnName()).append("=?");
PreparedStatement updateStmt = txn.prepareStatement(sql.toString()); PreparedStatement updateStmt = txn.prepareStatement(sql.toString());
updateStmt.setInt(2, _tg.allocationSize()); if(isRandom){
updateStmt.setInt(2, random.nextInt(10) + 1);
} else {
updateStmt.setInt(2, _tg.allocationSize());
}
if (_key == null) { if (_key == null) {
updateStmt.setString(3, _tg.pkColumnValue()); updateStmt.setString(3, _tg.pkColumnValue());
} else { } else {