@Override public void monitorTarget(ResourceID resourceID, HeartbeatTarget<O> heartbeatTarget) { if (!stopped) { if (heartbeatTargets.containsKey(resourceID)) { log.debug("The target with resource ID {} is already been monitored.", resourceID); } else { HeartbeatManagerImpl.HeartbeatMonitor<O> heartbeatMonitor = new HeartbeatManagerImpl.HeartbeatMonitor<>( resourceID, heartbeatTarget, scheduledExecutor, heartbeatListener, heartbeatTimeoutIntervalMs); heartbeatTargets.put( resourceID, heartbeatMonitor); // check if we have stopped in the meantime (concurrent stop operation) if (stopped) { heartbeatMonitor.cancel(); heartbeatTargets.remove(resourceID); } } } }
void cancel() { // we can only cancel if we are in state running if (state.compareAndSet(State.RUNNING, State.CANCELED)) { cancelTimeout(); } }
HeartbeatTarget<O> reportHeartbeat(ResourceID resourceID) { if (heartbeatTargets.containsKey(resourceID)) { HeartbeatManagerImpl.HeartbeatMonitor<O> heartbeatMonitor = heartbeatTargets.get(resourceID); heartbeatMonitor.reportHeartbeat(); return heartbeatMonitor.getHeartbeatTarget(); } else { return null; } }
@Override public void run() { if (!stopped) { log.debug("Trigger heartbeat request."); for (HeartbeatMonitor<O> heartbeatMonitor : getHeartbeatTargets()) { Future<O> futurePayload = getHeartbeatListener().retrievePayload(); final HeartbeatTarget<O> heartbeatTarget = heartbeatMonitor.getHeartbeatTarget(); if (futurePayload != null) { Future<Void> requestHeartbeatFuture = futurePayload.thenAcceptAsync(new AcceptFunction<O>() { @Override public void accept(O payload) { heartbeatTarget.requestHeartbeat(getOwnResourceID(), payload); } }, getExecutor()); requestHeartbeatFuture.exceptionally(new ApplyFunction<Throwable, Void>() { @Override public Void apply(Throwable failure) { log.warn("Could not request the heartbeat from target {}.", heartbeatTarget, failure); return null; } }); } else { heartbeatTarget.requestHeartbeat(getOwnResourceID(), null); } } } }
@Override public void run() { if (!stopped) { log.debug("Trigger heartbeat request."); for (HeartbeatMonitor<O> heartbeatMonitor : getHeartbeatTargets()) { CompletableFuture<O> futurePayload = getHeartbeatListener().retrievePayload(heartbeatMonitor.getHeartbeatTargetId()); final HeartbeatTarget<O> heartbeatTarget = heartbeatMonitor.getHeartbeatTarget(); if (futurePayload != null) { CompletableFuture<Void> requestHeartbeatFuture = futurePayload.thenAcceptAsync( payload -> heartbeatTarget.requestHeartbeat(getOwnResourceID(), payload), getExecutor()); requestHeartbeatFuture.exceptionally( (Throwable failure) -> { log.warn("Could not request the heartbeat from target {}.", heartbeatTarget, failure); return null; }); } else { heartbeatTarget.requestHeartbeat(getOwnResourceID(), null); } } } }
@Override public void run() { if (!stopped) { log.debug("Trigger heartbeat request."); for (HeartbeatMonitor<O> heartbeatMonitor : getHeartbeatTargets()) { CompletableFuture<O> futurePayload = getHeartbeatListener().retrievePayload(heartbeatMonitor.getHeartbeatTargetId()); final HeartbeatTarget<O> heartbeatTarget = heartbeatMonitor.getHeartbeatTarget(); if (futurePayload != null) { CompletableFuture<Void> requestHeartbeatFuture = futurePayload.thenAcceptAsync( payload -> heartbeatTarget.requestHeartbeat(getOwnResourceID(), payload), getExecutor()); requestHeartbeatFuture.exceptionally( (Throwable failure) -> { log.warn("Could not request the heartbeat from target {}.", heartbeatTarget, failure); return null; }); } else { heartbeatTarget.requestHeartbeat(getOwnResourceID(), null); } } } }
@Override public void run() { if (!stopped) { log.debug("Trigger heartbeat request."); for (HeartbeatMonitor<O> heartbeatMonitor : getHeartbeatTargets()) { CompletableFuture<O> futurePayload = getHeartbeatListener().retrievePayload(heartbeatMonitor.getHeartbeatTargetId()); final HeartbeatTarget<O> heartbeatTarget = heartbeatMonitor.getHeartbeatTarget(); if (futurePayload != null) { CompletableFuture<Void> requestHeartbeatFuture = futurePayload.thenAcceptAsync( payload -> heartbeatTarget.requestHeartbeat(getOwnResourceID(), payload), getExecutor()); requestHeartbeatFuture.exceptionally( (Throwable failure) -> { log.warn("Could not request the heartbeat from target {}.", heartbeatTarget, failure); return null; }); } else { heartbeatTarget.requestHeartbeat(getOwnResourceID(), null); } } } }
@Override public void monitorTarget(ResourceID resourceID, HeartbeatTarget<O> heartbeatTarget) { if (!stopped) { if (heartbeatTargets.containsKey(resourceID)) { log.debug("The target with resource ID {} is already been monitored.", resourceID); } else { HeartbeatManagerImpl.HeartbeatMonitor<O> heartbeatMonitor = new HeartbeatManagerImpl.HeartbeatMonitor<>( resourceID, heartbeatTarget, scheduledExecutor, heartbeatListener, heartbeatTimeoutIntervalMs); heartbeatTargets.put( resourceID, heartbeatMonitor); // check if we have stopped in the meantime (concurrent stop operation) if (stopped) { heartbeatMonitor.cancel(); heartbeatTargets.remove(resourceID); } } } }
@Override public void monitorTarget(ResourceID resourceID, HeartbeatTarget<O> heartbeatTarget) { if (!stopped) { if (heartbeatTargets.containsKey(resourceID)) { log.info("The target with resource ID {} is already been monitored.", resourceID); } else { HeartbeatManagerImpl.HeartbeatMonitor<O> heartbeatMonitor = new HeartbeatManagerImpl.HeartbeatMonitor<>( resourceID, heartbeatTarget, scheduledExecutor, heartbeatListener, heartbeatTimeoutIntervalMs); heartbeatTargets.put( resourceID, heartbeatMonitor); // check if we have stopped in the meantime (concurrent stop operation) if (stopped) { heartbeatMonitor.cancel(); heartbeatTargets.remove(resourceID); } } } }
@Override public void monitorTarget(ResourceID resourceID, HeartbeatTarget<O> heartbeatTarget) { if (!stopped) { if (heartbeatTargets.containsKey(resourceID)) { log.info("The target with resource ID {} is already been monitored.", resourceID); } else { HeartbeatManagerImpl.HeartbeatMonitor<O> heartbeatMonitor = new HeartbeatManagerImpl.HeartbeatMonitor<>( resourceID, heartbeatTarget, scheduledExecutor, heartbeatListener, heartbeatTimeoutIntervalMs); heartbeatTargets.put( resourceID, heartbeatMonitor); // check if we have stopped in the meantime (concurrent stop operation) if (stopped) { heartbeatMonitor.cancel(); heartbeatTargets.remove(resourceID); } } } }
HeartbeatMonitor( ResourceID resourceID, HeartbeatTarget<O> heartbeatTarget, ScheduledExecutor scheduledExecutor, HeartbeatListener<?, O> heartbeatListener, long heartbeatTimeoutIntervalMs) { this.resourceID = Preconditions.checkNotNull(resourceID); this.heartbeatTarget = Preconditions.checkNotNull(heartbeatTarget); this.scheduledExecutor = Preconditions.checkNotNull(scheduledExecutor); this.heartbeatListener = Preconditions.checkNotNull(heartbeatListener); Preconditions.checkArgument(heartbeatTimeoutIntervalMs >= 0L, "The heartbeat timeout interval has to be larger than 0."); this.heartbeatTimeoutIntervalMs = heartbeatTimeoutIntervalMs; lastHeartbeat = 0L; resetHeartbeatTimeout(heartbeatTimeoutIntervalMs); }
HeartbeatMonitor( ResourceID resourceID, HeartbeatTarget<O> heartbeatTarget, ScheduledExecutor scheduledExecutor, HeartbeatListener<?, O> heartbeatListener, long heartbeatTimeoutIntervalMs) { this.resourceID = Preconditions.checkNotNull(resourceID); this.heartbeatTarget = Preconditions.checkNotNull(heartbeatTarget); this.scheduledExecutor = Preconditions.checkNotNull(scheduledExecutor); this.heartbeatListener = Preconditions.checkNotNull(heartbeatListener); Preconditions.checkArgument(heartbeatTimeoutIntervalMs >= 0L, "The heartbeat timeout interval has to be larger than 0."); this.heartbeatTimeoutIntervalMs = heartbeatTimeoutIntervalMs; resetHeartbeatTimeout(heartbeatTimeoutIntervalMs); }
HeartbeatMonitor( ResourceID resourceID, HeartbeatTarget<O> heartbeatTarget, ScheduledExecutor scheduledExecutor, HeartbeatListener<?, O> heartbeatListener, long heartbeatTimeoutIntervalMs) { this.resourceID = Preconditions.checkNotNull(resourceID); this.heartbeatTarget = Preconditions.checkNotNull(heartbeatTarget); this.scheduledExecutor = Preconditions.checkNotNull(scheduledExecutor); this.heartbeatListener = Preconditions.checkNotNull(heartbeatListener); Preconditions.checkArgument(heartbeatTimeoutIntervalMs > 0L, "The heartbeat timeout interval has to be larger than 0."); this.heartbeatTimeoutIntervalMs = heartbeatTimeoutIntervalMs; lastHeartbeat = 0L; resetHeartbeatTimeout(heartbeatTimeoutIntervalMs); }
HeartbeatMonitor( ResourceID resourceID, HeartbeatTarget<O> heartbeatTarget, ScheduledExecutor scheduledExecutor, HeartbeatListener<?, O> heartbeatListener, long heartbeatTimeoutIntervalMs) { this.resourceID = Preconditions.checkNotNull(resourceID); this.heartbeatTarget = Preconditions.checkNotNull(heartbeatTarget); this.scheduledExecutor = Preconditions.checkNotNull(scheduledExecutor); this.heartbeatListener = Preconditions.checkNotNull(heartbeatListener); Preconditions.checkArgument(heartbeatTimeoutIntervalMs > 0L, "The heartbeat timeout interval has to be larger than 0."); this.heartbeatTimeoutIntervalMs = heartbeatTimeoutIntervalMs; lastHeartbeat = 0L; resetHeartbeatTimeout(heartbeatTimeoutIntervalMs); }
void reportHeartbeat() { lastHeartbeat = System.currentTimeMillis(); resetHeartbeatTimeout(heartbeatTimeoutIntervalMs); }
void reportHeartbeat() { lastHeartbeat = System.currentTimeMillis(); resetHeartbeatTimeout(heartbeatTimeoutIntervalMs); }
void reportHeartbeat() { lastHeartbeat = System.currentTimeMillis(); resetHeartbeatTimeout(heartbeatTimeoutIntervalMs); }
void reportHeartbeat() { resetHeartbeatTimeout(heartbeatTimeoutIntervalMs); }
@Override public long getLastHeartbeatFrom(ResourceID resourceId) { HeartbeatMonitor<O> heartbeatMonitor = heartbeatTargets.get(resourceId); if (heartbeatMonitor != null) { return heartbeatMonitor.getLastHeartbeat(); } else { return -1L; } }
void resetHeartbeatTimeout(long heartbeatTimeout) { if (state.get() == State.RUNNING) { cancelTimeout(); futureTimeout = scheduledExecutor.schedule(this, heartbeatTimeout, TimeUnit.MILLISECONDS); // Double check for concurrent accesses (e.g. a firing of the scheduled future) if (state.get() != State.RUNNING) { cancelTimeout(); } } }