@@ -75,81 +75,106 @@ private String formatDuplicateHostToReadText(Long poolId, ResultSet rs) throws S
7575 }
7676
7777 private Boolean checkDuplicateHostWithTheSameLocalStorage () {
78+
7879 TransactionLegacy txn = TransactionLegacy .open ("Integrity" );
79- txn .start ();
8080 try {
81- Connection conn ;
81+ txn .start ();
82+ Connection conn = txn .getConnection ();
83+ try (PreparedStatement pstmt =
84+ conn .prepareStatement ("SELECT pool_id FROM host INNER JOIN storage_pool_host_ref INNER JOIN storage_pool WHERE storage_pool.id = storage_pool_host_ref.pool_id and storage_pool.pool_type='LVM' AND host.id=storage_pool_host_ref.host_id AND host.removed IS NULL group by pool_id having count(*) > 1" );
85+ ResultSet rs = pstmt .executeQuery ();)
86+ {
87+ boolean noDuplicate = true ;
88+ StringBuffer helpInfo = new StringBuffer ();
89+ String note =
90+ "DATABASE INTEGRITY ERROR\n Management server detected there are some hosts connect to the same loacal storage, please contact CloudStack support team for solution. Below are detialed info, please attach all of them to CloudStack support. Thank you\n " ;
91+ helpInfo .append (note );
92+ while (rs .next ()) {
93+ try ( PreparedStatement sel_pstmt =
94+ conn .prepareStatement ("select id, status, removed, private_ip_address from host where id in (select host_id from storage_pool_host_ref where pool_id=?)" );
95+ ){
96+ long poolId = rs .getLong (1 );
97+ pstmt .setLong (1 , poolId );
98+ try (ResultSet dhrs = sel_pstmt .executeQuery ();) {
99+ String help = formatDuplicateHostToReadText (poolId , dhrs );
100+ helpInfo .append (help );
101+ helpInfo .append ("\n " );
102+ noDuplicate = false ;
103+ }
104+ catch (Exception e )
105+ {
106+ s_logger .error ("checkDuplicateHostWithTheSameLocalStorage: Exception :" + e .getMessage ());
107+ throw new CloudRuntimeException ("checkDuplicateHostWithTheSameLocalStorage: Exception :" + e .getMessage (),e );
108+ }
109+ }
110+ catch (Exception e )
111+ {
112+ s_logger .error ("checkDuplicateHostWithTheSameLocalStorage: Exception :" + e .getMessage ());
113+ throw new CloudRuntimeException ("checkDuplicateHostWithTheSameLocalStorage: Exception :" + e .getMessage (),e );
114+ }
115+ }
116+ if (noDuplicate ) {
117+ s_logger .debug ("No duplicate hosts with the same local storage found in database" );
118+ } else {
119+ s_logger .error (helpInfo .toString ());
120+ }
121+ txn .commit ();
122+ return noDuplicate ;
123+ }catch (Exception e )
124+ {
125+ s_logger .error ("checkDuplicateHostWithTheSameLocalStorage: Exception :" + e .getMessage ());
126+ throw new CloudRuntimeException ("checkDuplicateHostWithTheSameLocalStorage: Exception :" + e .getMessage (),e );
127+ }
128+ }
129+ catch (Exception e )
130+ {
131+ s_logger .error ("checkDuplicateHostWithTheSameLocalStorage: Exception :" + e .getMessage ());
132+ throw new CloudRuntimeException ("checkDuplicateHostWithTheSameLocalStorage: Exception :" + e .getMessage (),e );
133+ }
134+ finally
135+ {
82136 try {
83- conn = txn .getConnection ();
84- PreparedStatement pstmt =
85- conn .prepareStatement ("SELECT pool_id FROM host INNER JOIN storage_pool_host_ref INNER JOIN storage_pool WHERE storage_pool.id = storage_pool_host_ref.pool_id and storage_pool.pool_type='LVM' AND host.id=storage_pool_host_ref.host_id AND host.removed IS NULL group by pool_id having count(*) > 1" );
86- ResultSet rs = pstmt .executeQuery ();
87-
88- boolean noDuplicate = true ;
89- StringBuffer helpInfo = new StringBuffer ();
90- String note =
91- "DATABASE INTEGRITY ERROR\n Management server detected there are some hosts connect to the same loacal storage, please contact CloudStack support team for solution. Below are detialed info, please attach all of them to CloudStack support. Thank you\n " ;
92- helpInfo .append (note );
93- while (rs .next ()) {
94- long poolId = rs .getLong (1 );
95- pstmt =
96- conn .prepareStatement ("select id, status, removed, private_ip_address from host where id in (select host_id from storage_pool_host_ref where pool_id=?)" );
97- pstmt .setLong (1 , poolId );
98- ResultSet dhrs = pstmt .executeQuery ();
99- String help = formatDuplicateHostToReadText (poolId , dhrs );
100- helpInfo .append (help );
101- helpInfo .append ("\n " );
102- noDuplicate = false ;
137+ if (txn != null ) {
138+ txn .close ();
103139 }
104-
105- if (noDuplicate ) {
106- s_logger .debug ("No duplicate hosts with the same local storage found in database" );
107- } else {
108- s_logger .error (helpInfo .toString ());
109- }
110-
111- return noDuplicate ;
112- } catch (SQLException e ) {
113- s_logger .error ("Unable to check duplicate hosts with the same local storage in database" , e );
114- throw new CloudRuntimeException ("Unable to check duplicate hosts with the same local storage in database" , e );
140+ }catch (Exception e )
141+ {
142+ s_logger .error ("checkDuplicateHostWithTheSameLocalStorage: Exception:" + e .getMessage ());
115143 }
116- } finally {
117- txn .commit ();
118- txn .close ();
119144 }
120145 }
121146
122147 private boolean check21to22PremiumUprage (Connection conn ) throws SQLException {
123- PreparedStatement pstmt = conn .prepareStatement ("show tables in cloud_usage" );
124- ResultSet rs = pstmt .executeQuery ();
125- int num = 0 ;
126-
127- while ( rs .next ()) {
128- String tableName = rs . getString ( 1 );
129- if ( tableName . equalsIgnoreCase ( "usage_event" ) || tableName . equalsIgnoreCase ( "usage_port_forwarding" ) || tableName . equalsIgnoreCase ( "usage_network_offering" )) {
130- num ++ ;
131- s_logger . debug ( "Checking 21to22PremiumUprage table " + tableName + " found" );
132- }
133- if ( num == 3 ) {
134- return true ;
148+ try ( PreparedStatement pstmt = conn .prepareStatement ("show tables in cloud_usage" );
149+ ResultSet rs = pstmt .executeQuery ();) {
150+ int num = 0 ;
151+ while ( rs . next ()) {
152+ String tableName = rs .getString ( 1 );
153+ if ( tableName . equalsIgnoreCase ( "usage_event" ) || tableName . equalsIgnoreCase ( "usage_port_forwarding" ) || tableName . equalsIgnoreCase ( "usage_network_offering" )) {
154+ num ++;
155+ s_logger . debug ( "Checking 21to22PremiumUprage table " + tableName + " found" ) ;
156+ }
157+ if ( num == 3 ) {
158+ return true ;
159+ }
135160 }
161+ return false ;
136162 }
137-
138- return false ;
139163 }
140164
141165 private boolean isColumnExisted (Connection conn , String dbName , String tableName , String column ) throws SQLException {
142- PreparedStatement pstmt = conn .prepareStatement (String .format ("describe %1$s.%2$s" , dbName , tableName ));
143- ResultSet rs = pstmt .executeQuery ();
144- boolean found = false ;
145- while (rs .next ()) {
146- if (column .equalsIgnoreCase (rs .getString (1 ))) {
147- s_logger .debug (String .format ("Column %1$s.%2$s.%3$s found" , dbName , tableName , column ));
148- found = true ;
149- break ;
166+ try (PreparedStatement pstmt = conn .prepareStatement (String .format ("describe %1$s.%2$s" , dbName , tableName ));
167+ ResultSet rs = pstmt .executeQuery ();) {
168+ boolean found = false ;
169+ while (rs .next ()) {
170+ if (column .equalsIgnoreCase (rs .getString (1 ))) {
171+ s_logger .debug (String .format ("Column %1$s.%2$s.%3$s found" , dbName , tableName , column ));
172+ found = true ;
173+ break ;
174+ }
150175 }
176+ return found ;
151177 }
152- return found ;
153178 }
154179
155180 private boolean check221to222PremiumUprage (Connection conn ) throws SQLException {
@@ -174,22 +199,23 @@ private boolean check222to224PremiumUpgrade(Connection conn) throws SQLException
174199
175200 private boolean checkMissedPremiumUpgradeFor228 () {
176201 TransactionLegacy txn = TransactionLegacy .open ("Integrity" );
177- txn .start ();
178202 try {
179- String dbVersion = _dao .getCurrentVersion ();
180-
181- if (dbVersion == null )
182- return false ;
203+ txn .start ();
204+ Connection conn = txn .getConnection ();
205+ try (
206+ PreparedStatement pstmt = conn .prepareStatement ("show databases" );
207+ ResultSet rs = pstmt .executeQuery ();) {
208+ String dbVersion = _dao .getCurrentVersion ();
183209
184- if (Version .compare (Version .trimToPatch (dbVersion ), Version .trimToPatch ("2.2.8" )) != 0 ) {
185- return true ;
186- }
210+ if (dbVersion == null ) {
211+ txn .commit ();
212+ return false ;
213+ }
187214
188- Connection conn ;
189- try {
190- conn = txn .getConnection ();
191- PreparedStatement pstmt = conn .prepareStatement ("show databases" );
192- ResultSet rs = pstmt .executeQuery ();
215+ if (Version .compare (Version .trimToPatch (dbVersion ), Version .trimToPatch ("2.2.8" )) != 0 ) {
216+ txn .commit ();
217+ return true ;
218+ }
193219 boolean hasUsage = false ;
194220 while (rs .next ()) {
195221 String dbName = rs .getString (1 );
@@ -198,35 +224,46 @@ private boolean checkMissedPremiumUpgradeFor228() {
198224 break ;
199225 }
200226 }
201-
202227 if (!hasUsage ) {
203228 s_logger .debug ("No cloud_usage found in database, no need to check missed premium upgrade" );
229+ txn .commit ();
204230 return true ;
205231 }
206-
207232 if (!check21to22PremiumUprage (conn )) {
208233 s_logger .error ("21to22 premium upgrade missed" );
234+ txn .commit ();
209235 return false ;
210236 }
211-
212237 if (!check221to222PremiumUprage (conn )) {
213238 s_logger .error ("221to222 premium upgrade missed" );
239+ txn .commit ();
214240 return false ;
215241 }
216-
217242 if (!check222to224PremiumUpgrade (conn )) {
218243 s_logger .error ("222to224 premium upgrade missed" );
244+ txn .commit ();
219245 return false ;
220246 }
221-
247+ txn . commit ();
222248 return true ;
223- } catch (SQLException e ) {
224- s_logger .error ("Unable to check missed premiumg upgrade" );
225- throw new CloudRuntimeException ("Unable to check missed premiumg upgrade" );
249+ } catch (Exception e ) {
250+ s_logger .error ("checkMissedPremiumUpgradeFor228: Exception:" + e .getMessage ());
251+ throw new CloudRuntimeException ("checkMissedPremiumUpgradeFor228: Exception:" + e .getMessage (), e );
252+ }
253+ }catch (Exception e ) {
254+ s_logger .error ("checkMissedPremiumUpgradeFor228: Exception:" + e .getMessage ());
255+ throw new CloudRuntimeException ("checkMissedPremiumUpgradeFor228: Exception:" + e .getMessage (),e );
256+ }
257+ finally
258+ {
259+ try {
260+ if (txn != null ) {
261+ txn .close ();
262+ }
263+ }catch (Exception e )
264+ {
265+ s_logger .error ("checkMissedPremiumUpgradeFor228: Exception:" + e .getMessage ());
226266 }
227- } finally {
228- txn .commit ();
229- txn .close ();
230267 }
231268 }
232269
0 commit comments