use of com.actiontech.dble.server.ServerConnection in project dble by actiontech.
the class MultiNodeQueryHandler method okResponse.
@Override
public void okResponse(byte[] data, BackendConnection conn) {
this.netOutBytes += data.length;
boolean executeResponse = conn.syncAndExecute();
if (LOGGER.isDebugEnabled()) {
LOGGER.debug("received ok response ,executeResponse:" + executeResponse + " from " + conn);
}
if (executeResponse) {
ServerConnection source = session.getSource();
OkPacket ok = new OkPacket();
ok.read(data);
lock.lock();
try {
// the affected rows of global table will use the last node's response
if (!rrs.isGlobalTable()) {
affectedRows += ok.getAffectedRows();
} else {
affectedRows = ok.getAffectedRows();
}
if (ok.getInsertId() > 0) {
insertId = (insertId == 0) ? ok.getInsertId() : Math.min(insertId, ok.getInsertId());
}
if (--nodeCount > 0)
return;
if (isFail()) {
session.handleSpecial(rrs, source.getSchema(), false);
handleEndPacket(err.toBytes(), AutoTxOperation.ROLLBACK, conn);
return;
}
session.handleSpecial(rrs, source.getSchema(), true);
if (rrs.isLoadData()) {
byte lastPackId = source.getLoadDataInfileHandler().getLastPackId();
// OK_PACKET
ok.setPacketId(++lastPackId);
ok.setMessage(("Records: " + affectedRows + " Deleted: 0 Skipped: 0 Warnings: 0").getBytes());
source.getLoadDataInfileHandler().clear();
} else {
// OK_PACKET
ok.setPacketId(++packetId);
}
ok.setAffectedRows(affectedRows);
ok.setServerStatus(source.isAutocommit() ? 2 : 1);
if (insertId > 0) {
ok.setInsertId(insertId);
source.setLastInsertId(insertId);
}
handleEndPacket(ok.toBytes(), AutoTxOperation.COMMIT, conn);
} finally {
lock.unlock();
}
}
}
use of com.actiontech.dble.server.ServerConnection in project dble by actiontech.
the class SingleNodeHandler method okResponse.
/**
* insert/update/delete
* <p>
* okResponse():
* read data, make an OKPacket, write to writeQueue in FrontendConnection by ok.write(source)
*/
@Override
public void okResponse(byte[] data, BackendConnection conn) {
this.netOutBytes += data.length;
boolean executeResponse = conn.syncAndExecute();
if (executeResponse) {
session.handleSpecial(rrs, session.getSource().getSchema(), true);
ServerConnection source = session.getSource();
OkPacket ok = new OkPacket();
ok.read(data);
if (rrs.isLoadData()) {
byte lastPackId = source.getLoadDataInfileHandler().getLastPackId();
// OK_PACKET
ok.setPacketId(++lastPackId);
source.getLoadDataInfileHandler().clear();
} else {
// OK_PACKET
ok.setPacketId(++packetId);
}
ok.setServerStatus(source.isAutocommit() ? 2 : 1);
source.setLastInsertId(ok.getInsertId());
// handleSpecial
session.releaseConnectionIfSafe(conn, false);
session.setResponseTime();
ok.write(source);
waitingResponse = false;
}
}
use of com.actiontech.dble.server.ServerConnection in project dble by actiontech.
the class SingleNodeHandler method fieldEofResponse.
@Override
public void fieldEofResponse(byte[] header, List<byte[]> fields, List<FieldPacket> fieldPacketsNull, byte[] eof, boolean isLeft, BackendConnection conn) {
this.netOutBytes += header.length;
for (byte[] field : fields) {
this.netOutBytes += field.length;
}
String primaryKey = null;
if (rrs.hasPrimaryKeyToCache()) {
String[] items = rrs.getPrimaryKeyItems();
primaryKeyTable = items[0];
primaryKey = items[1];
}
header[3] = ++packetId;
ServerConnection source = session.getSource();
buffer = source.writeToBuffer(header, allocBuffer());
for (int i = 0, len = fields.size(); i < len; ++i) {
byte[] field = fields.get(i);
field[3] = ++packetId;
// save field
FieldPacket fieldPk = new FieldPacket();
fieldPk.read(field);
if (rrs.getSchema() != null) {
fieldPk.setDb(rrs.getSchema().getBytes());
}
if (rrs.getTableAlias() != null) {
fieldPk.setTable(rrs.getTableAlias().getBytes());
}
if (rrs.getTable() != null) {
fieldPk.setOrgTable(rrs.getTable().getBytes());
}
fieldPackets.add(fieldPk);
// find primary key index
if (primaryKey != null && primaryKeyIndex == -1) {
String fieldName = new String(fieldPk.getName());
if (primaryKey.equalsIgnoreCase(fieldName)) {
primaryKeyIndex = i;
}
}
buffer = fieldPk.write(buffer, source, false);
}
fieldCount = fieldPackets.size();
eof[3] = ++packetId;
buffer = source.writeToBuffer(eof, buffer);
}
use of com.actiontech.dble.server.ServerConnection in project dble by actiontech.
the class SingleNodeHandler method backConnectionErr.
private void backConnectionErr(ErrorPacket errPkg, BackendConnection conn) {
ServerConnection source = session.getSource();
String errUser = source.getUser();
String errHost = source.getHost();
int errPort = source.getLocalPort();
String errMsg = " errNo:" + errPkg.getErrNo() + " " + new String(errPkg.getMessage());
LOGGER.info("execute sql err :" + errMsg + " con:" + conn + " frontend host:" + errHost + "/" + errPort + "/" + errUser);
session.releaseConnectionIfSafe(conn, false);
source.setTxInterrupt(errMsg);
session.handleSpecial(rrs, session.getSource().getSchema(), false);
//
if (waitingResponse) {
errPkg.setPacketId(1);
errPkg.write(source);
waitingResponse = false;
}
recycleResources();
}
use of com.actiontech.dble.server.ServerConnection in project dble by actiontech.
the class OutputHandler method fieldEofResponse.
@Override
public void fieldEofResponse(byte[] headerNull, List<byte[]> fieldsNull, List<FieldPacket> fieldPackets, byte[] eofNull, boolean isLeft, BackendConnection conn) {
if (terminate.get()) {
return;
}
lock.lock();
try {
if (this.isBinary)
this.fieldPackets = fieldPackets;
ResultSetHeaderPacket hp = new ResultSetHeaderPacket();
hp.setFieldCount(fieldPackets.size());
hp.setPacketId(++packetId);
ServerConnection source = session.getSource();
buffer = hp.write(buffer, source, true);
for (FieldPacket fp : fieldPackets) {
fp.setPacketId(++packetId);
buffer = fp.write(buffer, source, true);
}
EOFPacket ep = new EOFPacket();
ep.setPacketId(++packetId);
buffer = ep.write(buffer, source, true);
} finally {
lock.unlock();
}
}
Aggregations