Skip to content

Commit 64ff67d

Browse files
committed
Merge pull request #654 from DaanHoogland/CLOUDSTACK-8656
Cloudstack 8656: do away with more silently ignoring exceptions.a lot of messages added. some restructuring for test exception assertions and try-with-resource blocks * pr/654: (29 commits) CLOUDSTACK-8656: more logging instead of sysout CLOUDSTACK-8656: use catch block for validation CLOUDSTACK-8656: class in json specified not found CLOUDSTACK-8656: removed unused classes CLOUDSTACK-8656: restructure of tests CLOUDSTACK-8656: reorganise sychronized block CLOUDSTACK-8656: restructure tests to ensure exception throwing CLOUDSTACK-8656: validate the throwing of ServerApiException CLOUDSTACK-8656: logging ignored exceptions CLOUDSTACK-8656: try-w-r removes need for empty catch block CLOUDSTACK-8656: try-w-r instead of clunckey close-except CLOUDSTACK-8656: deal with empty SQLException catch block by try-w-r CLOUDSTACK-8656: unnecessary close construct removed CLOUDSTACK-8656: message about timed buffer logging CLOUDSTACK-8656: message about invalid number from store CLOUDSTACK-8656: move cli test tool to separate file CLOUDSTACK-8656: exception is the rule for some tests CLOUDSTACK-8656: network related exception logging CLOUDSTACK-8656: reporting ignored exceptions in server CLOUDSTACK-8656: log in case we are on a platform not supporting UTF8 ... Signed-off-by: Remi Bergsma <github@remi.nl>
2 parents 05a29f0 + b6f1d29 commit 64ff67d

44 files changed

Lines changed: 473 additions & 802 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

