feature: set default failover size
parent
0b2ec3aa10
commit
77946b2382
|
@ -63,7 +63,7 @@ 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 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
|
||||||
|
@ -77,7 +77,7 @@ public class ClientGlobal {
|
||||||
public static int g_connection_pool_max_idle_time = DEFAULT_CONNECTION_POOL_MAX_IDLE_TIME * 1000; //millisecond
|
public static int g_connection_pool_max_idle_time = DEFAULT_CONNECTION_POOL_MAX_IDLE_TIME * 1000; //millisecond
|
||||||
public static int g_connection_pool_max_wait_time_in_ms = DEFAULT_CONNECTION_POOL_MAX_WAIT_TIME_IN_MS; //millisecond
|
public static int g_connection_pool_max_wait_time_in_ms = DEFAULT_CONNECTION_POOL_MAX_WAIT_TIME_IN_MS; //millisecond
|
||||||
|
|
||||||
public static int g_fail_over_retry_count = DEFAULT_FAIL_OVER_RETRY_COUNT ;//get connection retry count when fail
|
public static int g_fail_over_retry_count = 0;//get connection retry count when fail
|
||||||
|
|
||||||
public static TrackerGroup g_tracker_group;
|
public static TrackerGroup g_tracker_group;
|
||||||
|
|
||||||
|
@ -146,6 +146,12 @@ public class ClientGlobal {
|
||||||
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);
|
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;
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -221,6 +227,12 @@ public class ClientGlobal {
|
||||||
}
|
}
|
||||||
if (failOverRetryCount != null && failOverRetryCount.trim().length() != 0) {
|
if (failOverRetryCount != null && failOverRetryCount.trim().length() != 0) {
|
||||||
g_fail_over_retry_count = Integer.parseInt(failOverRetryCount);
|
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;
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -72,39 +72,55 @@ public class TrackerClient {
|
||||||
}
|
}
|
||||||
|
|
||||||
public Connection getConnection(TrackerServer trackerServer) throws IOException, MyException {
|
public Connection getConnection(TrackerServer trackerServer) throws IOException, MyException {
|
||||||
if (ClientGlobal.g_fail_over_retry_count > 0) {
|
Connection connection = null;
|
||||||
int retryCount = 0;
|
boolean failOver = ClientGlobal.g_fail_over_retry_count > 0 && trackerServer == null;
|
||||||
do {
|
try {
|
||||||
try {
|
|
||||||
trackerServer = getTrackerServer();
|
|
||||||
if (trackerServer == null) {
|
|
||||||
throw new MyException("tracker server is empty!");
|
|
||||||
}
|
|
||||||
return trackerServer.getConnection();
|
|
||||||
} catch (IOException e) {
|
|
||||||
if (retryCount <= ClientGlobal.g_fail_over_retry_count) {
|
|
||||||
//allow retry ignore exception
|
|
||||||
System.err.println("trackerServer get connection error, get connection from next tracker, 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, emsg:" + e.getMessage());
|
|
||||||
//allow retry ignore exception
|
|
||||||
} else {
|
|
||||||
throw e;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
} while (retryCount++ <= ClientGlobal.g_fail_over_retry_count);
|
|
||||||
} else {
|
|
||||||
if (trackerServer == null) {
|
if (trackerServer == null) {
|
||||||
trackerServer = getTrackerServer();
|
trackerServer = getTrackerServer();
|
||||||
if (trackerServer == null) {
|
if (trackerServer == null) {
|
||||||
throw new MyException("tracker server is empty!");
|
throw new MyException("tracker server is empty!");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
trackerServer.getConnection();
|
connection = trackerServer.getConnection();
|
||||||
|
} catch (IOException e) {
|
||||||
|
if (failOver) {
|
||||||
|
System.err.println("default 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());
|
||||||
|
} else {
|
||||||
|
throw e;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
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!");
|
||||||
|
}
|
||||||
|
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;
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
|
@ -23,4 +23,5 @@ 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
|
||||||
|
|
||||||
fastdfs.fail_over_retry_count = 0
|
## The missing value is - 1, and the acquisition times are the number of tracker servers minus 1
|
||||||
|
fastdfs.fail_over_retry_count = -1
|
|
@ -13,4 +13,4 @@ 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
|
fail_over_retry_count = -1
|
|
@ -33,7 +33,7 @@ public class FdfsTest {
|
||||||
TrackerClient tracker = new TrackerClient();
|
TrackerClient tracker = new TrackerClient();
|
||||||
trackerServer = tracker.getTrackerServer();
|
trackerServer = tracker.getTrackerServer();
|
||||||
StorageServer storageServer = null;
|
StorageServer storageServer = null;
|
||||||
storageClient = new StorageClient(trackerServer, storageServer);
|
storageClient = new StorageClient(null, storageServer);
|
||||||
}
|
}
|
||||||
|
|
||||||
@After
|
@After
|
||||||
|
@ -106,9 +106,9 @@ public class FdfsTest {
|
||||||
byte[] bytes = new byte[length];
|
byte[] bytes = new byte[length];
|
||||||
inputStream.read(bytes);
|
inputStream.read(bytes);
|
||||||
String[] result = storageClient.upload_file(bytes, null, metaList);
|
String[] result = storageClient.upload_file(bytes, null, metaList);
|
||||||
Assert.assertTrue(storageClient.isConnected());
|
//Assert.assertTrue(storageClient.isConnected());
|
||||||
// pool testOnborrow isAvaliable
|
// pool testOnborrow isAvaliable
|
||||||
Assert.assertTrue(storageClient.isAvaliable());
|
// Assert.assertTrue(storageClient.isAvaliable());
|
||||||
LOGGER.info("result {}", Arrays.asList(result));
|
LOGGER.info("result {}", Arrays.asList(result));
|
||||||
byte[] resultbytes = storageClient.download_file(result[0], result[1]);
|
byte[] resultbytes = storageClient.download_file(result[0], result[1]);
|
||||||
writeByteToFile(resultbytes, local_filename);
|
writeByteToFile(resultbytes, local_filename);
|
||||||
|
|
Loading…
Reference in New Issue