com.twitter.thrift.Endpoint Java Examples

The following examples show how to use com.twitter.thrift.Endpoint. 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: NameServerSet.java    From distributedlog with Apache License 2.0 6 votes vote down vote up
private ServiceInstance endpointAddressToServiceInstance(Address endpointAddress) {
    if (endpointAddress instanceof Address.Inet) {
        InetSocketAddress inetSocketAddress = ((Address.Inet) endpointAddress).addr();
        Endpoint endpoint = new Endpoint(inetSocketAddress.getHostString(), inetSocketAddress.getPort());
        HashMap<String, Endpoint> map = new HashMap<String, Endpoint>();
        map.put("thrift", endpoint);
        return new ServiceInstance(
            endpoint,
            map,
            Status.ALIVE);
    } else {
        logger.error("We expect InetSocketAddress while the resolved address {} was {}",
                    endpointAddress, endpointAddress.getClass());
        throw new UnsupportedOperationException("invalid endpoint address: " + endpointAddress);
    }
}
 
Example #3
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 #4
Source File: NameServerSet.java    From distributedlog with Apache License 2.0 6 votes vote down vote up
private ServiceInstance endpointAddressToServiceInstance(Address endpointAddress) {
    if (endpointAddress instanceof Address.Inet) {
        InetSocketAddress inetSocketAddress = ((Address.Inet) endpointAddress).addr();
        Endpoint endpoint = new Endpoint(inetSocketAddress.getHostString(), inetSocketAddress.getPort());
        HashMap<String, Endpoint> map = new HashMap<String, Endpoint>();
        map.put("thrift", endpoint);
        return new ServiceInstance(
            endpoint,
            map,
            Status.ALIVE);
    } else {
        logger.error("We expect InetSocketAddress while the resolved address {} was {}",
                    endpointAddress, endpointAddress.getClass());
        throw new UnsupportedOperationException("invalid endpoint address: " + endpointAddress);
    }
}
 
Example #5
Source File: NameServerSet.java    From distributedlog with Apache License 2.0 5 votes vote down vote up
private String hostSetToString(ImmutableSet<ServiceInstance> hostSet) {
    StringBuilder result = new StringBuilder();
    result.append("(");
    for (ServiceInstance serviceInstance : hostSet) {
        Endpoint endpoint = serviceInstance.getServiceEndpoint();
        result.append(String.format(" %s:%d", endpoint.getHost(), endpoint.getPort()));
    }
    result.append(" )");

    return result.toString();
}
 
Example #6
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 #7
Source File: NameServerSet.java    From distributedlog with Apache License 2.0 5 votes vote down vote up
private String hostSetToString(ImmutableSet<ServiceInstance> hostSet) {
    StringBuilder result = new StringBuilder();
    result.append("(");
    for (ServiceInstance serviceInstance : hostSet) {
        Endpoint endpoint = serviceInstance.getServiceEndpoint();
        result.append(String.format(" %s:%d", endpoint.getHost(), endpoint.getPort()));
    }
    result.append(" )");

    return result.toString();
}
 
Example #8
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 #9
Source File: ConfigFileServerSet.java    From pinlater with Apache License 2.0 5 votes vote down vote up
protected static ImmutableSet<ServiceInstance> readServerSet(byte[] fileContent)
    throws IOException {
  ImmutableSet.Builder<ServiceInstance> builder = new ImmutableSet.Builder<ServiceInstance>();
  InputStream stream = new ByteArrayInputStream(fileContent);
  BufferedReader reader = new BufferedReader(new InputStreamReader(stream));
  while (true) {
    String line = reader.readLine();
    if (line == null) {
      // EOF.
      break;
    } else if (line.isEmpty()) {
      // Skip empty lines.
      continue;
    }

    // We expect each line to be of the form "hostname:port". Note that host names can
    // contain ':' themselves (e.g. ipv6 addresses).
    int index = line.lastIndexOf(':');
    Preconditions.checkArgument(index > 0 && index < line.length() - 1);

    String host = line.substring(0, index);
    int port = Integer.parseInt(line.substring(index + 1));
    builder.add(new ServiceInstance(
        new Endpoint(host, port),                 // endpoint
        Collections.<String, Endpoint>emptyMap(), // additional endpoints
        Status.ALIVE));                           // status
  }
  return builder.build();
}