Skip to content

Commit 6347860

Browse files
author
Sina Kashipazha
committed
Use prepared statement to query database for a number of VM who use a specific tag.
1 parent bf11221 commit 6347860

File tree

2 files changed

+11
-6
lines changed

2 files changed

+11
-6
lines changed

engine/schema/src/main/java/com/cloud/vm/dao/VMInstanceDaoImpl.java

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,9 @@ public class VMInstanceDaoImpl extends GenericDaoBase<VMInstanceVO, Long> implem
129129

130130
private static final String UPDATE_SYSTEM_VM_TEMPLATE_ID_FOR_HYPERVISOR = "UPDATE `cloud`.`vm_instance` SET vm_template_id = ? WHERE type <> 'User' AND hypervisor_type = ? AND removed is NULL";
131131

132+
private static final String COUNT_VMS_BY_ZONE_AND_STATE_AND_HOST_TAG = "SELECT COUNT(1) FROM vm_instance vi JOIN service_offering so ON vi.service_offering_id=so.id " +
133+
"JOIN vm_template vt ON vi.vm_template_id = vt.id WHERE vi.data_center_id = ? AND vi.state = ? AND vi.removed IS NULL AND (so.host_tag = ? OR vt.template_tag = ?)";
134+
132135
@Inject
133136
protected HostDao _hostDao;
134137

@@ -810,14 +813,15 @@ public Long countByZoneAndState(long zoneId, State state) {
810813
@Override
811814
public Long countByZoneAndStateAndHostTag(long dcId, State state, String hostTag) {
812815
TransactionLegacy txn = TransactionLegacy.currentTxn();
813-
String sql = "SELECT COUNT(1) FROM vm_instance vi "
814-
+ "JOIN service_offering so ON vi.service_offering_id=so.id "
815-
+ "JOIN vm_template vt ON vi.vm_template_id = vt.id "
816-
+ "WHERE vi.data_center_id= " + dcId + " AND vi.state = '" + state + "' AND vi.removed IS NULL "
817-
+ "AND (so.host_tag='" + hostTag + "' OR vt.template_tag='" + hostTag + "')";
818816
PreparedStatement pstmt = null;
819817
try {
820-
pstmt = txn.prepareAutoCloseStatement(sql);
818+
pstmt = txn.prepareAutoCloseStatement(COUNT_VMS_BY_ZONE_AND_STATE_AND_HOST_TAG);
819+
820+
pstmt.setLong(1, dcId);
821+
pstmt.setString(2, String.valueOf(state));
822+
pstmt.setString(3, hostTag);
823+
pstmt.setString(4, hostTag);
824+
821825
ResultSet rs = pstmt.executeQuery();
822826
if (rs.next()) {
823827
return rs.getLong(1);

systemvm/debian/root/version

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
prometheus-exporter-enhancement-20211213T174523

0 commit comments

Comments
 (0)