Search in sources :

Example 21 with BackendConnection

use of com.actiontech.dble.backend.BackendConnection in project dble by actiontech.

the class ReloadConfig method findAndcloseFrontCon.

private static void findAndcloseFrontCon(BackendConnection con) {
    if (con instanceof MySQLConnection) {
        MySQLConnection mcon1 = (MySQLConnection) con;
        for (NIOProcessor processor : DbleServer.getInstance().getFrontProcessors()) {
            for (FrontendConnection fcon : processor.getFrontends().values()) {
                if (fcon instanceof ServerConnection) {
                    ServerConnection scon = (ServerConnection) fcon;
                    Map<RouteResultsetNode, BackendConnection> bons = scon.getSession2().getTargetMap();
                    for (BackendConnection bcon : bons.values()) {
                        if (bcon instanceof MySQLConnection) {
                            MySQLConnection mcon2 = (MySQLConnection) bcon;
                            if (mcon1 == mcon2) {
                                scon.killAndClose("reload config all");
                                return;
                            }
                        }
                    }
                }
            }
        }
    }
}
Also used : FrontendConnection(com.actiontech.dble.net.FrontendConnection) BackendConnection(com.actiontech.dble.backend.BackendConnection) RouteResultsetNode(com.actiontech.dble.route.RouteResultsetNode) ServerConnection(com.actiontech.dble.server.ServerConnection) NIOProcessor(com.actiontech.dble.net.NIOProcessor) MySQLConnection(com.actiontech.dble.backend.mysql.nio.MySQLConnection)

Example 22 with BackendConnection

use of com.actiontech.dble.backend.BackendConnection in project dble by actiontech.

the class ShowSession method getRow.

private static RowDataPacket getRow(ServerConnection sc, String charset) {
    StringBuilder sb = new StringBuilder();
    NonBlockingSession session = sc.getSession2();
    Collection<BackendConnection> backConnections = session.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(com.actiontech.dble.backend.BackendConnection) RowDataPacket(com.actiontech.dble.net.mysql.RowDataPacket) NonBlockingSession(com.actiontech.dble.server.NonBlockingSession)

Example 23 with BackendConnection

use of com.actiontech.dble.backend.BackendConnection 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);
        }
    }
}
Also used : PhysicalDBNode(com.actiontech.dble.backend.datasource.PhysicalDBNode) ServerConfig(com.actiontech.dble.config.ServerConfig) BackendConnection(com.actiontech.dble.backend.BackendConnection) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) RouteResultsetNode(com.actiontech.dble.route.RouteResultsetNode) ConcurrentMap(java.util.concurrent.ConcurrentMap) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) SQLSyntaxErrorException(java.sql.SQLSyntaxErrorException) MySQLOutPutException(com.actiontech.dble.plan.common.exception.MySQLOutPutException)

Example 24 with BackendConnection

use of com.actiontech.dble.backend.BackendConnection in project dble by actiontech.

the class MultiNodeSelectHandler method ownThreadJob.

