Update of /cvsroot/freenet/freenet/src/freenet/node
In directory sc8-pr-cvs1:/tmp/cvs-serv7797/freenet/src/freenet/node

Modified Files:
        FailureTable.java 
Log Message:
Wow, lots more changes to FailureTable... accesses, and whatnot are much more 
consistent now, hope toad likes it better than what he did.

Index: FailureTable.java
===================================================================
RCS file: /cvsroot/freenet/freenet/src/freenet/node/FailureTable.java,v
retrieving revision 1.24
retrieving revision 1.25
diff -u -w -r1.24 -r1.25
--- FailureTable.java   3 Nov 2003 05:48:55 -0000       1.24
+++ FailureTable.java   3 Nov 2003 07:52:54 -0000       1.25
@@ -82,7 +82,7 @@
        protected int maxItemsSize;
        protected long maxMillis;
        protected long cpMillis;
-    protected long lastCp = 0;
+    protected long lastCp;
 
     protected Hashtable failedKeys; // of FailureEntry's
     // In both cases, most recent is First(), LRU is Last()
@@ -120,14 +120,8 @@
         
         if (fe == null) {
             fe = new FailureEntry(k, time);
-            fe.failed(hopsToLive, time);
-            failedKeys.put(k, fe);
-            entries.push(fe);
-        } else {
-               entries.remove(fe);
-               fe.failed(hopsToLive, time);
-               entries.push(fe);
         }
