feature:add connection pool v2

dev
tanyawen 2019-12-29 03:28:26 +08:00
parent 1336b2b07f
commit 32167310dd
24 changed files with 3451 additions and 3670 deletions

View File

@ -19,5 +19,5 @@ fastdfs.connection_pool.max_count_per_entry = 100
## connections whose the idle time exceeds this time will be closed, unit: second,default value is 60
fastdfs.connection_pool.max_idle_time = 60
## Maximum waiting time when the maximum number of connections is reached, unit: second, default value is 5
fastdfs.connection_pool.max_wait_time = 5
## Maximum waiting time when the maximum number of connections is reached, unit: Millisecond, default value is 5000
fastdfs.connection_pool.max_wait_time_in_ms = 5000

View File

@ -9,7 +9,7 @@ tracker_server = 10.0.11.247:22122
tracker_server = 10.0.11.248:22122
tracker_server = 10.0.11.249:22122
connection_pool.enabled = false
connection_pool.enabled = true
connection_pool.max_count_per_entry = 100
connection_pool.max_idle_time = 60
connection_pool.max_wait_time = 5
connection_pool.max_wait_time_in_ms = 5000

View File

@ -46,7 +46,7 @@ public class ClientGlobal {
public static final String PROP_KEY_CONNECTION_POOL_ENABLED = "fastdfs.connection_pool.enabled";
public static final String PROP_KEY_CONNECTION_POOL_MAX_COUNT_PER_ENTRY = "fastdfs.connection_pool.max_count_per_entry";
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 = "fastdfs.connection_pool.max_wait_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
@ -56,10 +56,10 @@ public class ClientGlobal {
public static final String DEFAULT_HTTP_SECRET_KEY = "FastDFS1234567890";
public static final int DEFAULT_HTTP_TRACKER_HTTP_PORT = 80;
public static final boolean DEFAULT_CONNECTION_POOL_ENABLED = true;
public static final boolean DEFAULT_CONNECTION_POOL_ENABLED = false;
public static final int DEFAULT_CONNECTION_POOL_MAX_COUNT_PER_ENTRY = 100;
public static final int DEFAULT_CONNECTION_POOL_MAX_IDLE_TIME = 60 ;//second
public static final int DEFAULT_CONNECTION_POOL_MAX_WAIT_TIME = 5 ;//second
public static final int DEFAULT_CONNECTION_POOL_MAX_WAIT_TIME_IN_MS = 5000 ;//millisecond
public static int g_connect_timeout = DEFAULT_CONNECT_TIMEOUT * 1000; //millisecond
public static int g_network_timeout = DEFAULT_NETWORK_TIMEOUT * 1000; //millisecond
@ -71,7 +71,7 @@ public class ClientGlobal {
public static boolean g_connection_pool_enabled = DEFAULT_CONNECTION_POOL_ENABLED;
public static int g_connection_pool_max_count_per_entry = DEFAULT_CONNECTION_POOL_MAX_COUNT_PER_ENTRY;
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 = DEFAULT_CONNECTION_POOL_MAX_WAIT_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 TrackerGroup g_tracker_group;
@ -135,11 +135,10 @@ public class ClientGlobal {
g_connection_pool_max_idle_time = DEFAULT_CONNECTION_POOL_MAX_IDLE_TIME;
}
g_connection_pool_max_idle_time *= 1000;
g_connection_pool_max_wait_time = iniReader.getIntValue("connection_pool.max_wait_time", DEFAULT_CONNECTION_POOL_MAX_WAIT_TIME);
if (g_connection_pool_max_wait_time < 0) {
g_connection_pool_max_wait_time = DEFAULT_CONNECTION_POOL_MAX_WAIT_TIME;
g_connection_pool_max_wait_time_in_ms = iniReader.getIntValue("connection_pool.max_wait_time_in_ms", DEFAULT_CONNECTION_POOL_MAX_WAIT_TIME_IN_MS);
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 *= 1000;
}
/**
@ -180,7 +179,7 @@ public class ClientGlobal {
String poolEnabled = props.getProperty(PROP_KEY_CONNECTION_POOL_ENABLED);
String poolMaxCountPerEntry = props.getProperty(PROP_KEY_CONNECTION_POOL_MAX_COUNT_PER_ENTRY);
String poolMaxIdleTime = props.getProperty(PROP_KEY_CONNECTION_POOL_MAX_IDLE_TIME);
String poolMaxWaitTime = props.getProperty(PROP_KEY_CONNECTION_POOL_MAX_WAIT_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;
@ -209,8 +208,8 @@ public class ClientGlobal {
if (poolMaxIdleTime != null && poolMaxIdleTime.trim().length() != 0) {
g_connection_pool_max_idle_time = Integer.parseInt(poolMaxIdleTime) * 1000;
}
if (poolMaxWaitTime != null && poolMaxWaitTime.trim().length() != 0) {
g_connection_pool_max_wait_time = Integer.parseInt(poolMaxWaitTime) * 1000;
if (poolMaxWaitTimeInMS != null && poolMaxWaitTimeInMS.trim().length() != 0) {
g_connection_pool_max_wait_time_in_ms = Integer.parseInt(poolMaxWaitTimeInMS) * 1000;
}
}
@ -340,8 +339,8 @@ public class ClientGlobal {
return g_connection_pool_max_idle_time;
}
public static int getG_connection_pool_max_wait_time() {
return g_connection_pool_max_wait_time;
public static int getG_connection_pool_max_wait_time_in_ms() {
return g_connection_pool_max_wait_time_in_ms;
}
public static String configInfo() {
@ -363,7 +362,7 @@ public class ClientGlobal {
+ "\n g_connection_pool_enabled = " + g_connection_pool_enabled
+ "\n g_connection_pool_max_count_per_entry = " + g_connection_pool_max_count_per_entry
+ "\n g_connection_pool_max_idle_time = " + g_connection_pool_max_idle_time
+ "\n g_connection_pool_max_wait_time = " + g_connection_pool_max_wait_time
+ "\n g_connection_pool_max_wait_time_in_ms = " + g_connection_pool_max_wait_time_in_ms
+ "\n trackerServers = " + trackerServers
+ "\n}";
}

View File

@ -11,6 +11,8 @@ package org.csource.fastdfs;
import org.csource.common.Base64;
import org.csource.common.MyException;
import org.csource.common.NameValuePair;
import org.csource.fastdfs.pool.Connection;
import org.omg.CORBA.CODESET_INCOMPATIBLE;
import java.io.*;
import java.net.Socket;
@ -623,8 +625,8 @@ public class StorageClient {
*/
public String[] regenerate_appender_filename(String group_name, String appender_filename) throws IOException, MyException {
byte[] header;
boolean bNewConnection;
Socket storageSocket;
boolean bNewStorageServer;
Connection connection = null;
byte[] hexLenBytes;
byte[] appenderFilenameBytes;
int offset;
@ -636,10 +638,10 @@ public class StorageClient {
return null;
}
bNewConnection = this.newUpdatableStorageConnection(group_name, appender_filename);
bNewStorageServer = this.newUpdatableStorageConnection(group_name, appender_filename);
try {
storageSocket = this.storageServer.getSocket();
connection = this.storageServer.getConnection();
appenderFilenameBytes = appender_filename.getBytes(ClientGlobal.g_charset);
body_len = appenderFilenameBytes.length;
@ -652,10 +654,10 @@ public class StorageClient {
System.arraycopy(appenderFilenameBytes, 0, wholePkg, offset, appenderFilenameBytes.length);
offset += appenderFilenameBytes.length;
OutputStream out = storageSocket.getOutputStream();
OutputStream out = connection.getOutputStream();
out.write(wholePkg);
ProtoCommon.RecvPackageInfo pkgInfo = ProtoCommon.recvPackage(storageSocket.getInputStream(),
ProtoCommon.RecvPackageInfo pkgInfo = ProtoCommon.recvPackage(connection.getInputStream(),
ProtoCommon.STORAGE_PROTO_CMD_RESP, -1);
this.errno = pkgInfo.errno;
if (pkgInfo.errno != 0) {
@ -675,27 +677,16 @@ public class StorageClient {
return results;
} catch (IOException ex) {
if (!bNewConnection) {
try {
this.storageServer.close();
this.storageServer.close(connection);
} catch (IOException ex1) {
ex1.printStackTrace();
} finally {
this.storageServer = null;
connection = null;
}
}
throw ex;
} finally {
if (bNewConnection) {
try {
this.storageServer.close();
} catch (IOException ex1) {
ex1.printStackTrace();
} finally {
this.storageServer = null;
}
}
doFinally(connection, bNewStorageServer);
}
}
@ -722,8 +713,8 @@ public class StorageClient {
byte[] ext_name_bs;
String new_group_name;
String remote_filename;
boolean bNewConnection;
Socket storageSocket;
boolean bNewStorageServer;
Connection connection = null;
byte[] sizeBytes;
byte[] hexLenBytes;
byte[] masterFilenameBytes;
@ -735,13 +726,13 @@ public class StorageClient {
(master_filename != null && master_filename.length() > 0) &&
(prefix_name != null));
if (bUploadSlave) {
bNewConnection = this.newUpdatableStorageConnection(group_name, master_filename);
bNewStorageServer = this.newUpdatableStorageConnection(group_name, master_filename);
} else {
bNewConnection = this.newWritableStorageConnection(group_name);
bNewStorageServer = this.newWritableStorageConnection(group_name);
}
try {
storageSocket = this.storageServer.getSocket();
connection = this.storageServer.getConnection();
ext_name_bs = new byte[ProtoCommon.FDFS_FILE_EXT_NAME_MAX_LEN];
Arrays.fill(ext_name_bs, (byte) 0);
@ -776,7 +767,7 @@ public class StorageClient {
hexLenBytes = ProtoCommon.long2buff(file_size);
System.arraycopy(hexLenBytes, 0, sizeBytes, offset, hexLenBytes.length);
OutputStream out = storageSocket.getOutputStream();
OutputStream out = connection.getOutputStream();
header = ProtoCommon.packHeader(cmd, body_len, (byte) 0);
byte[] wholePkg = new byte[(int) (header.length + body_len - file_size)];
System.arraycopy(header, 0, wholePkg, 0, header.length);
@ -812,7 +803,7 @@ public class StorageClient {
return null;
}
ProtoCommon.RecvPackageInfo pkgInfo = ProtoCommon.recvPackage(storageSocket.getInputStream(),
ProtoCommon.RecvPackageInfo pkgInfo = ProtoCommon.recvPackage(connection.getInputStream(),
ProtoCommon.STORAGE_PROTO_CMD_RESP, -1);
this.errno = pkgInfo.errno;
if (pkgInfo.errno != 0) {
@ -847,30 +838,19 @@ public class StorageClient {
return null;
}
}
return results;
} catch (IOException ex) {
if (!bNewConnection) {
try {
this.storageServer.close();
this.storageServer.close(connection);
} catch (IOException ex1) {
ex1.printStackTrace();
} finally {
this.storageServer = null;
connection = null;
}
}
throw ex;
} finally {
if (bNewConnection) {
try {
this.storageServer.close();
} catch (IOException ex1) {
ex1.printStackTrace();
} finally {
this.storageServer = null;
}
}
doFinally(connection, bNewStorageServer);
}
}
@ -886,8 +866,8 @@ public class StorageClient {
protected int do_append_file(String group_name, String appender_filename,
long file_size, UploadCallback callback) throws IOException, MyException {
byte[] header;
boolean bNewConnection;
Socket storageSocket;
boolean bNewStorageServer;
Connection connection = null;
byte[] hexLenBytes;
byte[] appenderFilenameBytes;
int offset;
@ -899,10 +879,10 @@ public class StorageClient {
return this.errno;
}
bNewConnection = this.newUpdatableStorageConnection(group_name, appender_filename);
bNewStorageServer = this.newUpdatableStorageConnection(group_name, appender_filename);
try {
storageSocket = this.storageServer.getSocket();
connection = this.storageServer.getConnection();
appenderFilenameBytes = appender_filename.getBytes(ClientGlobal.g_charset);
body_len = 2 * ProtoCommon.FDFS_PROTO_PKG_LEN_SIZE + appenderFilenameBytes.length + file_size;
@ -920,7 +900,7 @@ public class StorageClient {
System.arraycopy(hexLenBytes, 0, wholePkg, offset, hexLenBytes.length);
offset += hexLenBytes.length;
OutputStream out = storageSocket.getOutputStream();
OutputStream out = connection.getOutputStream();
System.arraycopy(appenderFilenameBytes, 0, wholePkg, offset, appenderFilenameBytes.length);
offset += appenderFilenameBytes.length;
@ -930,7 +910,7 @@ public class StorageClient {
return this.errno;
}
ProtoCommon.RecvPackageInfo pkgInfo = ProtoCommon.recvPackage(storageSocket.getInputStream(),
ProtoCommon.RecvPackageInfo pkgInfo = ProtoCommon.recvPackage(connection.getInputStream(),
ProtoCommon.STORAGE_PROTO_CMD_RESP, 0);
this.errno = pkgInfo.errno;
if (pkgInfo.errno != 0) {
@ -939,29 +919,32 @@ public class StorageClient {
return 0;
} catch (IOException ex) {
if (!bNewConnection) {
try {
this.storageServer.close();
this.storageServer.close(connection);
} catch (IOException ex1) {
ex1.printStackTrace();
} finally {
this.storageServer = null;
connection = null;
}
throw ex;
} finally {
doFinally(connection, bNewStorageServer);
}
}
throw ex;
} finally {
if (bNewConnection) {
private void doFinally(Connection connection, boolean bNewStorageServer) {
try {
this.storageServer.close();
if (connection != null) {
this.storageServer.releaseConnection(connection);
}
} catch (IOException ex1) {
ex1.printStackTrace();
} finally {
if (bNewStorageServer) {
this.storageServer = null;
}
}
}
}
/**
* modify appender file to storage server
@ -976,8 +959,8 @@ public class StorageClient {
protected int do_modify_file(String group_name, String appender_filename,
long file_offset, long modify_size, UploadCallback callback) throws IOException, MyException {
byte[] header;
boolean bNewConnection;
Socket storageSocket;
boolean bNewStorageServer;
Connection connection = null;
byte[] hexLenBytes;
byte[] appenderFilenameBytes;
int offset;
@ -989,10 +972,10 @@ public class StorageClient {
return this.errno;
}
bNewConnection = this.newUpdatableStorageConnection(group_name, appender_filename);
bNewStorageServer = this.newUpdatableStorageConnection(group_name, appender_filename);
try {
storageSocket = this.storageServer.getSocket();
connection = this.storageServer.getConnection();
appenderFilenameBytes = appender_filename.getBytes(ClientGlobal.g_charset);
body_len = 3 * ProtoCommon.FDFS_PROTO_PKG_LEN_SIZE + appenderFilenameBytes.length + modify_size;
@ -1014,7 +997,7 @@ public class StorageClient {
System.arraycopy(hexLenBytes, 0, wholePkg, offset, hexLenBytes.length);
offset += hexLenBytes.length;
OutputStream out = storageSocket.getOutputStream();
OutputStream out = connection.getOutputStream();
System.arraycopy(appenderFilenameBytes, 0, wholePkg, offset, appenderFilenameBytes.length);
offset += appenderFilenameBytes.length;
@ -1024,7 +1007,7 @@ public class StorageClient {
return this.errno;
}
ProtoCommon.RecvPackageInfo pkgInfo = ProtoCommon.recvPackage(storageSocket.getInputStream(),
ProtoCommon.RecvPackageInfo pkgInfo = ProtoCommon.recvPackage(connection.getInputStream(),
ProtoCommon.STORAGE_PROTO_CMD_RESP, 0);
this.errno = pkgInfo.errno;
if (pkgInfo.errno != 0) {
@ -1033,27 +1016,17 @@ public class StorageClient {
return 0;
} catch (IOException ex) {
if (!bNewConnection) {
try {
this.storageServer.close();
this.storageServer.close(connection);
} catch (IOException ex1) {
ex1.printStackTrace();
} finally {
this.storageServer = null;
}
connection = null;
}
throw ex;
} finally {
if (bNewConnection) {
try {
this.storageServer.close();
} catch (IOException ex1) {
ex1.printStackTrace();
} finally {
this.storageServer = null;
}
}
doFinally(connection, bNewStorageServer);
}
}
@ -1065,38 +1038,27 @@ public class StorageClient {
* @return 0 for success, none zero for fail (error code)
*/
public int delete_file(String group_name, String remote_filename) throws IOException, MyException {
boolean bNewConnection = this.newUpdatableStorageConnection(group_name, remote_filename);
Socket storageSocket = this.storageServer.getSocket();
boolean bNewStorageServer = this.newUpdatableStorageConnection(group_name, remote_filename);
Connection connection = this.storageServer.getConnection();
try {
this.send_package(ProtoCommon.STORAGE_PROTO_CMD_DELETE_FILE, group_name, remote_filename);
ProtoCommon.RecvPackageInfo pkgInfo = ProtoCommon.recvPackage(storageSocket.getInputStream(),
this.send_package(ProtoCommon.STORAGE_PROTO_CMD_DELETE_FILE, group_name, remote_filename, connection);
ProtoCommon.RecvPackageInfo pkgInfo = ProtoCommon.recvPackage(connection.getInputStream(),
ProtoCommon.STORAGE_PROTO_CMD_RESP, 0);
this.errno = pkgInfo.errno;
return pkgInfo.errno;
} catch (IOException ex) {
if (!bNewConnection) {
try {
this.storageServer.close();
this.storageServer.close(connection);
} catch (IOException ex1) {
ex1.printStackTrace();
} finally {
this.storageServer = null;
connection = null;
}
}
throw ex;
} finally {
if (bNewConnection) {
try {
this.storageServer.close();
} catch (IOException ex1) {
ex1.printStackTrace();
} finally {
this.storageServer = null;
}
}
doFinally(connection, bNewStorageServer);
}
}
@ -1123,8 +1085,8 @@ public class StorageClient {
public int truncate_file(String group_name, String appender_filename,
long truncated_file_size) throws IOException, MyException {
byte[] header;
boolean bNewConnection;
Socket storageSocket;
boolean bNewStorageServer;
Connection connection = null;
byte[] hexLenBytes;
byte[] appenderFilenameBytes;
int offset;
@ -1136,10 +1098,10 @@ public class StorageClient {
return this.errno;
}
bNewConnection = this.newUpdatableStorageConnection(group_name, appender_filename);
bNewStorageServer = this.newUpdatableStorageConnection(group_name, appender_filename);
try {
storageSocket = this.storageServer.getSocket();
connection = this.storageServer.getConnection();
appenderFilenameBytes = appender_filename.getBytes(ClientGlobal.g_charset);
body_len = 2 * ProtoCommon.FDFS_PROTO_PKG_LEN_SIZE + appenderFilenameBytes.length;
@ -1157,38 +1119,27 @@ public class StorageClient {
System.arraycopy(hexLenBytes, 0, wholePkg, offset, hexLenBytes.length);
offset += hexLenBytes.length;
OutputStream out = storageSocket.getOutputStream();
OutputStream out = connection.getOutputStream();
System.arraycopy(appenderFilenameBytes, 0, wholePkg, offset, appenderFilenameBytes.length);
offset += appenderFilenameBytes.length;
out.write(wholePkg);
ProtoCommon.RecvPackageInfo pkgInfo = ProtoCommon.recvPackage(storageSocket.getInputStream(),
ProtoCommon.RecvPackageInfo pkgInfo = ProtoCommon.recvPackage(connection.getInputStream(),
ProtoCommon.STORAGE_PROTO_CMD_RESP, 0);
this.errno = pkgInfo.errno;
return pkgInfo.errno;
} catch (IOException ex) {
if (!bNewConnection) {
try {
this.storageServer.close();
this.storageServer.close(connection);
} catch (IOException ex1) {
ex1.printStackTrace();
} finally {
this.storageServer = null;
connection = null;
}
}
throw ex;
} finally {
if (bNewConnection) {
try {
this.storageServer.close();
} catch (IOException ex1) {
ex1.printStackTrace();
} finally {
this.storageServer = null;
}
}
doFinally(connection, bNewStorageServer);
}
}
@ -1216,14 +1167,14 @@ public class StorageClient {
* @return file content/buff, return null if fail
*/
public byte[] download_file(String group_name, String remote_filename, long file_offset, long download_bytes) throws IOException, MyException {
boolean bNewConnection = this.newReadableStorageConnection(group_name, remote_filename);
Socket storageSocket = this.storageServer.getSocket();
boolean bNewStorageServer = this.newReadableStorageConnection(group_name, remote_filename);
Connection connection = this.storageServer.getConnection();
try {
ProtoCommon.RecvPackageInfo pkgInfo;
this.send_download_package(group_name, remote_filename, file_offset, download_bytes);
pkgInfo = ProtoCommon.recvPackage(storageSocket.getInputStream(),
this.send_download_package(group_name, remote_filename, file_offset, download_bytes, connection);
pkgInfo = ProtoCommon.recvPackage(connection.getInputStream(),
ProtoCommon.STORAGE_PROTO_CMD_RESP, -1);
this.errno = pkgInfo.errno;
@ -1233,27 +1184,16 @@ public class StorageClient {
return pkgInfo.body;
} catch (IOException ex) {
if (!bNewConnection) {
try {
this.storageServer.close();
this.storageServer.close(connection);
} catch (IOException ex1) {
ex1.printStackTrace();
} finally {
this.storageServer = null;
connection = null;
}
}
throw ex;
} finally {
if (bNewConnection) {
try {
this.storageServer.close();
} catch (IOException ex1) {
ex1.printStackTrace();
} finally {
this.storageServer = null;
}
}
doFinally(connection, bNewStorageServer);
}
}
@ -1286,16 +1226,16 @@ public class StorageClient {
public int download_file(String group_name, String remote_filename,
long file_offset, long download_bytes,
String local_filename) throws IOException, MyException {
boolean bNewConnection = this.newReadableStorageConnection(group_name, remote_filename);
Socket storageSocket = this.storageServer.getSocket();
boolean bNewStorageServer = this.newReadableStorageConnection(group_name, remote_filename);
Connection connection = this.storageServer.getConnection();
try {
ProtoCommon.RecvHeaderInfo header;
FileOutputStream out = new FileOutputStream(local_filename);
try {
this.errno = 0;
this.send_download_package(group_name, remote_filename, file_offset, download_bytes);
this.send_download_package(group_name, remote_filename, file_offset, download_bytes, connection);
InputStream in = storageSocket.getInputStream();
InputStream in = connection.getInputStream();
header = ProtoCommon.recvHeader(in, ProtoCommon.STORAGE_PROTO_CMD_RESP, -1);
this.errno = header.errno;
if (header.errno != 0) {
@ -1333,27 +1273,16 @@ public class StorageClient {
}
}
} catch (IOException ex) {
if (!bNewConnection) {
try {
this.storageServer.close();
this.storageServer.close(connection);
} catch (IOException ex1) {
ex1.printStackTrace();
} finally {
this.storageServer = null;
connection = null;
}
}
throw ex;
} finally {
if (bNewConnection) {
try {
this.storageServer.close();
} catch (IOException ex1) {
ex1.printStackTrace();
} finally {
this.storageServer = null;
}
}
doFinally(connection, bNewStorageServer);
}
}
@ -1387,14 +1316,14 @@ public class StorageClient {
long file_offset, long download_bytes,
DownloadCallback callback) throws IOException, MyException {
int result;
boolean bNewConnection = this.newReadableStorageConnection(group_name, remote_filename);
Socket storageSocket = this.storageServer.getSocket();
boolean bNewStorageServer = this.newReadableStorageConnection(group_name, remote_filename);
Connection connection = this.storageServer.getConnection();
try {
ProtoCommon.RecvHeaderInfo header;
this.send_download_package(group_name, remote_filename, file_offset, download_bytes);
this.send_download_package(group_name, remote_filename, file_offset, download_bytes, connection);
InputStream in = storageSocket.getInputStream();
InputStream in = connection.getInputStream();
header = ProtoCommon.recvHeader(in, ProtoCommon.STORAGE_PROTO_CMD_RESP, -1);
this.errno = header.errno;
if (header.errno != 0) {
@ -1423,27 +1352,16 @@ public class StorageClient {
return 0;
} catch (IOException ex) {
if (!bNewConnection) {
try {
this.storageServer.close();
this.storageServer.close(connection);
} catch (IOException ex1) {
ex1.printStackTrace();
} finally {
this.storageServer = null;
connection = null;
}
}
throw ex;
} finally {
if (bNewConnection) {
try {
this.storageServer.close();
} catch (IOException ex1) {
ex1.printStackTrace();
} finally {
this.storageServer = null;
}
}
doFinally(connection, bNewStorageServer);
}
}
@ -1455,14 +1373,13 @@ public class StorageClient {
* @return meta info array, return null if fail
*/
public NameValuePair[] get_metadata(String group_name, String remote_filename) throws IOException, MyException {
boolean bNewConnection = this.newUpdatableStorageConnection(group_name, remote_filename);
Socket storageSocket = this.storageServer.getSocket();
boolean bNewStorageServer = this.newUpdatableStorageConnection(group_name, remote_filename);
Connection connection = this.storageServer.getConnection();
try {
ProtoCommon.RecvPackageInfo pkgInfo;
this.send_package(ProtoCommon.STORAGE_PROTO_CMD_GET_METADATA, group_name, remote_filename);
pkgInfo = ProtoCommon.recvPackage(storageSocket.getInputStream(),
this.send_package(ProtoCommon.STORAGE_PROTO_CMD_GET_METADATA, group_name, remote_filename, connection);
pkgInfo = ProtoCommon.recvPackage(connection.getInputStream(),
ProtoCommon.STORAGE_PROTO_CMD_RESP, -1);
this.errno = pkgInfo.errno;
@ -1472,27 +1389,16 @@ public class StorageClient {
return ProtoCommon.split_metadata(new String(pkgInfo.body, ClientGlobal.g_charset));
} catch (IOException ex) {
if (!bNewConnection) {
try {
this.storageServer.close();
this.storageServer.close(connection);
} catch (IOException ex1) {
ex1.printStackTrace();
} finally {
this.storageServer = null;
connection = null;
}
}
throw ex;
} finally {
if (bNewConnection) {
try {
this.storageServer.close();
} catch (IOException ex1) {
ex1.printStackTrace();
} finally {
this.storageServer = null;
}
}
doFinally(connection, bNewStorageServer);
}
}
@ -1511,9 +1417,8 @@ public class StorageClient {
*/
public int set_metadata(String group_name, String remote_filename,
NameValuePair[] meta_list, byte op_flag) throws IOException, MyException {
boolean bNewConnection = this.newUpdatableStorageConnection(group_name, remote_filename);
Socket storageSocket = this.storageServer.getSocket();
boolean bNewStorageServer = this.newUpdatableStorageConnection(group_name, remote_filename);
Connection connection = this.storageServer.getConnection();
try {
byte[] header;
byte[] groupBytes;
@ -1553,7 +1458,7 @@ public class StorageClient {
header = ProtoCommon.packHeader(ProtoCommon.STORAGE_PROTO_CMD_SET_METADATA,
2 * ProtoCommon.FDFS_PROTO_PKG_LEN_SIZE + 1 + groupBytes.length
+ filenameBytes.length + meta_buff.length, (byte) 0);
OutputStream out = storageSocket.getOutputStream();
OutputStream out = connection.getOutputStream();
byte[] wholePkg = new byte[header.length + sizeBytes.length + 1 + groupBytes.length + filenameBytes.length];
System.arraycopy(header, 0, wholePkg, 0, header.length);
System.arraycopy(sizeBytes, 0, wholePkg, header.length, sizeBytes.length);
@ -1565,33 +1470,22 @@ public class StorageClient {
out.write(meta_buff);
}
pkgInfo = ProtoCommon.recvPackage(storageSocket.getInputStream(),
pkgInfo = ProtoCommon.recvPackage(connection.getInputStream(),
ProtoCommon.STORAGE_PROTO_CMD_RESP, 0);
this.errno = pkgInfo.errno;
return pkgInfo.errno;
} catch (IOException ex) {
if (!bNewConnection) {
try {
this.storageServer.close();
this.storageServer.close(connection);
} catch (IOException ex1) {
ex1.printStackTrace();
} finally {
this.storageServer = null;
connection = null;
}
}
throw ex;
} finally {
if (bNewConnection) {
try {
this.storageServer.close();
} catch (IOException ex1) {
ex1.printStackTrace();
} finally {
this.storageServer = null;
}
}
doFinally(connection, bNewStorageServer);
}
}
@ -1614,23 +1508,18 @@ public class StorageClient {
short file_type;
long file_size = ProtoCommon.buff2long(buff, 4 * 2);
if (((file_size & ProtoCommon.APPENDER_FILE_SIZE) != 0))
{
if (((file_size & ProtoCommon.APPENDER_FILE_SIZE) != 0)) {
file_type = FileInfo.FILE_TYPE_APPENDER;
}
else if ((remote_filename.length() > ProtoCommon.TRUNK_LOGIC_FILENAME_LENGTH) ||
} else if ((remote_filename.length() > ProtoCommon.TRUNK_LOGIC_FILENAME_LENGTH) ||
((remote_filename.length() > ProtoCommon.NORMAL_LOGIC_FILENAME_LENGTH) &&
((file_size & ProtoCommon.TRUNK_FILE_MARK_SIZE) == 0)))
{
((file_size & ProtoCommon.TRUNK_FILE_MARK_SIZE) == 0))) {
file_type = FileInfo.FILE_TYPE_SLAVE;
}
else {
} else {
file_type = FileInfo.FILE_TYPE_NORMAL;
}
if (file_type == FileInfo.FILE_TYPE_SLAVE ||
file_type == FileInfo.FILE_TYPE_APPENDER)
{ //slave file or appender file
file_type == FileInfo.FILE_TYPE_APPENDER) { //slave file or appender file
FileInfo fi = this.query_file_info(group_name, remote_filename);
if (fi == null) {
return null;
@ -1658,9 +1547,8 @@ public class StorageClient {
* @return FileInfo object for success, return null for fail
*/
public FileInfo query_file_info(String group_name, String remote_filename) throws IOException, MyException {
boolean bNewConnection = this.newUpdatableStorageConnection(group_name, remote_filename);
Socket storageSocket = this.storageServer.getSocket();
boolean bNewStorageServer = this.newUpdatableStorageConnection(group_name, remote_filename);
Connection connection = this.storageServer.getConnection();
try {
byte[] header;
byte[] groupBytes;
@ -1683,14 +1571,14 @@ public class StorageClient {
header = ProtoCommon.packHeader(ProtoCommon.STORAGE_PROTO_CMD_QUERY_FILE_INFO,
+groupBytes.length + filenameBytes.length, (byte) 0);
OutputStream out = storageSocket.getOutputStream();
OutputStream out = connection.getOutputStream();
byte[] wholePkg = new byte[header.length + groupBytes.length + filenameBytes.length];
System.arraycopy(header, 0, wholePkg, 0, header.length);
System.arraycopy(groupBytes, 0, wholePkg, header.length, groupBytes.length);
System.arraycopy(filenameBytes, 0, wholePkg, header.length + groupBytes.length, filenameBytes.length);
out.write(wholePkg);
pkgInfo = ProtoCommon.recvPackage(storageSocket.getInputStream(),
pkgInfo = ProtoCommon.recvPackage(connection.getInputStream(),
ProtoCommon.STORAGE_PROTO_CMD_RESP,
3 * ProtoCommon.FDFS_PROTO_PKG_LEN_SIZE +
ProtoCommon.FDFS_IPADDR_SIZE);
@ -1707,27 +1595,16 @@ public class StorageClient {
return new FileInfo(true, FileInfo.FILE_TYPE_NORMAL, file_size,
create_timestamp, crc32, source_ip_addr);
} catch (IOException ex) {
if (!bNewConnection) {
try {
this.storageServer.close();
this.storageServer.close(connection);
} catch (IOException ex1) {
ex1.printStackTrace();
} finally {
this.storageServer = null;
connection = null;
}
}
throw ex;
} finally {
if (bNewConnection) {
try {
this.storageServer.close();
} catch (IOException ex1) {
ex1.printStackTrace();
} finally {
this.storageServer = null;
}
}
doFinally(connection, bNewStorageServer);
}
}
@ -1797,7 +1674,7 @@ public class StorageClient {
* @param group_name the group name of storage server
* @param remote_filename filename on storage server
*/
protected void send_package(byte cmd, String group_name, String remote_filename) throws IOException {
protected void send_package(byte cmd, String group_name, String remote_filename, Connection connection) throws IOException {
byte[] header;
byte[] groupBytes;
byte[] filenameBytes;
@ -1821,7 +1698,7 @@ public class StorageClient {
System.arraycopy(header, 0, wholePkg, 0, header.length);
System.arraycopy(groupBytes, 0, wholePkg, header.length, groupBytes.length);
System.arraycopy(filenameBytes, 0, wholePkg, header.length + groupBytes.length, filenameBytes.length);
this.storageServer.getSocket().getOutputStream().write(wholePkg);
connection.getOutputStream().write(wholePkg);
}
/**
@ -1832,7 +1709,7 @@ public class StorageClient {
* @param file_offset the start offset of the file
* @param download_bytes download bytes
*/
protected void send_download_package(String group_name, String remote_filename, long file_offset, long download_bytes) throws IOException {
protected void send_download_package(String group_name, String remote_filename, long file_offset, long download_bytes, Connection connection) throws IOException {
byte[] header;
byte[] bsOffset;
byte[] bsDownBytes;
@ -1863,19 +1740,19 @@ public class StorageClient {
System.arraycopy(bsDownBytes, 0, wholePkg, header.length + bsOffset.length, bsDownBytes.length);
System.arraycopy(groupBytes, 0, wholePkg, header.length + bsOffset.length + bsDownBytes.length, groupBytes.length);
System.arraycopy(filenameBytes, 0, wholePkg, header.length + bsOffset.length + bsDownBytes.length + groupBytes.length, filenameBytes.length);
this.storageServer.getSocket().getOutputStream().write(wholePkg);
connection.getOutputStream().write(wholePkg);
}
public boolean isConnected() {
return trackerServer.isConnected();
return trackerServer != null;
}
public boolean isAvaliable() {
return trackerServer.isAvaliable();
return trackerServer != null;
}
public void close() throws IOException {
trackerServer.close();
trackerServer = null;
}
public TrackerServer getTrackerServer() {

View File

@ -8,6 +8,9 @@
package org.csource.fastdfs;
import org.csource.common.MyException;
import org.csource.fastdfs.pool.Connection;
import java.io.IOException;
import java.io.OutputStream;
import java.net.Socket;
@ -54,8 +57,8 @@ public class TrackerClient {
*
* @return tracker server Socket object, return null if fail
*/
public TrackerServer getConnection() throws IOException {
return this.tracker_group.getConnection();
public TrackerServer getTrackerServer() throws IOException {
return this.tracker_group.getTrackerServer();
}
/**
@ -64,7 +67,7 @@ public class TrackerClient {
* @param trackerServer the tracker server
* @return storage server Socket object, return null if fail
*/
public StorageServer getStoreStorage(TrackerServer trackerServer) throws IOException {
public StorageServer getStoreStorage(TrackerServer trackerServer) throws IOException, MyException {
final String groupName = null;
return this.getStoreStorage(trackerServer, groupName);
}
@ -76,28 +79,20 @@ public class TrackerClient {
* @param groupName the group name to upload file to, can be empty
* @return storage server object, return null if fail
*/
public StorageServer getStoreStorage(TrackerServer trackerServer, String groupName) throws IOException {
public StorageServer getStoreStorage(TrackerServer trackerServer, String groupName) throws IOException, MyException {
byte[] header;
String ip_addr;
int port;
byte cmd;
int out_len;
boolean bNewConnection;
byte store_path;
Socket trackerSocket;
Connection connection;
if (trackerServer == null) {
trackerServer = getConnection();
if (trackerServer == null) {
return null;
trackerServer = getTrackerServer();
}
bNewConnection = true;
} else {
bNewConnection = false;
}
trackerSocket = trackerServer.getSocket();
OutputStream out = trackerSocket.getOutputStream();
connection = trackerServer.getConnection();
OutputStream out = connection.getOutputStream();
try {
if (groupName == null || groupName.length() == 0) {
@ -128,7 +123,7 @@ public class TrackerClient {
out.write(bGroupName);
}
ProtoCommon.RecvPackageInfo pkgInfo = ProtoCommon.recvPackage(trackerSocket.getInputStream(),
ProtoCommon.RecvPackageInfo pkgInfo = ProtoCommon.recvPackage(connection.getInputStream(),
ProtoCommon.TRACKER_PROTO_CMD_RESP,
ProtoCommon.TRACKER_QUERY_STORAGE_STORE_BODY_LEN);
this.errno = pkgInfo.errno;
@ -144,19 +139,18 @@ public class TrackerClient {
return new StorageServer(ip_addr, port, store_path);
} catch (IOException ex) {
if (!bNewConnection) {
try {
trackerServer.close();
trackerServer.close(connection);
} catch (IOException ex1) {
ex1.printStackTrace();
} finally {
connection = null;
}
}
throw ex;
} finally {
if (bNewConnection) {
if (connection != null) {
try {
trackerServer.close();
trackerServer.releaseConnection(connection);
} catch (IOException ex1) {
ex1.printStackTrace();
}
@ -171,27 +165,23 @@ public class TrackerClient {
* @param groupName the group name to upload file to, can be empty
* @return storage servers, return null if fail
*/
public StorageServer[] getStoreStorages(TrackerServer trackerServer, String groupName) throws IOException {
public StorageServer[] getStoreStorages(TrackerServer trackerServer, String groupName) throws IOException, MyException {
byte[] header;
String ip_addr;
int port;
byte cmd;
int out_len;
boolean bNewConnection;
Socket trackerSocket;
Connection connection;
if (trackerServer == null) {
trackerServer = getConnection();
trackerServer = getTrackerServer();
if (trackerServer == null) {
return null;
}
bNewConnection = true;
} else {
bNewConnection = false;
}
trackerSocket = trackerServer.getSocket();
OutputStream out = trackerSocket.getOutputStream();
connection = trackerServer.getConnection();
OutputStream out = connection.getOutputStream();
try {
if (groupName == null || groupName.length() == 0) {
@ -222,7 +212,7 @@ public class TrackerClient {
out.write(bGroupName);
}
ProtoCommon.RecvPackageInfo pkgInfo = ProtoCommon.recvPackage(trackerSocket.getInputStream(),
ProtoCommon.RecvPackageInfo pkgInfo = ProtoCommon.recvPackage(connection.getInputStream(),
ProtoCommon.TRACKER_PROTO_CMD_RESP, -1);
this.errno = pkgInfo.errno;
if (pkgInfo.errno != 0) {
@ -264,19 +254,18 @@ public class TrackerClient {
return results;
} catch (IOException ex) {
if (!bNewConnection) {
try {
trackerServer.close();
trackerServer.close(connection);
} catch (IOException ex1) {
ex1.printStackTrace();
} finally {
connection = null;
}
}
throw ex;
} finally {
if (bNewConnection) {
if (connection != null) {
try {
trackerServer.close();
trackerServer.releaseConnection(connection);
} catch (IOException ex1) {
ex1.printStackTrace();
}
@ -293,7 +282,7 @@ public class TrackerClient {
* @return storage server Socket object, return null if fail
*/
public StorageServer getFetchStorage(TrackerServer trackerServer,
String groupName, String filename) throws IOException {
String groupName, String filename) throws IOException, MyException {
ServerInfo[] servers = this.getStorages(trackerServer, ProtoCommon.TRACKER_PROTO_CMD_SERVICE_QUERY_FETCH_ONE,
groupName, filename);
if (servers == null) {
@ -312,7 +301,7 @@ public class TrackerClient {
* @return storage server Socket object, return null if fail
*/
public StorageServer getUpdateStorage(TrackerServer trackerServer,
String groupName, String filename) throws IOException {
String groupName, String filename) throws IOException, MyException {
ServerInfo[] servers = this.getStorages(trackerServer, ProtoCommon.TRACKER_PROTO_CMD_SERVICE_QUERY_UPDATE,
groupName, filename);
if (servers == null) {
@ -331,7 +320,7 @@ public class TrackerClient {
* @return storage servers, return null if fail
*/
public ServerInfo[] getFetchStorages(TrackerServer trackerServer,
String groupName, String filename) throws IOException {
String groupName, String filename) throws IOException, MyException {
return this.getStorages(trackerServer, ProtoCommon.TRACKER_PROTO_CMD_SERVICE_QUERY_FETCH_ALL,
groupName, filename);
}
@ -347,7 +336,7 @@ public class TrackerClient {
* @return storage server Socket object, return null if fail
*/
protected ServerInfo[] getStorages(TrackerServer trackerServer,
byte cmd, String groupName, String filename) throws IOException {
byte cmd, String groupName, String filename) throws IOException, MyException {
byte[] header;
byte[] bFileName;
byte[] bGroupName;
@ -355,20 +344,16 @@ public class TrackerClient {
int len;
String ip_addr;
int port;
boolean bNewConnection;
Socket trackerSocket;
Connection connection;
if (trackerServer == null) {
trackerServer = getConnection();
trackerServer = getTrackerServer();
if (trackerServer == null) {
return null;
}
bNewConnection = true;
} else {
bNewConnection = false;
}
trackerSocket = trackerServer.getSocket();
OutputStream out = trackerSocket.getOutputStream();
connection = trackerServer.getConnection();
OutputStream out = connection.getOutputStream();
try {
bs = groupName.getBytes(ClientGlobal.g_charset);
@ -390,7 +375,7 @@ public class TrackerClient {
System.arraycopy(bFileName, 0, wholePkg, header.length + bGroupName.length, bFileName.length);
out.write(wholePkg);
ProtoCommon.RecvPackageInfo pkgInfo = ProtoCommon.recvPackage(trackerSocket.getInputStream(),
ProtoCommon.RecvPackageInfo pkgInfo = ProtoCommon.recvPackage(connection.getInputStream(),
ProtoCommon.TRACKER_PROTO_CMD_RESP, -1);
this.errno = pkgInfo.errno;
if (pkgInfo.errno != 0) {
@ -422,19 +407,19 @@ public class TrackerClient {
return servers;
} catch (IOException ex) {
if (!bNewConnection) {
try {
trackerServer.close();
trackerServer.close(connection);
} catch (IOException ex1) {
ex1.printStackTrace();
}
} finally {
connection = null;
}
throw ex;
} finally {
if (bNewConnection) {
if (connection != null) {
try {
trackerServer.close();
trackerServer.releaseConnection(connection);
} catch (IOException ex1) {
ex1.printStackTrace();
}
@ -449,7 +434,7 @@ public class TrackerClient {
* @param file_id the file id(including group name and filename)
* @return storage server Socket object, return null if fail
*/
public StorageServer getFetchStorage1(TrackerServer trackerServer, String file_id) throws IOException {
public StorageServer getFetchStorage1(TrackerServer trackerServer, String file_id) throws IOException, MyException {
String[] parts = new String[2];
this.errno = StorageClient1.split_file_id(file_id, parts);
if (this.errno != 0) {
@ -466,7 +451,7 @@ public class TrackerClient {
* @param file_id the file id(including group name and filename)
* @return storage servers, return null if fail
*/
public ServerInfo[] getFetchStorages1(TrackerServer trackerServer, String file_id) throws IOException {
public ServerInfo[] getFetchStorages1(TrackerServer trackerServer, String file_id) throws IOException, MyException {
String[] parts = new String[2];
this.errno = StorageClient1.split_file_id(file_id, parts);
if (this.errno != 0) {
@ -482,34 +467,30 @@ public class TrackerClient {
* @param trackerServer the tracker server
* @return group stat array, return null if fail
*/
public StructGroupStat[] listGroups(TrackerServer trackerServer) throws IOException {
public StructGroupStat[] listGroups(TrackerServer trackerServer) throws IOException, MyException {
byte[] header;
String ip_addr;
int port;
byte cmd;
int out_len;
boolean bNewConnection;
byte store_path;
Socket trackerSocket;
Connection connection;
if (trackerServer == null) {
trackerServer = getConnection();
trackerServer = getTrackerServer();
if (trackerServer == null) {
return null;
}
bNewConnection = true;
} else {
bNewConnection = false;
}
trackerSocket = trackerServer.getSocket();
OutputStream out = trackerSocket.getOutputStream();
connection = trackerServer.getConnection();
OutputStream out = connection.getOutputStream();
try {
header = ProtoCommon.packHeader(ProtoCommon.TRACKER_PROTO_CMD_SERVER_LIST_GROUP, 0, (byte) 0);
out.write(header);
ProtoCommon.RecvPackageInfo pkgInfo = ProtoCommon.recvPackage(trackerSocket.getInputStream(),
ProtoCommon.RecvPackageInfo pkgInfo = ProtoCommon.recvPackage(connection.getInputStream(),
ProtoCommon.TRACKER_PROTO_CMD_RESP, -1);
this.errno = pkgInfo.errno;
if (pkgInfo.errno != 0) {
@ -519,23 +500,22 @@ public class TrackerClient {
ProtoStructDecoder<StructGroupStat> decoder = new ProtoStructDecoder<StructGroupStat>();
return decoder.decode(pkgInfo.body, StructGroupStat.class, StructGroupStat.getFieldsTotalSize());
} catch (IOException ex) {
if (!bNewConnection) {
try {
trackerServer.close();
trackerServer.close(connection);
} catch (IOException ex1) {
ex1.printStackTrace();
} finally {
connection = null;
}
}
throw ex;
} catch (Exception ex) {
ex.printStackTrace();
this.errno = ProtoCommon.ERR_NO_EINVAL;
return null;
} finally {
if (bNewConnection) {
if (connection != null) {
try {
trackerServer.close();
trackerServer.releaseConnection(connection);
} catch (IOException ex1) {
ex1.printStackTrace();
}
@ -550,7 +530,7 @@ public class TrackerClient {
* @param groupName the group name of storage server
* @return storage server stat array, return null if fail
*/
public StructStorageStat[] listStorages(TrackerServer trackerServer, String groupName) throws IOException {
public StructStorageStat[] listStorages(TrackerServer trackerServer, String groupName) throws IOException, MyException {
final String storageIpAddr = null;
return this.listStorages(trackerServer, groupName, storageIpAddr);
}
@ -564,25 +544,21 @@ public class TrackerClient {
* @return storage server stat array, return null if fail
*/
public StructStorageStat[] listStorages(TrackerServer trackerServer,
String groupName, String storageIpAddr) throws IOException {
String groupName, String storageIpAddr) throws IOException, MyException {
byte[] header;
byte[] bGroupName;
byte[] bs;
int len;
boolean bNewConnection;
Socket trackerSocket;
Connection connection;
if (trackerServer == null) {
trackerServer = getConnection();
trackerServer = getTrackerServer();
if (trackerServer == null) {
return null;
}
bNewConnection = true;
} else {
bNewConnection = false;
}
trackerSocket = trackerServer.getSocket();
OutputStream out = trackerSocket.getOutputStream();
connection = trackerServer.getConnection();
OutputStream out = connection.getOutputStream();
try {
bs = groupName.getBytes(ClientGlobal.g_charset);
@ -619,7 +595,7 @@ public class TrackerClient {
}
out.write(wholePkg);
ProtoCommon.RecvPackageInfo pkgInfo = ProtoCommon.recvPackage(trackerSocket.getInputStream(),
ProtoCommon.RecvPackageInfo pkgInfo = ProtoCommon.recvPackage(connection.getInputStream(),
ProtoCommon.TRACKER_PROTO_CMD_RESP, -1);
this.errno = pkgInfo.errno;
if (pkgInfo.errno != 0) {
@ -629,13 +605,11 @@ public class TrackerClient {
ProtoStructDecoder<StructStorageStat> decoder = new ProtoStructDecoder<StructStorageStat>();
return decoder.decode(pkgInfo.body, StructStorageStat.class, StructStorageStat.getFieldsTotalSize());
} catch (IOException ex) {
if (!bNewConnection) {
try {
trackerServer.close();
trackerServer.close(connection);
} catch (IOException ex1) {
ex1.printStackTrace();
}
}
throw ex;
} catch (Exception ex) {
@ -643,9 +617,9 @@ public class TrackerClient {
this.errno = ProtoCommon.ERR_NO_EINVAL;
return null;
} finally {
if (bNewConnection) {
if (connection != null) {
try {
trackerServer.close();
trackerServer.releaseConnection(connection);
} catch (IOException ex1) {
ex1.printStackTrace();
}
@ -662,16 +636,17 @@ public class TrackerClient {
* @return true for success, false for fail
*/
private boolean deleteStorage(TrackerServer trackerServer,
String groupName, String storageIpAddr) throws IOException {
String groupName, String storageIpAddr) throws IOException, MyException {
byte[] header;
byte[] bGroupName;
byte[] bs;
int len;
Socket trackerSocket;
Connection connection;
trackerSocket = trackerServer.getSocket();
OutputStream out = trackerSocket.getOutputStream();
connection = trackerServer.getConnection();
OutputStream out = connection.getOutputStream();
try {
bs = groupName.getBytes(ClientGlobal.g_charset);
bGroupName = new byte[ProtoCommon.FDFS_GROUP_NAME_MAX_LEN];
@ -698,10 +673,22 @@ public class TrackerClient {
System.arraycopy(bIpAddr, 0, wholePkg, header.length + bGroupName.length, ipAddrLen);
out.write(wholePkg);
ProtoCommon.RecvPackageInfo pkgInfo = ProtoCommon.recvPackage(trackerSocket.getInputStream(),
ProtoCommon.RecvPackageInfo pkgInfo = ProtoCommon.recvPackage(connection.getInputStream(),
ProtoCommon.TRACKER_PROTO_CMD_RESP, 0);
this.errno = pkgInfo.errno;
return pkgInfo.errno == 0;
} catch (IOException e) {
try {
trackerServer.close(connection);
} finally {
connection = null;
}
throw e;
} finally {
if (connection != null) {
trackerServer.releaseConnection(connection);
}
}
}
/**
@ -711,7 +698,7 @@ public class TrackerClient {
* @param storageIpAddr the storage server ip address
* @return true for success, false for fail
*/
public boolean deleteStorage(String groupName, String storageIpAddr) throws IOException {
public boolean deleteStorage(String groupName, String storageIpAddr) throws IOException, MyException {
return this.deleteStorage(ClientGlobal.g_tracker_group, groupName, storageIpAddr);
}
@ -724,7 +711,7 @@ public class TrackerClient {
* @return true for success, false for fail
*/
public boolean deleteStorage(TrackerGroup trackerGroup,
String groupName, String storageIpAddr) throws IOException {
String groupName, String storageIpAddr) throws IOException, MyException {
int serverIndex;
int notFoundCount;
TrackerServer trackerServer;
@ -732,14 +719,12 @@ public class TrackerClient {
notFoundCount = 0;
for (serverIndex = 0; serverIndex < trackerGroup.tracker_servers.length; serverIndex++) {
try {
trackerServer = trackerGroup.getConnection(serverIndex);
trackerServer = trackerGroup.getTrackerServer(serverIndex);
} catch (IOException ex) {
ex.printStackTrace(System.err);
this.errno = ProtoCommon.ECONNREFUSED;
return false;
}
try {
StructStorageStat[] storageStats = listStorages(trackerServer, groupName, storageIpAddr);
if (storageStats == null) {
if (this.errno == ProtoCommon.ERR_NO_ENOENT) {
@ -754,13 +739,7 @@ public class TrackerClient {
this.errno = ProtoCommon.ERR_NO_EBUSY;
return false;
}
} finally {
try {
trackerServer.close();
} catch (IOException ex1) {
ex1.printStackTrace();
}
}
}
if (notFoundCount == trackerGroup.tracker_servers.length) {
@ -771,15 +750,13 @@ public class TrackerClient {
notFoundCount = 0;
for (serverIndex = 0; serverIndex < trackerGroup.tracker_servers.length; serverIndex++) {
try {
trackerServer = trackerGroup.getConnection(serverIndex);
trackerServer = trackerGroup.getTrackerServer(serverIndex);
} catch (IOException ex) {
System.err.println("connect to server " + trackerGroup.tracker_servers[serverIndex].getAddress().getHostAddress() + ":" + trackerGroup.tracker_servers[serverIndex].getPort() + " fail");
ex.printStackTrace(System.err);
this.errno = ProtoCommon.ECONNREFUSED;
return false;
}
try {
if (!this.deleteStorage(trackerServer, groupName, storageIpAddr)) {
if (this.errno != 0) {
if (this.errno == ProtoCommon.ERR_NO_ENOENT) {
@ -789,13 +766,7 @@ public class TrackerClient {
}
}
}
} finally {
try {
trackerServer.close();
} catch (IOException ex1) {
ex1.printStackTrace();
}
}
}
if (notFoundCount == trackerGroup.tracker_servers.length) {

View File

@ -10,7 +10,6 @@ package org.csource.fastdfs;
import java.io.IOException;
import java.net.InetSocketAddress;
import java.net.Socket;
/**
* Tracker server group
@ -39,7 +38,7 @@ public class TrackerGroup {
*
* @return connected tracker server, null for fail
*/
public TrackerServer getConnection(int serverIndex) throws IOException {
public TrackerServer getTrackerServer(int serverIndex) throws IOException {
return new TrackerServer(this.tracker_servers[serverIndex]);
}
@ -48,7 +47,7 @@ public class TrackerGroup {
*
* @return connected tracker server, null for fail
*/
public TrackerServer getConnection() throws IOException {
public TrackerServer getTrackerServer() throws IOException {
int current_index;
synchronized (this.lock) {
@ -61,7 +60,7 @@ public class TrackerGroup {
}
try {
return this.getConnection(current_index);
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);
@ -73,7 +72,7 @@ public class TrackerGroup {
}
try {
TrackerServer trackerServer = this.getConnection(i);
TrackerServer trackerServer = this.getTrackerServer(i);
synchronized (this.lock) {
if (this.tracker_server_index == current_index) {

View File

@ -8,14 +8,13 @@
package org.csource.fastdfs;
import org.csource.fastdfs.pool.ConnectionInfo;
import org.csource.common.MyException;
import org.csource.fastdfs.pool.Connection;
import org.csource.fastdfs.pool.ConnectionPool;
import org.csource.fastdfs.pool.ConnectionFactory;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.InetSocketAddress;
import java.net.Socket;
/**
* Tracker Server Info
@ -24,46 +23,22 @@ import java.net.Socket;
* @version Version 1.11
*/
public class TrackerServer {
protected Socket sock;
protected InetSocketAddress inetSockAddr;
private Long lastAccessTime = System.currentTimeMillis();
/**
* Constructor
*
* @param sock Socket of server
* @param inetSockAddr the server info
*/
public TrackerServer(Socket sock, InetSocketAddress inetSockAddr) {
this.sock = sock;
this.inetSockAddr = inetSockAddr;
}
public TrackerServer(InetSocketAddress inetSockAddr) throws IOException {
this.inetSockAddr = inetSockAddr;
this.sock = getSocket();
}
/**
* get the connected socket
*
* @return the socket
*/
public Socket getSocket() throws IOException {
if (this.sock == null) {
public Connection getConnection() throws MyException, IOException {
Connection connection;
if (ClientGlobal.g_connection_pool_enabled) {
ConnectionInfo connection = ConnectionPool.getConnection(this.inetSockAddr);
this.sock = connection.getSocket();
this.lastAccessTime = connection.getLastAccessTime();
connection = ConnectionPool.getConnection(this.inetSockAddr);
} else {
this.sock = ClientGlobal.getSocket(this.inetSockAddr);
connection = ConnectionFactory.create(this.inetSockAddr);
}
return connection;
}
return this.sock;
}
/**
* get the server info
*
@ -72,88 +47,28 @@ public class TrackerServer {
public InetSocketAddress getInetSocketAddress() {
return this.inetSockAddr;
}
public OutputStream getOutputStream() throws IOException {
return this.sock.getOutputStream();
}
public InputStream getInputStream() throws IOException {
return this.sock.getInputStream();
}
public void close() throws IOException {
public void close(Connection connection) throws IOException {
//if connection enabled get from connection pool
if (ClientGlobal.g_connection_pool_enabled) {
ConnectionPool.freeConnection(this);
ConnectionPool.closeConnection(connection);
} else {
this.closeDirect();
connection.close();
}
}
/**
* close direct if not create from pool or not open pool
* releaseConnection connection
* @param connection
* @throws IOException
*/
public void closeDirect() throws IOException {
if (this.sock != null) {
try {
ProtoCommon.closeSocket(this.sock);
} finally {
this.sock = null;
}
public void releaseConnection(Connection connection) throws IOException {
if (ClientGlobal.g_connection_pool_enabled) {
ConnectionPool.releaseConnection(connection);
} else {
connection.close();
}
}
protected void finalize() throws Throwable {
this.close();
}
public boolean isConnected() {
boolean isConnected = false;
if (sock != null) {
if (sock.isConnected()) {
isConnected = true;
}
}
return isConnected;
}
public boolean isAvaliable() {
if (isConnected()) {
if (sock.getPort() == 0) {
return false;
}
if (sock.getInetAddress() == null) {
return false;
}
if (sock.getRemoteSocketAddress() == null) {
return false;
}
if (sock.isInputShutdown()) {
return false;
}
if (sock.isOutputShutdown()) {
return false;
}
return true;
}
return false;
}
public Long getLastAccessTime() {
return lastAccessTime;
}
public void setLastAccessTime(Long lastAccessTime) {
this.lastAccessTime = lastAccessTime;
}
@Override
public String toString() {
return "TrackerServer{" +
"sock=" + sock +
", inetSockAddr=" + inetSockAddr +
", lastAccessTime=" + lastAccessTime +
'}';
}
}

View File

@ -0,0 +1,107 @@
package org.csource.fastdfs.pool;
import org.csource.fastdfs.ProtoCommon;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.InetSocketAddress;
import java.net.Socket;
public class Connection {
private Socket sock;
private InetSocketAddress inetSockAddr;
private Long lastAccessTime = System.currentTimeMillis();
public Connection(Socket sock, InetSocketAddress inetSockAddr) {
this.sock = sock;
this.inetSockAddr = inetSockAddr;
}
/**
* get the server info
*
* @return the server info
*/
public InetSocketAddress getInetSocketAddress() {
return this.inetSockAddr;
}
public OutputStream getOutputStream() throws IOException {
return this.sock.getOutputStream();
}
public InputStream getInputStream() throws IOException {
return this.sock.getInputStream();
}
public Long getLastAccessTime() {
return lastAccessTime;
}
public void setLastAccessTime(Long lastAccessTime) {
this.lastAccessTime = lastAccessTime;
}
/**
* close direct if not create from pool or not open pool
*
* @throws IOException
*/
public void close() throws IOException {
if (this.sock != null) {
try {
ProtoCommon.closeSocket(this.sock);
} finally {
this.sock = null;
}
}
}
public boolean activeTest() throws IOException {
if (this.sock == null) {
return false;
}
return ProtoCommon.activeTest(this.sock);
}
public boolean isConnected() {
boolean isConnected = false;
if (sock != null) {
if (sock.isConnected()) {
isConnected = true;
}
}
return isConnected;
}
public boolean isAvaliable() {
if (isConnected()) {
if (sock.getPort() == 0) {
return false;
}
if (sock.getInetAddress() == null) {
return false;
}
if (sock.getRemoteSocketAddress() == null) {
return false;
}
if (sock.isInputShutdown()) {
return false;
}
if (sock.isOutputShutdown()) {
return false;
}
return true;
}
return false;
}
@Override
public String toString() {
return "TrackerServer{" +
"sock=" + sock +
", inetSockAddr=" + inetSockAddr +
'}';
}
}

View File

@ -5,9 +5,8 @@ import org.csource.fastdfs.ClientGlobal;
import java.io.IOException;
import java.net.InetSocketAddress;
import java.net.Socket;
public class PoolConnectionFactory {
public static ConnectionInfo create(String key) throws IOException {
public class ConnectionFactory {
public static Connection create(String key) throws IOException {
if (key == null) {
System.err.printf("ip:port entry conn't be null");
return null;
@ -24,7 +23,21 @@ public class PoolConnectionFactory {
sock.setSoTimeout(ClientGlobal.g_network_timeout);
InetSocketAddress inetSocketAddress = new InetSocketAddress(ip, port);
sock.connect(inetSocketAddress, ClientGlobal.g_connect_timeout);
return new ConnectionInfo(sock, inetSocketAddress, System.currentTimeMillis(), false);
return new Connection(sock, inetSocketAddress);
}
/**
* create from InetSocketAddress
* @param socketAddress
* @return
* @throws IOException
*/
public static Connection create(InetSocketAddress socketAddress) throws IOException {
Socket sock = new Socket();
sock.setReuseAddress(true);
sock.setSoTimeout(ClientGlobal.g_network_timeout);
sock.connect(socketAddress, ClientGlobal.g_connect_timeout);
return new Connection(sock, socketAddress);
}
}

View File

@ -1,60 +0,0 @@
package org.csource.fastdfs.pool;
import java.net.InetSocketAddress;
import java.net.Socket;
public class ConnectionInfo {
private Socket socket;
protected InetSocketAddress inetSockAddr;
private Long lastAccessTime;
private boolean needActiveCheck = false;
public Socket getSocket() {
return socket;
}
public void setSocket(Socket socket) {
this.socket = socket;
}
public InetSocketAddress getInetSockAddr() {
return inetSockAddr;
}
public void setInetSockAddr(InetSocketAddress inetSockAddr) {
this.inetSockAddr = inetSockAddr;
}
public Long getLastAccessTime() {
return lastAccessTime;
}
public void setLastAccessTime(Long lastAccessTime) {
this.lastAccessTime = lastAccessTime;
}
public boolean isNeedActiveCheck() {
return needActiveCheck;
}
public void setNeedActiveCheck(boolean needActiveCheck) {
this.needActiveCheck = needActiveCheck;
}
public ConnectionInfo(Socket socket, InetSocketAddress inetSockAddr, Long lastAccessTime, boolean needActiveCheck) {
this.socket = socket;
this.inetSockAddr = inetSockAddr;
this.lastAccessTime = lastAccessTime;
this.needActiveCheck = needActiveCheck;
}
@Override
public String toString() {
return "ConnectionInfo{" +
"socket=" + socket +
", inetSockAddr=" + inetSockAddr +
", lastAccessTime=" + lastAccessTime +
", needActiveCheck=" + needActiveCheck +
'}';
}
}

View File

@ -1,9 +1,7 @@
package org.csource.fastdfs.pool;
import org.csource.common.MyException;
import org.csource.fastdfs.ClientGlobal;
import org.csource.fastdfs.ProtoCommon;
import org.csource.fastdfs.TrackerServer;
import java.io.IOException;
import java.util.concurrent.ConcurrentLinkedQueue;
import java.util.concurrent.TimeUnit;
@ -37,7 +35,7 @@ public class ConnectionManager {
/**
* free connections
*/
private volatile ConcurrentLinkedQueue<ConnectionInfo> freeConnections = new ConcurrentLinkedQueue<ConnectionInfo>();
private volatile ConcurrentLinkedQueue<Connection> freeConnections = new ConcurrentLinkedQueue<Connection>();
private ConnectionManager() {
@ -47,57 +45,46 @@ public class ConnectionManager {
this.key = key;
}
private synchronized ConnectionInfo newConnection() throws IOException {
private Connection newConnection() throws IOException {
try {
ConnectionInfo connectionInfo = PoolConnectionFactory.create(this.key);
return connectionInfo;
Connection connection = ConnectionFactory.create(this.key);
return connection;
} catch (IOException e) {
throw e;
}
}
public synchronized ConnectionInfo getConnection() throws IOException {
public Connection getConnection() throws MyException {
lock.lock();
try {
ConnectionInfo connectionInfo = null;
Connection connection = null;
while (true) {
if (freeCount.get() > 0) {
connectionInfo = freeConnections.poll();
if ((System.currentTimeMillis() - connectionInfo.getLastAccessTime()) > ClientGlobal.getG_connection_pool_max_idle_time()) {
closeConnection(connectionInfo);
continue;
} else {
freeCount.decrementAndGet();
connection = freeConnections.poll();
if (!connection.isAvaliable() || (System.currentTimeMillis() - connection.getLastAccessTime()) > ClientGlobal.getG_connection_pool_max_idle_time()) {
closeConnection(connection);
continue;
}
} else if (ClientGlobal.getG_connection_pool_max_count_per_entry() == 0 || totalCount.get() < ClientGlobal.getG_connection_pool_max_count_per_entry()) {
connectionInfo = newConnection();
if (connectionInfo != null) {
connection = newConnection();
if (connection != null) {
totalCount.incrementAndGet();
}
} else {
try {
if (condition.await(ClientGlobal.getG_connection_pool_max_wait_time(), TimeUnit.MILLISECONDS)) {
if (condition.await(ClientGlobal.getG_connection_pool_max_wait_time_in_ms(), TimeUnit.MILLISECONDS)) {
//wait single success
continue;
}
throw new MyException("get connection fail, wait_time greater than " + ClientGlobal.g_connection_pool_max_wait_time_in_ms + "ms");
} catch (InterruptedException e) {
e.printStackTrace();
throw new MyException("get connection fail, emsg > " + e.getMessage());
}
}
//if need check active
if (connectionInfo.isNeedActiveCheck()) {
boolean activeYes = ProtoCommon.activeTest(connectionInfo.getSocket());
if (activeYes) {
connectionInfo.setLastAccessTime(System.currentTimeMillis());
} else {
//close if check fail
closeConnection(connectionInfo);
continue;
}
}
return connectionInfo;
return connection;
}
} catch (IOException e) {
return null;
@ -106,33 +93,34 @@ public class ConnectionManager {
}
}
public void freeConnection(TrackerServer trackerServer) throws IOException {
if (trackerServer == null || !trackerServer.isConnected()) {
public void releaseConnection(Connection connection) throws IOException {
if (connection == null) {
return;
}
ConnectionInfo connectionInfo = new ConnectionInfo(trackerServer.getSocket(),trackerServer.getInetSocketAddress(),System.currentTimeMillis(),true);
if ((System.currentTimeMillis() - trackerServer.getLastAccessTime()) < ClientGlobal.getG_connection_pool_max_idle_time()) {
if ((System.currentTimeMillis() - connection.getLastAccessTime()) < ClientGlobal.g_connection_pool_max_idle_time) {
try {
lock.lock();
freeConnections.add(connectionInfo);
freeConnections.add(connection);
freeCount.incrementAndGet();
condition.signal();
} finally {
lock.unlock();
}
} else {
closeConnection(connectionInfo);
}
closeConnection(connection);
}
public void closeConnection(ConnectionInfo connectionInfo) throws IOException {
if (connectionInfo.getSocket() != null) {
totalCount.decrementAndGet();
try {
ProtoCommon.closeSocket(connectionInfo.getSocket());
} finally {
connectionInfo.setSocket(null);
}
public void closeConnection(Connection connection) throws IOException {
try {
if (connection != null) {
totalCount.decrementAndGet();
connection.close();
}
} catch (IOException e) {
System.err.println("close socket error , msg:" + e.getMessage());
e.printStackTrace();
}
}

View File

@ -1,6 +1,6 @@
package org.csource.fastdfs.pool;
import org.csource.fastdfs.TrackerServer;
import org.csource.common.MyException;
import java.io.IOException;
import java.net.InetSocketAddress;
@ -13,54 +13,51 @@ public class ConnectionPool {
*/
private final static ConcurrentHashMap<String, ConnectionManager> CP = new ConcurrentHashMap<String, ConnectionManager>();
public static synchronized ConnectionInfo getConnection(InetSocketAddress socketAddress) throws IOException {
public static Connection getConnection(InetSocketAddress socketAddress) throws MyException {
if (socketAddress == null) {
return null;
}
String key = getKey(socketAddress);
ConnectionManager connectionManager = CP.get(key);
ConnectionManager connectionManager;
synchronized (ConnectionPool.class) {
connectionManager = CP.get(key);
if (connectionManager == null) {
connectionManager = new ConnectionManager(key);
CP.put(key, connectionManager);
}
}
return connectionManager.getConnection();
}
/**
* release connection
*/
public static void closeConnection(TrackerServer trackerServer) throws IOException {
if (trackerServer == null || trackerServer.getInetSocketAddress() == null) {
public static void releaseConnection(Connection connection) throws IOException {
if (connection == null) {
return;
}
String key = getKey(trackerServer.getInetSocketAddress());
if (key != null) {
String key = getKey(connection.getInetSocketAddress());
ConnectionManager connectionManager = CP.get(key);
if (connectionManager != null) {
connectionManager.closeConnection(new ConnectionInfo(trackerServer.getSocket(), trackerServer.getInetSocketAddress(),trackerServer.getLastAccessTime(),true));
connectionManager.releaseConnection(connection);
} else {
trackerServer.closeDirect();
}
} else {
trackerServer.closeDirect();
try {
connection.close();
} catch (IOException e) {
System.err.println("close socket error, msg:" + e.getMessage());
e.printStackTrace();
}
}
public static void freeConnection(TrackerServer trackerServer) throws IOException {
if (trackerServer == null || trackerServer.getInetSocketAddress() == null) {
return;
}
String key = getKey(trackerServer.getInetSocketAddress());
if (key != null) {
ConnectionManager connectionManager = CP.get(key);
if (connectionManager != null) {
connectionManager.freeConnection(trackerServer);
} else {
trackerServer.closeDirect();
}
public static void closeConnection(Connection connection) throws IOException {
if (connection == null) {
return;
}
String key = getKey(connection.getInetSocketAddress());
ConnectionManager connectionManager = CP.get(key);
if (connectionManager != null) {
connectionManager.closeConnection(connection);
} else {
trackerServer.closeDirect();
connection.close();
}
}
@ -70,6 +67,7 @@ public class ConnectionPool {
}
return String.format("%s:%s", socketAddress.getHostName(), socketAddress.getPort());
}
@Override
public String toString() {
if (!CP.isEmpty()) {

View File

@ -14,4 +14,4 @@ fastdfs.tracker_servers = 10.0.11.201:22122,10.0.11.202:22122,10.0.11.203:22122
fastdfs.connection_pool.enabled = false
fastdfs.connection_pool.max_count_per_entry = 50
fastdfs.connection_pool.max_idle_time = 60
fastdfs.connection_pool.max_wait_time = 5
fastdfs.connection_pool.max_wait_time_in_ms = 5000

View File

@ -18,4 +18,4 @@ fastdfs.connection_pool.enabled = false
fastdfs.connection_pool.max_count_per_entry = 100
fastdfs.connection_pool.max_idle_time = 60
fastdfs.connection_pool.max_wait_time = 5
fastdfs.connection_pool.max_wait_time_in_ms = 5000

View File

@ -11,4 +11,4 @@ tracker_server = 10.0.11.244:22122
connection_pool.enabled = false
connection_pool.max_count_per_entry = 100
connection_pool.max_idle_time = 60
connection_pool.max_wait_time = 5
connection_pool.max_wait_time_in_ms = 5000

View File

@ -31,7 +31,7 @@ public class FdfsTest {
LOGGER.info("network_timeout=" + ClientGlobal.g_network_timeout + "ms");
LOGGER.info("charset=" + ClientGlobal.g_charset);
TrackerClient tracker = new TrackerClient();
trackerServer = tracker.getConnection();
trackerServer = tracker.getTrackerServer();
StorageServer storageServer = null;
storageClient = new StorageClient(trackerServer, storageServer);
}

View File

@ -8,8 +8,6 @@
package org.csource.fastdfs;
import org.csource.fastdfs.*;
import java.text.SimpleDateFormat;
/**
@ -48,7 +46,7 @@ public class Monitor {
System.out.println("delete storage errno: " + tracker.getErrorCode());
*/
TrackerServer trackerServer = tracker.getConnection();
TrackerServer trackerServer = tracker.getTrackerServer();
if (trackerServer == null) {
return;
}
@ -157,8 +155,6 @@ public class Monitor {
System.out.println("\t\tlast_synced_timestamp = " + df.format(storageStat.getLastSyncedTimestamp()) + getSyncedDelayString(storageStats, storageStat));
}
}
trackerServer.close();
} catch (Exception ex) {
ex.printStackTrace();
}

View File

@ -9,7 +9,6 @@
package org.csource.fastdfs;
import org.csource.common.NameValuePair;
import org.csource.fastdfs.*;
/**
* client test
@ -46,7 +45,7 @@ public class Test {
System.out.println("charset=" + ClientGlobal.g_charset);
TrackerClient tracker = new TrackerClient();
TrackerServer trackerServer = tracker.getConnection();
TrackerServer trackerServer = tracker.getTrackerServer();
StorageServer storageServer = null;
StorageClient1 client = new StorageClient1(trackerServer, storageServer);
@ -60,8 +59,6 @@ public class Test {
byte[] result = client.download_file1(fileId);
System.out.println(i + ", download result is: " + result.length);
}
trackerServer.close();
} catch (Exception ex) {
ex.printStackTrace();
}

View File

@ -1,7 +1,6 @@
package org.csource.fastdfs;
import org.csource.common.NameValuePair;
import org.csource.fastdfs.*;
import java.net.InetSocketAddress;
@ -39,9 +38,9 @@ public class Test1 {
TrackerGroup tg = new TrackerGroup(new InetSocketAddress[]{new InetSocketAddress("47.95.221.159", 22122)});
TrackerClient tc = new TrackerClient(tg);
TrackerServer ts = tc.getConnection();
TrackerServer ts = tc.getTrackerServer();
if (ts == null) {
System.out.println("getConnection return null");
System.out.println("getTrackerServer return null");
return;
}

View File

@ -9,7 +9,6 @@
package org.csource.fastdfs;
import org.csource.common.NameValuePair;
import org.csource.fastdfs.*;
import java.io.File;
import java.net.InetSocketAddress;
@ -53,7 +52,7 @@ public class TestAppender {
String remote_filename;
ServerInfo[] servers;
TrackerClient tracker = new TrackerClient();
TrackerServer trackerServer = tracker.getConnection();
TrackerServer trackerServer = tracker.getTrackerServer();
StorageServer storageServer = null;
@ -298,14 +297,10 @@ public class TestAppender {
return;
}
/* for test only */
System.out.println("active test to storage server: " + ProtoCommon.activeTest(storageServer.getSocket()));
storageServer.close();
System.out.println("active test to storage server: " + storageServer.getConnection().activeTest());
/* for test only */
System.out.println("active test to tracker server: " + ProtoCommon.activeTest(trackerServer.getSocket()));
trackerServer.close();
System.out.println("active test to tracker server: " + trackerServer.getConnection().activeTest());
} catch (Exception ex) {
ex.printStackTrace();
}

View File

@ -9,7 +9,6 @@
package org.csource.fastdfs;
import org.csource.common.NameValuePair;
import org.csource.fastdfs.*;
import java.io.File;
import java.net.InetSocketAddress;
@ -51,7 +50,7 @@ public class TestAppender1 {
long startTime;
ServerInfo[] servers;
TrackerClient tracker = new TrackerClient();
TrackerServer trackerServer = tracker.getConnection();
TrackerServer trackerServer = tracker.getTrackerServer();
StorageServer storageServer = null;
@ -276,15 +275,12 @@ public class TestAppender1 {
System.out.println("getFetchStorage fail, errno code: " + tracker.getErrorCode());
return;
}
/* for test only */
System.out.println("active test to storage server: " + ProtoCommon.activeTest(storageServer.getSocket()));
storageServer.close();
/* for test only */
System.out.println("active test to tracker server: " + ProtoCommon.activeTest(trackerServer.getSocket()));
System.out.println("active test to storage server: " + storageServer.getConnection().activeTest());
trackerServer.close();
/* for test only */
System.out.println("active test to tracker server: " + trackerServer.getConnection().activeTest());
} catch (Exception ex) {
ex.printStackTrace();
}

View File

@ -9,7 +9,6 @@
package org.csource.fastdfs;
import org.csource.common.NameValuePair;
import org.csource.fastdfs.*;
import java.io.File;
import java.net.InetSocketAddress;
@ -53,7 +52,7 @@ public class TestClient {
String remote_filename;
ServerInfo[] servers;
TrackerClient tracker = new TrackerClient();
TrackerServer trackerServer = tracker.getConnection();
TrackerServer trackerServer = tracker.getTrackerServer();
StorageServer storageServer = null;
@ -288,15 +287,12 @@ public class TestClient {
System.out.println("getFetchStorage fail, errno code: " + tracker.getErrorCode());
return;
}
/* for test only */
System.out.println("active test to storage server: " + ProtoCommon.activeTest(storageServer.getSocket()));
storageServer.close();
/* for test only */
System.out.println("active test to tracker server: " + ProtoCommon.activeTest(trackerServer.getSocket()));
System.out.println("active test to storage server: " + storageServer.getConnection().activeTest());
trackerServer.close();
/* for test only */
System.out.println("active test to tracker server: " + trackerServer.getConnection().activeTest());
} catch (Exception ex) {
ex.printStackTrace();
}

View File

@ -9,7 +9,6 @@
package org.csource.fastdfs;
import org.csource.common.NameValuePair;
import org.csource.fastdfs.*;
import java.io.File;
import java.net.InetSocketAddress;
@ -52,7 +51,7 @@ public class TestClient1 {
String file_id;
TrackerClient tracker = new TrackerClient();
TrackerServer trackerServer = tracker.getConnection();
TrackerServer trackerServer = tracker.getTrackerServer();
StorageServer storageServer = null;
/*
@ -251,14 +250,12 @@ public class TestClient1 {
return;
}
/* for test only */
System.out.println("active test to storage server: " + ProtoCommon.activeTest(storageServer.getSocket()));
storageServer.close();
/* for test only */
System.out.println("active test to tracker server: " + ProtoCommon.activeTest(trackerServer.getSocket()));
System.out.println("active test to storage server: " + storageServer.getConnection().activeTest());
trackerServer.close();
/* for test only */
System.out.println("active test to tracker server: " + trackerServer.getConnection().activeTest());
} catch (Exception ex) {
ex.printStackTrace();
}

View File

@ -8,8 +8,6 @@
package org.csource.fastdfs;
import org.csource.fastdfs.*;
/**
* load test class
*
@ -88,7 +86,7 @@ public class TestLoad {
public Uploader() throws Exception {
this.tracker = new TrackerClient();
this.trackerServer = tracker.getConnection();
this.trackerServer = tracker.getTrackerServer();
}
public int uploadFile() throws Exception {
@ -129,7 +127,7 @@ public class TestLoad {
public Downloader() throws Exception {
this.tracker = new TrackerClient();
this.trackerServer = tracker.getConnection();
this.trackerServer = tracker.getTrackerServer();
this.callback = new DownloadFileDiscard();
}