From 0087216c0addc06811252f24ee2f85b424a6d244 Mon Sep 17 00:00:00 2001 From: tanyawen01770 Date: Sat, 28 Jan 2023 19:53:06 +0800 Subject: [PATCH] feature: fail over from index --- .../org/csource/fastdfs/ClientGlobal.java | 23 -------- .../org/csource/fastdfs/TrackerClient.java | 57 +++++++++++-------- .../org/csource/fastdfs/TrackerGroup.java | 2 +- .../org/csource/fastdfs/TrackerServer.java | 14 +++++ .../fastdfs-client.properties.sample | 5 +- src/main/resources/fdfs_client.conf.sample | 4 +- 6 files changed, 50 insertions(+), 55 deletions(-) diff --git a/src/main/java/org/csource/fastdfs/ClientGlobal.java b/src/main/java/org/csource/fastdfs/ClientGlobal.java index 9d36104..e8377c5 100644 --- a/src/main/java/org/csource/fastdfs/ClientGlobal.java +++ b/src/main/java/org/csource/fastdfs/ClientGlobal.java @@ -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; - } - } - } } /** diff --git a/src/main/java/org/csource/fastdfs/TrackerClient.java b/src/main/java/org/csource/fastdfs/TrackerClient.java index c11f131..b8fe088 100644 --- a/src/main/java/org/csource/fastdfs/TrackerClient.java +++ b/src/main/java/org/csource/fastdfs/TrackerClient.java @@ -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; diff --git a/src/main/java/org/csource/fastdfs/TrackerGroup.java b/src/main/java/org/csource/fastdfs/TrackerGroup.java index d9ae475..b79f706 100644 --- a/src/main/java/org/csource/fastdfs/TrackerGroup.java +++ b/src/main/java/org/csource/fastdfs/TrackerGroup.java @@ -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 diff --git a/src/main/java/org/csource/fastdfs/TrackerServer.java b/src/main/java/org/csource/fastdfs/TrackerServer.java index a9875ed..6ff7834 100644 --- a/src/main/java/org/csource/fastdfs/TrackerServer.java +++ b/src/main/java/org/csource/fastdfs/TrackerServer.java @@ -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; + } } diff --git a/src/main/resources/fastdfs-client.properties.sample b/src/main/resources/fastdfs-client.properties.sample index 72d1a4a..dd33b50 100644 --- a/src/main/resources/fastdfs-client.properties.sample +++ b/src/main/resources/fastdfs-client.properties.sample @@ -21,7 +21,4 @@ fastdfs.connection_pool.max_count_per_entry = 500 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 \ No newline at end of file +fastdfs.connection_pool.max_wait_time_in_ms = 1000 \ No newline at end of file diff --git a/src/main/resources/fdfs_client.conf.sample b/src/main/resources/fdfs_client.conf.sample index e7df694..741218c 100644 --- a/src/main/resources/fdfs_client.conf.sample +++ b/src/main/resources/fdfs_client.conf.sample @@ -11,6 +11,4 @@ tracker_server = 10.0.11.244:22122 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 \ No newline at end of file +connection_pool.max_wait_time_in_ms = 1000 \ No newline at end of file