[metrics-db/master] Remove unused parts of ConsensusStatsFileHandler.


Mon Feb 28 22:06:02 UTC 2011


commit a605c4b5c31df3648501b1e89a624cd3a01633c4
Author: Karsten Loesing <karsten.loesing at gmx.net>
Date:   Thu Jan 6 17:19:18 2011 +0100

    Remove unused parts of ConsensusStatsFileHandler.
---
 .../ernie/db/ConsensusStatsFileHandler.java        |  343 ++------------------
 .../torproject/ernie/db/RelayDescriptorParser.java |    4 -
 2 files changed, 19 insertions(+), 328 deletions(-)

diff --git a/src/org/torproject/ernie/db/ConsensusStatsFileHandler.java b/src/org/torproject/ernie/db/ConsensusStatsFileHandler.java
index 469e239..81e4e3a 100644
--- a/src/org/torproject/ernie/db/ConsensusStatsFileHandler.java
+++ b/src/org/torproject/ernie/db/ConsensusStatsFileHandler.java
@@ -20,27 +20,6 @@ import java.util.logging.*;
 public class ConsensusStatsFileHandler {
 
   /**
-   * Intermediate results file holding the number of relays with Exit,
-   * Fast, Guard, Running, and Stable flags per consensus.
-   */
-  private File consensusStatsRawFile;
-
-  /**
-   * Number of relays in a given consensus with Exit, Fast, Guard,
-   * Running, and Stable flags set. Map keys are consensus valid-after
-   * times formatted as "yyyy-MM-dd HH:mm:ss", map values are lines as
-   * read from <code>stats/consensus-stats-raw</code>.
-   */
-  private SortedMap<String, String> relaysRaw;
-
-  /**
-   * Modification flag for <code>relaysRaw</code>. This flag is used to
-   * decide whether the contents of <code>relaysRaw</code> need to be
-   * written to disk during <code>writeFiles</code>.
-   */
-  private boolean relaysRawModified;
-
-  /**
    * Intermediate results file holding the number of running bridges per
    * bridge status.
    */
@@ -54,28 +33,6 @@ public class ConsensusStatsFileHandler {
   private SortedMap<String, String> bridgesRaw;
 
   /**
-   * Modification flag for <code>bridgesRaw</code>. This flag is used to
-   * decide whether the contents of <code>bridgesRaw</code> need to be
-   * written to disk during <code>writeFiles</code>.
-   */
-  private boolean bridgesRawModified;
-
-  /**
-   * Final results file holding the average number of relays with Exit,
-   * Fast, Guard, Running, and Stable flags set and the number of running
-   * bridges per day.
-   */
-  private File consensusStatsFile;
-
-  /**
-   * Average number of relays with Exit, Fast, Guard, Running, and Stable
-   * flags set per day. Map keys are dates formatted as "yyyy-MM-dd", map
-   * values are lines as written to <code>stats/consensus-stats</code>
-   * without the last column that contains the number of running bridges.
-   */
-  private SortedMap<String, String> relaysPerDay;
-
-  /**
    * Average number of running bridges per day. Map keys are dates
    * formatted as "yyyy-MM-dd", map values are the last column as written
    * to <code>stats/consensus-stats</code>.
@@ -87,7 +44,7 @@ public class ConsensusStatsFileHandler {
    */
   private Logger logger;
 
-  private int relayResultsAdded = 0, bridgeResultsAdded = 0;
+  private int bridgeResultsAdded = 0;
 
   /* Database connection string. */
   private String connectionURL = null;
@@ -102,16 +59,12 @@ public class ConsensusStatsFileHandler {
 
     /* Initialize local data structures to hold intermediate and final
      * results. */
-    this.relaysPerDay = new TreeMap<String, String>();
     this.bridgesPerDay = new TreeMap<String, String>();
-    this.relaysRaw = new TreeMap<String, String>();
     this.bridgesRaw = new TreeMap<String, String>();
 
     /* Initialize file names for intermediate and final results files. */
-    this.consensusStatsRawFile = new File("stats/consensus-stats-raw");
     this.bridgeConsensusStatsRawFile = new File(
         "stats/bridge-consensus-stats-raw");
-    this.consensusStatsFile = new File("stats/consensus-stats");
 
     /* Initialize database connection string. */
     this.connectionURL = connectionURL;
@@ -120,38 +73,6 @@ public class ConsensusStatsFileHandler {
     this.logger = Logger.getLogger(
         ConsensusStatsFileHandler.class.getName());
 
-    /* Read in number of relays with flags set per consensus. */
-    if (this.consensusStatsRawFile.exists()) {
-      try {
-        this.logger.fine("Reading file "
-            + this.consensusStatsRawFile.getAbsolutePath() + "...");
-        BufferedReader br = new BufferedReader(new FileReader(
-            this.consensusStatsRawFile));
-        String line = null;
-        while ((line = br.readLine()) != null) {
-          if (line.startsWith("date")) {
-            /* Skip headers. */
-            continue;
-          }
-          String[] parts = line.split(",");
-          if (parts.length != 6) {
-            this.logger.warning("Corrupt line '" + line + "' in file "
-                + this.consensusStatsRawFile.getAbsolutePath()
-                + "! Aborting to read this file!");
-            break;
-          }
-          String dateTime = parts[0];
-          this.relaysRaw.put(dateTime, line);
-        }
-        br.close();
-        this.logger.fine("Finished reading file "
-            + this.consensusStatsRawFile.getAbsolutePath() + ".");
-      } catch (IOException e) {
-        this.logger.log(Level.WARNING, "Failed to read file "
-            + this.consensusStatsRawFile.getAbsolutePath() + "!", e);
-      }
-    }
-
     /* Read in number of running bridges per bridge status. */
     if (this.bridgeConsensusStatsRawFile.exists()) {
       try {
@@ -184,75 +105,6 @@ public class ConsensusStatsFileHandler {
             e);
       }
     }
-
-    /* Read in previous results on average numbers of relays and running
-     * bridges per day. */
-    if (this.consensusStatsFile.exists()) {
-      try {
-        this.logger.fine("Reading file "
-            + this.consensusStatsFile.getAbsolutePath() + "...");
-        BufferedReader br = new BufferedReader(new FileReader(
-            this.consensusStatsFile));
-        String line = null;
-        while ((line = br.readLine()) != null) {
-          if (line.startsWith("date")) {
-            /* Skip headers. */
-            continue;
-          }
-          String[] parts = line.split(",");
-          if (parts.length != 7) {
-            this.logger.warning("Corrupt line '" + line + "' in file "
-                + this.consensusStatsFile.getAbsolutePath()
-                + "! Aborting to read this file!");
-            break;
-          }
-          String date = parts[0];
-          /* Split line into relay and bridge part; the relay part ends
-           * with the last comma (excluding) and the bridge part starts at
-           * that comma (including). */
-          String relayPart = line.substring(0, line.lastIndexOf(","));
-          String bridgePart = line.substring(line.lastIndexOf(","));
-          if (!relayPart.endsWith(",NA,NA,NA,NA,NA")) {
-            this.relaysPerDay.put(date, relayPart);
-          }
-          if (!bridgePart.equals(",NA")) {
-            this.bridgesPerDay.put(date, bridgePart);
-          }
-        }
-        br.close();
-        this.logger.fine("Finished reading file "
-            + this.consensusStatsFile.getAbsolutePath() + ".");
-      } catch (IOException e) {
-        this.logger.log(Level.WARNING, "Failed to write file "
-            + this.consensusStatsFile.getAbsolutePath() + "!", e);
-      }
-    }
-
-    /* Set modification flags to false. */
-    this.relaysRawModified = this.bridgesRawModified = false;
-  }
-
-  /**
-   * Adds the intermediate results of the number of relays with certain
-   * flags in a given consensus to the existing observations.
-   */
-  public void addConsensusResults(String validAfter, int exit, int fast,
-      int guard, int running, int stable) {
-    String line = validAfter + "," + exit + "," + fast + "," + guard + ","
-        + running + "," + stable;
-    if (!this.relaysRaw.containsKey(validAfter)) {
-      this.logger.finer("Adding new relay numbers: " + line);
-      this.relaysRaw.put(validAfter, line);
-      this.relaysRawModified = true;
-      this.relayResultsAdded++;
-    } else if (!line.equals(this.relaysRaw.get(validAfter))) {
-      this.logger.warning("The numbers of relays with Exit, Fast, "
-        + "Guard, Running, and Stable flag we were just given (" + line
-        + ") are different from what we learned before ("
-        + this.relaysRaw.get(validAfter) + ")! Overwriting!");
-      this.relaysRaw.put(validAfter, line);
-      this.relaysRawModified = true;
-    }
   }
 
   /**
@@ -264,7 +116,6 @@ public class ConsensusStatsFileHandler {
     if (!this.bridgesRaw.containsKey(published)) {
       this.logger.finer("Adding new bridge numbers: " + line);
       this.bridgesRaw.put(published, line);
-      this.bridgesRawModified = true;
       this.bridgeResultsAdded++;
     } else if (!line.equals(this.bridgesRaw.get(published))) {
       this.logger.warning("The numbers of running bridges we were just "
@@ -272,7 +123,6 @@ public class ConsensusStatsFileHandler {
         + "before (" + this.bridgesRaw.get(published) + ")! "
         + "Overwriting!");
       this.bridgesRaw.put(published, line);
-      this.bridgesRawModified = true;
     }
   }
 
@@ -286,57 +136,6 @@ public class ConsensusStatsFileHandler {
      * this run? */
     boolean writeConsensusStats = false;
 
-    /* Go through raw observations of numbers of relays in consensuses,
-     * calculate averages per day, and add these averages to final
-     * results. */
-    if (!this.relaysRaw.isEmpty()) {
-      String tempDate = null;
-      int exit = 0, fast = 0, guard = 0, running = 0, stable = 0,
-          consensuses = 0;
-      Iterator<String> it = this.relaysRaw.values().iterator();
-      boolean haveWrittenFinalLine = false;
-      while (it.hasNext() || !haveWrittenFinalLine) {
-        String next = it.hasNext() ? it.next() : null;
-        /* Finished reading a day or even all lines? */
-        if (tempDate != null && (next == null
-            || !next.substring(0, 10).equals(tempDate))) {
-          /* Only write results if we have seen at least half of all
-           * consensuses. */
-          if (consensuses >= 12) {
-            String line = tempDate + "," + (exit / consensuses) + ","
-                + (fast/ consensuses) + "," + (guard/ consensuses) + ","
-                + (running/ consensuses) + "," + (stable/ consensuses);
-            /* Are our results new? */
-            if (!this.relaysPerDay.containsKey(tempDate)) {
-              this.logger.finer("Adding new average relay numbers: "
-                  + line);
-              this.relaysPerDay.put(tempDate, line);
-              writeConsensusStats = true;
-            } else if (!line.equals(this.relaysPerDay.get(tempDate))) {
-              this.logger.finer("Replacing existing average relay numbers "
-                  + "(" + this.relaysPerDay.get(tempDate) + " with new "
-                  + "numbers: " + line);
-              this.relaysPerDay.put(tempDate, line);
-              writeConsensusStats = true;
-            }
-          }
-          exit = fast = guard = running = stable = consensuses = 0;
-          haveWrittenFinalLine = (next == null);
-        }
-        /* Sum up number of relays with given flags. */
-        if (next != null) {
-          String[] parts = next.split(",");
-          tempDate = next.substring(0, 10);
-          consensuses++;
-          exit += Integer.parseInt(parts[1]);
-          fast += Integer.parseInt(parts[2]);
-          guard += Integer.parseInt(parts[3]);
-          running += Integer.parseInt(parts[4]);
-          stable += Integer.parseInt(parts[5]);
-        }
-      }
-    }
-
     /* Go through raw observations of numbers of running bridges in bridge
      * statuses, calculate averages per day, and add these averages to
      * final results. */
@@ -380,109 +179,24 @@ public class ConsensusStatsFileHandler {
       }
     }
 
-    /* Write raw numbers of relays with flags set to disk. */
-    if (this.relaysRawModified) {
-      try {
-        this.logger.fine("Writing file "
-            + this.consensusStatsRawFile.getAbsolutePath() + "...");
-        this.consensusStatsRawFile.getParentFile().mkdirs();
-        BufferedWriter bw = new BufferedWriter(new FileWriter(
-            this.consensusStatsRawFile));
-        bw.append("datetime,exit,fast,guard,running,stable\n");
-        for (String line : this.relaysRaw.values()) {
-          bw.append(line + "\n");
-        }
-        bw.close();
-        this.logger.fine("Finished writing file "
-            + this.consensusStatsRawFile.getAbsolutePath() + ".");
-      } catch (IOException e) {
-        this.logger.log(Level.WARNING, "Failed to write file "
-            + this.consensusStatsRawFile.getAbsolutePath() + "!", e);
-      }
-    } else {
-      this.logger.fine("Not writing file "
-          + this.consensusStatsRawFile.getAbsolutePath() + ", because "
-          + "nothing has changed.");
-    }
-
     /* Write raw numbers of running bridges to disk. */
-    if (this.bridgesRawModified) {
-      try {
-        this.logger.fine("Writing file "
-            + this.bridgeConsensusStatsRawFile.getAbsolutePath() + "...");
-        this.bridgeConsensusStatsRawFile.getParentFile().mkdirs();
-        BufferedWriter bw = new BufferedWriter(
-            new FileWriter(this.bridgeConsensusStatsRawFile));
-        bw.append("datetime,brunning\n");
-        for (String line : this.bridgesRaw.values()) {
-          bw.append(line + "\n");
-        }
-        bw.close();
-        this.logger.fine("Finished writing file "
-            + this.bridgeConsensusStatsRawFile.getAbsolutePath() + ".");
-      } catch (IOException e) {
-        this.logger.log(Level.WARNING, "Failed to write file "
-            + this.bridgeConsensusStatsRawFile.getAbsolutePath() + "!",
-            e);
+    try {
+      this.logger.fine("Writing file "
+          + this.bridgeConsensusStatsRawFile.getAbsolutePath() + "...");
+      this.bridgeConsensusStatsRawFile.getParentFile().mkdirs();
+      BufferedWriter bw = new BufferedWriter(
+          new FileWriter(this.bridgeConsensusStatsRawFile));
+      bw.append("datetime,brunning\n");
+      for (String line : this.bridgesRaw.values()) {
+        bw.append(line + "\n");
       }
-    } else {
-      this.logger.fine("Not writing file "
-          + this.bridgeConsensusStatsRawFile.getAbsolutePath()
-          + ", because nothing has changed.");
-    }
-
-    /* Write final results of relays with flags set and running bridges
-     * to disk. */
-    if (writeConsensusStats) {
-      try {
-        this.logger.fine("Writing file "
-            + this.consensusStatsFile.getAbsolutePath() + "...");
-        this.consensusStatsFile.getParentFile().mkdirs();
-        BufferedWriter bw = new BufferedWriter(new FileWriter(
-            this.consensusStatsFile));
-        bw.append("date,exit,fast,guard,running,stable,brunning\n");
-        /* Iterate over all days, including those for which we don't have
-         * observations for which we add NA's to all columns. */
-        SortedSet<String> allDates = new TreeSet<String>();
-        allDates.addAll(this.relaysPerDay.keySet());
-        allDates.addAll(this.bridgesPerDay.keySet());
-        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
-        dateFormat.setTimeZone(TimeZone.getTimeZone("UTC"));
-        long firstDateMillis = dateFormat.parse(allDates.first()).
-            getTime();
-        long lastDateMillis = dateFormat.parse(allDates.last()).getTime();
-        long currentDateMillis = firstDateMillis;
-        while (currentDateMillis <= lastDateMillis) {
-          /* Write observations about relays, bridges, both, or none of
-           * them. */
-          String date = dateFormat.format(currentDateMillis);
-          if (this.relaysPerDay.containsKey(date)) {
-            bw.append(this.relaysPerDay.get(date));
-          } else {
-            bw.append(date + ",NA,NA,NA,NA,NA");
-          }
-          if (this.bridgesPerDay.containsKey(date)) {
-            bw.append(this.bridgesPerDay.get(date) + "\n");
-          } else {
-            bw.append(",NA\n");
-          }
-          /* Advance by 1 day. */
-          currentDateMillis += 24L * 60L * 60L * 1000L;
-        }
-        bw.close();
-        this.logger.fine("Finished writing file "
-            + this.consensusStatsFile.getAbsolutePath() + ".");
-      } catch (IOException e) {
-        this.logger.log(Level.WARNING, "Failed to write file "
-            + this.consensusStatsFile.getAbsolutePath() + "!", e);
-      } catch (ParseException e) {
-        this.logger.log(Level.WARNING, "Failed to write file "
-            + this.consensusStatsFile.getAbsolutePath() + "!", e);
-      }
-    } else {
-      this.logger.fine("Not writing file "
-          + this.consensusStatsFile.getAbsolutePath()
-          + ", because nothing has changed.");
+      bw.close();
+      this.logger.fine("Finished writing file "
+          + this.bridgeConsensusStatsRawFile.getAbsolutePath() + ".");
+    } catch (IOException e) {
+      this.logger.log(Level.WARNING, "Failed to write file "
+          + this.bridgeConsensusStatsRawFile.getAbsolutePath() + "!",
+          e);
     }
 
     /* Add average number of bridges per day to the database. */
@@ -538,34 +252,15 @@ public class ConsensusStatsFileHandler {
       }
     }
 
-    /* Set modification flags to false again. */
-    this.relaysRawModified = this.bridgesRawModified = false;
-
     /* Write stats. */
     StringBuilder dumpStats = new StringBuilder("Finished writing "
-        + "statistics on relay consensuses and bridge statuses to disk.\n"
-        + "Added " + this.relayResultsAdded + " relay consensus(es) and "
-        + this.bridgeResultsAdded + " bridge status(es) in this "
+        + "statistics on bridge network statuses to disk.\nAdded "
+        + this.bridgeResultsAdded + " bridge network status(es) in this "
         + "execution.");
     long now = System.currentTimeMillis();
     SimpleDateFormat dateTimeFormat =
         new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
     dateTimeFormat.setTimeZone(TimeZone.getTimeZone("UTC"));
-    if (this.relaysRaw.isEmpty()) {
-      dumpStats.append("\nNo relay consensus known yet.");
-    } else {
-      dumpStats.append("\nLast known relay consensus was published "
-          + this.relaysRaw.lastKey() + ".");
-      try {
-        if (now - 6L * 60L * 60L * 1000L > dateTimeFormat.parse(
-            this.relaysRaw.lastKey()).getTime()) {
-          logger.warning("Last known relay consensus is more than 6 hours "
-              + "old: " + this.relaysRaw.lastKey());
-        }
-      } catch (ParseException e) {
-         /* Can't parse the timestamp? Whatever. */
-      }
-    }
     if (this.bridgesRaw.isEmpty()) {
       dumpStats.append("\nNo bridge status known yet.");
     } else {
diff --git a/src/org/torproject/ernie/db/RelayDescriptorParser.java b/src/org/torproject/ernie/db/RelayDescriptorParser.java
index a1f6cd3..187bddb 100644
--- a/src/org/torproject/ernie/db/RelayDescriptorParser.java
+++ b/src/org/torproject/ernie/db/RelayDescriptorParser.java
@@ -217,10 +217,6 @@ public class RelayDescriptorParser {
               this.bsfh.addHashedRelay(hashedRelayIdentity);
             }
           }
-          if (this.csfh != null) {
-            this.csfh.addConsensusResults(validAfterTime, exit, fast,
-                guard, running, stable);
-          }
           if (this.rdd != null) {
             this.rdd.haveParsedConsensus(validAfterTime, dirSources,
                 serverDescriptors);



More information about the tor-commits mailing list