use of io.mycat.server.ServerConnection in project Mycat-Server by MyCATApache.
the class SingleNodeHandler method rowEofResponse.
/**
* select
*
* 行结束标志返回时触发,将EOF标志写入缓冲区,最后调用source.write(buffer)将缓冲区放入前端连接的写缓冲队列中,等待NIOSocketWR将其发送给应用
*/
@Override
public void rowEofResponse(byte[] eof, BackendConnection conn) {
this.netOutBytes += eof.length;
ServerConnection source = session.getSource();
conn.recordSql(source.getHost(), source.getSchema(), node.getStatement());
// 判断是调用存储过程的话不能在这里释放链接
if (!rrs.isCallStatement() || (rrs.isCallStatement() && rrs.getProcedure().isResultSimpleValue())) {
session.releaseConnectionIfSafe(conn, LOGGER.isDebugEnabled(), false);
endRunning();
}
eof[3] = ++packetId;
buffer = source.writeToBuffer(eof, allocBuffer());
int resultSize = source.getWriteQueue().size() * MycatServer.getInstance().getConfig().getSystem().getBufferPoolPageSize();
resultSize = resultSize + buffer.position();
source.write(buffer);
//TODO: add by zhuam
//查询结果派发
QueryResult queryResult = new QueryResult(session.getSource().getUser(), rrs.getSqlType(), rrs.getStatement(), affectedRows, netInBytes, netOutBytes, startTime, System.currentTimeMillis(), resultSize);
QueryResultDispatcher.dispatchQuery(queryResult);
}
use of io.mycat.server.ServerConnection in project Mycat-Server by MyCATApache.
the class SingleNodeHandler method connectionError.
@Override
public void connectionError(Throwable e, BackendConnection conn) {
endRunning();
ErrorPacket err = new ErrorPacket();
err.packetId = ++packetId;
err.errno = ErrorCode.ER_NEW_ABORTING_CONNECTION;
err.message = StringUtil.encode(e.getMessage(), session.getSource().getCharset());
ServerConnection source = session.getSource();
source.write(err.write(allocBuffer(), source, true));
}
use of io.mycat.server.ServerConnection in project Mycat-Server by MyCATApache.
the class SingleNodeHandler method backConnectionErr.
private void backConnectionErr(ErrorPacket errPkg, BackendConnection conn) {
endRunning();
ServerConnection source = session.getSource();
String errUser = source.getUser();
String errHost = source.getHost();
int errPort = source.getLocalPort();
String errmgs = " errno:" + errPkg.errno + " " + new String(errPkg.message);
LOGGER.warn("execute sql err :" + errmgs + " con:" + conn + " frontend host:" + errHost + "/" + errPort + "/" + errUser);
session.releaseConnectionIfSafe(conn, LOGGER.isDebugEnabled(), false);
source.setTxInterrupt(errmgs);
/**
* TODO: 修复全版本BUG
*
* BUG复现:
* 1、MysqlClient: SELECT 9223372036854775807 + 1;
* 2、MyCatServer: ERROR 1690 (22003): BIGINT value is out of range in '(9223372036854775807 + 1)'
* 3、MysqlClient: ERROR 2013 (HY000): Lost connection to MySQL server during query
*
* Fixed后
* 1、MysqlClient: SELECT 9223372036854775807 + 1;
* 2、MyCatServer: ERROR 1690 (22003): BIGINT value is out of range in '(9223372036854775807 + 1)'
* 3、MysqlClient: ERROR 1690 (22003): BIGINT value is out of range in '(9223372036854775807 + 1)'
*
*/
// 由于 pakcetId != 1 造成的问题
errPkg.packetId = 1;
errPkg.write(source);
recycleResources();
}
use of io.mycat.server.ServerConnection in project Mycat-Server by MyCATApache.
the class ShowConnection method getRow.
private static RowDataPacket getRow(FrontendConnection c, String charset) {
RowDataPacket row = new RowDataPacket(FIELD_COUNT);
row.add(c.getProcessor().getName().getBytes());
row.add(LongUtil.toBytes(c.getId()));
row.add(StringUtil.encode(c.getHost(), charset));
row.add(IntegerUtil.toBytes(c.getPort()));
row.add(IntegerUtil.toBytes(c.getLocalPort()));
row.add(StringUtil.encode(c.getUser(), charset));
row.add(StringUtil.encode(c.getSchema(), charset));
row.add(StringUtil.encode(c.getCharset() + ":" + c.getCharsetIndex(), charset));
row.add(LongUtil.toBytes(c.getNetInBytes()));
row.add(LongUtil.toBytes(c.getNetOutBytes()));
row.add(LongUtil.toBytes((TimeUtil.currentTimeMillis() - c.getStartupTime()) / 1000L));
ByteBuffer bb = c.getReadBuffer();
row.add(IntegerUtil.toBytes(bb == null ? 0 : bb.capacity()));
row.add(IntegerUtil.toBytes(c.getWriteQueue().size()));
String txLevel = "";
String txAutommit = "";
if (c instanceof ServerConnection) {
ServerConnection mysqlC = (ServerConnection) c;
txLevel = mysqlC.getTxIsolation() + "";
txAutommit = mysqlC.isAutocommit() + "";
}
row.add(txLevel.getBytes());
row.add(txAutommit.getBytes());
return row;
}
use of io.mycat.server.ServerConnection in project Mycat-Server by MyCATApache.
the class DataMergeService method run.
@Override
public void run() {
// @since 2016-03-23
if (!running.compareAndSet(false, true)) {
return;
}
// eof handler has been placed to "if (pack == END_FLAG_PACK){}" in for-statement
// @author Uncle-pan
// @since 2016-03-23
boolean nulpack = false;
try {
// loop-on-packs
for (; ; ) {
final PackWraper pack = packs.poll();
// @since 2016-03-23
if (pack == null) {
nulpack = true;
break;
}
// eof: handling eof pack and exit
if (pack == END_FLAG_PACK) {
final int warningCount = 0;
final EOFPacket eofp = new EOFPacket();
final ByteBuffer eof = ByteBuffer.allocate(9);
BufferUtil.writeUB3(eof, eofp.calcPacketSize());
eof.put(eofp.packetId);
eof.put(eofp.fieldCount);
BufferUtil.writeUB2(eof, warningCount);
BufferUtil.writeUB2(eof, eofp.status);
final ServerConnection source = multiQueryHandler.getSession().getSource();
final byte[] array = eof.array();
multiQueryHandler.outputMergeResult(source, array, getResults(array));
break;
}
// merge: sort-or-group, or simple add
final RowDataPacket row = new RowDataPacket(fieldCount);
row.read(pack.rowData);
if (grouper != null) {
grouper.addRow(row);
} else if (sorter != null) {
if (!sorter.addRow(row)) {
canDiscard.put(pack.dataNode, true);
}
} else {
result.get(pack.dataNode).add(row);
}
}
// rof
} catch (final Exception e) {
multiQueryHandler.handleDataProcessException(e);
} finally {
running.set(false);
}
// @since 2016-03-23
if (nulpack && !packs.isEmpty()) {
this.run();
}
}
Aggregations