instance.getPort() >= 0 || (instance.getSslPort() != null && instance.getSslPort() >= 0), "WTH?! Both port and sslPort not set" ); final String scheme; if (instance.getSslPort() == null) { port = instance.getPort(); scheme = "http"; } else { port = instance.getSslPort() >= 0 ? instance.getSslPort() : instance.getPort(); scheme = instance.getSslPort() >= 0 ? "https" : "http";
/** {@inheritDoc} */ @Override public Collection<InetSocketAddress> getRegisteredAddresses() throws IgniteSpiException { init(); if (log.isDebugEnabled()) log.debug("Getting registered addresses from ZooKeeper IP Finder."); Collection<ServiceInstance<IgniteInstanceDetails>> serviceInstances; try { serviceInstances = discovery.queryForInstances(serviceName); } catch (Exception e) { log.warning("Error while getting registered addresses from ZooKeeper IP Finder.", e); return Collections.emptyList(); } Set<InetSocketAddress> answer = new HashSet<>(); for (ServiceInstance<IgniteInstanceDetails> si : serviceInstances) answer.add(new InetSocketAddress(si.getAddress(), si.getPort())); if (log.isInfoEnabled()) log.info("ZooKeeper IP Finder resolved addresses: " + answer); return answer; }
try { for (ServiceInstance<IgniteInstanceDetails> sd : discovery.queryForInstances(serviceName)) registrationsToIgnore.add(new InetSocketAddress(sd.getAddress(), sd.getPort()));
@Test public void testPickWithSslPort() throws Exception { EasyMock.expect(serviceProvider.getInstance()).andReturn(instance).anyTimes(); EasyMock.expect(instance.getAddress()).andReturn(ADDRESS).anyTimes(); EasyMock.expect(instance.getPort()).andReturn(PORT).anyTimes(); EasyMock.expect(instance.getSslPort()).andReturn(SSL_PORT).anyTimes(); EasyMock.replay(instance, serviceProvider); Server server = serverDiscoverySelector.pick(); Assert.assertEquals(SSL_PORT, server.getPort()); Assert.assertEquals(ADDRESS, server.getAddress()); Assert.assertTrue(server.getHost().contains(Integer.toString(SSL_PORT))); Assert.assertTrue(server.getHost().contains(ADDRESS)); Assert.assertEquals("https", server.getScheme()); EasyMock.verify(instance, serviceProvider); final URI uri = new URI( server.getScheme(), null, server.getAddress(), server.getPort(), "/druid/indexer/v1/action", null, null ); Assert.assertEquals(SSL_PORT, uri.getPort()); Assert.assertEquals(ADDRESS, uri.getHost()); Assert.assertEquals("https", uri.getScheme()); }
@Test public void testPickWithNullSslPort() throws Exception { EasyMock.expect(serviceProvider.getInstance()).andReturn(instance).anyTimes(); EasyMock.expect(instance.getAddress()).andReturn(ADDRESS).anyTimes(); EasyMock.expect(instance.getPort()).andReturn(PORT).anyTimes(); EasyMock.expect(instance.getSslPort()).andReturn(null).anyTimes(); EasyMock.replay(instance, serviceProvider); Server server = serverDiscoverySelector.pick(); Assert.assertEquals(PORT, server.getPort()); Assert.assertEquals(ADDRESS, server.getAddress()); Assert.assertTrue(server.getHost().contains(Integer.toString(PORT))); Assert.assertTrue(server.getHost().contains(ADDRESS)); Assert.assertEquals("http", server.getScheme()); EasyMock.verify(instance, serviceProvider); final URI uri = new URI( server.getScheme(), null, server.getAddress(), server.getPort(), "/druid/indexer/v1/action", null, null ); Assert.assertEquals(PORT, uri.getPort()); Assert.assertEquals(ADDRESS, uri.getHost()); Assert.assertEquals("http", uri.getScheme()); }
@Test public void testPick() throws Exception { EasyMock.expect(serviceProvider.getInstance()).andReturn(instance).anyTimes(); EasyMock.expect(instance.getAddress()).andReturn(ADDRESS).anyTimes(); EasyMock.expect(instance.getPort()).andReturn(PORT).anyTimes(); EasyMock.expect(instance.getSslPort()).andReturn(-1).anyTimes(); EasyMock.replay(instance, serviceProvider); Server server = serverDiscoverySelector.pick(); Assert.assertEquals(PORT, server.getPort()); Assert.assertEquals(ADDRESS, server.getAddress()); Assert.assertTrue(server.getHost().contains(Integer.toString(PORT))); Assert.assertTrue(server.getHost().contains(ADDRESS)); Assert.assertEquals("http", server.getScheme()); EasyMock.verify(instance, serviceProvider); final URI uri = new URI( server.getScheme(), null, server.getAddress(), server.getPort(), "/druid/indexer/v1/action", null, null ); Assert.assertEquals(PORT, uri.getPort()); Assert.assertEquals(ADDRESS, uri.getHost()); Assert.assertEquals("http", uri.getScheme()); }
@Test public void testPickIPv6Bracket() throws Exception { final String ADDRESS = "[2001:0db8:0000:0000:0000:ff00:0042:8329]"; EasyMock.expect(serviceProvider.getInstance()).andReturn(instance).anyTimes(); EasyMock.expect(instance.getAddress()).andReturn(ADDRESS).anyTimes(); EasyMock.expect(instance.getPort()).andReturn(PORT).anyTimes(); EasyMock.expect(instance.getSslPort()).andReturn(-1).anyTimes(); EasyMock.replay(instance, serviceProvider); Server server = serverDiscoverySelector.pick(); Assert.assertEquals(PORT, server.getPort()); Assert.assertEquals(ADDRESS, server.getAddress()); Assert.assertTrue(server.getHost().contains(Integer.toString(PORT))); Assert.assertTrue(server.getHost().contains(ADDRESS)); Assert.assertEquals("http", server.getScheme()); EasyMock.verify(instance, serviceProvider); final URI uri = new URI( server.getScheme(), null, server.getAddress(), server.getPort(), "/druid/indexer/v1/action", null, null ); Assert.assertEquals(PORT, uri.getPort()); Assert.assertEquals(ADDRESS, uri.getHost()); Assert.assertEquals("http", uri.getScheme()); }
@Test public void testPickIPv6() throws Exception { final String ADDRESS = "2001:0db8:0000:0000:0000:ff00:0042:8329"; EasyMock.expect(serviceProvider.getInstance()).andReturn(instance).anyTimes(); EasyMock.expect(instance.getAddress()).andReturn(ADDRESS).anyTimes(); EasyMock.expect(instance.getPort()).andReturn(PORT).anyTimes(); EasyMock.expect(instance.getSslPort()).andReturn(-1).anyTimes(); EasyMock.replay(instance, serviceProvider); Server server = serverDiscoverySelector.pick(); Assert.assertEquals(PORT, server.getPort()); Assert.assertEquals(ADDRESS, server.getAddress()); Assert.assertTrue(server.getHost().contains(Integer.toString(PORT))); Assert.assertTrue(server.getHost().contains(ADDRESS)); Assert.assertEquals("http", server.getScheme()); EasyMock.verify(instance, serviceProvider); final URI uri = new URI( server.getScheme(), null, server.getAddress(), server.getPort(), "/druid/indexer/v1/action", null, null ); Assert.assertEquals(PORT, uri.getPort()); Assert.assertEquals(StringUtils.format("[%s]", ADDRESS), uri.getHost()); Assert.assertEquals("http", uri.getScheme()); }
public int getPort() { if (this.serviceInstance == null) { return 0; } return this.serviceInstance.getPort(); }
@Override public int getPort() { return instance.getPort(); }
public ArrayList<InetSocketAddress> queryServerNodesFromZK(Class<T> clazz) { Collection<ServiceInstance<InstanceDetails>> instances; ArrayList<InetSocketAddress> serverList = new ArrayList<InetSocketAddress>(); try { instances = ZooRegistry.getInstance().queryForInstances(clazz.getName()); for(ServiceInstance<InstanceDetails> instance : instances){ serverList.add(new InetSocketAddress(instance.getAddress(), instance.getPort())); } if(serverList.isEmpty()){ throw new RuntimeException("Server list is empty, can not find any corresponding client.objects in the conf file."); } } catch (Exception e) { new RuntimeException("RPCClient|loadServerListFromZookeeper", e); } return serverList; }
public static InetSocketAddress convertServiceInstance(ServiceInstance<?> service) { return NetUtils.createSocketAddr(service.getAddress(),service.getPort()); }
public Iterable<DiscoveryNode> discoverNodes() { try { Collection<ServiceInstance<Void>> members = serviceDiscovery.queryForInstances(group); List<DiscoveryNode> nodes = new ArrayList<DiscoveryNode>(members.size()); for (ServiceInstance<Void> serviceInstance : members) { String host = serviceInstance.getAddress(); Integer port = serviceInstance.getPort(); Address address = new Address(host, port); SimpleDiscoveryNode node = new SimpleDiscoveryNode(address); nodes.add(node); } return nodes; } catch (InterruptedException e) { Thread.currentThread().interrupt(); throw new IllegalStateException("Error while talking to ZooKeeper", e); } catch (Exception e) { throw new IllegalStateException("Error while talking to ZooKeeper", e); } }
private DiscoveryInstance toSoaInstance(ServiceInstance<Payload> instance) { if ( instance == null ) { return null; } Payload payload = instance.getPayload(); int port = Objects.firstNonNull(instance.getPort(), Objects.firstNonNull(instance.getSslPort(), 0)); return new DiscoveryInstanceImpl(instance.getId(), instance.getAddress(), port, instance.getSslPort() != null, payload); }
public ServiceInstance<JsonObject> deserialize(byte[] bytes) throws Exception { ServiceInstance rawServiceInstance = this.mapper.readValue(bytes, this.type); ServiceInstanceBuilder<JsonObject> builder = ServiceInstance.<JsonObject>builder() .address(rawServiceInstance.getAddress()) .id(rawServiceInstance.getId()) .name(rawServiceInstance.getName()) .payload(new JsonObject(rawServiceInstance.getPayload().toString())) .registrationTimeUTC(rawServiceInstance.getRegistrationTimeUTC()) .serviceType(rawServiceInstance.getServiceType()); if (rawServiceInstance.getSslPort() != null) { builder.sslPort(rawServiceInstance.getSslPort()); } if (rawServiceInstance.getPort() != null) { builder.sslPort(rawServiceInstance.getPort()); } if (rawServiceInstance.getUriSpec() != null) { builder.uriSpec(rawServiceInstance.getUriSpec()); } return builder.build(); } }
@Override public byte[] serialize(ServiceInstance<T> instance) throws Exception { if ( compatibleSerializationMode ) { OldServiceInstance<T> compatible = new OldServiceInstance<T>(instance.getName(), instance.getId(), instance.getAddress(), instance.getPort(), instance.getSslPort(), instance.getPayload(), instance.getRegistrationTimeUTC(), instance.getServiceType(), instance.getUriSpec()); return mapper.writeValueAsBytes(compatible); } return mapper.writeValueAsBytes(instance); } }
public static GroupInstance groupInstance(ServiceInstance<GroupProxy> curatorServiceInstance) { if (curatorServiceInstance == null) return null; GroupInstance groupInstance = new GroupInstance(); groupInstance.setId(curatorServiceInstance.getId()); groupInstance.setAddress(curatorServiceInstance.getAddress()); groupInstance.setEnabled(curatorServiceInstance.isEnabled()); groupInstance.setName(curatorServiceInstance.getName()); groupInstance.setPayload(curatorServiceInstance.getPayload()); groupInstance.setPort(curatorServiceInstance.getPort()); groupInstance.setRegistrationTimestamp(curatorServiceInstance.getRegistrationTimeUTC()); return groupInstance; }
public static ApplicationInstance applicationInstance(ServiceInstance<NodeStatus> serviceInstance) { if (serviceInstance == null) return null; ApplicationInstance applicationInstance = new ApplicationInstance(); applicationInstance.setAddress(serviceInstance.getAddress()); applicationInstance.setEnabled(serviceInstance.isEnabled()); applicationInstance.setId(serviceInstance.getId()); applicationInstance.setName(serviceInstance.getName()); applicationInstance.setPayload(serviceInstance.getPayload()); applicationInstance.setPort(serviceInstance.getPort()); applicationInstance.setRegistrationTimestamp(serviceInstance.getRegistrationTimeUTC()); return applicationInstance; }
public static UnitInstance unitInstance(ServiceInstance<UnitProxy> serviceInstance) { if (serviceInstance == null) return null; UnitInstance unitInstance = new UnitInstance(); unitInstance.setId(serviceInstance.getId()); unitInstance.setAddress(serviceInstance.getAddress()); unitInstance.setEnabled(serviceInstance.isEnabled()); unitInstance.setName(serviceInstance.getName()); unitInstance.setPayload(serviceInstance.getPayload()); unitInstance.setPort(serviceInstance.getPort()); unitInstance.setRegistrationTimestamp(serviceInstance.getRegistrationTimeUTC()); return unitInstance; }
@Test public void testRegisterOneService() throws Exception { final String hostname = "localhost1"; final Integer port = 123; ServiceRegister register = new ServiceRegister(haContext); register.regService(hostname, port); ServiceManager manager = new ServiceManager(haContext); ServiceInstance<Void> instance = manager.getServiceInstance(); assertEquals("'hostname' doesn't match.", hostname, instance.getAddress()); assertEquals("'port' doesn't match.", port, instance.getPort()); }