Skip to content

Commit

Permalink
HDFS-16831. [RBF SBN] GetNamenodesForNameserviceId should shuffle Obs…
Browse files Browse the repository at this point in the history
…erver NameNodes every time
  • Loading branch information
ZanderXu committed Nov 2, 2022
1 parent 8396caa commit 15470ab
Showing 1 changed file with 32 additions and 1 deletion.
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 {
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) {
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

0 comments on commit 15470ab

Please sign in to comment.