22 Commits
Author SHA1 Message Date
YuQing 42132c694c add @SuppressWarnings(unchecked) 2023-01-29 14:47:09 +08:00
YuQing 1b263e6436 add *.class to .gitignore 2023-01-29 14:16:17 +08:00
tanyawen01770 41655defd8 feature:add history 2023-01-29 11:46:55 +08:00
tanyawen01770 a1df79c50e fix:upgrade pom version 1.30 2023-01-29 09:49:29 +08:00
tanyawen01770 2b4693643a fix:not use code 2023-01-29 09:45:14 +08:00
tanyawen01770 4de15ae592 fix:fail over 2023-01-28 21:37:29 +08:00
tanyawen01770 0087216c0a feature: fail over from index 2023-01-28 19:53:06 +08:00
tanyawen01770 77946b2382 feature: set default failover size 2023-01-28 16:51:33 +08:00
tanyawen01770 0b2ec3aa10 fix:remove not use code 2023-01-28 15:27:19 +08:00
tanyawen01770 8d966af4a4 feature:add tracker server fail over 2023-01-28 15:02:40 +08:00
YuQing 9cc79ed75d Merge pull request #94 from niloay6/fix_configInfo_print
fix: 修复tracker_server配置为域名时,打印信息有误的问题
2021-12-10 15:52:15 +08:00
niloay6 8db4ff7dd0 fix: 修复tracker_server配置为域名时,打印信息有误的问题 2021-07-27 10:50:18 +08:00
YuQing 4987967e0b Merge pull request #88 from happyfish100/dependabot/maven/junit-junit-4.13.1
build(deps-dev): bump junit from 4.12 to 4.13.1
2021-04-20 21:48:12 +08:00
YuQing 56af1ae72f Merge pull request #91 from rui8832/master
feat: 日志依赖 log4j,具体实现由使用方决定
2021-04-20 21:47:11 +08:00
Alvin Zhang 987e9194ad feat: 日志依赖 log4j,具体实现由使用方决定
* 如 Spring Boot 默认使用了 Logback,与 slf4j-log4j12 同时存在时会冲突
2021-04-20 14:11:49 +08:00
dependabot[bot] 670909eed1 build(deps-dev): bump junit from 4.12 to 4.13.1
Bumps [junit](https://github.com/junit-team/junit4) from 4.12 to 4.13.1.
- [Release notes](https://github.com/junit-team/junit4/releases)
- [Changelog](https://github.com/junit-team/junit4/blob/main/doc/ReleaseNotes4.12.md)
- [Commits](https://github.com/junit-team/junit4/compare/r4.12...r4.13.1)

Signed-off-by: dependabot[bot] <support@github.com>
2020-10-13 14:34:35 +00:00
YuQing 1ba6fb7975 update version for README.md 2020-01-03 08:52:34 +08:00
YuQing aafd0b4b8c upgrade version to v1.29 2020-01-03 08:51:17 +08:00
YuQing 06f5624a8d Merge branch 'feature_add_connect_pool_20191205' 2020-01-01 14:45:04 +08:00
tanyawen 47d53c9a19 fix:active test flag 2019-12-31 15:33:48 +08:00
tanyawen bb13294205 fix:active test flag 2019-12-31 15:32:48 +08:00
tanyawen f742f3c6aa feature:add active test flag 2019-12-31 15:28:43 +08:00
16 changed files with 231 additions and 131 deletions
+1 -1
View File
@@ -12,6 +12,6 @@ target
*.iws
*.log
.idea
*.conf
*.PNG
*.class
+11
View File
@@ -1,3 +1,14 @@
Version 1.30 2023-01-29
* support tracker server fail over
If the tracker server is not specified, when the tracker server fails to
get the connection, it will try to get the connection from other tracker servers.
The maximum number of attempts is the number of tracker servers minus 1
Version 1.29 2020-01-03
* support active test for connection pool.
when the tracker / storage server restarts or the network burst failure
occurs, only one request will be sacrificed (failed), the rest of
the connections in the pool will be enabled to active test for validation.
Version 1.28 2019-12-30
* support connection pool. you can use connection pool or a short connection.
+1 -1
View File
@@ -26,7 +26,7 @@ mvn install:install-file -DgroupId=org.csource -DartifactId=fastdfs-client-java
<dependency>
<groupId>org.csource</groupId>
<artifactId>fastdfs-client-java</artifactId>
<version>1.28-SNAPSHOT</version>
<version>1.29-SNAPSHOT</version>
</dependency>
```
+11 -4
View File
@@ -4,7 +4,7 @@
<groupId>org.csource</groupId>
<artifactId>fastdfs-client-java</artifactId>
<version>1.28-SNAPSHOT</version>
<version>1.30-SNAPSHOT</version>
<name>fastdfs-client-java</name>
<description>fastdfs client for java</description>
<packaging>jar</packaging>
@@ -15,19 +15,26 @@
<maven.test.failure.ignore>true</maven.test.failure.ignore>
<maven.test.skip>true</maven.test.skip>
<jdk.version>1.6</jdk.version>
<slf4j.version>1.7.26</slf4j.version>
</properties>
<dependencies>
<dependency>
<artifactId>slf4j-api</artifactId>
<groupId>org.slf4j</groupId>
<version>${slf4j.version}</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
<version>1.7.26</version>
<version>${slf4j.version}</version>
<scope>provided</scope>
<optional>true</optional>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<version>4.13.1</version>
<scope>test</scope>
</dependency>
</dependencies>
@@ -155,13 +155,13 @@ public class IniFileReader {
} finally {
try {
if (in != null) in.close();
//System.out.println("loadFrom...finally...in.close(); done");
} catch (Exception ex) {
ex.printStackTrace();
}
}
}
@SuppressWarnings("unchecked")
private void readToParamTable(InputStream in) throws IOException {
this.paramTable = new Hashtable();
if (in == null) return;
@@ -206,7 +206,6 @@ public class IniFileReader {
try {
if (bufferedReader != null) bufferedReader.close();
if (inReader != null) inReader.close();
//System.out.println("readToParamTable...finally...bufferedReader.close();inReader.close(); done");
} catch (Exception ex) {
ex.printStackTrace();
}
@@ -48,7 +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 int DEFAULT_CONNECT_TIMEOUT = 5; //second
public static final int DEFAULT_NETWORK_TIMEOUT = 30; //second
public static final String DEFAULT_CHARSET = "UTF-8";
@@ -180,7 +179,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);
if (connectTimeoutInSecondsConf != null && connectTimeoutInSecondsConf.trim().length() != 0) {
g_connect_timeout = Integer.parseInt(connectTimeoutInSecondsConf.trim()) * 1000;
}
@@ -221,7 +219,7 @@ public class ClientGlobal {
* server之间用逗号','分隔
*/
public static void initByTrackers(String trackerServers) throws IOException, MyException {
List<InetSocketAddress> list = new ArrayList();
List<InetSocketAddress> list = new ArrayList<InetSocketAddress>();
String spr1 = ",";
String spr2 = ":";
String[] arr1 = trackerServers.trim().split(spr1);
@@ -337,7 +335,8 @@ public class ClientGlobal {
InetSocketAddress[] trackerAddresses = g_tracker_group.tracker_servers;
for (InetSocketAddress inetSocketAddress : trackerAddresses) {
if(trackerServers.length() > 0) trackerServers += ",";
trackerServers += inetSocketAddress.toString().substring(1);
String address = inetSocketAddress.toString();
trackerServers += address.startsWith("/") ? address.substring(1) : address;
}
}
return "{"
@@ -27,6 +27,7 @@ public class ProtoStructDecoder<T extends StructBase> {
/**
* decode byte buffer
*/
@SuppressWarnings("unchecked")
public T[] decode(byte[] bs, Class<T> clazz, int fieldsTotalSize) throws Exception {
if (bs.length % fieldsTotalSize != 0) {
throw new IOException("byte array length: " + bs.length + " is invalid!");
@@ -731,7 +731,6 @@ public class StorageClient {
try {
connection = this.storageServer.getConnection();
ext_name_bs = new byte[ProtoCommon.FDFS_FILE_EXT_NAME_MAX_LEN];
Arrays.fill(ext_name_bs, (byte) 0);
if (file_ext_name != null && file_ext_name.length() > 0) {
@@ -71,6 +71,71 @@ public class TrackerClient {
return this.getStoreStorage(trackerServer, groupName);
}
public Connection getConnection(TrackerServer trackerServer) throws IOException, MyException {
Connection connection = null;
int length = this.tracker_group.tracker_servers.length;
boolean failOver = length > 1 && trackerServer == null;
try {
if (trackerServer == null) {
trackerServer = getTrackerServer();
if (trackerServer == null) {
throw new MyException("tracker server is empty!");
}
}
connection = trackerServer.getConnection();
} catch (IOException e) {
if (failOver) {
System.err.println("trackerServer get connection error, emsg:" + e.getMessage());
} else {
throw e;
}
} catch (MyException e) {
if (failOver) {
System.err.println("trackerServer get connection error, emsg:" + e.getMessage());
} else {
throw e;
}
}
if (connection != null || !failOver) {
return connection;
}
//do fail over
int currentIndex = 0;
if (trackerServer != null) {
currentIndex = trackerServer.getIndex();
}
int failOverCount = 0;
while (failOverCount < length - 1) {
failOverCount++;
currentIndex++;
if (currentIndex >= length) {
currentIndex = 0;
}
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;
}
/**
* query storage server to upload file
*
@@ -85,12 +150,7 @@ public class TrackerClient {
byte cmd;
int out_len;
byte store_path;
Connection connection;
if (trackerServer == null) {
trackerServer = getTrackerServer();
}
connection = trackerServer.getConnection();
Connection connection = getConnection(trackerServer);
OutputStream out = connection.getOutputStream();
try {
@@ -170,16 +230,7 @@ public class TrackerClient {
int port;
byte cmd;
int out_len;
Connection connection;
if (trackerServer == null) {
trackerServer = getTrackerServer();
if (trackerServer == null) {
return null;
}
}
connection = trackerServer.getConnection();
Connection connection = getConnection(trackerServer);
OutputStream out = connection.getOutputStream();
try {
@@ -343,15 +394,7 @@ public class TrackerClient {
int len;
String ip_addr;
int port;
Connection connection;
if (trackerServer == null) {
trackerServer = getTrackerServer();
if (trackerServer == null) {
return null;
}
}
connection = trackerServer.getConnection();
Connection connection = getConnection(trackerServer);
OutputStream out = connection.getOutputStream();
try {
@@ -473,16 +516,7 @@ public class TrackerClient {
byte cmd;
int out_len;
byte store_path;
Connection connection;
if (trackerServer == null) {
trackerServer = getTrackerServer();
if (trackerServer == null) {
return null;
}
}
connection = trackerServer.getConnection();
Connection connection = getConnection(trackerServer);
OutputStream out = connection.getOutputStream();
try {
@@ -548,15 +582,7 @@ public class TrackerClient {
byte[] bGroupName;
byte[] bs;
int len;
Connection connection;
if (trackerServer == null) {
trackerServer = getTrackerServer();
if (trackerServer == null) {
return null;
}
}
connection = trackerServer.getConnection();
Connection connection = getConnection(trackerServer);
OutputStream out = connection.getOutputStream();
try {
@@ -18,84 +18,83 @@ import java.net.InetSocketAddress;
* @version Version 1.17
*/
public class TrackerGroup {
public int tracker_server_index;
public InetSocketAddress[] tracker_servers;
protected Integer lock;
public int tracker_server_index;
public InetSocketAddress[] tracker_servers;
protected Integer lock;
/**
* Constructor
*
* @param tracker_servers tracker servers
*/
public TrackerGroup(InetSocketAddress[] tracker_servers) {
this.tracker_servers = tracker_servers;
this.lock = new Integer(0);
this.tracker_server_index = 0;
}
/**
* return connected tracker server
*
* @return connected tracker server, null for fail
*/
public TrackerServer getTrackerServer(int serverIndex) throws IOException {
return new TrackerServer(this.tracker_servers[serverIndex]);
}
/**
* return connected tracker server
*
* @return connected tracker server, null for fail
*/
public TrackerServer getTrackerServer() throws IOException {
int current_index;
synchronized (this.lock) {
this.tracker_server_index++;
if (this.tracker_server_index >= this.tracker_servers.length) {
/**
* Constructor
*
* @param tracker_servers tracker servers
*/
public TrackerGroup(InetSocketAddress[] tracker_servers) {
this.tracker_servers = tracker_servers;
this.lock = new Integer(0);
this.tracker_server_index = 0;
}
current_index = this.tracker_server_index;
}
try {
return this.getTrackerServer(current_index);
} catch (IOException ex) {
System.err.println("connect to server " + this.tracker_servers[current_index].getAddress().getHostAddress() + ":" + this.tracker_servers[current_index].getPort() + " fail");
ex.printStackTrace(System.err);
/**
* return connected tracker server
*
* @return connected tracker server, null for fail
*/
public TrackerServer getTrackerServer(int serverIndex) throws IOException {
return new TrackerServer(this.tracker_servers[serverIndex], serverIndex);
}
for (int i = 0; i < this.tracker_servers.length; i++) {
if (i == current_index) {
continue;
}
try {
TrackerServer trackerServer = this.getTrackerServer(i);
/**
* return connected tracker server
*
* @return connected tracker server, null for fail
*/
public TrackerServer getTrackerServer() throws IOException {
int current_index;
synchronized (this.lock) {
if (this.tracker_server_index == current_index) {
this.tracker_server_index = i;
}
this.tracker_server_index++;
if (this.tracker_server_index >= this.tracker_servers.length) {
this.tracker_server_index = 0;
}
current_index = this.tracker_server_index;
}
return trackerServer;
} catch (IOException ex) {
System.err.println("connect to server " + this.tracker_servers[i].getAddress().getHostAddress() + ":" + this.tracker_servers[i].getPort() + " fail");
ex.printStackTrace(System.err);
}
try {
return this.getTrackerServer(current_index);
} catch (IOException ex) {
System.err.println("connect to server " + this.tracker_servers[current_index].getAddress().getHostAddress() + ":" + this.tracker_servers[current_index].getPort() + " fail");
ex.printStackTrace(System.err);
}
for (int i = 0; i < this.tracker_servers.length; i++) {
if (i == current_index) {
continue;
}
try {
TrackerServer trackerServer = this.getTrackerServer(i);
synchronized (this.lock) {
if (this.tracker_server_index == current_index) {
this.tracker_server_index = i;
}
}
return trackerServer;
} catch (IOException ex) {
System.err.println("connect to server " + this.tracker_servers[i].getAddress().getHostAddress() + ":" + this.tracker_servers[i].getPort() + " fail");
ex.printStackTrace(System.err);
}
}
return null;
}
return null;
}
public Object clone() {
InetSocketAddress[] trackerServers = new InetSocketAddress[this.tracker_servers.length];
for (int i = 0; i < trackerServers.length; i++) {
trackerServers[i] = new InetSocketAddress(this.tracker_servers[i].getAddress().getHostAddress(), this.tracker_servers[i].getPort());
}
public Object clone() {
InetSocketAddress[] trackerServers = new InetSocketAddress[this.tracker_servers.length];
for (int i = 0; i < trackerServers.length; i++) {
trackerServers[i] = new InetSocketAddress(this.tracker_servers[i].getAddress().getHostAddress(), this.tracker_servers[i].getPort());
return new TrackerGroup(trackerServers);
}
return new TrackerGroup(trackerServers);
}
}
@@ -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;
}
}
@@ -15,6 +15,8 @@ public class Connection {
private InetSocketAddress inetSockAddr;
private Long lastAccessTime = System.currentTimeMillis();
private boolean needActiveTest = false;
public Connection(Socket sock, InetSocketAddress inetSockAddr) {
this.sock = sock;
this.inetSockAddr = inetSockAddr;
@@ -117,11 +119,21 @@ public class Connection {
return false;
}
public boolean isNeedActiveTest() {
return needActiveTest;
}
public void setNeedActiveTest(boolean needActiveTest) {
this.needActiveTest = needActiveTest;
}
@Override
public String toString() {
return "TrackerServer{" +
return "Connection{" +
"sock=" + sock +
", inetSockAddr=" + inetSockAddr +
", lastAccessTime=" + lastAccessTime +
", needActiveTest=" + needActiveTest +
'}';
}
}
@@ -57,6 +57,21 @@ public class ConnectionManager {
closeConnection(connection);
continue;
}
if (connection.isNeedActiveTest()) {
boolean isActive = false;
try {
isActive = connection.activeTest();
} catch (IOException e) {
System.err.println("send to server[" + inetSocketAddress.getAddress().getHostAddress() + ":" + inetSocketAddress.getPort() + "] active test error ,emsg:" + e.getMessage());
isActive = false;
}
if (!isActive) {
closeConnection(connection);
continue;
} else {
connection.setNeedActiveTest(false);
}
}
} else if (ClientGlobal.g_connection_pool_max_count_per_entry == 0 || totalCount.get() < ClientGlobal.g_connection_pool_max_count_per_entry) {
connection = ConnectionFactory.create(this.inetSocketAddress);
totalCount.incrementAndGet();
@@ -107,6 +122,20 @@ public class ConnectionManager {
}
}
public void setActiveTestFlag() {
if (freeCount.get() > 0) {
lock.lock();
try {
for (Connection freeConnection : freeConnections) {
freeConnection.setNeedActiveTest(true);
}
} finally {
lock.unlock();
}
}
}
@Override
public String toString() {
return "ConnectionManager{" +
@@ -54,6 +54,7 @@ public class ConnectionPool {
ConnectionManager connectionManager = CP.get(key);
if (connectionManager != null) {
connectionManager.closeConnection(connection);
connectionManager.setActiveTestFlag();
} else {
connection.closeDirectly();
}
@@ -21,6 +21,9 @@ public class ClientGlobalTests {
ClientGlobal.initByProperties(props);
System.out.println("ClientGlobal.configInfo(): " + ClientGlobal.configInfo());
String trackerServer = "www.baidu.com:22122";
ClientGlobal.initByTrackers(trackerServer);
System.out.println("Host >>> ClientGlobal.configInfo() : " + ClientGlobal.configInfo());
}
}
@@ -87,9 +87,9 @@ public class FdfsTest {
@Test
public void download() throws Exception {
String[] uploadresult = {"group1", "M00/00/00/wKgBZV0phl2ASV1nAACk1tFxwrM3814331"};
String[] uploadresult = {"group1", "M00/00/00/J2fL12PVypeAWiGcAAM_gDeWVyw5817085"};
byte[] result = storageClient.download_file(uploadresult[0], uploadresult[1]);
String local_filename = "build.PNG";
String local_filename = "commitment.d2f57e10.jpg";
writeByteToFile(result, local_filename);
File file = new File(local_filename);
Assert.assertTrue(file.isFile());
@@ -98,17 +98,17 @@ public class FdfsTest {
@Test
public void testUploadDownload() throws Exception {
NameValuePair[] metaList = new NameValuePair[1];
String local_filename = "build.PNG";
String local_filename = "commitment.d2f57e10 (2).jpg";
metaList[0] = new NameValuePair("fileName", local_filename);
File file = new File("C:/Users/chengdu/Desktop/build.PNG");
File file = new File("/Users/iyw/Downloads/commitment.d2f57e10 (2).jpg");
InputStream inputStream = new FileInputStream(file);
int length = inputStream.available();
byte[] bytes = new byte[length];
inputStream.read(bytes);
String[] result = storageClient.upload_file(bytes, null, metaList);
Assert.assertTrue(storageClient.isConnected());
//Assert.assertTrue(storageClient.isConnected());
// pool testOnborrow isAvaliable
Assert.assertTrue(storageClient.isAvaliable());
// Assert.assertTrue(storageClient.isAvaliable());
LOGGER.info("result {}", Arrays.asList(result));
byte[] resultbytes = storageClient.download_file(result[0], result[1]);
writeByteToFile(resultbytes, local_filename);