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: unable to skip the unavailable tracker #81

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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 src/main/java/org/csource/common/MyException.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
*/
public class MyException extends Exception {
public MyException(String s, Exception e) {
super(s,e);
}

public MyException(String message) {
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/org/csource/fastdfs/TrackerGroup.java
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ public TrackerServer getTrackerServer() throws IOException {
}
}

return null;
throw new IOException("can not found available server, please check: " + tracker_servers);
}

public Object clone() {
Expand Down
18 changes: 15 additions & 3 deletions src/main/java/org/csource/fastdfs/TrackerServer.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@

package org.csource.fastdfs;

import org.csource.common.MyException;
import org.csource.fastdfs.pool.Connection;
import org.csource.fastdfs.pool.ConnectionPool;
import org.csource.fastdfs.pool.ConnectionFactory;
Expand All @@ -26,11 +25,12 @@ public class TrackerServer {
protected InetSocketAddress inetSockAddr;


public TrackerServer(InetSocketAddress inetSockAddr) throws IOException {
public TrackerServer(InetSocketAddress inetSockAddr) throws IOException{
this.inetSockAddr = inetSockAddr;
checkServerEnabled();
}

public Connection getConnection() throws MyException, IOException {
public Connection getConnection() throws IOException {
Connection connection;
if (ClientGlobal.g_connection_pool_enabled) {
connection = ConnectionPool.getConnection(this.inetSockAddr);
Expand All @@ -48,4 +48,16 @@ public InetSocketAddress getInetSocketAddress() {
return this.inetSockAddr;
}

/**
* check server enabled
* @throws IOException
*/
public void checkServerEnabled() throws IOException {
Connection connection = getConnection();
try {
connection.release();
}catch (Exception e){
//ignore
}
}
}
4 changes: 2 additions & 2 deletions src/main/java/org/csource/fastdfs/pool/ConnectionFactory.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,15 @@ public class ConnectionFactory {
* @return
* @throws IOException
*/
public static Connection create(InetSocketAddress socketAddress) throws MyException {
public static Connection create(InetSocketAddress socketAddress) throws IOException {
try {
Socket sock = new Socket();
sock.setReuseAddress(true);
sock.setSoTimeout(ClientGlobal.g_network_timeout);
sock.connect(socketAddress, ClientGlobal.g_connect_timeout);
return new Connection(sock, socketAddress);
} catch (Exception e) {
throw new MyException("connect to server " + socketAddress.getAddress().getHostAddress() + ":" + socketAddress.getPort() + " fail, emsg:" + e.getMessage());
throw new IOException("connect to server " + socketAddress.getAddress().getHostAddress() + ":" + socketAddress.getPort() + " fail, emsg:" + e.getMessage(), e);
}
}
}
6 changes: 3 additions & 3 deletions src/main/java/org/csource/fastdfs/pool/ConnectionManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ public ConnectionManager(InetSocketAddress socketAddress) {
this.inetSocketAddress = socketAddress;
}

public Connection getConnection() throws MyException {
public Connection getConnection() throws IOException {
lock.lock();
try {
Connection connection = null;
Expand Down Expand Up @@ -81,10 +81,10 @@ public Connection getConnection() throws MyException {
//wait single success
continue;
}
throw new MyException("connect to server " + inetSocketAddress.getAddress().getHostAddress() + ":" + inetSocketAddress.getPort() + " fail, wait_time > " + ClientGlobal.g_connection_pool_max_wait_time_in_ms + "ms");
throw new IOException("connect to server " + inetSocketAddress.getAddress().getHostAddress() + ":" + inetSocketAddress.getPort() + " fail, wait_time > " + ClientGlobal.g_connection_pool_max_wait_time_in_ms + "ms");
} catch (InterruptedException e) {
e.printStackTrace();
throw new MyException("connect to server " + inetSocketAddress.getAddress().getHostAddress() + ":" + inetSocketAddress.getPort() + " fail, emsg:" + e.getMessage());
throw new IOException("connect to server " + inetSocketAddress.getAddress().getHostAddress() + ":" + inetSocketAddress.getPort() + " fail, emsg:" + e.getMessage());
}
}
return connection;
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/org/csource/fastdfs/pool/ConnectionPool.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ public class ConnectionPool {
*/
private final static ConcurrentHashMap<String, ConnectionManager> CP = new ConcurrentHashMap<String, ConnectionManager>();

public static Connection getConnection(InetSocketAddress socketAddress) throws MyException {
public static Connection getConnection(InetSocketAddress socketAddress) throws IOException {
if (socketAddress == null) {
return null;
}
Expand Down