com.twitter.thrift.ServiceInstance Java Examples

The following examples show how to use com.twitter.thrift.ServiceInstance. 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: LindenClient.java    From linden with Apache License 2.0 5 votes vote down vote up
private LindenService.ServiceIface buildClient(String parent, String node) {
  String nodePath = FilenameUtils.separatorsToUnix(FilenameUtils.concat(parent, node));
  byte[] bytes = zkClient.readData(nodePath);
  ServiceInstance serviceInstance = JSONObject.parseObject(new String(bytes), ServiceInstance.class);
  String schema = String.format("%s:%s",
                                serviceInstance.getServiceEndpoint().getHost(),
                                serviceInstance.getServiceEndpoint().getPort());
  return Thrift.newIface(schema, LindenService.ServiceIface.class);
}
 
Example #6
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 #7
Source File: NameServerSet.java    From distributedlog with Apache License 2.0 5 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
 * @return A command which, when executed, will stop monitoring the host set.
 * @throws com.twitter.common.net.pool.DynamicHostSet.MonitorException if there is a problem monitoring the host set
 */
@Override
public Command watch(HostChangeMonitor<ServiceInstance> monitor) throws MonitorException {
    // First add the monitor to the watchers so that it does not miss any changes and invoke
    // the onChange method
    synchronized (watchers) {
        watchers.add(monitor);
    }

    if (resolutionPending.compareAndSet(false, false)) {
        monitor.onChange(hostSet);
    }

    return Commands.NOOP; // Return value is not used
}
 
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: 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 #10
Source File: NameServerSet.java    From distributedlog with Apache License 2.0 5 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
 * @return A command which, when executed, will stop monitoring the host set.
 * @throws com.twitter.common.net.pool.DynamicHostSet.MonitorException if there is a problem monitoring the host set
 */
@Override
public Command watch(HostChangeMonitor<ServiceInstance> monitor) throws MonitorException {
    // First add the monitor to the watchers so that it does not miss any changes and invoke
    // the onChange method
    synchronized (watchers) {
        watchers.add(monitor);
    }

    if(resolutionPending.compareAndSet(false, false)) {
        monitor.onChange(hostSet);
    }

    return Commands.NOOP; // Return value is not used
}
 
Example #11
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 #12
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();
}
 
Example #13
Source File: NameServerSet.java    From distributedlog with Apache License 2.0 4 votes vote down vote up
private BoxedUnit respondToChanges(Addr addr) {
    ImmutableSet<ServiceInstance> oldHostSet = ImmutableSet.copyOf(hostSet);

    ImmutableSet<ServiceInstance> newHostSet = oldHostSet;

    if (addr instanceof Addr.Bound) {
        scala.collection.immutable.Set<Address> endpointAddresses = ((Addr.Bound) addr).addrs();
        scala.collection.Iterator<Address> endpointAddressesIterator = endpointAddresses.toIterator();
        HashSet<ServiceInstance> serviceInstances = new HashSet<ServiceInstance>();
        while (endpointAddressesIterator.hasNext()) {
            serviceInstances.add(endpointAddressToServiceInstance(endpointAddressesIterator.next()));
        }
        newHostSet = ImmutableSet.copyOf(serviceInstances);

    } else if (addr instanceof Addr.Failed) {
        logger.error("Name resolution failed", ((Addr.Failed) addr).cause());
        newHostSet = ImmutableSet.of();
    } else if (addr.toString().equals("Pending")) {
        logger.info("Name resolution pending");
        newHostSet = oldHostSet;
    } else if (addr.toString().equals("Neg")) {
        newHostSet = ImmutableSet.of();
    } else {
        logger.error("Invalid Addr type: {}", addr.getClass().getName());
        throw new UnsupportedOperationException("Invalid Addr type:" + addr.getClass().getName());
    }

    // Reference comparison is valid as the sets are immutable
    if (oldHostSet != newHostSet) {
        logger.info("NameServerSet updated: {} -> {}", hostSetToString(oldHostSet), hostSetToString(newHostSet));
        resolutionPending.set(false);
        hostSet = newHostSet;
        synchronized (watchers) {
            for (HostChangeMonitor<ServiceInstance> watcher: watchers) {
                watcher.onChange(newHostSet);
            }
        }

    }

    return BoxedUnit.UNIT;
}
 
Example #14
Source File: NameServerSet.java    From distributedlog with Apache License 2.0 4 votes vote down vote up
private BoxedUnit respondToChanges(Addr addr) {
    ImmutableSet<ServiceInstance> oldHostSet = ImmutableSet.copyOf(hostSet);

    ImmutableSet<ServiceInstance> newHostSet = oldHostSet;

    if (addr instanceof Addr.Bound) {
        scala.collection.immutable.Set<Address> endpointAddresses = ((Addr.Bound)addr).addrs();
        scala.collection.Iterator<Address> endpointAddressesIterator = endpointAddresses.toIterator();
        HashSet<ServiceInstance> serviceInstances = new HashSet<ServiceInstance>();
        while (endpointAddressesIterator.hasNext()) {
            serviceInstances.add(endpointAddressToServiceInstance(endpointAddressesIterator.next()));
        }
        newHostSet = ImmutableSet.copyOf(serviceInstances);

    } else if (addr instanceof Addr.Failed) {
        logger.error("Name resolution failed", ((Addr.Failed)addr).cause());
        newHostSet = ImmutableSet.of();
    } else if (addr.toString().equals("Pending")) {
        logger.info("Name resolution pending");
        newHostSet = oldHostSet;
    } else if (addr.toString().equals("Neg")) {
        newHostSet = ImmutableSet.of();
    } else {
        logger.error("Invalid Addr type: {}", addr.getClass().getName());
        throw new UnsupportedOperationException("Invalid Addr type:" + addr.getClass().getName());
    }

    // Reference comparison is valid as the sets are immutable
    if (oldHostSet != newHostSet) {
        logger.info("NameServerSet updated: {} -> {}", hostSetToString(oldHostSet), hostSetToString(newHostSet));
        resolutionPending.set(false);
        hostSet = newHostSet;
        synchronized (watchers) {
            for (HostChangeMonitor<ServiceInstance> watcher: watchers) {
                watcher.onChange(newHostSet);
            }
        }

    }

    return BoxedUnit.UNIT;
}
 
Example #15
Source File: ConfigFileServerSet.java    From pinlater with Apache License 2.0 4 votes vote down vote up
@Override
public Command watch(final HostChangeMonitor<ServiceInstance> monitor) throws MonitorException {
  monitor(monitor);
  return null;
}
 
Example #16
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;
        }
      });
}