Skip to content

Commit 4ba3aae

Browse files
sedukullDaanHoogland
authored andcommitted
Fixed coverity issues
1 parent e9f60ee commit 4ba3aae

10 files changed

Lines changed: 291 additions & 326 deletions

File tree

‎engine/schema/src/com/cloud/upgrade/DatabaseCreator.java‎

Lines changed: 20 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -20,14 +20,13 @@
2020

2121
import java.io.File;
2222
import java.io.FileInputStream;
23-
import java.io.FileNotFoundException;
2423
import java.io.FileReader;
2524
import java.io.IOException;
2625
import java.io.Reader;
27-
import java.sql.Connection;
28-
import java.sql.DriverManager;
29-
import java.sql.SQLException;
3026
import java.sql.Statement;
27+
import java.sql.SQLException;
28+
import java.sql.DriverManager;
29+
import java.sql.Connection;
3130
import java.util.ArrayList;
3231
import java.util.List;
3332
import java.util.Properties;
@@ -75,13 +74,10 @@ private static void runScript(Connection conn, Reader reader, String filename, b
7574

7675
private static void runQuery(String host, String port, String rootPassword, String query, boolean dryRun) {
7776
System.out.println("============> Running query: " + query);
78-
Connection conn = null;
79-
try {
80-
conn = DriverManager.getConnection(String.format("jdbc:mysql://%s:%s/", host, port), "root", rootPassword);
81-
Statement stmt = conn.createStatement();
82-
if (!dryRun)
77+
try (Connection conn = DriverManager.getConnection(String.format("jdbc:mysql://%s:%s/", host, port), "root", rootPassword);
78+
Statement stmt = conn.createStatement();){
79+
if (!dryRun)
8380
stmt.executeUpdate(query);
84-
conn.close();
8581
} catch (SQLException e) {
8682
System.out.println("SQL exception in trying initDB: " + e);
8783
System.exit(1);
@@ -186,24 +182,23 @@ public static void main(String[] args) {
186182
}
187183

188184
System.out.println("========> Processing SQL file at " + sqlScript.getAbsolutePath());
189-
Connection conn = TransactionLegacy.getStandaloneConnection();
190-
try {
191-
FileReader reader = null;
192-
try {
193-
reader = new FileReader(sqlScript);
194-
} catch (FileNotFoundException e) {
195-
System.err.println("Unable to read " + sqlFile + ": " + e.getMessage());
196-
System.exit(1);
197-
}
185+
186+
try(Connection conn = TransactionLegacy.getStandaloneConnection();
187+
FileReader reader = new FileReader(sqlScript);
188+
) {
198189
if (!dryRun)
199190
runScript(conn, reader, sqlFile, verbosity);
200-
} finally {
201-
try {
202-
conn.close();
203-
} catch (SQLException e) {
204-
System.err.println("Unable to close DB connection: " + e.getMessage());
205-
}
191+
}catch (SQLException e)
192+
{
193+
System.err.println("Sql Exception:" + e.getMessage());
194+
System.exit(1);
195+
}
196+
catch (IOException e)
197+
{
198+
System.err.println("File IO Exception : " + e.getMessage());
199+
System.exit(1);
206200
}
201+
207202
}
208203

209204
TransactionLegacy txn = TransactionLegacy.open(TransactionLegacy.CLOUD_DB);

‎engine/schema/src/com/cloud/upgrade/DatabaseIntegrityChecker.java‎

Lines changed: 120 additions & 83 deletions
Original file line numberDiff line numberDiff line change
@@ -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\nManagement 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\nManagement 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

‎engine/schema/src/com/cloud/upgrade/DatabaseUpgradeChecker.java‎

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -237,8 +237,8 @@ public DatabaseUpgradeChecker() {
237237
}
238238

239239
protected void runScript(Connection conn, File file) {
240-
try {
241-
FileReader reader = new FileReader(file);
240+
241+
try(FileReader reader = new FileReader(file);) {
242242
ScriptRunner runner = new ScriptRunner(conn, false, true);
243243
runner.runScript(reader);
244244
} catch (FileNotFoundException e) {
@@ -251,6 +251,7 @@ protected void runScript(Connection conn, File file) {
251251
s_logger.error("Unable to execute upgrade script: " + file.getAbsolutePath(), e);
252252
throw new CloudRuntimeException("Unable to execute upgrade script: " + file.getAbsolutePath(), e);
253253
}
254+
254255
}
255256

256257
protected void upgrade(String dbVersion, String currentVersion) {
@@ -314,10 +315,9 @@ protected void upgrade(String dbVersion, String currentVersion) {
314315
// we don't have VersionDao in 2.1.x
315316
upgradeVersion = false;
316317
} else if (upgrade.getUpgradedVersion().equals("2.2.4")) {
317-
try {
318+
try(PreparedStatement pstmt = conn.prepareStatement("SELECT * FROM version WHERE version='2.2.4'");
319+
ResultSet rs = pstmt.executeQuery();) {
318320
// specifically for domain vlan update from 2.1.8 to 2.2.4
319-
PreparedStatement pstmt = conn.prepareStatement("SELECT * FROM version WHERE version='2.2.4'");
320-
ResultSet rs = pstmt.executeQuery();
321321
if (rs.next()) {
322322
upgradeVersion = false;
323323
}

0 commit comments

Comments
 (0)