@Override public String getServiceId() { if (this.serviceInstance == null) { return null; } return this.serviceInstance.getName(); }
@Override public String getAppName() { return instance.getName(); }
@Override public boolean apply(ExampleServer server) { return server.getThisInstance().getName().endsWith(serviceName); } }, null);
@Override public boolean apply(ExampleServer server) { return server.getThisInstance().getName().endsWith(serviceName); } },
/** * Verify services are registered. * * @throws Exception */ public void verifyRegistrations() throws Exception { ServiceDiscovery<MetaData> serviceDiscovery = ServiceUtil.getServiceDiscovery(curatorFramework, basePath); listInstances(serviceDiscovery); for (Map.Entry<ServiceDiscovery<MetaData>, ServiceInstance<MetaData>> entry : discoveryMap.entrySet()) { ServiceInstance<MetaData> instance = entry.getValue(); try { ServiceInstance<MetaData> found = entry.getKey().queryForInstance(instance.getName(), instance.getId()); if (found == null) { throw new RuntimeException("There is no instance for: " + instance.getName() + ":" + instance.getId() + " registered "); } log.debug(found.getName() + " is verified at: " + found.getAddress() + ":" + found.getPort()); } catch (Exception e) { log.error("Could not find service: " + (instance.getName() + ":" + instance.getId()), e); throw new RuntimeException(e); } } }
@Override public void updateService(ServiceInstance<T> service) throws Exception { Preconditions.checkArgument(services.containsKey(service.getId()), "Service is not registered: " + service); byte[] bytes = serializer.serialize(service); String path = String.format("%s/instance", pathForInstance(service.getName(), service.getId())); client.set(path, Base64.encodeBase64String(bytes)); }
@Override public void unregisterService(ServiceInstance<T> service) throws Exception { String path = pathForInstance(service.getName(), service.getId()); services.remove(service.getId()); futures.remove(service.getId()).cancel(true); client.deleteDirectoryRecursive(path); }
@Override public void updateService(final ServiceInstance<T> service) throws Exception { Entry<T> entry = services.get(service.getId()); if ( entry == null ) { throw new Exception("Service not registered: " + service); } synchronized(entry) { entry.service = service; byte[] bytes = serializer.serialize(service); String path = pathForInstance(service.getName(), service.getId()); client.setData().forPath(path, bytes); } }
@Override public void updateService(final ServiceInstance<T> service) throws Exception { Entry<T> entry = services.get(service.getId()); if ( entry == null ) { throw new Exception("Service not registered: " + service); } synchronized(entry) { entry.service = service; byte[] bytes = serializer.serialize(service); String path = pathForInstance(service.getName(), service.getId()); client.setData().forPath(path, bytes); } }
@Override public List<ServiceDefinition> getServices(String name) { if (serviceDiscovery == null) { return Collections.emptyList(); } try { return serviceDiscovery.queryForInstances(name).stream() .map(si -> { Map<String, String> meta = new HashMap<>(); ObjectHelper.ifNotEmpty(si.getPayload(), meta::putAll); meta.putIfAbsent(ServiceDefinition.SERVICE_META_NAME, si.getName()); meta.putIfAbsent(ServiceDefinition.SERVICE_META_ID, si.getId()); return new DefaultServiceDefinition( si.getName(), si.getAddress(), si.getSslPort() != null ? si.getSslPort() : si.getPort(), meta); }) .collect(Collectors.toList()); } catch (Exception e) { throw new RuntimeCamelException(e); } }
private void internalUnregisterService(final Entry<T> entry) throws Exception { if ( entry != null ) { synchronized(entry) { if ( entry.cache != null ) { CloseableUtils.closeQuietly(entry.cache); entry.cache = null; } String path = pathForInstance(entry.service.getName(), entry.service.getId()); try { client.delete().guaranteed().forPath(path); } catch ( KeeperException.NoNodeException ignore ) { // ignore } } } } }
@Override public byte[] serialize(ServiceInstance<NodeEndpoint> i) throws Exception { DremioServiceInstance.Builder b = DremioServiceInstance.newBuilder(); b.setId(i.getId()); b.setName(i.getName()); b.setRegistrationTimeUTC(i.getRegistrationTimeUTC()); b.setEndpoint(i.getPayload()); return b.build().toByteArray(); }
@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 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(); } }
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(); } }
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; }
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; }
static<T> ObjectNode writeInstance(ObjectMapper mapper, ServiceInstance<T> instance, DiscoveryContext<T> context) { ObjectNode node = mapper.createObjectNode(); node.put("name", instance.getName()); node.put("id", instance.getId()); node.put("address", instance.getAddress()); putInteger(node, "port", instance.getPort()); putInteger(node, "sslPort", instance.getSslPort()); node.put("registrationTimeUTC", instance.getRegistrationTimeUTC()); node.put("serviceType", instance.getServiceType().name()); try { context.marshallJson(node, "payload", instance.getPayload()); } catch ( Exception e ) { ThreadUtils.checkInterrupted(e); throw new WebApplicationException(e); } return node; }
public void testMetaData() throws Exception { ServiceDiscovery<Map> sDiscovery = getServiceDiscovery(); assertNull(sDiscovery.queryForInstance("servers", ZK_ID)); DummyUser du = new DummyUser(); try { du.register(); assertNotNull(sDiscovery.queryForInstance("servers", ZK_ID)); List<ServiceInstance<Map>> allMetaData = du.getZKUtils().getAllMetaData(); assertEquals(1, allMetaData.size()); ServiceInstance<Map> meta = allMetaData.get(0); assertEquals(ZK_ID, meta.getId()); assertEquals("servers", meta.getName()); Map<String, String> data = meta.getPayload(); assertEquals(2, data.size()); assertEquals(ZK_ID, data.get("OOZIE_ID")); String url = ConfigUtils.getOozieURL(false); assertEquals(url, data.get("OOZIE_URL")); } finally { du.unregister(); } }