You are viewing a plain text version of this content. The canonical link for it is here.
Posted to issues@ozone.apache.org by "smengcl (via GitHub)" <gi...@apache.org> on 2025/08/06 21:47:35 UTC
[PR] HDDS-13005. MinHeap based SstFileSetReader to return unique results in sorted order [ozone]
smengcl opened a new pull request, #8912:
URL: https://github.com/apache/ozone/pull/8912
## What changes were proposed in this pull request?
Currently SstFileSetReader returns non unique entries from a given set of sst files. We need to implement a MinHeap based SstFileSetIterator which should return a distinct entries and in sorted order of their keys. Effectively acting like a compaction Iterator for the SstFiles.
## What is the link to the Apache JIRA
https://issues.apache.org/jira/browse/HDDS-13005
## How was this patch tested?
- Added and existing test cases in `TestSstFileSetReader`.
--
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: issues-unsubscribe@ozone.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@ozone.apache.org
For additional commands, e-mail: issues-help@ozone.apache.org
Re: [PR] HDDS-13005. MinHeap based SstFileSetReader to return unique results in sorted order [ozone]
Posted by "swamirishi (via GitHub)" <gi...@apache.org>.
swamirishi commented on code in PR #8912:
URL: https://github.com/apache/ozone/pull/8912#discussion_r2341346661
##########
hadoop-hdds/rocksdb-checkpoint-differ/src/main/java/org/apache/ozone/rocksdb/util/SstFileSetReader.java:
##########
@@ -233,71 +235,146 @@ public String next() {
}
}
- private abstract static class MultipleSstFileIterator<T> implements ClosableIterator<T> {
+ /**
+ * A wrapper class that holds an iterator and its current value for heap operations.
+ */
+ private static class HeapEntryWithFileIdx<T extends Comparable<T>>
Review Comment:
nit: Change class name
```suggestion
private static class HeapEntry<T extends Comparable<T>>
```
##########
hadoop-hdds/rocksdb-checkpoint-differ/src/main/java/org/apache/ozone/rocksdb/util/SstFileSetReader.java:
##########
@@ -233,71 +235,146 @@ public String next() {
}
}
- private abstract static class MultipleSstFileIterator<T> implements ClosableIterator<T> {
+ /**
+ * A wrapper class that holds an iterator and its current value for heap operations.
+ */
+ private static class HeapEntryWithFileIdx<T extends Comparable<T>>
+ implements Comparable<HeapEntryWithFileIdx<T>>, Closeable {
+ private final ClosableIterator<T> iterator;
+ private T currentKey;
+
+ HeapEntryWithFileIdx(ClosableIterator<T> iterator, int fileIndex) {
+ this.iterator = iterator;
+ advance();
+ }
+
+ @Override
+ public void close() {
+ iterator.close();
+ }
+
+ boolean advance() {
+ if (iterator.hasNext()) {
+ currentKey = iterator.next();
+ return true;
+ } else {
+ currentKey = null;
+ return false;
+ }
+ }
+
+ T getCurrentKey() {
+ return currentKey;
+ }
+
+ @Override
+ public int compareTo(@Nonnull HeapEntryWithFileIdx<T> other) {
+ if (this.currentKey == null && other.currentKey == null) {
+ return 0;
+ }
+ if (this.currentKey == null) {
+ return 1;
+ }
+ if (other.currentKey == null) {
+ return -1;
+ }
+ return this.currentKey.compareTo(other.currentKey);
+ }
+
+ @Override
+ public boolean equals(Object obj) {
+ if (this == obj) {
+ return true;
+ }
+ if (obj == null || getClass() != obj.getClass()) {
+ return false;
+ }
- private final Iterator<String> fileNameIterator;
+ HeapEntryWithFileIdx<T> other = (HeapEntryWithFileIdx<T>) obj;
+ return compareTo(other) == 0;
+ }
- private String currentFile;
- private ClosableIterator<T> currentFileIterator;
+ @Override
+ public int hashCode() {
+ return Objects.hash(iterator, currentKey);
+ }
+ }
- private MultipleSstFileIterator(Collection<String> files) {
- this.fileNameIterator = files.iterator();
+ /**
+ * The MultipleSstFileIterator class is an abstract base for iterating over multiple SST files.
+ * It uses a PriorityQueue to merge keys from all files in sorted order.
+ * Each file's iterator is wrapped in a HeapEntryWithFileIdx object,
+ * which ensures stable ordering for identical keys by considering the file index.
+ * @param <T>
+ */
+ private abstract static class MultipleSstFileIterator<T extends Comparable<T>> implements ClosableIterator<T> {
+ private final PriorityQueue<HeapEntryWithFileIdx<T>> minHeap;
+
+ private MultipleSstFileIterator(Collection<String> sstFiles) {
+ this.minHeap = new PriorityQueue<>();
init();
+ initMinHeap(sstFiles);
}
protected abstract void init();
protected abstract ClosableIterator<T> getKeyIteratorForFile(String file) throws RocksDBException, IOException;
- @Override
- public boolean hasNext() {
+ private void initMinHeap(Collection<String> files) {
try {
- do {
- if (Objects.nonNull(currentFileIterator) && currentFileIterator.hasNext()) {
- return true;
+ int fileIndex = 0;
+ for (String file : files) {
+ ClosableIterator<T> iterator = getKeyIteratorForFile(file);
+ HeapEntryWithFileIdx<T> entry = new HeapEntryWithFileIdx<>(iterator, fileIndex++);
Review Comment:
```suggestion
for (String file : files) {
ClosableIterator<T> iterator = getKeyIteratorForFile(file);
HeapEntryWithFileIdx<T> entry = new HeapEntryWithFileIdx<>(iterator);
```
##########
hadoop-hdds/rocksdb-checkpoint-differ/src/main/java/org/apache/ozone/rocksdb/util/SstFileSetReader.java:
##########
@@ -233,71 +235,146 @@ public String next() {
}
}
- private abstract static class MultipleSstFileIterator<T> implements ClosableIterator<T> {
+ /**
+ * A wrapper class that holds an iterator and its current value for heap operations.
+ */
+ private static class HeapEntryWithFileIdx<T extends Comparable<T>>
+ implements Comparable<HeapEntryWithFileIdx<T>>, Closeable {
+ private final ClosableIterator<T> iterator;
+ private T currentKey;
+
+ HeapEntryWithFileIdx(ClosableIterator<T> iterator, int fileIndex) {
Review Comment:
```suggestion
HeapEntryWithFileIdx(ClosableIterator<T> iterator) {
```
##########
hadoop-hdds/rocksdb-checkpoint-differ/src/main/java/org/apache/ozone/rocksdb/util/SstFileSetReader.java:
##########
@@ -233,71 +235,146 @@ public String next() {
}
}
- private abstract static class MultipleSstFileIterator<T> implements ClosableIterator<T> {
+ /**
+ * A wrapper class that holds an iterator and its current value for heap operations.
+ */
+ private static class HeapEntryWithFileIdx<T extends Comparable<T>>
+ implements Comparable<HeapEntryWithFileIdx<T>>, Closeable {
+ private final ClosableIterator<T> iterator;
+ private T currentKey;
+
+ HeapEntryWithFileIdx(ClosableIterator<T> iterator, int fileIndex) {
+ this.iterator = iterator;
+ advance();
+ }
+
+ @Override
+ public void close() {
+ iterator.close();
+ }
+
+ boolean advance() {
+ if (iterator.hasNext()) {
+ currentKey = iterator.next();
+ return true;
+ } else {
+ currentKey = null;
+ return false;
+ }
+ }
+
+ T getCurrentKey() {
+ return currentKey;
+ }
+
+ @Override
+ public int compareTo(@Nonnull HeapEntryWithFileIdx<T> other) {
+ if (this.currentKey == null && other.currentKey == null) {
+ return 0;
+ }
+ if (this.currentKey == null) {
+ return 1;
+ }
+ if (other.currentKey == null) {
+ return -1;
+ }
+ return this.currentKey.compareTo(other.currentKey);
+ }
+
+ @Override
+ public boolean equals(Object obj) {
+ if (this == obj) {
+ return true;
+ }
+ if (obj == null || getClass() != obj.getClass()) {
+ return false;
+ }
- private final Iterator<String> fileNameIterator;
+ HeapEntryWithFileIdx<T> other = (HeapEntryWithFileIdx<T>) obj;
+ return compareTo(other) == 0;
+ }
- private String currentFile;
- private ClosableIterator<T> currentFileIterator;
+ @Override
+ public int hashCode() {
+ return Objects.hash(iterator, currentKey);
+ }
+ }
- private MultipleSstFileIterator(Collection<String> files) {
- this.fileNameIterator = files.iterator();
+ /**
+ * The MultipleSstFileIterator class is an abstract base for iterating over multiple SST files.
+ * It uses a PriorityQueue to merge keys from all files in sorted order.
+ * Each file's iterator is wrapped in a HeapEntryWithFileIdx object,
+ * which ensures stable ordering for identical keys by considering the file index.
+ * @param <T>
+ */
+ private abstract static class MultipleSstFileIterator<T extends Comparable<T>> implements ClosableIterator<T> {
+ private final PriorityQueue<HeapEntryWithFileIdx<T>> minHeap;
+
+ private MultipleSstFileIterator(Collection<String> sstFiles) {
+ this.minHeap = new PriorityQueue<>();
init();
+ initMinHeap(sstFiles);
}
protected abstract void init();
protected abstract ClosableIterator<T> getKeyIteratorForFile(String file) throws RocksDBException, IOException;
- @Override
- public boolean hasNext() {
+ private void initMinHeap(Collection<String> files) {
try {
- do {
- if (Objects.nonNull(currentFileIterator) && currentFileIterator.hasNext()) {
- return true;
+ int fileIndex = 0;
+ for (String file : files) {
+ ClosableIterator<T> iterator = getKeyIteratorForFile(file);
+ HeapEntryWithFileIdx<T> entry = new HeapEntryWithFileIdx<>(iterator, fileIndex++);
+
+ if (entry.getCurrentKey() != null) {
+ minHeap.offer(entry);
+ } else {
+ // No valid entries, close the iterator
+ entry.close();
}
- } while (moveToNextFile());
+ }
} catch (IOException | RocksDBException e) {
- // TODO: [Snapshot] This exception has to be handled by the caller.
- // We have to do better exception handling.
- throw new RuntimeException(e);
+ // Clean up any opened iterators
+ close();
+ throw new RuntimeException("Failed to initialize SST file iterators", e);
}
- return false;
}
@Override
- public T next() {
- if (hasNext()) {
- return currentFileIterator.next();
- }
- throw new NoSuchElementException("No more elements found.");
+ public boolean hasNext() {
+ return !minHeap.isEmpty();
}
@Override
- public void close() throws UncheckedIOException {
- try {
- closeCurrentFile();
- } catch (IOException e) {
- throw new UncheckedIOException(e);
+ public T next() {
+ if (!hasNext()) {
+ throw new NoSuchElementException("No more elements found.");
}
- }
- private boolean moveToNextFile() throws IOException, RocksDBException {
- if (fileNameIterator.hasNext()) {
- closeCurrentFile();
- currentFile = fileNameIterator.next();
- this.currentFileIterator = getKeyIteratorForFile(currentFile);
- return true;
+ assert minHeap.peek() != null;
+ // Get current key from heap
+ T currentKey = minHeap.peek().getCurrentKey();
Review Comment:
```suggestion
T currentKey = minHeap.peek();
```
--
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: issues-unsubscribe@ozone.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@ozone.apache.org
For additional commands, e-mail: issues-help@ozone.apache.org
Re: [PR] HDDS-13005. MinHeap based SstFileSetReader to return unique results in sorted order [ozone]
Posted by "smengcl (via GitHub)" <gi...@apache.org>.
smengcl merged PR #8912:
URL: https://github.com/apache/ozone/pull/8912
--
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: issues-unsubscribe@ozone.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@ozone.apache.org
For additional commands, e-mail: issues-help@ozone.apache.org
Re: [PR] HDDS-13005. MinHeap based SstFileSetReader to return unique results in sorted order [ozone]
Posted by "swamirishi (via GitHub)" <gi...@apache.org>.
swamirishi commented on code in PR #8912:
URL: https://github.com/apache/ozone/pull/8912#discussion_r2296270604
##########
hadoop-hdds/rocksdb-checkpoint-differ/src/main/java/org/apache/ozone/rocksdb/util/SstFileSetReader.java:
##########
@@ -233,70 +236,180 @@ public String next() {
}
}
- private abstract static class MultipleSstFileIterator<T> implements ClosableIterator<T> {
+ /**
+ * A wrapper class that holds an iterator and its current value for heap operations.
+ */
+ private static class HeapEntryWithFileIdx<T extends Comparable<T>>
+ implements Comparable<HeapEntryWithFileIdx<T>> {
+ private final ClosableIterator<T> iterator;
+ private T currentKey;
+ // To ensure stable ordering for identical keys
+ private final int fileIndex;
+
+ HeapEntryWithFileIdx(ClosableIterator<T> iterator, int fileIndex) {
+ this.iterator = iterator;
+ this.fileIndex = fileIndex;
+ advance();
+ }
+
+ void close() {
+ iterator.close();
+ }
+
+ boolean advance() {
+ if (iterator.hasNext()) {
+ currentKey = iterator.next();
+ return true;
+ } else {
+ currentKey = null;
+ return false;
+ }
+ }
+
+ T getCurrentKey() {
+ return currentKey;
+ }
+
+ @Override
+ public int compareTo(@Nonnull HeapEntryWithFileIdx<T> other) {
+ if (this.currentKey == null && other.currentKey == null) {
+ return 0;
+ }
+ if (this.currentKey == null) {
+ return 1;
+ }
+ if (other.currentKey == null) {
+ return -1;
+ }
+
+ int result = this.currentKey.compareTo(other.currentKey);
+ if (result == 0) {
+ // For identical keys, prefer the one from the file with the higher index
+ return Integer.compare(other.fileIndex, this.fileIndex);
+ }
+ return result;
+ }
+
+ @Override
+ public boolean equals(Object obj) {
+ if (this == obj) {
+ return true;
+ }
+ if (obj == null || getClass() != obj.getClass()) {
+ return false;
+ }
+
+ HeapEntryWithFileIdx<T> other = (HeapEntryWithFileIdx<T>) obj;
+
+ if (this.currentKey == null && other.currentKey == null) {
+ return this.fileIndex == other.fileIndex;
+ }
+ if (this.currentKey == null || other.currentKey == null) {
+ return false;
+ }
+
+ return this.currentKey.equals(other.currentKey) && this.fileIndex == other.fileIndex;
Review Comment:
```suggestion
return compareTo(other) == 0
```
##########
hadoop-hdds/rocksdb-checkpoint-differ/src/main/java/org/apache/ozone/rocksdb/util/SstFileSetReader.java:
##########
@@ -233,70 +236,180 @@ public String next() {
}
}
- private abstract static class MultipleSstFileIterator<T> implements ClosableIterator<T> {
+ /**
+ * A wrapper class that holds an iterator and its current value for heap operations.
+ */
+ private static class HeapEntryWithFileIdx<T extends Comparable<T>>
+ implements Comparable<HeapEntryWithFileIdx<T>> {
+ private final ClosableIterator<T> iterator;
+ private T currentKey;
+ // To ensure stable ordering for identical keys
+ private final int fileIndex;
+
+ HeapEntryWithFileIdx(ClosableIterator<T> iterator, int fileIndex) {
+ this.iterator = iterator;
+ this.fileIndex = fileIndex;
+ advance();
+ }
+
+ void close() {
+ iterator.close();
+ }
+
+ boolean advance() {
+ if (iterator.hasNext()) {
+ currentKey = iterator.next();
+ return true;
+ } else {
+ currentKey = null;
+ return false;
+ }
+ }
+
+ T getCurrentKey() {
+ return currentKey;
+ }
+
+ @Override
+ public int compareTo(@Nonnull HeapEntryWithFileIdx<T> other) {
+ if (this.currentKey == null && other.currentKey == null) {
+ return 0;
+ }
+ if (this.currentKey == null) {
+ return 1;
+ }
+ if (other.currentKey == null) {
+ return -1;
+ }
+
+ int result = this.currentKey.compareTo(other.currentKey);
+ if (result == 0) {
+ // For identical keys, prefer the one from the file with the higher index
+ return Integer.compare(other.fileIndex, this.fileIndex);
+ }
+ return result;
+ }
+
+ @Override
+ public boolean equals(Object obj) {
+ if (this == obj) {
+ return true;
+ }
+ if (obj == null || getClass() != obj.getClass()) {
+ return false;
+ }
+
+ HeapEntryWithFileIdx<T> other = (HeapEntryWithFileIdx<T>) obj;
+
+ if (this.currentKey == null && other.currentKey == null) {
+ return this.fileIndex == other.fileIndex;
+ }
+ if (this.currentKey == null || other.currentKey == null) {
+ return false;
+ }
+
+ return this.currentKey.equals(other.currentKey) && this.fileIndex == other.fileIndex;
+ }
- private final Iterator<String> fileNameIterator;
+ @Override
+ public int hashCode() {
+ return Objects.hash(iterator, currentKey, fileIndex);
+ }
+ }
- private String currentFile;
- private ClosableIterator<T> currentFileIterator;
+ private abstract static class MultipleSstFileIterator<T extends Comparable<T>> implements ClosableIterator<T> {
+ private final Collection<String> files;
+ private final PriorityQueue<HeapEntryWithFileIdx<T>> minHeap;
+ private final List<HeapEntryWithFileIdx<T>> allIterators;
+ private T lastReturnedValue;
private MultipleSstFileIterator(Collection<String> files) {
- this.fileNameIterator = files.iterator();
+ this.files = files;
+ this.minHeap = new PriorityQueue<>();
+ this.allIterators = new ArrayList<>();
+ this.lastReturnedValue = null;
init();
+ initMinHeap();
}
protected abstract void init();
protected abstract ClosableIterator<T> getKeyIteratorForFile(String file) throws RocksDBException, IOException;
- @Override
- public boolean hasNext() {
+ private void initMinHeap() {
try {
- do {
- if (Objects.nonNull(currentFileIterator) && currentFileIterator.hasNext()) {
- return true;
+ int fileIndex = 0;
+ for (String file : files) {
+ ClosableIterator<T> iterator = getKeyIteratorForFile(file);
+ HeapEntryWithFileIdx<T> entry = new HeapEntryWithFileIdx<>(iterator, fileIndex++);
+ allIterators.add(entry);
+
+ if (entry.getCurrentKey() != null) {
+ minHeap.offer(entry);
}
- } while (moveToNextFile());
+ }
} catch (IOException | RocksDBException e) {
- // TODO: [Snapshot] This exception has to be handled by the caller.
- // We have to do better exception handling.
- throw new RuntimeException(e);
+ // Clean up any opened iterators
+ close();
+ throw new RuntimeException("Failed to initialize SST file iterators", e);
}
- return false;
}
@Override
- public T next() {
- if (hasNext()) {
- return currentFileIterator.next();
+ public boolean hasNext() {
+ // Skip duplicates, keep advancing until we find a different key or run out of entries
+ while (!minHeap.isEmpty()) {
Review Comment:
hasNext() could just check minHeap.isEmpty() and next() function could remove all duplicate entries it would make sense. We wouldn't have to keep currentKey in memory then. We just have to pop out all entries which are equal to the key to be returned in the next() method.
--
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: issues-unsubscribe@ozone.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@ozone.apache.org
For additional commands, e-mail: issues-help@ozone.apache.org
Re: [PR] HDDS-13005. MinHeap based SstFileSetReader to return unique results in sorted order [ozone]
Posted by "smengcl (via GitHub)" <gi...@apache.org>.
smengcl commented on code in PR #8912:
URL: https://github.com/apache/ozone/pull/8912#discussion_r2666652611
##########
hadoop-hdds/rocksdb-checkpoint-differ/src/main/java/org/apache/ozone/rocksdb/util/SstFileSetReader.java:
##########
@@ -233,69 +236,136 @@ public String next() {
}
}
- private abstract static class MultipleSstFileIterator<T> implements ClosableIterator<T> {
+ /**
+ * A wrapper class that holds an iterator and its current value for heap operations.
+ */
+ private static class HeapEntry<T extends Comparable<T>>
+ implements Comparable<HeapEntry<T>>, Closeable {
+ private final ClosableIterator<T> iterator;
+ private T currentKey;
+
+ HeapEntry(ClosableIterator<T> iterator) {
+ this.iterator = iterator;
+ advance();
+ }
+
+ @Override
+ public void close() {
+ iterator.close();
+ }
- private final Iterator<String> fileNameIterator;
+ boolean advance() {
+ if (iterator.hasNext()) {
+ currentKey = iterator.next();
+ return true;
+ } else {
+ currentKey = null;
+ return false;
+ }
+ }
+
+ T getCurrentKey() {
+ return currentKey;
+ }
- private String currentFile;
- private ClosableIterator<T> currentFileIterator;
+ @Override
+ public int compareTo(@Nonnull HeapEntry<T> other) {
+ return Comparator.comparing(HeapEntry<T>::getCurrentKey).compare(this, other);
+ }
+
+ @Override
+ @SuppressWarnings("unchecked")
+ public boolean equals(Object obj) {
+ if (this == obj) {
+ return true;
+ }
+ if (obj == null || getClass() != obj.getClass()) {
+ return false;
+ }
+
+ HeapEntry<T> other = (HeapEntry<T>) obj;
+ return this.compareTo(other) == 0;
+ }
- private MultipleSstFileIterator(Collection<String> files) {
- this.fileNameIterator = files.iterator();
+ @Override
+ public int hashCode() {
+ return currentKey.hashCode();
+ }
+ }
+
+ /**
+ * The MultipleSstFileIterator class is an abstract base for iterating over multiple SST files.
+ * It uses a PriorityQueue to merge keys from all files in sorted order.
+ * Each file's iterator is wrapped in a HeapEntryWithFileIdx object,
+ * which ensures stable ordering for identical keys by considering the file index.
+ * @param <T>
+ */
+ private abstract static class MultipleSstFileIterator<T extends Comparable<T>> implements ClosableIterator<T> {
+ private final PriorityQueue<HeapEntry<T>> minHeap;
+
+ private MultipleSstFileIterator(Collection<String> sstFiles) {
+ this.minHeap = new PriorityQueue<>();
init();
+ initMinHeap(sstFiles);
}
protected abstract void init();
protected abstract ClosableIterator<T> getKeyIteratorForFile(String file) throws RocksDBException, IOException;
- @Override
- public boolean hasNext() {
+ private void initMinHeap(Collection<String> files) {
try {
- do {
- if (Objects.nonNull(currentFileIterator) && currentFileIterator.hasNext()) {
- return true;
+ for (String file : files) {
+ ClosableIterator<T> iterator = getKeyIteratorForFile(file);
+ HeapEntry<T> entry = new HeapEntry<>(iterator);
+
+ if (entry.getCurrentKey() != null) {
+ minHeap.offer(entry);
+ } else {
+ // No valid entries, close the iterator
+ entry.close();
}
- } while (moveToNextFile());
+ }
} catch (IOException | RocksDBException e) {
- // TODO: [Snapshot] This exception has to be handled by the caller.
- // We have to do better exception handling.
- throw new RuntimeException(e);
+ // Clean up any opened iterators
+ close();
+ throw new RuntimeException("Failed to initialize SST file iterators", e);
}
- return false;
}
@Override
- public T next() {
- if (hasNext()) {
- return currentFileIterator.next();
- }
- throw new NoSuchElementException("No more elements found.");
+ public boolean hasNext() {
+ return !minHeap.isEmpty();
}
@Override
- public void close() throws UncheckedIOException {
- try {
- closeCurrentFile();
- } catch (IOException e) {
- throw new UncheckedIOException(e);
+ public T next() {
+ if (!hasNext()) {
+ throw new NoSuchElementException("No more elements found.");
}
- }
- private boolean moveToNextFile() throws IOException, RocksDBException {
- if (fileNameIterator.hasNext()) {
- closeCurrentFile();
- currentFile = fileNameIterator.next();
- this.currentFileIterator = getKeyIteratorForFile(currentFile);
- return true;
+ assert minHeap.peek() != null;
+ // Get current key from heap
+ T currentKey = minHeap.peek().getCurrentKey();
+
+ // Advance all entries with the same key (from different files)
+ while (!minHeap.isEmpty() && Objects.equals(minHeap.peek().getCurrentKey(), currentKey)) {
Review Comment:
Thanks @swamirishi for fixing that in https://github.com/apache/ozone/pull/9576
--
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: issues-unsubscribe@ozone.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@ozone.apache.org
For additional commands, e-mail: issues-help@ozone.apache.org
Re: [PR] HDDS-13005. MinHeap based SstFileSetReader to return unique results in sorted order [ozone]
Posted by "Copilot (via GitHub)" <gi...@apache.org>.
Copilot commented on code in PR #8912:
URL: https://github.com/apache/ozone/pull/8912#discussion_r2325882538
##########
hadoop-hdds/rocksdb-checkpoint-differ/src/main/java/org/apache/ozone/rocksdb/util/SstFileSetReader.java:
##########
@@ -233,70 +236,149 @@ public String next() {
}
}
- private abstract static class MultipleSstFileIterator<T> implements ClosableIterator<T> {
+ /**
+ * A wrapper class that holds an iterator and its current value for heap operations.
+ */
+ private static class HeapEntryWithFileIdx<T extends Comparable<T>>
+ implements Comparable<HeapEntryWithFileIdx<T>> {
+ private final ClosableIterator<T> iterator;
+ private T currentKey;
+ // To ensure stable ordering for identical keys
+ private final int fileIndex;
+
+ HeapEntryWithFileIdx(ClosableIterator<T> iterator, int fileIndex) {
+ this.iterator = iterator;
+ this.fileIndex = fileIndex;
+ advance();
+ }
+
+ void close() {
+ iterator.close();
+ }
+
+ boolean advance() {
+ if (iterator.hasNext()) {
+ currentKey = iterator.next();
+ return true;
+ } else {
+ currentKey = null;
+ return false;
+ }
+ }
+
+ T getCurrentKey() {
+ return currentKey;
+ }
+
+ @Override
+ public int compareTo(@Nonnull HeapEntryWithFileIdx<T> other) {
+ if (this.currentKey == null && other.currentKey == null) {
+ return 0;
+ }
+ if (this.currentKey == null) {
+ return 1;
+ }
+ if (other.currentKey == null) {
+ return -1;
+ }
+
+ int result = this.currentKey.compareTo(other.currentKey);
+ if (result == 0) {
+ // For identical keys, prefer the one from the file with the higher index
+ return Integer.compare(other.fileIndex, this.fileIndex);
+ }
+ return result;
+ }
+
+ @Override
+ public boolean equals(Object obj) {
+ if (this == obj) {
+ return true;
+ }
+ if (obj == null || getClass() != obj.getClass()) {
+ return false;
+ }
+
+ HeapEntryWithFileIdx<T> other = (HeapEntryWithFileIdx<T>) obj;
Review Comment:
The cast is unsafe and could throw ClassCastException. Consider using instanceof check before casting or implementing type-safe equals method.
##########
hadoop-hdds/rocksdb-checkpoint-differ/src/main/java/org/apache/ozone/rocksdb/util/SstFileSetReader.java:
##########
@@ -233,70 +236,149 @@ public String next() {
}
}
- private abstract static class MultipleSstFileIterator<T> implements ClosableIterator<T> {
+ /**
+ * A wrapper class that holds an iterator and its current value for heap operations.
+ */
+ private static class HeapEntryWithFileIdx<T extends Comparable<T>>
+ implements Comparable<HeapEntryWithFileIdx<T>> {
+ private final ClosableIterator<T> iterator;
+ private T currentKey;
+ // To ensure stable ordering for identical keys
+ private final int fileIndex;
+
+ HeapEntryWithFileIdx(ClosableIterator<T> iterator, int fileIndex) {
+ this.iterator = iterator;
+ this.fileIndex = fileIndex;
+ advance();
+ }
+
+ void close() {
+ iterator.close();
+ }
+
+ boolean advance() {
+ if (iterator.hasNext()) {
+ currentKey = iterator.next();
+ return true;
+ } else {
+ currentKey = null;
+ return false;
+ }
+ }
+
+ T getCurrentKey() {
+ return currentKey;
+ }
+
+ @Override
+ public int compareTo(@Nonnull HeapEntryWithFileIdx<T> other) {
+ if (this.currentKey == null && other.currentKey == null) {
+ return 0;
+ }
+ if (this.currentKey == null) {
+ return 1;
+ }
+ if (other.currentKey == null) {
+ return -1;
+ }
+
+ int result = this.currentKey.compareTo(other.currentKey);
+ if (result == 0) {
+ // For identical keys, prefer the one from the file with the higher index
+ return Integer.compare(other.fileIndex, this.fileIndex);
+ }
+ return result;
+ }
+
+ @Override
+ public boolean equals(Object obj) {
+ if (this == obj) {
+ return true;
+ }
+ if (obj == null || getClass() != obj.getClass()) {
+ return false;
+ }
+
+ HeapEntryWithFileIdx<T> other = (HeapEntryWithFileIdx<T>) obj;
+ return compareTo(other) == 0;
+ }
Review Comment:
The equals implementation violates the equals-hashCode contract. Two objects that are equal according to compareTo(other) == 0 may have different hash codes because hashCode includes the iterator object reference, but equals only compares the comparable result.
--
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: issues-unsubscribe@ozone.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@ozone.apache.org
For additional commands, e-mail: issues-help@ozone.apache.org
Re: [PR] HDDS-13005. MinHeap based SstFileSetReader to return unique results in sorted order [ozone]
Posted by "smengcl (via GitHub)" <gi...@apache.org>.
smengcl commented on code in PR #8912:
URL: https://github.com/apache/ozone/pull/8912#discussion_r2331514624
##########
hadoop-hdds/rocksdb-checkpoint-differ/src/main/java/org/apache/ozone/rocksdb/util/SstFileSetReader.java:
##########
@@ -233,70 +236,149 @@ public String next() {
}
}
- private abstract static class MultipleSstFileIterator<T> implements ClosableIterator<T> {
+ /**
+ * A wrapper class that holds an iterator and its current value for heap operations.
+ */
+ private static class HeapEntryWithFileIdx<T extends Comparable<T>>
+ implements Comparable<HeapEntryWithFileIdx<T>> {
+ private final ClosableIterator<T> iterator;
+ private T currentKey;
+ // To ensure stable ordering for identical keys
+ private final int fileIndex;
+
+ HeapEntryWithFileIdx(ClosableIterator<T> iterator, int fileIndex) {
+ this.iterator = iterator;
+ this.fileIndex = fileIndex;
+ advance();
+ }
+
+ void close() {
+ iterator.close();
+ }
+
+ boolean advance() {
+ if (iterator.hasNext()) {
+ currentKey = iterator.next();
+ return true;
+ } else {
+ currentKey = null;
+ return false;
+ }
+ }
+
+ T getCurrentKey() {
+ return currentKey;
+ }
+
+ @Override
+ public int compareTo(@Nonnull HeapEntryWithFileIdx<T> other) {
+ if (this.currentKey == null && other.currentKey == null) {
+ return 0;
+ }
+ if (this.currentKey == null) {
+ return 1;
+ }
+ if (other.currentKey == null) {
+ return -1;
+ }
+
+ int result = this.currentKey.compareTo(other.currentKey);
+ if (result == 0) {
+ // For identical keys, prefer the one from the file with the higher index
+ return Integer.compare(other.fileIndex, this.fileIndex);
+ }
+ return result;
+ }
+
+ @Override
+ public boolean equals(Object obj) {
+ if (this == obj) {
+ return true;
+ }
+ if (obj == null || getClass() != obj.getClass()) {
+ return false;
+ }
+
+ HeapEntryWithFileIdx<T> other = (HeapEntryWithFileIdx<T>) obj;
+ return compareTo(other) == 0;
+ }
- private final Iterator<String> fileNameIterator;
+ @Override
+ public int hashCode() {
+ return Objects.hash(iterator, currentKey, fileIndex);
+ }
+ }
- private String currentFile;
- private ClosableIterator<T> currentFileIterator;
+ private abstract static class MultipleSstFileIterator<T extends Comparable<T>> implements ClosableIterator<T> {
+ private final Collection<String> files;
+ private final PriorityQueue<HeapEntryWithFileIdx<T>> minHeap;
+ private final List<HeapEntryWithFileIdx<T>> allIterators;
private MultipleSstFileIterator(Collection<String> files) {
- this.fileNameIterator = files.iterator();
+ this.files = files;
+ this.minHeap = new PriorityQueue<>();
+ this.allIterators = new ArrayList<>();
init();
+ initMinHeap();
}
protected abstract void init();
protected abstract ClosableIterator<T> getKeyIteratorForFile(String file) throws RocksDBException, IOException;
- @Override
- public boolean hasNext() {
+ private void initMinHeap() {
try {
- do {
- if (Objects.nonNull(currentFileIterator) && currentFileIterator.hasNext()) {
- return true;
+ int fileIndex = 0;
+ for (String file : files) {
+ ClosableIterator<T> iterator = getKeyIteratorForFile(file);
+ HeapEntryWithFileIdx<T> entry = new HeapEntryWithFileIdx<>(iterator, fileIndex++);
+ allIterators.add(entry);
+
+ if (entry.getCurrentKey() != null) {
+ minHeap.offer(entry);
}
Review Comment:
Good catch
--
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: issues-unsubscribe@ozone.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@ozone.apache.org
For additional commands, e-mail: issues-help@ozone.apache.org
Re: [PR] HDDS-13005. MinHeap based SstFileSetReader to return unique results in sorted order [ozone]
Posted by "swamirishi (via GitHub)" <gi...@apache.org>.
swamirishi commented on PR #8912:
URL: https://github.com/apache/ozone/pull/8912#issuecomment-3188885962
@SaketaChalamchala Can you please review this PR as well?
--
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: issues-unsubscribe@ozone.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@ozone.apache.org
For additional commands, e-mail: issues-help@ozone.apache.org
Re: [PR] HDDS-13005. MinHeap based SstFileSetReader to return unique results in sorted order [ozone]
Posted by "Copilot (via GitHub)" <gi...@apache.org>.
Copilot commented on code in PR #8912:
URL: https://github.com/apache/ozone/pull/8912#discussion_r2346029798
##########
hadoop-hdds/rocksdb-checkpoint-differ/src/main/java/org/apache/ozone/rocksdb/util/SstFileSetReader.java:
##########
@@ -233,69 +235,145 @@ public String next() {
}
}
- private abstract static class MultipleSstFileIterator<T> implements ClosableIterator<T> {
+ /**
+ * A wrapper class that holds an iterator and its current value for heap operations.
+ */
+ private static class HeapEntry<T extends Comparable<T>>
+ implements Comparable<HeapEntry<T>>, Closeable {
+ private final ClosableIterator<T> iterator;
+ private T currentKey;
+
+ HeapEntry(ClosableIterator<T> iterator) {
+ this.iterator = iterator;
+ advance();
+ }
+
+ @Override
+ public void close() {
+ iterator.close();
+ }
+
+ boolean advance() {
+ if (iterator.hasNext()) {
+ currentKey = iterator.next();
+ return true;
+ } else {
+ currentKey = null;
+ return false;
+ }
+ }
+
+ T getCurrentKey() {
+ return currentKey;
+ }
+
+ @Override
+ public int compareTo(@Nonnull HeapEntry<T> other) {
+ if (this.currentKey == null && other.currentKey == null) {
+ return 0;
+ }
+ if (this.currentKey == null) {
+ return 1;
+ }
+ if (other.currentKey == null) {
+ return -1;
+ }
+ return this.currentKey.compareTo(other.currentKey);
+ }
+
+ @Override
+ public boolean equals(Object obj) {
+ if (this == obj) {
+ return true;
+ }
+ if (obj == null || getClass() != obj.getClass()) {
+ return false;
+ }
- private final Iterator<String> fileNameIterator;
+ HeapEntry<T> other = (HeapEntry<T>) obj;
+ return compareTo(other) == 0;
+ }
- private String currentFile;
- private ClosableIterator<T> currentFileIterator;
+ @Override
+ public int hashCode() {
+ return Objects.hash(iterator, currentKey);
+ }
+ }
- private MultipleSstFileIterator(Collection<String> files) {
- this.fileNameIterator = files.iterator();
+ /**
+ * The MultipleSstFileIterator class is an abstract base for iterating over multiple SST files.
+ * It uses a PriorityQueue to merge keys from all files in sorted order.
+ * Each file's iterator is wrapped in a HeapEntryWithFileIdx object,
+ * which ensures stable ordering for identical keys by considering the file index.
+ * @param <T>
+ */
+ private abstract static class MultipleSstFileIterator<T extends Comparable<T>> implements ClosableIterator<T> {
+ private final PriorityQueue<HeapEntry<T>> minHeap;
+
+ private MultipleSstFileIterator(Collection<String> sstFiles) {
+ this.minHeap = new PriorityQueue<>();
init();
+ initMinHeap(sstFiles);
}
protected abstract void init();
protected abstract ClosableIterator<T> getKeyIteratorForFile(String file) throws RocksDBException, IOException;
- @Override
- public boolean hasNext() {
+ private void initMinHeap(Collection<String> files) {
try {
- do {
- if (Objects.nonNull(currentFileIterator) && currentFileIterator.hasNext()) {
- return true;
+ for (String file : files) {
+ ClosableIterator<T> iterator = getKeyIteratorForFile(file);
+ HeapEntry<T> entry = new HeapEntry<>(iterator);
+
+ if (entry.getCurrentKey() != null) {
+ minHeap.offer(entry);
+ } else {
+ // No valid entries, close the iterator
+ entry.close();
}
- } while (moveToNextFile());
+ }
} catch (IOException | RocksDBException e) {
- // TODO: [Snapshot] This exception has to be handled by the caller.
- // We have to do better exception handling.
- throw new RuntimeException(e);
+ // Clean up any opened iterators
+ close();
+ throw new RuntimeException("Failed to initialize SST file iterators", e);
}
- return false;
}
@Override
- public T next() {
- if (hasNext()) {
- return currentFileIterator.next();
- }
- throw new NoSuchElementException("No more elements found.");
+ public boolean hasNext() {
+ return !minHeap.isEmpty();
}
@Override
- public void close() throws UncheckedIOException {
- try {
- closeCurrentFile();
- } catch (IOException e) {
- throw new UncheckedIOException(e);
+ public T next() {
+ if (!hasNext()) {
+ throw new NoSuchElementException("No more elements found.");
}
- }
- private boolean moveToNextFile() throws IOException, RocksDBException {
- if (fileNameIterator.hasNext()) {
- closeCurrentFile();
- currentFile = fileNameIterator.next();
- this.currentFileIterator = getKeyIteratorForFile(currentFile);
- return true;
+ assert minHeap.peek() != null;
+ // Get current key from heap
+ HeapEntry<T> currentKey = minHeap.peek();
+
+ // Advance all entries with the same key (from different files)
+ // and keep the one with the highest file index
+ while (!minHeap.isEmpty() && Objects.equals(minHeap.peek(), currentKey)) {
Review Comment:
The comment mentions keeping 'the one with the highest file index' but the code doesn't implement this logic. The current implementation advances all entries with the same key but doesn't actually select based on file index. There's no mechanism to track or compare file indices in the HeapEntry class.
##########
hadoop-hdds/rocksdb-checkpoint-differ/src/main/java/org/apache/ozone/rocksdb/util/SstFileSetReader.java:
##########
@@ -233,69 +235,145 @@ public String next() {
}
}
- private abstract static class MultipleSstFileIterator<T> implements ClosableIterator<T> {
+ /**
+ * A wrapper class that holds an iterator and its current value for heap operations.
+ */
+ private static class HeapEntry<T extends Comparable<T>>
+ implements Comparable<HeapEntry<T>>, Closeable {
+ private final ClosableIterator<T> iterator;
+ private T currentKey;
+
+ HeapEntry(ClosableIterator<T> iterator) {
+ this.iterator = iterator;
+ advance();
+ }
+
+ @Override
+ public void close() {
+ iterator.close();
+ }
+
+ boolean advance() {
+ if (iterator.hasNext()) {
+ currentKey = iterator.next();
+ return true;
+ } else {
+ currentKey = null;
+ return false;
+ }
+ }
+
+ T getCurrentKey() {
+ return currentKey;
+ }
+
+ @Override
+ public int compareTo(@Nonnull HeapEntry<T> other) {
+ if (this.currentKey == null && other.currentKey == null) {
+ return 0;
+ }
+ if (this.currentKey == null) {
+ return 1;
+ }
+ if (other.currentKey == null) {
+ return -1;
+ }
+ return this.currentKey.compareTo(other.currentKey);
+ }
+
+ @Override
+ public boolean equals(Object obj) {
+ if (this == obj) {
+ return true;
+ }
+ if (obj == null || getClass() != obj.getClass()) {
+ return false;
+ }
- private final Iterator<String> fileNameIterator;
+ HeapEntry<T> other = (HeapEntry<T>) obj;
Review Comment:
Unchecked cast should be avoided. Add a type check before casting or use @SuppressWarnings(\"unchecked\") if the cast is guaranteed to be safe.
##########
hadoop-hdds/rocksdb-checkpoint-differ/src/test/java/org/apache/ozone/rocksdb/util/TestSstFileSetReader.java:
##########
@@ -87,14 +98,32 @@ private String createRandomSSTFile(TreeMap<String, Integer> keys)
return file.getAbsolutePath();
}
+ /**
+ * Helper method to create a map of keys with values alternating between 0 and 1.
+ * Keys with even indices get value 0 (will be treated as deletions/tombstones),
+ * keys with odd indices get value 1 (will be treated as regular entries).
+ *
+ * @param startRange Starting range for key generation (inclusive)
+ * @param endRange Ending range for key generation (exclusive)
+ * @return Map of keys with alternating 0/1 values
+ */
private Map<String, Integer> createKeys(int startRange, int endRange) {
return IntStream.range(startRange, endRange).boxed()
.collect(Collectors.toMap(i -> KEY_PREFIX + i,
i -> i % 2));
}
- private Pair<SortedMap<String, Integer>, List<String>> createDummyData(
- int numberOfFiles) throws RocksDBException, IOException {
+ /**
+ * Helper method to create dummy test data consisting of multiple SST files.
+ * Keys are distributed across files in round-robin fashion, ensuring each file
+ * contains a subset of the total key space for testing overlapping scenarios.
+ *
+ * @param numberOfFiles Number of SST files to create
+ * @return Pair containing the complete sorted key map and list of SST file paths
+ * @throws RocksDBException if there's an error during SST file creation
+ * @throws IOException if there's an I/O error during file operations
Review Comment:
The method signature was changed to remove IOException from the throws clause, but the JavaDoc still mentions it can throw IOException. The JavaDoc should be updated to reflect the actual exceptions thrown.
```suggestion
```
--
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: issues-unsubscribe@ozone.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@ozone.apache.org
For additional commands, e-mail: issues-help@ozone.apache.org
Re: [PR] HDDS-13005. MinHeap based SstFileSetReader to return unique results in sorted order [ozone]
Posted by "swamirishi (via GitHub)" <gi...@apache.org>.
swamirishi commented on code in PR #8912:
URL: https://github.com/apache/ozone/pull/8912#discussion_r2336518393
##########
hadoop-hdds/rocksdb-checkpoint-differ/src/main/java/org/apache/ozone/rocksdb/util/SstFileSetReader.java:
##########
@@ -233,70 +236,149 @@ public String next() {
}
}
- private abstract static class MultipleSstFileIterator<T> implements ClosableIterator<T> {
+ /**
+ * A wrapper class that holds an iterator and its current value for heap operations.
+ */
+ private static class HeapEntryWithFileIdx<T extends Comparable<T>>
+ implements Comparable<HeapEntryWithFileIdx<T>> {
+ private final ClosableIterator<T> iterator;
+ private T currentKey;
+ // To ensure stable ordering for identical keys
+ private final int fileIndex;
Review Comment:
It doesn't really matter. We are going to return current entry and it would be the same. We need not compare the fileIndex altogether.
--
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: issues-unsubscribe@ozone.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@ozone.apache.org
For additional commands, e-mail: issues-help@ozone.apache.org
Re: [PR] HDDS-13005. MinHeap based SstFileSetReader to return unique results in sorted order [ozone]
Posted by "swamirishi (via GitHub)" <gi...@apache.org>.
swamirishi commented on code in PR #8912:
URL: https://github.com/apache/ozone/pull/8912#discussion_r2656467892
##########
hadoop-hdds/rocksdb-checkpoint-differ/src/main/java/org/apache/ozone/rocksdb/util/SstFileSetReader.java:
##########
@@ -233,69 +236,136 @@ public String next() {
}
}
- private abstract static class MultipleSstFileIterator<T> implements ClosableIterator<T> {
+ /**
+ * A wrapper class that holds an iterator and its current value for heap operations.
+ */
+ private static class HeapEntry<T extends Comparable<T>>
+ implements Comparable<HeapEntry<T>>, Closeable {
+ private final ClosableIterator<T> iterator;
+ private T currentKey;
+
+ HeapEntry(ClosableIterator<T> iterator) {
+ this.iterator = iterator;
+ advance();
+ }
+
+ @Override
+ public void close() {
+ iterator.close();
+ }
- private final Iterator<String> fileNameIterator;
+ boolean advance() {
+ if (iterator.hasNext()) {
+ currentKey = iterator.next();
+ return true;
+ } else {
+ currentKey = null;
+ return false;
+ }
+ }
+
+ T getCurrentKey() {
+ return currentKey;
+ }
- private String currentFile;
- private ClosableIterator<T> currentFileIterator;
+ @Override
+ public int compareTo(@Nonnull HeapEntry<T> other) {
+ return Comparator.comparing(HeapEntry<T>::getCurrentKey).compare(this, other);
+ }
+
+ @Override
+ @SuppressWarnings("unchecked")
+ public boolean equals(Object obj) {
+ if (this == obj) {
+ return true;
+ }
+ if (obj == null || getClass() != obj.getClass()) {
+ return false;
+ }
+
+ HeapEntry<T> other = (HeapEntry<T>) obj;
+ return this.compareTo(other) == 0;
+ }
- private MultipleSstFileIterator(Collection<String> files) {
- this.fileNameIterator = files.iterator();
+ @Override
+ public int hashCode() {
+ return currentKey.hashCode();
+ }
+ }
+
+ /**
+ * The MultipleSstFileIterator class is an abstract base for iterating over multiple SST files.
+ * It uses a PriorityQueue to merge keys from all files in sorted order.
+ * Each file's iterator is wrapped in a HeapEntryWithFileIdx object,
+ * which ensures stable ordering for identical keys by considering the file index.
+ * @param <T>
+ */
+ private abstract static class MultipleSstFileIterator<T extends Comparable<T>> implements ClosableIterator<T> {
+ private final PriorityQueue<HeapEntry<T>> minHeap;
+
+ private MultipleSstFileIterator(Collection<String> sstFiles) {
+ this.minHeap = new PriorityQueue<>();
init();
+ initMinHeap(sstFiles);
}
protected abstract void init();
protected abstract ClosableIterator<T> getKeyIteratorForFile(String file) throws RocksDBException, IOException;
- @Override
- public boolean hasNext() {
+ private void initMinHeap(Collection<String> files) {
try {
- do {
- if (Objects.nonNull(currentFileIterator) && currentFileIterator.hasNext()) {
- return true;
+ for (String file : files) {
+ ClosableIterator<T> iterator = getKeyIteratorForFile(file);
+ HeapEntry<T> entry = new HeapEntry<>(iterator);
+
+ if (entry.getCurrentKey() != null) {
+ minHeap.offer(entry);
+ } else {
+ // No valid entries, close the iterator
+ entry.close();
}
- } while (moveToNextFile());
+ }
} catch (IOException | RocksDBException e) {
- // TODO: [Snapshot] This exception has to be handled by the caller.
- // We have to do better exception handling.
- throw new RuntimeException(e);
+ // Clean up any opened iterators
+ close();
+ throw new RuntimeException("Failed to initialize SST file iterators", e);
}
- return false;
}
@Override
- public T next() {
- if (hasNext()) {
- return currentFileIterator.next();
- }
- throw new NoSuchElementException("No more elements found.");
+ public boolean hasNext() {
+ return !minHeap.isEmpty();
}
@Override
- public void close() throws UncheckedIOException {
- try {
- closeCurrentFile();
- } catch (IOException e) {
- throw new UncheckedIOException(e);
+ public T next() {
+ if (!hasNext()) {
+ throw new NoSuchElementException("No more elements found.");
}
- }
- private boolean moveToNextFile() throws IOException, RocksDBException {
- if (fileNameIterator.hasNext()) {
- closeCurrentFile();
- currentFile = fileNameIterator.next();
- this.currentFileIterator = getKeyIteratorForFile(currentFile);
- return true;
+ assert minHeap.peek() != null;
+ // Get current key from heap
+ T currentKey = minHeap.peek().getCurrentKey();
+
+ // Advance all entries with the same key (from different files)
+ while (!minHeap.isEmpty() && Objects.equals(minHeap.peek().getCurrentKey(), currentKey)) {
Review Comment:
@smengcl The key comparator should have been used here
--
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: issues-unsubscribe@ozone.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@ozone.apache.org
For additional commands, e-mail: issues-help@ozone.apache.org
Re: [PR] HDDS-13005. MinHeap based SstFileSetReader to return unique results in sorted order [ozone]
Posted by "smengcl (via GitHub)" <gi...@apache.org>.
smengcl commented on code in PR #8912:
URL: https://github.com/apache/ozone/pull/8912#discussion_r2331508488
##########
hadoop-hdds/rocksdb-checkpoint-differ/src/main/java/org/apache/ozone/rocksdb/util/SstFileSetReader.java:
##########
@@ -233,70 +236,149 @@ public String next() {
}
}
- private abstract static class MultipleSstFileIterator<T> implements ClosableIterator<T> {
+ /**
+ * A wrapper class that holds an iterator and its current value for heap operations.
+ */
+ private static class HeapEntryWithFileIdx<T extends Comparable<T>>
+ implements Comparable<HeapEntryWithFileIdx<T>> {
+ private final ClosableIterator<T> iterator;
+ private T currentKey;
+ // To ensure stable ordering for identical keys
+ private final int fileIndex;
Review Comment:
Purpose of fileIndex is to keep track of which sst file has higher index when multiple entries exist. When there are duplicates, values in SST files with higher index should be in the result.
--
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: issues-unsubscribe@ozone.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@ozone.apache.org
For additional commands, e-mail: issues-help@ozone.apache.org
Re: [PR] HDDS-13005. MinHeap based SstFileSetReader to return unique results in sorted order [ozone]
Posted by "swamirishi (via GitHub)" <gi...@apache.org>.
swamirishi commented on code in PR #8912:
URL: https://github.com/apache/ozone/pull/8912#discussion_r2656467892
##########
hadoop-hdds/rocksdb-checkpoint-differ/src/main/java/org/apache/ozone/rocksdb/util/SstFileSetReader.java:
##########
@@ -233,69 +236,136 @@ public String next() {
}
}
- private abstract static class MultipleSstFileIterator<T> implements ClosableIterator<T> {
+ /**
+ * A wrapper class that holds an iterator and its current value for heap operations.
+ */
+ private static class HeapEntry<T extends Comparable<T>>
+ implements Comparable<HeapEntry<T>>, Closeable {
+ private final ClosableIterator<T> iterator;
+ private T currentKey;
+
+ HeapEntry(ClosableIterator<T> iterator) {
+ this.iterator = iterator;
+ advance();
+ }
+
+ @Override
+ public void close() {
+ iterator.close();
+ }
- private final Iterator<String> fileNameIterator;
+ boolean advance() {
+ if (iterator.hasNext()) {
+ currentKey = iterator.next();
+ return true;
+ } else {
+ currentKey = null;
+ return false;
+ }
+ }
+
+ T getCurrentKey() {
+ return currentKey;
+ }
- private String currentFile;
- private ClosableIterator<T> currentFileIterator;
+ @Override
+ public int compareTo(@Nonnull HeapEntry<T> other) {
+ return Comparator.comparing(HeapEntry<T>::getCurrentKey).compare(this, other);
+ }
+
+ @Override
+ @SuppressWarnings("unchecked")
+ public boolean equals(Object obj) {
+ if (this == obj) {
+ return true;
+ }
+ if (obj == null || getClass() != obj.getClass()) {
+ return false;
+ }
+
+ HeapEntry<T> other = (HeapEntry<T>) obj;
+ return this.compareTo(other) == 0;
+ }
- private MultipleSstFileIterator(Collection<String> files) {
- this.fileNameIterator = files.iterator();
+ @Override
+ public int hashCode() {
+ return currentKey.hashCode();
+ }
+ }
+
+ /**
+ * The MultipleSstFileIterator class is an abstract base for iterating over multiple SST files.
+ * It uses a PriorityQueue to merge keys from all files in sorted order.
+ * Each file's iterator is wrapped in a HeapEntryWithFileIdx object,
+ * which ensures stable ordering for identical keys by considering the file index.
+ * @param <T>
+ */
+ private abstract static class MultipleSstFileIterator<T extends Comparable<T>> implements ClosableIterator<T> {
+ private final PriorityQueue<HeapEntry<T>> minHeap;
+
+ private MultipleSstFileIterator(Collection<String> sstFiles) {
+ this.minHeap = new PriorityQueue<>();
init();
+ initMinHeap(sstFiles);
}
protected abstract void init();
protected abstract ClosableIterator<T> getKeyIteratorForFile(String file) throws RocksDBException, IOException;
- @Override
- public boolean hasNext() {
+ private void initMinHeap(Collection<String> files) {
try {
- do {
- if (Objects.nonNull(currentFileIterator) && currentFileIterator.hasNext()) {
- return true;
+ for (String file : files) {
+ ClosableIterator<T> iterator = getKeyIteratorForFile(file);
+ HeapEntry<T> entry = new HeapEntry<>(iterator);
+
+ if (entry.getCurrentKey() != null) {
+ minHeap.offer(entry);
+ } else {
+ // No valid entries, close the iterator
+ entry.close();
}
- } while (moveToNextFile());
+ }
} catch (IOException | RocksDBException e) {
- // TODO: [Snapshot] This exception has to be handled by the caller.
- // We have to do better exception handling.
- throw new RuntimeException(e);
+ // Clean up any opened iterators
+ close();
+ throw new RuntimeException("Failed to initialize SST file iterators", e);
}
- return false;
}
@Override
- public T next() {
- if (hasNext()) {
- return currentFileIterator.next();
- }
- throw new NoSuchElementException("No more elements found.");
+ public boolean hasNext() {
+ return !minHeap.isEmpty();
}
@Override
- public void close() throws UncheckedIOException {
- try {
- closeCurrentFile();
- } catch (IOException e) {
- throw new UncheckedIOException(e);
+ public T next() {
+ if (!hasNext()) {
+ throw new NoSuchElementException("No more elements found.");
}
- }
- private boolean moveToNextFile() throws IOException, RocksDBException {
- if (fileNameIterator.hasNext()) {
- closeCurrentFile();
- currentFile = fileNameIterator.next();
- this.currentFileIterator = getKeyIteratorForFile(currentFile);
- return true;
+ assert minHeap.peek() != null;
+ // Get current key from heap
+ T currentKey = minHeap.peek().getCurrentKey();
+
+ // Advance all entries with the same key (from different files)
+ while (!minHeap.isEmpty() && Objects.equals(minHeap.peek().getCurrentKey(), currentKey)) {
Review Comment:
@smengcl @jojochuang The key comparator should have been used here instead of Object.equals()
--
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: issues-unsubscribe@ozone.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@ozone.apache.org
For additional commands, e-mail: issues-help@ozone.apache.org
Re: [PR] HDDS-13005. MinHeap based SstFileSetReader to return unique results in sorted order [ozone]
Posted by "smengcl (via GitHub)" <gi...@apache.org>.
smengcl commented on code in PR #8912:
URL: https://github.com/apache/ozone/pull/8912#discussion_r2337411601
##########
hadoop-hdds/rocksdb-checkpoint-differ/src/main/java/org/apache/ozone/rocksdb/util/SstFileSetReader.java:
##########
@@ -233,70 +236,149 @@ public String next() {
}
}
- private abstract static class MultipleSstFileIterator<T> implements ClosableIterator<T> {
+ /**
+ * A wrapper class that holds an iterator and its current value for heap operations.
+ */
+ private static class HeapEntryWithFileIdx<T extends Comparable<T>>
+ implements Comparable<HeapEntryWithFileIdx<T>> {
+ private final ClosableIterator<T> iterator;
+ private T currentKey;
+ // To ensure stable ordering for identical keys
+ private final int fileIndex;
Review Comment:
@swamirishi i originally was thinking this minheap reader could be used for iterating original DB ssts as well thus the fileIndex. Ok, if we are not going to use it for that we can safely remove this.
--
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: issues-unsubscribe@ozone.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@ozone.apache.org
For additional commands, e-mail: issues-help@ozone.apache.org
Re: [PR] HDDS-13005. MinHeap based SstFileSetReader to return unique results in sorted order [ozone]
Posted by "smengcl (via GitHub)" <gi...@apache.org>.
smengcl commented on code in PR #8912:
URL: https://github.com/apache/ozone/pull/8912#discussion_r2345693894
##########
hadoop-hdds/rocksdb-checkpoint-differ/src/main/java/org/apache/ozone/rocksdb/util/SstFileSetReader.java:
##########
@@ -233,69 +235,145 @@ public String next() {
}
}
- private abstract static class MultipleSstFileIterator<T> implements ClosableIterator<T> {
+ /**
+ * A wrapper class that holds an iterator and its current value for heap operations.
+ */
+ private static class HeapEntryWithFileIdx<T extends Comparable<T>>
+ implements Comparable<HeapEntryWithFileIdx<T>>, Closeable {
+ private final ClosableIterator<T> iterator;
+ private T currentKey;
+
+ HeapEntryWithFileIdx(ClosableIterator<T> iterator) {
+ this.iterator = iterator;
+ advance();
+ }
+
+ @Override
+ public void close() {
+ iterator.close();
+ }
+
+ boolean advance() {
+ if (iterator.hasNext()) {
+ currentKey = iterator.next();
+ return true;
+ } else {
+ currentKey = null;
+ return false;
+ }
+ }
+
+ T getCurrentKey() {
+ return currentKey;
+ }
+
+ @Override
+ public int compareTo(@Nonnull HeapEntryWithFileIdx<T> other) {
+ if (this.currentKey == null && other.currentKey == null) {
+ return 0;
+ }
+ if (this.currentKey == null) {
+ return 1;
+ }
+ if (other.currentKey == null) {
+ return -1;
+ }
+ return this.currentKey.compareTo(other.currentKey);
+ }
+
+ @Override
+ public boolean equals(Object obj) {
+ if (this == obj) {
+ return true;
+ }
+ if (obj == null || getClass() != obj.getClass()) {
+ return false;
+ }
- private final Iterator<String> fileNameIterator;
+ HeapEntryWithFileIdx<T> other = (HeapEntryWithFileIdx<T>) obj;
+ return compareTo(other) == 0;
+ }
- private String currentFile;
- private ClosableIterator<T> currentFileIterator;
+ @Override
+ public int hashCode() {
+ return Objects.hash(iterator, currentKey);
+ }
+ }
- private MultipleSstFileIterator(Collection<String> files) {
- this.fileNameIterator = files.iterator();
+ /**
+ * The MultipleSstFileIterator class is an abstract base for iterating over multiple SST files.
+ * It uses a PriorityQueue to merge keys from all files in sorted order.
+ * Each file's iterator is wrapped in a HeapEntryWithFileIdx object,
+ * which ensures stable ordering for identical keys by considering the file index.
+ * @param <T>
+ */
+ private abstract static class MultipleSstFileIterator<T extends Comparable<T>> implements ClosableIterator<T> {
+ private final PriorityQueue<HeapEntryWithFileIdx<T>> minHeap;
+
+ private MultipleSstFileIterator(Collection<String> sstFiles) {
+ this.minHeap = new PriorityQueue<>();
init();
+ initMinHeap(sstFiles);
}
protected abstract void init();
protected abstract ClosableIterator<T> getKeyIteratorForFile(String file) throws RocksDBException, IOException;
- @Override
- public boolean hasNext() {
+ private void initMinHeap(Collection<String> files) {
try {
- do {
- if (Objects.nonNull(currentFileIterator) && currentFileIterator.hasNext()) {
- return true;
+ for (String file : files) {
+ ClosableIterator<T> iterator = getKeyIteratorForFile(file);
+ HeapEntryWithFileIdx<T> entry = new HeapEntryWithFileIdx<>(iterator);
+
+ if (entry.getCurrentKey() != null) {
+ minHeap.offer(entry);
+ } else {
+ // No valid entries, close the iterator
+ entry.close();
}
- } while (moveToNextFile());
+ }
} catch (IOException | RocksDBException e) {
- // TODO: [Snapshot] This exception has to be handled by the caller.
- // We have to do better exception handling.
- throw new RuntimeException(e);
+ // Clean up any opened iterators
+ close();
+ throw new RuntimeException("Failed to initialize SST file iterators", e);
}
- return false;
}
@Override
- public T next() {
- if (hasNext()) {
- return currentFileIterator.next();
- }
- throw new NoSuchElementException("No more elements found.");
+ public boolean hasNext() {
+ return !minHeap.isEmpty();
}
@Override
- public void close() throws UncheckedIOException {
- try {
- closeCurrentFile();
- } catch (IOException e) {
- throw new UncheckedIOException(e);
+ public T next() {
+ if (!hasNext()) {
+ throw new NoSuchElementException("No more elements found.");
}
- }
- private boolean moveToNextFile() throws IOException, RocksDBException {
- if (fileNameIterator.hasNext()) {
- closeCurrentFile();
- currentFile = fileNameIterator.next();
- this.currentFileIterator = getKeyIteratorForFile(currentFile);
- return true;
+ assert minHeap.peek() != null;
+ // Get current key from heap
+ T currentKey = minHeap.peek().getCurrentKey();
+
+ // Advance all entries with the same key (from different files)
+ // and keep the one with the highest file index
+ while (!minHeap.isEmpty() && Objects.equals(minHeap.peek().getCurrentKey(), currentKey)) {
Review Comment:
good catch
--
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: issues-unsubscribe@ozone.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@ozone.apache.org
For additional commands, e-mail: issues-help@ozone.apache.org
Re: [PR] HDDS-13005. MinHeap based SstFileSetReader to return unique results in sorted order [ozone]
Posted by "smengcl (via GitHub)" <gi...@apache.org>.
smengcl commented on PR #8912:
URL: https://github.com/apache/ozone/pull/8912#issuecomment-3320603805
Thanks @jojochuang @swamirishi for reviewing this.
--
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: issues-unsubscribe@ozone.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@ozone.apache.org
For additional commands, e-mail: issues-help@ozone.apache.org
Re: [PR] HDDS-13005. MinHeap based SstFileSetReader to return unique results in sorted order [ozone]
Posted by "smengcl (via GitHub)" <gi...@apache.org>.
smengcl commented on code in PR #8912:
URL: https://github.com/apache/ozone/pull/8912#discussion_r2314482585
##########
hadoop-hdds/rocksdb-checkpoint-differ/src/main/java/org/apache/ozone/rocksdb/util/SstFileSetReader.java:
##########
@@ -233,70 +236,180 @@ public String next() {
}
}
- private abstract static class MultipleSstFileIterator<T> implements ClosableIterator<T> {
+ /**
+ * A wrapper class that holds an iterator and its current value for heap operations.
+ */
+ private static class HeapEntryWithFileIdx<T extends Comparable<T>>
+ implements Comparable<HeapEntryWithFileIdx<T>> {
+ private final ClosableIterator<T> iterator;
+ private T currentKey;
+ // To ensure stable ordering for identical keys
+ private final int fileIndex;
+
+ HeapEntryWithFileIdx(ClosableIterator<T> iterator, int fileIndex) {
+ this.iterator = iterator;
+ this.fileIndex = fileIndex;
+ advance();
+ }
+
+ void close() {
+ iterator.close();
+ }
+
+ boolean advance() {
+ if (iterator.hasNext()) {
+ currentKey = iterator.next();
+ return true;
+ } else {
+ currentKey = null;
+ return false;
+ }
+ }
+
+ T getCurrentKey() {
+ return currentKey;
+ }
+
+ @Override
+ public int compareTo(@Nonnull HeapEntryWithFileIdx<T> other) {
+ if (this.currentKey == null && other.currentKey == null) {
+ return 0;
+ }
+ if (this.currentKey == null) {
+ return 1;
+ }
+ if (other.currentKey == null) {
+ return -1;
+ }
+
+ int result = this.currentKey.compareTo(other.currentKey);
+ if (result == 0) {
+ // For identical keys, prefer the one from the file with the higher index
+ return Integer.compare(other.fileIndex, this.fileIndex);
+ }
+ return result;
+ }
+
+ @Override
+ public boolean equals(Object obj) {
+ if (this == obj) {
+ return true;
+ }
+ if (obj == null || getClass() != obj.getClass()) {
+ return false;
+ }
+
+ HeapEntryWithFileIdx<T> other = (HeapEntryWithFileIdx<T>) obj;
+
+ if (this.currentKey == null && other.currentKey == null) {
+ return this.fileIndex == other.fileIndex;
+ }
+ if (this.currentKey == null || other.currentKey == null) {
+ return false;
+ }
+
+ return this.currentKey.equals(other.currentKey) && this.fileIndex == other.fileIndex;
+ }
- private final Iterator<String> fileNameIterator;
+ @Override
+ public int hashCode() {
+ return Objects.hash(iterator, currentKey, fileIndex);
+ }
+ }
- private String currentFile;
- private ClosableIterator<T> currentFileIterator;
+ private abstract static class MultipleSstFileIterator<T extends Comparable<T>> implements ClosableIterator<T> {
+ private final Collection<String> files;
+ private final PriorityQueue<HeapEntryWithFileIdx<T>> minHeap;
+ private final List<HeapEntryWithFileIdx<T>> allIterators;
+ private T lastReturnedValue;
private MultipleSstFileIterator(Collection<String> files) {
- this.fileNameIterator = files.iterator();
+ this.files = files;
+ this.minHeap = new PriorityQueue<>();
+ this.allIterators = new ArrayList<>();
+ this.lastReturnedValue = null;
init();
+ initMinHeap();
}
protected abstract void init();
protected abstract ClosableIterator<T> getKeyIteratorForFile(String file) throws RocksDBException, IOException;
- @Override
- public boolean hasNext() {
+ private void initMinHeap() {
try {
- do {
- if (Objects.nonNull(currentFileIterator) && currentFileIterator.hasNext()) {
- return true;
+ int fileIndex = 0;
+ for (String file : files) {
+ ClosableIterator<T> iterator = getKeyIteratorForFile(file);
+ HeapEntryWithFileIdx<T> entry = new HeapEntryWithFileIdx<>(iterator, fileIndex++);
+ allIterators.add(entry);
+
+ if (entry.getCurrentKey() != null) {
+ minHeap.offer(entry);
}
- } while (moveToNextFile());
+ }
} catch (IOException | RocksDBException e) {
- // TODO: [Snapshot] This exception has to be handled by the caller.
- // We have to do better exception handling.
- throw new RuntimeException(e);
+ // Clean up any opened iterators
+ close();
+ throw new RuntimeException("Failed to initialize SST file iterators", e);
}
- return false;
}
@Override
- public T next() {
- if (hasNext()) {
- return currentFileIterator.next();
+ public boolean hasNext() {
+ // Skip duplicates, keep advancing until we find a different key or run out of entries
+ while (!minHeap.isEmpty()) {
Review Comment:
Yup that's better. Let me make the change.
--
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: issues-unsubscribe@ozone.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@ozone.apache.org
For additional commands, e-mail: issues-help@ozone.apache.org
Re: [PR] HDDS-13005. MinHeap based SstFileSetReader to return unique results in sorted order [ozone]
Posted by "swamirishi (via GitHub)" <gi...@apache.org>.
swamirishi commented on code in PR #8912:
URL: https://github.com/apache/ozone/pull/8912#discussion_r2336522165
##########
hadoop-hdds/rocksdb-checkpoint-differ/src/main/java/org/apache/ozone/rocksdb/util/SstFileSetReader.java:
##########
@@ -233,70 +236,149 @@ public String next() {
}
}
- private abstract static class MultipleSstFileIterator<T> implements ClosableIterator<T> {
+ /**
+ * A wrapper class that holds an iterator and its current value for heap operations.
+ */
+ private static class HeapEntryWithFileIdx<T extends Comparable<T>>
+ implements Comparable<HeapEntryWithFileIdx<T>> {
+ private final ClosableIterator<T> iterator;
+ private T currentKey;
+ // To ensure stable ordering for identical keys
+ private final int fileIndex;
Review Comment:
Consider I have string
1.sst : a,b,c
2.sst : b,c,d
The output is still going to be a,b,c,d we need not be worried about where the entry b and c came from. We are going to return only the keys here
--
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: issues-unsubscribe@ozone.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@ozone.apache.org
For additional commands, e-mail: issues-help@ozone.apache.org
Re: [PR] HDDS-13005. MinHeap based SstFileSetReader to return unique results in sorted order [ozone]
Posted by "swamirishi (via GitHub)" <gi...@apache.org>.
swamirishi commented on code in PR #8912:
URL: https://github.com/apache/ozone/pull/8912#discussion_r2327176357
##########
hadoop-hdds/rocksdb-checkpoint-differ/src/main/java/org/apache/ozone/rocksdb/util/SstFileSetReader.java:
##########
@@ -233,70 +236,149 @@ public String next() {
}
}
- private abstract static class MultipleSstFileIterator<T> implements ClosableIterator<T> {
+ /**
+ * A wrapper class that holds an iterator and its current value for heap operations.
+ */
+ private static class HeapEntryWithFileIdx<T extends Comparable<T>>
+ implements Comparable<HeapEntryWithFileIdx<T>> {
+ private final ClosableIterator<T> iterator;
+ private T currentKey;
+ // To ensure stable ordering for identical keys
+ private final int fileIndex;
+
+ HeapEntryWithFileIdx(ClosableIterator<T> iterator, int fileIndex) {
+ this.iterator = iterator;
+ this.fileIndex = fileIndex;
+ advance();
+ }
+
+ void close() {
+ iterator.close();
+ }
+
+ boolean advance() {
+ if (iterator.hasNext()) {
+ currentKey = iterator.next();
+ return true;
+ } else {
+ currentKey = null;
+ return false;
+ }
+ }
+
+ T getCurrentKey() {
+ return currentKey;
+ }
+
+ @Override
+ public int compareTo(@Nonnull HeapEntryWithFileIdx<T> other) {
+ if (this.currentKey == null && other.currentKey == null) {
+ return 0;
+ }
+ if (this.currentKey == null) {
+ return 1;
+ }
+ if (other.currentKey == null) {
+ return -1;
+ }
+
+ int result = this.currentKey.compareTo(other.currentKey);
+ if (result == 0) {
+ // For identical keys, prefer the one from the file with the higher index
+ return Integer.compare(other.fileIndex, this.fileIndex);
+ }
+ return result;
+ }
+
+ @Override
+ public boolean equals(Object obj) {
+ if (this == obj) {
+ return true;
+ }
+ if (obj == null || getClass() != obj.getClass()) {
+ return false;
+ }
+
+ HeapEntryWithFileIdx<T> other = (HeapEntryWithFileIdx<T>) obj;
+ return compareTo(other) == 0;
+ }
- private final Iterator<String> fileNameIterator;
+ @Override
+ public int hashCode() {
+ return Objects.hash(iterator, currentKey, fileIndex);
+ }
+ }
- private String currentFile;
- private ClosableIterator<T> currentFileIterator;
+ private abstract static class MultipleSstFileIterator<T extends Comparable<T>> implements ClosableIterator<T> {
+ private final Collection<String> files;
+ private final PriorityQueue<HeapEntryWithFileIdx<T>> minHeap;
+ private final List<HeapEntryWithFileIdx<T>> allIterators;
private MultipleSstFileIterator(Collection<String> files) {
- this.fileNameIterator = files.iterator();
+ this.files = files;
+ this.minHeap = new PriorityQueue<>();
+ this.allIterators = new ArrayList<>();
init();
+ initMinHeap();
}
protected abstract void init();
protected abstract ClosableIterator<T> getKeyIteratorForFile(String file) throws RocksDBException, IOException;
- @Override
- public boolean hasNext() {
+ private void initMinHeap() {
try {
- do {
- if (Objects.nonNull(currentFileIterator) && currentFileIterator.hasNext()) {
- return true;
+ int fileIndex = 0;
+ for (String file : files) {
+ ClosableIterator<T> iterator = getKeyIteratorForFile(file);
+ HeapEntryWithFileIdx<T> entry = new HeapEntryWithFileIdx<>(iterator, fileIndex++);
+ allIterators.add(entry);
+
+ if (entry.getCurrentKey() != null) {
+ minHeap.offer(entry);
}
- } while (moveToNextFile());
+ }
} catch (IOException | RocksDBException e) {
- // TODO: [Snapshot] This exception has to be handled by the caller.
- // We have to do better exception handling.
- throw new RuntimeException(e);
+ // Clean up any opened iterators
+ close();
+ throw new RuntimeException("Failed to initialize SST file iterators", e);
}
- return false;
}
@Override
- public T next() {
- if (hasNext()) {
- return currentFileIterator.next();
- }
- throw new NoSuchElementException("No more elements found.");
+ public boolean hasNext() {
+ return !minHeap.isEmpty();
}
@Override
- public void close() throws UncheckedIOException {
- try {
- closeCurrentFile();
- } catch (IOException e) {
- throw new UncheckedIOException(e);
+ public T next() {
+ if (!hasNext()) {
+ throw new NoSuchElementException("No more elements found.");
}
- }
- private boolean moveToNextFile() throws IOException, RocksDBException {
- if (fileNameIterator.hasNext()) {
- closeCurrentFile();
- currentFile = fileNameIterator.next();
- this.currentFileIterator = getKeyIteratorForFile(currentFile);
- return true;
+ assert minHeap.peek() != null;
+ // Get current key from heap
+ T currentKey = minHeap.peek().getCurrentKey();
+
+ // Advance all entries with the same key (from different files)
+ // and keep the one with the highest file index
+ while (!minHeap.isEmpty() && Objects.equals(minHeap.peek().getCurrentKey(), currentKey)) {
+ HeapEntryWithFileIdx<T> entry = minHeap.poll();
+ if (entry.advance()) {
+ minHeap.offer(entry);
+ }
}
- return false;
+
+ return currentKey;
}
- private void closeCurrentFile() throws IOException {
- if (currentFile != null) {
- currentFileIterator.close();
- currentFile = null;
+ @Override
+ public void close() {
+ minHeap.clear();
+ for (HeapEntryWithFileIdx<T> entry : allIterators) {
+ entry.close();
}
+ allIterators.clear();
Review Comment:
```suggestion
while (!minHeap.isEmpty()) {
minHeap.poll().close();
}
```
##########
hadoop-hdds/rocksdb-checkpoint-differ/src/main/java/org/apache/ozone/rocksdb/util/SstFileSetReader.java:
##########
@@ -233,70 +236,149 @@ public String next() {
}
}
- private abstract static class MultipleSstFileIterator<T> implements ClosableIterator<T> {
+ /**
+ * A wrapper class that holds an iterator and its current value for heap operations.
+ */
+ private static class HeapEntryWithFileIdx<T extends Comparable<T>>
+ implements Comparable<HeapEntryWithFileIdx<T>> {
Review Comment:
```suggestion
implements Comparable<HeapEntryWithFileIdx<T>>, Closeable {
```
##########
hadoop-hdds/rocksdb-checkpoint-differ/src/main/java/org/apache/ozone/rocksdb/util/SstFileSetReader.java:
##########
@@ -233,70 +236,149 @@ public String next() {
}
}
- private abstract static class MultipleSstFileIterator<T> implements ClosableIterator<T> {
+ /**
+ * A wrapper class that holds an iterator and its current value for heap operations.
+ */
+ private static class HeapEntryWithFileIdx<T extends Comparable<T>>
+ implements Comparable<HeapEntryWithFileIdx<T>> {
+ private final ClosableIterator<T> iterator;
+ private T currentKey;
+ // To ensure stable ordering for identical keys
+ private final int fileIndex;
+
+ HeapEntryWithFileIdx(ClosableIterator<T> iterator, int fileIndex) {
+ this.iterator = iterator;
+ this.fileIndex = fileIndex;
+ advance();
+ }
+
+ void close() {
+ iterator.close();
+ }
+
+ boolean advance() {
+ if (iterator.hasNext()) {
+ currentKey = iterator.next();
+ return true;
+ } else {
+ currentKey = null;
+ return false;
+ }
+ }
+
+ T getCurrentKey() {
+ return currentKey;
+ }
+
+ @Override
+ public int compareTo(@Nonnull HeapEntryWithFileIdx<T> other) {
+ if (this.currentKey == null && other.currentKey == null) {
+ return 0;
+ }
+ if (this.currentKey == null) {
+ return 1;
+ }
+ if (other.currentKey == null) {
+ return -1;
+ }
+
+ int result = this.currentKey.compareTo(other.currentKey);
+ if (result == 0) {
+ // For identical keys, prefer the one from the file with the higher index
+ return Integer.compare(other.fileIndex, this.fileIndex);
+ }
+ return result;
+ }
+
+ @Override
+ public boolean equals(Object obj) {
+ if (this == obj) {
+ return true;
+ }
+ if (obj == null || getClass() != obj.getClass()) {
+ return false;
+ }
+
+ HeapEntryWithFileIdx<T> other = (HeapEntryWithFileIdx<T>) obj;
+ return compareTo(other) == 0;
+ }
- private final Iterator<String> fileNameIterator;
+ @Override
+ public int hashCode() {
+ return Objects.hash(iterator, currentKey, fileIndex);
+ }
+ }
- private String currentFile;
- private ClosableIterator<T> currentFileIterator;
+ private abstract static class MultipleSstFileIterator<T extends Comparable<T>> implements ClosableIterator<T> {
+ private final Collection<String> files;
+ private final PriorityQueue<HeapEntryWithFileIdx<T>> minHeap;
+ private final List<HeapEntryWithFileIdx<T>> allIterators;
private MultipleSstFileIterator(Collection<String> files) {
- this.fileNameIterator = files.iterator();
+ this.files = files;
+ this.minHeap = new PriorityQueue<>();
+ this.allIterators = new ArrayList<>();
Review Comment:
No need for this additional allIterator list.
##########
hadoop-hdds/rocksdb-checkpoint-differ/src/main/java/org/apache/ozone/rocksdb/util/SstFileSetReader.java:
##########
@@ -233,70 +236,149 @@ public String next() {
}
}
- private abstract static class MultipleSstFileIterator<T> implements ClosableIterator<T> {
+ /**
+ * A wrapper class that holds an iterator and its current value for heap operations.
+ */
+ private static class HeapEntryWithFileIdx<T extends Comparable<T>>
+ implements Comparable<HeapEntryWithFileIdx<T>> {
+ private final ClosableIterator<T> iterator;
+ private T currentKey;
+ // To ensure stable ordering for identical keys
+ private final int fileIndex;
+
+ HeapEntryWithFileIdx(ClosableIterator<T> iterator, int fileIndex) {
+ this.iterator = iterator;
+ this.fileIndex = fileIndex;
+ advance();
+ }
+
+ void close() {
+ iterator.close();
+ }
+
+ boolean advance() {
+ if (iterator.hasNext()) {
+ currentKey = iterator.next();
+ return true;
+ } else {
+ currentKey = null;
+ return false;
+ }
+ }
+
+ T getCurrentKey() {
+ return currentKey;
+ }
+
+ @Override
+ public int compareTo(@Nonnull HeapEntryWithFileIdx<T> other) {
+ if (this.currentKey == null && other.currentKey == null) {
+ return 0;
+ }
+ if (this.currentKey == null) {
+ return 1;
+ }
+ if (other.currentKey == null) {
+ return -1;
+ }
+
+ int result = this.currentKey.compareTo(other.currentKey);
+ if (result == 0) {
+ // For identical keys, prefer the one from the file with the higher index
+ return Integer.compare(other.fileIndex, this.fileIndex);
+ }
+ return result;
+ }
+
+ @Override
+ public boolean equals(Object obj) {
+ if (this == obj) {
+ return true;
+ }
+ if (obj == null || getClass() != obj.getClass()) {
+ return false;
+ }
+
+ HeapEntryWithFileIdx<T> other = (HeapEntryWithFileIdx<T>) obj;
+ return compareTo(other) == 0;
+ }
- private final Iterator<String> fileNameIterator;
+ @Override
+ public int hashCode() {
+ return Objects.hash(iterator, currentKey, fileIndex);
+ }
+ }
- private String currentFile;
- private ClosableIterator<T> currentFileIterator;
+ private abstract static class MultipleSstFileIterator<T extends Comparable<T>> implements ClosableIterator<T> {
+ private final Collection<String> files;
+ private final PriorityQueue<HeapEntryWithFileIdx<T>> minHeap;
+ private final List<HeapEntryWithFileIdx<T>> allIterators;
private MultipleSstFileIterator(Collection<String> files) {
- this.fileNameIterator = files.iterator();
+ this.files = files;
Review Comment:
```suggestion
```
##########
hadoop-hdds/rocksdb-checkpoint-differ/src/main/java/org/apache/ozone/rocksdb/util/SstFileSetReader.java:
##########
@@ -233,70 +236,149 @@ public String next() {
}
}
- private abstract static class MultipleSstFileIterator<T> implements ClosableIterator<T> {
+ /**
+ * A wrapper class that holds an iterator and its current value for heap operations.
+ */
+ private static class HeapEntryWithFileIdx<T extends Comparable<T>>
+ implements Comparable<HeapEntryWithFileIdx<T>> {
+ private final ClosableIterator<T> iterator;
+ private T currentKey;
+ // To ensure stable ordering for identical keys
+ private final int fileIndex;
+
+ HeapEntryWithFileIdx(ClosableIterator<T> iterator, int fileIndex) {
+ this.iterator = iterator;
+ this.fileIndex = fileIndex;
+ advance();
+ }
+
+ void close() {
+ iterator.close();
+ }
+
+ boolean advance() {
+ if (iterator.hasNext()) {
+ currentKey = iterator.next();
+ return true;
+ } else {
+ currentKey = null;
+ return false;
+ }
+ }
+
+ T getCurrentKey() {
+ return currentKey;
+ }
+
+ @Override
+ public int compareTo(@Nonnull HeapEntryWithFileIdx<T> other) {
+ if (this.currentKey == null && other.currentKey == null) {
+ return 0;
+ }
+ if (this.currentKey == null) {
+ return 1;
+ }
+ if (other.currentKey == null) {
+ return -1;
+ }
+
+ int result = this.currentKey.compareTo(other.currentKey);
+ if (result == 0) {
+ // For identical keys, prefer the one from the file with the higher index
+ return Integer.compare(other.fileIndex, this.fileIndex);
+ }
+ return result;
+ }
+
+ @Override
+ public boolean equals(Object obj) {
+ if (this == obj) {
+ return true;
+ }
+ if (obj == null || getClass() != obj.getClass()) {
+ return false;
+ }
+
+ HeapEntryWithFileIdx<T> other = (HeapEntryWithFileIdx<T>) obj;
+ return compareTo(other) == 0;
+ }
- private final Iterator<String> fileNameIterator;
+ @Override
+ public int hashCode() {
+ return Objects.hash(iterator, currentKey, fileIndex);
+ }
+ }
- private String currentFile;
- private ClosableIterator<T> currentFileIterator;
+ private abstract static class MultipleSstFileIterator<T extends Comparable<T>> implements ClosableIterator<T> {
+ private final Collection<String> files;
Review Comment:
Do we need the file list as private member. We don't seem to be using it anywhere. We can pass the files to the initMinHeap() function directly
##########
hadoop-hdds/rocksdb-checkpoint-differ/src/main/java/org/apache/ozone/rocksdb/util/SstFileSetReader.java:
##########
@@ -233,70 +236,149 @@ public String next() {
}
}
- private abstract static class MultipleSstFileIterator<T> implements ClosableIterator<T> {
+ /**
+ * A wrapper class that holds an iterator and its current value for heap operations.
+ */
+ private static class HeapEntryWithFileIdx<T extends Comparable<T>>
+ implements Comparable<HeapEntryWithFileIdx<T>> {
+ private final ClosableIterator<T> iterator;
+ private T currentKey;
+ // To ensure stable ordering for identical keys
+ private final int fileIndex;
+
+ HeapEntryWithFileIdx(ClosableIterator<T> iterator, int fileIndex) {
+ this.iterator = iterator;
+ this.fileIndex = fileIndex;
+ advance();
+ }
+
+ void close() {
+ iterator.close();
+ }
+
+ boolean advance() {
+ if (iterator.hasNext()) {
+ currentKey = iterator.next();
+ return true;
+ } else {
+ currentKey = null;
+ return false;
+ }
+ }
+
+ T getCurrentKey() {
+ return currentKey;
+ }
+
+ @Override
+ public int compareTo(@Nonnull HeapEntryWithFileIdx<T> other) {
+ if (this.currentKey == null && other.currentKey == null) {
+ return 0;
+ }
+ if (this.currentKey == null) {
+ return 1;
+ }
+ if (other.currentKey == null) {
+ return -1;
+ }
+
+ int result = this.currentKey.compareTo(other.currentKey);
+ if (result == 0) {
+ // For identical keys, prefer the one from the file with the higher index
+ return Integer.compare(other.fileIndex, this.fileIndex);
+ }
+ return result;
+ }
+
+ @Override
+ public boolean equals(Object obj) {
+ if (this == obj) {
+ return true;
+ }
+ if (obj == null || getClass() != obj.getClass()) {
+ return false;
+ }
+
+ HeapEntryWithFileIdx<T> other = (HeapEntryWithFileIdx<T>) obj;
+ return compareTo(other) == 0;
+ }
- private final Iterator<String> fileNameIterator;
+ @Override
+ public int hashCode() {
+ return Objects.hash(iterator, currentKey, fileIndex);
+ }
+ }
- private String currentFile;
- private ClosableIterator<T> currentFileIterator;
+ private abstract static class MultipleSstFileIterator<T extends Comparable<T>> implements ClosableIterator<T> {
+ private final Collection<String> files;
+ private final PriorityQueue<HeapEntryWithFileIdx<T>> minHeap;
+ private final List<HeapEntryWithFileIdx<T>> allIterators;
private MultipleSstFileIterator(Collection<String> files) {
- this.fileNameIterator = files.iterator();
+ this.files = files;
+ this.minHeap = new PriorityQueue<>();
+ this.allIterators = new ArrayList<>();
init();
+ initMinHeap();
Review Comment:
```suggestion
initMinHeap(files);
```
##########
hadoop-hdds/rocksdb-checkpoint-differ/src/main/java/org/apache/ozone/rocksdb/util/SstFileSetReader.java:
##########
@@ -233,70 +236,149 @@ public String next() {
}
}
- private abstract static class MultipleSstFileIterator<T> implements ClosableIterator<T> {
+ /**
+ * A wrapper class that holds an iterator and its current value for heap operations.
+ */
+ private static class HeapEntryWithFileIdx<T extends Comparable<T>>
+ implements Comparable<HeapEntryWithFileIdx<T>> {
+ private final ClosableIterator<T> iterator;
+ private T currentKey;
+ // To ensure stable ordering for identical keys
+ private final int fileIndex;
+
+ HeapEntryWithFileIdx(ClosableIterator<T> iterator, int fileIndex) {
+ this.iterator = iterator;
+ this.fileIndex = fileIndex;
+ advance();
+ }
+
+ void close() {
+ iterator.close();
+ }
+
+ boolean advance() {
+ if (iterator.hasNext()) {
+ currentKey = iterator.next();
+ return true;
+ } else {
+ currentKey = null;
+ return false;
+ }
+ }
+
+ T getCurrentKey() {
+ return currentKey;
+ }
+
+ @Override
+ public int compareTo(@Nonnull HeapEntryWithFileIdx<T> other) {
+ if (this.currentKey == null && other.currentKey == null) {
+ return 0;
+ }
+ if (this.currentKey == null) {
+ return 1;
+ }
+ if (other.currentKey == null) {
+ return -1;
+ }
+
+ int result = this.currentKey.compareTo(other.currentKey);
+ if (result == 0) {
+ // For identical keys, prefer the one from the file with the higher index
+ return Integer.compare(other.fileIndex, this.fileIndex);
+ }
+ return result;
+ }
+
+ @Override
+ public boolean equals(Object obj) {
+ if (this == obj) {
+ return true;
+ }
+ if (obj == null || getClass() != obj.getClass()) {
+ return false;
+ }
+
+ HeapEntryWithFileIdx<T> other = (HeapEntryWithFileIdx<T>) obj;
+ return compareTo(other) == 0;
+ }
- private final Iterator<String> fileNameIterator;
+ @Override
+ public int hashCode() {
+ return Objects.hash(iterator, currentKey, fileIndex);
+ }
+ }
- private String currentFile;
- private ClosableIterator<T> currentFileIterator;
+ private abstract static class MultipleSstFileIterator<T extends Comparable<T>> implements ClosableIterator<T> {
+ private final Collection<String> files;
+ private final PriorityQueue<HeapEntryWithFileIdx<T>> minHeap;
+ private final List<HeapEntryWithFileIdx<T>> allIterators;
private MultipleSstFileIterator(Collection<String> files) {
- this.fileNameIterator = files.iterator();
+ this.files = files;
+ this.minHeap = new PriorityQueue<>();
+ this.allIterators = new ArrayList<>();
init();
+ initMinHeap();
}
protected abstract void init();
protected abstract ClosableIterator<T> getKeyIteratorForFile(String file) throws RocksDBException, IOException;
- @Override
- public boolean hasNext() {
+ private void initMinHeap() {
Review Comment:
```suggestion
private void initMinHeap(Collection<String> files) {
```
##########
hadoop-hdds/rocksdb-checkpoint-differ/src/main/java/org/apache/ozone/rocksdb/util/SstFileSetReader.java:
##########
@@ -233,70 +236,149 @@ public String next() {
}
}
- private abstract static class MultipleSstFileIterator<T> implements ClosableIterator<T> {
+ /**
+ * A wrapper class that holds an iterator and its current value for heap operations.
+ */
+ private static class HeapEntryWithFileIdx<T extends Comparable<T>>
+ implements Comparable<HeapEntryWithFileIdx<T>> {
+ private final ClosableIterator<T> iterator;
+ private T currentKey;
+ // To ensure stable ordering for identical keys
+ private final int fileIndex;
Review Comment:
We don't need fileIndex given that we already have the iterator here
##########
hadoop-hdds/rocksdb-checkpoint-differ/src/main/java/org/apache/ozone/rocksdb/util/SstFileSetReader.java:
##########
@@ -233,70 +236,149 @@ public String next() {
}
}
- private abstract static class MultipleSstFileIterator<T> implements ClosableIterator<T> {
+ /**
+ * A wrapper class that holds an iterator and its current value for heap operations.
+ */
+ private static class HeapEntryWithFileIdx<T extends Comparable<T>>
+ implements Comparable<HeapEntryWithFileIdx<T>> {
+ private final ClosableIterator<T> iterator;
+ private T currentKey;
+ // To ensure stable ordering for identical keys
+ private final int fileIndex;
+
+ HeapEntryWithFileIdx(ClosableIterator<T> iterator, int fileIndex) {
+ this.iterator = iterator;
+ this.fileIndex = fileIndex;
+ advance();
+ }
+
+ void close() {
+ iterator.close();
+ }
+
+ boolean advance() {
+ if (iterator.hasNext()) {
+ currentKey = iterator.next();
+ return true;
+ } else {
+ currentKey = null;
+ return false;
+ }
+ }
+
+ T getCurrentKey() {
+ return currentKey;
+ }
+
+ @Override
+ public int compareTo(@Nonnull HeapEntryWithFileIdx<T> other) {
+ if (this.currentKey == null && other.currentKey == null) {
+ return 0;
+ }
+ if (this.currentKey == null) {
+ return 1;
+ }
+ if (other.currentKey == null) {
+ return -1;
+ }
+
+ int result = this.currentKey.compareTo(other.currentKey);
+ if (result == 0) {
+ // For identical keys, prefer the one from the file with the higher index
+ return Integer.compare(other.fileIndex, this.fileIndex);
+ }
+ return result;
+ }
+
+ @Override
+ public boolean equals(Object obj) {
+ if (this == obj) {
+ return true;
+ }
+ if (obj == null || getClass() != obj.getClass()) {
+ return false;
+ }
+
+ HeapEntryWithFileIdx<T> other = (HeapEntryWithFileIdx<T>) obj;
+ return compareTo(other) == 0;
+ }
- private final Iterator<String> fileNameIterator;
+ @Override
+ public int hashCode() {
+ return Objects.hash(iterator, currentKey, fileIndex);
+ }
+ }
- private String currentFile;
- private ClosableIterator<T> currentFileIterator;
+ private abstract static class MultipleSstFileIterator<T extends Comparable<T>> implements ClosableIterator<T> {
+ private final Collection<String> files;
+ private final PriorityQueue<HeapEntryWithFileIdx<T>> minHeap;
+ private final List<HeapEntryWithFileIdx<T>> allIterators;
private MultipleSstFileIterator(Collection<String> files) {
- this.fileNameIterator = files.iterator();
+ this.files = files;
+ this.minHeap = new PriorityQueue<>();
+ this.allIterators = new ArrayList<>();
init();
+ initMinHeap();
}
protected abstract void init();
protected abstract ClosableIterator<T> getKeyIteratorForFile(String file) throws RocksDBException, IOException;
- @Override
- public boolean hasNext() {
+ private void initMinHeap() {
try {
- do {
- if (Objects.nonNull(currentFileIterator) && currentFileIterator.hasNext()) {
- return true;
+ int fileIndex = 0;
+ for (String file : files) {
+ ClosableIterator<T> iterator = getKeyIteratorForFile(file);
+ HeapEntryWithFileIdx<T> entry = new HeapEntryWithFileIdx<>(iterator, fileIndex++);
+ allIterators.add(entry);
+
+ if (entry.getCurrentKey() != null) {
+ minHeap.offer(entry);
}
Review Comment:
```suggestion
} else {
entry.close();
}
```
--
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: issues-unsubscribe@ozone.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@ozone.apache.org
For additional commands, e-mail: issues-help@ozone.apache.org
Re: [PR] HDDS-13005. MinHeap based SstFileSetReader to return unique results in sorted order [ozone]
Posted by "jojochuang (via GitHub)" <gi...@apache.org>.
jojochuang commented on code in PR #8912:
URL: https://github.com/apache/ozone/pull/8912#discussion_r2326024846
##########
hadoop-hdds/rocksdb-checkpoint-differ/src/main/java/org/apache/ozone/rocksdb/util/SstFileSetReader.java:
##########
@@ -233,70 +236,149 @@ public String next() {
}
}
- private abstract static class MultipleSstFileIterator<T> implements ClosableIterator<T> {
+ /**
+ * A wrapper class that holds an iterator and its current value for heap operations.
+ */
+ private static class HeapEntryWithFileIdx<T extends Comparable<T>>
+ implements Comparable<HeapEntryWithFileIdx<T>> {
+ private final ClosableIterator<T> iterator;
+ private T currentKey;
+ // To ensure stable ordering for identical keys
+ private final int fileIndex;
+
+ HeapEntryWithFileIdx(ClosableIterator<T> iterator, int fileIndex) {
+ this.iterator = iterator;
+ this.fileIndex = fileIndex;
+ advance();
+ }
+
+ void close() {
+ iterator.close();
+ }
+
+ boolean advance() {
+ if (iterator.hasNext()) {
+ currentKey = iterator.next();
+ return true;
+ } else {
+ currentKey = null;
+ return false;
+ }
+ }
+
+ T getCurrentKey() {
+ return currentKey;
+ }
+
+ @Override
+ public int compareTo(@Nonnull HeapEntryWithFileIdx<T> other) {
+ if (this.currentKey == null && other.currentKey == null) {
+ return 0;
+ }
+ if (this.currentKey == null) {
+ return 1;
+ }
+ if (other.currentKey == null) {
+ return -1;
+ }
+
+ int result = this.currentKey.compareTo(other.currentKey);
+ if (result == 0) {
+ // For identical keys, prefer the one from the file with the higher index
+ return Integer.compare(other.fileIndex, this.fileIndex);
+ }
+ return result;
+ }
+
+ @Override
+ public boolean equals(Object obj) {
+ if (this == obj) {
+ return true;
+ }
+ if (obj == null || getClass() != obj.getClass()) {
+ return false;
+ }
+
+ HeapEntryWithFileIdx<T> other = (HeapEntryWithFileIdx<T>) obj;
+ return compareTo(other) == 0;
+ }
- private final Iterator<String> fileNameIterator;
+ @Override
+ public int hashCode() {
+ return Objects.hash(iterator, currentKey, fileIndex);
+ }
+ }
- private String currentFile;
- private ClosableIterator<T> currentFileIterator;
+ private abstract static class MultipleSstFileIterator<T extends Comparable<T>> implements ClosableIterator<T> {
Review Comment:
The MultipleSstFileIterator class is an abstract base for iterating over multiple SST files. It uses a PriorityQueue to merge keys from all files in sorted order. Each file's iterator is wrapped in a HeapEntryWithFileIdx object, which ensures stable ordering for identical keys by considering the file index.
--
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: issues-unsubscribe@ozone.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@ozone.apache.org
For additional commands, e-mail: issues-help@ozone.apache.org
Re: [PR] HDDS-13005. MinHeap based SstFileSetReader to return unique results in sorted order [ozone]
Posted by "smengcl (via GitHub)" <gi...@apache.org>.
smengcl commented on code in PR #8912:
URL: https://github.com/apache/ozone/pull/8912#discussion_r2331518801
##########
hadoop-hdds/rocksdb-checkpoint-differ/src/main/java/org/apache/ozone/rocksdb/util/SstFileSetReader.java:
##########
@@ -233,70 +236,149 @@ public String next() {
}
}
- private abstract static class MultipleSstFileIterator<T> implements ClosableIterator<T> {
+ /**
+ * A wrapper class that holds an iterator and its current value for heap operations.
+ */
+ private static class HeapEntryWithFileIdx<T extends Comparable<T>>
+ implements Comparable<HeapEntryWithFileIdx<T>> {
+ private final ClosableIterator<T> iterator;
+ private T currentKey;
+ // To ensure stable ordering for identical keys
+ private final int fileIndex;
+
+ HeapEntryWithFileIdx(ClosableIterator<T> iterator, int fileIndex) {
+ this.iterator = iterator;
+ this.fileIndex = fileIndex;
+ advance();
+ }
+
+ void close() {
+ iterator.close();
+ }
+
+ boolean advance() {
+ if (iterator.hasNext()) {
+ currentKey = iterator.next();
+ return true;
+ } else {
+ currentKey = null;
+ return false;
+ }
+ }
+
+ T getCurrentKey() {
+ return currentKey;
+ }
+
+ @Override
+ public int compareTo(@Nonnull HeapEntryWithFileIdx<T> other) {
+ if (this.currentKey == null && other.currentKey == null) {
+ return 0;
+ }
+ if (this.currentKey == null) {
+ return 1;
+ }
+ if (other.currentKey == null) {
+ return -1;
+ }
+
+ int result = this.currentKey.compareTo(other.currentKey);
+ if (result == 0) {
+ // For identical keys, prefer the one from the file with the higher index
+ return Integer.compare(other.fileIndex, this.fileIndex);
+ }
+ return result;
+ }
+
+ @Override
+ public boolean equals(Object obj) {
+ if (this == obj) {
+ return true;
+ }
+ if (obj == null || getClass() != obj.getClass()) {
+ return false;
+ }
+
+ HeapEntryWithFileIdx<T> other = (HeapEntryWithFileIdx<T>) obj;
+ return compareTo(other) == 0;
+ }
- private final Iterator<String> fileNameIterator;
+ @Override
+ public int hashCode() {
+ return Objects.hash(iterator, currentKey, fileIndex);
+ }
+ }
- private String currentFile;
- private ClosableIterator<T> currentFileIterator;
+ private abstract static class MultipleSstFileIterator<T extends Comparable<T>> implements ClosableIterator<T> {
Review Comment:
Thanks. Added to javadoc
--
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: issues-unsubscribe@ozone.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@ozone.apache.org
For additional commands, e-mail: issues-help@ozone.apache.org
Re: [PR] HDDS-13005. MinHeap based SstFileSetReader to return unique results in sorted order [ozone]
Posted by "swamirishi (via GitHub)" <gi...@apache.org>.
swamirishi commented on code in PR #8912:
URL: https://github.com/apache/ozone/pull/8912#discussion_r2341343197
##########
hadoop-hdds/rocksdb-checkpoint-differ/src/main/java/org/apache/ozone/rocksdb/util/SstFileSetReader.java:
##########
@@ -233,71 +235,146 @@ public String next() {
}
}
- private abstract static class MultipleSstFileIterator<T> implements ClosableIterator<T> {
+ /**
+ * A wrapper class that holds an iterator and its current value for heap operations.
+ */
+ private static class HeapEntryWithFileIdx<T extends Comparable<T>>
+ implements Comparable<HeapEntryWithFileIdx<T>>, Closeable {
+ private final ClosableIterator<T> iterator;
+ private T currentKey;
+
+ HeapEntryWithFileIdx(ClosableIterator<T> iterator, int fileIndex) {
+ this.iterator = iterator;
+ advance();
+ }
+
+ @Override
+ public void close() {
+ iterator.close();
+ }
+
+ boolean advance() {
+ if (iterator.hasNext()) {
+ currentKey = iterator.next();
+ return true;
+ } else {
+ currentKey = null;
+ return false;
+ }
+ }
+
+ T getCurrentKey() {
+ return currentKey;
+ }
+
+ @Override
+ public int compareTo(@Nonnull HeapEntryWithFileIdx<T> other) {
+ if (this.currentKey == null && other.currentKey == null) {
+ return 0;
+ }
+ if (this.currentKey == null) {
+ return 1;
+ }
+ if (other.currentKey == null) {
+ return -1;
+ }
+ return this.currentKey.compareTo(other.currentKey);
+ }
+
+ @Override
+ public boolean equals(Object obj) {
+ if (this == obj) {
+ return true;
+ }
+ if (obj == null || getClass() != obj.getClass()) {
+ return false;
+ }
- private final Iterator<String> fileNameIterator;
+ HeapEntryWithFileIdx<T> other = (HeapEntryWithFileIdx<T>) obj;
+ return compareTo(other) == 0;
+ }
- private String currentFile;
- private ClosableIterator<T> currentFileIterator;
+ @Override
+ public int hashCode() {
+ return Objects.hash(iterator, currentKey);
+ }
+ }
- private MultipleSstFileIterator(Collection<String> files) {
- this.fileNameIterator = files.iterator();
+ /**
+ * The MultipleSstFileIterator class is an abstract base for iterating over multiple SST files.
+ * It uses a PriorityQueue to merge keys from all files in sorted order.
+ * Each file's iterator is wrapped in a HeapEntryWithFileIdx object,
+ * which ensures stable ordering for identical keys by considering the file index.
+ * @param <T>
+ */
+ private abstract static class MultipleSstFileIterator<T extends Comparable<T>> implements ClosableIterator<T> {
+ private final PriorityQueue<HeapEntryWithFileIdx<T>> minHeap;
+
+ private MultipleSstFileIterator(Collection<String> sstFiles) {
+ this.minHeap = new PriorityQueue<>();
init();
+ initMinHeap(sstFiles);
}
protected abstract void init();
protected abstract ClosableIterator<T> getKeyIteratorForFile(String file) throws RocksDBException, IOException;
- @Override
- public boolean hasNext() {
+ private void initMinHeap(Collection<String> files) {
try {
- do {
- if (Objects.nonNull(currentFileIterator) && currentFileIterator.hasNext()) {
- return true;
+ int fileIndex = 0;
+ for (String file : files) {
+ ClosableIterator<T> iterator = getKeyIteratorForFile(file);
+ HeapEntryWithFileIdx<T> entry = new HeapEntryWithFileIdx<>(iterator, fileIndex++);
+
+ if (entry.getCurrentKey() != null) {
+ minHeap.offer(entry);
+ } else {
+ // No valid entries, close the iterator
+ entry.close();
}
- } while (moveToNextFile());
+ }
} catch (IOException | RocksDBException e) {
- // TODO: [Snapshot] This exception has to be handled by the caller.
- // We have to do better exception handling.
- throw new RuntimeException(e);
+ // Clean up any opened iterators
+ close();
+ throw new RuntimeException("Failed to initialize SST file iterators", e);
}
- return false;
}
@Override
- public T next() {
- if (hasNext()) {
- return currentFileIterator.next();
- }
- throw new NoSuchElementException("No more elements found.");
+ public boolean hasNext() {
+ return !minHeap.isEmpty();
}
@Override
- public void close() throws UncheckedIOException {
- try {
- closeCurrentFile();
- } catch (IOException e) {
- throw new UncheckedIOException(e);
+ public T next() {
+ if (!hasNext()) {
+ throw new NoSuchElementException("No more elements found.");
}
- }
- private boolean moveToNextFile() throws IOException, RocksDBException {
- if (fileNameIterator.hasNext()) {
- closeCurrentFile();
- currentFile = fileNameIterator.next();
- this.currentFileIterator = getKeyIteratorForFile(currentFile);
- return true;
+ assert minHeap.peek() != null;
+ // Get current key from heap
+ T currentKey = minHeap.peek().getCurrentKey();
Review Comment:
```suggestion
HeapEntry currentKey = minHeap.peek();
```
##########
hadoop-hdds/rocksdb-checkpoint-differ/src/main/java/org/apache/ozone/rocksdb/util/SstFileSetReader.java:
##########
@@ -233,71 +235,146 @@ public String next() {
}
}
- private abstract static class MultipleSstFileIterator<T> implements ClosableIterator<T> {
+ /**
+ * A wrapper class that holds an iterator and its current value for heap operations.
+ */
+ private static class HeapEntryWithFileIdx<T extends Comparable<T>>
+ implements Comparable<HeapEntryWithFileIdx<T>>, Closeable {
+ private final ClosableIterator<T> iterator;
+ private T currentKey;
+
+ HeapEntryWithFileIdx(ClosableIterator<T> iterator, int fileIndex) {
+ this.iterator = iterator;
+ advance();
+ }
+
+ @Override
+ public void close() {
+ iterator.close();
+ }
+
+ boolean advance() {
+ if (iterator.hasNext()) {
+ currentKey = iterator.next();
+ return true;
+ } else {
+ currentKey = null;
+ return false;
+ }
+ }
+
+ T getCurrentKey() {
+ return currentKey;
+ }
+
+ @Override
+ public int compareTo(@Nonnull HeapEntryWithFileIdx<T> other) {
+ if (this.currentKey == null && other.currentKey == null) {
+ return 0;
+ }
+ if (this.currentKey == null) {
+ return 1;
+ }
+ if (other.currentKey == null) {
+ return -1;
+ }
+ return this.currentKey.compareTo(other.currentKey);
+ }
+
+ @Override
+ public boolean equals(Object obj) {
+ if (this == obj) {
+ return true;
+ }
+ if (obj == null || getClass() != obj.getClass()) {
+ return false;
+ }
- private final Iterator<String> fileNameIterator;
+ HeapEntryWithFileIdx<T> other = (HeapEntryWithFileIdx<T>) obj;
+ return compareTo(other) == 0;
+ }
- private String currentFile;
- private ClosableIterator<T> currentFileIterator;
+ @Override
+ public int hashCode() {
+ return Objects.hash(iterator, currentKey);
+ }
+ }
- private MultipleSstFileIterator(Collection<String> files) {
- this.fileNameIterator = files.iterator();
+ /**
+ * The MultipleSstFileIterator class is an abstract base for iterating over multiple SST files.
+ * It uses a PriorityQueue to merge keys from all files in sorted order.
+ * Each file's iterator is wrapped in a HeapEntryWithFileIdx object,
+ * which ensures stable ordering for identical keys by considering the file index.
+ * @param <T>
+ */
+ private abstract static class MultipleSstFileIterator<T extends Comparable<T>> implements ClosableIterator<T> {
+ private final PriorityQueue<HeapEntryWithFileIdx<T>> minHeap;
+
+ private MultipleSstFileIterator(Collection<String> sstFiles) {
+ this.minHeap = new PriorityQueue<>();
init();
+ initMinHeap(sstFiles);
}
protected abstract void init();
protected abstract ClosableIterator<T> getKeyIteratorForFile(String file) throws RocksDBException, IOException;
- @Override
- public boolean hasNext() {
+ private void initMinHeap(Collection<String> files) {
try {
- do {
- if (Objects.nonNull(currentFileIterator) && currentFileIterator.hasNext()) {
- return true;
+ int fileIndex = 0;
+ for (String file : files) {
+ ClosableIterator<T> iterator = getKeyIteratorForFile(file);
+ HeapEntryWithFileIdx<T> entry = new HeapEntryWithFileIdx<>(iterator, fileIndex++);
+
+ if (entry.getCurrentKey() != null) {
+ minHeap.offer(entry);
+ } else {
+ // No valid entries, close the iterator
+ entry.close();
}
- } while (moveToNextFile());
+ }
} catch (IOException | RocksDBException e) {
- // TODO: [Snapshot] This exception has to be handled by the caller.
- // We have to do better exception handling.
- throw new RuntimeException(e);
+ // Clean up any opened iterators
+ close();
+ throw new RuntimeException("Failed to initialize SST file iterators", e);
}
- return false;
}
@Override
- public T next() {
- if (hasNext()) {
- return currentFileIterator.next();
- }
- throw new NoSuchElementException("No more elements found.");
+ public boolean hasNext() {
+ return !minHeap.isEmpty();
}
@Override
- public void close() throws UncheckedIOException {
- try {
- closeCurrentFile();
- } catch (IOException e) {
- throw new UncheckedIOException(e);
+ public T next() {
+ if (!hasNext()) {
+ throw new NoSuchElementException("No more elements found.");
}
- }
- private boolean moveToNextFile() throws IOException, RocksDBException {
- if (fileNameIterator.hasNext()) {
- closeCurrentFile();
- currentFile = fileNameIterator.next();
- this.currentFileIterator = getKeyIteratorForFile(currentFile);
- return true;
+ assert minHeap.peek() != null;
+ // Get current key from heap
+ T currentKey = minHeap.peek().getCurrentKey();
Review Comment:
```suggestion
HeapEntry<T> currentKey = minHeap.peek();
```
--
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: issues-unsubscribe@ozone.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@ozone.apache.org
For additional commands, e-mail: issues-help@ozone.apache.org
Re: [PR] HDDS-13005. MinHeap based SstFileSetReader to return unique results in sorted order [ozone]
Posted by "swamirishi (via GitHub)" <gi...@apache.org>.
swamirishi commented on code in PR #8912:
URL: https://github.com/apache/ozone/pull/8912#discussion_r2344505540
##########
hadoop-hdds/rocksdb-checkpoint-differ/src/main/java/org/apache/ozone/rocksdb/util/SstFileSetReader.java:
##########
@@ -233,69 +235,145 @@ public String next() {
}
}
- private abstract static class MultipleSstFileIterator<T> implements ClosableIterator<T> {
+ /**
+ * A wrapper class that holds an iterator and its current value for heap operations.
+ */
+ private static class HeapEntryWithFileIdx<T extends Comparable<T>>
+ implements Comparable<HeapEntryWithFileIdx<T>>, Closeable {
+ private final ClosableIterator<T> iterator;
+ private T currentKey;
+
+ HeapEntryWithFileIdx(ClosableIterator<T> iterator) {
+ this.iterator = iterator;
+ advance();
+ }
+
+ @Override
+ public void close() {
+ iterator.close();
+ }
+
+ boolean advance() {
+ if (iterator.hasNext()) {
+ currentKey = iterator.next();
+ return true;
+ } else {
+ currentKey = null;
+ return false;
+ }
+ }
+
+ T getCurrentKey() {
+ return currentKey;
+ }
+
+ @Override
+ public int compareTo(@Nonnull HeapEntryWithFileIdx<T> other) {
+ if (this.currentKey == null && other.currentKey == null) {
+ return 0;
+ }
+ if (this.currentKey == null) {
+ return 1;
+ }
+ if (other.currentKey == null) {
+ return -1;
+ }
+ return this.currentKey.compareTo(other.currentKey);
+ }
+
+ @Override
+ public boolean equals(Object obj) {
+ if (this == obj) {
+ return true;
+ }
+ if (obj == null || getClass() != obj.getClass()) {
+ return false;
+ }
- private final Iterator<String> fileNameIterator;
+ HeapEntryWithFileIdx<T> other = (HeapEntryWithFileIdx<T>) obj;
+ return compareTo(other) == 0;
+ }
- private String currentFile;
- private ClosableIterator<T> currentFileIterator;
+ @Override
+ public int hashCode() {
+ return Objects.hash(iterator, currentKey);
+ }
+ }
- private MultipleSstFileIterator(Collection<String> files) {
- this.fileNameIterator = files.iterator();
+ /**
+ * The MultipleSstFileIterator class is an abstract base for iterating over multiple SST files.
+ * It uses a PriorityQueue to merge keys from all files in sorted order.
+ * Each file's iterator is wrapped in a HeapEntryWithFileIdx object,
+ * which ensures stable ordering for identical keys by considering the file index.
+ * @param <T>
+ */
+ private abstract static class MultipleSstFileIterator<T extends Comparable<T>> implements ClosableIterator<T> {
+ private final PriorityQueue<HeapEntryWithFileIdx<T>> minHeap;
+
+ private MultipleSstFileIterator(Collection<String> sstFiles) {
+ this.minHeap = new PriorityQueue<>();
init();
+ initMinHeap(sstFiles);
}
protected abstract void init();
protected abstract ClosableIterator<T> getKeyIteratorForFile(String file) throws RocksDBException, IOException;
- @Override
- public boolean hasNext() {
+ private void initMinHeap(Collection<String> files) {
try {
- do {
- if (Objects.nonNull(currentFileIterator) && currentFileIterator.hasNext()) {
- return true;
+ for (String file : files) {
+ ClosableIterator<T> iterator = getKeyIteratorForFile(file);
+ HeapEntryWithFileIdx<T> entry = new HeapEntryWithFileIdx<>(iterator);
+
+ if (entry.getCurrentKey() != null) {
+ minHeap.offer(entry);
+ } else {
+ // No valid entries, close the iterator
+ entry.close();
}
- } while (moveToNextFile());
+ }
} catch (IOException | RocksDBException e) {
- // TODO: [Snapshot] This exception has to be handled by the caller.
- // We have to do better exception handling.
- throw new RuntimeException(e);
+ // Clean up any opened iterators
+ close();
+ throw new RuntimeException("Failed to initialize SST file iterators", e);
}
- return false;
}
@Override
- public T next() {
- if (hasNext()) {
- return currentFileIterator.next();
- }
- throw new NoSuchElementException("No more elements found.");
+ public boolean hasNext() {
+ return !minHeap.isEmpty();
}
@Override
- public void close() throws UncheckedIOException {
- try {
- closeCurrentFile();
- } catch (IOException e) {
- throw new UncheckedIOException(e);
+ public T next() {
+ if (!hasNext()) {
+ throw new NoSuchElementException("No more elements found.");
}
- }
- private boolean moveToNextFile() throws IOException, RocksDBException {
- if (fileNameIterator.hasNext()) {
- closeCurrentFile();
- currentFile = fileNameIterator.next();
- this.currentFileIterator = getKeyIteratorForFile(currentFile);
- return true;
+ assert minHeap.peek() != null;
+ // Get current key from heap
+ T currentKey = minHeap.peek().getCurrentKey();
+
+ // Advance all entries with the same key (from different files)
+ // and keep the one with the highest file index
+ while (!minHeap.isEmpty() && Objects.equals(minHeap.peek().getCurrentKey(), currentKey)) {
Review Comment:
```suggestion
while (!minHeap.isEmpty() && Objects.equals(minHeap.peek(), currentKey)) {
```
--
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: issues-unsubscribe@ozone.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@ozone.apache.org
For additional commands, e-mail: issues-help@ozone.apache.org