engine/schema/src/com/cloud/upgrade/dao/Upgrade40to41.java

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -82,23 +82,14 @@ private void updateRegionEntries(Connection conn) {
8282
if (regionId != null) {
8383
region_id = Integer.parseInt(regionId);
8484
}
85-
PreparedStatement pstmt = null;
86-
try {
85+
try (PreparedStatement pstmt = conn.prepareStatement("update `cloud`.`region` set id = ?");) {
8786
//Update regionId in region table
8887
s_logger.debug("Updating region table with Id: " + region_id);
89-
pstmt = conn.prepareStatement("update `cloud`.`region` set id = ?");
9088
pstmt.setInt(1, region_id);
9189
pstmt.executeUpdate();
9290

9391
} catch (SQLException e) {
9492
throw new CloudRuntimeException("Error while updating region entries", e);
95-
} finally {
96-
try {
97-
if (pstmt != null) {
98-
pstmt.close();
99-
}
100-
} catch (SQLException e) {
101-
}
10293
}
10394
}
10495

engine/schema/src/com/cloud/upgrade/dao/Upgrade410to420.java

Lines changed: 79 additions & 207 deletions
Large diffs are not rendered by default.

engine/schema/src/com/cloud/upgrade/dao/Upgrade420to421.java

Lines changed: 76 additions & 99 deletions
Original file line numberDiff line numberDiff line change
@@ -131,137 +131,114 @@ private void updateOverprovisioningPerVm(Connection conn) {
131131

132132
private void upgradeResourceCount(Connection conn) {
133133
s_logger.debug("upgradeResourceCount start");
134-
PreparedStatement pstmt1 = null;
135-
PreparedStatement pstmt2 = null;
136-
PreparedStatement pstmt3 = null;
137-
PreparedStatement pstmt4 = null;
138-
PreparedStatement pstmt5 = null;
139-
ResultSet rsAccount = null;
140-
ResultSet rsCount = null;
141-
try {
142-
pstmt1 = conn.prepareStatement("select id, domain_id FROM `cloud`.`account` where removed is NULL ");
143-
rsAccount = pstmt1.executeQuery();
134+
String sqlSelectAccountIds = "select id, domain_id FROM `cloud`.`account` where removed is NULL ";
135+
String sqlSelectOfferingTotals = "SELECT SUM(service_offering.cpu), SUM(service_offering.ram_size)"
136+
+ " FROM `cloud`.`vm_instance`, `cloud`.`service_offering`"
137+
+ " WHERE vm_instance.service_offering_id = service_offering.id AND vm_instance.account_id = ?"
138+
+ " AND vm_instance.removed is NULL"
139+
+ " AND vm_instance.vm_type='User' AND state not in ('Destroyed', 'Error', 'Expunging')";
140+
String sqlSelectTotalVolumeSize =
141+
"SELECT sum(size) FROM `cloud`.`volumes` WHERE account_id= ?"
142+
+ " AND (path is not NULL OR state in ('Allocated')) AND removed is NULL"
143+
+ " AND instance_id IN (SELECT id FROM `cloud`.`vm_instance` WHERE vm_type='User')";
144+
String sqlSelectTotalPathlessVolumeSize =
145+
"SELECT sum(size) FROM `cloud`.`volumes` WHERE account_id= ?"
146+
+ " AND path is NULL AND state not in ('Allocated') AND removed is NULL";
147+
String sqlSelectTotalSnapshotSize = "SELECT sum(size) FROM `cloud`.`snapshots` WHERE account_id= ? AND removed is NULL";
148+
String sqlSelectTotalTemplateStoreSize = "SELECT sum(template_store_ref.size) FROM `cloud`.`template_store_ref`,`cloud`.`vm_template` WHERE account_id = ?"
149+
+ " AND template_store_ref.template_id = vm_template.id AND download_state = 'DOWNLOADED' AND destroyed = false AND removed is NULL";
150+
String sqlSelectDomainIds = "select id FROM `cloud`.`domain`";
151+
String sqlSelectAccountCount = "select account.domain_id,sum(resource_count.count) from `cloud`.`account` left join `cloud`.`resource_count` on account.id=resource_count.account_id "
152+
+ "where resource_count.type=? group by account.domain_id;";
153+
154+
try (
155+
PreparedStatement pstmtSelectAccountIds = conn.prepareStatement(sqlSelectAccountIds);
156+
PreparedStatement pstmtSelectOfferingTotals = conn.prepareStatement(sqlSelectOfferingTotals);
157+
PreparedStatement pstmtSelectTotalVolumeSize = conn.prepareStatement(sqlSelectTotalVolumeSize);
158+
PreparedStatement pstmtSelectTotalPathlessVolumeSize = conn.prepareStatement(sqlSelectTotalPathlessVolumeSize);
159+
PreparedStatement pstmtSelectTotalSnapshotSize = conn.prepareStatement(sqlSelectTotalSnapshotSize);
160+
PreparedStatement pstmtSelectTotalTemplateStoreSize = conn.prepareStatement(sqlSelectTotalTemplateStoreSize);
161+
PreparedStatement pstmtSelectDomainIds = conn.prepareStatement(sqlSelectDomainIds);
162+
PreparedStatement pstmtSelectAccountCount = conn.prepareStatement(sqlSelectAccountCount);
163+
ResultSet rsAccount = pstmtSelectAccountIds.executeQuery();
164+
) {
144165
while (rsAccount.next()) {
145166
long account_id = rsAccount.getLong(1);
146167
long domain_id = rsAccount.getLong(2);
147168
// 1. update cpu,memory for all accounts
148-
pstmt2 =
149-
conn.prepareStatement("SELECT SUM(service_offering.cpu), SUM(service_offering.ram_size)" + " FROM `cloud`.`vm_instance`, `cloud`.`service_offering`"
150-
+ " WHERE vm_instance.service_offering_id = service_offering.id AND vm_instance.account_id = ?" + " AND vm_instance.removed is NULL"
151-
+ " AND vm_instance.vm_type='User' AND state not in ('Destroyed', 'Error', 'Expunging')");
152-
pstmt2.setLong(1, account_id);
153-
rsCount = pstmt2.executeQuery();
154-
if (rsCount.next()) {
155-
upgradeResourceCountforAccount(conn, account_id, domain_id, "cpu", rsCount.getLong(1));
156-
upgradeResourceCountforAccount(conn, account_id, domain_id, "memory", rsCount.getLong(2));
157-
} else {
158-
upgradeResourceCountforAccount(conn, account_id, domain_id, "cpu", 0L);
159-
upgradeResourceCountforAccount(conn, account_id, domain_id, "memory", 0L);
169+
pstmtSelectOfferingTotals.setLong(1, account_id);
170+
try (ResultSet rsOfferingTotals = pstmtSelectOfferingTotals.executeQuery();) {
171+
if (rsOfferingTotals.next()) {
172+
upgradeResourceCountforAccount(conn, account_id, domain_id, "cpu", rsOfferingTotals.getLong(1));
173+
upgradeResourceCountforAccount(conn, account_id, domain_id, "memory", rsOfferingTotals.getLong(2));
174+
} else {
175+
upgradeResourceCountforAccount(conn, account_id, domain_id, "cpu", 0L);
176+
upgradeResourceCountforAccount(conn, account_id, domain_id, "memory", 0L);
177+
}
160178
}
161-
rsCount.close();
162179

163180
// 2. update primary_storage for all accounts
164-
pstmt3 =
165-
conn.prepareStatement("SELECT sum(size) FROM `cloud`.`volumes` WHERE account_id= ?"
166-
+ " AND (path is not NULL OR state in ('Allocated')) AND removed is NULL"
167-
+ " AND instance_id IN (SELECT id FROM `cloud`.`vm_instance` WHERE vm_type='User')");
168-
pstmt3.setLong(1, account_id);
169-
rsCount = pstmt3.executeQuery();
170-
if (rsCount.next()) {
171-
upgradeResourceCountforAccount(conn, account_id, domain_id, "primary_storage", rsCount.getLong(1));
172-
} else {
173-
upgradeResourceCountforAccount(conn, account_id, domain_id, "primary_storage", 0L);
181+
pstmtSelectTotalVolumeSize.setLong(1, account_id);
182+
try (ResultSet rsTotalVolumeSize = pstmtSelectTotalVolumeSize.executeQuery();) {
183+
if (rsTotalVolumeSize.next()) {
184+
upgradeResourceCountforAccount(conn, account_id, domain_id, "primary_storage", rsTotalVolumeSize.getLong(1));
185+
} else {
186+
upgradeResourceCountforAccount(conn, account_id, domain_id, "primary_storage", 0L);
187+
}
174188
}
175-
rsCount.close();
176189

177190
// 3. update secondary_storage for all accounts
178191
long totalVolumesSize = 0;
179192
long totalSnapshotsSize = 0;
180193
long totalTemplatesSize = 0;
181-
pstmt4 =
182-
conn.prepareStatement("SELECT sum(size) FROM `cloud`.`volumes` WHERE account_id= ?"
183-
+ " AND path is NULL AND state not in ('Allocated') AND removed is NULL");
184-
pstmt4.setLong(1, account_id);
185-
rsCount = pstmt4.executeQuery();
186-
if (rsCount.next()) {
187-
totalVolumesSize = rsCount.getLong(1);
194+
pstmtSelectTotalPathlessVolumeSize.setLong(1, account_id);
195+
try (ResultSet rsTotalPathlessVolumeSize = pstmtSelectTotalPathlessVolumeSize.executeQuery();) {
196+
if (rsTotalPathlessVolumeSize.next()) {
197+
totalVolumesSize = rsTotalPathlessVolumeSize.getLong(1);
198+
}
188199
}
189-
rsCount.close();
190-
pstmt4.close();
191200

192-
pstmt4 = conn.prepareStatement("SELECT sum(size) FROM `cloud`.`snapshots` WHERE account_id= ? AND removed is NULL");
193-
pstmt4.setLong(1, account_id);
194-
rsCount = pstmt4.executeQuery();
195-
if (rsCount.next()) {
196-
totalSnapshotsSize = rsCount.getLong(1);
201+
pstmtSelectTotalSnapshotSize.setLong(1, account_id);
202+
try (ResultSet rsTotalSnapshotSize = pstmtSelectTotalSnapshotSize.executeQuery();) {
203+
if (rsTotalSnapshotSize.next()) {
204+
totalSnapshotsSize = rsTotalSnapshotSize.getLong(1);
205+
}
197206
}
198-
rsCount.close();
199-
pstmt4.close();
200-
201-
pstmt4 =
202-
conn.prepareStatement("SELECT sum(template_store_ref.size) FROM `cloud`.`template_store_ref`,`cloud`.`vm_template` WHERE account_id = ?"
203-
+ " AND template_store_ref.template_id = vm_template.id AND download_state = 'DOWNLOADED' AND destroyed = false AND removed is NULL");
204-
pstmt4.setLong(1, account_id);
205-
rsCount = pstmt4.executeQuery();
206-
if (rsCount.next()) {
207-
totalTemplatesSize = rsCount.getLong(1);
207+
pstmtSelectTotalTemplateStoreSize.setLong(1, account_id);
208+
try (ResultSet rsTotalTemplateStoreSize = pstmtSelectTotalTemplateStoreSize.executeQuery();) {
209+
if (rsTotalTemplateStoreSize.next()) {
210+
totalTemplatesSize = rsTotalTemplateStoreSize.getLong(1);
211+
}
208212
}
209213
upgradeResourceCountforAccount(conn, account_id, domain_id, "secondary_storage", totalVolumesSize + totalSnapshotsSize + totalTemplatesSize);
210214
}
211215
rsAccount.close();
212216

213217
// 4. upgrade cpu,memory,primary_storage,secondary_storage for domains
214218
String resource_types[] = {"cpu", "memory", "primary_storage", "secondary_storage"};
215-
pstmt5 = conn.prepareStatement("select id FROM `cloud`.`domain`");
216-
rsAccount = pstmt5.executeQuery();
217-
while (rsAccount.next()) {
218-
long domain_id = rsAccount.getLong(1);
219-
for (int count = 0; count < resource_types.length; count++) {
220-
String resource_type = resource_types[count];
221-
upgradeResourceCountforDomain(conn, domain_id, resource_type, 0L); // reset value to 0 before statistics
219+
try (ResultSet rsDomainIds = pstmtSelectDomainIds.executeQuery();) {
220+
while (rsDomainIds.next()) {
221+
long domain_id = rsDomainIds.getLong(1);
222+
for (int count = 0; count < resource_types.length; count++) {
223+
String resource_type = resource_types[count];
224+
upgradeResourceCountforDomain(conn, domain_id, resource_type, 0L); // reset value to 0 before statistics
225+
}
222226
}
223227
}
224228
for (int count = 0; count < resource_types.length; count++) {
225229
String resource_type = resource_types[count];
226-
pstmt5 =
227-
conn.prepareStatement("select account.domain_id,sum(resource_count.count) from `cloud`.`account` left join `cloud`.`resource_count` on account.id=resource_count.account_id "
228-
+ "where resource_count.type=? group by account.domain_id;");
229-
pstmt5.setString(1, resource_type);
230-
rsCount = pstmt5.executeQuery();
231-
while (rsCount.next()) {
232-
long domain_id = rsCount.getLong(1);
233-
long resource_count = rsCount.getLong(2);
234-
upgradeResourceCountforDomain(conn, domain_id, resource_type, resource_count);
230+
pstmtSelectAccountCount.setString(1, resource_type);
231+
try (ResultSet rsAccountCount = pstmtSelectAccountCount.executeQuery();) {
232+
while (rsAccountCount.next()) {
233+
long domain_id = rsAccountCount.getLong(1);
234+
long resource_count = rsAccountCount.getLong(2);
235+
upgradeResourceCountforDomain(conn, domain_id, resource_type, resource_count);
236+
}
235237
}
236238
}
237239
s_logger.debug("upgradeResourceCount finish");
238240
} catch (SQLException e) {
239241
throw new CloudRuntimeException("Unable to upgrade resource count (cpu,memory,primary_storage,secondary_storage) ", e);
240-
} finally {
241-
try {
242-
if (rsAccount != null) {
243-
rsAccount.close();
244-
}
245-
if (rsCount != null) {
246-
rsCount.close();
247-
}
248-
if (pstmt1 != null) {
249-
pstmt1.close();
250-
}
251-
if (pstmt2 != null) {
252-
pstmt2.close();
253-
}
254-
if (pstmt3 != null) {
255-
pstmt3.close();
256-
}
257-
if (pstmt4 != null) {
258-
pstmt4.close();
259-
}
260-
if (pstmt5 != null) {
261-
pstmt5.close();
262-
}
263-
} catch (SQLException e) {
264-
}
265242
}
266243
}
267244

engine/schema/src/com/cloud/upgrade/dao/Upgrade442to450.java

Lines changed: 8 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232

3333
import com.cloud.hypervisor.Hypervisor;
3434
import com.cloud.utils.crypt.DBEncryptionUtil;
35+
3536
import org.apache.log4j.Logger;
3637

3738
import com.cloud.utils.exception.CloudRuntimeException;
@@ -76,31 +77,20 @@ public void performDataMigration(Connection conn) {
7677
}
7778

7879
private void updateMaxRouterSizeConfig(Connection conn) {
79-
PreparedStatement updatePstmt = null;
80-
try {
80+
String sqlUpdateConfig = "UPDATE `cloud`.`configuration` SET value=? WHERE name='router.ram.size' AND category='Hidden'";
81+
try (PreparedStatement updatePstmt = conn.prepareStatement(sqlUpdateConfig);){
8182
String encryptedValue = DBEncryptionUtil.encrypt("256");
82-
updatePstmt = conn.prepareStatement("UPDATE `cloud`.`configuration` SET value=? WHERE name='router.ram.size' AND category='Hidden'");
8383
updatePstmt.setBytes(1, encryptedValue.getBytes("UTF-8"));
8484
updatePstmt.executeUpdate();
8585
} catch (SQLException e) {
8686
throw new CloudRuntimeException("Unable to upgrade max ram size of router in config.", e);
8787
} catch (UnsupportedEncodingException e) {
8888
throw new CloudRuntimeException("Unable encrypt configuration values ", e);
89-
} finally {
90-
try {
91-
if (updatePstmt != null) {
92-
updatePstmt.close();
93-
}
94-
} catch (SQLException e) {
95-
}
9689
}
9790
s_logger.debug("Done updating router.ram.size config to 256");
9891
}
9992

10093
private void upgradeMemoryOfVirtualRoutervmOffering(Connection conn) {
101-
PreparedStatement updatePstmt = null;
102-
PreparedStatement selectPstmt = null;
103-
ResultSet selectResultSet = null;
10494
int newRamSize = 256; //256MB
10595
long serviceOfferingId = 0;
10696

@@ -109,10 +99,11 @@ private void upgradeMemoryOfVirtualRoutervmOffering(Connection conn) {
10999
* We should not update/modify any user-defined offering.
110100
*/
111101

112-
try {
113-
selectPstmt = conn.prepareStatement("SELECT id FROM `cloud`.`service_offering` WHERE vm_type='domainrouter'");
114-
updatePstmt = conn.prepareStatement("UPDATE `cloud`.`service_offering` SET ram_size=? WHERE id=?");
115-
selectResultSet = selectPstmt.executeQuery();
102+
try (
103+
PreparedStatement selectPstmt = conn.prepareStatement("SELECT id FROM `cloud`.`service_offering` WHERE vm_type='domainrouter'");
104+
PreparedStatement updatePstmt = conn.prepareStatement("UPDATE `cloud`.`service_offering` SET ram_size=? WHERE id=?");
105+
ResultSet selectResultSet = selectPstmt.executeQuery();
106+
) {
116107
if(selectResultSet.next()) {
117108
serviceOfferingId = selectResultSet.getLong("id");
118109
}
@@ -122,19 +113,6 @@ private void upgradeMemoryOfVirtualRoutervmOffering(Connection conn) {
122113
updatePstmt.executeUpdate();
123114
} catch (SQLException e) {
124115
throw new CloudRuntimeException("Unable to upgrade ram_size of service offering for domain router. ", e);
125-
} finally {
126-
try {
127-
if (selectPstmt != null) {
128-
selectPstmt.close();
129-
}
130-
if (selectResultSet != null) {
131-
selectResultSet.close();
132-
}
133-
if (updatePstmt != null) {
134-
updatePstmt.close();
135-
}
136-
} catch (SQLException e) {
137-
}
138116
}
139117
s_logger.debug("Done upgrading RAM for service offering of domain router to " + newRamSize);
140118
}

framework/cluster/src/com/cloud/cluster/ClusterServiceServletImpl.java

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -134,15 +134,4 @@ private HttpClient getHttpClient() {
134134
return s_client;
135135
}
136136

137-
// for test purpose only
138-
public static void main(final String[] args) {
139-
/*
140-
ClusterServiceServletImpl service = new ClusterServiceServletImpl("http://localhost:9090/clusterservice", 300);
141-
try {
142-
String result = service.execute("test", 1, "{ p1:v1, p2:v2 }", true);
143-
System.out.println(result);
144-
} catch (RemoteException e) {
145-
}
146-
*/
147-
}
148137
}

framework/db/src/com/cloud/utils/db/ConnectionConcierge.java

Lines changed: 7 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -142,26 +142,17 @@ public void unregister(String name) {
142142
}
143143

144144
protected String testValidity(String name, Connection conn) {
145-
PreparedStatement pstmt = null;
146-
try {
147-
if (conn != null) {
148-
synchronized (conn) {
149-
pstmt = conn.prepareStatement("SELECT 1");
145+
if (conn != null) {
146+
synchronized (conn) {
147+
try (PreparedStatement pstmt = conn.prepareStatement("SELECT 1");) {
150148
pstmt.executeQuery();
151-
}
152-
}
153-
return null;
154-
} catch (Throwable th) {
155-
s_logger.error("Unable to keep the db connection for " + name, th);
156-
return th.toString();
157-
} finally {
158-
if (pstmt != null) {
159-
try {
160-
pstmt.close();
161-
} catch (SQLException e) {
149+
} catch (Throwable th) {
150+
s_logger.error("Unable to keep the db connection for " + name, th);
151+
return th.toString();
162152
}
163153
}
164154
}
155+
return null;
165156
}
166157

167158
@Override

0 commit comments

Comments
 (0)