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