-
Notifications
You must be signed in to change notification settings - Fork 3.8k
CircularList round-robin iterator for the KillUnusedSegments duty #16719
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
Merged
abhishekrb19
merged 12 commits into
apache:master
from
abhishekrb19:kill_roundrobin_iterator
Jul 26, 2024
Merged
Changes from all commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
08fad73
Round-robin iterator for datasources to kill.
abhishekrb19 5115e8e
Renames in RoundRobinIteratorTest.
abhishekrb19 d9c572b
Address review comments.
abhishekrb19 91b0dda
Add null check to input candidates.
abhishekrb19 fd61ce8
More commentary.
abhishekrb19 75a6744
Addres review feedback: downgrade some new info logs to debug; invert…
abhishekrb19 d0158b9
CircularList adjustments.
abhishekrb19 5fa342c
Updates to CircularList and cleanup RoundRobinInterator.
abhishekrb19 d2a92fe
Merge branch 'master' into kill_roundrobin_iterator
abhishekrb19 3c186a2
One more case and add more tests.
abhishekrb19 52dc479
Make advanceCursor private for now.
abhishekrb19 9263b9c
Review comments.
abhishekrb19 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
89 changes: 89 additions & 0 deletions
89
processing/src/main/java/org/apache/druid/collections/CircularList.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,89 @@ | ||
| /* | ||
| * 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.druid.collections; | ||
|
|
||
| import javax.annotation.concurrent.NotThreadSafe; | ||
| import java.util.ArrayList; | ||
| import java.util.Comparator; | ||
| import java.util.HashSet; | ||
| import java.util.Iterator; | ||
| import java.util.List; | ||
| import java.util.NoSuchElementException; | ||
| import java.util.Set; | ||
|
|
||
| /** | ||
| * A circular list that is backed by an ordered list of elements containing no duplicates. The list is ordered by the | ||
| * supplied comparator. The iterator keeps track of the current position, so iterating the list multiple times will | ||
| * resume from the last location and continue until a caller explicitly terminates it. | ||
| * <p> | ||
| * This class is not thread-safe and must be used from a single thread. | ||
| */ | ||
| @NotThreadSafe | ||
| public class CircularList<T> implements Iterable<T> | ||
| { | ||
| private final List<T> elements = new ArrayList<>(); | ||
| private int currentPosition; | ||
|
|
||
| public CircularList(final Set<T> elements, final Comparator<? super T> comparator) | ||
| { | ||
| this.elements.addAll(elements); | ||
| this.elements.sort(comparator); | ||
| this.currentPosition = -1; | ||
| } | ||
|
|
||
| @Override | ||
| public Iterator<T> iterator() | ||
| { | ||
| return new Iterator<T>() | ||
| { | ||
| @Override | ||
| public boolean hasNext() | ||
| { | ||
| return elements.size() > 0; | ||
| } | ||
|
|
||
| @Override | ||
| public T next() | ||
| { | ||
| if (!hasNext()) { | ||
| throw new NoSuchElementException(); | ||
| } | ||
|
|
||
| advanceCursor(); | ||
| return elements.get(currentPosition); | ||
| } | ||
|
|
||
| private void advanceCursor() | ||
| { | ||
| if (++currentPosition >= elements.size()) { | ||
| currentPosition = 0; | ||
| } | ||
| } | ||
| }; | ||
| } | ||
|
|
||
| /** | ||
| * @return true if the supplied set is equal to the set used to instantiate this circular list, otherwise false. | ||
| */ | ||
| public boolean equalsSet(final Set<T> inputSet) | ||
| { | ||
| return new HashSet<>(elements).equals(inputSet); | ||
| } | ||
| } |
135 changes: 135 additions & 0 deletions
135
processing/src/test/java/org/apache/druid/collections/CircularListTest.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,135 @@ | ||
| /* | ||
| * 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.druid.collections; | ||
|
|
||
| import com.google.common.collect.ImmutableList; | ||
| import com.google.common.collect.ImmutableSet; | ||
| import org.junit.Assert; | ||
| import org.junit.Test; | ||
|
|
||
| import java.util.ArrayList; | ||
| import java.util.Comparator; | ||
| import java.util.Iterator; | ||
| import java.util.List; | ||
| import java.util.NoSuchElementException; | ||
| import java.util.Set; | ||
|
|
||
| public class CircularListTest | ||
| { | ||
| @Test | ||
| public void testIterateInNaturalOrder() | ||
| { | ||
| final Set<String> input = ImmutableSet.of("b", "a", "c"); | ||
| final CircularList<String> circularList = new CircularList<>(input, Comparator.naturalOrder()); | ||
| final List<String> observedElements = new ArrayList<>(); | ||
| int cnt = 0; | ||
| for (String x : circularList) { | ||
| observedElements.add(x); | ||
| if (++cnt >= input.size()) { | ||
| break; | ||
| } | ||
| } | ||
| Assert.assertEquals(ImmutableList.of("a", "b", "c"), observedElements); | ||
| } | ||
|
|
||
| @Test | ||
| public void testIterateInReverseOrder() | ||
| { | ||
| final Set<Integer> input = ImmutableSet.of(-1, 100, 0, -4); | ||
| final CircularList<Integer> circularList = new CircularList<>(input, Comparator.reverseOrder()); | ||
| final List<Integer> observedElements = new ArrayList<>(); | ||
| int cnt = 0; | ||
| for (Integer x : circularList) { | ||
| observedElements.add(x); | ||
| if (++cnt >= 2 * input.size()) { | ||
| break; | ||
| } | ||
| } | ||
|
|
||
| Assert.assertEquals(ImmutableList.of(100, 0, -1, -4, 100, 0, -1, -4), observedElements); | ||
| } | ||
|
|
||
| @Test | ||
| public void testIteratorResumesFromLastPosition() | ||
| { | ||
| final Set<String> input = ImmutableSet.of("a", "b", "c", "d", "e", "f"); | ||
| final CircularList<String> circularList = new CircularList<>(input, Comparator.naturalOrder()); | ||
|
|
||
| List<String> observedElements = new ArrayList<>(); | ||
| int cnt = 0; | ||
| for (String element : circularList) { | ||
| observedElements.add(element); | ||
| if (++cnt >= input.size() / 2) { | ||
| break; | ||
| } | ||
| } | ||
|
|
||
| Assert.assertEquals(ImmutableList.of("a", "b", "c"), observedElements); | ||
|
|
||
| observedElements = new ArrayList<>(); | ||
| for (String element : circularList) { | ||
| observedElements.add(element); | ||
| // Resume and go till the end, and add two more elements looping around | ||
| if (++cnt == input.size() + 2) { | ||
| break; | ||
| } | ||
| } | ||
|
|
||
| Assert.assertEquals(ImmutableList.of("d", "e", "f", "a", "b"), observedElements); | ||
| } | ||
|
|
||
| @Test | ||
| public void testEqualsSet() | ||
| { | ||
| final Set<String> input = ImmutableSet.of("a", "b", "c"); | ||
| final CircularList<String> circularList = new CircularList<>(input, Comparator.naturalOrder()); | ||
| Assert.assertTrue(circularList.equalsSet(ImmutableSet.of("b", "a", "c"))); | ||
| Assert.assertFalse(circularList.equalsSet(ImmutableSet.of("c"))); | ||
| Assert.assertFalse(circularList.equalsSet(ImmutableSet.of("a", "c"))); | ||
| } | ||
|
|
||
| @Test | ||
| public void testEmptyIterator() | ||
| { | ||
| final Set<String> input = ImmutableSet.of(); | ||
| final CircularList<String> circularList = new CircularList<>(input, Comparator.naturalOrder()); | ||
| final List<String> observedElements = new ArrayList<>(); | ||
|
|
||
| int cnt = 0; | ||
| for (String x : circularList) { | ||
| observedElements.add(x); | ||
| if (++cnt >= input.size()) { | ||
| break; | ||
| } | ||
| } | ||
| Assert.assertEquals(ImmutableList.of(), observedElements); | ||
| } | ||
|
|
||
| @Test | ||
| public void testNextOnEmptyIteratorThrowsException() | ||
| { | ||
| final Set<String> input = ImmutableSet.of(); | ||
| final CircularList<String> circularList = new CircularList<>(input, Comparator.naturalOrder()); | ||
|
|
||
| final Iterator<String> iterator = circularList.iterator(); | ||
| Assert.assertFalse(iterator.hasNext()); | ||
| Assert.assertThrows(NoSuchElementException.class, iterator::next); | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.