-
Notifications
You must be signed in to change notification settings - Fork 532
[KV] Kvstore snapshot queries #2409
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
Changes from 7 commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
4155cf7
add snapshot query fucntionality
polyzos 60bae8e
update api and add tests
polyzos 63ad71f
update tests
polyzos ae488e3
update the snapshotQuery and the tests
polyzos 7808564
fix proto
polyzos c8f5464
fix violation
polyzos 1bd22d8
update interface
polyzos 43a01e7
remove keepalive and reuse TS executor
polyzos b3cd3d7
test
polyzos 40d978d
update tests
polyzos 093c8f9
fix small issues
polyzos 0f8380b
refactor
polyzos 730b2ae
add some optimizations
polyzos b3d5b7a
update
polyzos File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
47 changes: 47 additions & 0 deletions
47
fluss-client/src/main/java/org/apache/fluss/client/table/scanner/SnapshotQuery.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,47 @@ | ||
| /* | ||
| * 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.fluss.client.table.scanner; | ||
|
|
||
| import org.apache.fluss.annotation.PublicEvolving; | ||
| import org.apache.fluss.metadata.TableBucket; | ||
| import org.apache.fluss.row.InternalRow; | ||
| import org.apache.fluss.utils.CloseableIterator; | ||
|
|
||
| /** | ||
| * Used to configure and execute a snapshot query to read all kv data of a primary key table. | ||
| * | ||
| * @since 0.9 | ||
| */ | ||
| @PublicEvolving | ||
| public interface SnapshotQuery { | ||
| /** | ||
| * Executes the snapshot query to read all current data in the given table bucket. | ||
| * | ||
| * @param tableBucket the table bucket to read | ||
| * @return a closeable iterator of the rows in the table bucket | ||
| */ | ||
| CloseableIterator<InternalRow> execute(TableBucket tableBucket); | ||
|
|
||
| /** | ||
| * Executes the snapshot query to read all current data in the table. Everything around | ||
| * partitions and buckets will be taken care of from the client. | ||
| * | ||
| * @return a closeable iterator of the rows in the table | ||
| */ | ||
| CloseableIterator<InternalRow> execute(); | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
204 changes: 204 additions & 0 deletions
204
fluss-client/src/main/java/org/apache/fluss/client/table/scanner/TableSnapshotQuery.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,204 @@ | ||
| /* | ||
| * 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.fluss.client.table.scanner; | ||
|
|
||
| import org.apache.fluss.client.FlussConnection; | ||
| import org.apache.fluss.client.table.scanner.batch.BatchScanner; | ||
| import org.apache.fluss.exception.FlussRuntimeException; | ||
| import org.apache.fluss.metadata.PartitionInfo; | ||
| import org.apache.fluss.metadata.SchemaGetter; | ||
| import org.apache.fluss.metadata.TableBucket; | ||
| import org.apache.fluss.metadata.TableInfo; | ||
| import org.apache.fluss.row.InternalRow; | ||
| import org.apache.fluss.utils.CloseableIterator; | ||
|
|
||
| import javax.annotation.Nullable; | ||
|
|
||
| import java.io.IOException; | ||
| import java.time.Duration; | ||
| import java.util.ArrayList; | ||
| import java.util.Iterator; | ||
| import java.util.List; | ||
| import java.util.NoSuchElementException; | ||
|
|
||
| /** Implementation of {@link SnapshotQuery}. */ | ||
| public class TableSnapshotQuery implements SnapshotQuery { | ||
|
|
||
| private final FlussConnection conn; | ||
| private final TableInfo tableInfo; | ||
| private final SchemaGetter schemaGetter; | ||
|
|
||
| /** The projected fields to do projection. No projection if is null. */ | ||
| @Nullable private final int[] projectedColumns; | ||
|
|
||
| public TableSnapshotQuery( | ||
| FlussConnection conn, TableInfo tableInfo, SchemaGetter schemaGetter) { | ||
| this(conn, tableInfo, schemaGetter, null); | ||
| } | ||
|
|
||
| private TableSnapshotQuery( | ||
| FlussConnection conn, | ||
| TableInfo tableInfo, | ||
| SchemaGetter schemaGetter, | ||
| @Nullable int[] projectedColumns) { | ||
| this.conn = conn; | ||
| this.tableInfo = tableInfo; | ||
| this.schemaGetter = schemaGetter; | ||
| this.projectedColumns = projectedColumns; | ||
| } | ||
|
|
||
| @Override | ||
| public CloseableIterator<InternalRow> execute(TableBucket tableBucket) { | ||
| Scan scan = new TableScan(conn, tableInfo, schemaGetter); | ||
| if (projectedColumns != null) { | ||
| scan = scan.project(projectedColumns); | ||
| } | ||
| BatchScanner batchScanner = scan.createBatchScanner(tableBucket); | ||
| return new BatchScannerIterator(batchScanner); | ||
| } | ||
|
|
||
| @Override | ||
| public CloseableIterator<InternalRow> execute() { | ||
| List<TableBucket> buckets = new ArrayList<>(); | ||
| try { | ||
| if (tableInfo.isPartitioned()) { | ||
| List<PartitionInfo> partitions = | ||
| conn.getAdmin().listPartitionInfos(tableInfo.getTablePath()).get(); | ||
| for (PartitionInfo partition : partitions) { | ||
| for (int i = 0; i < tableInfo.getNumBuckets(); i++) { | ||
| buckets.add( | ||
| new TableBucket( | ||
| tableInfo.getTableId(), partition.getPartitionId(), i)); | ||
| } | ||
| } | ||
| } else { | ||
| for (int i = 0; i < tableInfo.getNumBuckets(); i++) { | ||
| buckets.add(new TableBucket(tableInfo.getTableId(), i)); | ||
| } | ||
| } | ||
| } catch (Exception e) { | ||
| throw new FlussRuntimeException( | ||
| "Failed to list partitions for table " + tableInfo.getTablePath(), e); | ||
| } | ||
|
|
||
| return new MultiBucketBatchScannerIterator(buckets); | ||
| } | ||
|
|
||
| private class MultiBucketBatchScannerIterator implements CloseableIterator<InternalRow> { | ||
| private final Iterator<TableBucket> bucketIterator; | ||
| private CloseableIterator<InternalRow> currentScannerIterator; | ||
| private boolean isClosed = false; | ||
|
|
||
| private MultiBucketBatchScannerIterator(List<TableBucket> buckets) { | ||
| this.bucketIterator = buckets.iterator(); | ||
| } | ||
|
|
||
| @Override | ||
| public boolean hasNext() { | ||
| if (isClosed) { | ||
| return false; | ||
| } | ||
| while (currentScannerIterator == null || !currentScannerIterator.hasNext()) { | ||
| if (currentScannerIterator != null) { | ||
| currentScannerIterator.close(); | ||
| currentScannerIterator = null; | ||
| } | ||
| if (bucketIterator.hasNext()) { | ||
| currentScannerIterator = execute(bucketIterator.next()); | ||
| } else { | ||
| return false; | ||
| } | ||
| } | ||
| return true; | ||
| } | ||
|
|
||
| @Override | ||
| public InternalRow next() { | ||
| if (!hasNext()) { | ||
| throw new NoSuchElementException(); | ||
| } | ||
| return currentScannerIterator.next(); | ||
| } | ||
|
|
||
| @Override | ||
| public void close() { | ||
| if (!isClosed) { | ||
| if (currentScannerIterator != null) { | ||
| currentScannerIterator.close(); | ||
| } | ||
| isClosed = true; | ||
| } | ||
| } | ||
| } | ||
|
|
||
| private static class BatchScannerIterator implements CloseableIterator<InternalRow> { | ||
| private final BatchScanner scanner; | ||
| private Iterator<InternalRow> currentBatch; | ||
| private boolean isClosed = false; | ||
|
|
||
| private BatchScannerIterator(BatchScanner scanner) { | ||
| this.scanner = scanner; | ||
| } | ||
|
|
||
| @Override | ||
| public boolean hasNext() { | ||
| ensureBatch(); | ||
| return currentBatch != null && currentBatch.hasNext(); | ||
| } | ||
|
|
||
| @Override | ||
| public InternalRow next() { | ||
| if (!hasNext()) { | ||
| throw new NoSuchElementException(); | ||
| } | ||
| return currentBatch.next(); | ||
| } | ||
|
|
||
| private void ensureBatch() { | ||
| try { | ||
| while ((currentBatch == null || !currentBatch.hasNext()) && !isClosed) { | ||
| CloseableIterator<InternalRow> it = | ||
| scanner.pollBatch(Duration.ofMinutes(1)); // Use a large timeout | ||
| if (it == null) { | ||
| isClosed = true; | ||
| break; | ||
| } | ||
| if (it.hasNext()) { | ||
| currentBatch = it; | ||
| } else { | ||
| it.close(); | ||
| } | ||
| } | ||
| } catch (IOException e) { | ||
| throw new RuntimeException("Error polling batch from scanner", e); | ||
| } | ||
| } | ||
|
|
||
| @Override | ||
| public void close() { | ||
| if (!isClosed) { | ||
| try { | ||
| scanner.close(); | ||
| } catch (IOException e) { | ||
| throw new RuntimeException("Error closing scanner", e); | ||
| } | ||
| isClosed = true; | ||
| } | ||
| } | ||
| } | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nit: SnapshotQuery sounds like querying an existing snapshot. I cannot think of any name though.. maybe just Query?