-
Notifications
You must be signed in to change notification settings - Fork 1k
PHOENIX-7878 CDC perf improvement - skip redundant cell versions on data table scans #2493
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
Open
virajjasani
wants to merge
2
commits into
apache:master
Choose a base branch
from
virajjasani:PHOENIX-7878-master
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+917
−15
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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
223 changes: 223 additions & 0 deletions
223
phoenix-core-client/src/main/java/org/apache/phoenix/filter/CDCVersionFilter.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,223 @@ | ||
| /* | ||
| * 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.phoenix.filter; | ||
|
|
||
| import java.io.DataInput; | ||
| import java.io.DataOutput; | ||
| import java.io.IOException; | ||
| import java.util.HashMap; | ||
| import java.util.Map; | ||
| import org.apache.hadoop.hbase.Cell; | ||
| import org.apache.hadoop.hbase.CellUtil; | ||
| import org.apache.hadoop.hbase.exceptions.DeserializationException; | ||
| import org.apache.hadoop.hbase.filter.FilterBase; | ||
| import org.apache.hadoop.hbase.util.Bytes; | ||
| import org.apache.hadoop.hbase.util.Writables; | ||
| import org.apache.hadoop.io.Writable; | ||
| import org.apache.phoenix.hbase.index.util.ImmutableBytesPtr; | ||
|
|
||
| /** | ||
| * Filter for CDC data table scans that prunes redundant cell versions. For each row, given a set of | ||
| * change timestamps (from the CDC batch), this filter includes only: | ||
| * <ul> | ||
| * <li>Cells at a change timestamp (the change itself)</li> | ||
| * <li>The first cell below each change timestamp per column (the pre-image)</li> | ||
| * <li>All DeleteFamily markers (needed for CDC deletion tracking)</li> | ||
| * </ul> | ||
| * All other cell versions are skipped, reducing network I/O, memory, and processing overhead. | ||
| * <p> | ||
| * Within each column (family:qualifier), HBase delivers cells in timestamp-descending order. The | ||
| * filter maintains a pointer into the sorted change timestamps and tracks whether a pre-image is | ||
| * still needed, advancing through the timestamps as cells arrive. | ||
| */ | ||
| public class CDCVersionFilter extends FilterBase implements Writable { | ||
|
|
||
| private Map<ImmutableBytesPtr, long[]> timestampMap; | ||
|
|
||
| // Per-row state | ||
| private long[] currentTimestamps; | ||
| private byte[] currentRowKey; | ||
|
|
||
| // Per-column state | ||
| private byte[] prevFamily; | ||
| private byte[] prevQualifier; | ||
| private int tsIdx; | ||
| private boolean needPreImage; | ||
|
|
||
| public CDCVersionFilter() { | ||
| } | ||
|
|
||
| /** | ||
| * @param timestampMap mapping of data row key to sorted (descending) array of change timestamps | ||
| * for that row | ||
| */ | ||
| public CDCVersionFilter(Map<ImmutableBytesPtr, long[]> timestampMap) { | ||
| this.timestampMap = timestampMap; | ||
| } | ||
|
|
||
| @Override | ||
| public void reset() throws IOException { | ||
| currentTimestamps = null; | ||
| currentRowKey = null; | ||
| resetColumnState(); | ||
| } | ||
|
|
||
| private void resetColumnState() { | ||
| prevFamily = null; | ||
| prevQualifier = null; | ||
| tsIdx = 0; | ||
| needPreImage = false; | ||
| } | ||
|
|
||
| private boolean isNewRow(Cell cell) { | ||
| if (currentRowKey == null) { | ||
| return true; | ||
| } | ||
| return !Bytes.equals(currentRowKey, 0, currentRowKey.length, cell.getRowArray(), | ||
| cell.getRowOffset(), cell.getRowLength()); | ||
| } | ||
|
|
||
| private void onNewRow(Cell cell) { | ||
| currentRowKey = CellUtil.cloneRow(cell); | ||
| ImmutableBytesPtr rowKeyPtr = new ImmutableBytesPtr(currentRowKey); | ||
| currentTimestamps = timestampMap != null ? timestampMap.get(rowKeyPtr) : null; | ||
| resetColumnState(); | ||
| } | ||
|
|
||
| private boolean isNewColumn(Cell cell) { | ||
| if (prevFamily == null) { | ||
| return true; | ||
| } | ||
| if ( | ||
| !Bytes.equals(prevFamily, 0, prevFamily.length, cell.getFamilyArray(), cell.getFamilyOffset(), | ||
| cell.getFamilyLength()) | ||
| ) { | ||
| return true; | ||
| } | ||
| return !Bytes.equals(prevQualifier, 0, prevQualifier.length, cell.getQualifierArray(), | ||
| cell.getQualifierOffset(), cell.getQualifierLength()); | ||
| } | ||
|
|
||
| private void trackColumn(Cell cell) { | ||
| prevFamily = CellUtil.cloneFamily(cell); | ||
| prevQualifier = CellUtil.cloneQualifier(cell); | ||
| } | ||
|
|
||
| // No @Override for HBase 3 compatibility | ||
| public ReturnCode filterKeyValue(Cell v) throws IOException { | ||
| return filterCell(v); | ||
| } | ||
|
|
||
| @Override | ||
| public ReturnCode filterCell(Cell cell) throws IOException { | ||
| if (isNewRow(cell)) { | ||
| onNewRow(cell); | ||
| } | ||
|
|
||
| if (currentTimestamps == null || currentTimestamps.length == 0) { | ||
| return ReturnCode.INCLUDE; | ||
| } | ||
|
|
||
| Cell.Type type = cell.getType(); | ||
| if (type == Cell.Type.DeleteFamily || type == Cell.Type.DeleteFamilyVersion) { | ||
| return ReturnCode.INCLUDE; | ||
| } | ||
|
|
||
| if (isNewColumn(cell)) { | ||
| trackColumn(cell); | ||
| tsIdx = 0; | ||
| needPreImage = false; | ||
| } | ||
|
|
||
| long cellTs = cell.getTimestamp(); | ||
|
|
||
| // Advance past change timestamps that are above this cell's timestamp. | ||
| // These timestamps had no cell for this column, but we still need a pre-image | ||
| // below them (the first cell we encounter serves as pre-image for all of them). | ||
| while (tsIdx < currentTimestamps.length && currentTimestamps[tsIdx] > cellTs) { | ||
| needPreImage = true; | ||
| tsIdx++; | ||
| } | ||
|
|
||
| if (tsIdx < currentTimestamps.length && cellTs == currentTimestamps[tsIdx]) { | ||
| needPreImage = true; | ||
| tsIdx++; | ||
| return ReturnCode.INCLUDE; | ||
| } | ||
|
|
||
| if (needPreImage) { | ||
| needPreImage = false; | ||
| return ReturnCode.INCLUDE; | ||
| } | ||
|
|
||
| if (tsIdx >= currentTimestamps.length) { | ||
| return ReturnCode.NEXT_COL; | ||
| } | ||
|
|
||
| return ReturnCode.SKIP; | ||
| } | ||
|
|
||
| @Override | ||
| public byte[] toByteArray() throws IOException { | ||
| return Writables.getBytes(this); | ||
| } | ||
|
|
||
| public static CDCVersionFilter parseFrom(byte[] pbBytes) throws DeserializationException { | ||
| try { | ||
| return (CDCVersionFilter) Writables.getWritable(pbBytes, new CDCVersionFilter()); | ||
| } catch (IOException e) { | ||
| throw new DeserializationException(e); | ||
| } | ||
| } | ||
|
|
||
| @Override | ||
| public void write(DataOutput out) throws IOException { | ||
| if (timestampMap == null) { | ||
| out.writeInt(0); | ||
| return; | ||
| } | ||
| out.writeInt(timestampMap.size()); | ||
| for (Map.Entry<ImmutableBytesPtr, long[]> entry : timestampMap.entrySet()) { | ||
| ImmutableBytesPtr key = entry.getKey(); | ||
| long[] timestamps = entry.getValue(); | ||
| out.writeInt(key.getLength()); | ||
| out.write(key.get(), key.getOffset(), key.getLength()); | ||
| out.writeInt(timestamps.length); | ||
| for (long ts : timestamps) { | ||
| out.writeLong(ts); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| @Override | ||
| public void readFields(DataInput in) throws IOException { | ||
| int numRows = in.readInt(); | ||
| timestampMap = new HashMap<>(numRows); | ||
| for (int i = 0; i < numRows; i++) { | ||
| int keyLen = in.readInt(); | ||
| byte[] keyBytes = new byte[keyLen]; | ||
| in.readFully(keyBytes); | ||
| int numTs = in.readInt(); | ||
| long[] timestamps = new long[numTs]; | ||
| for (int j = 0; j < numTs; j++) { | ||
| timestamps[j] = in.readLong(); | ||
| } | ||
| timestampMap.put(new ImmutableBytesPtr(keyBytes), timestamps); | ||
| } | ||
| } | ||
| } |
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
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.
Can we avoid building this timestamp map in every task and precompute beforehand? But maybe it is okay since number of tasks will usually be small - based on number of regions involved and number of rowkeys?
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.
It is possible but it is far bigger refactor, worth doing as separate PR because this PR is already too big. However, dataRowKeys are already available so generating map would not be that time consuming.