com.twitter.common.net.pool.DynamicHostSet Java Examples

The following examples show how to use com.twitter.common.net.pool.DynamicHostSet. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: TwitterServerSetWatcher.java    From distributedlog with Apache License 2.0 6 votes vote down vote up
/**
 * Registers a monitor to receive change notices for this server set as long as this jvm process is alive.
 *
 * <p>Blocks until the initial server set can be gathered and delivered to the monitor.
 * The monitor will be notified if the membership set or parameters of existing members have
 * changed.
 *
 * @param monitor the server set monitor to call back when the host set changes
 * @throws MonitorException if there is a problem monitoring the host set
 */
public void watch(final ServerSetMonitor monitor)
        throws MonitorException {
    try {
        serverSet.watch(new DynamicHostSet.HostChangeMonitor<ServiceInstance>() {
            @Override
            public void onChange(ImmutableSet<ServiceInstance> serviceInstances) {
                Set<DLSocketAddress> dlServers = Sets.newHashSet();
                for (ServiceInstance serviceInstance : serviceInstances) {
                    Endpoint endpoint = serviceInstance.getAdditionalEndpoints().get("thrift");
                    InetSocketAddress inetAddr =
                            new InetSocketAddress(endpoint.getHost(), endpoint.getPort());
                    int shardId = resolvedFromName ? -1 : serviceInstance.getShard();
                    DLSocketAddress address = new DLSocketAddress(shardId, inetAddr);
                    dlServers.add(address);
                }
                monitor.onChange(ImmutableSet.copyOf(dlServers));
            }
        });
    } catch (DynamicHostSet.MonitorException me) {
        throw new MonitorException("Failed to monitor server set : ", me);
    }
}
 
Example #2
Source File: TwitterServerSetWatcher.java    From distributedlog with Apache License 2.0 6 votes vote down vote up
/**
 * Registers a monitor to receive change notices for this server set as long as this jvm process
 * is alive.  Blocks until the initial server set can be gathered and delivered to the monitor.
 * The monitor will be notified if the membership set or parameters of existing members have
 * changed.
 *
 * @param monitor the server set monitor to call back when the host set changes
 * @throws MonitorException if there is a problem monitoring the host set
 */
public void watch(final ServerSetMonitor monitor)
        throws MonitorException {
    try {
        serverSet.watch(new DynamicHostSet.HostChangeMonitor<ServiceInstance>() {
            @Override
            public void onChange(ImmutableSet<ServiceInstance> serviceInstances) {
                Set<DLSocketAddress> dlServers = Sets.newHashSet();
                for (ServiceInstance serviceInstance : serviceInstances) {
                    Endpoint endpoint = serviceInstance.getAdditionalEndpoints().get("thrift");
                    InetSocketAddress inetAddr =
                            new InetSocketAddress(endpoint.getHost(), endpoint.getPort());
                    int shardId = resolvedFromName ? -1 : serviceInstance.getShard();
                    DLSocketAddress address = new DLSocketAddress(shardId, inetAddr);
                    dlServers.add(address);
                }
                monitor.onChange(ImmutableSet.copyOf(dlServers));
            }
        });
    } catch (DynamicHostSet.MonitorException me) {
        throw new MonitorException("Failed to monitor server set : ", me);
    }
}
 
Example #3
Source File: TestInetNameResolution.java    From distributedlog with Apache License 2.0 5 votes vote down vote up
@Test(timeout = 10000)
public void testInetNameResolution() throws Exception {
    String nameStr = "inet!127.0.0.1:3181";
    final CountDownLatch resolved = new CountDownLatch(1);
    final AtomicBoolean validationFailed = new AtomicBoolean(false);

    NameServerSet serverSet = new NameServerSet(nameStr);
    serverSet.watch(new DynamicHostSet.HostChangeMonitor<ServiceInstance>() {
        @Override
        public void onChange(ImmutableSet<ServiceInstance> hostSet) {
            if (hostSet.size() > 1) {
                logger.error("HostSet has more elements than expected {}", hostSet);
                validationFailed.set(true);
                resolved.countDown();
            } else if (hostSet.size() == 1) {
                ServiceInstance serviceInstance = hostSet.iterator().next();
                Endpoint endpoint = serviceInstance.getAdditionalEndpoints().get("thrift");
                InetSocketAddress address = new InetSocketAddress(endpoint.getHost(), endpoint.getPort());
                if (endpoint.getPort() != 3181) {
                    logger.error("Port does not match the expected port {}", endpoint.getPort());
                    validationFailed.set(true);
                } else if (!address.getAddress().getHostAddress().equals("127.0.0.1")) {
                    logger.error("Host address does not match the expected address {}",
                        address.getAddress().getHostAddress());
                    validationFailed.set(true);
                }
                resolved.countDown();
            }
        }
    });

    resolved.await();
    Assert.assertEquals(false, validationFailed.get());
}
 
