Search in sources :

Example 6 with SchemaConfig

use of com.actiontech.dble.config.model.SchemaConfig in project dble by actiontech.

the class ShowDatabase 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.getPacketId();
    Map<String, SchemaConfig> schemas = DbleServer.getInstance().getConfig().getSchemas();
    for (String name : new TreeSet<>(schemas.keySet())) {
        RowDataPacket row = new RowDataPacket(FIELD_COUNT);
        row.add(StringUtil.encode(name, c.getCharset().getResults()));
        row.setPacketId(++packetId);
        buffer = row.write(buffer, c, true);
    }
    // write lastEof
    EOFPacket lastEof = new EOFPacket();
    lastEof.setPacketId(++packetId);
    buffer = lastEof.write(buffer, c, true);
    // write buffer
    c.write(buffer);
}
Also used : SchemaConfig(com.actiontech.dble.config.model.SchemaConfig) TreeSet(java.util.TreeSet) RowDataPacket(com.actiontech.dble.net.mysql.RowDataPacket) EOFPacket(com.actiontech.dble.net.mysql.EOFPacket) ByteBuffer(java.nio.ByteBuffer) FieldPacket(com.actiontech.dble.net.mysql.FieldPacket)

Example 7 with SchemaConfig

use of com.actiontech.dble.config.model.SchemaConfig in project dble by actiontech.

the class ShowTableDataNode method execute.

public static void execute(ManagerConnection c, String tableInfo) {
    Matcher ma = PATTERN_FOR_TABLE_INFO.matcher(tableInfo);
    if (!ma.matches()) {
        c.writeErrMessage(ErrorCode.ER_UNKNOWN_ERROR, "The Correct Query Format Is:show @@datanodes where schema=? and table =?");
        return;
    }
    String schemaName = ma.group(2);
    String tableName = ma.group(4);
    if (DbleServer.getInstance().getSystemVariables().isLowerCaseTableNames()) {
        schemaName = schemaName.toLowerCase();
        tableName = tableName.toLowerCase();
    }
    SchemaConfig schemaConfig = DbleServer.getInstance().getConfig().getSchemas().get(schemaName);
    List<String> dataNodes;
    if (schemaConfig == null) {
        c.writeErrMessage(ErrorCode.ER_UNKNOWN_ERROR, "the schema [" + schemaName + "] does not exists");
        return;
    } else if (schemaConfig.isNoSharding()) {
        dataNodes = Collections.singletonList(schemaConfig.getDataNode());
    } else {
        TableConfig tableConfig = schemaConfig.getTables().get(tableName);
        if (tableConfig == null) {
            if (schemaConfig.getDataNode() == null) {
                c.writeErrMessage(ErrorCode.ER_UNKNOWN_ERROR, "the table [" + tableName + "] in schema [" + schemaName + "] does not exists");
                return;
            } else {
                dataNodes = Collections.singletonList(schemaConfig.getDataNode());
            }
        } else {
            dataNodes = tableConfig.getDataNodes();
        }
    }
    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.getPacketId();
    for (RowDataPacket row : getRows(dataNodes, c.getCharset().getResults())) {
        row.setPacketId(++packetId);
        buffer = row.write(buffer, c, true);
    }
    // write last eof
    EOFPacket lastEof = new EOFPacket();
    lastEof.setPacketId(++packetId);
    buffer = lastEof.write(buffer, c, true);
    // post write
    c.write(buffer);
}
Also used : SchemaConfig(com.actiontech.dble.config.model.SchemaConfig) Matcher(java.util.regex.Matcher) RowDataPacket(com.actiontech.dble.net.mysql.RowDataPacket) EOFPacket(com.actiontech.dble.net.mysql.EOFPacket) TableConfig(com.actiontech.dble.config.model.TableConfig) ByteBuffer(java.nio.ByteBuffer) FieldPacket(com.actiontech.dble.net.mysql.FieldPacket)

Example 8 with SchemaConfig

use of com.actiontech.dble.config.model.SchemaConfig in project dble by actiontech.

the class DbleServer method tryRecovery.

