Skip to content

Commit 9967869

Browse files
dponomarevnicktorwald
dponomarev
authored andcommitted
Add support for tarantool clusters
1 parent 9b3f91f commit 9967869

12 files changed

+858
-104
lines changed

src/main/java/org/tarantool/SqlProtoUtils.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
import java.util.Map;
99

1010
public abstract class SqlProtoUtils {
11+
1112
public static List<Map<String, Object>> readSqlResult(TarantoolPacket pack) {
1213
List<List<?>> data = (List<List<?>>) pack.getBody().get(Key.DATA.getId());
1314

src/main/java/org/tarantool/TarantoolBase.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,4 +81,5 @@ public void setInitialRequestSize(int initialRequestSize) {
8181
public String getServerVersion() {
8282
return serverVersion;
8383
}
84+
8485
}
Lines changed: 194 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,53 +1,226 @@
11
package org.tarantool;
22

3+
public class TarantoolClientConfig {
34

4-
import java.util.concurrent.TimeUnit;
5+
private String username;
6+
private String password;
57

6-
public class TarantoolClientConfig {
8+
private int defaultRequestSize;
9+
private int predictedFutures;
710

8-
/**
9-
* username and password for authorization
10-
*/
11-
public String username;
11+
private int writerThreadPriority;
12+
private int readerThreadPriority;
13+
14+
private int sharedBufferSize;
15+
private double directWriteFactor;
1216

13-
public String password;
17+
private boolean useNewCall;
18+
19+
private long initTimeoutMillis;
20+
private long writeTimeoutMillis;
21+
22+
public static TarantoolClientConfigBuilder builder() {
23+
return new TarantoolClientConfigBuilder();
24+
}
25+
26+
protected TarantoolClientConfig(TarantoolClientConfigBuilder builder) {
27+
this.username = builder.username;
28+
this.password = builder.password;
29+
this.defaultRequestSize = builder.defaultRequestSize;
30+
this.predictedFutures = builder.predictedFutures;
31+
this.writerThreadPriority = builder.writerThreadPriority;
32+
this.readerThreadPriority = builder.readerThreadPriority;
33+
this.sharedBufferSize = builder.sharedBufferSize;
34+
this.directWriteFactor = builder.directWriteFactor;
35+
this.useNewCall = builder.useNewCall;
36+
this.initTimeoutMillis = builder.initTimeoutMillis;
37+
this.writeTimeoutMillis = builder.writeTimeoutMillis;
38+
}
1439

1540
/**
16-
* default ByteArrayOutputStream size when make query serialization
41+
* Gets an username for auth
42+
*
43+
* @return username
1744
*/
18-
public int defaultRequestSize = 4096;
45+
public String getUsername() {
46+
return username;
47+
}
1948

2049
/**
21-
* initial size for map which holds futures of sent request
50+
* Gets a password for auth
51+
*
52+
* @return password
2253
*/
23-
public int predictedFutures = (int) ((1024 * 1024) / 0.75) + 1;
54+
public String getPassword() {
55+
return password;
56+
}
2457

58+
/**
59+
* Gets a default request size when make query serialization
60+
*
61+
* @return
62+
*/
63+
public int getDefaultRequestSize() {
64+
return defaultRequestSize;
65+
}
2566

26-
public int writerThreadPriority = Thread.NORM_PRIORITY;
67+
/**
68+
* Gets an initial capacity for the map which holds futures of sent request
69+
*
70+
* @return initial capacity
71+
*/
72+
public int getPredictedFutures() {
73+
return predictedFutures;
74+
}
2775

28-
public int readerThreadPriority = Thread.NORM_PRIORITY;
76+
/**
77+
* Gets a writer thread priority
78+
*
79+
* @return thread priority
80+
*/
81+
public int getWriterThreadPriority() {
82+
return writerThreadPriority;
83+
}
2984

85+
/**
86+
* Gets a reader thread priority
87+
*
88+
* @return thread priority
89+
*/
90+
public int getReaderThreadPriority() {
91+
return readerThreadPriority;
92+
}
3093

3194
/**
32-
* shared buffer is place where client collect requests when socket is busy on write
95+
* Gets a shared buffer size (place where client collects requests
96+
* when socket is busy on write)
97+
*
98+
* @return buffer size
3399
*/
34-
public int sharedBufferSize = 8 * 1024 * 1024;
100+
public int getSharedBufferSize() {
101+
return sharedBufferSize;
102+
}
103+
35104
/**
36-
* not put request into the shared buffer if request size is ge directWriteFactor * sharedBufferSize
105+
* Gets a factor to calculate a threshold whether request will be accommodated
106+
* in the shared buffer.
107+
* if request size exceeds <code>directWriteFactor * sharedBufferSize</code>
108+
* request is sent directly.
109+
*
110+
* @return buffer factor
37111
*/
38-
public double directWriteFactor = 0.5d;
112+
public double getDirectWriteFactor() {
113+
return directWriteFactor;
114+
}
39115

40116
/**
117+
* Gets a flag whether the client will use new CALL version.
118+
*
41119
* Use old call command https://github.com/tarantool/doc/issues/54,
42120
* please ensure that you server supports new call command
121+
*
122+
* @return flag indicating CALL command version
43123
*/
44-
public boolean useNewCall = false;
124+
public boolean isUseNewCall() {
125+
return useNewCall;
126+
}
45127

46128
/**
47-
* Any blocking ops timeout
129+
* Gets an init timeout for synchronous operations
130+
*
131+
* @return init timeout
48132
*/
49-
public long initTimeoutMillis = 60*1000L;
133+
public long getInitTimeoutMillis() {
134+
return initTimeoutMillis;
135+
}
50136

51-
public long writeTimeoutMillis = 60*1000L;
137+
/**
138+
* Gets a write timeout for synchronous operations
139+
*
140+
* @return write timeout
141+
*/
142+
public long getWriteTimeoutMillis() {
143+
return writeTimeoutMillis;
144+
}
145+
146+
public static class TarantoolClientConfigBuilder<T extends TarantoolClientConfigBuilder<T>> {
147+
148+
private String username;
149+
private String password;
150+
private int defaultRequestSize = 4096;
151+
private int predictedFutures = (int) ((1024 * 1024) / 0.75) + 1;
152+
private int writerThreadPriority = Thread.NORM_PRIORITY;
153+
private int readerThreadPriority = Thread.NORM_PRIORITY;
154+
private int sharedBufferSize = 8 * 1024 * 1024;
155+
private double directWriteFactor = 0.5d;
156+
private boolean useNewCall = false;
157+
private long initTimeoutMillis = 60 * 1000L;
158+
private long writeTimeoutMillis = 60 * 1000L;
159+
160+
public T setUsername(String username) {
161+
this.username = username;
162+
return self();
163+
}
164+
165+
public T setPassword(String password) {
166+
this.password = password;
167+
return self();
168+
}
169+
170+
public T setDefaultRequestSize(int defaultRequestSize) {
171+
this.defaultRequestSize = defaultRequestSize;
172+
return self();
173+
}
174+
175+
public T setPredictedFutures(int predictedFutures) {
176+
this.predictedFutures = predictedFutures;
177+
return self();
178+
}
179+
180+
public TarantoolClientConfigBuilder setWriterThreadPriority(int writerThreadPriority) {
181+
this.writerThreadPriority = writerThreadPriority;
182+
return self();
183+
}
184+
185+
public T setReaderThreadPriority(int readerThreadPriority) {
186+
this.readerThreadPriority = readerThreadPriority;
187+
return self();
188+
}
189+
190+
public T setSharedBufferSize(int sharedBufferSize) {
191+
this.sharedBufferSize = sharedBufferSize;
192+
return self();
193+
}
194+
195+
public T setDirectWriteFactor(double directWriteFactor) {
196+
this.directWriteFactor = directWriteFactor;
197+
return self();
198+
}
199+
200+
public T setUseNewCall(boolean useNewCall) {
201+
this.useNewCall = useNewCall;
202+
return self();
203+
}
204+
205+
public T setInitTimeoutMillis(long initTimeoutMillis) {
206+
this.initTimeoutMillis = initTimeoutMillis;
207+
return self();
208+
}
209+
210+
public T setWriteTimeoutMillis(long writeTimeoutMillis) {
211+
this.writeTimeoutMillis = writeTimeoutMillis;
212+
return self();
213+
}
214+
215+
public TarantoolClientConfig build() {
216+
return new TarantoolClientConfig(this);
217+
}
218+
219+
@SuppressWarnings("unchecked")
220+
protected T self() {
221+
return (T) this;
222+
}
223+
224+
}
52225

53226
}

src/main/java/org/tarantool/TarantoolClientImpl.java

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,9 @@
2727

2828

2929
public class TarantoolClientImpl extends TarantoolBase<Future<?>> implements TarantoolClient {
30+
3031
public static final CommunicationException NOT_INIT_EXCEPTION = new CommunicationException("Not connected, initializing connection");
32+
3133
protected TarantoolClientConfig config;
3234

3335
/**
@@ -138,8 +140,9 @@ protected void reconnect(int retry, Throwable lastError) {
138140

139141
protected void connect(final SocketChannel channel) throws Exception {
140142
try {
141-
TarantoolGreeting greeting = ProtoUtils.connect(channel,
142-
config.username, config.password);
143+
TarantoolGreeting greeting = ProtoUtils.connect(
144+
channel, config.username, config.password
145+
);
143146
this.serverVersion = greeting.getServerVersion();
144147
} catch (IOException e) {
145148
try {

0 commit comments

Comments
 (0)