change state on observation even if there is not enough history yet. Initialize MC with last state from DB

pull/53/head
Zlatin Balevsky 2020-10-24 12:38:18 +01:00
parent e1b2582ca6
commit f5f84899ec
No known key found for this signature in database
GPG Key ID: A72832072D525E41
2 changed files with 18 additions and 7 deletions

View File

@ -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")

View File

@ -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<Probability> {