-
Notifications
You must be signed in to change notification settings - Fork 34
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
DBZ-8163 Add inherit epoch feature #208
Merged
Merged
Changes from all commits
Commits
Show all changes
4 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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
95 changes: 95 additions & 0 deletions
95
src/main/java/io/debezium/connector/vitess/pipeline/txmetadata/ShardLineage.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
/* | ||
* Copyright Debezium Authors. | ||
* | ||
* Licensed under the Apache Software License version 2.0, available at http://www.apache.org/licenses/LICENSE-2.0 | ||
*/ | ||
|
||
package io.debezium.connector.vitess.pipeline.txmetadata; | ||
|
||
import java.util.Map; | ||
|
||
/** | ||
* Class used to determine which parents a shard range descended from. Used to set the epoch to the succeediing | ||
* epoch of its parents. | ||
*/ | ||
public class ShardLineage { | ||
|
||
/** | ||
* Return the epoch value of the shard, based on its parents epochs. | ||
* If there are parents present, return the max of the parent epochs plus one. | ||
* If there are no parents present, it returns zero. | ||
* | ||
* @param shardString The descendant shard to find parents of | ||
* @param shardEpochMap The map to search for parents | ||
* @return The epoch value of the descendant shard | ||
*/ | ||
public static Long getInheritedEpoch(String shardString, ShardEpochMap shardEpochMap) { | ||
Shard shard = new Shard(shardString); | ||
|
||
Long maxParentEpoch = -1L; | ||
for (Map.Entry<String, Long> shardEpoch : shardEpochMap.getMap().entrySet()) { | ||
String currentShardString = shardEpoch.getKey(); | ||
Long currentEpoch = shardEpoch.getValue(); | ||
Shard currentShard = new Shard(currentShardString); | ||
if (shard.overlaps(currentShard)) { | ||
maxParentEpoch = Math.max(maxParentEpoch, currentEpoch); | ||
} | ||
} | ||
|
||
return maxParentEpoch + 1; | ||
} | ||
|
||
private static class Shard { | ||
|
||
// A string of a single char that is lexicographically less than all other chars | ||
public static final String NEGATIVE_INFINITY = String.valueOf(Character.MIN_VALUE); | ||
// A string of a single char that is lexicographically greater than all other chars | ||
public static final String POSITIVE_INFINITY = String.valueOf(Character.MAX_VALUE); | ||
|
||
private final String lowerBound; | ||
private final String upperBound; | ||
|
||
Shard(String shard) { | ||
String[] shardInterval = getShardInterval(shard.toLowerCase()); | ||
this.lowerBound = getLowerBound(shardInterval); | ||
this.upperBound = getUpperBound(shardInterval); | ||
validateBounds(); | ||
} | ||
|
||
private void validateBounds() { | ||
if (this.lowerBound.compareTo(this.upperBound) >= 0) { | ||
throw new IllegalArgumentException("Invalid shard range " + this); | ||
} | ||
} | ||
|
||
public boolean overlaps(Shard shard) { | ||
return this.lowerBound.compareTo(shard.upperBound) < 0 && this.upperBound.compareTo(shard.lowerBound) > 0; | ||
} | ||
|
||
private static String getLowerBound(String[] shardInterval) { | ||
if (shardInterval.length < 1 || shardInterval[0].isEmpty()) { | ||
return NEGATIVE_INFINITY; | ||
} | ||
return shardInterval[0]; | ||
} | ||
|
||
private static String getUpperBound(String[] shardInterval) { | ||
if (shardInterval.length != 2 || shardInterval[1].isEmpty()) { | ||
return POSITIVE_INFINITY; | ||
} | ||
return shardInterval[1]; | ||
} | ||
|
||
private static String[] getShardInterval(String shard) { | ||
return shard.split("-"); | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
return "Shard{" + | ||
"lowerBound=" + lowerBound + | ||
", upperBound=" + upperBound + | ||
"}"; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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.
How about the situation when the two ranges meet on the boundary ( == 0 case)?
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.
Vitess ranges are have inclusive lower bounds and exclusive upper bounds so this should be correct (docs). We can think about each part:
this.lowerBound.compareTo(shard.upperBound) == 0
the lower bound is the same as the upper bound we are comparing to. Since the upper bound is exclusive, this interval does not overlap.this.upperBound.compareTo(shard.lowerBound) == 0
the upper bound is the same as the lower bound we are comparing to. Since the upper bound is exclusive, these two intervals do not overlap.I added a test to demonstrate this case.