use of com.actiontech.dble.backend.datasource.PhysicalDBNode in project dble by actiontech.
the class MultiNodeQueryHandler method execute.
public void execute() throws Exception {
lock.lock();
try {
this.reset(rrs.getNodes().length);
this.fieldsReturned = false;
this.affectedRows = 0L;
this.insertId = 0L;
} finally {
lock.unlock();
}
startTime = System.currentTimeMillis();
LOGGER.debug("rrs.getRunOnSlave()-" + rrs.getRunOnSlave());
StringBuilder sb = new StringBuilder();
for (final RouteResultsetNode node : rrs.getNodes()) {
if (node.isModifySQL()) {
sb.append("[").append(node.getName()).append("]").append(node.getStatement()).append(";\n");
}
}
if (sb.length() > 0) {
TxnLogHelper.putTxnLog(session.getSource(), sb.toString());
}
for (final RouteResultsetNode node : rrs.getNodes()) {
BackendConnection conn = session.getTarget(node);
if (session.tryExistsCon(conn, node)) {
node.setRunOnSlave(rrs.getRunOnSlave());
innerExecute(conn, node);
} else {
// create new connection
node.setRunOnSlave(rrs.getRunOnSlave());
PhysicalDBNode dn = DbleServer.getInstance().getConfig().getDataNodes().get(node.getName());
dn.getConnection(dn.getDatabase(), session.getSource().isTxStart(), sessionAutocommit, node, this, node);
}
}
session.endDelive();
}
use of com.actiontech.dble.backend.datasource.PhysicalDBNode in project dble by actiontech.
the class BaseSelectHandler method initConnection.
public MySQLConnection initConnection() throws Exception {
if (session.closed()) {
return null;
}
MySQLConnection exeConn = (MySQLConnection) session.getTarget(rrss);
if (session.tryExistsCon(exeConn, rrss)) {
return exeConn;
} else {
PhysicalDBNode dn = DbleServer.getInstance().getConfig().getDataNodes().get(rrss.getName());
// autocommit is session.getSource().isAutocommit() && !session.getSource().isTxStart()
final BackendConnection newConn = dn.getConnection(dn.getDatabase(), autocommit, autocommit);
session.bindConnection(rrss, newConn);
return (MySQLConnection) newConn;
}
}
use of com.actiontech.dble.backend.datasource.PhysicalDBNode in project dble by actiontech.
the class FetchStoreNodeOfChildTableHandler method execute.
public String execute(String schema, ArrayList<String> dataNodes) {
String key = schema + ":" + sql;
CachePool cache = DbleServer.getInstance().getCacheService().getCachePool("ER_SQL2PARENTID");
if (cache != null) {
String cacheResult = (String) cache.get(key);
if (cacheResult != null) {
return cacheResult;
}
}
int totalCount = dataNodes.size();
LOGGER.debug("find child node with sql:" + sql);
for (String dn : dataNodes) {
if (!LOGGER.isDebugEnabled()) {
// no early return when debug
if (dataNode != null) {
LOGGER.debug(" found return ");
return dataNode;
}
}
PhysicalDBNode mysqlDN = DbleServer.getInstance().getConfig().getDataNodes().get(dn);
try {
if (LOGGER.isDebugEnabled()) {
LOGGER.debug("execute in data_node " + dn);
}
RouteResultsetNode node = new RouteResultsetNode(dn, ServerParse.SELECT, sql);
// get child node from master
node.setRunOnSlave(false);
BackendConnection conn = session.getTarget(node);
if (session.tryExistsCon(conn, node)) {
if (session.closed()) {
session.clearResources(true);
return null;
}
conn.setResponseHandler(this);
conn.setSession(session);
((MySQLConnection) conn).setComplexQuery(true);
conn.execute(node, session.getSource(), isAutoCommit());
} else {
mysqlDN.getConnection(mysqlDN.getDatabase(), session.getSource().isTxStart(), session.getSource().isAutocommit(), node, this, node);
}
} catch (Exception e) {
LOGGER.info("get connection err " + e);
}
}
lock.lock();
try {
while (dataNode == null) {
try {
result.await(50, TimeUnit.MILLISECONDS);
} catch (InterruptedException e) {
break;
}
if (dataNode != null || finished.get() >= totalCount) {
break;
}
}
} finally {
lock.unlock();
}
if (!LOGGER.isDebugEnabled()) {
// no cached when debug
if (dataNode != null && cache != null) {
cache.putIfAbsent(key, dataNode);
}
}
return dataNode;
}
use of com.actiontech.dble.backend.datasource.PhysicalDBNode in project dble by actiontech.
the class MultiNodeDdlHandler method execute.
public void execute() throws Exception {
lock.lock();
try {
this.reset(rrs.getNodes().length);
} finally {
lock.unlock();
}
LOGGER.debug("rrs.getRunOnSlave()-" + rrs.getRunOnSlave());
StringBuilder sb = new StringBuilder();
for (final RouteResultsetNode node : rrs.getNodes()) {
if (node.isModifySQL()) {
sb.append("[").append(node.getName()).append("]").append(node.getStatement()).append(";\n");
}
}
if (sb.length() > 0) {
TxnLogHelper.putTxnLog(session.getSource(), sb.toString());
}
for (final RouteResultsetNode node : rrs.getNodes()) {
BackendConnection conn = session.getTarget(node);
if (session.tryExistsCon(conn, node)) {
node.setRunOnSlave(rrs.getRunOnSlave());
innerExecute(conn, node);
} else {
// create new connection
node.setRunOnSlave(rrs.getRunOnSlave());
PhysicalDBNode dn = DbleServer.getInstance().getConfig().getDataNodes().get(node.getName());
dn.getConnection(dn.getDatabase(), true, sessionAutocommit, node, this, node);
}
}
}
use of com.actiontech.dble.backend.datasource.PhysicalDBNode in project dble by actiontech.
the class ConfigInitializer method initDataNodes.
private Map<String, PhysicalDBNode> initDataNodes(SchemaLoader schemaLoader) {
Map<String, DataNodeConfig> nodeConf = schemaLoader.getDataNodes();
Map<String, PhysicalDBNode> nodes = new HashMap<>(nodeConf.size());
for (DataNodeConfig conf : nodeConf.values()) {
PhysicalDBPool pool = this.dataHosts.get(conf.getDataHost());
if (pool == null) {
throw new ConfigException("dataHost not exists " + conf.getDataHost());
}
PhysicalDBNode dataNode = new PhysicalDBNode(conf.getName(), conf.getDatabase(), pool);
nodes.put(dataNode.getName(), dataNode);
}
return nodes;
}
Aggregations