-
Notifications
You must be signed in to change notification settings - Fork 2.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[HUDI-8781] Optimize executor memory usage during executing clustering (
#12515) * perf: optimize executor memory usage during executing clustering 1. optimize executor memory usage during executing clustering Signed-off-by: TheR1sing3un <[email protected]> * Cosmetic changes --------- Signed-off-by: TheR1sing3un <[email protected]> Co-authored-by: danny0405 <[email protected]>
- Loading branch information
1 parent
cb447c9
commit 9da3221
Showing
4 changed files
with
274 additions
and
56 deletions.
There are no files selected for viewing
This file contains 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
98 changes: 98 additions & 0 deletions
98
...i-client-common/src/main/java/org/apache/hudi/client/utils/LazyConcatenatingIterator.java
This file contains 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,98 @@ | ||
/* | ||
* 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.hudi.client.utils; | ||
|
||
import org.apache.hudi.common.util.ValidationUtils; | ||
import org.apache.hudi.common.util.collection.ClosableIterator; | ||
|
||
import java.util.LinkedList; | ||
import java.util.List; | ||
import java.util.Queue; | ||
import java.util.function.Supplier; | ||
|
||
/** | ||
* Provides iterator interface over List of iterators. Consumes all records from first iterator element | ||
* before moving to next iterator in the list. That is concatenating elements across multiple iterators. | ||
* | ||
* <p>Different with {@link ConcatenatingIterator}, the internal iterators are instantiated lazily. | ||
*/ | ||
public class LazyConcatenatingIterator<T> implements ClosableIterator<T> { | ||
|
||
private final Queue<Supplier<ClosableIterator<T>>> iteratorSuppliers; | ||
|
||
private ClosableIterator<T> itr; | ||
|
||
private boolean initialed = false; | ||
|
||
private boolean closed = false; | ||
|
||
public LazyConcatenatingIterator(List<Supplier<ClosableIterator<T>>> iteratorSuppliers) { | ||
this.iteratorSuppliers = new LinkedList<>(iteratorSuppliers); | ||
} | ||
|
||
@Override | ||
public void close() { | ||
if (!closed) { | ||
if (itr != null) { | ||
itr.close(); | ||
itr = null; | ||
} | ||
iteratorSuppliers.clear(); | ||
closed = true; | ||
} | ||
} | ||
|
||
@Override | ||
public boolean hasNext() { | ||
init(); | ||
while (itr != null) { | ||
if (itr.hasNext()) { | ||
return true; | ||
} | ||
// close current iterator | ||
this.itr.close(); | ||
if (!iteratorSuppliers.isEmpty()) { | ||
// move to the next | ||
itr = iteratorSuppliers.poll().get(); | ||
} else { | ||
itr = null; | ||
} | ||
} | ||
return false; | ||
} | ||
|
||
@Override | ||
public T next() { | ||
ValidationUtils.checkState(hasNext(), "No more elements left"); | ||
return itr.next(); | ||
} | ||
|
||
// ------------------------------------------------------------------------- | ||
// Utilities | ||
// ------------------------------------------------------------------------- | ||
|
||
private void init() { | ||
if (!initialed) { | ||
if (!this.iteratorSuppliers.isEmpty()) { | ||
this.itr = iteratorSuppliers.poll().get(); | ||
} | ||
initialed = true; | ||
} | ||
} | ||
} |
114 changes: 114 additions & 0 deletions
114
...hudi-client-common/src/test/java/org/apache/hudi/utils/TestLazyConcatenatingIterator.java
This file contains 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,114 @@ | ||
/* | ||
* 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.hudi.utils; | ||
|
||
import org.apache.hudi.client.utils.LazyConcatenatingIterator; | ||
import org.apache.hudi.common.util.collection.ClosableIterator; | ||
|
||
import org.junit.jupiter.api.Test; | ||
|
||
import java.util.ArrayList; | ||
import java.util.Arrays; | ||
import java.util.Collections; | ||
import java.util.Iterator; | ||
import java.util.List; | ||
import java.util.function.Supplier; | ||
|
||
import static org.junit.jupiter.api.Assertions.assertEquals; | ||
import static org.junit.jupiter.api.Assertions.assertFalse; | ||
import static org.junit.jupiter.api.Assertions.fail; | ||
|
||
public class TestLazyConcatenatingIterator { | ||
|
||
int initTimes; | ||
int closeTimes; | ||
|
||
private class MockClosableIterator implements ClosableIterator { | ||
|
||
Iterator<Integer> iterator; | ||
|
||
public MockClosableIterator(Iterator<Integer> iterator) { | ||
initTimes++; | ||
this.iterator = iterator; | ||
} | ||
|
||
@Override | ||
public void close() { | ||
closeTimes++; | ||
} | ||
|
||
@Override | ||
public boolean hasNext() { | ||
return iterator.hasNext(); | ||
} | ||
|
||
@Override | ||
public Object next() { | ||
return iterator.next(); | ||
} | ||
} | ||
|
||
// Simple test for iterator concatenation | ||
@Test | ||
public void testConcatBasic() { | ||
Supplier<ClosableIterator<Integer>> i1 = () -> new MockClosableIterator(Arrays.asList(5, 3, 2, 1).iterator()); | ||
Supplier<ClosableIterator<Integer>> i2 = () -> new MockClosableIterator(Collections.emptyIterator()); // empty iterator | ||
Supplier<ClosableIterator<Integer>> i3 = () -> new MockClosableIterator(Collections.singletonList(3).iterator()); | ||
|
||
LazyConcatenatingIterator<Integer> ci = new LazyConcatenatingIterator<>(Arrays.asList(i1, i2, i3)); | ||
|
||
assertEquals(0, initTimes); | ||
|
||
List<Integer> allElements = new ArrayList<>(); | ||
int count = 0; | ||
while (ci.hasNext()) { | ||
count++; | ||
if (count == 1) { | ||
assertEquals(1, initTimes); | ||
assertEquals(0, closeTimes); | ||
} | ||
if (count == 5) { | ||
assertEquals(3, initTimes); | ||
assertEquals(2, closeTimes); | ||
} | ||
allElements.add(ci.next()); | ||
} | ||
|
||
assertEquals(3, initTimes); | ||
assertEquals(3, closeTimes); | ||
|
||
assertEquals(5, allElements.size()); | ||
assertEquals(Arrays.asList(5, 3, 2, 1, 3), allElements); | ||
} | ||
|
||
@Test | ||
public void testConcatError() { | ||
Supplier<ClosableIterator<Integer>> i1 = () -> new MockClosableIterator(Collections.emptyIterator()); // empty iterator | ||
|
||
LazyConcatenatingIterator<Integer> ci = new LazyConcatenatingIterator<>(Collections.singletonList(i1)); | ||
assertFalse(ci.hasNext()); | ||
try { | ||
ci.next(); | ||
fail("expected error for empty iterator"); | ||
} catch (IllegalStateException e) { | ||
// | ||
} | ||
} | ||
|
||
} |
This file contains 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