@@ -22,7 +22,7 @@ To get the Java connector for Tarantool 1.6.9, visit
22
22
23
23
## Getting started
24
24
25
- 1 . Add a dependency to your ` pom.xml ` file.
25
+ 1 . Add a dependency to your ` pom.xml ` file:
26
26
27
27
``` xml
28
28
<dependency >
@@ -32,75 +32,81 @@ To get the Java connector for Tarantool 1.6.9, visit
32
32
</dependency >
33
33
```
34
34
35
- 2 . Configure ` TarantoolClientConfig ` .
35
+ 2 . Configure ` TarantoolClientConfig ` :
36
36
37
37
``` java
38
38
TarantoolClientConfig config = new TarantoolClientConfig ();
39
39
config. username = " test" ;
40
40
config. password = " test" ;
41
41
```
42
42
43
- 3 . Implement your ` SocketChannelProvider ` .
44
- It should return a connected ` SocketChannel ` .
43
+ 3 . Create a client:
45
44
46
45
``` java
47
- SocketChannelProvider socketChannelProvider = new SocketChannelProvider () {
48
- @Override
49
- public SocketChannel get (int retryNumber , Throwable lastError ) {
50
- if (lastError != null ) {
51
- lastError. printStackTrace(System . out);
52
- }
53
- try {
54
- return SocketChannel . open(new InetSocketAddress (" localhost" , 3301 ));
55
- } catch (IOException e) {
56
- throw new IllegalStateException (e);
57
- }
58
- }
59
- };
46
+ TarantoolClient client = new TarantoolClientImpl (" host:3301" , config);
60
47
```
61
48
62
- Here you could also implement some reconnection or fallback policy.
63
- Remember that ` TarantoolClient ` adopts a
64
- [ fail-fast] ( https://en.wikipedia.org/wiki/Fail-fast ) policy
65
- when a client is not connected.
49
+ using ` TarantoolClientImpl(String, TarantoolClientConfig) ` is equivalent to:
66
50
67
- The ` TarantoolClient ` will stop functioning if your implementation of a socket
68
- channel provider raises an exception or returns a null. You will need a new
69
- instance of client to recover. Hence, you should only throw in case you have
70
- met unrecoverable error.
51
+ ``` java
52
+ SocketChannelProvider socketChannelProvider = new SingleSocketChannelProviderImpl ( " host:3301 " )
53
+ TarantoolClient client = new TarantoolClientImpl (socketChannelProvider, config);
54
+ ```
71
55
72
- Below is an example of ` SocketChannelProvider ` implementation that handles short
73
- tarantool restarts.
56
+ You could implement your own ` SocketChannelProvider ` . It should return
57
+ a connected ` SocketChannel ` . Feel free to implement ` get(int retryNumber, Throwable lastError) `
58
+ using your appropriate strategy to obtain the channel. The strategy can take into
59
+ account current attempt number (retryNumber) and the last transient error occurred on
60
+ the previous attempt.
61
+
62
+ The ` TarantoolClient ` will be closed if your implementation of a socket
63
+ channel provider raises any exceptions but not a ` SocketProviderTransientException ` .
64
+ Latter is handled by the client as a recoverable error. Otherwise, you will
65
+ need a new instance of client to recover. Hence, you should only throw an
66
+ error different to ` SocketProviderTransientException ` in case you have met
67
+ unrecoverable error.
68
+
69
+ Below is an example of ` SocketChannelProvider ` implementation that tries
70
+ to connect no more than 3 times, two seconds for each attempt at max.
74
71
75
72
``` java
76
73
SocketChannelProvider socketChannelProvider = new SocketChannelProvider () {
77
74
@Override
78
75
public SocketChannel get (int retryNumber , Throwable lastError ) {
79
- long deadline = System . currentTimeMillis() + RESTART_TIMEOUT ;
80
- while (! Thread . currentThread(). isInterrupted()) {
81
- try {
82
- return SocketChannel . open(new InetSocketAddress (" localhost" , 3301 ));
83
- } catch (IOException e) {
84
- if (deadline < System . currentTimeMillis())
85
- throw new RuntimeException (e);
86
- try {
87
- Thread . sleep(100 );
88
- } catch (InterruptedException ignored) {
89
- Thread . currentThread(). interrupt();
90
- }
76
+ if (retryNumber > 3 ) {
77
+ throw new RuntimeException (" Too many attempts" );
78
+ }
79
+ SocketChannel channel = null ;
80
+ try {
81
+ channel = SocketChannel . open();
82
+ channel. socket(). connect(new InetSocketAddress (" localhost" , 3301 ), 2000 );
83
+ return channel;
84
+ } catch (IOException e) {
85
+ if (channel != null ) {
86
+ try {
87
+ channel. close();
88
+ } catch (IOException ignored) { }
91
89
}
90
+ throw new SocketProviderTransientException (" Couldn't connect to server" , e);
92
91
}
93
- throw new RuntimeException (new TimeoutException (" Connect timed out." ));
94
92
}
95
93
};
96
94
```
97
95
98
- 4 . Create a client.
96
+ Same behaviour can be achieved using built-in ` SingleSocketChannelProviderImpl ` :
99
97
100
98
``` java
99
+ TarantoolClientConfig config = new TarantoolClientConfig ();
100
+ config. connectionTimeout = 2_000 ; // two seconds timeout per attempt
101
+ config. retryCount = 3 ; // three attempts at max
102
+
103
+ SocketChannelProvider socketChannelProvider = new SingleSocketChannelProviderImpl (" localhost:3301" )
101
104
TarantoolClient client = new TarantoolClientImpl (socketChannelProvider, config);
102
105
```
103
106
107
+ ` SingleSocketChannelProviderImpl ` implements ` ConfigurableSocketChannelProvider ` that
108
+ makes possible for the client to configure a socket provider.
109
+
104
110
> ** Notes:**
105
111
> * ` TarantoolClient ` is thread-safe and asynchronous, so you should use one
106
112
> client inside the whole application.
@@ -168,6 +174,21 @@ a list of nodes which will be used by the cluster client to provide such
168
174
ability. Also you can prefer to use a [ discovery mechanism] ( #auto-discovery )
169
175
in order to dynamically fetch and apply the node list.
170
176
177
+ ### The RoundRobinSocketProviderImpl class
178
+
179
+ This cluster-aware provider uses addresses pool to connect to DB server.
180
+ The provider picks up next address in order the addresses were passed.
181
+
182
+ Similar to ` SingleSocketChannelProviderImpl ` this RR provider also
183
+ relies on two options from the config: ` TarantoolClientConfig.connectionTimeout `
184
+ and ` TarantoolClientConfig.retryCount ` but in a bit different way.
185
+ The latter option says how many times the provider should try to establish a
186
+ connection to _ one instance_ before failing an attempt. The provider requires
187
+ positive retry count to work properly. The socket timeout is used to limit
188
+ an interval between connections attempts per instance. In other words, the provider
189
+ follows a pattern _ connection should succeed after N attempts with M interval between
190
+ them at max_ .
191
+
171
192
### Basic cluster client usage
172
193
173
194
1 . Configure ` TarantoolClusterClientConfig ` :
@@ -198,7 +219,7 @@ client.syncOps().insert(23, Arrays.asList(1, 1));
198
219
Auto-discovery feature allows a cluster client to fetch addresses of
199
220
cluster nodes to reflect changes related to the cluster topology. To achieve
200
221
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
222
+ a single array result. Client periodically polls the server to obtain a
202
223
fresh list and apply it if its content changes.
203
224
204
225
1 . On the server side create a function which returns nodes:
0 commit comments