/** * Pretend to be a Drillbit creating its ZK entry. Real Drillbits use a GUID * as the key, but we just use the host name, which is good enough for our * purposes here. * * @param zk * @param host * @throws Exception */ private void addDrillbit(CuratorFramework zk, String host) throws Exception { DrillbitEndpoint dbe = makeEndpoint(host); ServiceInstance<DrillbitEndpoint> si = ServiceInstance .<DrillbitEndpoint> builder().name(CLUSTER_ID).payload(dbe).build(); byte data[] = DrillServiceInstanceHelper.SERIALIZER.serialize(si); zk.create().forPath("/" + host, data); }
/** * Return a service instance POJO * * @param name name of the service * @param id ID of the instance * @return the instance or <code>null</code> if not found * @throws Exception errors */ @Override public ServiceInstance<T> queryForInstance(String name, String id) throws Exception { String path = pathForInstance(name, id); try { byte[] bytes = client.getData().forPath(path); return serializer.deserialize(bytes); } catch ( KeeperException.NoNodeException ignore ) { // ignore } return null; }
/** * Return a group instance POJO * * @param name name of the group * @param id ID of the instance * @return the instance or <code>null</code> if not found * @throws Exception errors */ @Override public ServiceInstance<T> queryForInstance(String name, String id) throws Exception { String path = pathForInstance(name, id); try { byte[] bytes = client.getData().forPath(path); return serializer.deserialize(bytes); } catch ( KeeperException.NoNodeException ignore ) { // ignore } return null; }
@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 ServiceInstance<T> queryForInstance(String name, String id) throws Exception { EtcdResult result = client.get(String.format("%s/instance", pathForInstance(name, id))); String b64Instance = result.getNode().getValue(); byte[] bytes = Base64.decodeBase64(b64Instance); return serializer.deserialize(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 void nodeChanged() throws Exception { if ( nodeCache.getCurrentData() != null ) { ServiceInstance<T> newInstance = serializer.deserialize(nodeCache.getCurrentData().getData()); Entry<T> entry = services.get(newInstance.getId()); if ( entry != null ) { synchronized(entry) { entry.service = newInstance; } } } else { log.warn("Instance data has been deleted for: " + instance); } } };
@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 nodeChanged() throws Exception { if ( nodeCache.getCurrentData() != null ) { ServiceInstance<T> newInstance = serializer.deserialize(nodeCache.getCurrentData().getData()); Entry<T> entry = services.get(newInstance.getId()); if ( entry != null ) { synchronized(entry) { entry.service = newInstance; } } } else { log.warn("Instance data has been deleted for: " + instance); } } };
@VisibleForTesting protected void internalRegisterService(ServiceInstance<T> service) throws Exception final byte[] bytes = serializer.serialize(service); final String path = pathForInstance(service.getName(), service.getId());
private ServiceInstance<T> addInstance(ChildData childData, boolean onlyIfAbsent) throws Exception { String instanceId = instanceIdFromData(childData); ServiceInstance<T> serviceInstance = discovery.getSerializer().deserialize(childData.getData()); if (onlyIfAbsent) { instances.putIfAbsent(instanceId, serviceInstance); } else { instances.put(instanceId, serviceInstance); } cache.clearDataBytes(childData.getPath(), childData.getStat().getVersion()); return serviceInstance; } }
@VisibleForTesting protected void internalRegisterService(ServiceInstance<T> service) throws Exception byte[] bytes = serializer.serialize(service); String path = pathForInstance(service.getName(), service.getId());
private void addInstance(ChildData childData, boolean onlyIfAbsent) throws Exception { String instanceId = instanceIdFromData(childData); ServiceInstance<T> serviceInstance = discovery.getSerializer().deserialize(childData.getData()); if ( onlyIfAbsent ) { instances.putIfAbsent(instanceId, serviceInstance); } else { instances.put(instanceId, serviceInstance); } cache.clearDataBytes(childData.getPath(), childData.getStat().getVersion()); } }
@VisibleForTesting protected void internalRegisterService(ServiceInstance<T> service) throws Exception byte[] bytes = serializer.serialize(service); String path = pathForInstance(service.getName(), service.getId());
ServiceInstance<T> serviceInstance = discovery.getSerializer().deserialize(Base64.decodeBase64(er.getNode().getValue()));
ServiceInstance<T> serviceInstance = discovery.getSerializer().deserialize(Base64.decodeBase64(result.getNode().getValue()));