Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix infinite loop when retrying to get a connection #441

Merged
merged 1 commit into from
Nov 16, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
### Bugfixes

- Fix Instant converter to parse 8 bytes datetime ([#408](https://github.com/tarantool/cartridge-java/issues/408))
- Fix infinite loop when retrying to get a connection ([#440](https://github.com/tarantool/cartridge-java/issues/440))

### Internal and API changes

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,22 +38,27 @@ static final class RoundRobinStrategy implements ConnectionSelectionStrategy {

private final TarantoolConnectionIterator connectionIterator;
private final AtomicInteger available;
private final int connectionsCount;

RoundRobinStrategy(Collection<TarantoolConnection> connections) {
this.available = new AtomicInteger(connections.size());
this.connectionsCount = connections.size();
nickkkccc marked this conversation as resolved.
Show resolved Hide resolved
this.available = new AtomicInteger(this.connectionsCount);
this.connectionIterator = new TarantoolConnectionIterator(connections.stream()
.peek(conn -> conn.addConnectionCloseListener(c -> available.getAndDecrement()))
.collect(Collectors.toList()));
}

@Override
public TarantoolConnection next() throws NoAvailableConnectionsException {
if (available.get() > 0) {
while (connectionIterator.hasNext()) {
TarantoolConnection connection = connectionIterator.next();
if (connection.isConnected()) {
return connection;
}
int attempts = 0;
while (available.get() > 0 && connectionIterator.hasNext()) {
TarantoolConnection connection = connectionIterator.next();
if (connection.isConnected()) {
return connection;
}

if (++attempts > connectionsCount) {
break;
}
}
throw new NoAvailableConnectionsException();
Expand Down Expand Up @@ -84,10 +89,12 @@ static final class ParallelRoundRobinStrategy implements ConnectionSelectionStra
private final TarantoolClientConfig config;
private final CyclingIterator<TarantoolConnectionIterator> iteratorsIterator;
private final AtomicInteger available;
private final int connectionsCount;

ParallelRoundRobinStrategy(TarantoolClientConfig config, Collection<TarantoolConnection> connections) {
this.config = config;
this.available = new AtomicInteger(connections.size());
this.connectionsCount = connections.size();
this.available = new AtomicInteger(this.connectionsCount);
this.iteratorsIterator = new CyclingIterator<>(populateIterators(connections));
}

Expand All @@ -106,12 +113,14 @@ private Collection<TarantoolConnectionIterator> populateIterators(

@Override
public TarantoolConnection next() throws NoAvailableConnectionsException {
if (available.get() > 0) {
while (iteratorsIterator.hasNext()) {
TarantoolConnection connection = iteratorsIterator.next().next();
if (connection.isConnected()) {
return connection;
}
int attempts = 0;
while (available.get() > 0 && iteratorsIterator.hasNext()) {
TarantoolConnection connection = iteratorsIterator.next().next();
if (connection.isConnected()) {
return connection;
}
if (++attempts > connectionsCount) {
break;
}
}
throw new NoAvailableConnectionsException();
Expand Down