Skip to content

Commit

Permalink
Multibinding for RetryStrategy
Browse files Browse the repository at this point in the history
  • Loading branch information
dominikzalewski committed Dec 15, 2023
1 parent 3aea15e commit 7cc22ac
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,28 +25,30 @@
import java.sql.Connection;
import java.sql.SQLException;
import java.sql.SQLTransientException;
import java.util.Set;

import static java.time.temporal.ChronoUnit.MILLIS;
import static java.time.temporal.ChronoUnit.SECONDS;
import static java.util.Collections.singleton;
import static java.util.Objects.requireNonNull;

public class RetryingConnectionFactory
implements ConnectionFactory
{
private final Set<RetryStrategy> retryStrategies;
private final RetryPolicy<Object> retryPolicy;

private final ConnectionFactory delegate;

@Inject
public RetryingConnectionFactory(StatisticsAwareConnectionFactory delegate, RetryStrategy retryStrategy)
public RetryingConnectionFactory(StatisticsAwareConnectionFactory delegate, Set<RetryStrategy> retryStrategies)
{
requireNonNull(retryStrategy);
this.retryStrategies = retryStrategiesWithDefault(requireNonNull(retryStrategies));
this.delegate = requireNonNull(delegate, "delegate is null");
this.retryPolicy = RetryPolicy.builder()
.withMaxDuration(java.time.Duration.of(30, SECONDS))
.withMaxAttempts(5)
.withBackoff(50, 5_000, MILLIS, 4)
.handleIf(retryStrategy::isExceptionRecoverable)
.handleIf(this::combineRetryStrategies)
.abortOn(TrinoException.class)
.build();
}
Expand Down Expand Up @@ -89,4 +91,23 @@ public boolean isExceptionRecoverable(Throwable exception)
.anyMatch(SQLTransientException.class::isInstance);
}
}

private boolean combineRetryStrategies(Throwable throwable)
{
return retryStrategies.stream()
.map(retryStrategy -> retryStrategy.isExceptionRecoverable(throwable))
.filter(recoverable -> recoverable)
.findAny()
.orElse(false);
}

private static Set<RetryStrategy> retryStrategiesWithDefault(Set<RetryStrategy> retryStrategies)
{
if (retryStrategies.isEmpty()) {
return singleton(new DefaultRetryStrategy());
}
else {
return retryStrategies;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,9 @@

import com.google.inject.AbstractModule;
import com.google.inject.Scopes;
import io.trino.plugin.jdbc.RetryingConnectionFactory.DefaultRetryStrategy;
import io.trino.plugin.jdbc.RetryingConnectionFactory.RetryStrategy;

import static com.google.inject.multibindings.OptionalBinder.newOptionalBinder;
import static com.google.inject.multibindings.Multibinder.newSetBinder;

public class RetryingConnectionFactoryModule
extends AbstractModule
Expand All @@ -27,9 +26,6 @@ public class RetryingConnectionFactoryModule
public void configure()
{
bind(RetryingConnectionFactory.class).in(Scopes.SINGLETON);
newOptionalBinder(binder(), RetryStrategy.class)
.setDefault()
.to(DefaultRetryStrategy.class)
.in(Scopes.SINGLETON);
newSetBinder(binder(), RetryStrategy.class);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
import java.util.stream.Stream;

import static com.google.common.reflect.Reflection.newProxy;
import static com.google.inject.multibindings.OptionalBinder.newOptionalBinder;
import static com.google.inject.multibindings.Multibinder.newSetBinder;
import static io.trino.plugin.jdbc.TestRetryingConnectionFactory.MockConnectorFactory.Action.RETURN;
import static io.trino.plugin.jdbc.TestRetryingConnectionFactory.MockConnectorFactory.Action.THROW_NPE;
import static io.trino.plugin.jdbc.TestRetryingConnectionFactory.MockConnectorFactory.Action.THROW_SQL_EXCEPTION;
Expand Down Expand Up @@ -171,7 +171,7 @@ private static Injector createInjectorWithOverridenStrategy(MockConnectorFactory
binder.bind(MockConnectorFactory.class).in(Scopes.SINGLETON);
binder.bind(ConnectionFactory.class).annotatedWith(ForBaseJdbc.class).to(Key.get(MockConnectorFactory.class));
binder.install(new RetryingConnectionFactoryModule());
newOptionalBinder(binder, RetryStrategy.class).setBinding().to(OverrideRetryStrategy.class).in(Scopes.SINGLETON);
newSetBinder(binder, RetryStrategy.class).addBinding().to(OverrideRetryStrategy.class);
});
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ public void configure(Binder binder)
configBinder(binder).bindConfig(OracleConfig.class);
newOptionalBinder(binder, Key.get(int.class, MaxDomainCompactionThreshold.class)).setBinding().toInstance(ORACLE_MAX_LIST_EXPRESSIONS);
newSetBinder(binder, ConnectorTableFunction.class).addBinding().toProvider(Query.class).in(Scopes.SINGLETON);
newOptionalBinder(binder, RetryStrategy.class).setBinding().to(OracleRetryStrategy.class).in(Scopes.SINGLETON);
newSetBinder(binder, RetryStrategy.class).addBinding().to(OracleRetryStrategy.class);
}

@Provides
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@
import static io.trino.testing.TestingConnectorSession.SESSION;
import static io.trino.testing.containers.TestContainers.startOrReuse;
import static java.lang.String.format;
import static java.util.Collections.singleton;

public class TestingOracleServer
implements AutoCloseable
Expand Down Expand Up @@ -131,7 +132,7 @@ private ConnectionFactory getConnectionFactory(String connectionUrl, String user
new OracleDriver(),
new BaseJdbcConfig().setConnectionUrl(connectionUrl),
StaticCredentialProvider.of(username, password)));
return new RetryingConnectionFactory(connectionFactory, new DefaultRetryStrategy());
return new RetryingConnectionFactory(connectionFactory, singleton(new DefaultRetryStrategy()));
}

@Override
Expand Down

0 comments on commit 7cc22ac

Please sign in to comment.