Skip to content

[Bug] [seatunnel-connectors] [seatunnel-connector-spark-clickhouse] Data cannot be imported to all nodes by configuring split_mode and sharding_key #4772

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
wants to merge 15 commits into
base: dev
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 0 additions & 32 deletions .idea/vcs.xml

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -111,13 +111,14 @@ public Shard getShard(Object shardValue) {
}
int offset =
(int)
(HASH_INSTANCE.hash(
ByteBuffer.wrap(
shardValue
.toString()
.getBytes(StandardCharsets.UTF_8)),
0)
& Long.MAX_VALUE % shardWeightCount);
((HASH_INSTANCE.hash(
ByteBuffer.wrap(
shardValue
.toString()
.getBytes(StandardCharsets.UTF_8)),
0)
& Long.MAX_VALUE)
% shardWeightCount);
return shards.lowerEntry(offset + 1).getValue();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,56 @@
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;

import net.jpountz.xxhash.XXHash64;
import net.jpountz.xxhash.XXHashFactory;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

public class ClickhouseFactoryTest {
private static final XXHash64 HASH_INSTANCE = XXHashFactory.fastestInstance().hash64();

@Test
public void testOptionRule() {
Assertions.assertNotNull((new ClickhouseSourceFactory()).optionRule());
Assertions.assertNotNull((new ClickhouseSinkFactory()).optionRule());
Assertions.assertNotNull((new ClickhouseFileSinkFactory()).optionRule());
}

@Test
public void testShared() {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The test case look weird, it only prove the logic of (hashValue & Long.MAX_VALUE) % modulo are right. But can not make sure the getShard method return right result. I think the test case should test getShard method.

// Create an instance of the XXHash64 algorithm
XXHashFactory factory = XXHashFactory.fastestInstance();
XXHash64 hash64 = factory.hash64();

// Define your input data
byte[] input;
ArrayList<String> strings = new ArrayList<>();

Map<Long, Long> resultCount = new HashMap<>();
for (int i = 1; i <= 1000000; i++) {
input = UUID.randomUUID().toString().getBytes();
// Calculate the hash value
long hashValue = hash64.hash(input, 0, input.length, 0);

// Apply modulo operation to get a non-negative result
int modulo = 10;
long nonNegativeResult = (hashValue & Long.MAX_VALUE) % modulo;
Long keyValue = resultCount.get(nonNegativeResult);

if (keyValue != null) {
resultCount.put(nonNegativeResult, keyValue + 1L);

} else {
resultCount.put(nonNegativeResult, 1L);
}
}
Long totalResult = 0L;
for (Long key : resultCount.keySet()) {
totalResult += resultCount.get(key);
}
Assertions.assertEquals(1000000, totalResult);
}
}