-
Notifications
You must be signed in to change notification settings - Fork 2.1k
[Improve][Connector-v2][MySQL] Optimize shard calculation strategy #9975
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
Merged
+196
−0
Merged
Changes from 2 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
e9f0757
feat(jdbc): add hashModForField method for MySQL dialect
zhangshenghang 28921bb
feat(jdbc): add hashModForField method for MySQL dialect
zhangshenghang 3fed16d
feat(jdbc): add hashModForField method for MySQL dialect
zhangshenghang b6484a4
feat(jdbc): add hashModForField method for MySQL dialect
zhangshenghang 254696e
Merge remote-tracking branch 'upstream/dev' into imporve-mysql-partition
zhangshenghang 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
186 changes: 186 additions & 0 deletions
186
...g/apache/seatunnel/connectors/seatunnel/jdbc/internal/dialect/mysql/MysqlDialectTest.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,186 @@ | ||
| /* | ||
| * 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.seatunnel.connectors.seatunnel.jdbc.internal.dialect.mysql; | ||
|
|
||
| import org.junit.jupiter.api.Assertions; | ||
| import org.junit.jupiter.api.Test; | ||
|
|
||
| import java.security.MessageDigest; | ||
| import java.util.ArrayList; | ||
| import java.util.HashMap; | ||
| import java.util.List; | ||
| import java.util.Map; | ||
| import java.util.zip.CRC32; | ||
|
|
||
| public class MysqlDialectTest { | ||
|
|
||
| @Test | ||
| public void testHashDistributionMD5vsCRC32WithSnowflakeIds() { | ||
| int totalRecords = 1_100_000; | ||
| int partitions = 10; | ||
| List<String> snowflakeIds = generateSnowflakeIds(totalRecords); | ||
|
|
||
| Map<Integer, Integer> md5Distribution = new HashMap<>(); | ||
| for (int i = 0; i < partitions; i++) { | ||
| md5Distribution.put(i, 0); | ||
| } | ||
|
|
||
| for (String id : snowflakeIds) { | ||
| int partition = calculateMD5Partition(id, partitions); | ||
| md5Distribution.put(partition, md5Distribution.get(partition) + 1); | ||
| } | ||
|
|
||
| Map<Integer, Integer> crc32Distribution = new HashMap<>(); | ||
| for (int i = 0; i < partitions; i++) { | ||
| crc32Distribution.put(i, 0); | ||
| } | ||
|
|
||
| for (String id : snowflakeIds) { | ||
| int partition = calculateCRC32Partition(id, partitions); | ||
| crc32Distribution.put(partition, crc32Distribution.get(partition) + 1); | ||
| } | ||
|
|
||
| System.out.println("MD5 Distribution (OLD - Has Issue):"); | ||
| for (int i = 0; i < partitions; i++) { | ||
| int count = md5Distribution.get(i); | ||
| double percentage = (count * 100.0) / totalRecords; | ||
| System.out.printf( | ||
| " Partition %d: %,7d records (%.2f%%)%s\n", | ||
| i, count, percentage, (percentage > 20 ? " SKEWED!" : "")); | ||
| } | ||
|
|
||
| System.out.println("\nCRC32 Distribution (NEW - Fixed):"); | ||
| for (int i = 0; i < partitions; i++) { | ||
| int count = crc32Distribution.get(i); | ||
| double percentage = (count * 100.0) / totalRecords; | ||
| System.out.printf( | ||
| " Partition %d: %,7d records (%.2f%%)%s\n", | ||
| i, count, percentage, (percentage > 20 ? " SKEWED!" : " ✓")); | ||
| } | ||
|
|
||
| // Verify that MD5 is severely skewed | ||
| double md5Partition0Percentage = (md5Distribution.get(0) * 100.0) / totalRecords; | ||
| Assertions.assertTrue(md5Partition0Percentage > 30); | ||
|
|
||
| // Verify that CRC32 is evenly distributed | ||
| for (int i = 0; i < partitions; i++) { | ||
| double crc32Percentage = (crc32Distribution.get(i) * 100.0) / totalRecords; | ||
| Assertions.assertTrue(crc32Percentage >= 7 && crc32Percentage <= 13); | ||
| } | ||
|
|
||
| double md5StdDev = calculateStandardDeviation(md5Distribution, totalRecords, partitions); | ||
| double crc32StdDev = | ||
| calculateStandardDeviation(crc32Distribution, totalRecords, partitions); | ||
|
|
||
| // The standard deviation of CRC32 should be much smaller than MD5 | ||
| Assertions.assertTrue(crc32StdDev < md5StdDev / 2); | ||
| } | ||
|
|
||
| /** Generate Snowflake Algorithm ID */ | ||
| private List<String> generateSnowflakeIds(int count) { | ||
| List<String> ids = new ArrayList<>(count); | ||
| long baseTimestamp = 1704067200000L; | ||
| long timestampBits = baseTimestamp << 22; | ||
|
|
||
| for (int i = 0; i < count; i++) { | ||
| long timeIncrement = (i / 4096) << 22; | ||
| long machineId = (i % 1024) << 12; | ||
| long sequence = i % 4096; | ||
|
|
||
| long snowflakeId = timestampBits + timeIncrement + machineId + sequence; | ||
| ids.add(String.valueOf(snowflakeId)); | ||
| } | ||
|
|
||
| return ids; | ||
| } | ||
|
|
||
| /** Simulate the MD5 behavior of MySQL */ | ||
| private int calculateMD5Partition(String id, int mod) { | ||
| try { | ||
| MessageDigest md = MessageDigest.getInstance("MD5"); | ||
| byte[] digest = md.digest(id.getBytes()); | ||
|
|
||
| StringBuilder hexString = new StringBuilder(); | ||
| for (byte b : digest) { | ||
| String hex = Integer.toHexString(0xff & b); | ||
| if (hex.length() == 1) { | ||
| hexString.append('0'); | ||
| } | ||
| hexString.append(hex); | ||
| } | ||
|
|
||
| String hexResult = hexString.toString(); | ||
| long numericValue = convertHexStringToNumberMySQLWay(hexResult); | ||
|
|
||
| return (int) Math.abs(numericValue % mod); | ||
| } catch (Exception e) { | ||
| throw new RuntimeException(e); | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Simulate MySQL string to number conversion: Read from left to right and stop when the first | ||
| * non numeric character is encountered. | ||
| */ | ||
| private long convertHexStringToNumberMySQLWay(String hexString) { | ||
| if (hexString == null || hexString.isEmpty()) { | ||
| return 0; | ||
| } | ||
|
|
||
| StringBuilder numericPart = new StringBuilder(); | ||
| for (char c : hexString.toCharArray()) { | ||
| if (c >= '0' && c <= '9') { | ||
| numericPart.append(c); | ||
| } else { | ||
| break; | ||
| } | ||
| } | ||
|
|
||
| if (numericPart.length() == 0) { | ||
| return 0; | ||
| } | ||
|
|
||
| try { | ||
| return Long.parseLong(numericPart.toString()); | ||
| } catch (NumberFormatException e) { | ||
| return 0; | ||
| } | ||
| } | ||
|
|
||
| /** Simulate CRC32 behavior */ | ||
| private int calculateCRC32Partition(String id, int mod) { | ||
| CRC32 crc32 = new CRC32(); | ||
| crc32.update(id.getBytes()); | ||
| long crcValue = crc32.getValue(); | ||
|
|
||
| return (int) Math.abs(crcValue % mod); | ||
| } | ||
|
|
||
| private double calculateStandardDeviation( | ||
| Map<Integer, Integer> distribution, int totalRecords, int partitions) { | ||
| double mean = totalRecords / (double) partitions; | ||
| double sumSquaredDiff = 0; | ||
|
|
||
| for (int i = 0; i < partitions; i++) { | ||
| double diff = distribution.get(i) - mean; | ||
| sumSquaredDiff += diff * diff; | ||
| } | ||
|
|
||
| return Math.sqrt(sumSquaredDiff / partitions); | ||
| } | ||
| } | ||
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.
Uh oh!
There was an error while loading. Please reload this page.