Search in sources :

Example 21 with UserConfig

use of io.mycat.config.model.UserConfig in project Mycat_plus by coderczp.

the class ReloadConfig method reload.

public static boolean reload() {
    /**
     *  1、载入新的配置, ConfigInitializer 内部完成自检工作, 由于不更新数据源信息,此处不自检 dataHost  dataNode
     */
    ConfigInitializer loader = new ConfigInitializer(false);
    Map<String, UserConfig> users = loader.getUsers();
    Map<String, SchemaConfig> schemas = loader.getSchemas();
    Map<String, PhysicalDBNode> dataNodes = loader.getDataNodes();
    Map<String, PhysicalDBPool> dataHosts = loader.getDataHosts();
    MycatCluster cluster = loader.getCluster();
    FirewallConfig firewall = loader.getFirewall();
    /**
     * 2、在老的配置上,应用新的配置
     */
    MycatServer.getInstance().getConfig().reload(users, schemas, dataNodes, dataHosts, cluster, firewall, false);
    /**
     * 3、清理缓存
     */
    MycatServer.getInstance().getCacheService().clearCache();
    MycatServer.getInstance().initRuleData();
    return true;
}
Also used : PhysicalDBNode(io.mycat.backend.datasource.PhysicalDBNode) SchemaConfig(io.mycat.config.model.SchemaConfig) ConfigInitializer(io.mycat.config.ConfigInitializer) MycatCluster(io.mycat.config.MycatCluster) PhysicalDBPool(io.mycat.backend.datasource.PhysicalDBPool) UserConfig(io.mycat.config.model.UserConfig) FirewallConfig(io.mycat.config.model.FirewallConfig)

Example 22 with UserConfig

use of io.mycat.config.model.UserConfig in project Mycat_plus by coderczp.

the class ReloadConfig method reload_all.

public static boolean reload_all() {
    /**
     *  1、载入新的配置
     *  1.1、ConfigInitializer 初始化,基本自检
     *  1.2、DataNode/DataHost 实际链路检测
     */
    ConfigInitializer loader = new ConfigInitializer(true);
    Map<String, UserConfig> newUsers = loader.getUsers();
    Map<String, SchemaConfig> newSchemas = loader.getSchemas();
    Map<String, PhysicalDBNode> newDataNodes = loader.getDataNodes();
    Map<String, PhysicalDBPool> newDataHosts = loader.getDataHosts();
    MycatCluster newCluster = loader.getCluster();
    FirewallConfig newFirewall = loader.getFirewall();
    /**
     * 1.2、实际链路检测
     */
    loader.testConnection();
    /**
     *  2、承接
     *  2.1、老的 dataSource 继续承接新建请求
     *  2.2、新的 dataSource 开始初始化, 完毕后交由 2.3
     *  2.3、新的 dataSource 开始承接新建请求
     *  2.4、老的 dataSource 内部的事务执行完毕, 相继关闭
     *  2.5、老的 dataSource 超过阀值的,强制关闭
     */
    MycatConfig config = MycatServer.getInstance().getConfig();
    /**
     * 2.1 、老的 dataSource 继续承接新建请求, 此处什么也不需要做
     */
    boolean isReloadStatusOK = true;
    /**
     * 2.2、新的 dataHosts 初始化
     */
    for (PhysicalDBPool dbPool : newDataHosts.values()) {
        String hostName = dbPool.getHostName();
        // 设置 schemas
        ArrayList<String> dnSchemas = new ArrayList<String>(30);
        for (PhysicalDBNode dn : newDataNodes.values()) {
            if (dn.getDbPool().getHostName().equals(hostName)) {
                dnSchemas.add(dn.getDatabase());
            }
        }
        dbPool.setSchemas(dnSchemas.toArray(new String[dnSchemas.size()]));
        // 获取 data host
        String dnIndex = DnPropertyUtil.loadDnIndexProps().getProperty(dbPool.getHostName(), "0");
        if (!"0".equals(dnIndex)) {
            LOGGER.info("init datahost: " + dbPool.getHostName() + "  to use datasource index:" + dnIndex);
        }
        dbPool.init(Integer.valueOf(dnIndex));
        if (!dbPool.isInitSuccess()) {
            isReloadStatusOK = false;
            break;
        }
    }
    /**
     *  TODO: 确认初始化情况
     *
     *  新的 dataHosts 是否初始化成功
     */
    if (isReloadStatusOK) {
        /**
         * 2.3、 在老的配置上,应用新的配置,开始准备承接任务
         */
        config.reload(newUsers, newSchemas, newDataNodes, newDataHosts, newCluster, newFirewall, true);
        /**
         * 2.4、 处理旧的资源
         */
        LOGGER.warn("1、clear old backend connection(size): " + NIOProcessor.backends_old.size());
        // 清除前一次 reload 转移出去的 old Cons
        Iterator<BackendConnection> iter = NIOProcessor.backends_old.iterator();
        while (iter.hasNext()) {
            BackendConnection con = iter.next();
            con.close("clear old datasources");
            iter.remove();
        }
        Map<String, PhysicalDBPool> oldDataHosts = config.getBackupDataHosts();
        for (PhysicalDBPool dbPool : oldDataHosts.values()) {
            dbPool.stopHeartbeat();
            // 提取数据源下的所有连接
            for (PhysicalDatasource ds : dbPool.getAllDataSources()) {
                // 
                for (NIOProcessor processor : MycatServer.getInstance().getProcessors()) {
                    for (BackendConnection con : processor.getBackends().values()) {
                        if (con instanceof MySQLConnection) {
                            MySQLConnection mysqlCon = (MySQLConnection) con;
                            if (mysqlCon.getPool() == ds) {
                                NIOProcessor.backends_old.add(con);
                            }
                        } else if (con instanceof JDBCConnection) {
                            JDBCConnection jdbcCon = (JDBCConnection) con;
                            if (jdbcCon.getPool() == ds) {
                                NIOProcessor.backends_old.add(con);
                            }
                        }
                    }
                }
            }
        }
        LOGGER.warn("2、to be recycled old backend connection(size): " + NIOProcessor.backends_old.size());
        // 清理缓存
        MycatServer.getInstance().getCacheService().clearCache();
        MycatServer.getInstance().initRuleData();
        return true;
    } else {
        // 如果重载不成功,则清理已初始化的资源。
        LOGGER.warn("reload failed, clear previously created datasources ");
        for (PhysicalDBPool dbPool : newDataHosts.values()) {
            dbPool.clearDataSources("reload config");
            dbPool.stopHeartbeat();
        }
        return false;
    }
}
Also used : PhysicalDBNode(io.mycat.backend.datasource.PhysicalDBNode) BackendConnection(io.mycat.backend.BackendConnection) SchemaConfig(io.mycat.config.model.SchemaConfig) ConfigInitializer(io.mycat.config.ConfigInitializer) MycatCluster(io.mycat.config.MycatCluster) ArrayList(java.util.ArrayList) PhysicalDBPool(io.mycat.backend.datasource.PhysicalDBPool) UserConfig(io.mycat.config.model.UserConfig) FirewallConfig(io.mycat.config.model.FirewallConfig) MycatConfig(io.mycat.config.MycatConfig) NIOProcessor(io.mycat.net.NIOProcessor) PhysicalDatasource(io.mycat.backend.datasource.PhysicalDatasource) JDBCConnection(io.mycat.backend.jdbc.JDBCConnection) MySQLConnection(io.mycat.backend.mysql.nio.MySQLConnection)

