use of io.mycat.route.RouteResultsetNode in project Mycat-Server by MyCATApache.
the class LoadDataUtil method requestFileDataResponse.
public static void requestFileDataResponse(byte[] data, BackendConnection conn) {
byte packId = data[3];
BackendAIOConnection backendAIOConnection = (BackendAIOConnection) conn;
RouteResultsetNode rrn = (RouteResultsetNode) conn.getAttachment();
LoadData loadData = rrn.getLoadData();
List<String> loadDataData = loadData.getData();
try {
if (loadDataData != null && loadDataData.size() > 0) {
ByteArrayOutputStream bos = new ByteArrayOutputStream();
for (int i = 0, loadDataDataSize = loadDataData.size(); i < loadDataDataSize; i++) {
String line = loadDataData.get(i);
String s = (i == loadDataDataSize - 1) ? line : line + loadData.getLineTerminatedBy();
byte[] bytes = s.getBytes(loadData.getCharset());
bos.write(bytes);
}
packId = writeToBackConnection(packId, new ByteArrayInputStream(bos.toByteArray()), backendAIOConnection);
} else {
//从文件读取
packId = writeToBackConnection(packId, new BufferedInputStream(new FileInputStream(loadData.getFileName())), backendAIOConnection);
}
} catch (IOException e) {
throw new RuntimeException(e);
} finally {
//结束必须发空包
byte[] empty = new byte[] { 0, 0, 0, 3 };
empty[3] = ++packId;
backendAIOConnection.write(empty);
}
}
use of io.mycat.route.RouteResultsetNode in project Mycat-Server by MyCATApache.
the class MultiNodeQueryHandler method connectionAcquired.
@Override
public void connectionAcquired(final BackendConnection conn) {
final RouteResultsetNode node = (RouteResultsetNode) conn.getAttachment();
session.bindConnection(node, conn);
_execute(conn, node);
}
use of io.mycat.route.RouteResultsetNode in project Mycat-Server by MyCATApache.
the class RollbackNodeHandler method rollback.
public void rollback() {
final int initCount = session.getTargetCount();
lock.lock();
try {
reset(initCount);
} finally {
lock.unlock();
}
if (session.closed()) {
decrementCountToZero();
return;
}
// 执行
int started = 0;
for (final RouteResultsetNode node : session.getTargetKeys()) {
if (node == null) {
LOGGER.error("null is contained in RoutResultsetNodes, source = " + session.getSource());
continue;
}
final BackendConnection conn = session.getTarget(node);
if (conn != null) {
boolean isClosed = conn.isClosedOrQuit();
if (isClosed) {
session.getSource().writeErrMessage(ErrorCode.ER_UNKNOWN_ERROR, "receive rollback,but find backend con is closed or quit");
LOGGER.error(conn + "receive rollback,but fond backend con is closed or quit");
}
if (LOGGER.isDebugEnabled()) {
LOGGER.debug("rollback job run for " + conn);
}
if (clearIfSessionClosed(session)) {
return;
}
conn.setResponseHandler(RollbackNodeHandler.this);
//support the XA rollback
if (session.getXaTXID() != null && conn instanceof MySQLConnection) {
MySQLConnection mysqlCon = (MySQLConnection) conn;
String xaTxId = session.getXaTXID();
//exeBatch cmd issue : the 2nd package can not receive the response
mysqlCon.execCmd("XA END " + xaTxId + ";");
mysqlCon.execCmd("XA ROLLBACK " + xaTxId + ";");
} else {
conn.rollback();
}
++started;
}
}
if (started < initCount && decrementCountBy(initCount - started)) {
/**
* assumption: only caused by front-end connection close. <br/>
* Otherwise, packet must be returned to front-end
*/
session.clearResources(true);
}
}
use of io.mycat.route.RouteResultsetNode in project Mycat-Server by MyCATApache.
the class PostgreSQLBackendConnection method query.
/**********
* 此查询用于心跳检查和获取连接后的健康检查
*/
@Override
public void query(String query) throws UnsupportedEncodingException {
RouteResultsetNode rrn = new RouteResultsetNode("default", ServerParse.SELECT, query);
synAndDoExecute(null, rrn, this.charsetIndex, this.txIsolation, true);
}
use of io.mycat.route.RouteResultsetNode in project Mycat-Server by MyCATApache.
the class DruidInsertParser method parserBatchInsert.
/**
* insert into .... select .... 或insert into table() values (),(),....
* @param schema
* @param rrs
* @param insertStmt
* @throws SQLNonTransientException
*/
private void parserBatchInsert(SchemaConfig schema, RouteResultset rrs, String partitionColumn, String tableName, MySqlInsertStatement insertStmt) throws SQLNonTransientException {
//insert into table() values (),(),....
if (insertStmt.getValuesList().size() > 1) {
//字段列数
int columnNum = insertStmt.getColumns().size();
int shardingColIndex = getShardingColIndex(insertStmt, partitionColumn);
if (shardingColIndex == -1) {
String msg = "bad insert sql (sharding column:" + partitionColumn + " not provided," + insertStmt;
LOGGER.warn(msg);
throw new SQLNonTransientException(msg);
} else {
List<ValuesClause> valueClauseList = insertStmt.getValuesList();
Map<Integer, List<ValuesClause>> nodeValuesMap = new HashMap<Integer, List<ValuesClause>>();
Map<Integer, Integer> slotsMap = new HashMap<>();
TableConfig tableConfig = schema.getTables().get(tableName);
AbstractPartitionAlgorithm algorithm = tableConfig.getRule().getRuleAlgorithm();
for (ValuesClause valueClause : valueClauseList) {
if (valueClause.getValues().size() != columnNum) {
String msg = "bad insert sql columnSize != valueSize:" + columnNum + " != " + valueClause.getValues().size() + "values:" + valueClause;
LOGGER.warn(msg);
throw new SQLNonTransientException(msg);
}
SQLExpr expr = valueClause.getValues().get(shardingColIndex);
String shardingValue = null;
if (expr instanceof SQLIntegerExpr) {
SQLIntegerExpr intExpr = (SQLIntegerExpr) expr;
shardingValue = intExpr.getNumber() + "";
} else if (expr instanceof SQLCharExpr) {
SQLCharExpr charExpr = (SQLCharExpr) expr;
shardingValue = charExpr.getText();
}
Integer nodeIndex = algorithm.calculate(shardingValue);
if (algorithm instanceof SlotFunction) {
slotsMap.put(nodeIndex, ((SlotFunction) algorithm).slotValue());
}
//没找到插入的分片
if (nodeIndex == null) {
String msg = "can't find any valid datanode :" + tableName + " -> " + partitionColumn + " -> " + shardingValue;
LOGGER.warn(msg);
throw new SQLNonTransientException(msg);
}
if (nodeValuesMap.get(nodeIndex) == null) {
nodeValuesMap.put(nodeIndex, new ArrayList<ValuesClause>());
}
nodeValuesMap.get(nodeIndex).add(valueClause);
}
RouteResultsetNode[] nodes = new RouteResultsetNode[nodeValuesMap.size()];
int count = 0;
for (Map.Entry<Integer, List<ValuesClause>> node : nodeValuesMap.entrySet()) {
Integer nodeIndex = node.getKey();
List<ValuesClause> valuesList = node.getValue();
insertStmt.setValuesList(valuesList);
nodes[count] = new RouteResultsetNode(tableConfig.getDataNodes().get(nodeIndex), rrs.getSqlType(), insertStmt.toString());
if (algorithm instanceof SlotFunction) {
nodes[count].setSlot(slotsMap.get(nodeIndex));
nodes[count].setStatement(ParseUtil.changeInsertAddSlot(nodes[count].getStatement(), nodes[count].getSlot()));
}
nodes[count++].setSource(rrs);
}
rrs.setNodes(nodes);
rrs.setFinishedRoute(true);
}
} else if (insertStmt.getQuery() != null) {
// insert into .... select ....
String msg = "TODO:insert into .... select .... not supported!";
LOGGER.warn(msg);
throw new SQLNonTransientException(msg);
}
}
Aggregations