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}";
}

File diff suppressed because it is too large Load Diff

File diff suppressed because it is too large Load Diff

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) {
if (ClientGlobal.g_connection_pool_enabled) {
ConnectionInfo connection = ConnectionPool.getConnection(this.inetSockAddr);
this.sock = connection.getSocket();
this.lastAccessTime = connection.getLastAccessTime();
} else {
this.sock = ClientGlobal.getSocket(this.inetSockAddr);
}
public Connection getConnection() throws MyException, IOException {
Connection connection;
if (ClientGlobal.g_connection_pool_enabled) {
connection = ConnectionPool.getConnection(this.inetSockAddr);
} else {
connection = ConnectionFactory.create(this.inetSockAddr);
}
return this.sock;
return connection;
}
/**
* 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);
freeCount.decrementAndGet();
connection = freeConnections.poll();
if (!connection.isAvaliable() || (System.currentTimeMillis() - connection.getLastAccessTime()) > ClientGlobal.getG_connection_pool_max_idle_time()) {
closeConnection(connection);
continue;
} else {
freeCount.decrementAndGet();
}
} 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);
if (connectionManager == null) {
connectionManager = new ConnectionManager(key);
CP.put(key, connectionManager);
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) {
ConnectionManager connectionManager = CP.get(key);
if (connectionManager != null) {
connectionManager.closeConnection(new ConnectionInfo(trackerServer.getSocket(), trackerServer.getInetSocketAddress(),trackerServer.getLastAccessTime(),true));
} else {
trackerServer.closeDirect();
}
String key = getKey(connection.getInetSocketAddress());
ConnectionManager connectionManager = CP.get(key);
if (connectionManager != null) {
connectionManager.releaseConnection(connection);
} 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) {
public static void closeConnection(Connection connection) throws IOException {
if (connection == null) {
return;
}
String key = getKey(trackerServer.getInetSocketAddress());
if (key != null) {
ConnectionManager connectionManager = CP.get(key);
if (connectionManager != null) {
connectionManager.freeConnection(trackerServer);
} else {
trackerServer.closeDirect();
}
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;
@ -21,39 +20,39 @@ import java.net.InetSocketAddress;
* @version Version 1.20
*/
public class TestAppender1 {
private TestAppender1() {
}
/**
* entry point
*
* @param args comand arguments
* <ul><li>args[0]: config filename</li></ul>
* <ul><li>args[1]: local filename to upload</li></ul>
*/
public static void main(String args[]) {
if (args.length < 2) {
System.out.println("Error: Must have 2 parameters, one is config filename, "
+ "the other is the local filename to upload");
return;
private TestAppender1() {
}
System.out.println("java.version=" + System.getProperty("java.version"));
/**
* entry point
*
* @param args comand arguments
* <ul><li>args[0]: config filename</li></ul>
* <ul><li>args[1]: local filename to upload</li></ul>
*/
public static void main(String args[]) {
if (args.length < 2) {
System.out.println("Error: Must have 2 parameters, one is config filename, "
+ "the other is the local filename to upload");
return;
}
String conf_filename = args[0];
String local_filename = args[1];
System.out.println("java.version=" + System.getProperty("java.version"));
try {
ClientGlobal.init(conf_filename);
System.out.println("network_timeout=" + ClientGlobal.g_network_timeout + "ms");
System.out.println("charset=" + ClientGlobal.g_charset);
String conf_filename = args[0];
String local_filename = args[1];
long startTime;
ServerInfo[] servers;
TrackerClient tracker = new TrackerClient();
TrackerServer trackerServer = tracker.getConnection();
try {
ClientGlobal.init(conf_filename);
System.out.println("network_timeout=" + ClientGlobal.g_network_timeout + "ms");
System.out.println("charset=" + ClientGlobal.g_charset);
StorageServer storageServer = null;
long startTime;
ServerInfo[] servers;
TrackerClient tracker = new TrackerClient();
TrackerServer trackerServer = tracker.getTrackerServer();
StorageServer storageServer = null;
/*
storageServer = tracker.getStoreStorage(trackerServer);
@ -64,229 +63,226 @@ public class TestAppender1 {
}
*/
StorageClient1 client = new StorageClient1(trackerServer, storageServer);
byte[] file_buff;
NameValuePair[] meta_list;
String group_name;
String appender_file_id;
String file_ext_name;
int errno;
StorageClient1 client = new StorageClient1(trackerServer, storageServer);
byte[] file_buff;
NameValuePair[] meta_list;
String group_name;
String appender_file_id;
String file_ext_name;
int errno;
meta_list = new NameValuePair[4];
meta_list[0] = new NameValuePair("width", "800");
meta_list[1] = new NameValuePair("heigth", "600");
meta_list[2] = new NameValuePair("bgcolor", "#FFFFFF");
meta_list[3] = new NameValuePair("author", "Mike");
meta_list = new NameValuePair[4];
meta_list[0] = new NameValuePair("width", "800");
meta_list[1] = new NameValuePair("heigth", "600");
meta_list[2] = new NameValuePair("bgcolor", "#FFFFFF");
meta_list[3] = new NameValuePair("author", "Mike");
file_buff = "this is a test".getBytes(ClientGlobal.g_charset);
System.out.println("file length: " + file_buff.length);
file_buff = "this is a test".getBytes(ClientGlobal.g_charset);
System.out.println("file length: " + file_buff.length);
group_name = null;
StorageServer[] storageServers = tracker.getStoreStorages(trackerServer, group_name);
if (storageServers == null) {
System.err.println("get store storage servers fail, error code: " + tracker.getErrorCode());
} else {
System.err.println("store storage servers count: " + storageServers.length);
for (int k = 0; k < storageServers.length; k++) {
System.err.println((k + 1) + ". " + storageServers[k].getInetSocketAddress().getAddress().getHostAddress() + ":" + storageServers[k].getInetSocketAddress().getPort());
}
System.err.println("");
}
group_name = null;
StorageServer[] storageServers = tracker.getStoreStorages(trackerServer, group_name);
if (storageServers == null) {
System.err.println("get store storage servers fail, error code: " + tracker.getErrorCode());
} else {
System.err.println("store storage servers count: " + storageServers.length);
for (int k = 0; k < storageServers.length; k++) {
System.err.println((k + 1) + ". " + storageServers[k].getInetSocketAddress().getAddress().getHostAddress() + ":" + storageServers[k].getInetSocketAddress().getPort());
}
System.err.println("");
}
startTime = System.currentTimeMillis();
appender_file_id = client.upload_appender_file1(file_buff, "txt", meta_list);
System.out.println("upload_appender_file1 time used: " + (System.currentTimeMillis() - startTime) + " ms");
startTime = System.currentTimeMillis();
appender_file_id = client.upload_appender_file1(file_buff, "txt", meta_list);
System.out.println("upload_appender_file1 time used: " + (System.currentTimeMillis() - startTime) + " ms");
/*
group_name = "";
appender_file_id = client.upload_appender_file1(group_name, file_buff, "txt", meta_list);
*/
if (appender_file_id == null) {
System.err.println("upload file fail, error code: " + client.getErrorCode());
return;
} else {
System.err.println(client.get_file_info1(appender_file_id));
if (appender_file_id == null) {
System.err.println("upload file fail, error code: " + client.getErrorCode());
return;
} else {
System.err.println(client.get_file_info1(appender_file_id));
servers = tracker.getFetchStorages1(trackerServer, appender_file_id);
if (servers == null) {
System.err.println("get storage servers fail, error code: " + tracker.getErrorCode());
} else {
System.err.println("storage servers count: " + servers.length);
for (int k = 0; k < servers.length; k++) {
System.err.println((k + 1) + ". " + servers[k].getIpAddr() + ":" + servers[k].getPort());
}
System.err.println("");
servers = tracker.getFetchStorages1(trackerServer, appender_file_id);
if (servers == null) {
System.err.println("get storage servers fail, error code: " + tracker.getErrorCode());
} else {
System.err.println("storage servers count: " + servers.length);
for (int k = 0; k < servers.length; k++) {
System.err.println((k + 1) + ". " + servers[k].getIpAddr() + ":" + servers[k].getPort());
}
System.err.println("");
}
meta_list = new NameValuePair[4];
meta_list[0] = new NameValuePair("width", "1024");
meta_list[1] = new NameValuePair("heigth", "768");
meta_list[2] = new NameValuePair("bgcolor", "#000000");
meta_list[3] = new NameValuePair("title", "Untitle");
startTime = System.currentTimeMillis();
errno = client.set_metadata1(appender_file_id, meta_list, ProtoCommon.STORAGE_SET_METADATA_FLAG_MERGE);
System.out.println("set_metadata time used: " + (System.currentTimeMillis() - startTime) + " ms");
if (errno == 0) {
System.err.println("set_metadata success");
} else {
System.err.println("set_metadata fail, error no: " + errno);
}
meta_list = client.get_metadata1(appender_file_id);
if (meta_list != null) {
for (int i = 0; i < meta_list.length; i++) {
System.out.println(meta_list[i].getName() + " " + meta_list[i].getValue());
}
}
startTime = System.currentTimeMillis();
file_buff = client.download_file1(appender_file_id);
System.out.println("download_file time used: " + (System.currentTimeMillis() - startTime) + " ms");
if (file_buff != null) {
System.out.println("file length:" + file_buff.length);
System.out.println((new String(file_buff)));
}
file_buff = "this is a slave buff".getBytes(ClientGlobal.g_charset);
file_ext_name = "txt";
startTime = System.currentTimeMillis();
errno = client.append_file1(appender_file_id, file_buff);
System.out.println("append_file time used: " + (System.currentTimeMillis() - startTime) + " ms");
if (errno == 0) {
System.err.println(client.get_file_info1(appender_file_id));
} else {
System.err.println("append file fail, error no: " + errno);
}
startTime = System.currentTimeMillis();
String new_file_id = client.regenerate_appender_filename1(appender_file_id);
System.out.println("regenerate_appender_filename time used: " + (System.currentTimeMillis() - startTime) + " ms");
if (errno == 0) {
appender_file_id = new_file_id;
System.err.println(client.get_file_info1(appender_file_id));
} else {
System.err.println("regenerate_appender_filename fail, error no: " + errno);
}
startTime = System.currentTimeMillis();
errno = client.delete_file1(appender_file_id);
System.out.println("delete_file time used: " + (System.currentTimeMillis() - startTime) + " ms");
if (errno == 0) {
System.err.println("Delete file success");
} else {
System.err.println("Delete file fail, error no: " + errno);
}
}
appender_file_id = client.upload_appender_file1(local_filename, null, meta_list);
if (appender_file_id != null) {
int ts;
String token;
String file_url;
InetSocketAddress inetSockAddr;
inetSockAddr = trackerServer.getInetSocketAddress();
file_url = "http://" + inetSockAddr.getAddress().getHostAddress();
if (ClientGlobal.g_tracker_http_port != 80) {
file_url += ":" + ClientGlobal.g_tracker_http_port;
}
file_url += "/" + appender_file_id;
if (ClientGlobal.g_anti_steal_token) {
ts = (int) (System.currentTimeMillis() / 1000);
token = ProtoCommon.getToken(appender_file_id, ts, ClientGlobal.g_secret_key);
file_url += "?token=" + token + "&ts=" + ts;
}
System.err.println(client.get_file_info1(appender_file_id));
System.err.println("file url: " + file_url);
errno = client.download_file1(appender_file_id, 0, 0, "c:\\" + appender_file_id.replaceAll("/", "_"));
if (errno == 0) {
System.err.println("Download file success");
} else {
System.err.println("Download file fail, error no: " + errno);
}
errno = client.download_file1(appender_file_id, 0, 0, new DownloadFileWriter("c:\\" + appender_file_id.replaceAll("/", "-")));
if (errno == 0) {
System.err.println("Download file success");
} else {
System.err.println("Download file fail, error no: " + errno);
}
file_ext_name = null;
startTime = System.currentTimeMillis();
errno = client.append_file1(appender_file_id, local_filename);
System.out.println("append_file time used: " + (System.currentTimeMillis() - startTime) + " ms");
if (errno == 0) {
System.err.println(client.get_file_info1(appender_file_id));
} else {
System.err.println("append file fail, error no: " + errno);
}
}
File f;
f = new File(local_filename);
int nPos = local_filename.lastIndexOf('.');
if (nPos > 0 && local_filename.length() - nPos <= ProtoCommon.FDFS_FILE_EXT_NAME_MAX_LEN + 1) {
file_ext_name = local_filename.substring(nPos + 1);
} else {
file_ext_name = null;
}
appender_file_id = client.upload_appender_file1(null, f.length(),
new UploadLocalFileSender(local_filename), file_ext_name, meta_list);
if (appender_file_id != null) {
System.out.println(client.get_file_info1(appender_file_id));
startTime = System.currentTimeMillis();
errno = client.append_file1(appender_file_id, f.length(), new UploadLocalFileSender(local_filename));
System.out.println("append_file time used: " + (System.currentTimeMillis() - startTime) + " ms");
if (errno == 0) {
System.err.println(client.get_file_info1(appender_file_id));
} else {
System.err.println("append file fail, error no: " + errno);
}
startTime = System.currentTimeMillis();
errno = client.modify_file1(appender_file_id, 0, f.length(), new UploadLocalFileSender(local_filename));
System.out.println("modify_file time used: " + (System.currentTimeMillis() - startTime) + " ms");
if (errno == 0) {
System.err.println(client.get_file_info1(appender_file_id));
} else {
System.err.println("modify file fail, error no: " + errno);
}
startTime = System.currentTimeMillis();
errno = client.truncate_file1(appender_file_id, 0);
System.out.println("truncate_file time used: " + (System.currentTimeMillis() - startTime) + " ms");
if (errno == 0) {
System.err.println(client.get_file_info1(appender_file_id));
} else {
System.err.println("truncate file fail, error no: " + errno);
}
} else {
System.err.println("Upload file fail, error no: " + errno);
}
storageServer = tracker.getFetchStorage1(trackerServer, appender_file_id);
if (storageServer == null) {
System.out.println("getFetchStorage fail, errno code: " + tracker.getErrorCode());
return;
}
/* for test only */
System.out.println("active test to storage server: " + storageServer.getConnection().activeTest());
/* for test only */
System.out.println("active test to tracker server: " + trackerServer.getConnection().activeTest());
} catch (Exception ex) {
ex.printStackTrace();
}
meta_list = new NameValuePair[4];
meta_list[0] = new NameValuePair("width", "1024");
meta_list[1] = new NameValuePair("heigth", "768");
meta_list[2] = new NameValuePair("bgcolor", "#000000");
meta_list[3] = new NameValuePair("title", "Untitle");
startTime = System.currentTimeMillis();
errno = client.set_metadata1(appender_file_id, meta_list, ProtoCommon.STORAGE_SET_METADATA_FLAG_MERGE);
System.out.println("set_metadata time used: " + (System.currentTimeMillis() - startTime) + " ms");
if (errno == 0) {
System.err.println("set_metadata success");
} else {
System.err.println("set_metadata fail, error no: " + errno);
}
meta_list = client.get_metadata1(appender_file_id);
if (meta_list != null) {
for (int i = 0; i < meta_list.length; i++) {
System.out.println(meta_list[i].getName() + " " + meta_list[i].getValue());
}
}
startTime = System.currentTimeMillis();
file_buff = client.download_file1(appender_file_id);
System.out.println("download_file time used: " + (System.currentTimeMillis() - startTime) + " ms");
if (file_buff != null) {
System.out.println("file length:" + file_buff.length);
System.out.println((new String(file_buff)));
}
file_buff = "this is a slave buff".getBytes(ClientGlobal.g_charset);
file_ext_name = "txt";
startTime = System.currentTimeMillis();
errno = client.append_file1(appender_file_id, file_buff);
System.out.println("append_file time used: " + (System.currentTimeMillis() - startTime) + " ms");
if (errno == 0) {
System.err.println(client.get_file_info1(appender_file_id));
} else {
System.err.println("append file fail, error no: " + errno);
}
startTime = System.currentTimeMillis();
String new_file_id = client.regenerate_appender_filename1(appender_file_id);
System.out.println("regenerate_appender_filename time used: " + (System.currentTimeMillis() - startTime) + " ms");
if (errno == 0) {
appender_file_id = new_file_id;
System.err.println(client.get_file_info1(appender_file_id));
} else {
System.err.println("regenerate_appender_filename fail, error no: " + errno);
}
startTime = System.currentTimeMillis();
errno = client.delete_file1(appender_file_id);
System.out.println("delete_file time used: " + (System.currentTimeMillis() - startTime) + " ms");
if (errno == 0) {
System.err.println("Delete file success");
} else {
System.err.println("Delete file fail, error no: " + errno);
}
}
appender_file_id = client.upload_appender_file1(local_filename, null, meta_list);
if (appender_file_id != null) {
int ts;
String token;
String file_url;
InetSocketAddress inetSockAddr;
inetSockAddr = trackerServer.getInetSocketAddress();
file_url = "http://" + inetSockAddr.getAddress().getHostAddress();
if (ClientGlobal.g_tracker_http_port != 80) {
file_url += ":" + ClientGlobal.g_tracker_http_port;
}
file_url += "/" + appender_file_id;
if (ClientGlobal.g_anti_steal_token) {
ts = (int) (System.currentTimeMillis() / 1000);
token = ProtoCommon.getToken(appender_file_id, ts, ClientGlobal.g_secret_key);
file_url += "?token=" + token + "&ts=" + ts;
}
System.err.println(client.get_file_info1(appender_file_id));
System.err.println("file url: " + file_url);
errno = client.download_file1(appender_file_id, 0, 0, "c:\\" + appender_file_id.replaceAll("/", "_"));
if (errno == 0) {
System.err.println("Download file success");
} else {
System.err.println("Download file fail, error no: " + errno);
}
errno = client.download_file1(appender_file_id, 0, 0, new DownloadFileWriter("c:\\" + appender_file_id.replaceAll("/", "-")));
if (errno == 0) {
System.err.println("Download file success");
} else {
System.err.println("Download file fail, error no: " + errno);
}
file_ext_name = null;
startTime = System.currentTimeMillis();
errno = client.append_file1(appender_file_id, local_filename);
System.out.println("append_file time used: " + (System.currentTimeMillis() - startTime) + " ms");
if (errno == 0) {
System.err.println(client.get_file_info1(appender_file_id));
} else {
System.err.println("append file fail, error no: " + errno);
}
}
File f;
f = new File(local_filename);
int nPos = local_filename.lastIndexOf('.');
if (nPos > 0 && local_filename.length() - nPos <= ProtoCommon.FDFS_FILE_EXT_NAME_MAX_LEN + 1) {
file_ext_name = local_filename.substring(nPos + 1);
} else {
file_ext_name = null;
}
appender_file_id = client.upload_appender_file1(null, f.length(),
new UploadLocalFileSender(local_filename), file_ext_name, meta_list);
if (appender_file_id != null) {
System.out.println(client.get_file_info1(appender_file_id));
startTime = System.currentTimeMillis();
errno = client.append_file1(appender_file_id, f.length(), new UploadLocalFileSender(local_filename));
System.out.println("append_file time used: " + (System.currentTimeMillis() - startTime) + " ms");
if (errno == 0) {
System.err.println(client.get_file_info1(appender_file_id));
} else {
System.err.println("append file fail, error no: " + errno);
}
startTime = System.currentTimeMillis();
errno = client.modify_file1(appender_file_id, 0, f.length(), new UploadLocalFileSender(local_filename));
System.out.println("modify_file time used: " + (System.currentTimeMillis() - startTime) + " ms");
if (errno == 0) {
System.err.println(client.get_file_info1(appender_file_id));
} else {
System.err.println("modify file fail, error no: " + errno);
}
startTime = System.currentTimeMillis();
errno = client.truncate_file1(appender_file_id, 0);
System.out.println("truncate_file time used: " + (System.currentTimeMillis() - startTime) + " ms");
if (errno == 0) {
System.err.println(client.get_file_info1(appender_file_id));
} else {
System.err.println("truncate file fail, error no: " + errno);
}
} else {
System.err.println("Upload file fail, error no: " + errno);
}
storageServer = tracker.getFetchStorage1(trackerServer, appender_file_id);
if (storageServer == null) {
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()));
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.*;
import java.io.File;
import java.net.InetSocketAddress;
@ -21,41 +20,41 @@ import java.net.InetSocketAddress;
* @version Version 1.18
*/
public class TestClient {
private TestClient() {
}
/**
* entry point
*
* @param args comand arguments
* <ul><li>args[0]: config filename</li></ul>
* <ul><li>args[1]: local filename to upload</li></ul>
*/
public static void main(String args[]) {
if (args.length < 2) {
System.out.println("Error: Must have 2 parameters, one is config filename, "
+ "the other is the local filename to upload");
return;
private TestClient() {
}
System.out.println("java.version=" + System.getProperty("java.version"));
/**
* entry point
*
* @param args comand arguments
* <ul><li>args[0]: config filename</li></ul>
* <ul><li>args[1]: local filename to upload</li></ul>
*/
public static void main(String args[]) {
if (args.length < 2) {
System.out.println("Error: Must have 2 parameters, one is config filename, "
+ "the other is the local filename to upload");
return;
}
String conf_filename = args[0];
String local_filename = args[1];
System.out.println("java.version=" + System.getProperty("java.version"));
try {
ClientGlobal.init(conf_filename);
System.out.println("network_timeout=" + ClientGlobal.g_network_timeout + "ms");
System.out.println("charset=" + ClientGlobal.g_charset);
String conf_filename = args[0];
String local_filename = args[1];
long startTime;
String group_name;
String remote_filename;
ServerInfo[] servers;
TrackerClient tracker = new TrackerClient();
TrackerServer trackerServer = tracker.getConnection();
try {
ClientGlobal.init(conf_filename);
System.out.println("network_timeout=" + ClientGlobal.g_network_timeout + "ms");
System.out.println("charset=" + ClientGlobal.g_charset);
StorageServer storageServer = null;
long startTime;
String group_name;
String remote_filename;
ServerInfo[] servers;
TrackerClient tracker = new TrackerClient();
TrackerServer trackerServer = tracker.getTrackerServer();
StorageServer storageServer = null;
/*
storageServer = tracker.getStoreStorage(trackerServer);
@ -66,239 +65,236 @@ public class TestClient {
}
*/
StorageClient client = new StorageClient(trackerServer, storageServer);
byte[] file_buff;
NameValuePair[] meta_list;
String[] results;
String master_filename;
String prefix_name;
String file_ext_name;
String generated_slave_filename;
int errno;
StorageClient client = new StorageClient(trackerServer, storageServer);
byte[] file_buff;
NameValuePair[] meta_list;
String[] results;
String master_filename;
String prefix_name;
String file_ext_name;
String generated_slave_filename;
int errno;
meta_list = new NameValuePair[4];
meta_list[0] = new NameValuePair("width", "800");
meta_list[1] = new NameValuePair("heigth", "600");
meta_list[2] = new NameValuePair("bgcolor", "#FFFFFF");
meta_list[3] = new NameValuePair("author", "Mike");
meta_list = new NameValuePair[4];
meta_list[0] = new NameValuePair("width", "800");
meta_list[1] = new NameValuePair("heigth", "600");
meta_list[2] = new NameValuePair("bgcolor", "#FFFFFF");
meta_list[3] = new NameValuePair("author", "Mike");
file_buff = "this is a test".getBytes(ClientGlobal.g_charset);
System.out.println("file length: " + file_buff.length);
file_buff = "this is a test".getBytes(ClientGlobal.g_charset);
System.out.println("file length: " + file_buff.length);
group_name = null;
StorageServer[] storageServers = tracker.getStoreStorages(trackerServer, group_name);
if (storageServers == null) {
System.err.println("get store storage servers fail, error code: " + tracker.getErrorCode());
} else {
System.err.println("store storage servers count: " + storageServers.length);
for (int k = 0; k < storageServers.length; k++) {
System.err.println((k + 1) + ". " + storageServers[k].getInetSocketAddress().getAddress().getHostAddress() + ":" + storageServers[k].getInetSocketAddress().getPort());
}
System.err.println("");
}
group_name = null;
StorageServer[] storageServers = tracker.getStoreStorages(trackerServer, group_name);
if (storageServers == null) {
System.err.println("get store storage servers fail, error code: " + tracker.getErrorCode());
} else {
System.err.println("store storage servers count: " + storageServers.length);
for (int k = 0; k < storageServers.length; k++) {
System.err.println((k + 1) + ". " + storageServers[k].getInetSocketAddress().getAddress().getHostAddress() + ":" + storageServers[k].getInetSocketAddress().getPort());
}
System.err.println("");
}
startTime = System.currentTimeMillis();
results = client.upload_file(file_buff, "txt", meta_list);
System.out.println("upload_file time used: " + (System.currentTimeMillis() - startTime) + " ms");
startTime = System.currentTimeMillis();
results = client.upload_file(file_buff, "txt", meta_list);
System.out.println("upload_file time used: " + (System.currentTimeMillis() - startTime) + " ms");
/*
group_name = "";
results = client.upload_file(group_name, file_buff, "txt", meta_list);
*/
if (results == null) {
System.err.println("upload file fail, error code: " + client.getErrorCode());
return;
} else {
group_name = results[0];
remote_filename = results[1];
System.err.println("group_name: " + group_name + ", remote_filename: " + remote_filename);
System.err.println(client.get_file_info(group_name, remote_filename));
if (results == null) {
System.err.println("upload file fail, error code: " + client.getErrorCode());
return;
} else {
group_name = results[0];
remote_filename = results[1];
System.err.println("group_name: " + group_name + ", remote_filename: " + remote_filename);
System.err.println(client.get_file_info(group_name, remote_filename));
servers = tracker.getFetchStorages(trackerServer, group_name, remote_filename);
if (servers == null) {
System.err.println("get storage servers fail, error code: " + tracker.getErrorCode());
} else {
System.err.println("storage servers count: " + servers.length);
for (int k = 0; k < servers.length; k++) {
System.err.println((k + 1) + ". " + servers[k].getIpAddr() + ":" + servers[k].getPort());
}
System.err.println("");
servers = tracker.getFetchStorages(trackerServer, group_name, remote_filename);
if (servers == null) {
System.err.println("get storage servers fail, error code: " + tracker.getErrorCode());
} else {
System.err.println("storage servers count: " + servers.length);
for (int k = 0; k < servers.length; k++) {
System.err.println((k + 1) + ". " + servers[k].getIpAddr() + ":" + servers[k].getPort());
}
System.err.println("");
}
meta_list = new NameValuePair[4];
meta_list[0] = new NameValuePair("width", "1024");
meta_list[1] = new NameValuePair("heigth", "768");
meta_list[2] = new NameValuePair("bgcolor", "#000000");
meta_list[3] = new NameValuePair("title", "Untitle");
startTime = System.currentTimeMillis();
errno = client.set_metadata(group_name, remote_filename, meta_list, ProtoCommon.STORAGE_SET_METADATA_FLAG_MERGE);
System.out.println("set_metadata time used: " + (System.currentTimeMillis() - startTime) + " ms");
if (errno == 0) {
System.err.println("set_metadata success");
} else {
System.err.println("set_metadata fail, error no: " + errno);
}
meta_list = client.get_metadata(group_name, remote_filename);
if (meta_list != null) {
for (int i = 0; i < meta_list.length; i++) {
System.out.println(meta_list[i].getName() + " " + meta_list[i].getValue());
}
}
//Thread.sleep(30000);
startTime = System.currentTimeMillis();
file_buff = client.download_file(group_name, remote_filename);
System.out.println("download_file time used: " + (System.currentTimeMillis() - startTime) + " ms");
if (file_buff != null) {
System.out.println("file length:" + file_buff.length);
System.out.println((new String(file_buff)));
}
file_buff = "this is a slave buff".getBytes(ClientGlobal.g_charset);
master_filename = remote_filename;
prefix_name = "-part1";
file_ext_name = "txt";
startTime = System.currentTimeMillis();
results = client.upload_file(group_name, master_filename, prefix_name, file_buff, file_ext_name, meta_list);
System.out.println("upload_file time used: " + (System.currentTimeMillis() - startTime) + " ms");
if (results != null) {
System.err.println("slave file group_name: " + results[0] + ", remote_filename: " + results[1]);
generated_slave_filename = ProtoCommon.genSlaveFilename(master_filename, prefix_name, file_ext_name);
if (!generated_slave_filename.equals(results[1])) {
System.err.println("generated slave file: " + generated_slave_filename + "\n != returned slave file: " + results[1]);
}
System.err.println(client.get_file_info(results[0], results[1]));
}
startTime = System.currentTimeMillis();
errno = client.delete_file(group_name, remote_filename);
System.out.println("delete_file time used: " + (System.currentTimeMillis() - startTime) + " ms");
if (errno == 0) {
System.err.println("Delete file success");
} else {
System.err.println("Delete file fail, error no: " + errno);
}
}
results = client.upload_file(local_filename, null, meta_list);
if (results != null) {
String file_id;
int ts;
String token;
String file_url;
InetSocketAddress inetSockAddr;
group_name = results[0];
remote_filename = results[1];
file_id = group_name + StorageClient1.SPLIT_GROUP_NAME_AND_FILENAME_SEPERATOR + remote_filename;
inetSockAddr = trackerServer.getInetSocketAddress();
file_url = "http://" + inetSockAddr.getAddress().getHostAddress();
if (ClientGlobal.g_tracker_http_port != 80) {
file_url += ":" + ClientGlobal.g_tracker_http_port;
}
file_url += "/" + file_id;
if (ClientGlobal.g_anti_steal_token) {
ts = (int) (System.currentTimeMillis() / 1000);
token = ProtoCommon.getToken(file_id, ts, ClientGlobal.g_secret_key);
file_url += "?token=" + token + "&ts=" + ts;
}
System.err.println("group_name: " + group_name + ", remote_filename: " + remote_filename);
System.err.println(client.get_file_info(group_name, remote_filename));
System.err.println("file url: " + file_url);
errno = client.download_file(group_name, remote_filename, 0, 0, "c:\\" + remote_filename.replaceAll("/", "_"));
if (errno == 0) {
System.err.println("Download file success");
} else {
System.err.println("Download file fail, error no: " + errno);
}
errno = client.download_file(group_name, remote_filename, 0, 0, new DownloadFileWriter("c:\\" + remote_filename.replaceAll("/", "-")));
if (errno == 0) {
System.err.println("Download file success");
} else {
System.err.println("Download file fail, error no: " + errno);
}
master_filename = remote_filename;
prefix_name = "-part2";
file_ext_name = null;
startTime = System.currentTimeMillis();
results = client.upload_file(group_name, master_filename, prefix_name, local_filename, null, meta_list);
System.out.println("upload_file time used: " + (System.currentTimeMillis() - startTime) + " ms");
if (results != null) {
System.err.println("slave file group_name: " + results[0] + ", remote_filename: " + results[1]);
generated_slave_filename = ProtoCommon.genSlaveFilename(master_filename, prefix_name, file_ext_name);
if (!generated_slave_filename.equals(results[1])) {
System.err.println("generated slave file: " + generated_slave_filename + "\n != returned slave file: " + results[1]);
}
System.err.println(client.get_file_info(results[0], results[1]));
}
}
File f;
f = new File(local_filename);
int nPos = local_filename.lastIndexOf('.');
if (nPos > 0 && local_filename.length() - nPos <= ProtoCommon.FDFS_FILE_EXT_NAME_MAX_LEN + 1) {
file_ext_name = local_filename.substring(nPos + 1);
} else {
file_ext_name = null;
}
results = client.upload_file(null, f.length(),
new UploadLocalFileSender(local_filename), file_ext_name, meta_list);
if (results != null) {
group_name = results[0];
remote_filename = results[1];
System.out.println("group name: " + group_name + ", remote filename: " + remote_filename);
System.out.println(client.get_file_info(group_name, remote_filename));
master_filename = remote_filename;
prefix_name = "-part3";
startTime = System.currentTimeMillis();
results = client.upload_file(group_name, master_filename, prefix_name, f.length(), new UploadLocalFileSender(local_filename), file_ext_name, meta_list);
System.out.println("upload_file time used: " + (System.currentTimeMillis() - startTime) + " ms");
if (results != null) {
System.err.println("slave file group_name: " + results[0] + ", remote_filename: " + results[1]);
generated_slave_filename = ProtoCommon.genSlaveFilename(master_filename, prefix_name, file_ext_name);
if (!generated_slave_filename.equals(results[1])) {
System.err.println("generated slave file: " + generated_slave_filename + "\n != returned slave file: " + results[1]);
}
System.err.println(client.get_file_info(results[0], results[1]));
}
} else {
System.err.println("Upload file fail, error no: " + errno);
}
storageServer = tracker.getFetchStorage(trackerServer, group_name, remote_filename);
if (storageServer == null) {
System.out.println("getFetchStorage fail, errno code: " + tracker.getErrorCode());
return;
}
/* for test only */
System.out.println("active test to storage server: " + storageServer.getConnection().activeTest());
/* for test only */
System.out.println("active test to tracker server: " + trackerServer.getConnection().activeTest());
} catch (Exception ex) {
ex.printStackTrace();
}
meta_list = new NameValuePair[4];
meta_list[0] = new NameValuePair("width", "1024");
meta_list[1] = new NameValuePair("heigth", "768");
meta_list[2] = new NameValuePair("bgcolor", "#000000");
meta_list[3] = new NameValuePair("title", "Untitle");
startTime = System.currentTimeMillis();
errno = client.set_metadata(group_name, remote_filename, meta_list, ProtoCommon.STORAGE_SET_METADATA_FLAG_MERGE);
System.out.println("set_metadata time used: " + (System.currentTimeMillis() - startTime) + " ms");
if (errno == 0) {
System.err.println("set_metadata success");
} else {
System.err.println("set_metadata fail, error no: " + errno);
}
meta_list = client.get_metadata(group_name, remote_filename);
if (meta_list != null) {
for (int i = 0; i < meta_list.length; i++) {
System.out.println(meta_list[i].getName() + " " + meta_list[i].getValue());
}
}
//Thread.sleep(30000);
startTime = System.currentTimeMillis();
file_buff = client.download_file(group_name, remote_filename);
System.out.println("download_file time used: " + (System.currentTimeMillis() - startTime) + " ms");
if (file_buff != null) {
System.out.println("file length:" + file_buff.length);
System.out.println((new String(file_buff)));
}
file_buff = "this is a slave buff".getBytes(ClientGlobal.g_charset);
master_filename = remote_filename;
prefix_name = "-part1";
file_ext_name = "txt";
startTime = System.currentTimeMillis();
results = client.upload_file(group_name, master_filename, prefix_name, file_buff, file_ext_name, meta_list);
System.out.println("upload_file time used: " + (System.currentTimeMillis() - startTime) + " ms");
if (results != null) {
System.err.println("slave file group_name: " + results[0] + ", remote_filename: " + results[1]);
generated_slave_filename = ProtoCommon.genSlaveFilename(master_filename, prefix_name, file_ext_name);
if (!generated_slave_filename.equals(results[1])) {
System.err.println("generated slave file: " + generated_slave_filename + "\n != returned slave file: " + results[1]);
}
System.err.println(client.get_file_info(results[0], results[1]));
}
startTime = System.currentTimeMillis();
errno = client.delete_file(group_name, remote_filename);
System.out.println("delete_file time used: " + (System.currentTimeMillis() - startTime) + " ms");
if (errno == 0) {
System.err.println("Delete file success");
} else {
System.err.println("Delete file fail, error no: " + errno);
}
}
results = client.upload_file(local_filename, null, meta_list);
if (results != null) {
String file_id;
int ts;
String token;
String file_url;
InetSocketAddress inetSockAddr;
group_name = results[0];
remote_filename = results[1];
file_id = group_name + StorageClient1.SPLIT_GROUP_NAME_AND_FILENAME_SEPERATOR + remote_filename;
inetSockAddr = trackerServer.getInetSocketAddress();
file_url = "http://" + inetSockAddr.getAddress().getHostAddress();
if (ClientGlobal.g_tracker_http_port != 80) {
file_url += ":" + ClientGlobal.g_tracker_http_port;
}
file_url += "/" + file_id;
if (ClientGlobal.g_anti_steal_token) {
ts = (int) (System.currentTimeMillis() / 1000);
token = ProtoCommon.getToken(file_id, ts, ClientGlobal.g_secret_key);
file_url += "?token=" + token + "&ts=" + ts;
}
System.err.println("group_name: " + group_name + ", remote_filename: " + remote_filename);
System.err.println(client.get_file_info(group_name, remote_filename));
System.err.println("file url: " + file_url);
errno = client.download_file(group_name, remote_filename, 0, 0, "c:\\" + remote_filename.replaceAll("/", "_"));
if (errno == 0) {
System.err.println("Download file success");
} else {
System.err.println("Download file fail, error no: " + errno);
}
errno = client.download_file(group_name, remote_filename, 0, 0, new DownloadFileWriter("c:\\" + remote_filename.replaceAll("/", "-")));
if (errno == 0) {
System.err.println("Download file success");
} else {
System.err.println("Download file fail, error no: " + errno);
}
master_filename = remote_filename;
prefix_name = "-part2";
file_ext_name = null;
startTime = System.currentTimeMillis();
results = client.upload_file(group_name, master_filename, prefix_name, local_filename, null, meta_list);
System.out.println("upload_file time used: " + (System.currentTimeMillis() - startTime) + " ms");
if (results != null) {
System.err.println("slave file group_name: " + results[0] + ", remote_filename: " + results[1]);
generated_slave_filename = ProtoCommon.genSlaveFilename(master_filename, prefix_name, file_ext_name);
if (!generated_slave_filename.equals(results[1])) {
System.err.println("generated slave file: " + generated_slave_filename + "\n != returned slave file: " + results[1]);
}
System.err.println(client.get_file_info(results[0], results[1]));
}
}
File f;
f = new File(local_filename);
int nPos = local_filename.lastIndexOf('.');
if (nPos > 0 && local_filename.length() - nPos <= ProtoCommon.FDFS_FILE_EXT_NAME_MAX_LEN + 1) {
file_ext_name = local_filename.substring(nPos + 1);
} else {
file_ext_name = null;
}
results = client.upload_file(null, f.length(),
new UploadLocalFileSender(local_filename), file_ext_name, meta_list);
if (results != null) {
group_name = results[0];
remote_filename = results[1];
System.out.println("group name: " + group_name + ", remote filename: " + remote_filename);
System.out.println(client.get_file_info(group_name, remote_filename));
master_filename = remote_filename;
prefix_name = "-part3";
startTime = System.currentTimeMillis();
results = client.upload_file(group_name, master_filename, prefix_name, f.length(), new UploadLocalFileSender(local_filename), file_ext_name, meta_list);
System.out.println("upload_file time used: " + (System.currentTimeMillis() - startTime) + " ms");
if (results != null) {
System.err.println("slave file group_name: " + results[0] + ", remote_filename: " + results[1]);
generated_slave_filename = ProtoCommon.genSlaveFilename(master_filename, prefix_name, file_ext_name);
if (!generated_slave_filename.equals(results[1])) {
System.err.println("generated slave file: " + generated_slave_filename + "\n != returned slave file: " + results[1]);
}
System.err.println(client.get_file_info(results[0], results[1]));
}
} else {
System.err.println("Upload file fail, error no: " + errno);
}
storageServer = tracker.getFetchStorage(trackerServer, group_name, remote_filename);
if (storageServer == null) {
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()));
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.*;
import java.io.File;
import java.net.InetSocketAddress;
@ -21,40 +20,40 @@ import java.net.InetSocketAddress;
* @version Version 1.16
*/
public class TestClient1 {
private TestClient1() {
}
/**
* entry point
*
* @param args comand arguments
* <ul><li>args[0]: config filename</li></ul>
* <ul><li>args[1]: local filename to upload</li></ul>
*/
public static void main(String args[]) {
if (args.length < 2) {
System.out.println("Error: Must have 2 parameters, one is config filename, "
+ "the other is the local filename to upload");
return;
private TestClient1() {
}
System.out.println("java.version=" + System.getProperty("java.version"));
/**
* entry point
*
* @param args comand arguments
* <ul><li>args[0]: config filename</li></ul>
* <ul><li>args[1]: local filename to upload</li></ul>
*/
public static void main(String args[]) {
if (args.length < 2) {
System.out.println("Error: Must have 2 parameters, one is config filename, "
+ "the other is the local filename to upload");
return;
}
String conf_filename = args[0];
String local_filename = args[1];
String group_name;
System.out.println("java.version=" + System.getProperty("java.version"));
try {
ClientGlobal.init(conf_filename);
System.out.println("network_timeout=" + ClientGlobal.g_network_timeout + "ms");
System.out.println("charset=" + ClientGlobal.g_charset);
String conf_filename = args[0];
String local_filename = args[1];
String group_name;
String file_id;
try {
ClientGlobal.init(conf_filename);
System.out.println("network_timeout=" + ClientGlobal.g_network_timeout + "ms");
System.out.println("charset=" + ClientGlobal.g_charset);
TrackerClient tracker = new TrackerClient();
TrackerServer trackerServer = tracker.getConnection();
String file_id;
StorageServer storageServer = null;
TrackerClient tracker = new TrackerClient();
TrackerServer trackerServer = tracker.getTrackerServer();
StorageServer storageServer = null;
/*
storageServer = tracker.getStoreStorage(trackerServer);
if (storageServer == null)
@ -63,204 +62,202 @@ public class TestClient1 {
return;
}
*/
StorageClient1 client = new StorageClient1(trackerServer, storageServer);
byte[] file_buff;
NameValuePair[] meta_list;
String master_file_id;
String prefix_name;
String file_ext_name;
String slave_file_id;
String generated_slave_file_id;
int errno;
StorageClient1 client = new StorageClient1(trackerServer, storageServer);
byte[] file_buff;
NameValuePair[] meta_list;
String master_file_id;
String prefix_name;
String file_ext_name;
String slave_file_id;
String generated_slave_file_id;
int errno;
group_name = "group1";
StorageServer[] storageServers = tracker.getStoreStorages(trackerServer, group_name);
if (storageServers == null) {
System.err.println("get store storage servers fail, error code: " + tracker.getErrorCode());
} else {
System.err.println("store storage servers count: " + storageServers.length);
for (int k = 0; k < storageServers.length; k++) {
System.err.println((k + 1) + ". " + storageServers[k].getInetSocketAddress().getAddress().getHostAddress() + ":" + storageServers[k].getInetSocketAddress().getPort());
}
System.err.println("");
}
group_name = "group1";
StorageServer[] storageServers = tracker.getStoreStorages(trackerServer, group_name);
if (storageServers == null) {
System.err.println("get store storage servers fail, error code: " + tracker.getErrorCode());
} else {
System.err.println("store storage servers count: " + storageServers.length);
for (int k = 0; k < storageServers.length; k++) {
System.err.println((k + 1) + ". " + storageServers[k].getInetSocketAddress().getAddress().getHostAddress() + ":" + storageServers[k].getInetSocketAddress().getPort());
}
System.err.println("");
}
meta_list = new NameValuePair[4];
meta_list[0] = new NameValuePair("width", "800");
meta_list[1] = new NameValuePair("heigth", "600");
meta_list[2] = new NameValuePair("bgcolor", "#FFFFFF");
meta_list[3] = new NameValuePair("author", "Mike");
meta_list = new NameValuePair[4];
meta_list[0] = new NameValuePair("width", "800");
meta_list[1] = new NameValuePair("heigth", "600");
meta_list[2] = new NameValuePair("bgcolor", "#FFFFFF");
meta_list[3] = new NameValuePair("author", "Mike");
file_buff = "this is a test".getBytes(ClientGlobal.g_charset);
System.out.println("file length: " + file_buff.length);
file_buff = "this is a test".getBytes(ClientGlobal.g_charset);
System.out.println("file length: " + file_buff.length);
file_id = client.upload_file1(file_buff, "txt", meta_list);
file_id = client.upload_file1(file_buff, "txt", meta_list);
/*
group_name = "group1";
file_id = client.upload_file1(group_name, file_buff, "txt", meta_list);
*/
if (file_id == null) {
System.err.println("upload file fail, error code: " + client.getErrorCode());
return;
} else {
System.err.println("file_id: " + file_id);
System.err.println(client.get_file_info1(file_id));
if (file_id == null) {
System.err.println("upload file fail, error code: " + client.getErrorCode());
return;
} else {
System.err.println("file_id: " + file_id);
System.err.println(client.get_file_info1(file_id));
ServerInfo[] servers = tracker.getFetchStorages1(trackerServer, file_id);
if (servers == null) {
System.err.println("get storage servers fail, error code: " + tracker.getErrorCode());
} else {
System.err.println("storage servers count: " + servers.length);
for (int k = 0; k < servers.length; k++) {
System.err.println((k + 1) + ". " + servers[k].getIpAddr() + ":" + servers[k].getPort());
}
System.err.println("");
ServerInfo[] servers = tracker.getFetchStorages1(trackerServer, file_id);
if (servers == null) {
System.err.println("get storage servers fail, error code: " + tracker.getErrorCode());
} else {
System.err.println("storage servers count: " + servers.length);
for (int k = 0; k < servers.length; k++) {
System.err.println((k + 1) + ". " + servers[k].getIpAddr() + ":" + servers[k].getPort());
}
System.err.println("");
}
meta_list = new NameValuePair[4];
meta_list[0] = new NameValuePair("width", "1024");
meta_list[1] = new NameValuePair("heigth", "768");
meta_list[2] = new NameValuePair("bgcolor", "#000000");
meta_list[3] = new NameValuePair("title", "Untitle");
if ((errno = client.set_metadata1(file_id, meta_list, ProtoCommon.STORAGE_SET_METADATA_FLAG_MERGE)) == 0) {
System.err.println("set_metadata success");
} else {
System.err.println("set_metadata fail, error no: " + errno);
}
meta_list = client.get_metadata1(file_id);
if (meta_list != null) {
for (int i = 0; i < meta_list.length; i++) {
System.out.println(meta_list[i].getName() + " " + meta_list[i].getValue());
}
}
//Thread.sleep(30000);
file_buff = client.download_file1(file_id);
if (file_buff != null) {
System.out.println("file length:" + file_buff.length);
System.out.println((new String(file_buff)));
}
master_file_id = file_id;
prefix_name = "-part1";
file_ext_name = "txt";
file_buff = "this is a slave buff.".getBytes(ClientGlobal.g_charset);
slave_file_id = client.upload_file1(master_file_id, prefix_name, file_buff, file_ext_name, meta_list);
if (slave_file_id != null) {
System.err.println("slave file_id: " + slave_file_id);
System.err.println(client.get_file_info1(slave_file_id));
generated_slave_file_id = ProtoCommon.genSlaveFilename(master_file_id, prefix_name, file_ext_name);
if (!generated_slave_file_id.equals(slave_file_id)) {
System.err.println("generated slave file: " + generated_slave_file_id + "\n != returned slave file: " + slave_file_id);
}
}
//Thread.sleep(10000);
if ((errno = client.delete_file1(file_id)) == 0) {
System.err.println("Delete file success");
} else {
System.err.println("Delete file fail, error no: " + errno);
}
}
if ((file_id = client.upload_file1(local_filename, null, meta_list)) != null) {
int ts;
String token;
String file_url;
InetSocketAddress inetSockAddr;
System.err.println("file_id: " + file_id);
System.err.println(client.get_file_info1(file_id));
inetSockAddr = trackerServer.getInetSocketAddress();
file_url = "http://" + inetSockAddr.getAddress().getHostAddress();
if (ClientGlobal.g_tracker_http_port != 80) {
file_url += ":" + ClientGlobal.g_tracker_http_port;
}
file_url += "/" + file_id;
if (ClientGlobal.g_anti_steal_token) {
ts = (int) (System.currentTimeMillis() / 1000);
token = ProtoCommon.getToken(file_id, ts, ClientGlobal.g_secret_key);
file_url += "?token=" + token + "&ts=" + ts;
}
System.err.println("file url: " + file_url);
errno = client.download_file1(file_id, 0, 100, "c:\\" + file_id.replaceAll("/", "_"));
if (errno == 0) {
System.err.println("Download file success");
} else {
System.err.println("Download file fail, error no: " + errno);
}
errno = client.download_file1(file_id, new DownloadFileWriter("c:\\" + file_id.replaceAll("/", "-")));
if (errno == 0) {
System.err.println("Download file success");
} else {
System.err.println("Download file fail, error no: " + errno);
}
master_file_id = file_id;
prefix_name = "-part2";
file_ext_name = null;
slave_file_id = client.upload_file1(master_file_id, prefix_name, local_filename, file_ext_name, meta_list);
if (slave_file_id != null) {
System.err.println("slave file_id: " + slave_file_id);
System.err.println(client.get_file_info1(slave_file_id));
generated_slave_file_id = ProtoCommon.genSlaveFilename(master_file_id, prefix_name, file_ext_name);
if (!generated_slave_file_id.equals(slave_file_id)) {
System.err.println("generated slave file: " + generated_slave_file_id + "\n != returned slave file: " + slave_file_id);
}
}
}
File f;
f = new File(local_filename);
int nPos = local_filename.lastIndexOf('.');
if (nPos > 0 && local_filename.length() - nPos <= ProtoCommon.FDFS_FILE_EXT_NAME_MAX_LEN + 1) {
file_ext_name = local_filename.substring(nPos + 1);
} else {
file_ext_name = null;
}
file_id = client.upload_file1(null, f.length(), new UploadLocalFileSender(local_filename), file_ext_name, meta_list);
if (file_id != null) {
System.out.println("file id: " + file_id);
System.out.println(client.get_file_info1(file_id));
master_file_id = file_id;
prefix_name = "-part3";
slave_file_id = client.upload_file1(master_file_id, prefix_name, f.length(), new UploadLocalFileSender(local_filename), file_ext_name, meta_list);
if (slave_file_id != null) {
System.err.println("slave file_id: " + slave_file_id);
generated_slave_file_id = ProtoCommon.genSlaveFilename(master_file_id, prefix_name, file_ext_name);
if (!generated_slave_file_id.equals(slave_file_id)) {
System.err.println("generated slave file: " + generated_slave_file_id + "\n != returned slave file: " + slave_file_id);
}
}
} else {
System.err.println("Upload file fail, error no: " + errno);
}
storageServer = tracker.getFetchStorage1(trackerServer, file_id);
if (storageServer == null) {
System.out.println("getFetchStorage fail, errno code: " + tracker.getErrorCode());
return;
}
/* for test only */
System.out.println("active test to storage server: " + storageServer.getConnection().activeTest());
/* for test only */
System.out.println("active test to tracker server: " + trackerServer.getConnection().activeTest());
} catch (Exception ex) {
ex.printStackTrace();
}
meta_list = new NameValuePair[4];
meta_list[0] = new NameValuePair("width", "1024");
meta_list[1] = new NameValuePair("heigth", "768");
meta_list[2] = new NameValuePair("bgcolor", "#000000");
meta_list[3] = new NameValuePair("title", "Untitle");
if ((errno = client.set_metadata1(file_id, meta_list, ProtoCommon.STORAGE_SET_METADATA_FLAG_MERGE)) == 0) {
System.err.println("set_metadata success");
} else {
System.err.println("set_metadata fail, error no: " + errno);
}
meta_list = client.get_metadata1(file_id);
if (meta_list != null) {
for (int i = 0; i < meta_list.length; i++) {
System.out.println(meta_list[i].getName() + " " + meta_list[i].getValue());
}
}
//Thread.sleep(30000);
file_buff = client.download_file1(file_id);
if (file_buff != null) {
System.out.println("file length:" + file_buff.length);
System.out.println((new String(file_buff)));
}
master_file_id = file_id;
prefix_name = "-part1";
file_ext_name = "txt";
file_buff = "this is a slave buff.".getBytes(ClientGlobal.g_charset);
slave_file_id = client.upload_file1(master_file_id, prefix_name, file_buff, file_ext_name, meta_list);
if (slave_file_id != null) {
System.err.println("slave file_id: " + slave_file_id);
System.err.println(client.get_file_info1(slave_file_id));
generated_slave_file_id = ProtoCommon.genSlaveFilename(master_file_id, prefix_name, file_ext_name);
if (!generated_slave_file_id.equals(slave_file_id)) {
System.err.println("generated slave file: " + generated_slave_file_id + "\n != returned slave file: " + slave_file_id);
}
}
//Thread.sleep(10000);
if ((errno = client.delete_file1(file_id)) == 0) {
System.err.println("Delete file success");
} else {
System.err.println("Delete file fail, error no: " + errno);
}
}
if ((file_id = client.upload_file1(local_filename, null, meta_list)) != null) {
int ts;
String token;
String file_url;
InetSocketAddress inetSockAddr;
System.err.println("file_id: " + file_id);
System.err.println(client.get_file_info1(file_id));
inetSockAddr = trackerServer.getInetSocketAddress();
file_url = "http://" + inetSockAddr.getAddress().getHostAddress();
if (ClientGlobal.g_tracker_http_port != 80) {
file_url += ":" + ClientGlobal.g_tracker_http_port;
}
file_url += "/" + file_id;
if (ClientGlobal.g_anti_steal_token) {
ts = (int) (System.currentTimeMillis() / 1000);
token = ProtoCommon.getToken(file_id, ts, ClientGlobal.g_secret_key);
file_url += "?token=" + token + "&ts=" + ts;
}
System.err.println("file url: " + file_url);
errno = client.download_file1(file_id, 0, 100, "c:\\" + file_id.replaceAll("/", "_"));
if (errno == 0) {
System.err.println("Download file success");
} else {
System.err.println("Download file fail, error no: " + errno);
}
errno = client.download_file1(file_id, new DownloadFileWriter("c:\\" + file_id.replaceAll("/", "-")));
if (errno == 0) {
System.err.println("Download file success");
} else {
System.err.println("Download file fail, error no: " + errno);
}
master_file_id = file_id;
prefix_name = "-part2";
file_ext_name = null;
slave_file_id = client.upload_file1(master_file_id, prefix_name, local_filename, file_ext_name, meta_list);
if (slave_file_id != null) {
System.err.println("slave file_id: " + slave_file_id);
System.err.println(client.get_file_info1(slave_file_id));
generated_slave_file_id = ProtoCommon.genSlaveFilename(master_file_id, prefix_name, file_ext_name);
if (!generated_slave_file_id.equals(slave_file_id)) {
System.err.println("generated slave file: " + generated_slave_file_id + "\n != returned slave file: " + slave_file_id);
}
}
}
File f;
f = new File(local_filename);
int nPos = local_filename.lastIndexOf('.');
if (nPos > 0 && local_filename.length() - nPos <= ProtoCommon.FDFS_FILE_EXT_NAME_MAX_LEN + 1) {
file_ext_name = local_filename.substring(nPos + 1);
} else {
file_ext_name = null;
}
file_id = client.upload_file1(null, f.length(), new UploadLocalFileSender(local_filename), file_ext_name, meta_list);
if (file_id != null) {
System.out.println("file id: " + file_id);
System.out.println(client.get_file_info1(file_id));
master_file_id = file_id;
prefix_name = "-part3";
slave_file_id = client.upload_file1(master_file_id, prefix_name, f.length(), new UploadLocalFileSender(local_filename), file_ext_name, meta_list);
if (slave_file_id != null) {
System.err.println("slave file_id: " + slave_file_id);
generated_slave_file_id = ProtoCommon.genSlaveFilename(master_file_id, prefix_name, file_ext_name);
if (!generated_slave_file_id.equals(slave_file_id)) {
System.err.println("generated slave file: " + generated_slave_file_id + "\n != returned slave file: " + slave_file_id);
}
}
} else {
System.err.println("Upload file fail, error no: " + errno);
}
storageServer = tracker.getFetchStorage1(trackerServer, file_id);
if (storageServer == null) {
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()));
trackerServer.close();
} 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();
}