Search in sources :

Example 61 with SchemaConfig

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

the class RollbackConfig method rollback.

public static void rollback() throws Exception {
    ServerConfig conf = DbleServer.getInstance().getConfig();
    Map<String, PhysicalDBPool> dataHosts = conf.getBackupDataHosts();
    Map<String, UserConfig> users = conf.getBackupUsers();
    Map<String, SchemaConfig> schemas = conf.getBackupSchemas();
    Map<String, PhysicalDBNode> dataNodes = conf.getBackupDataNodes();
    FirewallConfig firewall = conf.getBackupFirewall();
    Map<ERTable, Set<ERTable>> erRelations = conf.getBackupErRelations();
    boolean backDataHostWithoutWR = conf.backDataHostWithoutWR();
    if (conf.canRollback()) {
        conf.rollback(users, schemas, dataNodes, dataHosts, erRelations, firewall, backDataHostWithoutWR);
    } else if (conf.canRollbackAll()) {
        boolean rollbackStatus = true;
        String errorMsg = null;
        for (PhysicalDBPool dn : dataHosts.values()) {
            dn.init(dn.getActiveIndex());
            if (!dn.isInitSuccess()) {
                rollbackStatus = false;
                errorMsg = "dataHost[" + dn.getHostName() + "] inited failure";
                break;
            }
        }
        // INIT FAILED
        if (!rollbackStatus) {
            for (PhysicalDBPool dn : dataHosts.values()) {
                dn.clearDataSources("rollbackup config");
                dn.stopHeartbeat();
            }
            throw new Exception(errorMsg);
        }
        final Map<String, PhysicalDBPool> cNodes = conf.getDataHosts();
        // apply
        conf.rollback(users, schemas, dataNodes, dataHosts, erRelations, firewall, backDataHostWithoutWR);
        // stop old resource heartbeat
        for (PhysicalDBPool dn : cNodes.values()) {
            dn.clearDataSources("clear old config ");
            dn.stopHeartbeat();
        }
        AlarmAppender.rollbackConfig();
    } else {
        throw new Exception("there is no old version");
    }
}
Also used : PhysicalDBNode(com.actiontech.dble.backend.datasource.PhysicalDBNode) SchemaConfig(com.actiontech.dble.config.model.SchemaConfig) Set(java.util.Set) PhysicalDBPool(com.actiontech.dble.backend.datasource.PhysicalDBPool) ERTable(com.actiontech.dble.config.model.ERTable) UserConfig(com.actiontech.dble.config.model.UserConfig) FirewallConfig(com.actiontech.dble.config.model.FirewallConfig) ServerConfig(com.actiontech.dble.config.ServerConfig) Map(java.util.Map)

Example 62 with SchemaConfig

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

the class DruidUpdateParserTest method throwExceptionParse.

public void throwExceptionParse(String sql, boolean throwException) throws NoSuchMethodException {
    MySqlStatementParser parser = new MySqlStatementParser(sql);
    List<SQLStatement> statementList = parser.parseStatementList();
    SQLStatement sqlStatement = statementList.get(0);
    MySqlUpdateStatement update = (MySqlUpdateStatement) sqlStatement;
    SchemaConfig schemaConfig = mock(SchemaConfig.class);
    Map<String, TableConfig> tables = mock(Map.class);
    TableConfig tableConfig = mock(TableConfig.class);
    String tableName = "hotnews";
    when((schemaConfig).getTables()).thenReturn(tables);
    when(tables.get(tableName)).thenReturn(tableConfig);
    when(tableConfig.getParentTC()).thenReturn(null);
    RouteResultset routeResultset = new RouteResultset(sql, 11);
    Class c = DruidUpdateParser.class;
    Method method = c.getDeclaredMethod("confirmShardColumnNotUpdated", new Class[] { SQLUpdateStatement.class, SchemaConfig.class, String.class, String.class, String.class, RouteResultset.class });
    method.setAccessible(true);
    try {
        method.invoke(c.newInstance(), update, schemaConfig, tableName, "ID", "", routeResultset);
        if (throwException) {
            System.out.println("Not passed without exception is not correct");
            Assert.assertTrue(false);
        } else {
            System.out.println("Passed without exception. Maybe the partition key exists in update statement,but not update in fact");
            Assert.assertTrue(true);
        }
    } catch (Exception e) {
        if (throwException) {
            System.out.println(e.getCause().getClass());
            Assert.assertTrue(e.getCause() instanceof SQLNonTransientException);
            System.out.println("SQLNonTransientException is expected");
        } else {
            System.out.println("need checked");
            Assert.assertTrue(false);
        }
    }
}
Also used : SchemaConfig(com.actiontech.dble.config.model.SchemaConfig) Method(java.lang.reflect.Method) SQLStatement(com.alibaba.druid.sql.ast.SQLStatement) SQLNonTransientException(java.sql.SQLNonTransientException) MySqlUpdateStatement(com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlUpdateStatement) SQLNonTransientException(java.sql.SQLNonTransientException) TableConfig(com.actiontech.dble.config.model.TableConfig) MySqlStatementParser(com.alibaba.druid.sql.dialect.mysql.parser.MySqlStatementParser) DruidUpdateParser(com.actiontech.dble.route.parser.druid.impl.DruidUpdateParser) RouteResultset(com.actiontech.dble.route.RouteResultset)

