Skip to content

Commit 5ed7e3e

Browse files
authored
Refactoring for RequestRetryPolicies and classes in exceptions.errors package (#162)
Added TarantoolNoSuchProcedureException and refactored TarantoolErrors for parsing errors from MsgPack maps
1 parent a9bfb28 commit 5ed7e3e

27 files changed

+483
-285
lines changed

README.md

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -267,9 +267,13 @@ TarantoolClient<TarantoolTuple, TarantoolResult<TarantoolTuple>> retrying(
267267
TarantoolClient<TarantoolTuple, TarantoolResult<TarantoolTuple>> client, int retries, long delay) {
268268
return TarantoolClientFactory.configureClient(client)
269269
.withRetryingByNumberOfAttempts(
270-
retries,
271-
e -> e.getMessage().contains("Unsuccessful attempt"),
272-
policy -> policy.withDelay(delay))
270+
retries,
271+
// you can use default predicates from TarantoolRequestRetryPolicies for checking errors
272+
TarantoolRequestRetryPolicies.retryNetworkErrors()
273+
// also you can use your own predicates and combine them with each other or with defaults
274+
.or(e -> e.getMessage().contains("Unsuccessful attempt"))
275+
.or(TarantoolRequestRetryPolicies.retryTarantoolNoSuchProcedureErrors()),
276+
policy -> policy.withDelay(delay))
273277
.build();
274278
}
275279

src/main/java/io/tarantool/driver/api/TarantoolClientConfigurator.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
import io.tarantool.driver.api.retry.TarantoolRequestRetryPolicies;
66
import io.tarantool.driver.api.tuple.TarantoolTuple;
77

8-
import java.util.function.Function;
8+
import java.util.function.Predicate;
99
import java.util.function.UnaryOperator;
1010

1111
/**
@@ -55,7 +55,7 @@ public interface TarantoolClientConfigurator<SELF extends TarantoolClientConfigu
5555
*/
5656
SELF withRetryingByNumberOfAttempts(
5757
int numberOfAttempts, UnaryOperator<TarantoolRequestRetryPolicies
58-
.AttemptsBoundRetryPolicyFactory.Builder<Function<Throwable, Boolean>>> policy);
58+
.AttemptsBoundRetryPolicyFactory.Builder<Predicate<Throwable>>> policy);
5959

