forked from dork/tarantool-java
-
Notifications
You must be signed in to change notification settings - Fork 19
Support auto refresh a list of cluster nodes #136
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
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
169 changes: 169 additions & 0 deletions
169
src/main/java/org/tarantool/BaseSocketChannelProvider.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,169 @@ | ||
package org.tarantool; | ||
|
||
import java.io.IOException; | ||
import java.net.InetSocketAddress; | ||
import java.nio.channels.SocketChannel; | ||
|
||
public abstract class BaseSocketChannelProvider implements SocketChannelProvider { | ||
|
||
/** | ||
* Limit of retries. | ||
*/ | ||
private int retriesLimit = RETRY_NO_LIMIT; | ||
|
||
/** | ||
* Timeout to establish socket connection with an individual server. | ||
*/ | ||
private int timeout = NO_TIMEOUT; | ||
|
||
/** | ||
* Tries to establish a new connection to the Tarantool instances. | ||
* | ||
* @param retryNumber number of current retry. Reset after successful connect. | ||
* @param lastError the last error occurs when reconnecting | ||
* | ||
* @return connected socket channel | ||
* | ||
* @throws CommunicationException if any I/O errors happen or there are | ||
* no addresses available | ||
*/ | ||
@Override | ||
public final SocketChannel get(int retryNumber, Throwable lastError) { | ||
if (areRetriesExhausted(retryNumber)) { | ||
throw new CommunicationException("Connection retries exceeded.", lastError); | ||
} | ||
|
||
long deadline = System.currentTimeMillis() + timeout; | ||
while (!Thread.currentThread().isInterrupted()) { | ||
try { | ||
InetSocketAddress address = getAddress(retryNumber, lastError); | ||
return openChannel(address); | ||
} catch (IOException e) { | ||
checkTimeout(deadline, e); | ||
} | ||
} | ||
throw new CommunicationException("Thread interrupted.", new InterruptedException()); | ||
} | ||
|
||
private void checkTimeout(long deadline, Exception e) { | ||
long timeLeft = deadline - System.currentTimeMillis(); | ||
if (timeLeft <= 0) { | ||
throw new CommunicationException("Connection time out.", e); | ||
} | ||
try { | ||
Thread.sleep(timeLeft / 10); | ||
} catch (InterruptedException ignored) { | ||
Thread.currentThread().interrupt(); | ||
} | ||
} | ||
|
||
/** | ||
* Gets address to be used to establish a new connection | ||
* Address can be null. | ||
* | ||
* @param retryNumber reconnection attempt number | ||
* @param lastError reconnection reason | ||
* | ||
* @return available address which is depended on implementation | ||
* | ||
* @throws IOException if any I/O errors occur | ||
*/ | ||
protected abstract InetSocketAddress getAddress(int retryNumber, Throwable lastError) throws IOException; | ||
|
||
/** | ||
* Sets maximum amount of reconnect attempts to be made before an exception is raised. | ||
* The retry count is maintained by a {@link #get(int, Throwable)} caller | ||
* when a socket level connection was established. | ||
* <p> | ||
* Negative value means unlimited attempts. | ||
* | ||
* @param retriesLimit Limit of retries to use. | ||
*/ | ||
public void setRetriesLimit(int retriesLimit) { | ||
this.retriesLimit = retriesLimit; | ||
} | ||
|
||
/** | ||
* Gets limit of attempts to establish connection. | ||
* | ||
* @return Maximum reconnect attempts to make before raising exception. | ||
*/ | ||
public int getRetriesLimit() { | ||
return retriesLimit; | ||
} | ||
|
||
/** | ||
* Parse a string address in the form of host[:port] | ||
* and builds a socket address. | ||
* | ||
* @param address Server address. | ||
* | ||
* @return Socket address. | ||
*/ | ||
protected InetSocketAddress parseAddress(String address) { | ||
int separatorPosition = address.indexOf(':'); | ||
String host = (separatorPosition < 0) ? address : address.substring(0, separatorPosition); | ||
int port = (separatorPosition < 0) ? 3301 : Integer.parseInt(address.substring(separatorPosition + 1)); | ||
return new InetSocketAddress(host, port); | ||
} | ||
|
||
protected SocketChannel openChannel(InetSocketAddress socketAddress) throws IOException { | ||
SocketChannel channel = null; | ||
try { | ||
channel = SocketChannel.open(); | ||
Totktonada marked this conversation as resolved.
Show resolved
Hide resolved
|
||
channel.socket().connect(socketAddress, timeout); | ||
return channel; | ||
} catch (IOException e) { | ||
if (channel != null) { | ||
try { | ||
channel.close(); | ||
} catch (IOException ignored) { | ||
// No-op. | ||
} | ||
} | ||
throw e; | ||
} | ||
} | ||
|
||
/** | ||
* Sets maximum amount of time to wait for a socket connection establishment | ||
* with an individual server. | ||
* <p> | ||
* Zero means infinite timeout. | ||
* | ||
* @param timeout timeout value, ms. | ||
* | ||
* @throws IllegalArgumentException if timeout is negative. | ||
*/ | ||
public void setTimeout(int timeout) { | ||
Totktonada marked this conversation as resolved.
Show resolved
Hide resolved
|
||
if (timeout < 0) { | ||
throw new IllegalArgumentException("timeout is negative."); | ||
} | ||
this.timeout = timeout; | ||
} | ||
|
||
/** | ||
* Gest maximum amount of time to wait for a socket | ||
* connection establishment with an individual server. | ||
* | ||
* @return timeout | ||
*/ | ||
public int getTimeout() { | ||
return timeout; | ||
} | ||
|
||
/** | ||
* Provides a decision on whether retries limit is hit. | ||
* | ||
* @param retries Current count of retries. | ||
* | ||
* @return {@code true} if retries are exhausted. | ||
*/ | ||
private boolean areRetriesExhausted(int retries) { | ||
int limit = getRetriesLimit(); | ||
if (limit < 0) { | ||
return false; | ||
} | ||
return retries >= limit; | ||
} | ||
} |
12 changes: 12 additions & 0 deletions
12
src/main/java/org/tarantool/RefreshableSocketProvider.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
package org.tarantool; | ||
|
||
import java.net.SocketAddress; | ||
import java.util.Collection; | ||
|
||
public interface RefreshableSocketProvider { | ||
|
||
Collection<SocketAddress> getAddresses(); | ||
|
||
void refreshAddresses(Collection<String> addresses); | ||
|
||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.