+        fe.failed(hopsToLive, time);
     }
 
     /**
@@ -161,7 +155,7 @@
      * Removes the entry for this key.
      */
     public synchronized void remove(Key k) {
-        FailureEntry fe = (FailureEntry) failedKeys.remove(k);
+        FailureEntry fe = (FailureEntry) failedKeys.get(k);
         if (fe != null) {
             fe.remove();
             Core.diagnostics.occurrenceContinuous("failureTableBlocks",
@@ -174,25 +168,17 @@
      */
     public synchronized void checkpoint() {
                lastCp = System.currentTimeMillis();
-               int numEntries = 0;
-               int numItems = 0;
         FailureEntry fe;
                while (entries.size() > maxSize) {
-                       numEntries++;
                        fe = (FailureEntry) entries.shift();
-                       failedKeys.remove(fe.key);
                        Core.diagnostics.occurrenceContinuous("failureTableBlocks",
                                                                                       
           (double) fe.blocks);
+                       fe.remove();
                }
                while (items.size() > maxItemsSize) {
-                       numItems++;
                        FailItem fi = (FailItem) items.shift();
+                       fi.getEntry().removeItem(fi);
                }
-               long itemSubtract = (long)(( numItems - maxItemsSize*.10 ) * 1000);
-               long entrySubtract = (long)(( numEntries - maxSize*.10 ) * 1000);
-               long subtract = Math.max(Math.max(0,itemSubtract),entrySubtract);
-               cpMillis-=subtract==0?-1000:subtract;
-               cpMillis = Math.min(maxMillis/10,cpMillis);
     }
 
     public String getCheckpointName() {
@@ -210,7 +196,7 @@
                pw.println("<b>Current Key-HTL pairs:</b> " + items.size() + "<br>");
                pw.println("<b>Seconds Entries Persist:</b> " + maxMillis / 1000 + 
"<br>");
                pw.println("<b>Seconds Between Purges:</b> " + cpMillis / 1000 + 
"<br>");
-        pw.println("<table border=1>");
+        pw.println("<font size=-1><table border=1>");
         pw.println("<tr><th>Key</th><th>Blocked HTLs</th>" 
                    + "<th>Ages</th><th># of Blocks</th>" 
                    + "<th>Last Hit</th></tr>");
@@ -220,24 +206,41 @@
             fe = (FailureEntry)e.nextElement();
             fe.toHtml(pw, time);
         }
-        pw.println("</table>");
+        pw.println("</table></font>");
     }
     
     protected class FailItem extends Item {
-       FailureEntry fe;
-               public int hopsToLive;
+       private FailureEntry fe;
+               private int hopsToLive;
                private long time;
                
-               public FailItem(FailureEntry fe, int hopsToLive, long time) {
+               public FailItem(FailureEntry fe) {
                        this.fe = fe;
-                       this.hopsToLive = hopsToLive;
-                       this.time = time;
                }
                
-               boolean matches(int htl, long now) {
-                       if(htl <= hopsToLive && time + maxMillis < now) {
+               public FailureEntry getEntry() {
+                       return fe; 
+               }
+               
+               public long shouldFail(int hopsToLive, long time) {
+                       if (time + maxMillis > time && hopsToLive >= hopsToLive) {
+                               return time;
+                       }
+                       return -1;
+               }
+               
+               public boolean failed(int hopsToLive, long time) {
+                       if ( this.hopsToLive == hopsToLive ) {
+                               this.time = time;
+                               items.remove(this);
+                               items.push(this); // MRU
                                return true;
-                       } else return false;
+                       }
+                       return false;
+               }
+               
+               public void remove() {
+                       items.remove(this);
                }
 
                /**
@@ -262,49 +265,36 @@
         LinkedList myItems;
         // java.util.LinkedList, NOT DoublyLinkedList, because we are already in 
items DLList
         
-        public boolean shouldIgnoreDNF(int hopsToLive) {
-               return (hopsToLive <= highestHtl && failures >1);
-        }
-        
         public FailureEntry(Key key, long time) {
                myItems = new LinkedList();
             this.key = key;
             this.blocks = 0;
             lastHit = time;
             highestHtl = 0;
-        }
-        
-        /**
-                * @param pw
-                */
-               public void toHtml(PrintWriter pw, long time) {
-                       boolean active = (time - lastFail) < maxMillis;
-                       pw.println("<tr><td rowspan="+myItems.size()+"><font color=\"" 
+ (active ? "red" : "green") 
-                                               + "\">" + key + "</font></td>");
-                       Iterator i = myItems.iterator();
-                       if ( i.hasNext() ) {
-                               ((FailItem)(i.next())).toHtml(pw,time);
-                       }
-                       pw.println("<td rowspan="+myItems.size()+">" + blocks 
-                                               + "</td><td 
rowspan="+myItems.size()+">" + new Date(lastHit) + "</td></tr>");
-                       for ( ; i.hasNext(); ) {
-                               pw.print("<tr>");
-                               ((FailItem)(i.next())).toHtml(pw,time);
-                               pw.print("</tr>\n");
-                       }
+                       failedKeys.put(key,this);
                }
 
                /**
                 * 
                 */
                public void remove() {
-                       // TODO Auto-generated method stub
                        for(Iterator i = myItems.iterator();i.hasNext();) {
-                               FailItem fi = (FailItem)(i.next());
-                               items.remove(fi);
+                               ((FailItem)(i.next())).remove();
                        }
                        myItems = null;
                        entries.remove(this);
+                       failedKeys.remove(key);
+               }
+               
+               /** removes an item from the internal list
+                * does NOT remove it from the master list
+                * @param fi the item to remove
+                */
+               public void removeItem(FailItem fi) {
+                       myItems.remove(fi);
+                       if ( myItems.size() == 0 ) {
+                               remove();
+                       }
                }
                
                /**
@@ -320,25 +310,17 @@
                        lastFail = time;
                        boolean found = false;
                        for(Iterator i = myItems.iterator();i.hasNext();) {
-                               FailItem fi = (FailItem)(i.next());
-                               if(fi.hopsToLive == hopsToLive) {
-                                       fi.time = time;
-                                       items.remove(fi);
-                                       items.push(fi); // MRU
-                                       found = true;
+                               if ( 
found=((FailItem)i.next()).failed(hopsToLive,time) ) {
                                        break;
                                }
-                               if(fi.time + maxMillis < time) {
-                                       // Remove it
-                                       items.remove(fi);
-                                       i.remove();
-                               }
                        }
                        if(!found) {
-                               FailItem fi = new FailItem(this, hopsToLive, time);
-                               items.push(fi);
+                               FailItem fi = new FailItem(this);
+                               fi.failed(hopsToLive,time);
                                myItems.addLast(fi);
                        }
+                       entries.remove(this);
+                       entries.push(this);
                }
                
                /**
@@ -346,29 +328,22 @@
                 * @return  The time at which the query that failed to find the key
                 *          occured, or < 0 if no such query is known.
                 */
-               public long shouldFail(int hopsToLive, long now) {
+               public long shouldFail(int hopsToLive, long time) {
+                       long failedTime=-1;
                        for(Iterator i = myItems.iterator();i.hasNext();) {
-                               FailItem fi = (FailItem)(i.next());
-                               if(fi.time + maxMillis < now) {
-                                       items.remove(fi);
-                                       i.remove();
-                                       continue;
-                               }
-                               if(fi.hopsToLive >= hopsToLive) {
+                               if( 
(failedTime=((FailItem)i.next()).shouldFail(hopsToLive,time)) >= 0 ) {
                                        blocks++;
                                        
Core.diagnostics.occurrenceContinuous("timeBetweenFailedRequests",
-                                                                                      
                                                          (double)(now - lastHit));
-                                       lastHit = now;
-                                       return fi.time;
+                                                                                      
                                                          (double)(time - lastHit));
+                                       lastHit = time;
+                                       break;
                                }
                        }
-                       return -1;
+                       return failedTime;
                }
-               /**
-                * @param item
-                */
-               public void add(FailItem item) {
-                       myItems.add(item);
+
+               public boolean shouldIgnoreDNF(int hopsToLive) {
+                       return (hopsToLive <= highestHtl && failures >1);
                }
 
                public int compareTo(Object o) {
@@ -378,6 +353,26 @@
             long ot = ((FailureEntry) o).lastFail;
             return lastFail == ot ? 0 : (lastFail > ot ? -1 : 1);
         }
+
+               /**
+                * @param pw
+                */
+               public void toHtml(PrintWriter pw, long time) {
+                       boolean active = (time - lastFail) < maxMillis;
+                       pw.println("<tr><td rowspan="+myItems.size()+"><font color=\"" 
+ (active ? "red" : "green") 
+                                               + "\">" + key + "</font></td>");
+                       Iterator i = myItems.iterator();
+                       if ( i.hasNext() ) {
+                               ((FailItem)(i.next())).toHtml(pw,time);
+                       }
+                       pw.println("<td rowspan="+myItems.size()+">" + blocks 
+                                               + "</td><td 
rowspan="+myItems.size()+">" + new Date(lastHit) + "</td></tr>");
+                       for ( ; i.hasNext(); ) {
+                               pw.print("<tr>");
+                               ((FailItem)(i.next())).toHtml(pw,time);
+                               pw.print("</tr>\n");
+                       }
+               }
 
         public String toString() {
             return key.toString();

_______________________________________________
cvs mailing list
[EMAIL PROTECTED]
http://dodo.freenetproject.org/cgi-bin/mailman/listinfo/cvs

Reply via email to