/
StorageUsageLog.java
337 lines (270 loc) · 9.94 KB
/
StorageUsageLog.java
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
package com.eucalyptus.reporting.modules.storage;
import java.util.*;
import org.apache.log4j.Logger;
import com.eucalyptus.entities.EntityWrapper;
import com.eucalyptus.reporting.Period;
/**
* <p>StorageUsageLog is the main API for accessing storage usage information.
*
* @author tom.werges
*/
public class StorageUsageLog
{
private static Logger log = Logger.getLogger( StorageUsageLog.class );
private static StorageUsageLog instance;
private StorageUsageLog()
{
}
public static StorageUsageLog getStorageUsageLog()
{
if (instance == null) {
instance = new StorageUsageLog();
}
return instance;
}
public void purgeLog(long earlierThanMs)
{
log.info(String.format("purge earlierThan:%d ", earlierThanMs));
EntityWrapper<StorageUsageSnapshot> entityWrapper =
EntityWrapper.get(StorageUsageSnapshot.class);
try {
/* Delete older instance snapshots
*/
entityWrapper.createSQLQuery("DELETE FROM storage_usage_snapshot "
+ "WHERE timestamp_ms < ?")
.setLong(0, new Long(earlierThanMs))
.executeUpdate();
entityWrapper.commit();
} catch (Exception ex) {
log.error(ex);
entityWrapper.rollback();
throw new RuntimeException(ex);
}
}
/**
* <p>Find the latest allSnapshot before timestampMs, or null if none
*/
Long findLatestAllSnapshotBefore(long timestampMs)
{
Long foundTimestampMs = null;
/* Iteratively query before startingMs, moving backward in
* exponentially growing intervals, starting at 3 hrs before
*/
for (double minsBefore=180; /* 3 hrs */
System.currentTimeMillis()-(long)(minsBefore*60*1000) > 0;
minsBefore=Math.pow(minsBefore, 1.1))
{
long queryStartMs = System.currentTimeMillis()-(long)(minsBefore*60*1000);
EntityWrapper<StorageUsageSnapshot> entityWrapper =
EntityWrapper.get(StorageUsageSnapshot.class);
log.info("Searching for latest timestamp before beginning:" + queryStartMs);
try {
@SuppressWarnings("rawtypes")
List list = entityWrapper.createQuery(
"from StorageUsageSnapshot as sus"
+ " WHERE sus.key.timestampMs > ?"
+ " AND sus.key.timestampMs < ?"
+ " AND sus.allSnapshot = true")
.setLong(0, new Long(queryStartMs))
.setLong(1, new Long(timestampMs))
.list();
for (Object obj : list) {
StorageUsageSnapshot snapshot = (StorageUsageSnapshot) obj;
foundTimestampMs = snapshot.getSnapshotKey().getTimestampMs();
}
entityWrapper.commit();
} catch (Exception ex) {
log.error(ex);
entityWrapper.rollback();
throw new RuntimeException(ex);
}
if (foundTimestampMs != null) break;
}
log.info("Found latest timestamp before beginning:"
+ foundTimestampMs);
return foundTimestampMs;
}
Map<StorageSummaryKey, StorageUsageData> findLatestUsageData()
{
log.info("LoadLastUsageData");
final Map<StorageSummaryKey, StorageUsageData> usageMap =
new HashMap<StorageSummaryKey, StorageUsageData>();
EntityWrapper<StorageUsageSnapshot> entityWrapper =
EntityWrapper.get(StorageUsageSnapshot.class);
try {
Long latestSnapshotBeforeMs =
findLatestAllSnapshotBefore(System.currentTimeMillis());
@SuppressWarnings("rawtypes")
List list = entityWrapper.createQuery(
"from StorageUsageSnapshot as sus"
+ " WHERE sus.key.timestampMs > ?")
.setLong(0, (latestSnapshotBeforeMs!=null ? latestSnapshotBeforeMs : 0l))
.list();
for (Object obj: list) {
StorageUsageSnapshot snapshot = (StorageUsageSnapshot) obj;
StorageSnapshotKey snapshotKey = snapshot.getSnapshotKey();
StorageSummaryKey summaryKey = new StorageSummaryKey(snapshotKey);
usageMap.put(summaryKey, snapshot.getUsageData());
}
entityWrapper.commit();
} catch (Exception ex) {
log.error(ex);
entityWrapper.rollback();
throw new RuntimeException(ex);
}
return usageMap;
}
/**
* <p>Gather a Map of all Storage resource usage for a period.
*/
public Map<StorageSummaryKey, StorageUsageSummary> getUsageSummaryMap(
Period period, String accountId)
{
log.info("GetUsageSummaryMap period:" + period);
final Map<StorageSummaryKey, StorageUsageSummary> usageMap =
new HashMap<StorageSummaryKey, StorageUsageSummary>();
EntityWrapper<StorageUsageSnapshot> entityWrapper =
EntityWrapper.get(StorageUsageSnapshot.class);
try {
/* Start query from last snapshot before report beginning, iterate
* through the data, and accumulate all reporting info through the
* report end. We will accumulate a fraction of a snapshot at the
* beginning and end, since report boundaries will not likely
* coincide with sampling period boundaries.
*/
Map<StorageSummaryKey,StorageDataAccumulator> dataAccumulatorMap =
new HashMap<StorageSummaryKey,StorageDataAccumulator>();
Long latestSnapshotBeforeMs =
findLatestAllSnapshotBefore(period.getBeginningMs());
@SuppressWarnings("rawtypes")
List list = null;
if (accountId == null) {
list = entityWrapper.createQuery(
"from StorageUsageSnapshot as sus"
+ " WHERE sus.key.timestampMs > ?"
+ " AND sus.key.timestampMs < ?"
+ " ORDER BY sus.key.timestampMs")
.setLong(0, (latestSnapshotBeforeMs!=null ? latestSnapshotBeforeMs : 0l))
.setLong(1, new Long(period.getEndingMs()))
.list();
} else {
list = entityWrapper.createQuery(
"from StorageUsageSnapshot as sus"
+ " WHERE sus.key.timestampMs > ?"
+ " AND sus.key.timestampMs < ?"
+ " AND sus.key.accountId = ?"
+ " ORDER BY sus.key.timestampMs")
.setLong(0, (latestSnapshotBeforeMs!=null ? latestSnapshotBeforeMs : 0l))
.setLong(1, new Long(period.getEndingMs()))
.setString(2, accountId)
.list();
}
for (Object obj: list) {
StorageUsageSnapshot snapshot = (StorageUsageSnapshot) obj;
StorageSnapshotKey snapshotKey = snapshot.getSnapshotKey();
StorageSummaryKey summaryKey = new StorageSummaryKey(snapshotKey);
if ( snapshotKey.getTimestampMs() < period.getBeginningMs()
|| !dataAccumulatorMap.containsKey(summaryKey) ) {
//new accumulator, discard earlier accumulators from before report beginning
StorageDataAccumulator accumulator =
new StorageDataAccumulator(snapshotKey.getTimestampMs(),
snapshot.getUsageData(), new StorageUsageSummary());
dataAccumulatorMap.put(summaryKey, accumulator);
} else {
/* Within interval; accumulate resource usage by adding
* to accumulator, for this key.
*/
StorageDataAccumulator accumulator = dataAccumulatorMap.get( summaryKey );
/* Extrapolate fractional usage for snapshots which occurred
* before report beginning.
*/
long beginningMs = Math.max( period.getBeginningMs(),
accumulator.getLastTimestamp() );
//query above specifies timestamp is before report end
long endingMs = snapshotKey.getTimestampMs()-1;
long durationSecs = (endingMs - beginningMs) / 1000;
log.info(String.format("Accumulate usage, %d-%d, key:%s",
beginningMs, endingMs, summaryKey));
accumulator.accumulateUsage( durationSecs );
accumulator.setLastTimestamp(snapshotKey.getTimestampMs());
accumulator.setLastUsageData(snapshot.getUsageData());
}
}
/* Accumulate fractional data usage at end of reporting period.
*/
for ( StorageSummaryKey key: dataAccumulatorMap.keySet() ) {
StorageDataAccumulator accumulator = dataAccumulatorMap.get( key );
long beginningMs = Math.max( period.getBeginningMs(),
accumulator.getLastTimestamp() );
long endingMs = period.getEndingMs() - 1;
long durationSecs = ( endingMs-beginningMs ) / 1000;
log.info(String.format("Accumulate endUsage, %d-%d, key:%s",
beginningMs, endingMs, key));
accumulator.accumulateUsage( durationSecs );
//add to results
usageMap.put( key, accumulator.getCurrentSummary() );
}
entityWrapper.commit();
} catch (Exception ex) {
log.error(ex);
entityWrapper.rollback();
throw new RuntimeException(ex);
}
return usageMap;
}
private class StorageDataAccumulator
{
private Long lastTimestamp;
private StorageUsageData lastUsageData;
private StorageUsageSummary currentSummary;
public StorageDataAccumulator(Long lastTimestamp,
StorageUsageData lastUsageData, StorageUsageSummary currentSummary)
{
super();
this.lastTimestamp = lastTimestamp;
this.lastUsageData = lastUsageData;
this.currentSummary = currentSummary;
}
public Long getLastTimestamp()
{
return lastTimestamp;
}
public void setLastTimestamp(Long lastTimestamp)
{
this.lastTimestamp = lastTimestamp;
}
public void setLastUsageData(StorageUsageData lastUsageData)
{
this.lastUsageData = lastUsageData;
}
public StorageUsageSummary getCurrentSummary()
{
return currentSummary;
}
/**
* <p>Accumulate usage. We've been holding on to the last usage data
* seen. Now we know how long that usage data prevails. Add the usage
* to the summary.
*/
public void accumulateUsage( long durationSecs )
{
if (lastUsageData != null) {
currentSummary.addVolumesMegsSecs(
lastUsageData.getVolumesMegs() * durationSecs);
currentSummary.addSnapshotsMegsSecs(
lastUsageData.getSnapshotsMegs() * durationSecs);
currentSummary.setVolumesMegsMax(
Math.max(currentSummary.getVolumesMegsMax(),
lastUsageData.getVolumesMegs()));
currentSummary.setSnapshotsMegsMax(
Math.max(currentSummary.getSnapshotsMegsMax(),
lastUsageData.getSnapshotsMegs()));
log.info(String.format("Accumulated durationSecs:%d raw:%d,%d multDurationSecs:%d,%d",durationSecs,
lastUsageData.getVolumesMegs(), lastUsageData.getSnapshotsMegs(),
(lastUsageData.getVolumesMegs() * durationSecs),
(lastUsageData.getSnapshotsMegs() * durationSecs)));
this.lastUsageData = null;
}
}
}
}