Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

HDFS-16831. [RBF SBN] GetNamenodesForNameserviceId should shuffle Observer NameNodes every time #5098

Merged
merged 7 commits into from
Dec 23, 2022
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
import java.util.concurrent.ConcurrentHashMap;

import org.apache.commons.lang3.tuple.Pair;
import org.apache.hadoop.classification.VisibleForTesting;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hdfs.server.federation.store.DisabledNameserviceStore;
import org.apache.hadoop.hdfs.server.federation.store.MembershipStore;
Expand Down Expand Up @@ -189,13 +190,43 @@ private void updateNameNodeState(final String nsId,
}
}

@VisibleForTesting
public <T extends FederationNamenodeContext> List<T> shuffleObserverNN(
List<T> inputNameNodes, boolean listObserversFirst) {
if (!listObserversFirst) {
return inputNameNodes;
} else {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No ned for elses when we do the return.

List<T> observerNNList = new ArrayList<>();
List<T> activeAndStandbyList = new ArrayList<>();
for (T t : inputNameNodes) {
if (t.getState() == OBSERVER) {
observerNNList.add(t);
} else {
activeAndStandbyList.add(t);
}
}

if (observerNNList.size() <= 1) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We could've done this check earlier.

return inputNameNodes;
} else {
List<T> ret = new ArrayList<>(observerNNList.size() + activeAndStandbyList.size());
Collections.shuffle(observerNNList);
// No need because the inputNameNodes has already been sorted
// activeAndStandbyList.sort(new NamenodePriorityComparator());
ret.addAll(observerNNList);
ret.addAll(activeAndStandbyList);
return ret;
}
}
}

@Override
public List<? extends FederationNamenodeContext> getNamenodesForNameserviceId(
final String nsId, boolean listObserversFirst) throws IOException {

List<? extends FederationNamenodeContext> ret = cacheNS.get(Pair.of(nsId, listObserversFirst));
if (ret != null) {
return ret;
return shuffleObserverNN(ret, listObserversFirst);
}

// Not cached, generate the value
Expand Down