This project has retired. For details please refer to its Attic page.
Source code
001/*
002 * Licensed to the Apache Software Foundation (ASF) under one
003 * or more contributor license agreements.  See the NOTICE file
004 * distributed with this work for additional information
005 * regarding copyright ownership.  The ASF licenses this file
006 * to you under the Apache License, Version 2.0 (the
007 * "License"); you may not use this file except in compliance
008 * with the License.  You may obtain a copy of the License at
009 *
010 *   http://www.apache.org/licenses/LICENSE-2.0
011 *
012 * Unless required by applicable law or agreed to in writing,
013 * software distributed under the License is distributed on an
014 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015 * KIND, either express or implied.  See the License for the
016 * specific language governing permissions and limitations
017 * under the License.
018 */
019package org.apache.reef.io.network.naming;
020
021import org.apache.reef.exception.evaluator.NetworkException;
022import org.apache.reef.io.network.naming.exception.NamingException;
023import org.apache.reef.io.network.naming.parameters.NameResolverCacheTimeout;
024import org.apache.reef.io.network.naming.parameters.NameResolverRetryCount;
025import org.apache.reef.io.network.naming.parameters.NameResolverRetryTimeout;
026import org.apache.reef.tang.annotations.Parameter;
027import org.apache.reef.util.cache.Cache;
028import org.apache.reef.wake.Identifier;
029
030import javax.inject.Inject;
031import java.net.InetSocketAddress;
032import java.util.concurrent.Callable;
033import java.util.logging.Level;
034import java.util.logging.Logger;
035
036/**
037 * NameResolver looking up local name server.
038 * This class should be used when the NameServer is started locally.
039 */
040public final class LocalNameResolverImpl implements NameResolver {
041
042  private static final Logger LOG = Logger.getLogger(LocalNameResolverImpl.class.getName());
043
044  /**
045   * A local name server.
046   */
047  private final NameServer nameServer;
048
049  /**
050   * A cache for lookup.
051   */
052  private final Cache<Identifier, InetSocketAddress> cache;
053
054  /**
055   * Retry count for lookup.
056   */
057  private final int retryCount;
058
059  /**
060   * Retry timeout for lookup.
061   */
062  private final int retryTimeout;
063
064  @Inject
065  private LocalNameResolverImpl(
066      final NameServer nameServer,
067      @Parameter(NameResolverCacheTimeout.class) final long timeout,
068      @Parameter(NameResolverRetryCount.class) final int retryCount,
069      @Parameter(NameResolverRetryTimeout.class) final int retryTimeout) {
070    this.nameServer = nameServer;
071    this.cache = new NameCache(timeout);
072    this.retryCount = retryCount;
073    this.retryTimeout = retryTimeout;
074  }
075
076  @Override
077  public synchronized void register(final Identifier id, final InetSocketAddress address) throws NetworkException {
078    nameServer.register(id, address);
079  }
080
081  @Override
082  public synchronized void unregister(final Identifier id) throws NetworkException {
083    nameServer.unregister(id);
084  }
085
086  @Override
087  public void close() throws Exception {
088  }
089
090  @Override
091  public InetSocketAddress lookup(final Identifier id) throws Exception {
092    return cache.get(id, new Callable<InetSocketAddress>() {
093      @Override
094      public InetSocketAddress call() throws Exception {
095        final int origRetryCount = LocalNameResolverImpl.this.retryCount;
096        int retriesLeft = origRetryCount;
097        while (true) {
098          try {
099            final InetSocketAddress addr = nameServer.lookup(id);
100            if (addr == null) {
101              throw new NullPointerException("The lookup of the address in the nameServer returned null for id " + id);
102            } else {
103              return addr;
104            }
105          } catch (final NullPointerException e) {
106            if (retriesLeft <= 0) {
107              throw new NamingException("Cannot find " + id + " from the name server", e);
108            } else {
109              final int retTimeout = LocalNameResolverImpl.this.retryTimeout
110                  * (origRetryCount - retriesLeft + 1);
111              LOG.log(Level.WARNING,
112                  "Caught Naming Exception while looking up " + id
113                      + " with Name Server. Will retry " + retriesLeft
114                      + " time(s) after waiting for " + retTimeout + " msec.");
115              Thread.sleep(retTimeout);
116              --retriesLeft;
117            }
118          }
119        }
120      }
121    });
122  }
123}