Search in sources :

Example 11 with BackendConnection

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;
}
Also used : BackendConnection(io.mycat.backend.BackendConnection) RowDataPacket(io.mycat.net.mysql.RowDataPacket) NonBlockingSession(io.mycat.server.NonBlockingSession)

Example 12 with BackendConnection

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);
}
Also used : BackendConnection(io.mycat.backend.BackendConnection) RowDataPacket(io.mycat.net.mysql.RowDataPacket) EOFPacket(io.mycat.net.mysql.EOFPacket) NIOProcessor(io.mycat.net.NIOProcessor) ByteBuffer(java.nio.ByteBuffer) FieldPacket(io.mycat.net.mysql.FieldPacket)

Example 13 with BackendConnection

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);
        }
    }
}
Also used : ScheduledExecutorService(java.util.concurrent.ScheduledExecutorService) BackendConnection(io.mycat.backend.BackendConnection) ConcurrentMap(java.util.concurrent.ConcurrentMap) NIOProcessor(io.mycat.net.NIOProcessor)

Example 14 with BackendConnection

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);
            }
        }
    }
}
Also used : PhysicalDBNode(io.mycat.backend.datasource.PhysicalDBNode) BackendConnection(io.mycat.backend.BackendConnection) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) RouteResultsetNode(io.mycat.route.RouteResultsetNode) MycatConfig(io.mycat.config.MycatConfig)

Example 15 with BackendConnection

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();
}
Also used : BackendConnection(io.mycat.backend.BackendConnection)

Aggregations

BackendConnection (io.mycat.backend.BackendConnection)25 RouteResultsetNode (io.mycat.route.RouteResultsetNode)7 PhysicalDBNode (io.mycat.backend.datasource.PhysicalDBNode)6 MycatConfig (io.mycat.config.MycatConfig)6 MySQLConnection (io.mycat.backend.mysql.nio.MySQLConnection)3 NIOProcessor (io.mycat.net.NIOProcessor)3 RowDataPacket (io.mycat.net.mysql.RowDataPacket)3 ByteBuffer (java.nio.ByteBuffer)3 Entry (java.util.Map.Entry)3 ConQueue (io.mycat.backend.ConQueue)2 EOFPacket (io.mycat.net.mysql.EOFPacket)2 FieldPacket (io.mycat.net.mysql.FieldPacket)2 IOException (java.io.IOException)2 ArrayList (java.util.ArrayList)2 LinkedList (java.util.LinkedList)2 PhysicalDBPool (io.mycat.backend.datasource.PhysicalDBPool)1 PhysicalDatasource (io.mycat.backend.datasource.PhysicalDatasource)1 JDBCConnection (io.mycat.backend.jdbc.JDBCConnection)1 GetConnectionHandler (io.mycat.backend.mysql.nio.handler.GetConnectionHandler)1 CoordinatorLogEntry (io.mycat.backend.mysql.xa.CoordinatorLogEntry)1