-
Notifications
You must be signed in to change notification settings - Fork 0
doc(crud-cluster): add live examples #36
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
Merged
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
80 changes: 80 additions & 0 deletions
80
documentation/doc-src/java/src/client/TarantoolDBClusterConnectionAbstractExample.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,80 @@ | ||
| /* | ||
| * Copyright (c) 2025 VK DIGITAL TECHNOLOGIES LIMITED LIABILITY COMPANY | ||
| * All Rights Reserved. | ||
| */ | ||
|
|
||
| package client; | ||
|
|
||
| // --8<-- [start:all] | ||
|
|
||
| import java.net.InetSocketAddress; | ||
| import java.util.List; | ||
| import java.util.Map; | ||
|
|
||
| import org.junit.jupiter.api.AfterAll; | ||
| import org.junit.jupiter.api.BeforeAll; | ||
| import org.testcontainers.containers.tarantool.TarantoolContainer; | ||
| import org.testcontainers.containers.tarantool.config.ConfigurationUtils; | ||
| import org.testcontainers.containers.tdb.TDB2ClusterImpl; | ||
| import org.testcontainers.containers.tdb.TDBCluster; | ||
|
|
||
| import io.tarantool.autogen.Tarantool3Configuration; | ||
| import io.tarantool.autogen.credentials.users.usersProperty.UsersProperty; | ||
|
|
||
| public abstract class TarantoolDBClusterConnectionAbstractExample { | ||
|
|
||
| private static final List<String> ROLES = List.of("super"); | ||
|
|
||
| protected static final String TARANTOOL_DB_IMAGE = | ||
| System.getenv().getOrDefault("TARANTOOL_REGISTRY", "") + "tarantooldb:2.2.1"; | ||
|
|
||
| protected static final String SELLER_USER = "seller-user"; | ||
| protected static final String SELLER_USER_PWD = "pwd-1"; | ||
|
|
||
| protected static final String USER_1182 = "user-1182"; | ||
| protected static final String USER_1182_PWD = "pwd-2"; | ||
|
|
||
| protected static final String FIRST_ROUTER_CONTAINER_NAME = "router-1"; | ||
| protected static final String SECOND_ROUTER_CONTAINER_NAME = "router-2"; | ||
|
|
||
| protected static TDBCluster CLUSTER; | ||
|
|
||
| protected abstract void simpleCrudConnection(); | ||
|
|
||
| @BeforeAll | ||
| static void beforeAll() { | ||
| CLUSTER = createTDBCluster(); | ||
| CLUSTER.start(); | ||
| } | ||
|
|
||
| @AfterAll | ||
| static void afterAll() { | ||
| CLUSTER.close(); | ||
| } | ||
|
|
||
| @SuppressWarnings("unchecked") | ||
| private static TDBCluster createTDBCluster() { | ||
|
|
||
| // Adds custom users to emulate documentation example | ||
| Map<String, UsersProperty> users = | ||
| Map.of( | ||
| SELLER_USER, | ||
| UsersProperty.builder().withPassword(SELLER_USER_PWD).withRoles(ROLES).build(), | ||
| USER_1182, | ||
| UsersProperty.builder().withPassword(USER_1182_PWD).withRoles(ROLES).build()); | ||
|
|
||
| final Tarantool3Configuration commonConfigWithoutCustomUsers = | ||
| ConfigurationUtils.generateSimpleConfiguration(2, 3, 2); | ||
| final Tarantool3Configuration config = | ||
| ConfigurationUtils.addUsers(commonConfigWithoutCustomUsers, users); | ||
|
|
||
| return TDB2ClusterImpl.builder(TARANTOOL_DB_IMAGE).withTDB2Configuration(config).build(); | ||
| } | ||
|
|
||
| protected static InetSocketAddress getRouterAddress(String routerName) { | ||
| final Map<String, TarantoolContainer<?>> routers = CLUSTER.routers(); | ||
| return routers.get(routerName).mappedAddress(); | ||
| } | ||
| } | ||
|
|
||
| // --8<-- [end:all] |
64 changes: 64 additions & 0 deletions
64
...mentation/doc-src/java/src/client/TarantoolDBClusterConnectionCartridgeDriverExample.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,64 @@ | ||
| /* | ||
| * Copyright (c) 2025 VK DIGITAL TECHNOLOGIES LIMITED LIABILITY COMPANY | ||
| * All Rights Reserved. | ||
| */ | ||
|
|
||
| package client; | ||
|
|
||
| // --8<-- [start:all] | ||
|
|
||
| import java.net.InetSocketAddress; | ||
| import java.util.List; | ||
|
|
||
| import org.junit.jupiter.api.Assertions; | ||
| import org.junit.jupiter.api.Test; | ||
|
|
||
| import io.tarantool.driver.api.TarantoolClient; | ||
| import io.tarantool.driver.api.TarantoolClientFactory; | ||
| import io.tarantool.driver.api.TarantoolResult; | ||
| import io.tarantool.driver.api.TarantoolServerAddress; | ||
| import io.tarantool.driver.api.tuple.TarantoolTuple; | ||
|
|
||
| public class TarantoolDBClusterConnectionCartridgeDriverExample | ||
| extends TarantoolDBClusterConnectionAbstractExample { | ||
|
|
||
| @Test | ||
| @Override | ||
| protected void simpleCrudConnection() { | ||
| try (TarantoolClient<TarantoolTuple, TarantoolResult<TarantoolTuple>> crudClient = | ||
| setupClient()) { | ||
| final String helloWorld = "hello world"; | ||
|
|
||
| // Evals return instruction in Tarantool lua | ||
| final List<?> helloResponse = | ||
| crudClient.eval(String.format("return '%s'", helloWorld)).join(); | ||
| Assertions.assertEquals(1, helloResponse.size()); | ||
| Assertions.assertEquals(helloWorld, helloResponse.get(0)); | ||
| } catch (Exception e) { | ||
| throw new RuntimeException(e); | ||
| } | ||
| } | ||
|
|
||
| private static TarantoolClient<TarantoolTuple, TarantoolResult<TarantoolTuple>> setupClient() { | ||
|
|
||
| // Returns routers addresses mapped from docker | ||
| final InetSocketAddress firstRouterAddress = getRouterAddress(FIRST_ROUTER_CONTAINER_NAME); | ||
| final InetSocketAddress secondRouterAddress = getRouterAddress(SECOND_ROUTER_CONTAINER_NAME); | ||
|
|
||
| // Create crud client instance and connect to routers | ||
| return TarantoolClientFactory.createClient() | ||
| .withAddresses( | ||
| new TarantoolServerAddress( | ||
| firstRouterAddress.getHostName(), firstRouterAddress.getPort()), | ||
| new TarantoolServerAddress( | ||
| secondRouterAddress.getHostName(), secondRouterAddress.getPort())) | ||
|
|
||
| // Two connection groups with different users in one client instance | ||
| .withCredentials(USER_1182, USER_1182_PWD) | ||
| .withConnections(2) | ||
| .withProxyMethodMapping() | ||
| .build(); | ||
| } | ||
| } | ||
|
|
||
| // --8<-- [end:all] |
81 changes: 81 additions & 0 deletions
81
documentation/doc-src/java/src/client/TarantoolDBClusterConnectionTJSDKExample.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,81 @@ | ||
| /* | ||
| * Copyright (c) 2025 VK DIGITAL TECHNOLOGIES LIMITED LIABILITY COMPANY | ||
| * All Rights Reserved. | ||
| */ | ||
|
|
||
| package client; | ||
|
|
||
| // --8<-- [start:all] | ||
|
|
||
| import java.net.InetSocketAddress; | ||
| import java.util.Arrays; | ||
| import java.util.List; | ||
|
|
||
| import org.junit.jupiter.api.Assertions; | ||
| import org.junit.jupiter.api.Test; | ||
|
|
||
| import io.tarantool.client.crud.TarantoolCrudClient; | ||
| import io.tarantool.client.factory.TarantoolCrudClientBuilder; | ||
| import io.tarantool.client.factory.TarantoolFactory; | ||
| import io.tarantool.pool.InstanceConnectionGroup; | ||
|
|
||
| public class TarantoolDBClusterConnectionTJSDKExample | ||
| extends TarantoolDBClusterConnectionAbstractExample { | ||
|
|
||
| @Test | ||
| @Override | ||
| protected void simpleCrudConnection() { | ||
|
|
||
| try (final TarantoolCrudClient crudClient = setupClient()) { | ||
| final String helloWorld = "hello world"; | ||
|
|
||
| // Evals return instruction in Tarantool lua | ||
| final List<String> helloResponse = | ||
| crudClient.eval(String.format("return '%s'", helloWorld), String.class).join().get(); | ||
| Assertions.assertEquals(1, helloResponse.size()); | ||
| Assertions.assertEquals(helloWorld, helloResponse.get(0)); | ||
| } catch (Exception e) { | ||
| throw new RuntimeException(e); | ||
| } | ||
| } | ||
|
|
||
| private static TarantoolCrudClient setupClient() throws Exception { | ||
|
|
||
| // Returns routers addresses mapped from docker | ||
| final InetSocketAddress firstRouterAddress = getRouterAddress(FIRST_ROUTER_CONTAINER_NAME); | ||
| final InetSocketAddress secondRouterAddress = getRouterAddress(SECOND_ROUTER_CONTAINER_NAME); | ||
|
|
||
| final TarantoolCrudClientBuilder crudClientBuilder = TarantoolFactory.crud(); | ||
|
|
||
| // Setup first connection group with "seller-user" user and 2 connection to first router | ||
| final InstanceConnectionGroup firstRouterConnectionGroup = | ||
| InstanceConnectionGroup.builder() | ||
| .withHost(firstRouterAddress.getHostName()) | ||
| .withPort(firstRouterAddress.getPort()) | ||
| .withUser(SELLER_USER) | ||
| .withPassword(SELLER_USER_PWD) | ||
| .withSize(2) | ||
| .withTag(SELLER_USER + "-connection") | ||
| .build(); | ||
|
|
||
| // Setup second connection group with "user-1182" user and 3 connection to first router | ||
| final InstanceConnectionGroup secondRouterConnectionGroup = | ||
| InstanceConnectionGroup.builder() | ||
| .withHost(secondRouterAddress.getHostName()) | ||
| .withPort(secondRouterAddress.getPort()) | ||
| .withUser(USER_1182) | ||
| .withPassword(USER_1182_PWD) | ||
| .withSize(3) | ||
| .withTag(USER_1182 + "-connection") | ||
| .build(); | ||
|
|
||
| final List<InstanceConnectionGroup> connectionGroupsList = | ||
| Arrays.asList(firstRouterConnectionGroup, secondRouterConnectionGroup); | ||
|
|
||
| // Create crud client instance and connect to routers | ||
| // Two connection groups with different users in one client instance | ||
| return crudClientBuilder.withGroups(connectionGroupsList).build(); | ||
| } | ||
| } | ||
|
|
||
| // --8<-- [end:all] |
26 changes: 26 additions & 0 deletions
26
documentation/doc-src/pages/client/examples/connection/crud-cluster.en.md
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,26 @@ | ||
| --- | ||
| title: Connecting to Tarantool cluster via crud | ||
| --- | ||
|
|
||
| The following example demonstrates connecting to a `Tarantool` cluster via routers with | ||
| using the `crud` module: | ||
|
|
||
| === "tarantool-java-sdk" | ||
|
|
||
| ```title="Подключенние к кластеру при помощи TJSDK" | ||
| --8<-- "client/TarantoolDBClusterConnectionTJSDKExample.java:all" | ||
| ``` | ||
|
|
||
| ```title="Абстрактный класс для создания кластера в docker" | ||
| --8<-- "client/TarantoolDBClusterConnectionAbstractExample.java:all" | ||
| ``` | ||
|
|
||
| === "cartridge-driver" | ||
|
|
||
| ```title="Подключенние к кластеру при помощи Cartridge java" | ||
| --8<-- "client/TarantoolDBClusterConnectionCartridgeDriverExample.java:all" | ||
| ``` | ||
|
|
||
| ```title="Абстрактный класс для создания кластера в docker" | ||
| --8<-- "client/TarantoolDBClusterConnectionAbstractExample.java:all" | ||
| ``` |
26 changes: 26 additions & 0 deletions
26
documentation/doc-src/pages/client/examples/connection/crud-cluster.md
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,26 @@ | ||
| --- | ||
| title: Подключение к класетру Tarantool через crud | ||
| --- | ||
|
|
||
| Следующий пример демонстрирует подключение к кластеру `Tarantool` через маршрутизаторы с | ||
| использованием модуля `crud`: | ||
|
|
||
| === "tarantool-java-sdk" | ||
|
|
||
| ```title="Подключенние к кластеру при помощи TJSDK" | ||
| --8<-- "client/TarantoolDBClusterConnectionTJSDKExample.java:all" | ||
| ``` | ||
|
|
||
| ```title="Абстрактный класс для создания кластера в docker" | ||
| --8<-- "client/TarantoolDBClusterConnectionAbstractExample.java:all" | ||
| ``` | ||
|
|
||
| === "cartridge-driver" | ||
|
|
||
| ```title="Подключенние к кластеру при помощи Cartridge java" | ||
| --8<-- "client/TarantoolDBClusterConnectionCartridgeDriverExample.java:all" | ||
| ``` | ||
|
|
||
| ```title="Абстрактный класс для создания кластера в docker" | ||
| --8<-- "client/TarantoolDBClusterConnectionAbstractExample.java:all" | ||
| ``` |
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
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.
Нужны тесты на этот метод
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.
Сделано