@Override public void peerInserted(final PeerAddress peerAddress, final boolean verified) { if (!isReplicationEnabled() || !verified) { return; } LOG.debug("The peer {} was inserted in my map. I'm {}", peerAddress, selfAddress); // check if we should change responsibility. Collection<Number160> myResponsibleLocations = replicationStorage.findContentForResponsiblePeerID(selfAddress.getPeerId()); LOG.debug("I ({}) am currently responsibel for {}", selfAddress, myResponsibleLocations); for (Number160 myResponsibleLocation : myResponsibleLocations) { PeerAddress closest = closest(myResponsibleLocation); if (!closest.getPeerId().equals(selfAddress.getPeerId())) { if (replicationStorage.updateResponsibilities(myResponsibleLocation, closest.getPeerId())) { // notify that someone else is now responsible for the // content with key responsibleLocations notifyOtherResponsible(myResponsibleLocation, closest, false); // cancel any pending notifyMeResponsible*, as we are not responsible anymore. } } else if (isInReplicationRange(myResponsibleLocation, peerAddress, replicationFactor)) { // we are still responsible, but a new peer joined and if it is within the x close peers, we // need to // replicate if (replicationStorage.updateResponsibilities( myResponsibleLocation, selfAddress.getPeerId())) { // I figured out I'm the new responsible, so check all my peer in the replication range notifyMeResponsible(myResponsibleLocation); } else { // new peer joined, I'm responsible, so replicate to that peer notifyMeResponsible(myResponsibleLocation, peerAddress); } } } }
/** * Update responsibilities. This happens for a put / add. * * @param locationKey The location key. */ public void updateAndNotifyResponsibilities(final Number160 locationKey) { if (!isReplicationEnabled()) { return; } PeerAddress closest = closest(locationKey); if (closest.getPeerId().equals(selfAddress.getPeerId())) { if (replicationStorage.updateResponsibilities(locationKey, closest.getPeerId())) { // I am responsible for this content notifyMeResponsible(locationKey); } } else { if (replicationStorage.updateResponsibilities(locationKey, closest.getPeerId())) { // notify that someone else is now responsible for the // content with key responsibleLocations notifyOtherResponsible(locationKey, closest, false); } } }
@Override public void peerRemoved(final PeerAddress peerAddress, final PeerStatatistic peerStatatistic) { if (!isReplicationEnabled()) { return; } // check if we should change responsibility. Collection<Number160> otherResponsibleLocations = replicationStorage.findContentForResponsiblePeerID(peerAddress.getPeerId()); Collection<Number160> myResponsibleLocations = replicationStorage.findContentForResponsiblePeerID(selfAddress.getPeerId()); // check if we are now responsible for content where the other peer was responsible for (Number160 otherResponsibleLocation : otherResponsibleLocations) { PeerAddress closest = closest(otherResponsibleLocation); if (closest.getPeerId().equals(selfAddress.getPeerId())) { if (replicationStorage.updateResponsibilities( otherResponsibleLocation, closest.getPeerId())) { // notify that someone I'm now responsible for the // content // with key responsibleLocations notifyMeResponsible(otherResponsibleLocation); // we don't need to check this again, so remove it from the list if present myResponsibleLocations.remove(otherResponsibleLocation); } } else { if (replicationStorage.updateResponsibilities( otherResponsibleLocation, closest.getPeerId())) { LOG.debug("We should check if the closer peer has the content"); notifyOtherResponsible(otherResponsibleLocation, closest, true); } } } // now check for our responsibilities. If a peer is gone and it was in the replication range, we // need make sure // we have enough copies for (Number160 myResponsibleLocation : myResponsibleLocations) { if (isInReplicationRange(myResponsibleLocation, peerAddress, replicationFactor)) { notifyMeResponsible(myResponsibleLocation); } } }