@@ -42,7 +42,7 @@ public class ClientGlobal {
4242 public static final String PROP_KEY_HTTP_SECRET_KEY = "fastdfs.http_secret_key" ;
4343 public static final String PROP_KEY_HTTP_TRACKER_HTTP_PORT = "fastdfs.http_tracker_http_port" ;
4444 public static final String PROP_KEY_TRACKER_SERVERS = "fastdfs.tracker_servers" ;
45-
45+ public static final String PROP_KEY_CONNECT_FIRST_BY = "fastdfs.connect_first_by" ;
4646
4747 public static final String PROP_KEY_CONNECTION_POOL_ENABLED = "fastdfs.connection_pool.enabled" ;
4848 public static final String PROP_KEY_CONNECTION_POOL_MAX_COUNT_PER_ENTRY = "fastdfs.connection_pool.max_count_per_entry" ;
@@ -56,6 +56,9 @@ public class ClientGlobal {
5656 public static final String DEFAULT_HTTP_SECRET_KEY = "FastDFS1234567890" ;
5757 public static final int DEFAULT_HTTP_TRACKER_HTTP_PORT = 80 ;
5858
59+ public static final int CONNECT_FIRST_BY_TRACKER = 0 ;
60+ public static final int CONNECT_FIRST_BY_LAST_CONNECTED = 1 ;
61+
5962 public static final boolean DEFAULT_CONNECTION_POOL_ENABLED = true ;
6063 public static final int DEFAULT_CONNECTION_POOL_MAX_COUNT_PER_ENTRY = 100 ;
6164 public static final int DEFAULT_CONNECTION_POOL_MAX_IDLE_TIME = 3600 ;//second
@@ -67,6 +70,9 @@ public class ClientGlobal {
6770 public static boolean g_anti_steal_token = DEFAULT_HTTP_ANTI_STEAL_TOKEN ; //if anti-steal token
6871 public static String g_secret_key = DEFAULT_HTTP_SECRET_KEY ; //generage token secret key
6972 public static int g_tracker_http_port = DEFAULT_HTTP_TRACKER_HTTP_PORT ;
73+ public static int g_connect_first_by = CONNECT_FIRST_BY_TRACKER ;
74+ public static boolean g_multi_storage_ips = false ;
75+ public static StorageAddressMap g_storages_address_map ;
7076
7177 public static boolean g_connection_pool_enabled = DEFAULT_CONNECTION_POOL_ENABLED ;
7278 public static int g_connection_pool_max_count_per_entry = DEFAULT_CONNECTION_POOL_MAX_COUNT_PER_ENTRY ;
@@ -78,6 +84,76 @@ public class ClientGlobal {
7884 private ClientGlobal () {
7985 }
8086
87+ private static void loadStoragsFromTracker () throws IOException , MyException {
88+ TrackerClient tracker = new TrackerClient ();
89+ StringBuilder builder = tracker .fetchStorageIds ();
90+ if (builder .length () == 0 ) {
91+ return ;
92+ }
93+
94+ System .out .println (builder .toString ());
95+
96+ boolean without_port = true ;
97+ int count = 0 ;
98+ String [] lines = builder .toString ().split ("\n " );
99+ String [] ipAddresses = new String [lines .length ];
100+ for (String line : lines ) {
101+ String [] cols = line .split (" " );
102+ if (cols .length != 3 ) {
103+ throw new MyException ("invalid line: " + line );
104+ }
105+
106+ String ipAddrs = cols [2 ];
107+ if (ipAddrs .indexOf (',' ) > 0 ) {
108+ ipAddresses [count ++] = ipAddrs ;
109+ }
110+ }
111+
112+ if (count == 0 ) {
113+ return ;
114+ }
115+
116+ int startIndex ;
117+ if (ipAddresses [0 ].charAt (0 ) == '[' ) { //IPv6
118+ if ((startIndex =ipAddresses [0 ].indexOf (']' )) < 0 ) {
119+ throw new MyException ("invalid IPv6 address: " + ipAddresses [0 ]);
120+ }
121+ } else {
122+ startIndex = 0 ;
123+ }
124+ if (ipAddresses [0 ].indexOf (':' , startIndex ) > 0 ) {
125+ without_port = false ;
126+ }
127+
128+ g_multi_storage_ips = true ;
129+ g_storages_address_map = new StorageAddressMap (without_port );
130+ if (without_port ) {
131+ for (String ipAddr : ipAddresses ) {
132+ if (ipAddr .charAt (0 ) == '[' ) { //IPv6
133+ ipAddr = ipAddr .substring (1 , ipAddr .length () - 1 );
134+ }
135+ String [] cols = ipAddr .split ("," );
136+ g_storages_address_map .puts (cols [0 ], cols [1 ]);
137+ }
138+ } else {
139+ for (String ipPort : ipAddresses ) {
140+ int colonIndex = ipPort .lastIndexOf (':' );
141+ if (colonIndex < 0 ) {
142+ throw new MyException ("invalid ip and port: " + ipPort );
143+ }
144+
145+ String ipAddr = ipPort .substring (0 , colonIndex );
146+ int port = Integer .parseInt (ipPort .substring (colonIndex + 1 ));
147+
148+ if (ipAddr .charAt (0 ) == '[' ) { //IPv6
149+ ipAddr = ipAddr .substring (1 , ipAddr .length () - 1 );
150+ }
151+ String [] cols = ipAddr .split ("," );
152+ g_storages_address_map .puts (cols [0 ], cols [1 ], port );
153+ }
154+ }
155+ }
156+
81157 /**
82158 * load global variables
83159 *
@@ -114,11 +190,11 @@ public static void init(String conf_filename) throws IOException, MyException {
114190
115191 InetSocketAddress [] tracker_servers = new InetSocketAddress [szTrackerServers .length ];
116192 for (int i = 0 ; i < szTrackerServers .length ; i ++) {
117- if (szTrackerServers [i ].contains ("[" )){
193+ if (szTrackerServers [i ].contains ("[" )) {
118194 parts = new String [2 ];
119195 parts [0 ] = szTrackerServers [i ].substring (1 , szTrackerServers [i ].indexOf ("]" ));
120196 parts [1 ] = szTrackerServers [i ].substring (szTrackerServers [i ].lastIndexOf (":" ) + 1 );
121- }else {
197+ } else {
122198 parts = szTrackerServers [i ].split ("\\ :" , 2 );
123199 }
124200
@@ -130,6 +206,11 @@ public static void init(String conf_filename) throws IOException, MyException {
130206 }
131207 g_tracker_group = new TrackerGroup (tracker_servers );
132208
209+ String connect_first_by = iniReader .getStrValue ("connect_first_by" );
210+ if (connect_first_by != null && connect_first_by .equalsIgnoreCase ("last-connected" )) {
211+ g_connect_first_by = CONNECT_FIRST_BY_LAST_CONNECTED ;
212+ }
213+
133214 g_tracker_http_port = iniReader .getIntValue ("http.tracker_http_port" , 80 );
134215 g_anti_steal_token = iniReader .getBoolValue ("http.anti_steal_token" , false );
135216 if (g_anti_steal_token ) {
@@ -146,6 +227,8 @@ public static void init(String conf_filename) throws IOException, MyException {
146227 if (g_connection_pool_max_wait_time_in_ms < 0 ) {
147228 g_connection_pool_max_wait_time_in_ms = DEFAULT_CONNECTION_POOL_MAX_WAIT_TIME_IN_MS ;
148229 }
230+
231+ loadStoragsFromTracker ();
149232 }
150233
151234 /**
@@ -183,6 +266,8 @@ public static void initByProperties(Properties props) throws IOException, MyExce
183266 String httpAntiStealTokenConf = props .getProperty (PROP_KEY_HTTP_ANTI_STEAL_TOKEN );
184267 String httpSecretKeyConf = props .getProperty (PROP_KEY_HTTP_SECRET_KEY );
185268 String httpTrackerHttpPortConf = props .getProperty (PROP_KEY_HTTP_TRACKER_HTTP_PORT );
269+
270+ String connectFirstBy = props .getProperty (PROP_KEY_CONNECT_FIRST_BY );
186271 String poolEnabled = props .getProperty (PROP_KEY_CONNECTION_POOL_ENABLED );
187272 String poolMaxCountPerEntry = props .getProperty (PROP_KEY_CONNECTION_POOL_MAX_COUNT_PER_ENTRY );
188273 String poolMaxIdleTime = props .getProperty (PROP_KEY_CONNECTION_POOL_MAX_IDLE_TIME );
@@ -205,6 +290,10 @@ public static void initByProperties(Properties props) throws IOException, MyExce
205290 if (httpTrackerHttpPortConf != null && httpTrackerHttpPortConf .trim ().length () != 0 ) {
206291 g_tracker_http_port = Integer .parseInt (httpTrackerHttpPortConf );
207292 }
293+
294+ if (connectFirstBy != null && connectFirstBy .equalsIgnoreCase ("last-connected" )) {
295+ g_connect_first_by = CONNECT_FIRST_BY_LAST_CONNECTED ;
296+ }
208297 if (poolEnabled != null && poolEnabled .trim ().length () != 0 ) {
209298 g_connection_pool_enabled = Boolean .parseBoolean (poolEnabled );
210299 }
@@ -217,6 +306,8 @@ public static void initByProperties(Properties props) throws IOException, MyExce
217306 if (poolMaxWaitTimeInMS != null && poolMaxWaitTimeInMS .trim ().length () != 0 ) {
218307 g_connection_pool_max_wait_time_in_ms = Integer .parseInt (poolMaxWaitTimeInMS );
219308 }
309+
310+ loadStoragsFromTracker ();
220311 }
221312
222313 /**
@@ -360,6 +451,8 @@ public static String configInfo() {
360451 + "\n g_anti_steal_token = " + g_anti_steal_token
361452 + "\n g_secret_key = " + g_secret_key
362453 + "\n g_tracker_http_port = " + g_tracker_http_port
454+ + "\n g_multi_storage_ips = " + g_multi_storage_ips
455+ + "\n g_connect_first_by = " + (g_connect_first_by == CONNECT_FIRST_BY_TRACKER ? "tracker" : "last-connected" )
363456 + "\n g_connection_pool_enabled = " + g_connection_pool_enabled
364457 + "\n g_connection_pool_max_count_per_entry = " + g_connection_pool_max_count_per_entry
365458 + "\n g_connection_pool_max_idle_time(ms) = " + g_connection_pool_max_idle_time
0 commit comments