@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); } } }
/** * Set the peer address to bootstrap to. Please note that the peer address needs to know the * peerID of the bootstrap peer. If this is not known, use {@link #setInetAddress(InetAddress)} * instead. * * @param peerAddress The full address of the peer to bootstrap to (including the peerID of the * bootstrap peer). * @return this instance */ public BootstrapBuilder setPeerAddress(final PeerAddress peerAddress) { if (peerAddress != null && peerAddress.getPeerId().equals(Number160.ZERO)) { logger.warn( "You provided a peer address with peerID zero. " + "You won't be able to bootstrap since no peer can have a peerID set to zero"); return this; } this.peerAddress = peerAddress; return this; }
public static void routing(Number160 key, Peer[] peers, int start) { System.out.println("routing: searching for key " + key); NavigableSet<PeerAddress> pa1 = new TreeSet<PeerAddress>(PeerMap.createComparator(key)); NavigableSet<PeerAddress> queried = new TreeSet<PeerAddress>(PeerMap.createComparator(key)); Number160 result = Number160.ZERO; Number160 resultPeer = new Number160("0xd75d1a3d57841fbc9e2a3d175d6a35dc2e15b9f"); int round = 0; while (!resultPeer.equals(result)) { System.out.println("round " + round); round++; pa1.addAll(peers[start].getPeerBean().peerMap().getAll()); queried.add(peers[start].getPeerAddress()); System.out.println("closest so far: " + queried.first()); PeerAddress next = pa1.pollFirst(); while (queried.contains(next)) { next = pa1.pollFirst(); } result = next.getPeerId(); start = findNr(next.getPeerId().toString(), peers); } }
@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); } } }