/** * Indicates if a peer needs a maintenance check. * * @param peerStatatistic The peer with its statistics * @return True if the peer needs a maintenance check */ protected boolean needMaintenance(final PeerStatatistic peerStatatistic) { final int onlineSec = peerStatatistic.onlineTime() / 1000; int index; if (onlineSec <= 0) { index = 0; } else { index = intervalSeconds.length - 1; for (int i = 0; i < intervalSeconds.length; i++) { // interval is 2,4,8,16,32,64 // examples // I have seen a peer online for 5 sec -> next interval to check is 8 // I have seen a peer online for 4 sec -> next interval to check is 4 // I have seen a peer online for 17 sec -> next interval to check is 32 // I have seen a peer online for 112321 sec -> next interval to check is 64 if (intervalSeconds[i] >= onlineSec) { index = i; break; } } } final int time = intervalSeconds[index]; final long lastTimeWhenChecked = Timings.currentTimeMillis() - peerStatatistic.getLastSeenOnline(); return lastTimeWhenChecked > TimeUnit.SECONDS.toMillis(time); }
private void addToMaintenanceQueue(PeerAddress remotePeer) { if (maintenanceTimeoutsSeconds.length == 0) return; long scheduledCheck; if (peerMapStat.getLastSeenOnlineTime(remotePeer) == 0) { // we need to check now! scheduledCheck = Timings.currentTimeMillis(); } else { // check for next schedule int checked = peerMapStat.getChecked(remotePeer); if (checked >= maintenanceTimeoutsSeconds.length) checked = maintenanceTimeoutsSeconds.length - 1; scheduledCheck = Timings.currentTimeMillis() + (maintenanceTimeoutsSeconds[checked] * 1000L); } synchronized (maintenance) { maintenance.put(remotePeer, scheduledCheck); } }
/** * Checks if this peer has been removed. A peer that has been removed will be stored in a cache * list for a certain time. This method is tread-safe * * @param node The node to check * @return True if the peer has been removed recently */ public boolean isPeerRemovedTemporarly(PeerAddress remotePeer) { Log log; synchronized (peerOfflineLogs) { log = peerOfflineLogs.get(remotePeer); } if (log != null) { synchronized (log) { if (shouldPeerBeRemoved(log)) return true; else if (Timings.currentTimeMillis() - log.getLastOffline() > cacheTimeout) { // remove the peer if timeout occured synchronized (peerOfflineLogs) { peerOfflineLogs.remove(remotePeer); } } } } return false; }
public Collection<PeerAddress> peersForMaintenance() { Collection<PeerAddress> result = new ArrayList<PeerAddress>(); final long now = Timings.currentTimeMillis(); synchronized (maintenance) { for (Iterator<Map.Entry<PeerAddress, Long>> iterator = maintenance.entrySet().iterator(); iterator.hasNext(); ) { Map.Entry<PeerAddress, Long> entry = iterator.next(); if (entry.getValue() <= now) { iterator.remove(); result.add(entry.getKey()); } } } // add to maintenance queue with new timings for (PeerAddress peerAddress : result) { addToMaintenanceQueue(peerAddress); } return result; }
private void inc() { counter++; lastOffline = Timings.currentTimeMillis(); }
private boolean shouldPeerBeRemoved(Log log) { return Timings.currentTimeMillis() - log.getLastOffline() <= cacheTimeout && log.getCounter() >= maxFail; }
private void set(int counter) { this.counter = counter; lastOffline = Timings.currentTimeMillis(); }