Example 63 with SchemaConfig

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

the class ExplainHandler method getRouteResultset.

private static RouteResultset getRouteResultset(ServerConnection c, String stmt) {
    String db = c.getSchema();
    int sqlType = ServerParse.parse(stmt) & 0xff;
    if (db == null) {
        // TODO: EXPLAIN SCHEMA.TABLE
        c.writeErrMessage(ErrorCode.ER_NO_DB_ERROR, "No database selected");
        return null;
    }
    SchemaConfig schema = DbleServer.getInstance().getConfig().getSchemas().get(db);
    if (schema == null) {
        c.writeErrMessage(ErrorCode.ER_BAD_DB_ERROR, "Unknown database '" + db + "'");
        return null;
    }
    try {
        if (ServerParse.INSERT == sqlType && isInsertSeq(c, stmt, schema)) {
            c.writeErrMessage(ErrorCode.ER_PARSE_ERROR, "insert sql using sequence,the explain result depends by sequence");
            return null;
        }
        return DbleServer.getInstance().getRouterService().route(schema, sqlType, stmt, c);
    } catch (Exception e) {
        if (e instanceof SQLException && !(e instanceof SQLNonTransientException)) {
            SQLException sqlException = (SQLException) e;
            StringBuilder s = new StringBuilder();
            LOGGER.info(s.append(c).append(stmt).toString() + " error:" + sqlException);
            String msg = sqlException.getMessage();
            c.writeErrMessage(sqlException.getErrorCode(), msg == null ? sqlException.getClass().getSimpleName() : msg);
            return null;
        } else {
            StringBuilder s = new StringBuilder();
            LOGGER.info(s.append(c).append(stmt).toString() + " error:" + e);
            String msg = e.getMessage();
            c.writeErrMessage(ErrorCode.ER_PARSE_ERROR, msg == null ? e.getClass().getSimpleName() : msg);
            return null;
        }
    }
}
Also used : SQLNonTransientException(java.sql.SQLNonTransientException) SchemaConfig(com.actiontech.dble.config.model.SchemaConfig) SQLException(java.sql.SQLException) SQLNonTransientException(java.sql.SQLNonTransientException) SQLException(java.sql.SQLException)

Example 64 with SchemaConfig

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

the class ServerConnection method routeSQL.

public RouteResultset routeSQL(String sql, int type) {
    String db = this.schema;
    if (db == null) {
        writeErrMessage(ErrorCode.ERR_BAD_LOGICDB, "No Database selected");
        return null;
    }
    SchemaConfig schema = DbleServer.getInstance().getConfig().getSchemas().get(db);
    if (schema == null) {
        writeErrMessage(ErrorCode.ERR_BAD_LOGICDB, "Unknown Database '" + db + "'");
        return null;
    }
    RouteResultset rrs;
    try {
        rrs = DbleServer.getInstance().getRouterService().route(schema, type, sql, this);
    } catch (Exception e) {
        executeException(e, sql);
        return null;
    }
    return rrs;
}
Also used : SchemaConfig(com.actiontech.dble.config.model.SchemaConfig) SQLNonTransientException(java.sql.SQLNonTransientException) SQLException(java.sql.SQLException) IOException(java.io.IOException) RouteResultset(com.actiontech.dble.route.RouteResultset)

Example 65 with SchemaConfig

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

the class ServerConnection method execute.

public void execute(String sql, int type) {
    if (this.isClosed()) {
        LOGGER.info("ignore execute ,server connection is closed " + this);
        return;
    }
    if (txInterrupted) {
        writeErrMessage(ErrorCode.ER_YES, txInterruptMsg);
        return;
    }
    String db = this.schema;
    SchemaConfig schemaConfig = null;
    if (db != null) {
        schemaConfig = DbleServer.getInstance().getConfig().getSchemas().get(db);
        if (schemaConfig == null) {
            writeErrMessage(ErrorCode.ERR_BAD_LOGICDB, "Unknown Database '" + db + "'");
            return;
        }
    }
    routeEndExecuteSQL(sql, type, schemaConfig);
}
Also used : SchemaConfig(com.actiontech.dble.config.model.SchemaConfig)

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