@Override public void store(final ProgramId serviceId, final RouteConfig routeConfig) { Supplier<RouteConfig> supplier = Suppliers.ofInstance(routeConfig); SettableFuture<RouteConfig> oldConfigFuture = routeConfigMap.get(serviceId); Future<RouteConfig> future = ZKExtOperations.createOrSet(zkClient, getZKPath(serviceId), supplier, ROUTE_CONFIG_CODEC, 10); try { future.get(ZK_TIMEOUT_SECS, TimeUnit.SECONDS); SettableFuture<RouteConfig> newFuture = SettableFuture.create(); newFuture.set(routeConfig); if (oldConfigFuture != null) { routeConfigMap.replace(serviceId, oldConfigFuture, newFuture); } else { routeConfigMap.putIfAbsent(serviceId, newFuture); } } catch (ExecutionException | InterruptedException | TimeoutException ex) { throw Throwables.propagate(ex); } }
@Override public void store(final ProgramId serviceId, final RouteConfig routeConfig) { Supplier<RouteConfig> supplier = Suppliers.ofInstance(routeConfig); SettableFuture<RouteConfig> oldConfigFuture = routeConfigMap.get(serviceId); Future<RouteConfig> future = ZKExtOperations.createOrSet(zkClient, getZKPath(serviceId), supplier, ROUTE_CONFIG_CODEC, 10); try { future.get(ZK_TIMEOUT_SECS, TimeUnit.SECONDS); SettableFuture<RouteConfig> newFuture = SettableFuture.create(); newFuture.set(routeConfig); if (oldConfigFuture != null) { routeConfigMap.replace(serviceId, oldConfigFuture, newFuture); } else { routeConfigMap.putIfAbsent(serviceId, newFuture); } } catch (ExecutionException | InterruptedException | TimeoutException ex) { throw Throwables.propagate(ex); } }
@Override public void delete(final ProgramId serviceId) throws NotFoundException { OperationFuture<String> future = zkClient.delete(getZKPath(serviceId)); SettableFuture<RouteConfig> oldConfigFuture = routeConfigMap.get(serviceId); try { future.get(ZK_TIMEOUT_SECS, TimeUnit.SECONDS); routeConfigMap.remove(serviceId, oldConfigFuture); } catch (ExecutionException | InterruptedException | TimeoutException ex) { if (ex.getCause() instanceof KeeperException.NoNodeException) { throw new NotFoundException(String.format("Route Config for Service %s was not found.", serviceId)); } throw Throwables.propagate(ex); } }
@Override public void delete(final ProgramId serviceId) throws NotFoundException { OperationFuture<String> future = zkClient.delete(getZKPath(serviceId)); SettableFuture<RouteConfig> oldConfigFuture = routeConfigMap.get(serviceId); try { future.get(ZK_TIMEOUT_SECS, TimeUnit.SECONDS); routeConfigMap.remove(serviceId, oldConfigFuture); } catch (ExecutionException | InterruptedException | TimeoutException ex) { if (ex.getCause() instanceof KeeperException.NoNodeException) { throw new NotFoundException(String.format("Route Config for Service %s was not found.", serviceId)); } throw Throwables.propagate(ex); } }
final SettableFuture<RouteConfig> oldSettableFuture, final Watcher watcher) { OperationFuture<NodeData> future = zkClient.getData(getZKPath(serviceId), watcher); Futures.addCallback(future, new FutureCallback<NodeData>() { @Override
final SettableFuture<RouteConfig> oldSettableFuture, final Watcher watcher) { OperationFuture<NodeData> future = zkClient.getData(getZKPath(serviceId), watcher); Futures.addCallback(future, new FutureCallback<NodeData>() { @Override
private void existsAndWatch(final ProgramId serviceId, final SettableFuture<RouteConfig> oldSettableFuture) { Futures.addCallback(zkClient.exists(getZKPath(serviceId), new Watcher() { @Override public void process(WatchedEvent event) { // If service name doesn't exist in the map, then don't rewatch it. if (!routeConfigMap.containsKey(serviceId)) { return; } if (event.getType() == Event.EventType.NodeCreated) { getAndWatchData(serviceId, SettableFuture.<RouteConfig>create(), oldSettableFuture, new ZKRouteWatcher(serviceId)); } } }), new FutureCallback<Stat>() { @Override public void onSuccess(@Nullable Stat result) { if (result != null) { getAndWatchData(serviceId, SettableFuture.<RouteConfig>create(), oldSettableFuture, new ZKRouteWatcher(serviceId)); } } @Override public void onFailure(Throwable t) { routeConfigMap.remove(serviceId); LOG.debug("Failed to check exists for property data for {}", serviceId, t); } }); }
private void existsAndWatch(final ProgramId serviceId, final SettableFuture<RouteConfig> oldSettableFuture) { Futures.addCallback(zkClient.exists(getZKPath(serviceId), new Watcher() { @Override public void process(WatchedEvent event) { // If service name doesn't exist in the map, then don't rewatch it. if (!routeConfigMap.containsKey(serviceId)) { return; } if (event.getType() == Event.EventType.NodeCreated) { getAndWatchData(serviceId, SettableFuture.<RouteConfig>create(), oldSettableFuture, new ZKRouteWatcher(serviceId)); } } }), new FutureCallback<Stat>() { @Override public void onSuccess(@Nullable Stat result) { if (result != null) { getAndWatchData(serviceId, SettableFuture.<RouteConfig>create(), oldSettableFuture, new ZKRouteWatcher(serviceId)); } } @Override public void onFailure(Throwable t) { routeConfigMap.remove(serviceId); LOG.debug("Failed to check exists for property data for {}", serviceId, t); } }); }