-
Notifications
You must be signed in to change notification settings - Fork 13
Add example how to calculate bucketId on client #274
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
+197
−11
Merged
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
d4d45b8
Add tests how to calculate bucketId on client
ArtDu 59b635f
Fixes after review
ArtDu cfd048e
Update README.md
ArtDu 8f9901e
Update README.md
ArtDu 037805f
Update README.md
ArtDu 2f587d4
Update README.md
ArtDu cef3514
Update README.md
ArtDu d77ccea
Update README.md
ArtDu 0a1db62
Update README.md
ArtDu 6e7b3de
Fixes after review
ArtDu 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -244,6 +244,60 @@ class Scratch { | |
| } | ||
| ``` | ||
|
|
||
| #### Using custom sharding function | ||
|
|
||
| A custom sharding function can be used to determine the bucket number - location in the cluster - and used further in the cluster operations. | ||
| For this purpose you need: | ||
| 1) a hash function | ||
| As an example, a default function from tarantool/vshard - [crc32](https://www.tarantool.io/en/doc/latest/reference/reference_lua/digest/#lua-function.digest.crc32) with specific polynomial value. | ||
| Java doesn't have crc32 out of the box with the ability to pass a polynomial value, so we'll implement our own: | ||
| ```java | ||
| private static long crc32(byte[] data) { | ||
| BitSet bitSet = BitSet.valueOf(data); | ||
| int crc32 = 0xFFFFFFFF; // initial value | ||
| for (int i = 0; i < data.length * 8; i++) { | ||
| if (((crc32 >>> 31) & 1) != (bitSet.get(i) ? 1 : 0)) { | ||
| crc32 = (crc32 << 1) ^ 0x1EDC6F41; // xor with polynomial | ||
| } else { | ||
| crc32 = crc32 << 1; | ||
| } | ||
| } | ||
| crc32 = Integer.reverse(crc32); // result reflect | ||
| return crc32 & 0x00000000ffffffffL; // the unsigned java problem | ||
| } | ||
| ``` | ||
| 2) the number of buckets | ||
| This number can be obtained from Tarantool via `vshard.router.bucket_count` function out of [vshard module](https://github.com/tarantool/vshard) | ||
| ```java | ||
| public static <T extends Packable, R extends Collection<T>> Integer getBucketCount( | ||
| TarantoolClient<T, R> client) throws ExecutionException, InterruptedException { | ||
| if (!bucketCount.isPresent()) { | ||
| bucketCount = Optional.ofNullable( | ||
| client.callForSingleResult("vshard.router.bucket_count", Integer.class).get() | ||
| ); | ||
| } | ||
| bucketCount.orElseThrow(() -> new TarantoolClientException("Failed to get bucket count")); | ||
| } | ||
| ``` | ||
|
|
||
| Then we can determine bucket id by passing your key through hash function and get the remainder of the division by number of buckets: | ||
| ```java | ||
| TarantoolTuple tarantoolTuple = tupleFactory.create(1, null, "FIO", 50, 100); | ||
| byte[] key = getBytesFromList(Arrays.asList(tarantoolTuple.getInteger(0), tarantoolTuple.getInteger(2))); | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It may be good to place a link to this |
||
| Integer bucketId = (crc32(key) % getBucketCount(client)) + 1; | ||
| ``` | ||
|
|
||
| After that we may apply it in operations: | ||
| ```java | ||
| InsertOptions insertOptions = ProxyInsertOptions.create().withBucketId(bucketId); | ||
| insertResult = profileSpace.insert(tarantoolTuple, insertOptions).get(); | ||
|
|
||
| ProxySelectOptions selectOptions = ProxySelectOptions.create().withBucketId(bucketId); | ||
| selectResult = profileSpace.select(condition, selectOptions).get(); | ||
| ``` | ||
|
|
||
| You can see the sources of this example in the [tests](src/test/java/io/tarantool/driver/integration/proxy/options/ProxySpaceInsertOptionsIT.java) | ||
|
|
||
| ### Retrying Tarantool client | ||
|
|
||
| For the cases of reliable communication with a Cartridge cluster under heavy load or in a case of some failure causing | ||
|
|
||
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,30 +1,108 @@ | ||
| package io.tarantool.driver.integration; | ||
|
|
||
| import io.tarantool.driver.api.TarantoolClient; | ||
| import io.tarantool.driver.api.TarantoolResult; | ||
| import io.tarantool.driver.api.conditions.Conditions; | ||
| import io.tarantool.driver.api.space.TarantoolSpaceOperations; | ||
| import io.tarantool.driver.api.tuple.TarantoolTuple; | ||
| import io.tarantool.driver.exceptions.TarantoolClientException; | ||
| import io.tarantool.driver.exceptions.TarantoolException; | ||
| import io.tarantool.driver.exceptions.TarantoolSpaceFieldNotFoundException; | ||
| import io.tarantool.driver.protocol.Packable; | ||
|
|
||
| import java.io.ByteArrayOutputStream; | ||
| import java.io.IOException; | ||
| import java.util.BitSet; | ||
| import java.util.Collection; | ||
| import java.util.List; | ||
| import java.util.Optional; | ||
| import java.util.concurrent.ExecutionException; | ||
|
|
||
| import static org.junit.jupiter.api.Assertions.assertEquals; | ||
| import static org.junit.jupiter.api.Assertions.fail; | ||
|
|
||
| /** | ||
| * @author Ivan Dneprov | ||
| * @author Artyom Dubinin | ||
| */ | ||
| public final class Utils { | ||
| private static Optional<Integer> bucketCount = Optional.empty(); | ||
|
|
||
| private Utils() { | ||
| } | ||
|
|
||
| /** | ||
| * Checks if the space is empty. | ||
| * | ||
| * @param testSpace space to check | ||
| */ | ||
| static void checkSpaceIsEmpty(TarantoolSpaceOperations<TarantoolTuple, | ||
| TarantoolResult<TarantoolTuple>> testSpace) { | ||
| * Checks if the space is empty. | ||
| * | ||
| * @param testSpace space to check | ||
| */ | ||
| static void checkSpaceIsEmpty(TarantoolSpaceOperations<TarantoolTuple, TarantoolResult<TarantoolTuple>> testSpace) { | ||
| assertEquals(0, testSpace.select(Conditions.any()).thenApply(List::size).join()); | ||
| } | ||
|
|
||
| /** | ||
| * Get number of buckets in vshard cluster. | ||
| * | ||
| * @param client Tarantool client for with access to vshard router | ||
| * @param <T> target tuple type | ||
| * @param <R> target tuple collection type | ||
| * @return number of buckets | ||
| */ | ||
| public static <T extends Packable, R extends Collection<T>> Integer getBucketCount( | ||
| TarantoolClient<T, R> client) throws ExecutionException, InterruptedException { | ||
| if (!bucketCount.isPresent()) { | ||
| bucketCount = Optional.ofNullable( | ||
| client.callForSingleResult("vshard.router.bucket_count", Integer.class).get() | ||
| ); | ||
| } | ||
| return bucketCount.orElseThrow(() -> new TarantoolClientException("Failed to get bucket count")); | ||
| } | ||
|
|
||
| /** | ||
| * Get bucket_id via crc32 hash function. | ||
| * You can't use null, because null is packed to box.NULL((void *) 0) and java doesn't have equivalent. | ||
| * | ||
| * @param client Tarantool client for with access to vshard router | ||
| * @param key key that will be used to calculate bucketId | ||
| * @param <T> target tuple type | ||
| * @param <R> target tuple collection type | ||
| * @return bucketId number determining the location in the cluster | ||
| */ | ||
| public static <T extends Packable, R extends Collection<T>> Integer getBucketIdStrCRC32( | ||
| TarantoolClient<T, R> client, List<Object> key) throws ExecutionException, InterruptedException { | ||
| ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); | ||
| for (Object part : key) { | ||
| try { | ||
| if (part != null) { | ||
| outputStream.write(part.toString().getBytes()); | ||
| } | ||
| } catch (IOException e) { | ||
| throw new RuntimeException(e); | ||
| } | ||
| } | ||
| return Math.toIntExact( | ||
| (crc32(outputStream.toByteArray()) % getBucketCount(client)) + 1 | ||
| ); | ||
| } | ||
|
|
||
| /** | ||
| * Implementation of crc32 partially was taken from | ||
| * <a href="https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/others/CRC32.java"> | ||
| * github.com/TheAlgorithms</a> | ||
| * | ||
| * @param data input bytes array | ||
| * @return hash response in decimal view | ||
| */ | ||
| private static long crc32(byte[] data) { | ||
ArtDu marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| BitSet bitSet = BitSet.valueOf(data); | ||
| int crc32 = 0xFFFFFFFF; // initial value | ||
| for (int i = 0; i < data.length * 8; i++) { | ||
| if (((crc32 >>> 31) & 1) != (bitSet.get(i) ? 1 : 0)) { | ||
| crc32 = (crc32 << 1) ^ 0x1EDC6F41; // xor with polynomial | ||
| } else { | ||
| crc32 = crc32 << 1; | ||
| } | ||
| } | ||
| crc32 = Integer.reverse(crc32); // result reflect | ||
| return crc32 & 0x00000000ffffffffL; // the unsigned java problem | ||
| } | ||
| } | ||
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
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.
returnis missing