Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions engine/schema/src/main/java/com/cloud/host/dao/HostDao.java
Original file line number Diff line number Diff line change
Expand Up @@ -169,4 +169,6 @@ public interface HostDao extends GenericDao<HostVO, Long>, StateDao<Status, Stat
List<HostVO> findHostsWithTagRuleThatMatchComputeOferringTags(String computeOfferingTags);

List<Long> findClustersThatMatchHostTagRule(String computeOfferingTags);

List<Long> listSsvmHostsWithPendingMigrateJobsOrderedByJobCount();
}
25 changes: 25 additions & 0 deletions engine/schema/src/main/java/com/cloud/host/dao/HostDaoImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -1371,6 +1371,31 @@ public List<Long> findClustersThatMatchHostTagRule(String computeOfferingTags) {
return new ArrayList<>(result);
}

@Override
public List<Long> listSsvmHostsWithPendingMigrateJobsOrderedByJobCount() {
String query = "SELECT cel.host_id, COUNT(*) " +
"FROM cmd_exec_log cel " +
"JOIN host h ON cel.host_id = h.id " +
"WHERE h.removed IS NULL " +
"GROUP BY cel.host_id " +
"ORDER BY 2";

TransactionLegacy txn = TransactionLegacy.currentTxn();
List<Long> result = new ArrayList<>();

PreparedStatement pstmt;
try {
pstmt = txn.prepareAutoCloseStatement(query);
ResultSet rs = pstmt.executeQuery();
while (rs.next()) {
result.add((long) rs.getInt(1));
}
} catch (SQLException e) {
logger.warn("SQLException caught while listing SSVMs with least migrate jobs.", e);
}
return result;
}

private String getHostIdsByComputeTags(List<String> offeringTags){
List<String> questionMarks = new ArrayList();
offeringTags.forEach((tag) -> { questionMarks.add("?"); });
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,6 @@

import java.net.URI;
import java.net.URISyntaxException;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Date;
Expand Down Expand Up @@ -87,7 +84,6 @@
import com.cloud.storage.dao.VolumeDao;
import com.cloud.storage.download.DownloadMonitor;
import com.cloud.utils.NumbersUtil;
import com.cloud.utils.db.TransactionLegacy;
import com.cloud.utils.exception.CloudRuntimeException;
import com.cloud.utils.net.Proxy;
import com.cloud.vm.VirtualMachineManager;
Expand Down Expand Up @@ -425,8 +421,9 @@ public void copyAsync(DataObject srcData, DataObject destData, Host destHost, As
private Answer sendToLeastBusyEndpoint(List<EndPoint> eps, CopyCommand cmd) {
Answer answer = null;
EndPoint endPoint = null;
List<Long> epIds = ssvmWithLeastMigrateJobs();

logger.debug("Picking SSVM from the pool with least commands running on it.");
List<Long> epIds = hostDao.listSsvmHostsWithPendingMigrateJobsOrderedByJobCount();
if (epIds.isEmpty()) {
Collections.shuffle(eps);
endPoint = eps.get(0);
Expand Down Expand Up @@ -533,23 +530,4 @@ public Void createDataDiskTemplateAsync(TemplateInfo dataDiskTemplate, String pa
private Integer getCopyCmdsCountToSpecificSSVM(Long ssvmId) {
return _cmdExecLogDao.getCopyCmdCountForSSVM(ssvmId);
}

private List<Long> ssvmWithLeastMigrateJobs() {
logger.debug("Picking ssvm from the pool with least commands running on it");
String query = "select host_id, count(*) from cmd_exec_log group by host_id order by 2;";
TransactionLegacy txn = TransactionLegacy.currentTxn();

List<Long> result = new ArrayList<Long>();
PreparedStatement pstmt = null;
try {
pstmt = txn.prepareAutoCloseStatement(query);
ResultSet rs = pstmt.executeQuery();
while (rs.next()) {
result.add((long) rs.getInt(1));
}
} catch (SQLException e) {
logger.debug("SQLException caught", e);
}
return result;
}
}