1616//under the License.
1717package com .cloud .gpu .dao ;
1818
19+ import java .sql .PreparedStatement ;
20+ import java .sql .ResultSet ;
21+ import java .sql .SQLException ;
22+ import java .util .ArrayList ;
1923import java .util .HashMap ;
2024import java .util .Iterator ;
2125import java .util .List ;
3337import com .cloud .utils .db .GenericDaoBase ;
3438import com .cloud .utils .db .SearchBuilder ;
3539import com .cloud .utils .db .SearchCriteria ;
40+ import com .cloud .utils .db .TransactionLegacy ;
41+ import com .cloud .utils .exception .CloudRuntimeException ;
3642
3743@ Component
3844@ Local (value = VGPUTypesDao .class )
@@ -41,11 +47,14 @@ public class VGPUTypesDaoImpl extends GenericDaoBase<VGPUTypesVO, Long> implemen
4147
4248 private final SearchBuilder <VGPUTypesVO > _searchByGroupId ;
4349 private final SearchBuilder <VGPUTypesVO > _searchByGroupIdVGPUType ;
44- // private final SearchBuilder<VGPUTypesVO> _searchByHostId;
45- // private final SearchBuilder<VGPUTypesVO> _searchForStaleEntries;
4650
4751 @ Inject protected HostGpuGroupsDao _hostGpuGroupsDao ;
4852
53+ private static final String LIST_ZONE_POD_CLUSTER_WIDE_GPU_CAPACITIES =
54+ "SELECT host_gpu_groups.group_name, vgpu_type, max_vgpu_per_pgpu, SUM(remaining_capacity) AS remaining_capacity, SUM(max_capacity) AS total_capacity FROM" +
55+ " `cloud`.`vgpu_types` INNER JOIN `cloud`.`host_gpu_groups` ON vgpu_types.gpu_group_id = host_gpu_groups.id INNER JOIN `cloud`.`host`" +
56+ " ON host_gpu_groups.host_id = host.id WHERE host.type = 'Routing' AND host.data_center_id = ?" ;
57+
4958 public VGPUTypesDaoImpl () {
5059
5160 _searchByGroupId = createSearchBuilder ();
@@ -58,6 +67,47 @@ public VGPUTypesDaoImpl() {
5867 _searchByGroupIdVGPUType .done ();
5968 }
6069
70+ @ Override
71+ public List <VgpuTypesInfo > listGPUCapacities (Long dcId , Long podId , Long clusterId ) {
72+ StringBuilder finalQuery = new StringBuilder ();
73+ TransactionLegacy txn = TransactionLegacy .currentTxn ();
74+ PreparedStatement pstmt = null ;
75+ List <Long > resourceIdList = new ArrayList <Long >();
76+ ArrayList <VgpuTypesInfo > result = new ArrayList <VgpuTypesInfo >();
77+
78+ resourceIdList .add (dcId );
79+ finalQuery .append (LIST_ZONE_POD_CLUSTER_WIDE_GPU_CAPACITIES );
80+
81+ if (podId != null ) {
82+ finalQuery .append (" AND host.pod_id = ?" );
83+ resourceIdList .add (podId );
84+ }
85+
86+ if (clusterId != null ) {
87+ finalQuery .append (" AND host.cluster_id = ?" );
88+ resourceIdList .add (clusterId );
89+ }
90+ finalQuery .append (" GROUP BY host_gpu_groups.group_name, vgpu_type" );
91+
92+ try {
93+ pstmt = txn .prepareAutoCloseStatement (finalQuery .toString ());
94+ for (int i = 0 ; i < resourceIdList .size (); i ++) {
95+ pstmt .setLong (1 + i , resourceIdList .get (i ));
96+ }
97+ ResultSet rs = pstmt .executeQuery ();
98+ while (rs .next ()) {
99+
100+ VgpuTypesInfo gpuCapacity = new VgpuTypesInfo (rs .getString (1 ), rs .getString (2 ), null , null , null , null , rs .getLong (3 ), rs .getLong (4 ), rs .getLong (5 ));
101+ result .add (gpuCapacity );
102+ }
103+ return result ;
104+ } catch (SQLException e ) {
105+ throw new CloudRuntimeException ("DB Exception on: " + finalQuery , e );
106+ } catch (Throwable e ) {
107+ throw new CloudRuntimeException ("Caught: " + finalQuery , e );
108+ }
109+ }
110+
61111 @ Override
62112 public List <VGPUTypesVO > listByGroupId (long groupId ) {
63113 SearchCriteria <VGPUTypesVO > sc = _searchByGroupId .create ();
0 commit comments