Commit 3714c6f8 authored by Hiro's avatar Hiro 🏄
Browse files

The overload general field is reported only if fresher than 18 hours ago.

This solves issues:

relay-search#40016

tpo/network-health/helper-scripts#17
parent 778feb39
Pipeline #36271 failed with stage
in 1 minute and 34 seconds
......@@ -43,6 +43,8 @@ public class DateTimeHelper {
public static final long TWELVE_HOURS = 12L * ONE_HOUR;
public static final long EIGHTEEN_HOURS = 18L * ONE_HOUR;
public static final long ONE_DAY = 24L * ONE_HOUR;
public static final long TWO_DAYS = 2L * ONE_DAY;
......@@ -139,6 +141,21 @@ public class DateTimeHelper {
return timeNowMillis;
}
/** Given a time span (ex: 1 hour) return true if the current time passed is
* less than the duration passed, false otherwise.
*/
public static boolean timeFromNow(long timeInstant, long timeSpan) {
long timeLimit = timeInstant + timeSpan;
long timeDifference = timeLimit - millisNow();
if (timeInstant == -1L) {
return false;
} else if (timeDifference > timeSpan) {
return false;
} else {
return true;
}
}
/** Return the timedifference in milliseconds given a timestamp and a duration
* in days, hours, minutes, seconds.
*/
......
......@@ -188,6 +188,12 @@ public class NodeDetailsStatusUpdater implements DescriptorListener,
detailsStatus.setObservedBandwidth(observedBandwidth);
detailsStatus.setAdvertisedBandwidth(advertisedBandwidth);
long overloadGeneralTimestamp = descriptor.getOverloadGeneralTimestamp();
if (DateTimeHelper.timeFromNow(overloadGeneralTimestamp,
DateTimeHelper.EIGHTEEN_HOURS)) {
detailsStatus.setOverloadGeneralTimestamp(overloadGeneralTimestamp);
} else {
detailsStatus.setOverloadGeneralTimestamp(-1L);
}
detailsStatus.setOverloadGeneralTimestamp(overloadGeneralTimestamp);
detailsStatus.setExitPolicy(descriptor.getExitPolicyLines());
detailsStatus.setContact(descriptor.getContact());
......
......@@ -13,6 +13,7 @@ import org.torproject.descriptor.Descriptor;
import org.torproject.descriptor.DescriptorParser;
import org.torproject.descriptor.DescriptorSourceFactory;
import org.torproject.descriptor.ServerDescriptor;
import org.torproject.metrics.onionoo.docs.DateTimeHelper;
import org.torproject.metrics.onionoo.docs.DetailsStatus;
import org.torproject.metrics.onionoo.docs.DocumentStoreFactory;
import org.torproject.metrics.onionoo.docs.DummyDocumentStore;
......@@ -21,6 +22,7 @@ import org.junit.Before;
import org.junit.Test;
import java.io.File;
import java.util.Date;
import java.util.List;
public class NodeDetailsStatusUpdaterTest {
......@@ -99,6 +101,62 @@ public class NodeDetailsStatusUpdaterTest {
assertNotNull(dd);
}
@Test
public void testOverloadTimeCheck() {
NodeDetailsStatusUpdater ndsu = new NodeDetailsStatusUpdater(null, null);
DescriptorParser dp = DescriptorSourceFactory.createDescriptorParser();
String descString = BRIDGE_OVERLOAD;
for (Descriptor desc : dp.parseDescriptors(descString.getBytes(),
new File("dummy"), "dummy")) {
assertTrue(desc.getClass().getName(), desc instanceof ServerDescriptor);
ndsu.processDescriptor(desc, true);
}
assertEquals(1, this.docStore.getPerformedStoreOperations());
assertEquals(1, this.docStore.storedDocuments.size());
DetailsStatus dd = this.docStore.getDocument(DetailsStatus.class, FPB);
assertEquals(
DateTimeHelper.parse(overload_timestamp_human,
DateTimeHelper.ISO_DATETIME_FORMAT),
dd.getOverloadGeneralTimestamp());
}
@Test
public void testOverloadTimeExpired() {
NodeDetailsStatusUpdater ndsu = new NodeDetailsStatusUpdater(null, null);
DescriptorParser dp = DescriptorSourceFactory.createDescriptorParser();
String descString = BRIDGE_OVERLOAD_EXPIRED;
for (Descriptor desc : dp.parseDescriptors(descString.getBytes(),
new File("dummy"), "dummy")) {
assertTrue(desc.getClass().getName(), desc instanceof ServerDescriptor);
ndsu.processDescriptor(desc, true);
}
assertEquals(1, this.docStore.getPerformedStoreOperations());
assertEquals(1, this.docStore.storedDocuments.size());
DetailsStatus dd = this.docStore.getDocument(DetailsStatus.class, FPB);
assertEquals(null,
dd.getOverloadGeneralTimestamp());
}
public static final long ONE_SECOND = 1000L;
public static final long ONE_MINUTE = 60L * ONE_SECOND;
public static final long ONE_HOUR = 60L * ONE_MINUTE;
public static final Date date = new Date();
public static final long timeNowMillis = date.getTime();
public static final long overload_timestamp = timeNowMillis - (4L * ONE_HOUR);
public static final String overload_timestamp_human =
DateTimeHelper.format(overload_timestamp);
public static final long overload_timestamp_exp =
timeNowMillis - (20L * ONE_HOUR);
public static final String overload_timestamp_human_exp =
DateTimeHelper.format(overload_timestamp_exp);
private static final String FP = "42CAF9C0588BBADDD338025E8F2D3CCF35CEEC25";
private static final String FPB = "6306C870BE7415EB7167ED2E9C1224F28B7E6C61";
......@@ -107,6 +165,52 @@ public class NodeDetailsStatusUpdaterTest {
+ "6306C870BE7415EB7167ED2E9C1224F28B7E6C61 moat "
+ "transport=obfs4 ip=4 blocklist=ru\n";
private static final String BRIDGE_OVERLOAD = "@type bridge-"
+ "server-descriptor 1.2\n"
+ "router DockerObfs4Bridge 10.136.176.127 50451 0 0\n"
+ "master-key-ed25519 J6M6MmuZ2joRKaihSB1LDym+WiuUeId8+9nvtI1Sd1M\n"
+ "platform Tor 0.4.6.10 on Linux\n"
+ "proto Cons=1-2 Desc=1-2 DirCache=2 FlowCtrl=1 HSDir=2 HSIntro=4-5 "
+ "HSRend=1-2 Link=1-5 LinkAuth=1,3 Microdesc=1-2 Padding=2 Relay=1-3\n"
+ "published 2022-03-17 08:07:11\n"
+ "fingerprint 6306 C870 BE74 15EB 7167 ED2E 9C12 24F2 8B7E 6C61\n"
+ "uptime 113883\n"
+ "bandwidth 1073741824 1073741824 81352\n"
+ "extra-info-digest 29ECA238A04F0D00E941B2CC430393604430B7D4 "
+ "X0jHhL7ygWblKzjoTGMG4Eikz8Sqo2X9eIbrKzVANNw\n"
+ "hidden-service-dir\n"
+ "contact somebody\n"
+ "bridge-distribution-request any\n"
+ "ntor-onion-key kysCjXGfMAx6AdejV2r34d59atkXFkvLNT/9ADcd73M\n"
+ "reject *:*\n"
+ "tunnelled-dir-server\n"
+ "overload-general 1 " + overload_timestamp_human + "\n"
+ "router-digest-sha256 t+rUo6bm75/Qywapm5TTTnY2HGmxZqFAI1/ON2IxNP4\n"
+ "router-digest 78E7EDA527387064BB4410FC8BAED6DA0902BFE3\n";
private static final String BRIDGE_OVERLOAD_EXPIRED = "@type bridge-"
+ "server-descriptor 1.2\n"
+ "router DockerObfs4Bridge 10.136.176.127 50451 0 0\n"
+ "master-key-ed25519 J6M6MmuZ2joRKaihSB1LDym+WiuUeId8+9nvtI1Sd1M\n"
+ "platform Tor 0.4.6.10 on Linux\n"
+ "proto Cons=1-2 Desc=1-2 DirCache=2 FlowCtrl=1 HSDir=2 HSIntro=4-5 "
+ "HSRend=1-2 Link=1-5 LinkAuth=1,3 Microdesc=1-2 Padding=2 Relay=1-3\n"
+ "published 2022-03-17 08:07:11\n"
+ "fingerprint 6306 C870 BE74 15EB 7167 ED2E 9C12 24F2 8B7E 6C61\n"
+ "uptime 113883\n"
+ "bandwidth 1073741824 1073741824 81352\n"
+ "extra-info-digest 29ECA238A04F0D00E941B2CC430393604430B7D4 "
+ "X0jHhL7ygWblKzjoTGMG4Eikz8Sqo2X9eIbrKzVANNw\n"
+ "hidden-service-dir\n"
+ "contact somebody\n"
+ "bridge-distribution-request any\n"
+ "ntor-onion-key kysCjXGfMAx6AdejV2r34d59atkXFkvLNT/9ADcd73M\n"
+ "reject *:*\n"
+ "tunnelled-dir-server\n"
+ "overload-general 1 " + overload_timestamp_human_exp + "\n"
+ "router-digest-sha256 t+rUo6bm75/Qywapm5TTTnY2HGmxZqFAI1/ON2IxNP4\n"
+ "router-digest 78E7EDA527387064BB4410FC8BAED6DA0902BFE3\n";
private static final String BRIDGE1 = "@type bridge-server-descriptor 1.2\n"
+ "router DockerObfs4Bridge 10.136.176.127 50451 0 0\n"
+ "master-key-ed25519 J6M6MmuZ2joRKaihSB1LDym+WiuUeId8+9nvtI1Sd1M\n"
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment