Search in sources :

Example 6 with PhysicalDBNode

use of com.actiontech.dble.backend.datasource.PhysicalDBNode in project dble by actiontech.

the class ConfigInitializer method selfChecking0.

private void selfChecking0() throws ConfigException {
    // 2.schema's conf is not empty
    if (users == null || users.isEmpty()) {
        throw new ConfigException("SelfCheck### user all node is empty!");
    } else {
        for (UserConfig uc : users.values()) {
            if (uc == null) {
                throw new ConfigException("SelfCheck### users node within the item is empty!");
            }
            if (!uc.isManager()) {
                Set<String> authSchemas = uc.getSchemas();
                if (authSchemas == null) {
                    throw new ConfigException("SelfCheck### user " + uc.getName() + "referred schemas is empty!");
                }
                for (String schema : authSchemas) {
                    if (!schemas.containsKey(schema)) {
                        String errMsg = "SelfCheck###  schema " + schema + " referred by user " + uc.getName() + " is not exist!";
                        throw new ConfigException(errMsg);
                    }
                }
            }
        }
    }
    // check schema
    for (SchemaConfig sc : schemas.values()) {
        if (null == sc) {
            throw new ConfigException("SelfCheck### schema all node is empty!");
        } else {
            // check dataNode / dataHost
            if (this.dataNodes != null && this.dataHosts != null) {
                Set<String> dataNodeNames = sc.getAllDataNodes();
                for (String dataNodeName : dataNodeNames) {
                    PhysicalDBNode node = this.dataNodes.get(dataNodeName);
                    if (node == null) {
                        throw new ConfigException("SelfCheck### schema dataNode is empty!");
                    }
                }
            }
        }
    }
    deleteRedundancyConf();
    checkWriteHost();
}
Also used : PhysicalDBNode(com.actiontech.dble.backend.datasource.PhysicalDBNode) ConfigException(com.actiontech.dble.config.util.ConfigException)

Example 7 with PhysicalDBNode

use of com.actiontech.dble.backend.datasource.PhysicalDBNode in project dble by actiontech.

the class ReloadConfig method reload.

public static void reload() throws Exception {
    /* 1 load new conf, ConfigInitializer will check itself */
    ConfigInitializer loader;
    try {
        loader = new ConfigInitializer(false, DbleServer.getInstance().getSystemVariables().isLowerCaseTableNames());
    } catch (Exception e) {
        throw new Exception(e);
    }
    Map<String, UserConfig> users = loader.getUsers();
    Map<String, SchemaConfig> schemas = loader.getSchemas();
    Map<String, PhysicalDBNode> dataNodes = loader.getDataNodes();
    Map<String, PhysicalDBPool> dataHosts = loader.getDataHosts();
    Map<ERTable, Set<ERTable>> erRelations = loader.getErRelations();
    FirewallConfig firewall = loader.getFirewall();
    /* 2 apply the new conf */
    DbleServer.getInstance().getConfig().reload(users, schemas, dataNodes, dataHosts, erRelations, firewall, DbleServer.getInstance().getSystemVariables(), loader.isDataHostWithoutWH(), false);
}
Also used : PhysicalDBNode(com.actiontech.dble.backend.datasource.PhysicalDBNode) SchemaConfig(com.actiontech.dble.config.model.SchemaConfig) Set(java.util.Set) ConfigInitializer(com.actiontech.dble.config.ConfigInitializer) 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)

Example 8 with PhysicalDBNode

use of com.actiontech.dble.backend.datasource.PhysicalDBNode in project dble by actiontech.

the class ReloadConfig method reloadAll.

