Search in sources :

Example 6 with NIOProcessor

use of io.mycat.net.NIOProcessor in project Mycat-Server by MyCATApache.

the class ShowConnection method execute.

public static void execute(ManagerConnection c) {
    ByteBuffer buffer = c.allocate();
    // write header
    buffer = header.write(buffer, c, true);
    // write fields
    for (FieldPacket field : fields) {
        buffer = field.write(buffer, c, true);
    }
    // write eof
    buffer = eof.write(buffer, c, true);
    // write rows
    byte packetId = eof.packetId;
    String charset = c.getCharset();
    NIOProcessor[] processors = MycatServer.getInstance().getProcessors();
    for (NIOProcessor p : processors) {
        for (FrontendConnection fc : p.getFrontends().values()) {
            if (fc != null) {
                RowDataPacket row = getRow(fc, charset);
                row.packetId = ++packetId;
                buffer = row.write(buffer, c, true);
            }
        }
    }
    // write last eof
    EOFPacket lastEof = new EOFPacket();
    lastEof.packetId = ++packetId;
    buffer = lastEof.write(buffer, c, true);
    // write buffer
    c.write(buffer);
}
Also used : FrontendConnection(io.mycat.net.FrontendConnection) 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 7 with NIOProcessor

use of io.mycat.net.NIOProcessor in project Mycat-Server by MyCATApache.

the class ShowRouter method execute.

public static void execute(ManagerConnection c) {
    ByteBuffer buffer = c.allocate();
    // write header
    buffer = header.write(buffer, c, true);
    // write fields
    for (FieldPacket field : fields) {
        buffer = field.write(buffer, c, true);
    }
    // write eof
    buffer = eof.write(buffer, c, true);
    // write rows
    byte packetId = eof.packetId;
    for (NIOProcessor p : MycatServer.getInstance().getProcessors()) {
        RowDataPacket row = getRow(p, c.getCharset());
        row.packetId = ++packetId;
        buffer = row.write(buffer, c, true);
    }
    // write last eof
    EOFPacket lastEof = new EOFPacket();
    lastEof.packetId = ++packetId;
    buffer = lastEof.write(buffer, c, true);
    // write buffer
    c.write(buffer);
}
Also used : 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 8 with NIOProcessor

use of io.mycat.net.NIOProcessor 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 9 with NIOProcessor

use of io.mycat.net.NIOProcessor 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 10 with NIOProcessor

use of io.mycat.net.NIOProcessor in project Mycat-Server by MyCATApache.

the class JDBCDatasource method createNewConnection.

@Override
public void createNewConnection(ResponseHandler handler, String schema) throws IOException {
    DBHostConfig cfg = getConfig();
    JDBCConnection c = new JDBCConnection();
    c.setHost(cfg.getIp());
    c.setPort(cfg.getPort());
    c.setPool(this);
    c.setSchema(schema);
    c.setDbType(cfg.getDbType());
    NIOProcessor processor = (NIOProcessor) MycatServer.getInstance().nextProcessor();
    c.setProcessor(processor);
    //复用mysql的Backend的ID,需要在process中存储
    c.setId(NIOConnector.ID_GENERATOR.getId());
    processor.addBackend(c);
    try {
        Connection con = getConnection();
        // c.setIdleTimeout(pool.getConfig().getIdleTimeout());
        c.setCon(con);
        // notify handler
        handler.connectionAcquired(c);
    } catch (Exception e) {
        handler.connectionError(e, c);
    }
}
Also used : DBHostConfig(io.mycat.config.model.DBHostConfig) Connection(java.sql.Connection) NIOProcessor(io.mycat.net.NIOProcessor) IOException(java.io.IOException) SQLException(java.sql.SQLException)

Aggregations

NIOProcessor (io.mycat.net.NIOProcessor)15 EOFPacket (io.mycat.net.mysql.EOFPacket)7 FieldPacket (io.mycat.net.mysql.FieldPacket)7 RowDataPacket (io.mycat.net.mysql.RowDataPacket)7 ByteBuffer (java.nio.ByteBuffer)7 FrontendConnection (io.mycat.net.FrontendConnection)5 BackendConnection (io.mycat.backend.BackendConnection)3 PhysicalDBPool (io.mycat.backend.datasource.PhysicalDBPool)2 JDBCConnection (io.mycat.backend.jdbc.JDBCConnection)2 MySQLConnection (io.mycat.backend.mysql.nio.MySQLConnection)2 ArrayList (java.util.ArrayList)2 PhysicalDBNode (io.mycat.backend.datasource.PhysicalDBNode)1 PhysicalDatasource (io.mycat.backend.datasource.PhysicalDatasource)1 DirectByteBufferPool (io.mycat.buffer.DirectByteBufferPool)1 ConfigInitializer (io.mycat.config.ConfigInitializer)1 MycatCluster (io.mycat.config.MycatCluster)1 MycatConfig (io.mycat.config.MycatConfig)1 DBHostConfig (io.mycat.config.model.DBHostConfig)1 FirewallConfig (io.mycat.config.model.FirewallConfig)1 SchemaConfig (io.mycat.config.model.SchemaConfig)1