12
12
/**
13
13
* Basic implementation of a client that may work with the cluster
14
14
* of tarantool instances in fault-tolerant way.
15
- *
15
+ * <p>
16
16
* Failed operations will be retried once connection is re-established
17
17
* unless the configured expiration time is over.
18
18
*/
@@ -25,21 +25,21 @@ public class TarantoolClusterClient extends TarantoolClientImpl {
25
25
26
26
/**
27
27
* @param config Configuration.
28
- * @param addrs Array of addresses in the form of [host]:[port].
28
+ * @param addrs Array of addresses in the form of [host]:[port].
29
29
*/
30
30
public TarantoolClusterClient (TarantoolClusterClientConfig config , String ... addrs ) {
31
31
this (config , new RoundRobinSocketProviderImpl (addrs ).setTimeout (config .operationExpiryTimeMillis ));
32
32
}
33
33
34
34
/**
35
35
* @param provider Socket channel provider.
36
- * @param config Configuration.
36
+ * @param config Configuration.
37
37
*/
38
38
public TarantoolClusterClient (TarantoolClusterClientConfig config , SocketChannelProvider provider ) {
39
39
super (provider , config );
40
40
41
41
this .executor = config .executor == null ?
42
- Executors .newSingleThreadExecutor () : config .executor ;
42
+ Executors .newSingleThreadExecutor () : config .executor ;
43
43
}
44
44
45
45
@ Override
@@ -59,23 +59,23 @@ protected boolean isDead(CompletableFuture<?> q) {
59
59
protected CompletableFuture <?> doExec (Code code , Object [] args ) {
60
60
validateArgs (args );
61
61
long sid = syncId .incrementAndGet ();
62
- CompletableFuture <?> q = makeFuture (sid , code , args );
62
+ ExpirableOp <?> future = makeFuture (sid , code , args );
63
63
64
- if (isDead (q )) {
65
- return q ;
64
+ if (isDead (future )) {
65
+ return future ;
66
66
}
67
- futures .put (sid , q );
68
- if (isDead (q )) {
67
+ futures .put (sid , future );
68
+ if (isDead (future )) {
69
69
futures .remove (sid );
70
- return q ;
70
+ return future ;
71
71
}
72
72
try {
73
73
write (code , sid , null , args );
74
74
} catch (Exception e ) {
75
75
futures .remove (sid );
76
- fail (q , e );
76
+ fail (future , e );
77
77
}
78
- return q ;
78
+ return future ;
79
79
}
80
80
81
81
@ Override
@@ -85,12 +85,12 @@ protected void fail(CompletableFuture<?> q, Exception e) {
85
85
86
86
protected boolean checkFail (CompletableFuture <?> q , Exception e ) {
87
87
assert q instanceof ExpirableOp <?>;
88
- if (!isTransientError (e ) || ((ExpirableOp <?>)q ).hasExpired (System .currentTimeMillis ())) {
88
+ if (!isTransientError (e ) || ((ExpirableOp <?>) q ).hasExpired (System .currentTimeMillis ())) {
89
89
q .completeExceptionally (e );
90
90
return true ;
91
91
} else {
92
92
assert retries != null ;
93
- retries .put (((ExpirableOp <?>) q ).getId (), (ExpirableOp <?>)q );
93
+ retries .put (((ExpirableOp <?>) q ).getId (), (ExpirableOp <?>) q );
94
94
return false ;
95
95
}
96
96
}
@@ -114,12 +114,12 @@ protected boolean isTransientError(Exception e) {
114
114
return true ;
115
115
}
116
116
if (e instanceof TarantoolException ) {
117
- return ((TarantoolException )e ).isTransient ();
117
+ return ((TarantoolException ) e ).isTransient ();
118
118
}
119
119
return false ;
120
120
}
121
121
122
- protected CompletableFuture <?> makeFuture (long id , Code code , Object ...args ) {
122
+ private ExpirableOp <?> makeFuture (long id , Code code , Object ... args ) {
123
123
int expireTime = ((TarantoolClusterClientConfig ) config ).operationExpiryTimeMillis ;
124
124
return new ExpirableOp (id , expireTime , code , args );
125
125
}
@@ -133,20 +133,20 @@ protected void onReconnect() {
133
133
// First call is before the constructor finished. Skip it.
134
134
return ;
135
135
}
136
- Collection <ExpirableOp <?>> futsToRetry = new ArrayList <ExpirableOp <?>>(retries .values ());
136
+ Collection <ExpirableOp <?>> futuresToRetry = new ArrayList <ExpirableOp <?>>(retries .values ());
137
137
retries .clear ();
138
138
long now = System .currentTimeMillis ();
139
- for (final ExpirableOp <?> fut : futsToRetry ) {
140
- if (!fut .hasExpired (now )) {
139
+ for (final ExpirableOp <?> future : futuresToRetry ) {
140
+ if (!future .hasExpired (now )) {
141
141
executor .execute (new Runnable () {
142
142
@ Override
143
143
public void run () {
144
- futures .put (fut .getId (), fut );
144
+ futures .put (future .getId (), future );
145
145
try {
146
- write (fut .getCode (), fut .getId (), null , fut .getArgs ());
146
+ write (future .getCode (), future .getId (), null , future .getArgs ());
147
147
} catch (Exception e ) {
148
- futures .remove (fut .getId ());
149
- fail (fut , e );
148
+ futures .remove (future .getId ());
149
+ fail (future , e );
150
150
}
151
151
}
152
152
});
@@ -157,8 +157,11 @@ public void run() {
157
157
/**
158
158
* Holds operation code and arguments for retry.
159
159
*/
160
- private class ExpirableOp <V > extends CompletableFuture <V > {
161
- /** Moment in time when operation is not considered for retry. */
160
+ private class ExpirableOp <V > extends TarantoolOp <V > {
161
+
162
+ /**
163
+ * Moment in time when operation is not considered for retry.
164
+ */
162
165
final private long deadline ;
163
166
164
167
/**
@@ -167,24 +170,20 @@ private class ExpirableOp<V> extends CompletableFuture<V> {
167
170
final private long id ;
168
171
169
172
/**
170
- * Tarantool binary protocol operation code .
173
+ * Arguments of operation.
171
174
*/
172
- final private Code code ;
173
-
174
- /** Arguments of operation. */
175
175
final private Object [] args ;
176
176
177
177
/**
178
- *
179
- * @param id Sync.
178
+ * @param id Sync.
180
179
* @param expireTime Expiration time (relative) in ms.
181
- * @param code Tarantool operation code.
182
- * @param args Operation arguments.
180
+ * @param code Tarantool operation code.
181
+ * @param args Operation arguments.
183
182
*/
184
- ExpirableOp (long id , int expireTime , Code code , Object ...args ) {
183
+ ExpirableOp (long id , int expireTime , Code code , Object ... args ) {
184
+ super (code );
185
185
this .id = id ;
186
186
this .deadline = System .currentTimeMillis () + expireTime ;
187
- this .code = code ;
188
187
this .args = args ;
189
188
}
190
189
@@ -196,12 +195,9 @@ public long getId() {
196
195
return id ;
197
196
}
198
197
199
- public Code getCode () {
200
- return code ;
201
- }
202
-
203
198
public Object [] getArgs () {
204
199
return args ;
205
200
}
201
+
206
202
}
207
203
}
0 commit comments