Search in sources :

Example 6 with DataSourceConfig

use of com.alibaba.cobar.config.model.DataSourceConfig in project cobar by alibaba.

the class XMLSchemaLoader method loadDataSources.

private void loadDataSources(Element root) {
    NodeList list = root.getElementsByTagName("dataSource");
    for (int i = 0, n = list.getLength(); i < n; ++i) {
        Element element = (Element) list.item(i);
        ArrayList<DataSourceConfig> dscList = new ArrayList<DataSourceConfig>();
        String dsNamePrefix = element.getAttribute("name");
        try {
            String dsType = element.getAttribute("type");
            Element locElement = findPropertyByName(element, "location");
            if (locElement == null) {
                throw new NullPointerException("dataSource xml Element with name of " + dsNamePrefix + " has no location Element");
            }
            NodeList locationList = locElement.getElementsByTagName("location");
            int dsIndex = 0;
            for (int j = 0, m = locationList.getLength(); j < m; ++j) {
                String locStr = ((Element) locationList.item(j)).getTextContent();
                int colonIndex = locStr.indexOf(':');
                int slashIndex = locStr.indexOf('/');
                String dsHost = locStr.substring(0, colonIndex).trim();
                int dsPort = Integer.parseInt(locStr.substring(colonIndex + 1, slashIndex).trim());
                String[] schemas = SplitUtil.split(locStr.substring(slashIndex + 1).trim(), ',', '$', '-');
                for (String dsSchema : schemas) {
                    DataSourceConfig dsConf = new DataSourceConfig();
                    ParameterMapping.mapping(dsConf, ConfigUtil.loadElements(element));
                    dscList.add(dsConf);
                    switch(dsIndex) {
                        case 0:
                            dsConf.setName(dsNamePrefix);
                            break;
                        case 1:
                            dscList.get(0).setName(dsNamePrefix + "[0]");
                        default:
                            dsConf.setName(dsNamePrefix + "[" + dsIndex + "]");
                    }
                    dsConf.setType(dsType);
                    dsConf.setDatabase(dsSchema);
                    dsConf.setHost(dsHost);
                    dsConf.setPort(dsPort);
                    ++dsIndex;
                }
            }
        } catch (Exception e) {
            throw new ConfigException("dataSource " + dsNamePrefix + " define error", e);
        }
        for (DataSourceConfig dsConf : dscList) {
            if (dataSources.containsKey(dsConf.getName())) {
                throw new ConfigException("dataSource name " + dsConf.getName() + "duplicated!");
            }
            dataSources.put(dsConf.getName(), dsConf);
        }
    }
}
Also used : DataSourceConfig(com.alibaba.cobar.config.model.DataSourceConfig) NodeList(org.w3c.dom.NodeList) Element(org.w3c.dom.Element) ArrayList(java.util.ArrayList) ConfigException(com.alibaba.cobar.config.util.ConfigException) IOException(java.io.IOException) ConfigException(com.alibaba.cobar.config.util.ConfigException)

Example 7 with DataSourceConfig

use of com.alibaba.cobar.config.model.DataSourceConfig in project cobar by alibaba.

the class ConfigInitializer method getDataNode.

private MySQLDataNode getDataNode(DataNodeConfig dnc, ConfigLoader configLoader) {
    String[] dsNames = SplitUtil.split(dnc.getDataSource(), ',');
    checkDataSourceExists(dsNames);
    MySQLDataNode node = new MySQLDataNode(dnc);
    MySQLDataSource[] dsList = new MySQLDataSource[dsNames.length];
    int size = dnc.getPoolSize();
    for (int i = 0; i < dsList.length; i++) {
        DataSourceConfig dsc = dataSources.get(dsNames[i]);
        dsList[i] = new MySQLDataSource(node, i, dsc, size);
    }
    node.setSources(dsList);
    return node;
}
Also used : MySQLDataNode(com.alibaba.cobar.mysql.MySQLDataNode) MySQLDataSource(com.alibaba.cobar.mysql.MySQLDataSource) DataSourceConfig(com.alibaba.cobar.config.model.DataSourceConfig)

Example 8 with DataSourceConfig

use of com.alibaba.cobar.config.model.DataSourceConfig in project cobar by alibaba.

the class ShowDataSource method execute.

