[Author Prev][Author Next][Thread Prev][Thread Next][Author Index][Thread Index]
[tor-commits] [onionoo/master] Make BandwidthStatus smarter about itself.
commit ca10a8da5a0d30216c6365faca9cff7ba40a2d7a
Author: Karsten Loesing <karsten.loesing@xxxxxxx>
Date: Fri Aug 15 18:54:28 2014 +0200
Make BandwidthStatus smarter about itself.
Removes another dependency on DateTimeHelper, which gets us closer to
implementing #12866.
---
.../torproject/onionoo/docs/BandwidthStatus.java | 92 ++++++++++++++++++++
.../onionoo/updater/BandwidthStatusUpdater.java | 88 ++-----------------
2 files changed, 98 insertions(+), 82 deletions(-)
diff --git a/src/main/java/org/torproject/onionoo/docs/BandwidthStatus.java b/src/main/java/org/torproject/onionoo/docs/BandwidthStatus.java
index a2980e5..c01a242 100644
--- a/src/main/java/org/torproject/onionoo/docs/BandwidthStatus.java
+++ b/src/main/java/org/torproject/onionoo/docs/BandwidthStatus.java
@@ -2,14 +2,25 @@
* See LICENSE for licensing information */
package org.torproject.onionoo.docs;
+import java.util.Map;
import java.util.Scanner;
import java.util.SortedMap;
import java.util.TreeMap;
+import org.torproject.descriptor.BandwidthHistory;
import org.torproject.onionoo.util.DateTimeHelper;
+import org.torproject.onionoo.util.TimeFactory;
public class BandwidthStatus extends Document {
+ private transient boolean isDirty = false;
+ public boolean isDirty() {
+ return this.isDirty;
+ }
+ public void clearDirty() {
+ this.isDirty = false;
+ }
+
private SortedMap<Long, long[]> writeHistory =
new TreeMap<Long, long[]>();
public void setWriteHistory(SortedMap<Long, long[]> writeHistory) {
@@ -62,6 +73,87 @@ public class BandwidthStatus extends Document {
s.close();
}
+ public void addToWriteHistory(BandwidthHistory bandwidthHistory) {
+ this.addToHistory(this.writeHistory, bandwidthHistory);
+ }
+
+ public void addToReadHistory(BandwidthHistory bandwidthHistory) {
+ this.addToHistory(this.readHistory, bandwidthHistory);
+ }
+
+ private void addToHistory(SortedMap<Long, long[]> history,
+ BandwidthHistory bandwidthHistory) {
+ long intervalMillis = bandwidthHistory.getIntervalLength()
+ * DateTimeHelper.ONE_SECOND;
+ for (Map.Entry<Long, Long> e :
+ bandwidthHistory.getBandwidthValues().entrySet()) {
+ long endMillis = e.getKey(),
+ startMillis = endMillis - intervalMillis;
+ long bandwidthValue = e.getValue();
+ /* TODO Should we first check whether an interval is already
+ * contained in history? */
+ history.put(startMillis, new long[] { startMillis, endMillis,
+ bandwidthValue });
+ this.isDirty = true;
+ }
+ }
+
+ public void compressHistory() {
+ this.compressHistory(this.writeHistory);
+ this.compressHistory(this.readHistory);
+ }
+
+ private void compressHistory(SortedMap<Long, long[]> history) {
+ SortedMap<Long, long[]> uncompressedHistory =
+ new TreeMap<Long, long[]>(history);
+ history.clear();
+ long lastStartMillis = 0L, lastEndMillis = 0L, lastBandwidth = 0L;
+ String lastMonthString = "1970-01";
+ long now = TimeFactory.getTime().currentTimeMillis();
+ for (long[] v : uncompressedHistory.values()) {
+ long startMillis = v[0], endMillis = v[1], bandwidth = v[2];
+ long intervalLengthMillis;
+ if (now - endMillis <= DateTimeHelper.THREE_DAYS) {
+ intervalLengthMillis = DateTimeHelper.FIFTEEN_MINUTES;
+ } else if (now - endMillis <= DateTimeHelper.ONE_WEEK) {
+ intervalLengthMillis = DateTimeHelper.ONE_HOUR;
+ } else if (now - endMillis <=
+ DateTimeHelper.ROUGHLY_ONE_MONTH) {
+ intervalLengthMillis = DateTimeHelper.FOUR_HOURS;
+ } else if (now - endMillis <=
+ DateTimeHelper.ROUGHLY_THREE_MONTHS) {
+ intervalLengthMillis = DateTimeHelper.TWELVE_HOURS;
+ } else if (now - endMillis <=
+ DateTimeHelper.ROUGHLY_ONE_YEAR) {
+ intervalLengthMillis = DateTimeHelper.TWO_DAYS;
+ } else {
+ intervalLengthMillis = DateTimeHelper.TEN_DAYS;
+ }
+ String monthString = DateTimeHelper.format(startMillis,
+ DateTimeHelper.ISO_YEARMONTH_FORMAT);
+ if (lastEndMillis == startMillis &&
+ ((lastEndMillis - 1L) / intervalLengthMillis) ==
+ ((endMillis - 1L) / intervalLengthMillis) &&
+ lastMonthString.equals(monthString)) {
+ lastEndMillis = endMillis;
+ lastBandwidth += bandwidth;
+ } else {
+ if (lastStartMillis > 0L) {
+ history.put(lastStartMillis, new long[] { lastStartMillis,
+ lastEndMillis, lastBandwidth });
+ }
+ lastStartMillis = startMillis;
+ lastEndMillis = endMillis;
+ lastBandwidth = bandwidth;
+ }
+ lastMonthString = monthString;
+ }
+ if (lastStartMillis > 0L) {
+ history.put(lastStartMillis, new long[] { lastStartMillis,
+ lastEndMillis, lastBandwidth });
+ }
+ }
+
public String toDocumentString() {
StringBuilder sb = new StringBuilder();
for (long[] v : writeHistory.values()) {
diff --git a/src/main/java/org/torproject/onionoo/updater/BandwidthStatusUpdater.java b/src/main/java/org/torproject/onionoo/updater/BandwidthStatusUpdater.java
index 5c27464..9a7717c 100644
--- a/src/main/java/org/torproject/onionoo/updater/BandwidthStatusUpdater.java
+++ b/src/main/java/org/torproject/onionoo/updater/BandwidthStatusUpdater.java
@@ -2,18 +2,11 @@
* See LICENSE for licensing information */
package org.torproject.onionoo.updater;
-import java.util.Map;
-import java.util.SortedMap;
-import java.util.TreeMap;
-
-import org.torproject.descriptor.BandwidthHistory;
import org.torproject.descriptor.Descriptor;
import org.torproject.descriptor.ExtraInfoDescriptor;
import org.torproject.onionoo.docs.BandwidthStatus;
import org.torproject.onionoo.docs.DocumentStore;
import org.torproject.onionoo.docs.DocumentStoreFactory;
-import org.torproject.onionoo.util.DateTimeHelper;
-import org.torproject.onionoo.util.TimeFactory;
public class BandwidthStatusUpdater implements DescriptorListener,
StatusUpdater {
@@ -22,12 +15,9 @@ public class BandwidthStatusUpdater implements DescriptorListener,
private DocumentStore documentStore;
- private long now;
-
public BandwidthStatusUpdater() {
this.descriptorSource = DescriptorSourceFactory.getDescriptorSource();
this.documentStore = DocumentStoreFactory.getDocumentStore();
- this.now = TimeFactory.getTime().currentTimeMillis();
this.registerDescriptorListeners();
}
@@ -56,81 +46,15 @@ public class BandwidthStatusUpdater implements DescriptorListener,
bandwidthStatus = new BandwidthStatus();
}
if (descriptor.getWriteHistory() != null) {
- this.parseHistory(descriptor.getWriteHistory(),
- bandwidthStatus.getWriteHistory());
+ bandwidthStatus.addToWriteHistory(descriptor.getWriteHistory());
}
if (descriptor.getReadHistory() != null) {
- this.parseHistory(descriptor.getReadHistory(),
- bandwidthStatus.getReadHistory());
- }
- this.compressHistory(bandwidthStatus.getWriteHistory());
- this.compressHistory(bandwidthStatus.getReadHistory());
- this.documentStore.store(bandwidthStatus, fingerprint);
- }
-
- private void parseHistory(BandwidthHistory bandwidthHistory,
- SortedMap<Long, long[]> history) {
- long intervalMillis = bandwidthHistory.getIntervalLength()
- * DateTimeHelper.ONE_SECOND;
- for (Map.Entry<Long, Long> e :
- bandwidthHistory.getBandwidthValues().entrySet()) {
- long endMillis = e.getKey(),
- startMillis = endMillis - intervalMillis;
- long bandwidthValue = e.getValue();
- /* TODO Should we first check whether an interval is already
- * contained in history? */
- history.put(startMillis, new long[] { startMillis, endMillis,
- bandwidthValue });
- }
- }
-
- private void compressHistory(SortedMap<Long, long[]> history) {
- SortedMap<Long, long[]> uncompressedHistory =
- new TreeMap<Long, long[]>(history);
- history.clear();
- long lastStartMillis = 0L, lastEndMillis = 0L, lastBandwidth = 0L;
- String lastMonthString = "1970-01";
- for (long[] v : uncompressedHistory.values()) {
- long startMillis = v[0], endMillis = v[1], bandwidth = v[2];
- long intervalLengthMillis;
- if (this.now - endMillis <= DateTimeHelper.THREE_DAYS) {
- intervalLengthMillis = DateTimeHelper.FIFTEEN_MINUTES;
- } else if (this.now - endMillis <= DateTimeHelper.ONE_WEEK) {
- intervalLengthMillis = DateTimeHelper.ONE_HOUR;
- } else if (this.now - endMillis <=
- DateTimeHelper.ROUGHLY_ONE_MONTH) {
- intervalLengthMillis = DateTimeHelper.FOUR_HOURS;
- } else if (this.now - endMillis <=
- DateTimeHelper.ROUGHLY_THREE_MONTHS) {
- intervalLengthMillis = DateTimeHelper.TWELVE_HOURS;
- } else if (this.now - endMillis <=
- DateTimeHelper.ROUGHLY_ONE_YEAR) {
- intervalLengthMillis = DateTimeHelper.TWO_DAYS;
- } else {
- intervalLengthMillis = DateTimeHelper.TEN_DAYS;
- }
- String monthString = DateTimeHelper.format(startMillis,
- DateTimeHelper.ISO_YEARMONTH_FORMAT);
- if (lastEndMillis == startMillis &&
- ((lastEndMillis - 1L) / intervalLengthMillis) ==
- ((endMillis - 1L) / intervalLengthMillis) &&
- lastMonthString.equals(monthString)) {
- lastEndMillis = endMillis;
- lastBandwidth += bandwidth;
- } else {
- if (lastStartMillis > 0L) {
- history.put(lastStartMillis, new long[] { lastStartMillis,
- lastEndMillis, lastBandwidth });
- }
- lastStartMillis = startMillis;
- lastEndMillis = endMillis;
- lastBandwidth = bandwidth;
- }
- lastMonthString = monthString;
+ bandwidthStatus.addToReadHistory(descriptor.getReadHistory());
}
- if (lastStartMillis > 0L) {
- history.put(lastStartMillis, new long[] { lastStartMillis,
- lastEndMillis, lastBandwidth });
+ if (bandwidthStatus.isDirty()) {
+ bandwidthStatus.compressHistory();
+ this.documentStore.store(bandwidthStatus, fingerprint);
+ bandwidthStatus.clearDirty();
}
}
_______________________________________________
tor-commits mailing list
tor-commits@xxxxxxxxxxxxxxxxxxxx
https://lists.torproject.org/cgi-bin/mailman/listinfo/tor-commits