Skip to content

Commit e8aa289

Browse files
committed
Merge branch 'master' into fix/326
2 parents 317108c + 3717bb0 commit e8aa289

6 files changed

Lines changed: 143 additions & 6 deletions

File tree

README.md

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,11 @@ If you use this code in another project don't forget to add `cartridge-driver` d
4343
</dependency>
4444
```
4545
## Advanced usage
46-
* [Proxy methods](docs/ProxyTarantoolClient.md)
47-
Connecting to routers with crud library. It proxies space interface's methods to crud methods.
46+
* [Using CRUD stored procedures](docs/ProxyTarantoolClient.md)
47+
Connecting to routers with [tarantool/crud](https://github.com/tarantool/crud) library.
48+
It proxies space interface's methods to CRUD methods.
49+
* [Connecting to multiple instances](docs/MultiInstanceConnecting.md)
50+
Connecting to multiple Tarantool instances.
4851
* [Retrying](docs/RetryingTarantoolClient.md)
4952
Retrying transient cluster failures.
5053
* [TarantoolTuple usage](docs/TarantoolTupleUsage.md)

docs/MultiInstanceConnecting.md

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
[Main page](../README.md)
2+
3+
# Connecting to multiple Tarantool instances
4+
5+
This section shows examples of how to connect to multiple [Cartridge](https://github.com/tarantool/cartridge) routers.
6+
We recommend to use balancer in production applications rather than leaving this task to the connector.
7+
8+
## Setting addresses manually
9+
10+
Example of TarantoolClient set up with connecting to multiple routers
11+
https://github.com/tarantool/cartridge-java/blob/09ec7084780c66e5457a8cd9e508376f40a266b7/src/test/java/io/tarantool/driver/integration/ReconnectIT.java#L291-L301
12+
https://github.com/tarantool/cartridge-java/blob/09ec7084780c66e5457a8cd9e508376f40a266b7/src/test/java/io/tarantool/driver/integration/ReconnectIT.java#L315-L321
13+
14+
## Setting addresses with address provider
15+
16+
Example of TarantoolClient set up with address provider
17+
You may use the provided code as a reference for your own implementation
18+
https://github.com/tarantool/cartridge-java/blob/09ec7084780c66e5457a8cd9e508376f40a266b7/src/test/java/io/tarantool/driver/integration/ProxyTarantoolClientWithAddressProviderExampleIT.java#L75-L82
19+
Example of TarantoolClusterAddressProvider setup
20+
https://github.com/tarantool/cartridge-java/blob/09ec7084780c66e5457a8cd9e508376f40a266b7/src/test/java/io/tarantool/driver/integration/ProxyTarantoolClientWithAddressProviderExampleIT.java#L43-L68
21+
Do not forget to set tarantool function witch will return addresses
22+
The function must be global, so that it can be called from the connector
23+
https://github.com/tarantool/cartridge-java/blob/09ec7084780c66e5457a8cd9e508376f40a266b7/src/test/resources/cartridge/app/roles/custom.lua#L3-L30

docs/ProxyTarantoolClient.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[Main page](../README.md)
22

3-
# Proxy Tarantool client
3+
# Using CRUD stored procedures
44

55
This section shows the necessary settings for connecting to instances with a user-defined CRUD API exposed as Lua
66
stored functions or Cartridge roles implementing the API similar to the one of [tarantool/crud](https://github.com/tarantool/crud).
Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
package io.tarantool.driver.integration;
2+
3+
import io.tarantool.driver.api.TarantoolClientFactory;
4+
import io.tarantool.driver.api.TarantoolServerAddress;
5+
import io.tarantool.driver.api.TarantoolClient;
6+
import io.tarantool.driver.api.TarantoolClientConfig;
7+
import io.tarantool.driver.api.TarantoolClusterAddressProvider;
8+
import io.tarantool.driver.api.TarantoolResult;
9+
import io.tarantool.driver.api.space.TarantoolSpaceOperations;
10+
import io.tarantool.driver.api.tuple.DefaultTarantoolTupleFactory;
11+
import io.tarantool.driver.api.tuple.TarantoolTuple;
12+
import io.tarantool.driver.api.tuple.TarantoolTupleFactory;
13+
import io.tarantool.driver.auth.SimpleTarantoolCredentials;
14+
import io.tarantool.driver.auth.TarantoolCredentials;
15+
import io.tarantool.driver.cluster.BinaryClusterDiscoveryEndpoint;
16+
import io.tarantool.driver.cluster.BinaryDiscoveryClusterAddressProvider;
17+
import io.tarantool.driver.cluster.TarantoolClusterDiscoveryConfig;
18+
import io.tarantool.driver.cluster.TestWrappedClusterAddressProvider;
19+
import org.junit.jupiter.api.BeforeAll;
20+
import org.junit.jupiter.api.Test;
21+
22+
import java.util.Collections;
23+
import java.util.concurrent.ExecutionException;
24+
25+
import static org.junit.jupiter.api.Assertions.assertEquals;
26+
import static org.junit.jupiter.api.Assertions.assertNotNull;
27+
28+
public class ProxyTarantoolClientWithAddressProviderExampleIT extends SharedCartridgeContainer {
29+
30+
private static TarantoolClient<TarantoolTuple, TarantoolResult<TarantoolTuple>> client;
31+
32+
public static TarantoolCredentials credentials;
33+
34+
private static final String SPACE_NAME = "test__profile";
35+
36+
@BeforeAll
37+
public static void setUp() throws Exception {
38+
startCluster();
39+
initClient();
40+
truncateSpace(SPACE_NAME);
41+
}
42+
43+
private static TarantoolClusterAddressProvider getClusterAddressProvider() {
44+
TarantoolClientConfig config = TarantoolClientConfig.builder()
45+
.withCredentials(credentials)
46+
.build();
47+
48+
BinaryClusterDiscoveryEndpoint endpoint = new BinaryClusterDiscoveryEndpoint.Builder()
49+
// Config for connecting to instance with entry function
50+
.withClientConfig(config)
51+
// Name of a function that returns a pool of addresses to connect to
52+
.withEntryFunction("get_routers")
53+
// Setting first router URI as entry point
54+
.withEndpointProvider(() -> Collections.singletonList(
55+
new TarantoolServerAddress(container.getRouterHost(), container.getRouterPort())))
56+
.build();
57+
58+
TarantoolClusterDiscoveryConfig clusterDiscoveryConfig = new TarantoolClusterDiscoveryConfig.Builder()
59+
.withEndpoint(endpoint)
60+
.withDelay(1)
61+
.build();
62+
63+
// TestWrappedClusterAddressProvider changes ports provided by address providers to docker mapped ports
64+
// You need to use TestWrappedClusterAddressProvider only if you run Tarantool in TestContainers
65+
return new TestWrappedClusterAddressProvider(
66+
new BinaryDiscoveryClusterAddressProvider(clusterDiscoveryConfig),
67+
container);
68+
}
69+
70+
public static void initClient() {
71+
// For connecting to a Cartridge application, use the value of cluster_cookie parameter in the init.lua file
72+
credentials = new SimpleTarantoolCredentials(container.getUsername(),
73+
container.getPassword());
74+
75+
client = TarantoolClientFactory.createClient()
76+
// You don't have to set the routers addresses yourself address provider will do it for you
77+
.withAddressProvider(getClusterAddressProvider())
78+
// For connecting to a Cartridge application, use the value of cluster_cookie parameter in the init.lua file
79+
.withCredentials(credentials)
80+
// Specify using the default CRUD proxy operations mapping configuration
81+
.withProxyMethodMapping()
82+
.build();
83+
}
84+
85+
private static void truncateSpace(String spaceName) {
86+
client.space(spaceName).truncate().join();
87+
}
88+
89+
@Test
90+
public void insertOneTuple() throws ExecutionException, InterruptedException {
91+
TarantoolSpaceOperations<TarantoolTuple, TarantoolResult<TarantoolTuple>> space = client.space(SPACE_NAME);
92+
93+
// Use TarantoolTupleFactory for instantiating new tuples
94+
TarantoolTupleFactory tupleFactory = new DefaultTarantoolTupleFactory(
95+
client.getConfig().getMessagePackMapper());
96+
97+
TarantoolResult<TarantoolTuple> insertTuples = space.insert(tupleFactory.create(1, null, "FIO", 50, 100)).get();
98+
99+
assertEquals(insertTuples.size(), 1);
100+
TarantoolTuple tuple = insertTuples.get(0);
101+
assertEquals(tuple.size(), 5);
102+
assertEquals(tuple.getInteger(0), 1);
103+
assertNotNull(tuple.getInteger(1)); //bucket_id
104+
assertEquals(tuple.getString(2), "FIO");
105+
assertEquals(tuple.getInteger(3), 50);
106+
assertEquals(tuple.getInteger(4), 100);
107+
}
108+
}

src/test/java/io/tarantool/driver/integration/ReconnectIT.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -289,9 +289,14 @@ private TarantoolClient<TarantoolTuple, TarantoolResult<TarantoolTuple>> initCli
289289
private void initRouterStatuses() {
290290
final TarantoolClient<TarantoolTuple, TarantoolResult<TarantoolTuple>> initClient =
291291
TarantoolClientFactory.createClient()
292+
// You can connect to multiple routers
292293
.withAddresses(getTarantoolServerAddresses())
294+
// For connecting to a Cartridge application,
295+
// use the value of cluster_cookie parameter in the init.lua file
293296
.withCredentials(USER_NAME, PASSWORD)
297+
// Number of connections per Tarantool instance
294298
.withConnections(1)
299+
// Specify using the default CRUD proxy operations mapping configuration
295300
.withProxyMethodMapping()
296301
.build();
297302
initClient.call("init_router_status").join();

src/test/resources/cartridge/app/roles/custom.lua

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
local cartridge = require('cartridge')
22

3-
local function get_routers()
3+
function get_routers()
44
local function table_contains(table, element)
55
for _, value in pairs(table) do
66
if value == element then
@@ -48,8 +48,6 @@ local function init(opts)
4848

4949
init_httpd()
5050

51-
rawset(_G, 'get_routers', get_routers)
52-
5351
return true
5452
end
5553

0 commit comments

Comments
 (0)