public static void reloadAll(final int loadAllMode) throws Exception {
    /*
         *  1 load new conf
         *  1.1 ConfigInitializer init adn check itself
         *  1.2 DataNode/DataHost test connection
         */
    ConfigInitializer loader;
    try {
        loader = new ConfigInitializer(true, DbleServer.getInstance().getSystemVariables().isLowerCaseTableNames());
    } catch (Exception e) {
        throw new Exception(e);
    }
    Map<String, UserConfig> newUsers = loader.getUsers();
    Map<String, SchemaConfig> newSchemas = loader.getSchemas();
    Map<String, PhysicalDBNode> newDataNodes = loader.getDataNodes();
    Map<String, PhysicalDBPool> newDataHosts = loader.getDataHosts();
    Map<ERTable, Set<ERTable>> newErRelations = loader.getErRelations();
    FirewallConfig newFirewall = loader.getFirewall();
    SystemVariables newSystemVariables = DbleServer.getInstance().getSystemVariables();
    if (!loader.isDataHostWithoutWH()) {
        VarsExtractorHandler handler = new VarsExtractorHandler(newDataNodes);
        newSystemVariables = handler.execute();
        ConfigInitializer confInit = new ConfigInitializer(newSystemVariables.isLowerCaseTableNames());
        newUsers = confInit.getUsers();
        newSchemas = confInit.getSchemas();
        newDataNodes = confInit.getDataNodes();
        newErRelations = confInit.getErRelations();
        newFirewall = confInit.getFirewall();
        newDataHosts = confInit.getDataHosts();
    }
    if ((loadAllMode & ManagerParseConfig.OPTT_MODE) != 0) {
        try {
            loader.testConnection(false);
        } catch (Exception e) {
            throw new Exception(e);
        }
    }
    /*
         *  2 transform
         *  2.1 old dataSource continue to work
         *  2.2 init the new dataSource
         *  2.3 transform
         *  2.4 put the old connection into a queue
         */
    ServerConfig config = DbleServer.getInstance().getConfig();
    /* 2.1 do nothing */
    boolean isReloadStatusOK = true;
    /* 2.2 init the new dataSource */
    for (PhysicalDBPool dbPool : newDataHosts.values()) {
        String hostName = dbPool.getHostName();
        // set schemas
        ArrayList<String> dnSchemas = new ArrayList<>(30);
        for (PhysicalDBNode dn : newDataNodes.values()) {
            if (dn.getDbPool().getHostName().equals(hostName)) {
                dnSchemas.add(dn.getDatabase());
            }
        }
        dbPool.setSchemas(dnSchemas.toArray(new String[dnSchemas.size()]));
        // get data host
        String dnIndex = DnPropertyUtil.loadDnIndexProps().getProperty(dbPool.getHostName(), "0");
        if (!"0".equals(dnIndex)) {
            LOGGER.info("init data host: " + dbPool.getHostName() + " to use datasource index:" + dnIndex);
        }
        dbPool.init(Integer.parseInt(dnIndex));
        if (!dbPool.isInitSuccess()) {
            isReloadStatusOK = false;
            break;
        }
    }
    if (isReloadStatusOK) {
        /* 2.3 apply new conf */
        config.reload(newUsers, newSchemas, newDataNodes, newDataHosts, newErRelations, newFirewall, newSystemVariables, loader.isDataHostWithoutWH(), true);
        recycleOldBackendConnections(config, ((loadAllMode & ManagerParseConfig.OPTF_MODE) != 0));
        AlarmAppender.refreshConfig();
    } else {
        // INIT FAILED
        LOGGER.info("reload failed, clear previously created data sources ");
        for (PhysicalDBPool dbPool : newDataHosts.values()) {
            dbPool.clearDataSources("reload config");
            dbPool.stopHeartbeat();
        }
        throw new Exception("Init DbPool failed");
    }
}
Also used : PhysicalDBNode(com.actiontech.dble.backend.datasource.PhysicalDBNode) VarsExtractorHandler(com.actiontech.dble.server.variables.VarsExtractorHandler) SchemaConfig(com.actiontech.dble.config.model.SchemaConfig) Set(java.util.Set) ConfigInitializer(com.actiontech.dble.config.ConfigInitializer) ArrayList(java.util.ArrayList) 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) SystemVariables(com.actiontech.dble.server.variables.SystemVariables) ServerConfig(com.actiontech.dble.config.ServerConfig)

Example 9 with PhysicalDBNode

use of com.actiontech.dble.backend.datasource.PhysicalDBNode in project dble by actiontech.

the class ShowDataNode method execute.

public static void execute(ManagerConnection c, String name) {
    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();
    ServerConfig conf = DbleServer.getInstance().getConfig();
    Map<String, PhysicalDBNode> dataNodes = conf.getDataNodes();
    List<String> keys = new ArrayList<>();
    if (StringUtil.isEmpty(name)) {
        keys.addAll(dataNodes.keySet());
    } else {
        SchemaConfig sc = conf.getSchemas().get(name);
        if (null != sc) {
            keys.addAll(sc.getAllDataNodes());
        }
    }
    Collections.sort(keys, new Comparator<String>() {

        @Override
        public int compare(String o1, String o2) {
            Pair<String, Integer> p1 = PairUtil.splitIndex(o1, '[', ']');
            Pair<String, Integer> p2 = PairUtil.splitIndex(o2, '[', ']');
            if (p1.getKey().compareTo(p2.getKey()) == 0) {
                return p1.getValue() - p2.getValue();
            } else {
                return p1.getKey().compareTo(p2.getKey());
            }
        }
    });
    for (String key : keys) {
        RowDataPacket row = getRow(dataNodes.get(key), c.getCharset().getResults());
        if (row != null) {
            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 : PhysicalDBNode(com.actiontech.dble.backend.datasource.PhysicalDBNode) SchemaConfig(com.actiontech.dble.config.model.SchemaConfig) RowDataPacket(com.actiontech.dble.net.mysql.RowDataPacket) EOFPacket(com.actiontech.dble.net.mysql.EOFPacket) ByteBuffer(java.nio.ByteBuffer) ServerConfig(com.actiontech.dble.config.ServerConfig) FieldPacket(com.actiontech.dble.net.mysql.FieldPacket) Pair(com.actiontech.dble.route.parser.util.Pair)

Example 10 with PhysicalDBNode

use of com.actiontech.dble.backend.datasource.PhysicalDBNode 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)

Aggregations

PhysicalDBNode (com.actiontech.dble.backend.datasource.PhysicalDBNode)27 ServerConfig (com.actiontech.dble.config.ServerConfig)9 RouteResultsetNode (com.actiontech.dble.route.RouteResultsetNode)9 PhysicalDBPool (com.actiontech.dble.backend.datasource.PhysicalDBPool)8 BackendConnection (com.actiontech.dble.backend.BackendConnection)7 SchemaConfig (com.actiontech.dble.config.model.SchemaConfig)5 SQLJob (com.actiontech.dble.sqlengine.SQLJob)4 PhysicalDatasource (com.actiontech.dble.backend.datasource.PhysicalDatasource)3 MySQLConnection (com.actiontech.dble.backend.mysql.nio.MySQLConnection)3 ERTable (com.actiontech.dble.config.model.ERTable)3 FirewallConfig (com.actiontech.dble.config.model.FirewallConfig)3 UserConfig (com.actiontech.dble.config.model.UserConfig)3 ConfigException (com.actiontech.dble.config.util.ConfigException)3 RowDataPacket (com.actiontech.dble.net.mysql.RowDataPacket)3 OneRawSQLQueryResultHandler (com.actiontech.dble.sqlengine.OneRawSQLQueryResultHandler)3 Map (java.util.Map)3 Set (java.util.Set)3 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)3 ConfigInitializer (com.actiontech.dble.config.ConfigInitializer)2 DBHostConfig (com.actiontech.dble.config.model.DBHostConfig)2