Skip to content

Commit 7692b74

Browse files
authored
Fix NPE when sending copy command to least busy SSVM (apache#9125)
1 parent c05edc4 commit 7692b74

File tree

3 files changed

+29
-24
lines changed

3 files changed

+29
-24
lines changed

engine/schema/src/main/java/com/cloud/host/dao/HostDao.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -169,4 +169,6 @@ public interface HostDao extends GenericDao<HostVO, Long>, StateDao<Status, Stat
169169
List<HostVO> findHostsWithTagRuleThatMatchComputeOferringTags(String computeOfferingTags);
170170

171171
List<Long> findClustersThatMatchHostTagRule(String computeOfferingTags);
172+
173+
List<Long> listSsvmHostsWithPendingMigrateJobsOrderedByJobCount();
172174
}

engine/schema/src/main/java/com/cloud/host/dao/HostDaoImpl.java

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1371,6 +1371,31 @@ public List<Long> findClustersThatMatchHostTagRule(String computeOfferingTags) {
13711371
return new ArrayList<>(result);
13721372
}
13731373

1374+
@Override
1375+
public List<Long> listSsvmHostsWithPendingMigrateJobsOrderedByJobCount() {
1376+
String query = "SELECT cel.host_id, COUNT(*) " +
1377+
"FROM cmd_exec_log cel " +
1378+
"JOIN host h ON cel.host_id = h.id " +
1379+
"WHERE h.removed IS NULL " +
1380+
"GROUP BY cel.host_id " +
1381+
"ORDER BY 2";
1382+
1383+
TransactionLegacy txn = TransactionLegacy.currentTxn();
1384+
List<Long> result = new ArrayList<>();
1385+
1386+
PreparedStatement pstmt;
1387+
try {
1388+
pstmt = txn.prepareAutoCloseStatement(query);
1389+
ResultSet rs = pstmt.executeQuery();
1390+
while (rs.next()) {
1391+
result.add((long) rs.getInt(1));
1392+
}
1393+
} catch (SQLException e) {
1394+
logger.warn("SQLException caught while listing SSVMs with least migrate jobs.", e);
1395+
}
1396+
return result;
1397+
}
1398+
13741399
private String getHostIdsByComputeTags(List<String> offeringTags){
13751400
List<String> questionMarks = new ArrayList();
13761401
offeringTags.forEach((tag) -> { questionMarks.add("?"); });

engine/storage/src/main/java/org/apache/cloudstack/storage/image/BaseImageStoreDriverImpl.java

Lines changed: 2 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,6 @@
2020

2121
import java.net.URI;
2222
import java.net.URISyntaxException;
23-
import java.sql.PreparedStatement;
24-
import java.sql.ResultSet;
25-
import java.sql.SQLException;
2623
import java.util.ArrayList;
2724
import java.util.Collections;
2825
import java.util.Date;
@@ -87,7 +84,6 @@
8784
import com.cloud.storage.dao.VolumeDao;
8885
import com.cloud.storage.download.DownloadMonitor;
8986
import com.cloud.utils.NumbersUtil;
90-
import com.cloud.utils.db.TransactionLegacy;
9187
import com.cloud.utils.exception.CloudRuntimeException;
9288
import com.cloud.utils.net.Proxy;
9389
import com.cloud.vm.VirtualMachineManager;
@@ -425,8 +421,9 @@ public void copyAsync(DataObject srcData, DataObject destData, Host destHost, As
425421
private Answer sendToLeastBusyEndpoint(List<EndPoint> eps, CopyCommand cmd) {
426422
Answer answer = null;
427423
EndPoint endPoint = null;
428-
List<Long> epIds = ssvmWithLeastMigrateJobs();
429424

425+
logger.debug("Picking SSVM from the pool with least commands running on it.");
426+
List<Long> epIds = hostDao.listSsvmHostsWithPendingMigrateJobsOrderedByJobCount();
430427
if (epIds.isEmpty()) {
431428
Collections.shuffle(eps);
432429
endPoint = eps.get(0);
@@ -533,23 +530,4 @@ public Void createDataDiskTemplateAsync(TemplateInfo dataDiskTemplate, String pa
533530
private Integer getCopyCmdsCountToSpecificSSVM(Long ssvmId) {
534531
return _cmdExecLogDao.getCopyCmdCountForSSVM(ssvmId);
535532
}
536-
537-
private List<Long> ssvmWithLeastMigrateJobs() {
538-
logger.debug("Picking ssvm from the pool with least commands running on it");
539-
String query = "select host_id, count(*) from cmd_exec_log group by host_id order by 2;";
540-
TransactionLegacy txn = TransactionLegacy.currentTxn();
541-
542-
List<Long> result = new ArrayList<Long>();
543-
PreparedStatement pstmt = null;
544-
try {
545-
pstmt = txn.prepareAutoCloseStatement(query);
546-
ResultSet rs = pstmt.executeQuery();
547-
while (rs.next()) {
548-
result.add((long) rs.getInt(1));
549-
}
550-
} catch (SQLException e) {
551-
logger.debug("SQLException caught", e);
552-
}
553-
return result;
554-
}
555533
}

0 commit comments

Comments
 (0)