feature: fail over from index

master
tanyawen01770 2023-01-28 19:53:06 +08:00
parent 77946b2382
commit 0087216c0a
6 changed files with 50 additions and 55 deletions

View File

@ -48,9 +48,6 @@ public class ClientGlobal {
public static final String PROP_KEY_CONNECTION_POOL_MAX_IDLE_TIME = "fastdfs.connection_pool.max_idle_time";
public static final String PROP_KEY_CONNECTION_POOL_MAX_WAIT_TIME_IN_MS = "fastdfs.connection_pool.max_wait_time_in_ms";
public static final String PROP_KEY_FAIL_OVER_RETRY_COUNT = "fastdfs.fail_over_retry_count";
public static final int DEFAULT_CONNECT_TIMEOUT = 5; //second
public static final int DEFAULT_NETWORK_TIMEOUT = 30; //second
public static final String DEFAULT_CHARSET = "UTF-8";
@ -63,8 +60,6 @@ public class ClientGlobal {
public static final int DEFAULT_CONNECTION_POOL_MAX_IDLE_TIME = 3600 ;//second
public static final int DEFAULT_CONNECTION_POOL_MAX_WAIT_TIME_IN_MS = 1000 ;//millisecond
public static final int DEFAULT_FAIL_OVER_RETRY_COUNT = -1;
public static int g_connect_timeout = DEFAULT_CONNECT_TIMEOUT * 1000; //millisecond
public static int g_network_timeout = DEFAULT_NETWORK_TIMEOUT * 1000; //millisecond
public static String g_charset = DEFAULT_CHARSET;
@ -145,13 +140,6 @@ public class ClientGlobal {
if (g_connection_pool_max_wait_time_in_ms < 0) {
g_connection_pool_max_wait_time_in_ms = DEFAULT_CONNECTION_POOL_MAX_WAIT_TIME_IN_MS;
}
g_fail_over_retry_count = iniReader.getIntValue("fail_over_retry_count", DEFAULT_FAIL_OVER_RETRY_COUNT);
if (g_fail_over_retry_count == DEFAULT_FAIL_OVER_RETRY_COUNT) {
//缺省值为tracker server数量 -1
if (tracker_servers.length > 1) {
g_fail_over_retry_count = tracker_servers.length -1;
}
}
}
/**
@ -193,8 +181,6 @@ public class ClientGlobal {
String poolMaxCountPerEntry = props.getProperty(PROP_KEY_CONNECTION_POOL_MAX_COUNT_PER_ENTRY);
String poolMaxIdleTime = props.getProperty(PROP_KEY_CONNECTION_POOL_MAX_IDLE_TIME);
String poolMaxWaitTimeInMS = props.getProperty(PROP_KEY_CONNECTION_POOL_MAX_WAIT_TIME_IN_MS);
String failOverRetryCount = props.getProperty(PROP_KEY_FAIL_OVER_RETRY_COUNT);
if (connectTimeoutInSecondsConf != null && connectTimeoutInSecondsConf.trim().length() != 0) {
g_connect_timeout = Integer.parseInt(connectTimeoutInSecondsConf.trim()) * 1000;
}
@ -225,15 +211,6 @@ public class ClientGlobal {
if (poolMaxWaitTimeInMS != null && poolMaxWaitTimeInMS.trim().length() != 0) {
g_connection_pool_max_wait_time_in_ms = Integer.parseInt(poolMaxWaitTimeInMS);
}
if (failOverRetryCount != null && failOverRetryCount.trim().length() != 0) {
g_fail_over_retry_count = Integer.parseInt(failOverRetryCount);
if(g_fail_over_retry_count == DEFAULT_FAIL_OVER_RETRY_COUNT) {
int trackerLength = g_tracker_group.tracker_servers.length;
if (trackerLength > 1) {
g_fail_over_retry_count = trackerLength - 1;
}
}
}
}
/**

View File

@ -73,7 +73,8 @@ public class TrackerClient {
public Connection getConnection(TrackerServer trackerServer) throws IOException, MyException {
Connection connection = null;
boolean failOver = ClientGlobal.g_fail_over_retry_count > 0 && trackerServer == null;
int length = this.tracker_group.tracker_servers.length;
boolean failOver = length > 1 && trackerServer == null;
try {
if (trackerServer == null) {
trackerServer = getTrackerServer();
@ -84,42 +85,50 @@ public class TrackerClient {
connection = trackerServer.getConnection();
} catch (IOException e) {
if (failOver) {
System.err.println("default trackerServer get connection error, emsg:" + e.getMessage());
System.err.println("trackerServer get connection error, emsg:" + e.getMessage());
} else {
throw e;
}
} catch (MyException e) {
if (failOver) {
System.err.println("default trackerServer get connection error, emsg:" + e.getMessage());
System.err.println("trackerServer get connection error, emsg:" + e.getMessage());
} else {
throw e;
}
}
if (connection != null || !failOver) {
if (connection != null && !failOver) {
return connection;
}
int retryCount = 0;
while (retryCount++ <= ClientGlobal.g_fail_over_retry_count) {
try {
trackerServer = getTrackerServer();
if (trackerServer == null) {
throw new MyException("tracker server is empty!");
//do fail over
if (length > 1) {
int currentIndex = trackerServer.getIndex();
int failOverCount = 0;
while (failOverCount < length - 1) {
failOverCount++;
currentIndex++;
if (currentIndex >= length) {
currentIndex = 0;
}
return trackerServer.getConnection();
} catch (IOException e) {
if (retryCount <= ClientGlobal.g_fail_over_retry_count) {
//allow retry ignore exception
System.err.println("retry trackerServer get connection error, get connection from next tracker, retryCount:" + retryCount + ",+ emsg:" + e.getMessage());
} else {
throw e;
}
} catch (MyException e) {
if (retryCount <= ClientGlobal.g_fail_over_retry_count) {
System.err.println("trackerServer get connection error, get connection from next tracker, retryCount:" + retryCount + ", emsg:" + e.getMessage());
//allow retry ignore exception
} else {
throw e;
try {
trackerServer = this.tracker_group.getTrackerServer(currentIndex);
if (trackerServer == null) {
throw new MyException("tracker server is empty!");
}
return trackerServer.getConnection();
} catch (IOException e) {
System.err.println("fail over trackerServer get connection error, failOverCount:" + failOverCount + "," + e.getMessage());
if (failOverCount == length - 1) {
throw e;
}
} catch (MyException e) {
System.err.println("fail over trackerServer get connection error, failOverCount:" + failOverCount + ", " + e.getMessage());
if (failOverCount == length - 1) {
throw e;
}
}
}
}
return null;

View File

@ -39,7 +39,7 @@ public class TrackerGroup {
* @return connected tracker server, null for fail
*/
public TrackerServer getTrackerServer(int serverIndex) throws IOException {
return new TrackerServer(this.tracker_servers[serverIndex]);
return new TrackerServer(this.tracker_servers[serverIndex], serverIndex);
}
/**
* return connected tracker server

View File

@ -25,11 +25,18 @@ import java.net.InetSocketAddress;
public class TrackerServer {
protected InetSocketAddress inetSockAddr;
protected int index;
public TrackerServer(InetSocketAddress inetSockAddr) throws IOException {
this.inetSockAddr = inetSockAddr;
}
public TrackerServer(InetSocketAddress inetSockAddr, int index) {
this.inetSockAddr = inetSockAddr;
this.index = index;
}
public Connection getConnection() throws MyException, IOException {
Connection connection;
if (ClientGlobal.g_connection_pool_enabled) {
@ -48,4 +55,11 @@ public class TrackerServer {
return this.inetSockAddr;
}
public int getIndex() {
return index;
}
public void setIndex(int index) {
this.index = index;
}
}

View File

@ -22,6 +22,3 @@ fastdfs.connection_pool.max_idle_time = 3600
## Maximum waiting time when the maximum number of connections is reached, unit: millisecond, default value is 1000
fastdfs.connection_pool.max_wait_time_in_ms = 1000
## The missing value is - 1, and the acquisition times are the number of tracker servers minus 1
fastdfs.fail_over_retry_count = -1

View File

@ -12,5 +12,3 @@ connection_pool.enabled = true
connection_pool.max_count_per_entry = 500
connection_pool.max_idle_time = 3600
connection_pool.max_wait_time_in_ms = 1000
fail_over_retry_count = -1