[concurrency-interest] Migrating DNS problem to j.u.c

Tim Peierls tim at peierls.net
Tue Dec 12 10:01:59 EST 2006


The minimal approach would be to change pool.assign(...) to pool.execute(...),
where pool is now an Executor, externally managed by the parent class, say.
The pool implementation could be a fixed size ThreadPoolExecutor with a size
proportional to the number of processors on the server (a small constant
factor). For example,

    private final ExecutorService pool = Executors.newFixedThreadPool(2 *
Runtime.getRuntime().availableProcessors());

You need to make the various collections thread-safe, using
ConcurrentHashMap, for example -- that includes "domainsVisitados" and
"collection" (if the former is a set, you can use ConcurrentHashMap<String,
Boolean>).

And you can and should make the three private fields of SearchDomain final,
since SearchDomain is basically immutable.

Is ictx thread-safe? If not, you'll have to synchronize access to it.

--tim


On 12/11/06, robert lazarski <robertlazarski at gmail.com> wrote:
>
> Hi all,
>
> I've been tasked with converting a DNS problem from a GPL thread pool
> from around 2000 to j.u.c. The current code ignores exceptions, is not
> very maintainable and I've decided that it would be best to start
> over. One caveat: The program uses recursion and a new thread for
> every iteration.
>
> The goal however necessary is to get every 'A record' , determine if
> the 'A record' is actually a 'DN record' , and if so, start over -
> perhaps recursively. The result being a List of every host and ip from
> a given DNS server.
>
> I'm looking at the jcip TransformingSequential example, and thinking
> how I could apply that to this problem. I've also been thinking about
> perhaps dividing the 'A record' and 'DN record' into different
> CompletionService's . Nothing seems right yet. Here's the old code, an
> inner class - the only relevant thing in the parent class is that it
> inits the 'DirContext ictx'. Any ideas?
>
>     // fugly code
>     private class SearchDomain implements Runnable {
>
>         private boolean recursive;
>         private Map<String, String> collection;
>         private String domain;
>
>         public SearchDomain(String domain, Map<String, String> collection,
>                 boolean recursive) {
>
>             this.domain = domain;
>             this.collection = collection;
>             this.recursive = recursive;
>         }
>
>         public void run() {
>
>             if (!domainsVisitados.contains(domain)) {
>
>                 domainsVisitados.add(domain);
>
>                 try {
>                     NamingEnumeration hostEnumeration = ictx.list(domain);
>
>                     // get the hosts with their names and ip's.
>                     while (hostEnumeration.hasMore()) {
>
>                         String host = null;
>                         host = ((NameClassPair) hostEnumeration.next())
>                                 .getNameInNamespace();
>
>                         // Get 'A Records'
>                         Attributes a = ictx.getAttributes(host,
>                                 new String[] { "A" });
>
>                         if (a.get("A") != null) {
>                             String ip = a.get("A").get().toString();
>                             collection.put(host, ip);
>                         }
>                         if (recursive) {
>                             // find which ones are DNS e create new thread
>                             Attributes aDNS = ictx.getAttributes(host,
>                                     new String[] { "NS" });
>                             NamingEnumeration allDNS = aDNS.getAll();
>
>                             while (allDNS.hasMore()) {
>                                 Attribute attr = (Attribute) allDNS.next
> ();
>                                 NamingEnumeration values = attr.getAll();
>
>                                 // put the host and ip into the
> 'collection'
>                                 while (values.hasMore()) {
>                                     String dns = values.next().toString();
>                                     pool.assign(new SearchDomain(dns,
>                                             collection, recursive));
>                                 }
>                             }
>                         }
>                     }
>                 } catch (Exception e) {
>                     e.printStackTrace();
>                 }
>             }
>         }
>     }
> _______________________________________________
> Concurrency-interest mailing list
> Concurrency-interest at altair.cs.oswego.edu
> http://altair.cs.oswego.edu/mailman/listinfo/concurrency-interest
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: /pipermail/attachments/20061212/742da154/attachment-0001.html 


More information about the Concurrency-interest mailing list