mysql handle socket
DESCRIPTION
Mysql5.1 handle socket testing and write the java code.TRANSCRIPT
MYSQL Handler Socket
Philip zhong5 .10 .2011
Outline
Why using Handler Socket How to build Handler Socket Plug-in Handler Socket configuration
parameter Handler Socket java client program Best Practice for java client Helpful sites
Why using Handler Socket
item with handlesocket without handlesocket
Process=16 and request=1000000*16
QPS:130429Mysql server Cpu(s): 29.0%us, 20.6%sy
QPS:95291Mysql server Cpu(s): 71.2%us, 20.8%sy
Process=32 and request=1000000*32
QPS:268294Mysql server Cpu(s): 58.1%us, 27.8%sy
QPS:103165Mysql server Cpu(s): 71.1%us, 21.1%sy
Process=64 and request=1000000*64
QPS:291960Mysql server Cpu(s): 58.8%us, 28.7%sy
QPS:104180Mysql server Cpu(s): 71.3%us, 22.3%sy
How to build Handler Socket Plug-in
./autogen.sh
./configure --prefix=/home/oracle/mysql5.1.55/lib/mysql/plugin --with-mysql-source=/package/mysql-5.1.55 --with-mysql-bindir=/home/oracle/mysql5.1.55/bin --with-mysql-plugindir=/home/oracle/mysql5.1.55/lib/mysql/plugin
make
make install
mysql> install plugin handlersocket soname 'handlersocket.so';
Handler Socket configuration parameter
• handlersocket_port (default = '9998') • handlersocket_port_wr (default = '9999')• handlersocket_threads (default = 16, min = 1, max = 3000)• handlersocket_threads_wr (default = 1, min = 1, max = 3000)• handlersocket_sndbuf (default = 0, min = 0, max = 1677216)• handlersocket_rcvbuf (default = 0, min = 0, max = 1677216)• handlersocket_readsize (default = 0, min = 0, max = 1677216)• handlersocket_wrlock_timeout (default = 12, min = 0, max = 3600)• handlersocket_timeout (default = 300, min = 30, max = 3600)• open_files_limit = 65535• innodb_buffer_pool_size =8G
Handler Socket java client program
Dependent Packages
hs4j-0.1.jarlog4j-1.2.8.jarslf4j-api-1.5.6.jarslf4j-log4j12-1.3.0.jar
Java Key methods(1)
• public HSClientImpl(InetSocketAddress inetSocketAddress, int poolSize)
• public IndexSession openIndexSession(String dbname, String tableName,String indexName, String[] columns)
• public IndexSession openIndexSession(int indexId, String dbname, String tableName, String indexName, String[] columns)
Java Key methods(2)
• public boolean insert(String[] values)• public int delete(String[] keys)• public int delete(String[] keys, FindOperator
operator)• public int delete(String[] keys, FindOperator
operator, int limit,int offset)• public int update(String[] keys, String[] values,
FindOperator operator)
Java Key methods(3)
• public int update(String[] keys, String[] values, FindOperator operator,int limit, int offset)
• public ResultSet find(String[] keys)• public ResultSet find(String[] keys,
FindOperator operator, int limit,int offset)• public ModifyStatement createStatement()
Create the MYSQL tablescreate table mt_data( guid varchar(18) not null, orgid int(9) not null, tabid int(9) not null, name varchar(128) not null, IsDeleted char(1) not null, createtime datetime not null, modifytime datetime not null, .... primary key(guid,orgid)) ENGINE=InnoDB CHARSET=utf8;
Java example code fragment• HSClient hsClient = new HSClientImpl(new
InetSocketAddress("10.224.56.188", 9999), connectionPoolSize);• final String[] columns = { "guid", "orgid", "tabid", "name",
"IsDeleted", "createtime", "modifytime", "value0",
"value1", "value2", "value3", "value4", "value5", "value6",
"value7", "value8", "value9", "value10", "value11",
"value12", "value13", "value14", "value15" };
IndexSession session = hsClient.openIndexSession("meetingdb","mt_data", "PRIMARY", columns);
• Bind values
final String[] values3 = new String[9];
values3[0] = guid;
values3[1] = "1";
……• session.insert(values)• hsClient.shutdown();
Best Practice for java client
• HSClient is thread-safe, so you must use it as SINGLETON object in your application.
• Open index is an expensive operation, so reuse an opened index id as much as possible.
• IndexSession is thread-safe,so please reuse an opened IndexSession as much as possible.
• Use IndexSession rather than HSClient • Use ModifyStatement to insert/update data
ModifyStatement example code
ModifyStatement stmt = this.session.createStatement();
stmt.setInt(1, 0);stmt.setString(2, "dennis");stmt.setInt(4, 27);stmt.setString(5, "2010-11-28 13:24:00");stmt.update(keys, FindOperator.EQ);
Helpful sites
• http://code.google.com/p/hs4j/• http://yoshinorimatsunobu.blogspot.com/201
0/10/using-mysql-as-nosql-story-for.html• https://github.com/ahiguti/HandlerSocket-Plu
gin-for-MySQL