public static void execute(ManagerConnection c, String name) {
    ByteBuffer buffer = c.allocate();
    // write header
    buffer = header.write(buffer, c);
    // write fields
    for (FieldPacket field : fields) {
        buffer = field.write(buffer, c);
    }
    // write eof
    buffer = eof.write(buffer, c);
    // write rows
    byte packetId = eof.packetId;
    CobarConfig conf = CobarServer.getInstance().getConfig();
    Map<String, DataSourceConfig> dataSources = conf.getDataSources();
    List<String> keys = new ArrayList<String>();
    if (null != name) {
        MySQLDataNode dn = conf.getDataNodes().get(name);
        if (dn != null)
            for (MySQLDataSource ds : dn.getSources()) {
                if (ds != null) {
                    keys.add(ds.getName());
                }
            }
    } else {
        keys.addAll(dataSources.keySet());
    }
    Collections.sort(keys, new Comparators<String>());
    for (String key : keys) {
        RowDataPacket row = getRow(dataSources.get(key), c.getCharset());
        row.packetId = ++packetId;
        buffer = row.write(buffer, c);
    }
    // write last eof
    EOFPacket lastEof = new EOFPacket();
    lastEof.packetId = ++packetId;
    buffer = lastEof.write(buffer, c);
    // post write
    c.write(buffer);
}
Also used : MySQLDataNode(com.alibaba.cobar.mysql.MySQLDataNode) DataSourceConfig(com.alibaba.cobar.config.model.DataSourceConfig) RowDataPacket(com.alibaba.cobar.net.mysql.RowDataPacket) ArrayList(java.util.ArrayList) EOFPacket(com.alibaba.cobar.net.mysql.EOFPacket) CobarConfig(com.alibaba.cobar.CobarConfig) ByteBuffer(java.nio.ByteBuffer) MySQLDataSource(com.alibaba.cobar.mysql.MySQLDataSource) FieldPacket(com.alibaba.cobar.net.mysql.FieldPacket)

Example 9 with DataSourceConfig

use of com.alibaba.cobar.config.model.DataSourceConfig in project cobar by alibaba.

the class ShowDataSources method getRow.

private static RowDataPacket getRow(MySQLDataNode node, String charset) {
    RowDataPacket row = new RowDataPacket(FIELD_COUNT);
    row.add(StringUtil.encode(node.getName(), charset));
    MySQLDataSource ds = node.getSource();
    if (ds != null) {
        DataSourceConfig dsc = ds.getConfig();
        row.add(StringUtil.encode(dsc.getType(), charset));
        row.add(StringUtil.encode(dsc.getHost(), charset));
        row.add(IntegerUtil.toBytes(dsc.getPort()));
        row.add(StringUtil.encode(dsc.getDatabase(), charset));
    } else {
        row.add(null);
        row.add(null);
        row.add(null);
        row.add(null);
    }
    return row;
}
Also used : MySQLDataSource(com.alibaba.cobar.mysql.MySQLDataSource) DataSourceConfig(com.alibaba.cobar.config.model.DataSourceConfig) RowDataPacket(com.alibaba.cobar.net.mysql.RowDataPacket)

Aggregations

DataSourceConfig (com.alibaba.cobar.config.model.DataSourceConfig)9 MySQLDataSource (com.alibaba.cobar.mysql.MySQLDataSource)5 MySQLDataNode (com.alibaba.cobar.mysql.MySQLDataNode)4 CobarConfig (com.alibaba.cobar.CobarConfig)3 CobarCluster (com.alibaba.cobar.CobarCluster)2 QuarantineConfig (com.alibaba.cobar.config.model.QuarantineConfig)2 SchemaConfig (com.alibaba.cobar.config.model.SchemaConfig)2 UserConfig (com.alibaba.cobar.config.model.UserConfig)2 RowDataPacket (com.alibaba.cobar.net.mysql.RowDataPacket)2 SocketChannel (java.nio.channels.SocketChannel)2 ArrayList (java.util.ArrayList)2 ConfigInitializer (com.alibaba.cobar.ConfigInitializer)1 DataNodeConfig (com.alibaba.cobar.config.model.DataNodeConfig)1 ConfigException (com.alibaba.cobar.config.util.ConfigException)1 EOFPacket (com.alibaba.cobar.net.mysql.EOFPacket)1 FieldPacket (com.alibaba.cobar.net.mysql.FieldPacket)1 IOException (java.io.IOException)1 ByteBuffer (java.nio.ByteBuffer)1 Element (org.w3c.dom.Element)1 NodeList (org.w3c.dom.NodeList)1