use of com.actiontech.dble.route.RouteResultsetNode in project dble by actiontech.
the class RouterUtil method routeFromParser.
public static RouteResultset routeFromParser(DruidParser druidParser, SchemaConfig schema, RouteResultset rrs, SQLStatement statement, String originSql, LayerCachePool cachePool, ServerSchemaStatVisitor visitor, ServerConnection sc) throws SQLException {
schema = druidParser.parser(schema, rrs, statement, originSql, cachePool, visitor, sc);
if (rrs.isFinishedExecute()) {
return null;
}
if (rrs.isFinishedRoute()) {
return rrs;
}
/**
* no name table or others
*/
DruidShardingParseInfo ctx = druidParser.getCtx();
if ((ctx.getTables() == null || ctx.getTables().size() == 0) && (ctx.getTableAliasMap() == null || ctx.getTableAliasMap().isEmpty())) {
if (schema == null) {
schema = DbleServer.getInstance().getConfig().getSchemas().get(SchemaUtil.getRandomDb());
}
return RouterUtil.routeToSingleNode(rrs, schema.getRandomDataNode());
}
/* multi-tables*/
if (druidParser.getCtx().getRouteCalculateUnits().size() == 0) {
RouteCalculateUnit routeCalculateUnit = new RouteCalculateUnit();
druidParser.getCtx().addRouteCalculateUnit(routeCalculateUnit);
}
SortedSet<RouteResultsetNode> nodeSet = new TreeSet<>();
for (RouteCalculateUnit unit : druidParser.getCtx().getRouteCalculateUnits()) {
RouteResultset rrsTmp = RouterUtil.tryRouteForTables(schema, druidParser.getCtx(), unit, rrs, isSelect(statement), cachePool);
if (rrsTmp != null && rrsTmp.getNodes() != null) {
Collections.addAll(nodeSet, rrsTmp.getNodes());
if (rrsTmp.isGlobalTable()) {
break;
}
}
}
RouteResultsetNode[] nodes = new RouteResultsetNode[nodeSet.size()];
int i = 0;
for (RouteResultsetNode aNodeSet : nodeSet) {
nodes[i] = aNodeSet;
i++;
}
rrs.setNodes(nodes);
return rrs;
}
use of com.actiontech.dble.route.RouteResultsetNode in project dble by actiontech.
the class NonBlockingSession method kill.
protected void kill() {
AtomicInteger count = new AtomicInteger(0);
Map<RouteResultsetNode, BackendConnection> toKilled = new HashMap<>();
for (Map.Entry<RouteResultsetNode, BackendConnection> entry : target.entrySet()) {
BackendConnection c = entry.getValue();
if (c != null && !c.isDDL()) {
toKilled.put(entry.getKey(), c);
count.incrementAndGet();
} else if (c != null && c.isDDL()) {
// if the sql executing is a ddl,do not kill the query,just close the connection
this.terminate();
return;
}
}
for (Entry<RouteResultsetNode, BackendConnection> en : toKilled.entrySet()) {
KillConnectionHandler kill = new KillConnectionHandler(en.getValue(), this);
ServerConfig conf = DbleServer.getInstance().getConfig();
PhysicalDBNode dn = conf.getDataNodes().get(en.getKey().getName());
try {
dn.getConnectionFromSameSource(en.getValue().getSchema(), true, en.getValue(), kill, en.getKey());
} catch (Exception e) {
LOGGER.info("get killer connection failed for " + en.getKey(), e);
kill.connectionError(e, null);
}
}
}
use of com.actiontech.dble.route.RouteResultsetNode in project dble by actiontech.
the class Explain2Handler method handle.
public static void handle(String stmt, ServerConnection c, int offset) {
try {
stmt = stmt.substring(offset);
if (!stmt.toLowerCase().contains("datanode=") || !stmt.toLowerCase().contains("sql=")) {
showError(stmt, c, "explain2 datanode=? sql=?");
return;
}
String dataNode = stmt.substring(stmt.indexOf("=") + 1, stmt.indexOf("sql=")).trim();
String sql = "explain " + stmt.substring(stmt.indexOf("sql=") + 4, stmt.length()).trim();
if (dataNode == null || dataNode.isEmpty() || sql == null || sql.isEmpty()) {
showError(stmt, c, "dataNode or sql is null or empty");
return;
}
RouteResultsetNode node = new RouteResultsetNode(dataNode, ServerParse.SELECT, sql);
RouteResultset rrs = new RouteResultset(sql, ServerParse.SELECT);
EMPTY_ARRAY[0] = node;
rrs.setNodes(EMPTY_ARRAY);
SingleNodeHandler singleNodeHandler = new SingleNodeHandler(rrs, c.getSession2());
singleNodeHandler.execute();
} catch (Exception e) {
LOGGER.info(e.getMessage(), e.getCause());
showError(stmt, c, e.getMessage());
}
}
use of com.actiontech.dble.route.RouteResultsetNode in project dble by actiontech.
the class ExplainHandler method buildHandlerTree.
private static String buildHandlerTree(DMLResponseHandler endHandler, Map<String, RefHandlerInfo> refMap, Map<DMLResponseHandler, RefHandlerInfo> handlerMap, Map<String, Integer> nameMap, Set<String> dependencies) {
String rootName = null;
int mergeNodeSize = endHandler.getMerges().size();
for (int i = 0; i < mergeNodeSize; i++) {
DMLResponseHandler startHandler = endHandler.getMerges().get(i);
MultiNodeMergeHandler mergeHandler = (MultiNodeMergeHandler) startHandler;
List<BaseSelectHandler> mergeList = new ArrayList<>();
mergeList.addAll(((MultiNodeMergeHandler) startHandler).getExeHandlers());
String mergeNode = genHandlerName("MERGE", nameMap);
RefHandlerInfo refInfo = new RefHandlerInfo(mergeNode, "MERGE");
handlerMap.put(mergeHandler, refInfo);
refMap.put(mergeNode, refInfo);
for (BaseSelectHandler exeHandler : mergeList) {
RouteResultsetNode rrss = exeHandler.getRrss();
String dateNode = rrss.getName() + "." + rrss.getMultiplexNum();
refInfo.addChild(dateNode);
String type = "BASE SQL";
if (dependencies != null && dependencies.size() > 0) {
type += "(May No Need)";
}
RefHandlerInfo baseSQLInfo = new RefHandlerInfo(dateNode, type, rrss.getStatement());
refMap.put(dateNode, baseSQLInfo);
if (dependencies != null && dependencies.size() > 0) {
baseSQLInfo.addAllStepChildren(dependencies);
}
}
String mergeRootName = getAllNodesFromLeaf(mergeHandler, refMap, handlerMap, nameMap);
if (rootName == null) {
if (mergeRootName == null) {
rootName = mergeNode;
} else {
rootName = mergeRootName;
}
}
}
return rootName;
}
use of com.actiontech.dble.route.RouteResultsetNode in project dble by actiontech.
the class ServerLoadDataInfileHandler method buildResultSet.
private RouteResultset buildResultSet(Map<String, LoadData> routeMap) {
statement.setLocal(true);
// druid will filter path, reset it now
SQLLiteralExpr fn = new SQLCharExpr(fileName);
statement.setFileName(fn);
// replace IGNORE X LINES in SQL to avoid IGNORING X LINE in every node.
String srcStatement = this.ignoreLinesDelete(statement.toString());
RouteResultset rrs = new RouteResultset(srcStatement, ServerParse.LOAD_DATA_INFILE_SQL);
rrs.setLoadData(true);
rrs.setStatement(srcStatement);
rrs.setFinishedRoute(true);
int size = routeMap.size();
RouteResultsetNode[] routeResultsetNodes = new RouteResultsetNode[size];
int index = 0;
for (Map.Entry<String, LoadData> entry : routeMap.entrySet()) {
RouteResultsetNode rrNode = new RouteResultsetNode(entry.getKey(), ServerParse.LOAD_DATA_INFILE_SQL, srcStatement);
rrNode.setStatement(srcStatement);
LoadData newLoadData = new LoadData();
ObjectUtil.copyProperties(loadData, newLoadData);
newLoadData.setLocal(true);
LoadData loadData1 = entry.getValue();
if (loadData1.getFileName() != null) {
newLoadData.setFileName(loadData1.getFileName());
} else {
newLoadData.setData(loadData1.getData());
}
rrNode.setLoadData(newLoadData);
routeResultsetNodes[index] = rrNode;
index++;
}
rrs.setNodes(routeResultsetNodes);
return rrs;
}
Aggregations