Example #4
Source File: TestInetNameResolution.java    From distributedlog with Apache License 2.0 5 votes vote down vote up
@Test(timeout = 10000)
public void testInetNameResolution() throws Exception {
    String nameStr = "inet!127.0.0.1:3181";
    final CountDownLatch resolved = new CountDownLatch(1);
    final AtomicBoolean validationFailed = new AtomicBoolean(false);

    NameServerSet serverSet = new NameServerSet(nameStr);
    serverSet.watch(new DynamicHostSet.HostChangeMonitor<ServiceInstance>() {
        @Override
        public void onChange(ImmutableSet<ServiceInstance> hostSet) {
            if (hostSet.size() > 1) {
                logger.error("HostSet has more elements than expected {}", hostSet);
                validationFailed.set(true);
                resolved.countDown();
            } else if (hostSet.size() == 1) {
                ServiceInstance serviceInstance = hostSet.iterator().next();
                Endpoint endpoint = serviceInstance.getAdditionalEndpoints().get("thrift");
                InetSocketAddress address = new InetSocketAddress(endpoint.getHost(), endpoint.getPort());
                if (endpoint.getPort() != 3181) {
                    logger.error("Port does not match the expected port {}", endpoint.getPort());
                    validationFailed.set(true);
                } else if (!address.getAddress().getHostAddress().equals("127.0.0.1")) {
                    logger.error("Host address does not match the expected address {}", address.getAddress().getHostAddress());
                    validationFailed.set(true);
                }
                resolved.countDown();
            }
        }
    });

    resolved.await();
    Assert.assertEquals(false, validationFailed.get());
}
 
Example #5
Source File: PinLaterQueueConfig.java    From pinlater with Apache License 2.0 4 votes vote down vote up
public void initialize() throws Exception {
  // Check if use of serverset is enabled, and if so, register a change monitor so we
  // can find out how many PinLater servers are active. We use this to compute the
  // per-server rate limit.
  if (pinlaterServerSetEnabled) {
    MorePreconditions.checkNotBlank(pinlaterServerSetPath);
    LOG.info("Monitoring pinlater serverset: {}", pinlaterServerSetPath);
    String fullServerSetPath = getClass().getResource("/" + pinlaterServerSetPath).getPath();
    ServerSet serverSet = new ConfigFileServerSet(fullServerSetPath);
    serverSet.monitor(new DynamicHostSet.HostChangeMonitor<ServiceInstance>() {
      @Override
      public void onChange(ImmutableSet<ServiceInstance> hostSet) {
        int oldSize = numPinLaterServers.get();
        int newSize = hostSet.size();
        if (newSize == 0) {
          LOG.error("PinLater serverset is empty, ignoring and keeping old size: {}", oldSize);
          return;
        }
        if (oldSize == newSize) {
          LOG.info("PinLater serverset update, size unchanged: {}", oldSize);
          return;
        }

        LOG.info("PinLater serverset update, old size: {}, new size: {}", oldSize, newSize);
        numPinLaterServers.set(newSize);
        rebuild();
      }
    });
  } else {
    LOG.info("PinLater server set is disabled; rate limits will be applied per server.");
  }

  if (queueConfigFilePath == null || queueConfigFilePath.isEmpty()) {
    LOG.info("Queue config zookeeper path not specified, using defaults.");
    return;
  }

  LOG.info("Registering watch on queue config: {}", queueConfigFilePath);
  String fullQueueConfigFilePath = getClass().getResource("/" + queueConfigFilePath).getPath();
  ConfigFileWatcher.defaultInstance().addWatch(
      fullQueueConfigFilePath, new ExceptionalFunction<byte[], Void>() {
        @Override
        public Void applyE(byte[] bytes) throws Exception {
          QueueConfigSchema queueConfigSchema = QueueConfigSchema.load(bytes);
          LOG.info("Queue config update, new value: {}", queueConfigSchema);
          queueConfigSchemaRef.set(queueConfigSchema);
          rebuild();
          return null;
        }
      });
}