private void ownThreadJob() {
    try {
        ArrayMinHeap<HeapItem> heap = new ArrayMinHeap<>(new Comparator<HeapItem>() {

            @Override
            public int compare(HeapItem o1, HeapItem o2) {
                RowDataPacket row1 = o1.getRowPacket();
                RowDataPacket row2 = o2.getRowPacket();
                if (row1 == null || row2 == null) {
                    if (row1 == row2)
                        return 0;
                    if (row1 == null)
                        return -1;
                    return 1;
                }
                return rowComparator.compare(row1, row2);
            }
        });
        // init heap
        for (Map.Entry<BackendConnection, BlockingQueue<HeapItem>> entry : queues.entrySet()) {
            HeapItem firstItem = entry.getValue().take();
            heap.add(firstItem);
        }
        while (!heap.isEmpty()) {
            if (isFail())
                return;
            HeapItem top = heap.peak();
            if (top.isNullItem()) {
                heap.poll();
            } else {
                BlockingQueue<HeapItem> topItemQueue = queues.get(top.getIndex());
                HeapItem item = topItemQueue.take();
                heap.replaceTop(item);
                // limit
                this.selectRows++;
                if (rrs.getLimitSize() >= 0) {
                    if (selectRows <= rrs.getLimitStart()) {
                        continue;
                    } else if (selectRows > (rrs.getLimitStart() < 0 ? 0 : rrs.getLimitStart()) + rrs.getLimitSize()) {
                        noNeedRows = true;
                        while (!heap.isEmpty()) {
                            HeapItem itemToDiscard = heap.poll();
                            if (!itemToDiscard.isNullItem()) {
                                BlockingQueue<HeapItem> discardQueue = queues.get(itemToDiscard.getIndex());
                                while (true) {
                                    if (discardQueue.take().isNullItem() || isFail()) {
                                        break;
                                    }
                                }
                            }
                        }
                        continue;
                    }
                }
                outputHandler.rowResponse(top.getRowData(), top.getRowPacket(), false, top.getIndex());
            }
        }
        Iterator<Map.Entry<BackendConnection, BlockingQueue<HeapItem>>> iterator = this.queues.entrySet().iterator();
        while (iterator.hasNext()) {
            Map.Entry<BackendConnection, BlockingQueue<HeapItem>> entry = iterator.next();
            entry.getValue().clear();
            session.releaseConnectionIfSafe(entry.getKey(), false);
            iterator.remove();
        }
        outputHandler.rowEofResponse(null, false, null);
        doSqlStat(session.getSource());
    } catch (Exception e) {
        String msg = "Merge thread error, " + e.getLocalizedMessage();
        LOGGER.info(msg, e);
        session.onQueryError(msg.getBytes());
    }
}
Also used : BlockingQueue(java.util.concurrent.BlockingQueue) LinkedBlockingQueue(java.util.concurrent.LinkedBlockingQueue) HeapItem(com.actiontech.dble.backend.mysql.nio.handler.util.HeapItem) BackendConnection(com.actiontech.dble.backend.BackendConnection) ArrayMinHeap(com.actiontech.dble.backend.mysql.nio.handler.util.ArrayMinHeap) RowDataPacket(com.actiontech.dble.net.mysql.RowDataPacket) IOException(java.io.IOException) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap)

Example 25 with BackendConnection

use of com.actiontech.dble.backend.BackendConnection in project dble by actiontech.

the class SingleNodeHandler method execute.

public void execute() throws Exception {
    startTime = System.currentTimeMillis();
    ServerConnection sc = session.getSource();
    waitingResponse = true;
    this.packetId = 0;
    final BackendConnection conn = session.getTarget(node);
    node.setRunOnSlave(rrs.getRunOnSlave());
    if (session.tryExistsCon(conn, node)) {
        execute(conn);
    } else {
        // create new connection
        node.setRunOnSlave(rrs.getRunOnSlave());
        ServerConfig conf = DbleServer.getInstance().getConfig();
        PhysicalDBNode dn = conf.getDataNodes().get(node.getName());
        dn.getConnection(dn.getDatabase(), session.getSource().isTxStart(), sc.isAutocommit(), node, this, node);
    }
}
Also used : PhysicalDBNode(com.actiontech.dble.backend.datasource.PhysicalDBNode) ServerConfig(com.actiontech.dble.config.ServerConfig) BackendConnection(com.actiontech.dble.backend.BackendConnection) ServerConnection(com.actiontech.dble.server.ServerConnection)

Aggregations

BackendConnection (com.actiontech.dble.backend.BackendConnection)29 RouteResultsetNode (com.actiontech.dble.route.RouteResultsetNode)12 MySQLConnection (com.actiontech.dble.backend.mysql.nio.MySQLConnection)8 PhysicalDBNode (com.actiontech.dble.backend.datasource.PhysicalDBNode)7 NIOProcessor (com.actiontech.dble.net.NIOProcessor)4 RowDataPacket (com.actiontech.dble.net.mysql.RowDataPacket)4 IOException (java.io.IOException)3 ServerConfig (com.actiontech.dble.config.ServerConfig)2 EOFPacket (com.actiontech.dble.net.mysql.EOFPacket)2 FieldPacket (com.actiontech.dble.net.mysql.FieldPacket)2 ServerConnection (com.actiontech.dble.server.ServerConnection)2 ByteBuffer (java.nio.ByteBuffer)2 Entry (java.util.Map.Entry)2 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)2 ConQueue (com.actiontech.dble.backend.ConQueue)1 PhysicalDBPool (com.actiontech.dble.backend.datasource.PhysicalDBPool)1 PhysicalDatasource (com.actiontech.dble.backend.datasource.PhysicalDatasource)1 GetConnectionHandler (com.actiontech.dble.backend.mysql.nio.handler.GetConnectionHandler)1 NewConnectionRespHandler (com.actiontech.dble.backend.mysql.nio.handler.NewConnectionRespHandler)1 ArrayMinHeap (com.actiontech.dble.backend.mysql.nio.handler.util.ArrayMinHeap)1