@Override public String apply(ServiceInformation input) { if (input == null) { return null; } return input.getServiceName(); } };
public ServiceInformation build() { return new ServiceInformation(serviceName, serviceType, serviceId, grabBag); } }
@Override public int compare(ServiceInformation o1, ServiceInformation o2) { return o1.getServiceId().compareTo(o2.getServiceId()); } });
public ConsistentRingGroup(Collection<ServiceInformation> servers) { totalServers = servers.size(); Map<String, List<ServiceInformation>> serverGroups = Maps.newHashMap(); String serviceName = null; //Sort the servers by type for (ServiceInformation info: servers) { Preconditions.checkArgument(serviceName == null || StringUtils.equals(serviceName, info.getServiceName()), "All services must have the same name: " + servers); serviceName = info.getServiceName(); //Hashmaps allow null keys, so null service types should map correctly. if (!serverGroups.containsKey(info.getServiceType())) { serverGroups.put(info.getServiceType(), new ArrayList<ServiceInformation>()); } serverGroups.get(info.getServiceType()).add(info); } for (Map.Entry<String, List<ServiceInformation>> entry: serverGroups.entrySet()) { rings.put(entry.getKey(), new ConsistentHashRing(entry.getValue())); } }
@OnStage(LifecycleStage.ANNOUNCE) public void announce() { final HttpConnector connector = httpServer.getConnectors().get("internal-http"); Preconditions.checkState(connector != null, "could not find internal http connector!"); serviceInfo = ServiceInformation.forService(serviceName, null, "http", "localhost", connector.getPort()); discoveryClient.announce(serviceInfo); }
localAnnouncementGenerations.put(si.getAnnouncementName(), currentGeneration); if (!childNodes.contains(si.getAnnouncementName())) { LOG.debug("Need to announce %s", si.getAnnouncementName()); final String childPath = getNodePath(si.getAnnouncementName()); zookeeper.create(childPath, serialized, Ids.OPEN_ACL_UNSAFE, si.isStaticAnnouncement() ? CreateMode.PERSISTENT : CreateMode.EPHEMERAL); LOG.debug("Created %sannouncement for %s", si.isStaticAnnouncement() ? "static " : "", si.getAnnouncementName()); final String announcementName = si.getAnnouncementName();
@Override @SuppressWarnings("PMD.PreserveStackTrace") public URI findServiceUri(final String serviceName, final String serviceType, final ServiceHint... hints) throws ServiceNotAvailableException { final ServiceInformation service = findServiceInformation(serviceName, serviceType, hints); final String locatedScheme = service .getProperty(ServiceInformation.PROP_SERVICE_SCHEME); final String locatedAddress = service .getProperty(ServiceInformation.PROP_SERVICE_ADDRESS); final String locatedPort = service .getProperty(ServiceInformation.PROP_SERVICE_PORT); if (StringUtils.isEmpty(locatedScheme) || StringUtils.isEmpty(locatedAddress) || StringUtils.isEmpty(locatedPort)) { throw new ServiceNotAvailableException( "Service %s/%s exists but misses address information (%s/%s)", serviceName, serviceType, locatedAddress, locatedPort); } try { return new URI(locatedScheme, null, locatedAddress, Integer.parseInt(locatedPort), "", null, null); } catch (URISyntaxException use) { throw new ServiceNotAvailableException( "Could not create URI from '%s'!", service); } }
public static ServiceInformation staticAnnouncement(final String serviceName, final String serviceType, final String serviceScheme, final String serviceAddress, final int port) { return staticAnnouncement(UUID.randomUUID(), serviceName, serviceType, serviceScheme, serviceAddress, port); }
void unannounce(final ServiceInformation serviceInformation) { localAnnouncements.remove(serviceInformation); announcementGeneration.incrementAndGet(); if (serviceInformation.isStaticAnnouncement()) { staticAnnouncementsToRemove.add(serviceInformation); } }
public static final Set<ServiceInformation> buildInternalServices(final String serviceName, final String serviceType, final GalaxyHttpServerConfig httpServerConfig, final GalaxyConfig galaxyConfig) { final Set<ServiceInformation> services = Sets.newHashSet(); final GalaxyIp internalIp = galaxyConfig.getInternalIp(); if (httpServerConfig.isInternalHttpEnabled()) { services.add(ServiceInformation.forService(serviceName, serviceType, "http", internalIp.getIp(), internalIp.getHttpPort())); } if (httpServerConfig.isInternalHttpsEnabled()) { services.add(ServiceInformation.forService(serviceName, serviceType, "https", internalIp.getIp(), internalIp.getHttpsPort())); } return services; }
final String scheme = serviceInfo.getProperty(ServiceInformation.PROP_SERVICE_SCHEME); final String address = serviceInfo.getProperty(ServiceInformation.PROP_SERVICE_ADDRESS); if (StringUtils.isEmpty(scheme) || StringUtils.isEmpty(address)) { throw new ServiceNotAvailableException("Service Information %s is incomplete!", serviceInfo); sb.append(address); final String portStr = serviceInfo.getProperty(ServiceInformation.PROP_SERVICE_PORT);
public static final Set<ServiceInformation> buildExternalServices(final String serviceName, final String serviceType, final GalaxyHttpServerConfig httpServerConfig, final GalaxyConfig galaxyConfig) { final Set<ServiceInformation> services = Sets.newHashSet(); final GalaxyIp externalIp = galaxyConfig.getExternalIp(); if (httpServerConfig.isExternalHttpEnabled()) { services.add(ServiceInformation.forService(serviceName, serviceType, "http", externalIp.getIp(), externalIp.getHttpPort())); } if (httpServerConfig.isExternalHttpsEnabled()) { services.add(ServiceInformation.forService(serviceName, serviceType, "https", externalIp.getIp(), externalIp.getHttpsPort())); } return services; } }
public Builder<Type> addServiceInformation(final ServiceInformation serviceInformation) { Preconditions.checkArgument(serviceInformation != null, "serviceInformation must not be null"); final String serviceName = serviceInformation.getServiceName(); List<ServiceInformation> serviceInformations = Lists.newArrayList(); ConsistentRingGroup currentGroup = newWorldOrder.get(serviceName); if (currentGroup != null) { serviceInformations.addAll(currentGroup.getAll()); } serviceInformations.add(serviceInformation); newWorldOrder.put(serviceName, new ConsistentRingGroup(serviceInformations)); return this; }
public static ServiceInformation staticAnnouncement(final UUID serviceId, final String serviceName, final String serviceType, final String serviceScheme, final String serviceAddress, final int port) { return new ServiceInformation(serviceName, serviceType, serviceId, ImmutableMap.of(PROP_SERVICE_SCHEME, serviceScheme, PROP_SERVICE_ADDRESS, serviceAddress, PROP_SERVICE_PORT, Integer.toString(port)), true); }
public ConsistentHashRing(List<ServiceInformation> servers) { //Make a copy before sorting the list this.servers = Lists.newArrayList(servers); //Sort the servers, so that the order in which we insert them into the ring is stable Collections.sort(this.servers, new Comparator<ServiceInformation>() { @Override public int compare(ServiceInformation o1, ServiceInformation o2) { return o1.getServiceId().compareTo(o2.getServiceId()); } }); for (ServiceInformation info: this.servers) { //Insert each server at 100 points in the ring, so that load is //(more) evenly redistributed if it dies, and a new ring is built from the remaining servers. Random rand = new Random(0); for (int i = 0; i < 100; i++) { //Append a deterministic random sequence long hash = algorithm.hash(info.getServiceId().toString() + rand.nextInt()); ring.put(hash, info); } } }
@Override public void configure() { final DiscoveryClient client = MockedDiscoveryClient.builder() .addServiceInformation(ServiceInformation.forService(serviceName, serviceType, serviceScheme, serviceAddress, servicePort)) .build(); bind(DiscoveryClient.class).toInstance(client); bind(ReadOnlyDiscoveryClient.class).toInstance(client); bind(DiscoveryClientConfig.class).toProvider(ConfigProvider.of(DiscoveryClientConfig.class)).in(Scopes.SINGLETON); bind(ServiceURIConverter.class).in(Scopes.SINGLETON); HttpClientModule.bindNewObserver(binder()).to(DiscoveryServiceInterceptor.class); } }
@Override public void unannounce(final ServiceInformation serviceInformation) { Preconditions.checkArgument(serviceInformation != null, "serviceInformation must not be null!"); final Map<String, ConsistentRingGroup> worldOrder = getStateOfTheWorldHolder().getState(); ConsistentRingGroup group = worldOrder.get(serviceInformation.getServiceName()); if (group == null) { return; } Set<ServiceInformation> services = Sets.newHashSet(group.getAll()); services.remove(serviceInformation); worldOrder.put(serviceInformation.getServiceName(), new ConsistentRingGroup(services)); }
public static final ServiceInformation forService(final String serviceName, final String serviceType, final String serviceScheme, final String serviceAddress, final int port) { return new ServiceInformation(serviceName, serviceType, null, ImmutableMap.of(PROP_SERVICE_SCHEME, serviceScheme, PROP_SERVICE_ADDRESS, serviceAddress, PROP_SERVICE_PORT, Integer.toString(port))); }
@Before public void setup() { Injector injector = Guice.createInjector(new MockedDiscoveryModule("fake", "fake", "http", "localhost", 12345), ConfigModule.forTesting("ness.discovery.enabled", "true"), new HttpClientModule("fake"), new LifecycleModule()); DiscoveryClient dc = injector.getInstance(DiscoveryClient.class); dc.announce(ServiceInformation.forService("fake", "fake", "http", "localhost", 123456)); injector.injectMembers(this); }
@Override public void announce(final ServiceInformation serviceInformation) { Preconditions.checkArgument(serviceInformation != null, "serviceInformation must not be null!"); final Map<String, ConsistentRingGroup> worldOrder = getStateOfTheWorldHolder().getState(); final String serviceName = serviceInformation.getServiceName(); List<ServiceInformation> services = Lists.newArrayList(); ConsistentRingGroup currentGroup = worldOrder.get(serviceName); if (currentGroup != null) { services.addAll(currentGroup.getAll()); } if (!services.contains(serviceInformation)) { services.add(serviceInformation); worldOrder.put(serviceName, new ConsistentRingGroup(services)); } }