Example 23 with UserConfig

use of io.mycat.config.model.UserConfig in project Mycat_plus by coderczp.

the class FrontendAuthenticator method handle.

@Override
public void handle(byte[] data) {
    // check quit packet
    if (data.length == QuitPacket.QUIT.length && data[4] == MySQLPacket.COM_QUIT) {
        source.close("quit packet");
        return;
    }
    AuthPacket auth = new AuthPacket();
    auth.read(data);
    // huangyiming add
    int nopassWordLogin = MycatServer.getInstance().getConfig().getSystem().getNonePasswordLogin();
    // 如果无密码登陆则跳过密码验证这个步骤
    boolean skipPassWord = false;
    String defaultUser = "";
    if (nopassWordLogin == 1) {
        skipPassWord = true;
        Map<String, UserConfig> userMaps = MycatServer.getInstance().getConfig().getUsers();
        if (!userMaps.isEmpty()) {
            setDefaultAccount(auth, userMaps);
        }
    }
    // check user
    if (!checkUser(auth.user, source.getHost())) {
        failure(ErrorCode.ER_ACCESS_DENIED_ERROR, "Access denied for user '" + auth.user + "' with host '" + source.getHost() + "'");
        return;
    }
    // check password
    if (!skipPassWord && !checkPassword(auth.password, auth.user)) {
        failure(ErrorCode.ER_ACCESS_DENIED_ERROR, "Access denied for user '" + auth.user + "', because password is error ");
        return;
    }
    // check degrade
    if (isDegrade(auth.user)) {
        failure(ErrorCode.ER_ACCESS_DENIED_ERROR, "Access denied for user '" + auth.user + "', because service be degraded ");
        return;
    }
    // check schema
    switch(checkSchema(auth.database, auth.user)) {
        case ErrorCode.ER_BAD_DB_ERROR:
            failure(ErrorCode.ER_BAD_DB_ERROR, "Unknown database '" + auth.database + "'");
            break;
        case ErrorCode.ER_DBACCESS_DENIED_ERROR:
            String s = "Access denied for user '" + auth.user + "' to database '" + auth.database + "'";
            failure(ErrorCode.ER_DBACCESS_DENIED_ERROR, s);
            break;
        default:
            success(auth);
    }
}
Also used : AuthPacket(io.mycat.net.mysql.AuthPacket) UserConfig(io.mycat.config.model.UserConfig)

