forked from apache/cloudstack
-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathUsageEventDaoImpl.java
More file actions
229 lines (207 loc) · 9.73 KB
/
UsageEventDaoImpl.java
File metadata and controls
229 lines (207 loc) · 9.73 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
// 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.event.dao;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.TimeZone;
import javax.inject.Inject;
import org.springframework.stereotype.Component;
import com.cloud.dc.Vlan;
import com.cloud.event.EventTypes;
import com.cloud.event.UsageEventVO;
import com.cloud.utils.DateUtil;
import com.cloud.utils.db.DB;
import com.cloud.utils.db.Filter;
import com.cloud.utils.db.GenericDaoBase;
import com.cloud.utils.db.SearchBuilder;
import com.cloud.utils.db.SearchCriteria;
import com.cloud.utils.db.TransactionLegacy;
import com.cloud.utils.exception.CloudRuntimeException;
@Component
public class UsageEventDaoImpl extends GenericDaoBase<UsageEventVO, Long> implements UsageEventDao {
private final SearchBuilder<UsageEventVO> latestEventsSearch;
private final SearchBuilder<UsageEventVO> IpeventsSearch;
private static final String COPY_EVENTS =
"INSERT INTO cloud_usage.usage_event (id, type, account_id, created, zone_id, resource_id, resource_name, offering_id, template_id, size, resource_type, virtual_size, vm_id) "
+ "SELECT id, type, account_id, created, zone_id, resource_id, resource_name, offering_id, template_id, size, resource_type, virtual_size, vm_id FROM cloud.usage_event vmevt WHERE vmevt.id > ? and vmevt.id <= ? ";
private static final String COPY_ALL_EVENTS =
"INSERT INTO cloud_usage.usage_event (id, type, account_id, created, zone_id, resource_id, resource_name, offering_id, template_id, size, resource_type, virtual_size, vm_id) "
+ "SELECT id, type, account_id, created, zone_id, resource_id, resource_name, offering_id, template_id, size, resource_type, virtual_size, vm_id FROM cloud.usage_event vmevt WHERE vmevt.id <= ?";
private static final String COPY_EVENT_DETAILS = "INSERT INTO cloud_usage.usage_event_details (id, usage_event_id, name, value) "
+ "SELECT id, usage_event_id, name, value FROM cloud.usage_event_details vmevtDetails WHERE vmevtDetails.usage_event_id > ? and vmevtDetails.usage_event_id <= ? ";
private static final String COPY_ALL_EVENT_DETAILS = "INSERT INTO cloud_usage.usage_event_details (id, usage_event_id, name, value) "
+ "SELECT id, usage_event_id, name, value FROM cloud.usage_event_details vmevtDetails WHERE vmevtDetails.usage_event_id <= ?";
private static final String MAX_EVENT = "select max(id) from cloud.usage_event where created <= ?";
@Inject
protected UsageEventDetailsDao usageEventDetailsDao;
public UsageEventDaoImpl() {
latestEventsSearch = createSearchBuilder();
latestEventsSearch.and("processed", latestEventsSearch.entity().isProcessed(), SearchCriteria.Op.EQ);
latestEventsSearch.and("enddate", latestEventsSearch.entity().getCreateDate(), SearchCriteria.Op.LTEQ);
latestEventsSearch.done();
IpeventsSearch = createSearchBuilder();
IpeventsSearch.and("startdate", IpeventsSearch.entity().getCreateDate(), SearchCriteria.Op.GTEQ);
IpeventsSearch.and("enddate", IpeventsSearch.entity().getCreateDate(), SearchCriteria.Op.LTEQ);
IpeventsSearch.and("zoneid", IpeventsSearch.entity().getZoneId(), SearchCriteria.Op.EQ);
IpeventsSearch.and("networktype", IpeventsSearch.entity().getResourceType(), SearchCriteria.Op.EQ);
IpeventsSearch.and().op("assignEvent", IpeventsSearch.entity().getType(), SearchCriteria.Op.EQ);
IpeventsSearch.or("releaseEvent", IpeventsSearch.entity().getType(), SearchCriteria.Op.EQ);
IpeventsSearch.cp();
IpeventsSearch.done();
}
@Override
public List<UsageEventVO> listLatestEvents(Date endDate) {
Filter filter = new Filter(UsageEventVO.class, "createDate", Boolean.TRUE, null, null);
SearchCriteria<UsageEventVO> sc = latestEventsSearch.create();
sc.setParameters("processed", false);
sc.setParameters("enddate", endDate);
return listBy(sc, filter);
}
@Override
public List<UsageEventVO> getLatestEvent() {
Filter filter = new Filter(UsageEventVO.class, "id", Boolean.FALSE, Long.valueOf(0), Long.valueOf(1));
return listAll(filter);
}
@Override
@DB
public synchronized List<UsageEventVO> getRecentEvents(Date endDate) {
long recentEventId = getMostRecentEventId();
long maxEventId = getMaxEventId(endDate);
TransactionLegacy txn = TransactionLegacy.open(TransactionLegacy.USAGE_DB);
// Copy events from cloud db to usage db
String sql = COPY_EVENTS;
if (recentEventId == 0) {
if (logger.isDebugEnabled()) {
logger.debug("no recent event date, copying all events");
}
sql = COPY_ALL_EVENTS;
}
PreparedStatement pstmt = null;
try {
txn.start();
pstmt = txn.prepareAutoCloseStatement(sql);
int i = 1;
if (recentEventId != 0) {
pstmt.setLong(i++, recentEventId);
}
pstmt.setLong(i++, maxEventId);
pstmt.executeUpdate();
txn.commit();
} catch (Exception ex) {
txn.rollback();
logger.error("error copying events from cloud db to usage db", ex);
throw new CloudRuntimeException(ex.getMessage());
} finally {
txn.close();
}
// Copy event details from cloud db to usage db
sql = COPY_EVENT_DETAILS;
if (recentEventId == 0) {
if (logger.isDebugEnabled()) {
logger.debug("no recent event date, copying all event detailss");
}
sql = COPY_ALL_EVENT_DETAILS;
}
pstmt = null;
try {
txn.start();
pstmt = txn.prepareAutoCloseStatement(sql);
int i = 1;
if (recentEventId != 0) {
pstmt.setLong(i++, recentEventId);
}
pstmt.setLong(i++, maxEventId);
pstmt.executeUpdate();
txn.commit();
} catch (Exception ex) {
txn.rollback();
logger.error("error copying event details from cloud db to usage db", ex);
throw new CloudRuntimeException(ex.getMessage());
} finally {
txn.close();
}
return findRecentEvents(endDate);
}
@DB
private long getMostRecentEventId() {
TransactionLegacy txn = TransactionLegacy.open(TransactionLegacy.USAGE_DB);
try {
List<UsageEventVO> latestEvents = getLatestEvent();
if (latestEvents != null && latestEvents.size() == 1) {
UsageEventVO latestEvent = latestEvents.get(0);
if (latestEvent != null) {
return latestEvent.getId();
}
}
return 0;
} catch (Exception ex) {
logger.error("error getting most recent event id", ex);
throw new CloudRuntimeException(ex.getMessage());
} finally {
txn.close();
}
}
private List<UsageEventVO> findRecentEvents(Date endDate) {
TransactionLegacy txn = TransactionLegacy.open(TransactionLegacy.USAGE_DB);
try {
return listLatestEvents(endDate);
} catch (Exception ex) {
logger.error("error getting most recent event date", ex);
throw new CloudRuntimeException(ex.getMessage());
} finally {
txn.close();
}
}
private long getMaxEventId(Date endDate) {
TransactionLegacy txn = TransactionLegacy.currentTxn();
PreparedStatement pstmt = null;
try {
String sql = MAX_EVENT;
pstmt = txn.prepareAutoCloseStatement(sql);
pstmt.setString(1, DateUtil.getDateDisplayString(TimeZone.getTimeZone("GMT"), endDate));
ResultSet rs = pstmt.executeQuery();
if (rs.next()) {
return Long.valueOf(rs.getLong(1));
}
return 0;
} catch (Exception ex) {
logger.error("error getting max event id", ex);
throw new CloudRuntimeException(ex.getMessage());
} finally {
txn.close();
}
}
@Override
public List<UsageEventVO> listDirectIpEvents(Date startDate, Date endDate, long zoneId) {
Filter filter = new Filter(UsageEventVO.class, "createDate", Boolean.TRUE, null, null);
SearchCriteria<UsageEventVO> sc = IpeventsSearch.create();
sc.setParameters("startdate", startDate);
sc.setParameters("enddate", endDate);
sc.setParameters("assignEvent", EventTypes.EVENT_NET_IP_ASSIGN);
sc.setParameters("releaseEvent", EventTypes.EVENT_NET_IP_RELEASE);
sc.setParameters("zoneid", zoneId);
sc.setParameters("networktype", Vlan.VlanType.DirectAttached.toString());
return listBy(sc, filter);
}
@Override
public void saveDetails(long eventId, Map<String, String> details) {
usageEventDetailsDao.persist(eventId, details);
}
}