-
Notifications
You must be signed in to change notification settings - Fork 78
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Improve performance of connector by skipping unchanged files and batc…
…hing access to kafka connect offsets
- Loading branch information
1 parent
588d310
commit 03d81ba
Showing
7 changed files
with
233 additions
and
41 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
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
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
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
34 changes: 34 additions & 0 deletions
34
src/main/java/com/github/mmolimar/kafka/connect/fs/util/IteratorUtils.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,34 @@ | ||
package com.github.mmolimar.kafka.connect.fs.util; | ||
|
||
import java.util.ArrayList; | ||
import java.util.Iterator; | ||
import java.util.List; | ||
import java.util.stream.Stream; | ||
import java.util.stream.StreamSupport; | ||
|
||
public class IteratorUtils { | ||
public static <T> Iterator<List<T>> chunkIterator(Iterator<T> iterator, int elementsPerChunk){ | ||
if(elementsPerChunk <= 0){ | ||
throw new IllegalArgumentException(String.format("elementsPerChunk must be greater than 0 but was set to %d", elementsPerChunk)); | ||
} | ||
return new Iterator<List<T>>() { | ||
|
||
public boolean hasNext() { | ||
return iterator.hasNext(); | ||
} | ||
|
||
public List<T> next() { | ||
List<T> result = new ArrayList<>(elementsPerChunk); | ||
for (int i = 0; i < elementsPerChunk && iterator.hasNext(); i++) { | ||
result.add(iterator.next()); | ||
} | ||
return result; | ||
} | ||
}; | ||
} | ||
|
||
public static <T> Stream<T> asStream(Iterator<T> src) { | ||
Iterable<T> iterable = () -> src; | ||
return StreamSupport.stream(iterable.spliterator(), false); | ||
} | ||
} |
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
89 changes: 89 additions & 0 deletions
89
src/test/java/com/github/mmolimar/kafka/connect/fs/util/IteratorUtilsTest.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,89 @@ | ||
package com.github.mmolimar.kafka.connect.fs.util; | ||
|
||
import org.junit.jupiter.api.Test; | ||
|
||
import java.util.ArrayList; | ||
import java.util.Iterator; | ||
import java.util.List; | ||
import java.util.stream.Collectors; | ||
import java.util.stream.IntStream; | ||
|
||
import static com.github.mmolimar.kafka.connect.fs.util.IteratorUtils.asStream; | ||
import static org.junit.jupiter.api.Assertions.assertEquals; | ||
import static org.junit.jupiter.api.Assertions.assertThrows; | ||
|
||
public class IteratorUtilsTest { | ||
|
||
@Test | ||
public void testIteratorChunkingWorks() { | ||
Iterator<Integer> iterator = IntStream.rangeClosed(0, 9).boxed().iterator(); | ||
Iterator<List<Integer>> chunkedIterator = IteratorUtils.chunkIterator(iterator, 2); | ||
|
||
List<List<Integer>> materializedChunkedIterator = asStream(chunkedIterator).collect(Collectors.toList()); | ||
ArrayList<ArrayList<Integer>> expected = new ArrayList<ArrayList<Integer>>(){{ | ||
add(new ArrayList<Integer>(){{ | ||
add(0); add(1); | ||
}}); | ||
add(new ArrayList<Integer>(){{ | ||
add(2); add(3); | ||
}}); | ||
add(new ArrayList<Integer>(){{ | ||
add(4); add(5); | ||
}}); | ||
add(new ArrayList<Integer>(){{ | ||
add(6); add(7); | ||
}}); | ||
add(new ArrayList<Integer>(){{ | ||
add(8); add(9); | ||
}}); | ||
}}; | ||
|
||
assertEquals(expected, materializedChunkedIterator); | ||
} | ||
|
||
@Test | ||
public void testIteratorChunkingWorksWithUnevenChunks() { | ||
Iterator<Integer> iterator = IntStream.rangeClosed(0, 4).boxed().iterator(); | ||
Iterator<List<Integer>> chunkedIterator = IteratorUtils.chunkIterator(iterator, 2); | ||
|
||
List<List<Integer>> materializedChunkedIterator = asStream(chunkedIterator).collect(Collectors.toList()); | ||
ArrayList<ArrayList<Integer>> expected = new ArrayList<ArrayList<Integer>>(){{ | ||
add(new ArrayList<Integer>(){{ | ||
add(0); add(1); | ||
}}); | ||
add(new ArrayList<Integer>(){{ | ||
add(2); add(3); | ||
}}); | ||
add(new ArrayList<Integer>(){{ | ||
add(4); | ||
}}); | ||
}}; | ||
|
||
assertEquals(expected, materializedChunkedIterator); | ||
} | ||
|
||
@Test | ||
public void testIteratorChunkingWithChunkGreaterThanNumElementsWorks() { | ||
Iterator<Integer> iterator = IntStream.rangeClosed(0, 2).boxed().iterator(); | ||
Iterator<List<Integer>> chunkedIterator = IteratorUtils.chunkIterator(iterator, 5); | ||
|
||
List<List<Integer>> materializedChunkedIterator = asStream(chunkedIterator).collect(Collectors.toList()); | ||
ArrayList<ArrayList<Integer>> expected = new ArrayList<ArrayList<Integer>>(){{ | ||
add(new ArrayList<Integer>(){{ | ||
add(0); add(1); add(2); | ||
}}); | ||
}}; | ||
|
||
assertEquals(expected, materializedChunkedIterator); | ||
} | ||
|
||
@Test | ||
public void testIteratorChunkingThrowsWithInvalidChunkSize() { | ||
Iterator<Integer> iterator = IntStream.rangeClosed(0, 2).boxed().iterator(); | ||
|
||
assertThrows(IllegalArgumentException.class, () -> { | ||
IteratorUtils.chunkIterator(iterator, 0); | ||
}); | ||
|
||
} | ||
} |