LOG.debug("I {} have to check replication responsibilities for {}.", selfAddress, myResponsibleLocations);
for (final Number160 myResponsibleLocation : myResponsibleLocations) {
if (!nRootReplication) {
// use 0-root replication strategy
PeerAddress closest = closest(myResponsibleLocation);
if (!closest.peerId().equals(selfAddress.peerId())) {
if (isInReplicationRange(myResponsibleLocation, selfAddress, replicationFactor)) {
if (backend.updateResponsibilities(myResponsibleLocation, closest.peerId())) {
LOG.debug("I {} didn't know that {} is responsible for {}.", selfAddress, closest,
myResponsibleLocation);
// notify that someone else is now responsible for the
// content with key responsibleLocations
notifyOtherResponsible(myResponsibleLocation, closest);
// cancel any pending notifyMeResponsible*, as we are
// not responsible anymore.
} else {
LOG.debug("I {} know already that {} is responsible for {}.", selfAddress, closest,
myResponsibleLocation);
}
} else {
// notify closest replica node about responsibility
notifyOtherResponsible(myResponsibleLocation, closest);
LOG.debug("I {} am no more in the replica set of {}.", selfAddress, myResponsibleLocation);
backend.removeResponsibility(myResponsibleLocation, keepData);
}
} else if (isInReplicationRange(myResponsibleLocation, peerAddress, replicationFactor)) {
LOG.debug("{} is in the replica set for {}.", peerAddress, myResponsibleLocation);
// we are still responsible, but a new peer joined and if it
// is within the x close peers, we need to
// replicate
if (backend.updateResponsibilities(myResponsibleLocation,
selfAddress.peerId())) {
LOG.debug("I {} didn't know that I'm responsible for {}.", selfAddress,
myResponsibleLocation);
// I figured out I'm the new responsible, so check all
// my peer in the replication range
notifyMeResponsible(myResponsibleLocation);
} else {
LOG.debug("I {} already know that I'm responsible for {}.", selfAddress,
myResponsibleLocation);
// new peer joined, I'm responsible, so replicate to
// that peer
notifyMeResponsible(myResponsibleLocation, peerAddress);
}
}
} else {
// check if newly joined peer has duty to replicate
if (isInReplicationRange(myResponsibleLocation, peerAddress, replicationFactor)) {
// check if I still have to replicate
if (isInReplicationRange(myResponsibleLocation, selfAddress, replicationFactor)) {
LOG.debug("I {} and newly joined peer {} have replication responibility for {}.",
selfAddress, peerAddress, myResponsibleLocation);
// newly joined peer has to get notified
notifyMeResponsible(myResponsibleLocation, peerAddress);
} else {
LOG.debug("I {} lose and newly joined peer {} gets replication responsibility for {}.",
selfAddress, peerAddress, myResponsibleLocation);
// newly joined peer has to get notified
FutureForkJoin<FutureDone<Void>> futureForkJoin = notifyOtherResponsible(
myResponsibleLocation, peerAddress);
// I'm not in replication range, I don't need to know
// about all responsibility entries to the given key.
// Remove responsibility after notifying newly joined peer.
futureForkJoin.addListener(new BaseFutureListener<BaseFuture>() {
@Override
public void operationComplete(BaseFuture future)
throws Exception {
if (future.isSuccess()) {
backend.removeResponsibility(
myResponsibleLocation,
keepData);
} else {
LOG.debug(
"I {} couldn't notify newly joined peer {} about responsibility for {}."
+ " I keep responsibility.",
selfAddress, peerAddress,
myResponsibleLocation);
}
}
@Override
public void exceptionCaught(Throwable t)
throws Exception {
LOG.error("Unexcepted exception ocurred.", t);
}
});
}
} else {
// check if I still have to replicate
if (!isInReplicationRange(myResponsibleLocation,
selfAddress, replicationFactor)) {
LOG.debug(
"I {} and newly joined peer {} don't have to replicate {}.",
selfAddress, peerAddress, myResponsibleLocation);
// I'm not in replication range, I don't need to know
// about all responsibility entries to the given key
final PeerAddress closest = closest(myResponsibleLocation);
// Try to notify another replica node.
FutureForkJoin<FutureDone<Void>> futureForkJoin = notifyOtherResponsible(
myResponsibleLocation, closest);
// Remove responsibility after successful notifying.
futureForkJoin