Search in sources :

Example 11 with PhysicalDatasource

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

the class ConfigTest method getPhysicalDBPool.

private PhysicalDBPool getPhysicalDBPool(DataHostConfig conf) {
    String name = conf.getName();
    PhysicalDatasource[] writeSources = createDataSource(conf, name, conf.getWriteHosts(), false);
    Map<Integer, DBHostConfig[]> readHostsMap = conf.getReadHosts();
    Map<Integer, PhysicalDatasource[]> readSourcesMap = new HashMap<Integer, PhysicalDatasource[]>(readHostsMap.size());
    for (Map.Entry<Integer, DBHostConfig[]> entry : readHostsMap.entrySet()) {
        PhysicalDatasource[] readSources = createDataSource(conf, name, entry.getValue(), true);
        readSourcesMap.put(entry.getKey(), readSources);
    }
    PhysicalDBPool pool = new PhysicalDBPool(conf.getName(), conf, writeSources, readSourcesMap, conf.getBalance());
    return pool;
}
Also used : PhysicalDatasource(com.actiontech.dble.backend.datasource.PhysicalDatasource) HashMap(java.util.HashMap) PhysicalDBPool(com.actiontech.dble.backend.datasource.PhysicalDBPool) HashMap(java.util.HashMap) Map(java.util.Map)

Example 12 with PhysicalDatasource

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

the class ConfigTest method createDataSource.

private PhysicalDatasource[] createDataSource(DataHostConfig conf, String hostName, DBHostConfig[] nodes, boolean isRead) {
    PhysicalDatasource[] dataSources = new PhysicalDatasource[nodes.length];
    for (int i = 0; i < nodes.length; i++) {
        nodes[i].setIdleTimeout(system.getIdleTimeout());
        MySQLDataSource ds = new MySQLDataSource(nodes[i], conf, isRead);
        dataSources[i] = ds;
    }
    return dataSources;
}
Also used : PhysicalDatasource(com.actiontech.dble.backend.datasource.PhysicalDatasource) MySQLDataSource(com.actiontech.dble.backend.mysql.nio.MySQLDataSource)

Example 13 with PhysicalDatasource

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

the class GlobalTableUtil method consistencyCheck.