private void tryRecovery(CoordinatorLogEntry coordinatorLogEntry, boolean needCommit) {
    StringBuilder xaCmd = new StringBuilder();
    if (needCommit) {
        xaCmd.append("XA COMMIT ");
    } else {
        xaCmd.append("XA ROLLBACK ");
    }
    boolean finished = true;
    for (int j = 0; j < coordinatorLogEntry.getParticipants().length; j++) {
        ParticipantLogEntry participantLogEntry = coordinatorLogEntry.getParticipants()[j];
        // XA commit
        if (participantLogEntry.getTxState() != TxState.TX_COMMIT_FAILED_STATE && participantLogEntry.getTxState() != TxState.TX_COMMITTING_STATE && participantLogEntry.getTxState() != TxState.TX_PREPARE_UNCONNECT_STATE && participantLogEntry.getTxState() != TxState.TX_ROLLBACKING_STATE && participantLogEntry.getTxState() != TxState.TX_ROLLBACK_FAILED_STATE && participantLogEntry.getTxState() != TxState.TX_PREPARED_STATE) {
            continue;
        }
        finished = false;
        outLoop: for (SchemaConfig schema : DbleServer.getInstance().getConfig().getSchemas().values()) {
            for (TableConfig table : schema.getTables().values()) {
                for (String dataNode : table.getDataNodes()) {
                    PhysicalDBNode dn = DbleServer.getInstance().getConfig().getDataNodes().get(dataNode);
                    if (participantLogEntry.compareAddress(dn.getDbPool().getSource().getConfig().getIp(), dn.getDbPool().getSource().getConfig().getPort(), dn.getDatabase())) {
                        OneRawSQLQueryResultHandler resultHandler = new OneRawSQLQueryResultHandler(new String[0], new XARecoverCallback(needCommit, participantLogEntry));
                        xaCmd.append(coordinatorLogEntry.getId().substring(0, coordinatorLogEntry.getId().length() - 1));
                        xaCmd.append(".");
                        xaCmd.append(dn.getDatabase());
                        xaCmd.append("'");
                        SQLJob sqlJob = new SQLJob(xaCmd.toString(), dn.getDatabase(), resultHandler, dn.getDbPool().getSource());
                        sqlJob.run();
                        LOGGER.debug(String.format("[%s] Host:[%s] schema:[%s]", xaCmd, dn.getName(), dn.getDatabase()));
                        // reset xaCmd
                        xaCmd.setLength(0);
                        if (needCommit) {
                            xaCmd.append("XA COMMIT ");
                        } else {
                            xaCmd.append("XA ROLLBACK ");
                        }
                        break outLoop;
                    }
                }
            }
        }
    }
    if (finished) {
        XAStateLog.saveXARecoveryLog(coordinatorLogEntry.getId(), needCommit ? TxState.TX_COMMITTED_STATE : TxState.TX_ROLLBACKED_STATE);
        XAStateLog.writeCheckpoint(coordinatorLogEntry.getId());
    }
}
Also used : PhysicalDBNode(com.actiontech.dble.backend.datasource.PhysicalDBNode) OneRawSQLQueryResultHandler(com.actiontech.dble.sqlengine.OneRawSQLQueryResultHandler) SchemaConfig(com.actiontech.dble.config.model.SchemaConfig) SQLJob(com.actiontech.dble.sqlengine.SQLJob) TableConfig(com.actiontech.dble.config.model.TableConfig)

Example 9 with SchemaConfig

use of com.actiontech.dble.config.model.SchemaConfig in project dble by actiontech.

the class ProxyMetaManager method tableStructureCheck.

private void tableStructureCheck(Set<String> selfNode) {
    for (SchemaConfig schema : DbleServer.getInstance().getConfig().getSchemas().values()) {
        if (!checkDbExists(schema.getName())) {
            continue;
        }
        for (TableConfig table : schema.getTables().values()) {
            if (!checkTableExists(schema.getName(), table.getName())) {
                continue;
            }
            AbstractTableMetaHandler handler = new TableMetaCheckHandler(this, schema.getName(), table, selfNode);
            handler.execute();
        }
    }
}
Also used : SchemaConfig(com.actiontech.dble.config.model.SchemaConfig) TableConfig(com.actiontech.dble.config.model.TableConfig)

Example 10 with SchemaConfig

use of com.actiontech.dble.config.model.SchemaConfig in project dble by actiontech.

the class DruidInsertParser method parserChildTable.

