private static void updateConnectorIdAnnouncement(Announcer announcer, ConnectorId connectorId, InternalNodeManager nodeManager) { // // This code was copied from PrestoServer, and is a hack that should be removed when the connectorId property is removed // // get existing announcement ServiceAnnouncement announcement = getPrestoAnnouncement(announcer.getServiceAnnouncements()); // update connectorIds property Map<String, String> properties = new LinkedHashMap<>(announcement.getProperties()); String property = nullToEmpty(properties.get("connectorIds")); Set<String> connectorIds = new LinkedHashSet<>(Splitter.on(',').trimResults().omitEmptyStrings().splitToList(property)); connectorIds.add(connectorId.toString()); properties.put("connectorIds", Joiner.on(',').join(connectorIds)); // update announcement announcer.removeServiceAnnouncement(announcement.getId()); announcer.addServiceAnnouncement(serviceAnnouncement(announcement.getType()).addProperties(properties).build()); announcer.forceAnnounce(); nodeManager.refreshNodes(); }
binder.bind(TaskManagementExecutor.class).in(Scopes.SINGLETON); binder.bind(SqlTaskManager.class).in(Scopes.SINGLETON); binder.bind(TaskManager.class).to(Key.get(SqlTaskManager.class)); .addProperty("node_version", nodeVersion.toString()) .addProperty("coordinator", String.valueOf(serverConfig.isCoordinator())) .addProperty("connectorIds", nullToEmpty(serverConfig.getDataSources()));
@Test public void testHttpsSelector() { Injector injector = Guice.createInjector( new ConfigurationModule(new ConfigurationFactory(ImmutableMap.<String, String>of())), new TestingNodeModule(), new TestingDiscoveryModule(), binder -> discoveryBinder(binder).bindHttpSelector("apple")); InMemoryDiscoveryClient discoveryClient = injector.getInstance(InMemoryDiscoveryClient.class); discoveryClient.announce(ImmutableSet.of(serviceAnnouncement("apple").addProperty("https", "fake://server-https").build())); HttpServiceSelector selector = injector.getInstance(Key.get(HttpServiceSelector.class, serviceType("apple"))); assertEquals(getOnlyElement(selector.selectHttpService()), URI.create("fake://server-https")); }
@Test public void testHttpAnnouncementWithPool() { final StaticAnnouncementHttpServerInfoImpl httpServerInfo = new StaticAnnouncementHttpServerInfoImpl( URI.create("http://127.0.0.1:4444"), URI.create("http://example.com:4444"), URI.create("https://127.0.0.1:4444"), URI.create("https://example.com:4444")); Injector injector = Guice.createInjector( new TestingDiscoveryModule(), binder -> { binder.bind(AnnouncementHttpServerInfo.class).toInstance(httpServerInfo); discoveryBinder(binder).bindHttpAnnouncement("apple"); }); ServiceAnnouncement announcement = serviceAnnouncement("apple") .addProperty("http", httpServerInfo.getHttpUri().toASCIIString()) .addProperty("http-external", httpServerInfo.getHttpExternalUri().toASCIIString()) .addProperty("https", httpServerInfo.getHttpsUri().toASCIIString()) .addProperty("https-external", httpServerInfo.getHttpsExternalUri().toASCIIString()) .build(); Set<ServiceAnnouncement> announcements = injector.getInstance(Key.get(new TypeLiteral<Set<ServiceAnnouncement>>() { })); assertAnnouncement(announcements, announcement); }
@Test public void testJsonEncode() throws Exception { ServiceAnnouncement serviceAnnouncement = serviceAnnouncement("foo") .addProperty("http", "http://localhost:8080") .addProperty("jmx", "jmx://localhost:1234") .build(); Map<String, Object> actual = objectCodec.fromJson(serviceAnnouncementCodec.toJson(serviceAnnouncement)); String json = Resources.toString(Resources.getResource("service-announcement.json"), UTF_8); Map<String, Object> expected = objectCodec.fromJson(json); // set id in expected expected.put("id", serviceAnnouncement.getId().toString()); assertEquals(actual, expected); }
@Test public void testDynamicAnnouncement() throws Exception .initialize(); ServiceAnnouncement announcement = ServiceAnnouncement.serviceAnnouncement("apple") .addProperties(ImmutableMap.of("key", "value")) .build(); DiscoveryAnnouncementClient client = announcerInjector.getInstance(DiscoveryAnnouncementClient.class); client.announce(ImmutableSet.of(announcement)).get(); NodeInfo announcerNodeInfo = announcerInjector.getInstance(NodeInfo.class); assertEquals(services.size(), 1); assertNotNull(service.getId()); assertEquals(service.getNodeId(), announcerNodeInfo.getNodeId()); assertEquals(service.getLocation(), announcerNodeInfo.getLocation()); assertEquals(service.getPool(), announcerNodeInfo.getPool()); assertEquals(service.getProperties(), announcement.getProperties());
@Test public void testToString() { assertNotNull(new Announcement("environment", "node", "pool", "location", ImmutableSet.of( serviceAnnouncement("foo") .addProperty("http", "http://localhost:8080") .addProperty("jmx", "jmx://localhost:1234") .build()) )); }
@Test public void testEquivalence() { equivalenceTester() .addEquivalentGroup( new Announcement("environment", "node-A", "pool", "location", ImmutableSet.<ServiceAnnouncement>of(serviceAnnouncement("foo").build())), new Announcement("ENVIRONMENT", "node-A", "pool", "location", ImmutableSet.<ServiceAnnouncement>of(serviceAnnouncement("foo").build())), new Announcement("environment", "node-A", "pool", "LOCATION", ImmutableSet.<ServiceAnnouncement>of(serviceAnnouncement("foo").build())), new Announcement("environment", "node-A", "pool", "location", ImmutableSet.<ServiceAnnouncement>of(serviceAnnouncement("FOO").build()))) .addEquivalentGroup( new Announcement("environment", "node-B", "pool", "location", ImmutableSet.<ServiceAnnouncement>of(serviceAnnouncement("foo").build())), new Announcement("environment-X", "node-B", "pool", "location", ImmutableSet.<ServiceAnnouncement>of(serviceAnnouncement("foo").build())), new Announcement("environment", "node-B", "pool", "location-X", ImmutableSet.<ServiceAnnouncement>of(serviceAnnouncement("foo").build())), new Announcement("environment", "node-B", "pool", "location", ImmutableSet.<ServiceAnnouncement>of(serviceAnnouncement("bar").build()))) .check(); } }
@Test public void testAnnouncement() throws Exception .initialize(); lifeCycleManager = injector.getInstance(LifeCycleManager.class); injector.getInstance(Announcer.class).start(); AnnouncementHttpServerInfo httpServerInfo = injector.getInstance(AnnouncementHttpServerInfo.class); ServiceAnnouncement announcement = serviceAnnouncement("racktest") .addProperty("http", httpServerInfo.getHttpUri().toASCIIString()) .addProperty("http-external", httpServerInfo.getHttpExternalUri().toASCIIString()) .build(); DiscoveryLookupClient lookupClient = injector.getInstance(InMemoryDiscoveryClient.class); List<ServiceDescriptor> descriptors = lookupClient.getServices("racktest").get().getServiceDescriptors(); assertEquals(descriptors.size(), 1); ServiceDescriptor descriptor = Iterables.getOnlyElement(descriptors); assertEquals(descriptor.getProperties(), announcement.getProperties());
@Test public void testBuilder() { assertAnnouncement(serviceAnnouncement("foo").build(), "foo", ImmutableMap.<String, String>of()); assertAnnouncement(serviceAnnouncement("foo").build(), "foo", ImmutableMap.<String, String>of()); assertAnnouncement(serviceAnnouncement("foo").addProperty("a", "apple").build(), "foo", ImmutableMap.of("a", "apple")); assertAnnouncement(serviceAnnouncement("foo").addProperties(ImmutableMap.of("a", "apple", "b", "banana")).build(), "foo", ImmutableMap.of("a", "apple", "b", "banana")); }
@BeforeMethod protected void setUp() throws Exception { nodeInfo = new NodeInfo(new NodeConfig().setEnvironment("test").setPool("pool")); discoveryClient = new InMemoryDiscoveryClient(nodeInfo, MAX_AGE); serviceAnnouncement = ServiceAnnouncement.serviceAnnouncement(serviceType.value()).addProperty("a", "apple").build(); announcer = new Announcer(discoveryClient, ImmutableSet.of(serviceAnnouncement)); }
@Test public void testToString() { assertNotNull(serviceAnnouncement("foo").build()); }
private static ServiceAnnouncement createHttpAnnouncement(AnnouncementHttpServerInfo httpServerInfo, String announcement) { ServiceAnnouncementBuilder builder = serviceAnnouncement(announcement); if (httpServerInfo.getHttpUri() != null) { builder.addProperty("http", httpServerInfo.getHttpUri().toString()); builder.addProperty("http-external", httpServerInfo.getHttpExternalUri().toString()); } if (httpServerInfo.getHttpsUri() != null) { builder.addProperty("https", httpServerInfo.getHttpsUri().toString()); builder.addProperty("https-external", httpServerInfo.getHttpsExternalUri().toString()); } return builder.build(); } }
@Override public void configure(Binder binder) { binder.bind(AnnouncementHttpServerInfo.class).toInstance(httpServerInfo); DiscoveryBinder.discoveryBinder(binder).bindHttpAnnouncement("apple").addProperty("a", "apple"); } }
@Override public ServiceAnnouncement get() { if (httpServerInfo.getHttpUri() != null) { builder.addProperty("http", httpServerInfo.getHttpUri().toString()); builder.addProperty("http-external", httpServerInfo.getHttpExternalUri().toString()); } if (httpServerInfo.getHttpsUri() != null) { builder.addProperty("https", httpServerInfo.getHttpsUri().toString()); builder.addProperty("https-external", httpServerInfo.getHttpsExternalUri().toString()); } return builder.build(); } }
@Override public ServiceAnnouncement get() { if (httpServerInfo.getHttpUri() != null) { builder.addProperty("http", httpServerInfo.getHttpUri().toString()); builder.addProperty("http-external", httpServerInfo.getHttpExternalUri().toString()); } if (httpServerInfo.getHttpsUri() != null) { builder.addProperty("https", httpServerInfo.getHttpsUri().toString()); builder.addProperty("https-external", httpServerInfo.getHttpsExternalUri().toString()); } return builder.build(); } }
public static ServiceAnnouncementBuilder serviceAnnouncement(String type) { return new ServiceAnnouncementBuilder(type); }
public static ServiceAnnouncementBuilder serviceAnnouncement(String type) { return new ServiceAnnouncementBuilder(type); }
@Override public ServiceAnnouncement get() { if (httpServerInfo.getHttpUri() != null) { builder.addProperty("http", httpServerInfo.getHttpUri().toString()); builder.addProperty("http-external", httpServerInfo.getHttpExternalUri().toString()); } if (httpServerInfo.getHttpsUri() != null) { builder.addProperty("https", httpServerInfo.getHttpsUri().toString()); builder.addProperty("https-external", httpServerInfo.getHttpsExternalUri().toString()); } return builder.build(); } }
public static ServiceAnnouncementBuilder serviceAnnouncement(String type) { return new ServiceAnnouncementBuilder(type); }