Example 24 with UserConfig

use of io.mycat.config.model.UserConfig in project Mycat_plus by coderczp.

the class ShowDatabases method response.

public static void response(ServerConnection 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;
    MycatConfig conf = MycatServer.getInstance().getConfig();
    Map<String, UserConfig> users = conf.getUsers();
    UserConfig user = users == null ? null : users.get(c.getUser());
    if (user != null) {
        TreeSet<String> schemaSet = new TreeSet<String>();
        Set<String> schemaList = user.getSchemas();
        if (schemaList == null || schemaList.size() == 0) {
            schemaSet.addAll(conf.getSchemas().keySet());
        } else {
            for (String schema : schemaList) {
                schemaSet.add(schema);
            }
        }
        for (String name : schemaSet) {
            RowDataPacket row = new RowDataPacket(FIELD_COUNT);
            row.add(StringUtil.encode(name, 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);
    // post write
    c.write(buffer);
}
Also used : TreeSet(java.util.TreeSet) RowDataPacket(io.mycat.net.mysql.RowDataPacket) EOFPacket(io.mycat.net.mysql.EOFPacket) MycatConfig(io.mycat.config.MycatConfig) UserConfig(io.mycat.config.model.UserConfig) ByteBuffer(java.nio.ByteBuffer) FieldPacket(io.mycat.net.mysql.FieldPacket)

Example 25 with UserConfig

use of io.mycat.config.model.UserConfig in project Mycat_plus by coderczp.

the class ShowFullTables method getTableSet.

private static Set<String> getTableSet(ServerConnection c, Map<String, String> parm) {
    TreeSet<String> tableSet = new TreeSet<String>();
    MycatConfig conf = MycatServer.getInstance().getConfig();
    Map<String, UserConfig> users = conf.getUsers();
    UserConfig user = users == null ? null : users.get(c.getUser());
    if (user != null) {
        Map<String, SchemaConfig> schemas = conf.getSchemas();
        for (String name : schemas.keySet()) {
            if (null != parm.get(SCHEMA_KEY) && parm.get(SCHEMA_KEY).toUpperCase().equals(name.toUpperCase())) {
                if (null == parm.get("LIKE_KEY")) {
                    tableSet.addAll(schemas.get(name).getTables().keySet());
                } else {
                    String p = "^" + parm.get("LIKE_KEY").replaceAll("%", ".*");
                    Pattern pattern = Pattern.compile(p, Pattern.CASE_INSENSITIVE);
                    Matcher ma;
                    for (String tname : schemas.get(name).getTables().keySet()) {
                        ma = pattern.matcher(tname);
                        if (ma.matches()) {
                            tableSet.add(tname);
                        }
                    }
                }
            }
        }
        ;
    }
    return tableSet;
}
Also used : Pattern(java.util.regex.Pattern) SchemaConfig(io.mycat.config.model.SchemaConfig) Matcher(java.util.regex.Matcher) TreeSet(java.util.TreeSet) MycatConfig(io.mycat.config.MycatConfig) UserConfig(io.mycat.config.model.UserConfig)

Aggregations

UserConfig (io.mycat.config.model.UserConfig)36 MycatConfig (io.mycat.config.MycatConfig)10 SchemaConfig (io.mycat.config.model.SchemaConfig)10 FirewallConfig (io.mycat.config.model.FirewallConfig)8 ArrayList (java.util.ArrayList)8 Pattern (java.util.regex.Pattern)8 PhysicalDBNode (io.mycat.backend.datasource.PhysicalDBNode)6 PhysicalDBPool (io.mycat.backend.datasource.PhysicalDBPool)6 MycatCluster (io.mycat.config.MycatCluster)6 ConfigException (io.mycat.config.util.ConfigException)6 List (java.util.List)6 TreeSet (java.util.TreeSet)6 ConfigInitializer (io.mycat.config.ConfigInitializer)4 EOFPacket (io.mycat.net.mysql.EOFPacket)4 FieldPacket (io.mycat.net.mysql.FieldPacket)4 RowDataPacket (io.mycat.net.mysql.RowDataPacket)4 ByteBuffer (java.nio.ByteBuffer)4 Matcher (java.util.regex.Matcher)4 Element (org.w3c.dom.Element)4 Node (org.w3c.dom.Node)4