public static void consistencyCheck() {
    ServerConfig config = DbleServer.getInstance().getConfig();
    for (TableConfig table : globalTableMap.values()) {
        Map<String, ArrayList<PhysicalDBNode>> executedMap = new HashMap<>();
        // <table name="travelrecord" dataNode="dn1,dn2,dn3"
        for (String nodeName : table.getDataNodes()) {
            Map<String, PhysicalDBNode> map = config.getDataNodes();
            for (PhysicalDBNode dbNode : map.values()) {
                // <dataNode name="dn1" dataHost="localhost1" database="db1" />
                if (nodeName.equals(dbNode.getName())) {
                    // dn1,dn2,dn3
                    PhysicalDBPool pool = dbNode.getDbPool();
                    Collection<PhysicalDatasource> allDS = pool.getAllDataSources();
                    for (PhysicalDatasource pds : allDS) {
                        if (pds instanceof MySQLDataSource) {
                            ArrayList<PhysicalDBNode> nodes = executedMap.get(pds.getName());
                            if (nodes == null) {
                                nodes = new ArrayList<>();
                            }
                            nodes.add(dbNode);
                            executedMap.put(pds.getName(), nodes);
                        }
                    }
                }
            }
        }
        for (Map.Entry<String, ArrayList<PhysicalDBNode>> entry : executedMap.entrySet()) {
            ArrayList<PhysicalDBNode> nodes = entry.getValue();
            String[] schemas = new String[nodes.size()];
            for (int index = 0; index < nodes.size(); index++) {
                schemas[index] = StringUtil.removeBackQuote(nodes.get(index).getDatabase());
            }
            Collection<PhysicalDatasource> allDS = nodes.get(0).getDbPool().getAllDataSources();
            for (PhysicalDatasource pds : allDS) {
                if (pds instanceof MySQLDataSource && entry.getKey().equals(pds.getName())) {
                    MySQLDataSource mds = (MySQLDataSource) pds;
                    MySQLConsistencyChecker checker = new MySQLConsistencyChecker(mds, schemas, table.getName());
                    isInnerColumnCheckFinished = 0;
                    checker.checkInnerColumnExist();
                    while (isInnerColumnCheckFinished <= 0) {
                        LOGGER.debug("isInnerColumnCheckFinished:" + isInnerColumnCheckFinished);
                        try {
                            TimeUnit.SECONDS.sleep(1);
                        } catch (InterruptedException e) {
                            LOGGER.info(e.getMessage());
                        }
                    }
                    LOGGER.debug("isInnerColumnCheckFinished:" + isInnerColumnCheckFinished);
                    // check another measure
                    checker = new MySQLConsistencyChecker(mds, schemas, table.getName());
                    isColumnCountCheckFinished = 0;
                    checker.checkRecordCount();
                    while (isColumnCountCheckFinished <= 0) {
                        LOGGER.debug("isColumnCountCheckFinished:" + isColumnCountCheckFinished);
                        try {
                            TimeUnit.SECONDS.sleep(1);
                        } catch (InterruptedException e) {
                            LOGGER.info(e.getMessage());
                        }
                    }
                    LOGGER.debug("isColumnCountCheckFinished:" + isColumnCountCheckFinished);
                    checker = new MySQLConsistencyChecker(mds, schemas, table.getName());
                    checker.checkMaxTimeStamp();
                }
            }
        }
    }
}
Also used : PhysicalDBNode(com.actiontech.dble.backend.datasource.PhysicalDBNode) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) PhysicalDBPool(com.actiontech.dble.backend.datasource.PhysicalDBPool) ServerConfig(com.actiontech.dble.config.ServerConfig) MySQLConsistencyChecker(com.actiontech.dble.backend.heartbeat.MySQLConsistencyChecker) PhysicalDatasource(com.actiontech.dble.backend.datasource.PhysicalDatasource) MySQLDataSource(com.actiontech.dble.backend.mysql.nio.MySQLDataSource) TableConfig(com.actiontech.dble.config.model.TableConfig) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap)

Example 14 with PhysicalDatasource

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

the class ConfigInitializer method testConnection.

public void testConnection(boolean isStart) {
    if (this.dataNodes != null && this.dataHosts != null) {
        Map<String, Boolean> map = new HashMap<>();
        for (PhysicalDBNode dataNode : dataNodes.values()) {
            String database = dataNode.getDatabase();
            PhysicalDBPool pool = dataNode.getDbPool();
            if (isStart) {
                // start for first time, 1.you can set write host as empty
                if (pool.getSources() == null || pool.getSources().length == 0) {
                    continue;
                }
                DBHostConfig wHost = pool.getSource().getConfig();
                // start for first time, 2.you can set write host as yourself
                if (("localhost".equalsIgnoreCase(wHost.getIp()) || "127.0.0.1".equalsIgnoreCase(wHost.getIp())) && wHost.getPort() == this.system.getServerPort()) {
                    continue;
                }
            }
            for (PhysicalDatasource ds : pool.getAllDataSources()) {
                String key = ds.getName() + "_" + database;
                if (map.get(key) == null) {
                    map.put(key, false);
                    try {
                        boolean isConnected = ds.testConnection(database);
                        map.put(key, isConnected);
                    } catch (IOException e) {
                        LOGGER.info("test conn " + key + " error:", e);
                    }
                }
            }
        }
        boolean isConnectivity = true;
        for (Map.Entry<String, Boolean> entry : map.entrySet()) {
            String key = entry.getKey();
            Boolean value = entry.getValue();
            if (!value && isConnectivity) {
                LOGGER.info("SelfCheck### test " + key + " database connection failed ");
                isConnectivity = false;
            } else {
                LOGGER.info("SelfCheck### test " + key + " database connection success ");
            }
        }
        if (!isConnectivity) {
            throw new ConfigException("SelfCheck### there are some datasource connection failed, pls check!");
        }
    }
}
Also used : PhysicalDBNode(com.actiontech.dble.backend.datasource.PhysicalDBNode) PhysicalDBPool(com.actiontech.dble.backend.datasource.PhysicalDBPool) ConfigException(com.actiontech.dble.config.util.ConfigException) IOException(java.io.IOException) PhysicalDatasource(com.actiontech.dble.backend.datasource.PhysicalDatasource)

