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_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_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_CONNECT_TIMEOUT = 5; //second
public static final int DEFAULT_NETWORK_TIMEOUT = 30; //second public static final int DEFAULT_NETWORK_TIMEOUT = 30; //second
public static final String DEFAULT_CHARSET = "UTF-8"; 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_IDLE_TIME = 3600 ;//second
public static final int DEFAULT_CONNECTION_POOL_MAX_WAIT_TIME_IN_MS = 1000 ;//millisecond 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_connect_timeout = DEFAULT_CONNECT_TIMEOUT * 1000; //millisecond
public static int g_network_timeout = DEFAULT_NETWORK_TIMEOUT * 1000; //millisecond public static int g_network_timeout = DEFAULT_NETWORK_TIMEOUT * 1000; //millisecond
public static String g_charset = DEFAULT_CHARSET; public static String g_charset = DEFAULT_CHARSET;
@ -145,13 +140,6 @@ public class ClientGlobal {
if (g_connection_pool_max_wait_time_in_ms < 0) { 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_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 poolMaxCountPerEntry = props.getProperty(PROP_KEY_CONNECTION_POOL_MAX_COUNT_PER_ENTRY);
String poolMaxIdleTime = props.getProperty(PROP_KEY_CONNECTION_POOL_MAX_IDLE_TIME); 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 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) { if (connectTimeoutInSecondsConf != null && connectTimeoutInSecondsConf.trim().length() != 0) {
g_connect_timeout = Integer.parseInt(connectTimeoutInSecondsConf.trim()) * 1000; g_connect_timeout = Integer.parseInt(connectTimeoutInSecondsConf.trim()) * 1000;
} }
@ -225,15 +211,6 @@ public class ClientGlobal {
if (poolMaxWaitTimeInMS != null && poolMaxWaitTimeInMS.trim().length() != 0) { if (poolMaxWaitTimeInMS != null && poolMaxWaitTimeInMS.trim().length() != 0) {
g_connection_pool_max_wait_time_in_ms = Integer.parseInt(poolMaxWaitTimeInMS); 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 { public Connection getConnection(TrackerServer trackerServer) throws IOException, MyException {
Connection connection = null; 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 { try {
if (trackerServer == null) { if (trackerServer == null) {
trackerServer = getTrackerServer(); trackerServer = getTrackerServer();
@ -84,42 +85,50 @@ public class TrackerClient {
connection = trackerServer.getConnection(); connection = trackerServer.getConnection();
} catch (IOException e) { } catch (IOException e) {
if (failOver) { if (failOver) {
System.err.println("default trackerServer get connection error, emsg:" + e.getMessage()); System.err.println("trackerServer get connection error, emsg:" + e.getMessage());
} else { } else {
throw e; throw e;
} }
} catch (MyException e) { } catch (MyException e) {
if (failOver) { if (failOver) {
System.err.println("default trackerServer get connection error, emsg:" + e.getMessage()); System.err.println("trackerServer get connection error, emsg:" + e.getMessage());
} else { } else {
throw e; throw e;
} }
} }
if (connection != null || !failOver) { if (connection != null && !failOver) {
return connection; return connection;
} }
int retryCount = 0; //do fail over
while (retryCount++ <= ClientGlobal.g_fail_over_retry_count) { if (length > 1) {
try { int currentIndex = trackerServer.getIndex();
trackerServer = getTrackerServer(); int failOverCount = 0;
if (trackerServer == null) { while (failOverCount < length - 1) {
throw new MyException("tracker server is empty!"); failOverCount++;
currentIndex++;
if (currentIndex >= length) {
currentIndex = 0;
} }
return trackerServer.getConnection(); try {
} catch (IOException e) { trackerServer = this.tracker_group.getTrackerServer(currentIndex);
if (retryCount <= ClientGlobal.g_fail_over_retry_count) { if (trackerServer == null) {
//allow retry ignore exception throw new MyException("tracker server is empty!");
System.err.println("retry trackerServer get connection error, get connection from next tracker, retryCount:" + retryCount + ",+ emsg:" + e.getMessage()); }
} else { return trackerServer.getConnection();
throw e; } catch (IOException e) {
} System.err.println("fail over trackerServer get connection error, failOverCount:" + failOverCount + "," + e.getMessage());
} catch (MyException e) { if (failOverCount == length - 1) {
if (retryCount <= ClientGlobal.g_fail_over_retry_count) { throw e;
System.err.println("trackerServer get connection error, get connection from next tracker, retryCount:" + retryCount + ", emsg:" + e.getMessage()); }
//allow retry ignore exception
} else { } catch (MyException e) {
throw e; System.err.println("fail over trackerServer get connection error, failOverCount:" + failOverCount + ", " + e.getMessage());
if (failOverCount == length - 1) {
throw e;
}
} }
} }
} }
return null; return null;

View File

@ -39,7 +39,7 @@ public class TrackerGroup {
* @return connected tracker server, null for fail * @return connected tracker server, null for fail
*/ */
public TrackerServer getTrackerServer(int serverIndex) throws IOException { 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 * return connected tracker server

View File

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

View File

@ -21,7 +21,4 @@ fastdfs.connection_pool.max_count_per_entry = 500
fastdfs.connection_pool.max_idle_time = 3600 fastdfs.connection_pool.max_idle_time = 3600
## Maximum waiting time when the maximum number of connections is reached, unit: millisecond, default value is 1000 ## 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 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

@ -11,6 +11,4 @@ tracker_server = 10.0.11.244:22122
connection_pool.enabled = true connection_pool.enabled = true
connection_pool.max_count_per_entry = 500 connection_pool.max_count_per_entry = 500
connection_pool.max_idle_time = 3600 connection_pool.max_idle_time = 3600
connection_pool.max_wait_time_in_ms = 1000 connection_pool.max_wait_time_in_ms = 1000
fail_over_retry_count = -1