use of io.mycat.backend.BackendConnection in project Mycat-Server by MyCATApache.
the class ShowSession method getRow.
private static RowDataPacket getRow(ServerConnection sc, String charset) {
StringBuilder sb = new StringBuilder();
NonBlockingSession ssesion = sc.getSession2();
Collection<BackendConnection> backConnections = ssesion.getTargetMap().values();
int cncount = backConnections.size();
if (cncount == 0) {
return null;
}
for (BackendConnection backCon : backConnections) {
sb.append(backCon).append("\r\n");
}
RowDataPacket row = new RowDataPacket(FIELD_COUNT);
row.add(StringUtil.encode(sc.getId() + "", charset));
row.add(StringUtil.encode(cncount + "", charset));
row.add(StringUtil.encode(sb.toString(), charset));
return row;
}
use of io.mycat.backend.BackendConnection in project Mycat-Server by MyCATApache.
the class ShowBackend method execute.
public static void execute(ManagerConnection c) {
ByteBuffer buffer = c.allocate();
buffer = header.write(buffer, c, true);
for (FieldPacket field : fields) {
buffer = field.write(buffer, c, true);
}
buffer = eof.write(buffer, c, true);
byte packetId = eof.packetId;
String charset = c.getCharset();
for (NIOProcessor p : MycatServer.getInstance().getProcessors()) {
for (BackendConnection bc : p.getBackends().values()) {
if (bc != null) {
RowDataPacket row = getRow(bc, charset);
row.packetId = ++packetId;
buffer = row.write(buffer, c, true);
}
}
}
EOFPacket lastEof = new EOFPacket();
lastEof.packetId = ++packetId;
buffer = lastEof.write(buffer, c, true);
c.write(buffer);
}
use of io.mycat.backend.BackendConnection in project Mycat-Server by MyCATApache.
the class SwitchPrepareCheckRunner method run.
@Override
public void run() {
if (!allSwitchRunnerSet.contains(taskID)) {
return;
}
ScheduledExecutorService scheduledExecutorService = MycatServer.getInstance().getScheduler();
ConcurrentMap<String, ConcurrentMap<String, List<PartitionByCRC32PreSlot.Range>>> migrateRuleMap = RouteCheckRule.migrateRuleMap;
String schemal = taskNode.getSchema().toUpperCase();
if (!migrateRuleMap.containsKey(schemal) || !migrateRuleMap.get(schemal).containsKey(taskNode.getTable().toUpperCase())) {
scheduledExecutorService.schedule(this, 3, TimeUnit.SECONDS);
return;
}
boolean isHasInTransation = false;
NIOProcessor[] processors = MycatServer.getInstance().getProcessors();
for (NIOProcessor processor : processors) {
Collection<BackendConnection> backendConnections = processor.getBackends().values();
for (BackendConnection backendConnection : backendConnections) {
isHasInTransation = checkIsInTransation(backendConnection);
if (isHasInTransation) {
scheduledExecutorService.schedule(this, 3, TimeUnit.SECONDS);
return;
}
}
}
for (BackendConnection backendConnection : NIOProcessor.backends_old) {
isHasInTransation = checkIsInTransation(backendConnection);
if (isHasInTransation) {
scheduledExecutorService.schedule(this, 3, TimeUnit.SECONDS);
return;
}
}
//增加判断binlog完成
if (!isHasInTransation) {
try {
//先判断后端binlog都完成了才算本任务完成
boolean allIncrentmentSucess = true;
List<String> dataHosts = ZKUtils.getConnection().getChildren().forPath(taskPath);
for (String dataHostName : dataHosts) {
if ("_prepare".equals(dataHostName) || "_commit".equals(dataHostName) || "_clean".equals(dataHostName))
continue;
List<MigrateTask> migrateTaskList = JSON.parseArray(new String(ZKUtils.getConnection().getData().forPath(taskPath + "/" + dataHostName), "UTF-8"), MigrateTask.class);
for (MigrateTask migrateTask : migrateTaskList) {
String zkPath = taskPath + "/" + dataHostName + "/" + migrateTask.getFrom() + "-" + migrateTask.getTo();
if (ZKUtils.getConnection().checkExists().forPath(zkPath) != null) {
TaskStatus taskStatus = JSON.parseObject(new String(ZKUtils.getConnection().getData().forPath(zkPath), "UTF-8"), TaskStatus.class);
if (taskStatus.getStatus() != 3) {
allIncrentmentSucess = false;
break;
}
} else {
allIncrentmentSucess = false;
break;
}
}
}
if (allIncrentmentSucess) {
//需要关闭binlog,不然后续的清楚老数据会删除数据
BinlogStream stream = BinlogStreamHoder.binlogStreamMap.get(taskID);
if (stream != null) {
BinlogStreamHoder.binlogStreamMap.remove(taskID);
stream.disconnect();
}
String myID = ZkConfig.getInstance().getValue(ZkParamCfg.ZK_CFG_MYID);
String path = taskPath + "/_commit/" + myID;
if (ZKUtils.getConnection().checkExists().forPath(path) == null) {
ZKUtils.getConnection().create().creatingParentsIfNeeded().forPath(path);
}
allSwitchRunnerSet.remove(taskID);
} else {
scheduledExecutorService.schedule(this, 3, TimeUnit.SECONDS);
}
} catch (Exception e) {
LOGGER.error("error:", e);
}
}
}
use of io.mycat.backend.BackendConnection in project Mycat-Server by MyCATApache.
the class NonBlockingSession method kill.
// public boolean tryExistsCon(final BackendConnection conn,
// RouteResultsetNode node) {
//
// if (conn == null) {
// return false;
// }
// if (!conn.isFromSlaveDB()
// || node.canRunnINReadDB(getSource().isAutocommit())) {
// if (LOGGER.isDebugEnabled()) {
// LOGGER.debug("found connections in session to use " + conn
// + " for " + node);
// }
// conn.setAttachment(node);
// return true;
// } else {
// // slavedb connection and can't use anymore ,release it
// if (LOGGER.isDebugEnabled()) {
// LOGGER.debug("release slave connection,can't be used in trasaction "
// + conn + " for " + node);
// }
// releaseConnection(node, LOGGER.isDebugEnabled(), false);
// }
// return false;
// }
protected void kill() {
boolean hooked = false;
AtomicInteger count = null;
Map<RouteResultsetNode, BackendConnection> killees = null;
for (RouteResultsetNode node : target.keySet()) {
BackendConnection c = target.get(node);
if (c != null) {
if (!hooked) {
hooked = true;
killees = new HashMap<RouteResultsetNode, BackendConnection>();
count = new AtomicInteger(0);
}
killees.put(node, c);
count.incrementAndGet();
}
}
if (hooked) {
for (Entry<RouteResultsetNode, BackendConnection> en : killees.entrySet()) {
KillConnectionHandler kill = new KillConnectionHandler(en.getValue(), this);
MycatConfig conf = MycatServer.getInstance().getConfig();
PhysicalDBNode dn = conf.getDataNodes().get(en.getKey().getName());
try {
dn.getConnectionFromSameSource(null, true, en.getValue(), kill, en.getKey());
} catch (Exception e) {
LOGGER.error("get killer connection failed for " + en.getKey(), e);
kill.connectionError(e, null);
}
}
}
}
use of io.mycat.backend.BackendConnection in project Mycat-Server by MyCATApache.
the class NonBlockingSession method terminate.
/**
* {@link ServerConnection#isClosed()} must be true before invoking this
*/
public void terminate() {
for (BackendConnection node : target.values()) {
node.close("client closed ");
}
target.clear();
clearHandlesResources();
}
Aggregations