Example 15 with PhysicalDatasource

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

the class ShowDataSource 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();
    if (null != name) {
        PhysicalDBNode dn = conf.getDataNodes().get(name);
        for (PhysicalDatasource w : dn.getDbPool().getAllDataSources()) {
            RowDataPacket row = getRow(w, c.getCharset().getResults());
            row.setPacketId(++packetId);
            buffer = row.write(buffer, c, true);
        }
    } else {
        // add all
        for (Map.Entry<String, PhysicalDBPool> entry : conf.getDataHosts().entrySet()) {
            PhysicalDBPool dataHost = entry.getValue();
            for (int i = 0; i < dataHost.getSources().length; i++) {
                RowDataPacket row = getRow(dataHost.getSources()[i], c.getCharset().getResults());
                row.setPacketId(++packetId);
                buffer = row.write(buffer, c, true);
                if (dataHost.getrReadSources().get(i) != null) {
                    for (PhysicalDatasource w : dataHost.getrReadSources().get(i)) {
                        RowDataPacket sRow = getRow(w, c.getCharset().getResults());
                        sRow.setPacketId(++packetId);
                        buffer = sRow.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) RowDataPacket(com.actiontech.dble.net.mysql.RowDataPacket) EOFPacket(com.actiontech.dble.net.mysql.EOFPacket) PhysicalDBPool(com.actiontech.dble.backend.datasource.PhysicalDBPool) ByteBuffer(java.nio.ByteBuffer) ServerConfig(com.actiontech.dble.config.ServerConfig) PhysicalDatasource(com.actiontech.dble.backend.datasource.PhysicalDatasource) FieldPacket(com.actiontech.dble.net.mysql.FieldPacket) Map(java.util.Map)

Aggregations

PhysicalDatasource (com.actiontech.dble.backend.datasource.PhysicalDatasource)19 PhysicalDBPool (com.actiontech.dble.backend.datasource.PhysicalDBPool)16 ServerConfig (com.actiontech.dble.config.ServerConfig)7 RowDataPacket (com.actiontech.dble.net.mysql.RowDataPacket)7 DBHeartbeat (com.actiontech.dble.backend.heartbeat.DBHeartbeat)5 LinkedList (java.util.LinkedList)4 Map (java.util.Map)4 PhysicalDBNode (com.actiontech.dble.backend.datasource.PhysicalDBNode)3 MySQLDataSource (com.actiontech.dble.backend.mysql.nio.MySQLDataSource)3 DataSourceSyncRecorder (com.actiontech.dble.statistic.DataSourceSyncRecorder)3 ArrayList (java.util.ArrayList)3 HashMap (java.util.HashMap)3 SimpleDateFormat (java.text.SimpleDateFormat)2 BackendConnection (com.actiontech.dble.backend.BackendConnection)1 MySQLConsistencyChecker (com.actiontech.dble.backend.heartbeat.MySQLConsistencyChecker)1 MySQLConnection (com.actiontech.dble.backend.mysql.nio.MySQLConnection)1 TableConfig (com.actiontech.dble.config.model.TableConfig)1 ConfigException (com.actiontech.dble.config.util.ConfigException)1 NIOProcessor (com.actiontech.dble.net.NIOProcessor)1 EOFPacket (com.actiontech.dble.net.mysql.EOFPacket)1