adoroszlai commented on code in PR #9633: URL: https://github.com/apache/ozone/pull/9633#discussion_r2691322590
########## hadoop-hdds/server-scm/src/main/java/org/apache/hadoop/hdds/scm/pipeline/SortedList.java: ########## @@ -0,0 +1,270 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.hadoop.hdds.scm.pipeline; + +import java.util.ArrayList; +import java.util.Collection; +import java.util.Collections; +import java.util.Iterator; +import java.util.List; +import java.util.ListIterator; +import java.util.Map; +import java.util.Objects; +import java.util.SortedMap; +import java.util.TreeMap; +import java.util.function.BiFunction; +import java.util.function.Predicate; + +/** + * A sorted list using bucket-sort. + * The elements are sorted by their weights + * while different elements may have the same weight. + * <p> + * The number of buckets is assumed to be much smaller than the number of elements. + * For examples, a cluster may have 5,000 datanodes (elements) + * but the number of pipelines (buckets) is always less than 100. + * Therefore, this class (n log b) is more efficient than the usual sorting (n log n), + * where n is the number of elements and b is the number of buckets. + * <p> + * Note that some unused methods in {@link List} are unsupported. + * <p> + * This class is not threadsafe. + */ +final class SortedList<E> implements List<E> { + private final Class<E> clazz; + private final SortedMap<Integer, List<E>> buckets = new TreeMap<>(); + private int numElements = 0; + + SortedList(Class<E> clazz) { + Objects.requireNonNull(clazz, "clazz == null"); + this.clazz = clazz; + } + + @Override + public int size() { + return numElements; + } + + @Override + public boolean isEmpty() { + return size() == 0; + } + + /** Add the given element with the given weight to this list. */ + public boolean add(E element, int weight) { + Objects.requireNonNull(element, "element == null"); + buckets.computeIfAbsent(weight, k -> new ArrayList<>(64)).add(element); + numElements++; + return true; + } + + private E getOrRmove(String name, int index, BiFunction<List<E>, Integer, E> method) { Review Comment: typo: `getOrRmove -> getOrRemove` ########## hadoop-hdds/server-scm/src/test/java/org/apache/hadoop/hdds/scm/pipeline/TestSortedList.java: ########## @@ -0,0 +1,160 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.hadoop.hdds.scm.pipeline; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertTrue; + +import java.util.ArrayList; +import java.util.Collections; +import java.util.Comparator; +import java.util.List; +import java.util.Random; +import org.junit.jupiter.api.Test; + +/** Test {@link SortedList}. */ +public class TestSortedList { + private static final Random RANDOM = new Random(); + private static final int LOOP = 2_000; + private static final int MAX_WEIGHT = LOOP / 100; + private static int id = 0; + + static class Element implements Comparable<Element> { + private final int weight; + private final String value = "e" + ++id; + + Element(int weight) { + this.weight = weight; + } + + int getWeight() { + return weight; + } + + String getValue() { + return value; + } + + @Override + public int compareTo(Element that) { + return Comparator.comparingInt(Element::getWeight) + .thenComparing(Element::getValue) + .compare(this, that); + } + + @Override + public int hashCode() { + return weight; + } + + @Override + public boolean equals(Object obj) { + if (obj == this) { + return true; + } else if (!(obj instanceof Element)) { + return false; + } + final Element that = (Element) obj; + if (this.value.equals(that.value)) { + assertEquals(this.weight, that.weight); + return true; + } + return false; + } + + @Override + public String toString() { + return value + "(" + weight + ")"; + } + } + + enum Method { + ADD, REMOVE_JAVA, REMOVE_SORTED; + + static Method random() { + final int r = RANDOM.nextInt(100); + return r < 60 ? ADD : r < 80 ? REMOVE_JAVA : REMOVE_SORTED; + } + } + + @Test + public void test() { + final List<Element> javaList = new ArrayList<>(); + final SortedList<Element> sortedList = new SortedList<>(Element.class); + + for (int i = 0; i < LOOP; i++) { + final int size = javaList.size(); + final Method method = javaList.isEmpty() ? Method.ADD : Method.random(); + System.out.printf("%5d (size=%5d): %14s ", i, size, method); + switch (method) { + case ADD: + add(javaList, sortedList); + break; + case REMOVE_JAVA: + remove(javaList, sortedList); + break; + case REMOVE_SORTED: + remove(sortedList, javaList); + break; + default: + throw new AssertionError(); + } + } + } + + static void add(List<Element> javaList, SortedList<Element> sortedList) { + final Element e = new Element(RANDOM.nextInt(MAX_WEIGHT)); + System.out.println(e); + javaList.add(e); + Collections.sort(javaList); + sortedList.add(e, e.weight); + assertLists(javaList, sortedList); + } + + static void remove(List<Element> left, List<Element> right) { + final Element e = left.remove(RANDOM.nextInt(left.size())); + System.out.println(e); + assertTrue(right.remove(e)); + assertLists(left, right); + + assertFalse(left.remove(e)); + assertFalse(right.remove(e)); + } + + static void assertLists(List<Element> left, List<Element> right) { + assertEquals(left.isEmpty(), right.isEmpty()); + assertOrdering(left, right); + assertOrdering(right, left); + } + + static void assertOrdering(List<Element> ordering, List<Element> contains) { + final int size = contains.size(); + assertEquals(size, ordering.size()); + + int min = -1; + int count = 0; + for (Element e : ordering) { + count++; + assertTrue(e.weight >= min); + min = e.weight; + assertTrue(contains.contains(e)); + } Review Comment: We can assert that both lists have `e` at the same position instead of just `contains()`, but need to sort `value` numerically. ```diff diff --git hadoop-hdds/server-scm/src/test/java/org/apache/hadoop/hdds/scm/pipeline/TestSortedList.java hadoop-hdds/server-scm/src/test/java/org/apache/hadoop/hdds/scm/pipeline/TestSortedList.java index 724e6facbb..4c49515f2f 100644 --- hadoop-hdds/server-scm/src/test/java/org/apache/hadoop/hdds/scm/pipeline/TestSortedList.java +++ hadoop-hdds/server-scm/src/test/java/org/apache/hadoop/hdds/scm/pipeline/TestSortedList.java @@ -17,13 +17,16 @@ package org.apache.hadoop.hdds.scm.pipeline; +import static org.assertj.core.api.Assertions.assertThat; import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertSame; import static org.junit.jupiter.api.Assertions.assertTrue; import java.util.ArrayList; import java.util.Collections; import java.util.Comparator; +import java.util.Iterator; import java.util.List; import java.util.Random; import org.junit.jupiter.api.Test; @@ -37,7 +40,7 @@ public class TestSortedList { static class Element implements Comparable<Element> { private final int weight; - private final String value = "e" + ++id; + private final String value = "e" + String.format("%04d", ++id); Element(int weight) { this.weight = weight; @@ -149,11 +152,14 @@ static void assertOrdering(List<Element> ordering, List<Element> contains) { int min = -1; int count = 0; + final Iterator<Element> actual = contains.iterator(); for (Element e : ordering) { - count++; - assertTrue(e.weight >= min); + assertThat(e.weight).isGreaterThanOrEqualTo(min); min = e.weight; - assertTrue(contains.contains(e)); + assertThat(contains).contains(e); + assertTrue(actual.hasNext()); + assertSame(e, actual.next(), "[" + count + "]"); + count++; } assertEquals(size, count, () -> ordering.getClass().getSimpleName() + " " + ordering); } ``` -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected] --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
