forked from apache/cloudstack
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUsageDaoImpl.java
More file actions
454 lines (424 loc) · 20 KB
/
Copy pathUsageDaoImpl.java
File metadata and controls
454 lines (424 loc) · 20 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
// Licensed to the Apache Software Foundation (ASF) under one
// or more contributor license agreements. See the NOTICE file
// distributed with this work for additional information
// regarding copyright ownership. The ASF licenses this file
// to you under the Apache License, Version 2.0 (the
// "License"); you may not use this file except in compliance
// with the License. You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied. See the License for the
// specific language governing permissions and limitations
// under the License.
package com.cloud.usage.dao;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.Types;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.TimeZone;
import javax.ejb.Local;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Component;
import com.cloud.usage.UsageVO;
import com.cloud.user.AccountVO;
import com.cloud.user.UserStatisticsVO;
import com.cloud.user.VmDiskStatisticsVO;
import com.cloud.utils.DateUtil;
import com.cloud.utils.Pair;
import com.cloud.utils.db.Filter;
import com.cloud.utils.db.GenericDaoBase;
import com.cloud.utils.db.SearchCriteria;
import com.cloud.utils.db.TransactionLegacy;
import com.cloud.utils.exception.CloudRuntimeException;
@Component
@Local(value = {UsageDao.class})
public class UsageDaoImpl extends GenericDaoBase<UsageVO, Long> implements UsageDao {
public static final Logger s_logger = Logger.getLogger(UsageDaoImpl.class.getName());
private static final String DELETE_ALL = "DELETE FROM cloud_usage";
private static final String DELETE_ALL_BY_ACCOUNTID = "DELETE FROM cloud_usage WHERE account_id = ?";
private static final String INSERT_ACCOUNT = "INSERT INTO cloud_usage.account (id, account_name, type, domain_id, removed, cleanup_needed) VALUES (?,?,?,?,?,?)";
private static final String INSERT_USER_STATS =
"INSERT INTO cloud_usage.user_statistics (id, data_center_id, account_id, public_ip_address, device_id, device_type, network_id, net_bytes_received,"
+ " net_bytes_sent, current_bytes_received, current_bytes_sent, agg_bytes_received, agg_bytes_sent) VALUES (?,?,?,?,?,?,?,?,?,?, ?, ?, ?)";
private static final String UPDATE_ACCOUNT = "UPDATE cloud_usage.account SET account_name=?, removed=? WHERE id=?";
private static final String UPDATE_USER_STATS =
"UPDATE cloud_usage.user_statistics SET net_bytes_received=?, net_bytes_sent=?, current_bytes_received=?, current_bytes_sent=?, agg_bytes_received=?, agg_bytes_sent=? WHERE id=?";
private static final String GET_LAST_ACCOUNT = "SELECT id FROM cloud_usage.account ORDER BY id DESC LIMIT 1";
private static final String GET_LAST_USER_STATS = "SELECT id FROM cloud_usage.user_statistics ORDER BY id DESC LIMIT 1";
private static final String GET_PUBLIC_TEMPLATES_BY_ACCOUNTID = "SELECT id FROM cloud.vm_template WHERE account_id = ? AND public = '1' AND removed IS NULL";
private static final String GET_LAST_VM_DISK_STATS = "SELECT id FROM cloud_usage.vm_disk_statistics ORDER BY id DESC LIMIT 1";
private static final String INSERT_VM_DISK_STATS =
"INSERT INTO cloud_usage.vm_disk_statistics (id, data_center_id, account_id, vm_id, volume_id, net_io_read, net_io_write, current_io_read, "
+ "current_io_write, agg_io_read, agg_io_write, net_bytes_read, net_bytes_write, current_bytes_read, current_bytes_write, agg_bytes_read, agg_bytes_write) "
+ " VALUES (?,?,?,?,?,?,?,?,?,?, ?, ?, ?, ?,?, ?, ?)";
private static final String UPDATE_VM_DISK_STATS =
"UPDATE cloud_usage.vm_disk_statistics SET net_io_read=?, net_io_write=?, current_io_read=?, current_io_write=?, agg_io_read=?, agg_io_write=?, "
+ "net_bytes_read=?, net_bytes_write=?, current_bytes_read=?, current_bytes_write=?, agg_bytes_read=?, agg_bytes_write=? WHERE id=?";
private static final String INSERT_USAGE_RECORDS = "INSERT INTO cloud_usage.cloud_usage (zone_id, account_id, domain_id, description, usage_display, "
+
"usage_type, raw_usage, vm_instance_id, vm_name, offering_id, template_id, "
+ "usage_id, type, size, network_id, start_date, end_date, virtual_size) VALUES (?,?,?,?,?,?,?,?,?, ?, ?, ?,?,?,?,?,?,?)";
protected final static TimeZone s_gmtTimeZone = TimeZone.getTimeZone("GMT");
public UsageDaoImpl() {
}
@Override
public void deleteRecordsForAccount(Long accountId) {
String sql = ((accountId == null) ? DELETE_ALL : DELETE_ALL_BY_ACCOUNTID);
TransactionLegacy txn = TransactionLegacy.open(TransactionLegacy.USAGE_DB);
PreparedStatement pstmt = null;
try {
txn.start();
pstmt = txn.prepareAutoCloseStatement(sql);
if (accountId != null) {
pstmt.setLong(1, accountId.longValue());
}
pstmt.executeUpdate();
txn.commit();
} catch (Exception ex) {
txn.rollback();
s_logger.error("error retrieving usage vm instances for account id: " + accountId);
} finally {
txn.close();
}
}
@Override
public Pair<List<UsageVO>, Integer> searchAndCountAllRecords(SearchCriteria<UsageVO> sc, Filter filter) {
return listAndCountIncludingRemovedBy(sc, filter);
}
@Override
public void saveAccounts(List<AccountVO> accounts) {
TransactionLegacy txn = TransactionLegacy.currentTxn();
try {
txn.start();
String sql = INSERT_ACCOUNT;
PreparedStatement pstmt = null;
pstmt = txn.prepareAutoCloseStatement(sql); // in reality I just want CLOUD_USAGE dataSource connection
for (AccountVO acct : accounts) {
pstmt.setLong(1, acct.getId());
pstmt.setString(2, acct.getAccountName());
pstmt.setShort(3, acct.getType());
pstmt.setLong(4, acct.getDomainId());
Date removed = acct.getRemoved();
if (removed == null) {
pstmt.setString(5, null);
} else {
pstmt.setString(5, DateUtil.getDateDisplayString(TimeZone.getTimeZone("GMT"), acct.getRemoved()));
}
pstmt.setBoolean(6, acct.getNeedsCleanup());
pstmt.addBatch();
}
pstmt.executeBatch();
txn.commit();
} catch (Exception ex) {
txn.rollback();
s_logger.error("error saving account to cloud_usage db", ex);
throw new CloudRuntimeException(ex.getMessage());
}
}
@Override
public void updateAccounts(List<AccountVO> accounts) {
TransactionLegacy txn = TransactionLegacy.currentTxn();
try {
txn.start();
String sql = UPDATE_ACCOUNT;
PreparedStatement pstmt = null;
pstmt = txn.prepareAutoCloseStatement(sql); // in reality I just want CLOUD_USAGE dataSource connection
for (AccountVO acct : accounts) {
pstmt.setString(1, acct.getAccountName());
Date removed = acct.getRemoved();
if (removed == null) {
pstmt.setString(2, null);
} else {
pstmt.setString(2, DateUtil.getDateDisplayString(TimeZone.getTimeZone("GMT"), acct.getRemoved()));
}
pstmt.setLong(3, acct.getId());
pstmt.addBatch();
}
pstmt.executeBatch();
txn.commit();
} catch (Exception ex) {
txn.rollback();
s_logger.error("error saving account to cloud_usage db", ex);
throw new CloudRuntimeException(ex.getMessage());
}
}
@Override
public void saveUserStats(List<UserStatisticsVO> userStats) {
TransactionLegacy txn = TransactionLegacy.currentTxn();
try {
txn.start();
String sql = INSERT_USER_STATS;
PreparedStatement pstmt = null;
pstmt = txn.prepareAutoCloseStatement(sql); // in reality I just want CLOUD_USAGE dataSource connection
for (UserStatisticsVO userStat : userStats) {
pstmt.setLong(1, userStat.getId());
pstmt.setLong(2, userStat.getDataCenterId());
pstmt.setLong(3, userStat.getAccountId());
pstmt.setString(4, userStat.getPublicIpAddress());
if (userStat.getDeviceId() != null) {
pstmt.setLong(5, userStat.getDeviceId());
} else {
pstmt.setNull(5, Types.BIGINT);
}
pstmt.setString(6, userStat.getDeviceType());
if (userStat.getNetworkId() != null) {
pstmt.setLong(7, userStat.getNetworkId());
} else {
pstmt.setNull(7, Types.BIGINT);
}
pstmt.setLong(8, userStat.getNetBytesReceived());
pstmt.setLong(9, userStat.getNetBytesSent());
pstmt.setLong(10, userStat.getCurrentBytesReceived());
pstmt.setLong(11, userStat.getCurrentBytesSent());
pstmt.setLong(12, userStat.getAggBytesReceived());
pstmt.setLong(13, userStat.getAggBytesSent());
pstmt.addBatch();
}
pstmt.executeBatch();
txn.commit();
} catch (Exception ex) {
txn.rollback();
s_logger.error("error saving user stats to cloud_usage db", ex);
throw new CloudRuntimeException(ex.getMessage());
}
}
@Override
public void updateUserStats(List<UserStatisticsVO> userStats) {
TransactionLegacy txn = TransactionLegacy.currentTxn();
try {
txn.start();
String sql = UPDATE_USER_STATS;
PreparedStatement pstmt = null;
pstmt = txn.prepareAutoCloseStatement(sql); // in reality I just want CLOUD_USAGE dataSource connection
for (UserStatisticsVO userStat : userStats) {
pstmt.setLong(1, userStat.getNetBytesReceived());
pstmt.setLong(2, userStat.getNetBytesSent());
pstmt.setLong(3, userStat.getCurrentBytesReceived());
pstmt.setLong(4, userStat.getCurrentBytesSent());
pstmt.setLong(5, userStat.getAggBytesReceived());
pstmt.setLong(6, userStat.getAggBytesSent());
pstmt.setLong(7, userStat.getId());
pstmt.addBatch();
}
pstmt.executeBatch();
txn.commit();
} catch (Exception ex) {
txn.rollback();
s_logger.error("error saving user stats to cloud_usage db", ex);
throw new CloudRuntimeException(ex.getMessage());
}
}
@Override
public Long getLastAccountId() {
TransactionLegacy txn = TransactionLegacy.currentTxn();
PreparedStatement pstmt = null;
String sql = GET_LAST_ACCOUNT;
try {
pstmt = txn.prepareAutoCloseStatement(sql);
ResultSet rs = pstmt.executeQuery();
if (rs.next()) {
return Long.valueOf(rs.getLong(1));
}
} catch (Exception ex) {
s_logger.error("error getting last account id", ex);
}
return null;
}
@Override
public Long getLastUserStatsId() {
TransactionLegacy txn = TransactionLegacy.currentTxn();
PreparedStatement pstmt = null;
String sql = GET_LAST_USER_STATS;
try {
pstmt = txn.prepareAutoCloseStatement(sql);
ResultSet rs = pstmt.executeQuery();
if (rs.next()) {
return Long.valueOf(rs.getLong(1));
}
} catch (Exception ex) {
s_logger.error("error getting last user stats id", ex);
}
return null;
}
@Override
public List<Long> listPublicTemplatesByAccount(long accountId) {
TransactionLegacy txn = TransactionLegacy.currentTxn();
PreparedStatement pstmt = null;
String sql = GET_PUBLIC_TEMPLATES_BY_ACCOUNTID;
List<Long> templateList = new ArrayList<Long>();
try {
pstmt = txn.prepareAutoCloseStatement(sql);
pstmt.setLong(1, accountId);
ResultSet rs = pstmt.executeQuery();
if (rs.next()) {
templateList.add(Long.valueOf(rs.getLong(1)));
}
} catch (Exception ex) {
s_logger.error("error listing public templates", ex);
}
return templateList;
}
@Override
public Long getLastVmDiskStatsId() {
TransactionLegacy txn = TransactionLegacy.currentTxn();
PreparedStatement pstmt = null;
String sql = GET_LAST_VM_DISK_STATS;
try {
pstmt = txn.prepareAutoCloseStatement(sql);
ResultSet rs = pstmt.executeQuery();
if (rs.next()) {
return Long.valueOf(rs.getLong(1));
}
} catch (Exception ex) {
s_logger.error("error getting last vm disk stats id", ex);
}
return null;
}
@Override
public void updateVmDiskStats(List<VmDiskStatisticsVO> vmDiskStats) {
TransactionLegacy txn = TransactionLegacy.currentTxn();
try {
txn.start();
String sql = UPDATE_VM_DISK_STATS;
PreparedStatement pstmt = null;
pstmt = txn.prepareAutoCloseStatement(sql); // in reality I just want CLOUD_USAGE dataSource connection
for (VmDiskStatisticsVO vmDiskStat : vmDiskStats) {
pstmt.setLong(1, vmDiskStat.getNetIORead());
pstmt.setLong(2, vmDiskStat.getNetIOWrite());
pstmt.setLong(3, vmDiskStat.getCurrentIORead());
pstmt.setLong(4, vmDiskStat.getCurrentIOWrite());
pstmt.setLong(5, vmDiskStat.getAggIORead());
pstmt.setLong(6, vmDiskStat.getAggIOWrite());
pstmt.setLong(7, vmDiskStat.getNetBytesRead());
pstmt.setLong(8, vmDiskStat.getNetBytesWrite());
pstmt.setLong(9, vmDiskStat.getCurrentBytesRead());
pstmt.setLong(10, vmDiskStat.getCurrentBytesWrite());
pstmt.setLong(11, vmDiskStat.getAggBytesRead());
pstmt.setLong(12, vmDiskStat.getAggBytesWrite());
pstmt.setLong(13, vmDiskStat.getId());
pstmt.addBatch();
}
pstmt.executeBatch();
txn.commit();
} catch (Exception ex) {
txn.rollback();
s_logger.error("error saving vm disk stats to cloud_usage db", ex);
throw new CloudRuntimeException(ex.getMessage());
}
}
@Override
public void saveVmDiskStats(List<VmDiskStatisticsVO> vmDiskStats) {
TransactionLegacy txn = TransactionLegacy.currentTxn();
try {
txn.start();
String sql = INSERT_VM_DISK_STATS;
PreparedStatement pstmt = null;
pstmt = txn.prepareAutoCloseStatement(sql); // in reality I just want CLOUD_USAGE dataSource connection
for (VmDiskStatisticsVO vmDiskStat : vmDiskStats) {
pstmt.setLong(1, vmDiskStat.getId());
pstmt.setLong(2, vmDiskStat.getDataCenterId());
pstmt.setLong(3, vmDiskStat.getAccountId());
if (vmDiskStat.getVmId() != null) {
pstmt.setLong(4, vmDiskStat.getVmId());
} else {
pstmt.setNull(4, Types.BIGINT);
}
if (vmDiskStat.getVolumeId() != null) {
pstmt.setLong(5, vmDiskStat.getVolumeId());
} else {
pstmt.setNull(5, Types.BIGINT);
}
pstmt.setLong(6, vmDiskStat.getNetIORead());
pstmt.setLong(7, vmDiskStat.getNetIOWrite());
pstmt.setLong(8, vmDiskStat.getCurrentIORead());
pstmt.setLong(9, vmDiskStat.getCurrentIOWrite());
pstmt.setLong(10, vmDiskStat.getAggIORead());
pstmt.setLong(11, vmDiskStat.getAggIOWrite());
pstmt.setLong(12, vmDiskStat.getNetBytesRead());
pstmt.setLong(13, vmDiskStat.getNetBytesWrite());
pstmt.setLong(14, vmDiskStat.getCurrentBytesRead());
pstmt.setLong(15, vmDiskStat.getCurrentBytesWrite());
pstmt.setLong(16, vmDiskStat.getAggBytesRead());
pstmt.setLong(17, vmDiskStat.getAggBytesWrite());
pstmt.addBatch();
}
pstmt.executeBatch();
txn.commit();
} catch (Exception ex) {
txn.rollback();
s_logger.error("error saving vm disk stats to cloud_usage db", ex);
throw new CloudRuntimeException(ex.getMessage());
}
}
@Override
public void saveUsageRecords(List<UsageVO> usageRecords) {
TransactionLegacy txn = TransactionLegacy.currentTxn();
try {
txn.start();
String sql = INSERT_USAGE_RECORDS;
PreparedStatement pstmt = null;
pstmt = txn.prepareAutoCloseStatement(sql); // in reality I just want CLOUD_USAGE dataSource connection
for (UsageVO usageRecord : usageRecords) {
pstmt.setLong(1, usageRecord.getZoneId());
pstmt.setLong(2, usageRecord.getAccountId());
pstmt.setLong(3, usageRecord.getDomainId());
pstmt.setString(4, usageRecord.getDescription());
pstmt.setString(5, usageRecord.getUsageDisplay());
pstmt.setInt(6, usageRecord.getUsageType());
pstmt.setDouble(7, usageRecord.getRawUsage());
if (usageRecord.getVmInstanceId() != null) {
pstmt.setLong(8, usageRecord.getVmInstanceId());
} else {
pstmt.setNull(8, Types.BIGINT);
}
pstmt.setString(9, usageRecord.getVmName());
if (usageRecord.getOfferingId() != null) {
pstmt.setLong(10, usageRecord.getOfferingId());
} else {
pstmt.setNull(10, Types.BIGINT);
}
if (usageRecord.getTemplateId() != null) {
pstmt.setLong(11, usageRecord.getTemplateId());
} else {
pstmt.setNull(11, Types.BIGINT);
}
if (usageRecord.getUsageId() != null) {
pstmt.setLong(12, usageRecord.getUsageId());
} else {
pstmt.setNull(12, Types.BIGINT);
}
pstmt.setString(13, usageRecord.getType());
if (usageRecord.getSize() != null) {
pstmt.setLong(14, usageRecord.getSize());
} else {
pstmt.setNull(14, Types.BIGINT);
}
if (usageRecord.getNetworkId() != null) {
pstmt.setLong(15, usageRecord.getNetworkId());
} else {
pstmt.setNull(15, Types.BIGINT);
}
pstmt.setString(16, DateUtil.getDateDisplayString(s_gmtTimeZone, usageRecord.getStartDate()));
pstmt.setString(17, DateUtil.getDateDisplayString(s_gmtTimeZone, usageRecord.getEndDate()));
if (usageRecord.getVirtualSize() != null) {
pstmt.setLong(18, usageRecord.getVirtualSize());
} else {
pstmt.setNull(18, Types.BIGINT);
}
pstmt.addBatch();
}
pstmt.executeBatch();
txn.commit();
} catch (Exception ex) {
txn.rollback();
s_logger.error("error saving usage records to cloud_usage db", ex);
throw new CloudRuntimeException(ex.getMessage());
}
}
}