@Override public void finished(Throwable failureCause) { if (failureCause != null) { LOG.error("Finished with failure for Stream handler instance {}", discoverableSupplier.get().getName(), failureCause); } } }
@Override public boolean contains(Discoverable discoverable) { // If the name doesn't match, it shouldn't be in the list. return discoverable.getName().equals(name) && discoverables.get().contains(discoverable); }
@Override public boolean contains(Discoverable discoverable) { // If the name doesn't match, it shouldn't be in the list. return discoverable.getName().equals(name) && discoverables.get().contains(discoverable); }
@Override public int compare(Discoverable o1, Discoverable o2) { int cmp = o1.getName().compareTo(o2.getName()); if (cmp != 0) { return cmp; } InetSocketAddress address1 = o1.getSocketAddress(); InetSocketAddress address2 = o2.getSocketAddress(); cmp = Bytes.compareTo(address1.getAddress().getAddress(), address2.getAddress().getAddress()); return (cmp == 0) ? Ints.compare(address1.getPort(), address2.getPort()) : cmp; } }
@Override public int compare(Discoverable o1, Discoverable o2) { int cmp = o1.getName().compareTo(o2.getName()); if (cmp != 0) { return cmp; } InetSocketAddress address1 = o1.getSocketAddress(); InetSocketAddress address2 = o2.getSocketAddress(); cmp = Bytes.compareTo(address1.getAddress().getAddress(), address2.getAddress().getAddress()); return (cmp == 0) ? Ints.compare(address1.getPort(), address2.getPort()) : cmp; } }
@Override public boolean equals(Object o) { if (this == o) { return true; } if (o == null || getClass() != o.getClass()) { return false; } Discoverable other = (Discoverable) o; return name.equals(other.getName()) && address.equals(other.getSocketAddress()) && Arrays.equals(payload, other.getPayload()); }
@Override public JsonElement serialize(Discoverable src, Type typeOfSrc, JsonSerializationContext context) { JsonObject jsonObj = new JsonObject(); jsonObj.addProperty("service", src.getName()); jsonObj.addProperty("hostname", src.getSocketAddress().getHostName()); jsonObj.addProperty("port", src.getSocketAddress().getPort()); jsonObj.add("payload", context.serialize(src.getPayload())); return jsonObj; } }
/** * Generate unique node path for a given {@link Discoverable}. * @param discoverable An instance of {@link Discoverable}. * @return A node name based on the discoverable. */ private String getNodePath(Discoverable discoverable) { InetSocketAddress socketAddress = discoverable.getSocketAddress(); String node = Hashing.md5() .newHasher() .putBytes(socketAddress.getAddress().getAddress()) .putInt(socketAddress.getPort()) .hash().toString(); return String.format("/%s/%s", discoverable.getName(), node); }
@Override public JsonElement serialize(Discoverable src, Type typeOfSrc, JsonSerializationContext context) { JsonObject jsonObj = new JsonObject(); jsonObj.addProperty("service", src.getName()); jsonObj.addProperty("hostname", src.getSocketAddress().getHostName()); jsonObj.addProperty("port", src.getSocketAddress().getPort()); jsonObj.add("payload", context.serialize(src.getPayload())); return jsonObj; }
/** * Generate unique node path for a given {@link Discoverable}. * @param discoverable An instance of {@link Discoverable}. * @return A node name based on the discoverable. */ private String getNodePath(Discoverable discoverable) { InetSocketAddress socketAddress = discoverable.getSocketAddress(); String node = Hashing.md5() .newHasher() .putBytes(socketAddress.getAddress().getAddress()) .putInt(socketAddress.getPort()) .hash().toString(); return String.format("/%s/%s", discoverable.getName(), node); }
@Override public JsonElement serialize(Discoverable src, Type typeOfSrc, JsonSerializationContext context) { JsonObject jsonObj = new JsonObject(); jsonObj.addProperty("service", src.getName()); jsonObj.addProperty("hostname", src.getSocketAddress().getHostName()); jsonObj.addProperty("port", src.getSocketAddress().getPort()); jsonObj.add("payload", context.serialize(src.getPayload())); return jsonObj; } }
@Override public JsonElement serialize(Discoverable src, Type typeOfSrc, JsonSerializationContext context) { JsonObject jsonObj = new JsonObject(); jsonObj.addProperty("service", src.getName()); jsonObj.addProperty("hostname", src.getSocketAddress().getHostName()); jsonObj.addProperty("port", src.getSocketAddress().getPort()); jsonObj.add("payload", context.serialize(src.getPayload())); return jsonObj; }
private ResolvingDiscoverable(Discoverable discoverable) { super(discoverable.getName(), discoverable.getSocketAddress(), discoverable.getPayload()); }
@Override public final void onChange(ResourceAssignment assignment) { // If service name is different, ignore it if (!assignment.getName().equals(discoverable.getName())) { return; } // For each new assignment, see if it has been changed by comparing with the old assignment. Collection<PartitionReplica> newAssignment = assignment.getAssignments().get(discoverable); if (oldAssignment == null || !oldAssignment.equals(newAssignment)) { // Notify onChange(newAssignment); } oldAssignment = assignment.getAssignments().get(discoverable); }
private ResolvingDiscoverable(Discoverable discoverable) { super(discoverable.getName(), discoverable.getSocketAddress(), discoverable.getPayload()); }
@Override public final void onChange(ResourceAssignment assignment) { // If service name is different, ignore it if (!assignment.getName().equals(discoverable.getName())) { return; } // For each new assignment, see if it has been changed by comparing with the old assignment. Collection<PartitionReplica> newAssignment = assignment.getAssignments().get(discoverable); if (oldAssignment == null || !oldAssignment.equals(newAssignment)) { // Notify onChange(newAssignment); } oldAssignment = assignment.getAssignments().get(discoverable); }
@Override public void close() { if (!closed.compareAndSet(false, true)) { return; } // Stop the registration retry executor retryExecutor.shutdownNow(); // Cancel the connection watcher watcherCancellable.cancel(); // Cancel all registered services List<ListenableFuture<?>> futures = new ArrayList<>(); lock.lock(); try { for (Map.Entry<Discoverable, DiscoveryCancellable> entry : discoverables.entries()) { LOG.debug("Un-registering service {} - {}", entry.getKey().getName(), entry.getKey().getSocketAddress()); futures.add(entry.getValue().asyncCancel()); } } finally { lock.unlock(); } try { Futures.successfulAsList(futures).get(); LOG.debug("All services unregistered"); } catch (Exception e) { // This is not expected to happen LOG.warn("Unexpected exception when waiting for all services to get unregistered", e); } // Cancel all services being watched services.invalidateAll(); }
private Cancellable subscribe(ResourceCoordinatorClient client, final Discoverable discoverable, final BlockingQueue<Collection<PartitionReplica>> assignmentQueue, final Semaphore finishSemaphore) { return client.subscribe(discoverable.getName(), new ResourceHandler(discoverable) { @Override public void onChange(Collection<PartitionReplica> partitionReplicas) { try { LOG.debug("Discoverable {} Received: {}", discoverable.getSocketAddress().getPort(), partitionReplicas); assignmentQueue.put(partitionReplicas); } catch (InterruptedException e) { LOG.error("Interrupted.", e); } } @Override public void finished(Throwable failureCause) { LOG.debug("Finished on {}", discoverable.getSocketAddress().getPort()); if (failureCause == null) { finishSemaphore.release(); } else { LOG.error("Finished with failure for {}", discoverable.getSocketAddress().getPort(), failureCause); } } }); }
@Override protected void initialize() throws Exception { LOG.info("Initializing DistributedStreamService."); createHeartbeatsFeed(); heartbeatPublisher.startAndWait(); resourceCoordinatorClient.startAndWait(); coordinationSubscription = resourceCoordinatorClient.subscribe(discoverableSupplier.get().getName(), new StreamsLeaderHandler()); heartbeatsSubscriptionExecutor = Executors.newSingleThreadExecutor( Threads.createDaemonThreadFactory("heartbeats-subscription-executor")); heartbeatsSubscription = subscribeToHeartbeatsFeed(); leaderListenerCancellable = addLeaderListener(new StreamLeaderListener() { @Override public void leaderOf(Set<StreamId> streamIds) { aggregate(streamIds); } }); performLeaderElection(); LOG.info("DistributedStreamService initialized."); }
@Test public void testMasterDiscovery() { Injector injector = Guice.createInjector( new ConfigModule(cConf), new ZKClientModule(), new ZKDiscoveryModule() ); ZKClientService zkClient = injector.getInstance(ZKClientService.class); zkClient.startAndWait(); try { DiscoveryService discoveryService = injector.getInstance(DiscoveryService.class); DiscoveryServiceClient discoveryServiceClient = injector.getInstance(DiscoveryServiceClient.class); // Register a master service InetSocketAddress socketAddr = new InetSocketAddress(InetAddress.getLoopbackAddress(), 43210); Cancellable cancellable = discoveryService.register(new Discoverable(Constants.Service.APP_FABRIC_HTTP, socketAddr)); try { // Discover the master service Discoverable discoverable = new RandomEndpointStrategy( () -> discoveryServiceClient.discover(Constants.Service.APP_FABRIC_HTTP)).pick(10, TimeUnit.SECONDS); Assert.assertNotNull(discoverable); Assert.assertEquals(Constants.Service.APP_FABRIC_HTTP, discoverable.getName()); Assert.assertEquals(socketAddr, discoverable.getSocketAddress()); } finally { cancellable.cancel(); } } finally { zkClient.stopAndWait(); } }