[ 
https://issues.apache.org/jira/browse/FLINK-10041?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=16576399#comment-16576399
 ] 

ASF GitHub Bot commented on FLINK-10041:
----------------------------------------

azagrebin commented on a change in pull request #6501: [FLINK-10041] Extract 
iterators from RocksDBKeyedStateBackend (inner …
URL: https://github.com/apache/flink/pull/6501#discussion_r209288519
 
 

 ##########
 File path: 
flink-state-backends/flink-statebackend-rocksdb/src/main/java/org/apache/flink/contrib/streaming/state/iterator/RocksStatesPerKeyGroupMergeIterator.java
 ##########
 @@ -0,0 +1,217 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.flink.contrib.streaming.state.iterator;
+
+import org.apache.flink.api.java.tuple.Tuple2;
+import org.apache.flink.contrib.streaming.state.RocksIteratorWrapper;
+import org.apache.flink.util.IOUtils;
+import org.apache.flink.util.Preconditions;
+
+import java.util.ArrayList;
+import java.util.Comparator;
+import java.util.List;
+import java.util.PriorityQueue;
+
+/**
+ * Iterator that merges multiple RocksDB iterators to partition all states 
into contiguous key-groups.
+ * The resulting iteration sequence is ordered by (key-group, kv-state).
+ */
+public class RocksStatesPerKeyGroupMergeIterator implements AutoCloseable {
+
+       private final PriorityQueue<RocksSingleStateIterator> heap;
+       private final int keyGroupPrefixByteCount;
+       private boolean newKeyGroup;
+       private boolean newKVState;
+       private boolean valid;
+       private RocksSingleStateIterator currentSubIterator;
+
+       private static final List<Comparator<RocksSingleStateIterator>> 
COMPARATORS;
+
+       static {
+               int maxBytes = 2;
+               COMPARATORS = new ArrayList<>(maxBytes);
+               for (int i = 0; i < maxBytes; ++i) {
+                       final int currentBytes = i + 1;
+                       COMPARATORS.add((o1, o2) -> {
+                               int arrayCmpRes = compareKeyGroupsForByteArrays(
+                                       o1.getCurrentKey(), o2.getCurrentKey(), 
currentBytes);
+                               return arrayCmpRes == 0 ? o1.getKvStateId() - 
o2.getKvStateId() : arrayCmpRes;
+                       });
+               }
+       }
+
+       public RocksStatesPerKeyGroupMergeIterator(
+               List<Tuple2<RocksIteratorWrapper, Integer>> kvStateIterators,
+               final int keyGroupPrefixByteCount) {
+               Preconditions.checkNotNull(kvStateIterators);
+               Preconditions.checkArgument(keyGroupPrefixByteCount >= 1);
+
+               this.keyGroupPrefixByteCount = keyGroupPrefixByteCount;
+
+               Comparator<RocksSingleStateIterator> iteratorComparator = 
COMPARATORS.get(keyGroupPrefixByteCount - 1);
+
+               if (kvStateIterators.size() > 0) {
+                       PriorityQueue<RocksSingleStateIterator> 
iteratorPriorityQueue =
+                               new PriorityQueue<>(kvStateIterators.size(), 
iteratorComparator);
+
+                       for (Tuple2<RocksIteratorWrapper, Integer> 
rocksIteratorWithKVStateId : kvStateIterators) {
+                               final RocksIteratorWrapper rocksIterator = 
rocksIteratorWithKVStateId.f0;
+                               rocksIterator.seekToFirst();
+                               if (rocksIterator.isValid()) {
+                                       iteratorPriorityQueue.offer(new 
RocksSingleStateIterator(rocksIterator, rocksIteratorWithKVStateId.f1));
+                               } else {
+                                       IOUtils.closeQuietly(rocksIterator);
+                               }
+                       }
+
+                       kvStateIterators.clear();
+
+                       this.heap = iteratorPriorityQueue;
+                       this.valid = !heap.isEmpty();
+                       this.currentSubIterator = heap.poll();
+               } else {
+                       // creating a PriorityQueue of size 0 results in an 
exception.
+                       this.heap = null;
+                       this.valid = false;
+               }
+
+               this.newKeyGroup = true;
+               this.newKVState = true;
+       }
+
+       /**
+        * Advance the iterator. Should only be called if {@link #isValid()} 
returned true. Valid can only chance after
+        * calls to {@link #next()}.
 
 Review comment:
   Typos:
   Valid flag can only *change* after calling {@link #next()}

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
us...@infra.apache.org


> Extract all different iterators (inner or static inner classes) into full 
> classes
> ---------------------------------------------------------------------------------
>
>                 Key: FLINK-10041
>                 URL: https://issues.apache.org/jira/browse/FLINK-10041
>             Project: Flink
>          Issue Type: Sub-task
>            Reporter: Stefan Richter
>            Assignee: Stefan Richter
>            Priority: Major
>              Labels: pull-request-available
>




--
This message was sent by Atlassian JIRA
(v7.6.3#76005)

Reply via email to