6060
/**
6161
* Configure the attempts bound request retry policy.
@@ -67,7 +67,7 @@ SELF withRetryingByNumberOfAttempts(
6767
* @param <T> callback type for exceptions check
6868
* @return this instance of builder {@link TarantoolClientConfigurator}
6969
*/
70-
<T extends Function<Throwable, Boolean>> SELF withRetryingByNumberOfAttempts(
70+
<T extends Predicate<Throwable>> SELF withRetryingByNumberOfAttempts(
7171
int numberOfAttempts, T exceptionsCheck,
7272
UnaryOperator<TarantoolRequestRetryPolicies.AttemptsBoundRetryPolicyFactory.Builder<T>> policy);
7373

@@ -81,7 +81,7 @@ <T extends Function<Throwable, Boolean>> SELF withRetryingByNumberOfAttempts(
8181
*/
8282
SELF withRetryingIndefinitely(
8383
UnaryOperator<TarantoolRequestRetryPolicies.InfiniteRetryPolicyFactory.Builder
84-
<Function<Throwable, Boolean>>> policy);
84+
<Predicate<Throwable>>> policy);
8585

8686
/**
8787
* Configure the infinite request retry policy.
@@ -92,7 +92,7 @@ SELF withRetryingIndefinitely(
9292
* @param <T> callback type for exceptions check
9393
* @return this instance of builder {@link TarantoolClientConfigurator}
9494
*/
95-
<T extends Function<Throwable, Boolean>> SELF withRetryingIndefinitely(
95+
<T extends Predicate<Throwable>> SELF withRetryingIndefinitely(
9696
T callback,
9797
UnaryOperator<TarantoolRequestRetryPolicies.InfiniteRetryPolicyFactory.Builder<T>> policy);
9898

src/main/java/io/tarantool/driver/api/retry/RequestRetryPolicy.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
import java.util.function.Supplier;
1313

1414
/**
15-
* Request retry policy contains an algorithm of deciding whether an exception is retriable and settings for
15+
* Request retry policy contains an algorithm of deciding whether an exception is retryable and settings for
1616
* limiting the retry attempts
1717
*
1818
* @author Alexey Kuzin

src/main/java/io/tarantool/driver/api/retry/TarantoolRequestRetryPolicies.java

Lines changed: 55 additions & 61 deletions
Large diffs are not rendered by default.

src/main/java/io/tarantool/driver/core/AbstractTarantoolClient.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,7 @@ private TarantoolConnectionManager connectionManager() {
150150

151151
@Override
152152
public boolean establishLackingConnections() {
153-
return connectionManager.establishLackingConnections();
153+
return connectionManager().establishLackingConnections();
154154
}
155155

156156
@Override

src/main/java/io/tarantool/driver/core/TarantoolClientConfiguratorImpl.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
import io.tarantool.driver.api.retry.TarantoolRequestRetryPolicies;
99
import io.tarantool.driver.api.tuple.TarantoolTuple;
1010

11-
import java.util.function.Function;
11+
import java.util.function.Predicate;
1212
import java.util.function.UnaryOperator;
1313

1414
import static io.tarantool.driver.api.retry.TarantoolRequestRetryPolicies.retryNetworkErrors;
@@ -54,12 +54,12 @@ public SELF withRetryingByNumberOfAttempts(int numberOfAttempts) {
5454
@Override
5555
public SELF withRetryingByNumberOfAttempts(
5656
int numberOfAttempts, UnaryOperator<TarantoolRequestRetryPolicies
57-
.AttemptsBoundRetryPolicyFactory.Builder<Function<Throwable, Boolean>>> policy) {
57+
.AttemptsBoundRetryPolicyFactory.Builder<Predicate<Throwable>>> policy) {
5858
return withRetryingByNumberOfAttempts(numberOfAttempts, retryNetworkErrors(), policy);
5959
}
6060

6161
@Override
62-
public <T extends Function<Throwable, Boolean>> SELF withRetryingByNumberOfAttempts(
62+
public <T extends Predicate<Throwable>> SELF withRetryingByNumberOfAttempts(
6363
int numberOfAttempts, T exceptionsCheck,
6464
UnaryOperator<TarantoolRequestRetryPolicies.AttemptsBoundRetryPolicyFactory.Builder<T>> policy) {
6565
return withRetrying(policy.apply(TarantoolRequestRetryPolicies.AttemptsBoundRetryPolicyFactory
@@ -68,12 +68,12 @@ public <T extends Function<Throwable, Boolean>> SELF withRetryingByNumberOfAttem
6868

6969
@Override
7070
public SELF withRetryingIndefinitely(UnaryOperator<TarantoolRequestRetryPolicies
71-
.InfiniteRetryPolicyFactory.Builder<Function<Throwable, Boolean>>> policy) {
71+
.InfiniteRetryPolicyFactory.Builder<Predicate<Throwable>>> policy) {
7272
return withRetryingIndefinitely(retryNetworkErrors(), policy);
7373
}
7474

7575
@Override
76-
public <T extends Function<Throwable, Boolean>> SELF withRetryingIndefinitely(
76+
public <T extends Predicate<Throwable>> SELF withRetryingIndefinitely(
7777
T callback, UnaryOperator<TarantoolRequestRetryPolicies.InfiniteRetryPolicyFactory.Builder<T>> policy) {
7878
return withRetrying(policy.apply(TarantoolRequestRetryPolicies.InfiniteRetryPolicyFactory.builder(callback))
7979
.build());

src/main/java/io/tarantool/driver/core/connection/TarantoolConnectionFactory.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ public CompletableFuture<TarantoolConnection> singleConnection(InetSocketAddress
7575
timeoutScheduler.schedule(() -> {
7676
if (!connectionFuture.isDone()) {
7777
connectionFuture.completeExceptionally(new TimeoutException(
78-
String.format("Failed to to the Tarantool server at %s within %d ms",
78+
String.format("Failed to connect to the Tarantool server at %s within %d ms",
7979
serverAddress, config.getConnectTimeout())));
8080
}
8181
}, config.getConnectTimeout(), TimeUnit.MILLISECONDS);

src/main/java/io/tarantool/driver/core/metadata/DDLTarantoolSpaceMetadataConverter.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -221,7 +221,7 @@ private Map<String, TarantoolIndexMetadata> parseIndexes(Map<String, TarantoolFi
221221
.map(parts -> {
222222
if (!parts.isMapValue()) {
223223
throw new TarantoolClientException(
224-
"Unsupported index metadata format: index part metadata is not a map");
224+
"Unsupported index metadata format: index part metadata is not a map");
225225
}
226226

227227
Map<Value, Value> partsMap = parts.asMapValue().map();
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package io.tarantool.driver.exceptions;
2+
3+
/**
4+
* Corresponds to an exception that occurs when the procedure has not (yet) been defined in the Tarantool instance.
5+
* <p></p>
6+
* For example: this exception can be raised when a Cartridge role is not up yet or is being reloaded at the moment.
7+
* This can happen when the tarantool 'router' node is restarted or hot-reloaded but the cluster is under
8+
* load at the moment. If CRUD module is used in the cluster then 'cartridge.roles.crud-router' may
9+
* have not been initialized yet and all requests to CRUD will fail with this exception.
10+
*
11+
* You can use this exception when you are sure that it's worth trying a few more times until the role is up.
12+
*
13+
* @author Oleg Kuznetsov
14+
*/
15+
public class TarantoolNoSuchProcedureException extends TarantoolException {
16+
public TarantoolNoSuchProcedureException(String errorMessage) {
17+
super(errorMessage);
18+
}
19+
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package io.tarantool.driver.exceptions.errors;
2+
3+
import org.msgpack.value.StringValue;
4+
import org.msgpack.value.ValueFactory;
5+
6+
/**
7+
* Errors keys for error message in box error
8+
*
9+
* @author Oleg Kuznetsov
10+
*/
11+
enum BoxErrorKey implements ErrorKey {
12+
CODE("code", ValueFactory.newString("code")),
13+
BASE_TYPE("base_type", ValueFactory.newString("base_type")),
14+
TYPE("type", ValueFactory.newString("type")),
15+
MESSAGE("message", ValueFactory.newString("message")),
16+
TRACE("trace", ValueFactory.newString("trace"));
17+
18+
private final String key;
19+
private final StringValue msgPackKey;
20+
21+
BoxErrorKey(String key, StringValue msgPackKey) {
22+
this.key = key;
23+
this.msgPackKey = msgPackKey;
24+
}
25+
26+
@Override
27+
public String getKey() {
28+
return key;
29+
}
30+
31+
@Override
32+
public StringValue getMsgPackKey() {
33+
return msgPackKey;
34+
}
35+
}

0 commit comments

Comments
 (0)