private void parserChildTable(SchemaInfo schemaInfo, final RouteResultset rrs, MySqlInsertStatement insertStmt, final ServerConnection sc) throws SQLNonTransientException {
    final SchemaConfig schema = schemaInfo.getSchemaConfig();
    String tableName = schemaInfo.getTable();
    final TableConfig tc = schema.getTables().get(tableName);
    if (isMultiInsert(insertStmt)) {
        String msg = "ChildTable multi insert not provided";
        LOGGER.info(msg);
        throw new SQLNonTransientException(msg);
    }
    String joinKey = tc.getJoinKey();
    int joinKeyIndex = getJoinKeyIndex(schemaInfo, insertStmt, joinKey);
    final String joinKeyVal = insertStmt.getValues().getValues().get(joinKeyIndex).toString();
    String realVal = StringUtil.removeApostrophe(joinKeyVal);
    final String sql = RouterUtil.removeSchema(insertStmt.toString(), schemaInfo.getSchema());
    rrs.setStatement(sql);
    // try to route by ER parent partion key
    RouteResultset theRrs = routeByERParentKey(rrs, tc, realVal);
    if (theRrs != null) {
        rrs.setFinishedRoute(true);
    } else {
        rrs.setFinishedExecute(true);
        DbleServer.getInstance().getComplexQueryExecutor().execute(new Runnable() {

            // get child result will be blocked, so use ComplexQueryExecutor
            @Override
            public void run() {
                // route by sql query root parent's data node
                String findRootTBSql = tc.getLocateRTableKeySql().toLowerCase() + joinKeyVal;
                if (LOGGER.isDebugEnabled()) {
                    LOGGER.debug("to find root parent's node sql :" + findRootTBSql);
                }
                FetchStoreNodeOfChildTableHandler fetchHandler = new FetchStoreNodeOfChildTableHandler(findRootTBSql, sc.getSession2());
                String dn = fetchHandler.execute(schema.getName(), tc.getRootParent().getDataNodes());
                if (dn == null) {
                    sc.writeErrMessage(ErrorCode.ER_UNKNOWN_ERROR, "can't find (root) parent sharding node for sql:" + sql);
                    return;
                }
                if (LOGGER.isDebugEnabled()) {
                    LOGGER.debug("found partition node for child table to insert " + dn + " sql :" + sql);
                }
                RouterUtil.routeToSingleNode(rrs, dn);
                sc.getSession2().execute(rrs);
            }
        });
    }
}
Also used : SQLNonTransientException(java.sql.SQLNonTransientException) SchemaConfig(com.actiontech.dble.config.model.SchemaConfig) FetchStoreNodeOfChildTableHandler(com.actiontech.dble.backend.mysql.nio.handler.FetchStoreNodeOfChildTableHandler) TableConfig(com.actiontech.dble.config.model.TableConfig) RouteResultset(com.actiontech.dble.route.RouteResultset)

Aggregations

SchemaConfig (com.actiontech.dble.config.model.SchemaConfig)70 TableConfig (com.actiontech.dble.config.model.TableConfig)16 SQLNonTransientException (java.sql.SQLNonTransientException)16 Test (org.junit.Test)15 RouteResultset (com.actiontech.dble.route.RouteResultset)6 PhysicalDBNode (com.actiontech.dble.backend.datasource.PhysicalDBNode)5 ServerConfig (com.actiontech.dble.config.ServerConfig)5 UserConfig (com.actiontech.dble.config.model.UserConfig)5 SQLStatement (com.alibaba.druid.sql.ast.SQLStatement)5 MySqlStatementParser (com.alibaba.druid.sql.dialect.mysql.parser.MySqlStatementParser)5 CacheService (com.actiontech.dble.cache.CacheService)4 EOFPacket (com.actiontech.dble.net.mysql.EOFPacket)4 FieldPacket (com.actiontech.dble.net.mysql.FieldPacket)4 RowDataPacket (com.actiontech.dble.net.mysql.RowDataPacket)4 StringPtr (com.actiontech.dble.plan.common.ptr.StringPtr)4 ByteBuffer (java.nio.ByteBuffer)4 ArrayList (java.util.ArrayList)4 PhysicalDBPool (com.actiontech.dble.backend.datasource.PhysicalDBPool)3 ERTable (com.actiontech.dble.config.model.ERTable)3 FirewallConfig (com.actiontech.dble.config.model.FirewallConfig)3