Changeset 32efa2e


Ignore:
Timestamp:
Jun 15, 2017 3:48:22 PM (3 years ago)
Author:
zzz <zzz@…>
Branches:
master
Children:
e21479a
Parents:
8216b68
Message:

DNS (ticket #1998):

  • Data: Cache hostname lookups in RouterAddress?
  • Job Queue: Make search jobs droppable
  • Router: Increase JVM DNS cache time
  • Util: Add negative DNS lookup cache, increase cache size
Files:
7 edited

Legend:

Unmodified
Added
Removed
  • core/java/src/net/i2p/util/Addresses.java

    r8216b68 r32efa2e  
    294294     */
    295295    private static final Map<String, byte[]> _IPAddress;
     296    private static final Map<String, Long> _negativeCache;
     297    private static final long NEG_CACHE_TIME = 60*60*1000L;
    296298
    297299    static {
     
    302304            long min = 256;
    303305            long max = 4096;
    304             // 1024 nominal for 128 MB
    305             size = (int) Math.max(min, Math.min(max, 1 + (maxMemory / (128*1024))));
     306            // 2048 nominal for 128 MB
     307            size = (int) Math.max(min, Math.min(max, 1 + (maxMemory / (64*1024))));
    306308        } else {
    307309            size = 32;
    308310        }
    309311        _IPAddress = new LHMCache<String, byte[]>(size);
     312        _negativeCache = new LHMCache<String, Long>(128);
    310313    }
    311314
     
    330333        }
    331334        if (rv == null) {
     335            synchronized(_negativeCache) {
     336                Long when = _negativeCache.get(host);
     337                if (when != null) {
     338                    if (when.longValue() > System.currentTimeMillis() - NEG_CACHE_TIME)
     339                        return null;
     340                    _negativeCache.remove(host);
     341                }
     342            }
    332343            try {
    333344                rv = InetAddress.getByName(host).getAddress();
     
    338349                    }
    339350                }
    340             } catch (UnknownHostException uhe) {}
     351                // else we do not cache hostnames here, we rely on the JVM
     352            } catch (UnknownHostException uhe) {
     353                synchronized(_negativeCache) {
     354                    _negativeCache.put(host, Long.valueOf(System.currentTimeMillis()));
     355                }
     356            }
    341357        }
    342358        return rv;
     
    358374        if (InetAddressUtils.isIPv4Address(host) || InetAddressUtils.isIPv6Address(host))
    359375            return getIP(host);
     376        synchronized(_negativeCache) {
     377            Long when = _negativeCache.get(host);
     378            if (when != null) {
     379                if (when.longValue() > System.currentTimeMillis() - NEG_CACHE_TIME)
     380                    return null;
     381                _negativeCache.remove(host);
     382            }
     383        }
    360384        byte[] rv = null;
    361385        try {
     
    373397                }
    374398            }
    375         } catch (UnknownHostException uhe) {}
     399        } catch (UnknownHostException uhe) {
     400            synchronized(_negativeCache) {
     401                _negativeCache.put(host, Long.valueOf(System.currentTimeMillis()));
     402            }
     403        }
    376404        return rv;
    377405    }
     
    403431            return Collections.singletonList(brv);
    404432        }
     433        synchronized(_negativeCache) {
     434            Long when = _negativeCache.get(host);
     435            if (when != null) {
     436                if (when.longValue() > System.currentTimeMillis() - NEG_CACHE_TIME)
     437                    return null;
     438                _negativeCache.remove(host);
     439            }
     440        }
    405441        try {
    406442            InetAddress[] addrs = InetAddress.getAllByName(host);
     
    412448            }
    413449            return rv;
    414         } catch (UnknownHostException uhe) {}
     450        } catch (UnknownHostException uhe) {
     451            synchronized(_negativeCache) {
     452                _negativeCache.put(host, Long.valueOf(System.currentTimeMillis()));
     453            }
     454        }
    415455        return null;
    416456    }
     
    552592        synchronized(_IPAddress) {
    553593            _IPAddress.clear();
     594        }
     595        synchronized(_negativeCache) {
     596            _negativeCache.clear();
    554597        }
    555598        if (_ifCache != null) {
  • history.txt

    r8216b68 r32efa2e  
     12017-06-15 zzz
     2 * Data: Cache hostname lookups in RouterAddress (ticket #1998)
     3 * Job Queue: Make search jobs droppable (ticket #1998)
     4 * Router: Increase JVM DNS cache time (ticket #1998)
     5 * Util: Add negative DNS lookup cache, increase cache size (ticket #1998)
     6
    172017-06-12 str4d
    28 * Console:
  • router/java/src/net/i2p/data/router/RouterAddress.java

    r8216b68 r32efa2e  
    5151    private final Properties _options;
    5252    // cached values
    53     private byte[] _ip;
     53    private byte[] _ip = NOT_LOOKED_UP;
    5454    private int _port;
    5555
    5656    public static final String PROP_HOST = "host";
    5757    public static final String PROP_PORT = "port";
     58    private static final byte[] NOT_LOOKED_UP = new byte[0];
    5859
    5960    public RouterAddress() {
     
    214215    /**
    215216     *  Caching version of InetAddress.getByName(getOption("host")).getAddress(), which is slow.
    216      *  Caches numeric host names only.
    217      *  Will resolve but not cache resolution of DNS host names.
     217     *  Caches numeric host names AND DNS host names, and negative caches also.
    218218     *
    219219     *  @return IP or null
    220220     *  @since 0.9.3
    221221     */
    222     public byte[] getIP() {
    223         if (_ip != null)
    224             return _ip;
    225         byte[] rv = null;
    226         String host = getHost();
    227         if (host != null) {
    228             rv = Addresses.getIP(host);
    229             if (rv != null &&
    230                 (InetAddressUtils.isIPv4Address(host) ||
    231                  InetAddressUtils.isIPv6Address(host))) {
    232                 _ip = rv;
    233             }
     222    public synchronized byte[] getIP() {
     223        if (_ip == NOT_LOOKED_UP) {
     224            // Only look up once, even if it fails, so we don't generate excessive DNS lookups.
     225            // The lifetime of a RouterAddress object is a few hours at most,
     226            // it will get republished or expired, so it's OK even for host names.
     227            String host = getHost();
     228            if (host != null)
     229                _ip = Addresses.getIP(host);
     230            else
     231                _ip = null;
    234232        }
    235         return rv;
     233        return _ip;
    236234    }
    237235   
  • router/java/src/net/i2p/router/JobQueue.java

    r8216b68 r32efa2e  
    2828import net.i2p.router.message.HandleGarlicMessageJob;
    2929import net.i2p.router.networkdb.kademlia.HandleFloodfillDatabaseLookupMessageJob;
     30import net.i2p.router.networkdb.kademlia.IterativeSearchJob;
    3031import net.i2p.router.RouterClock;
    3132import net.i2p.util.Clock;
     
    317318            // with encrypted lookups
    318319            //
     320            // ISJ added in 0.9.31, can get backed up due to DNS
     321            //
    319322            // Obviously we can only drop one-shot jobs, not those that requeue
    320323            //
    321324            if (cls == HandleFloodfillDatabaseLookupMessageJob.class ||
    322                 cls == HandleGarlicMessageJob.class) {
     325                cls == HandleGarlicMessageJob.class ||
     326                cls == IterativeSearchJob.class) {
    323327                // this tail drops based on the lag at the tail, which
    324328                // makes no sense...
  • router/java/src/net/i2p/router/Router.java

    r8216b68 r32efa2e  
    118118    public static final String PROP_IB_RANDOM_KEY = TunnelPoolSettings.PREFIX_INBOUND_EXPLORATORY + TunnelPoolSettings.PROP_RANDOM_KEY;
    119119    public static final String PROP_OB_RANDOM_KEY = TunnelPoolSettings.PREFIX_OUTBOUND_EXPLORATORY + TunnelPoolSettings.PROP_RANDOM_KEY;
    120     private final static String DNS_CACHE_TIME = "" + (5*60);
    121120    private static final String EVENTLOG = "eventlog.txt";
    122121    private static final String PROP_JBIGI = "jbigi.loadedResource";
     
    134133            // grumble about sun's java caching DNS entries *forever* by default
    135134            // so lets just keep 'em for a short time
     135            String DNS_CACHE_TIME = Integer.toString(2*60*60);
     136            String DNS_NEG_CACHE_TIME = Integer.toString(30*60);
    136137            System.setProperty("sun.net.inetaddr.ttl", DNS_CACHE_TIME);
    137             System.setProperty("sun.net.inetaddr.negative.ttl", DNS_CACHE_TIME);
     138            System.setProperty("sun.net.inetaddr.negative.ttl", DNS_NEG_CACHE_TIME);
    138139            System.setProperty("networkaddress.cache.ttl", DNS_CACHE_TIME);
    139             System.setProperty("networkaddress.cache.negative.ttl", DNS_CACHE_TIME);
     140            System.setProperty("networkaddress.cache.negative.ttl", DNS_NEG_CACHE_TIME);
    140141        }
    141142        if (System.getProperty("I2P_DISABLE_HTTP_AGENT_OVERRIDE") == null) {
  • router/java/src/net/i2p/router/RouterVersion.java

    r8216b68 r32efa2e  
    1919    public final static String ID = "Monotone";
    2020    public final static String VERSION = CoreVersion.VERSION;
    21     public final static long BUILD = 11;
     21    public final static long BUILD = 12;
    2222
    2323    /** for example "-test" */
  • router/java/src/net/i2p/router/networkdb/kademlia/IterativeSearchJob.java

    r8216b68 r32efa2e  
    5555 * two sesarches in parallel like FOSJ does.
    5656 *
     57 * Public only for JobQueue, not a public API, not for external use.
     58 *
    5759 * @since 0.8.9
    5860 */
    59 class IterativeSearchJob extends FloodSearchJob {
     61public class IterativeSearchJob extends FloodSearchJob {
    6062    /** peers not sent to yet, sorted closest-to-the-routing-key */
    6163    private final SortedSet<Hash> _toTry;
     
    561563
    562564    /**
     565     *  Dropped by the job queue
     566     *  @since 0.9.31
     567     */
     568    @Override
     569    public void dropped() {
     570        failed();
     571    }
     572
     573    /**
    563574     *  Total failure
    564575     */
Note: See TracChangeset for help on using the changeset viewer.