From f5f84899ec5ef6d469ad62b2e9c7ff2ec5788e68 Mon Sep 17 00:00:00 2001 From: Zlatin Balevsky Date: Sat, 24 Oct 2020 12:38:18 +0100 Subject: [PATCH] change state on observation even if there is not enough history yet. Initialize MC with last state from DB --- .../muwire/core/hostcache/H2HostCache.groovy | 8 ++++++-- .../muwire/core/hostcache/HostMCProfile.groovy | 17 ++++++++++++----- 2 files changed, 18 insertions(+), 7 deletions(-) diff --git a/core/src/main/groovy/com/muwire/core/hostcache/H2HostCache.groovy b/core/src/main/groovy/com/muwire/core/hostcache/H2HostCache.groovy index 2d778d1d..8945245f 100644 --- a/core/src/main/groovy/com/muwire/core/hostcache/H2HostCache.groovy +++ b/core/src/main/groovy/com/muwire/core/hostcache/H2HostCache.groovy @@ -73,6 +73,7 @@ class H2HostCache extends HostCache { def count = sql.firstRow("select count(*) as COUNT from HOST_ATTEMPTS where DESTINATION=${d.toBase64()}") if (count.COUNT < settings.minHostProfileHistory) { log.fine("not enough history for Markov") + profiles.put(d, new HostMCProfile(state)) return } @@ -316,8 +317,11 @@ class H2HostCache extends HostCache { if (uniqueHosts.add(dest)) { def fromDB = sql.firstRow("select * from HOST_PROFILES where DESTINATION=${dest.toBase64()}") def profile = new HostMCProfile() - if (fromDB != null) - profile = new HostMCProfile(fromDB, ConnectionAttemptStatus.SUCCESSFUL) + if (fromDB != null) { + def lastObservation = sql.firstRow("select STATUS from HOST_ATTEMPTS where DESTINATION=${dest.toBase64()} order by TSTAMP desc limit 1") + if (lastObservation != null) + profile = new HostMCProfile(fromDB, ConnectionAttemptStatus.valueOf(lastObservation.STATUS)) + } profiles.put(dest, profile) allHosts.add(dest) log.fine("Loaded profile for ${dest.toBase32()} $profile") diff --git a/core/src/main/groovy/com/muwire/core/hostcache/HostMCProfile.groovy b/core/src/main/groovy/com/muwire/core/hostcache/HostMCProfile.groovy index d2c8a6a7..a1f88f1c 100644 --- a/core/src/main/groovy/com/muwire/core/hostcache/HostMCProfile.groovy +++ b/core/src/main/groovy/com/muwire/core/hostcache/HostMCProfile.groovy @@ -40,11 +40,18 @@ class HostMCProfile { boolean successfulAttempt /** - * constructs an "optimistic" predictor for newly discovered hosts. + * Constructs an optimistic predictor for newly discovered hosts */ HostMCProfile() { + this(ConnectionAttemptStatus.SUCCESSFUL) + } + + /** + * constructs a predictor with default values and the specified state + */ + HostMCProfile(ConnectionAttemptStatus state) { this.hasHistory = false - this.state = ConnectionAttemptStatus.SUCCESSFUL + this.state = state S = new Probability[3] R = new Probability[3] F = new Probability[3] @@ -67,7 +74,7 @@ class HostMCProfile { "RF:${R[2].probability},"+ "FS:${F[0].probability},"+ "FR:${F[1].probability},"+ - "FF:${F[2].probability}" + "FF:${F[2].probability} " + state Arrays.sort(S) S[1].probability += S[0].probability @@ -134,7 +141,7 @@ class HostMCProfile { "RF:${R[2].probability},"+ "FS:${F[0].probability},"+ "FR:${F[1].probability},"+ - "FF:${F[2].probability}" + "FF:${F[2].probability} " + state Arrays.sort(S) S[1].probability += S[0].probability @@ -193,7 +200,7 @@ class HostMCProfile { @Override public String toString() { - toString + " state " + state + toString } private static class Probability implements Comparable {