Skip to content

Commit 378dcd8

Browse files
committed
Add cluster feature docs
1 parent 2415808 commit 378dcd8

File tree

1 file changed

+98
-0
lines changed

1 file changed

+98
-0
lines changed

README.md

+98
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ To get the Java connector for Tarantool 1.6.9, visit
1717

1818
## Table of contents
1919
* [Getting started](#getting-started)
20+
* [Cluster support](#cluster-support)
2021
* [Where to get help](#where-to-get-help)
2122

2223
## Getting started
@@ -156,6 +157,102 @@ System.out.println(template.query("select * from hello_world where hello=:id", C
156157

157158
For more implementation details, see [API documentation](http://tarantool.github.io/tarantool-java/apidocs/index.html).
158159

160+
## Cluster support
161+
162+
To be more fault-tolerant the connector provides cluster extensions. In
163+
particular `TarantoolClusterClient` and built-in `RoundRobinSocketProviderImpl`
164+
used as a default `SocketProvider` implementation. When currently connected
165+
instance is down then the client will try to reconnect to the first available
166+
instance using strategy defined in a socket provider. You need to supply
167+
a list of nodes which will be used by the cluster client to provide such
168+
ability. Also you can prefer to use a [discovery mechanism](#auto-discovery)
169+
in order to dynamically fetch and apply the node list.
170+
171+
### Basic cluster client usage
172+
173+
1. Configure `TarantoolClusterClientConfig`:
174+
175+
```java
176+
TarantoolClusterClientConfig config = new TarantoolClusterClientConfig();
177+
// fill other settings
178+
config.operationExpiryTimeMillis = 2000;
179+
config.executor = Executors.newSingleThreadExecutor();
180+
```
181+
182+
2. Create an instance of `TarantoolClusterClientImpl`. You need to provide
183+
an initial list of nodes:
184+
185+
```java
186+
String[] nodes = new String[] { "myHost1:3301", "myHost2:3302", "myHost3:3301" };
187+
TarantoolClusterClient client = new TarantoolClusterClient(config, nodes);
188+
```
189+
190+
3. Work with the client using same API as defined in `TarantoolClient`:
191+
192+
```java
193+
client.syncOps().insert(23, Arrays.asList(1, 1));
194+
```
195+
196+
### Auto-discovery
197+
198+
Auto-discovery feature allows a cluster client to fetch addresses of
199+
cluster nodes to reflect changes related to the cluster topology. To achieve
200+
this you have to create a Lua function on the server side which returns
201+
a single array result. Client periodically pools the server to obtain a
202+
fresh list and apply it if its content changes.
203+
204+
1. On the server side create a function which returns nodes:
205+
206+
```bash
207+
tarantool> function get_cluster_nodes() return { 'host1:3301', 'host2:3302', 'host3:3301' } end
208+
```
209+
210+
You need to pay attention to a function contract we are currently supporting:
211+
* The client never passes any arguments to a discovery function.
212+
* A discovery function _should_ return a single result of strings (i.e. single
213+
string `return 'host:3301'` or array of strings `return {'host1:3301', 'host2:3301'}`).
214+
* A discovery function _may_ return multi-results but the client takes
215+
into account only first of them (i.e. `return {'host:3301'}, discovery_delay`, where
216+
the second result is unused). Even more, any extra results __are reserved__ by the client
217+
in order to extend its contract with a backward compatibility.
218+
* A discovery function _should NOT_ return no results, empty result, wrong type result,
219+
and Lua errors. The client discards such kinds of results but it does not affect the discovery
220+
process for next scheduled tasks.
221+
222+
2. On the client side configure discovery settings in `TarantoolClusterClientConfig`:
223+
224+
```java
225+
TarantoolClusterClientConfig config = new TarantoolClusterClientConfig();
226+
// fill other settings
227+
config.clusterDiscoveryEntryFunction = "get_cluster_nodes"; // discovery function used to fetch nodes
228+
config.clusterDiscoveryDelayMillis = 60_000; // how often client polls the discovery server
229+
```
230+
231+
3. Create a client using the config made above.
232+
233+
```java
234+
TarantoolClusterClient client = new TarantoolClusterClient(config);
235+
client.syncOps().insert(45, Arrays.asList(1, 1));
236+
```
237+
238+
### Auto-discovery caveats
239+
240+
* You need to set _not empty_ value to `clusterDiscoveryEntryFunction` to enable auto-discovery feature.
241+
* There are only two cases when a discovery task runs: just after initialization of the cluster
242+
client and a periodical scheduler timeout defined in `TarantoolClusterClientConfig.clusterDiscoveryDelayMillis`.
243+
* A discovery task always uses an active client connection to get the nodes list.
244+
It's in your responsibility to provide a function availability as well as a consistent
245+
nodes list on all instances you initially set or obtain from the task.
246+
* If some error occurs while a discovery task is running then this task
247+
will be aborted without any after-effects for next task executions. These cases, for instance, are
248+
a wrong function result (see discovery function contract) or a broken connection.
249+
There is an exception if the client is closed then discovery process will stop permanently.
250+
* It's possible to obtain a list which does NOT contain the node we are currently
251+
connected to. It leads the client to try to reconnect to another node from the
252+
new list. It may take some time to graceful disconnect from the current node.
253+
The client does its best to catch the moment when there are no pending responses
254+
and perform a reconnection.
255+
159256
## Where to get help
160257
161258
Got problems or questions? Post them on
@@ -164,6 +261,7 @@ Got problems or questions? Post them on
164261
base for possible answers and solutions.
165262
166263
## Building
264+
167265
To run tests
168266
```
169267
./mvnw clean test

0 commit comments

Comments
 (0)