Search in sources :

Example 1 with DataNodeConfig

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

the class MySQLDetectorFactory method make.

public MySQLDetector make(MySQLHeartbeat heartbeat) throws IOException {
    SocketChannel channel = openSocketChannel();
    DataSourceConfig dsc = heartbeat.getSource().getConfig();
    DataNodeConfig dnc = heartbeat.getSource().getNode().getConfig();
    MySQLDetector detector = new MySQLDetector(channel);
    detector.setHost(dsc.getHost());
    detector.setPort(dsc.getPort());
    detector.setUser(dsc.getUser());
    detector.setPassword(dsc.getPassword());
    detector.setSchema(dsc.getDatabase());
    detector.setHeartbeatTimeout(dnc.getHeartbeatTimeout());
    detector.setHeartbeat(heartbeat);
    postConnect(detector, CobarServer.getInstance().getConnector());
    return detector;
}
Also used : SocketChannel(java.nio.channels.SocketChannel) DataSourceConfig(com.alibaba.cobar.config.model.DataSourceConfig) DataNodeConfig(com.alibaba.cobar.config.model.DataNodeConfig)

Example 2 with DataNodeConfig

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

the class XMLSchemaLoader method loadDataNodes.

private void loadDataNodes(Element root) {
    NodeList list = root.getElementsByTagName("dataNode");
    for (int i = 0, n = list.getLength(); i < n; i++) {
        Element element = (Element) list.item(i);
        String dnNamePrefix = element.getAttribute("name");
        List<DataNodeConfig> confList = new ArrayList<DataNodeConfig>();
        try {
            Element dsElement = findPropertyByName(element, "dataSource");
            if (dsElement == null) {
                throw new NullPointerException("dataNode xml Element with name of " + dnNamePrefix + " has no dataSource Element");
            }
            NodeList dataSourceList = dsElement.getElementsByTagName("dataSourceRef");
            String[][] dataSources = new String[dataSourceList.getLength()][];
            for (int j = 0, m = dataSourceList.getLength(); j < m; ++j) {
                Element ref = (Element) dataSourceList.item(j);
                String dsString = ref.getTextContent();
                dataSources[j] = SplitUtil.split(dsString, ',', '$', '-', '[', ']');
            }
            if (dataSources.length <= 0) {
                throw new ConfigException("no dataSourceRef defined!");
            }
            for (String[] dss : dataSources) {
                if (dss.length != dataSources[0].length) {
                    throw new ConfigException("dataSource number not equals!");
                }
            }
            for (int k = 0, limit = dataSources[0].length; k < limit; ++k) {
                StringBuilder dsString = new StringBuilder();
                for (int dsIndex = 0; dsIndex < dataSources.length; ++dsIndex) {
                    if (dsIndex > 0) {
                        dsString.append(',');
                    }
                    dsString.append(dataSources[dsIndex][k]);
                }
                DataNodeConfig conf = new DataNodeConfig();
                ParameterMapping.mapping(conf, ConfigUtil.loadElements(element));
                confList.add(conf);
                switch(k) {
                    case 0:
                        conf.setName((limit == 1) ? dnNamePrefix : dnNamePrefix + "[" + k + "]");
                        break;
                    default:
                        conf.setName(dnNamePrefix + "[" + k + "]");
                        break;
                }
                conf.setDataSource(dsString.toString());
            }
        } catch (Exception e) {
            throw new ConfigException("dataNode " + dnNamePrefix + " define error", e);
        }
        for (DataNodeConfig conf : confList) {
            if (dataNodes.containsKey(conf.getName())) {
                throw new ConfigException("dataNode " + conf.getName() + " duplicated!");
            }
            dataNodes.put(conf.getName(), conf);
        }
    }
}
Also used : 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) DataNodeConfig(com.alibaba.cobar.config.model.DataNodeConfig)

Example 3 with DataNodeConfig

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

the class ConfigInitializer method initDataNodes.

private Map<String, MySQLDataNode> initDataNodes(ConfigLoader configLoader) {
    Map<String, DataNodeConfig> nodeConfs = configLoader.getDataNodes();
    Map<String, MySQLDataNode> nodes = new HashMap<String, MySQLDataNode>(nodeConfs.size());
    for (DataNodeConfig conf : nodeConfs.values()) {
        MySQLDataNode dataNode = getDataNode(conf, configLoader);
        if (nodes.containsKey(dataNode.getName())) {
            throw new ConfigException("dataNode " + dataNode.getName() + " duplicated!");
        }
        nodes.put(dataNode.getName(), dataNode);
    }
    return nodes;
}
Also used : MySQLDataNode(com.alibaba.cobar.mysql.MySQLDataNode) HashMap(java.util.HashMap) ConfigException(com.alibaba.cobar.config.util.ConfigException) DataNodeConfig(com.alibaba.cobar.config.model.DataNodeConfig)

Aggregations

DataNodeConfig (com.alibaba.cobar.config.model.DataNodeConfig)3 ConfigException (com.alibaba.cobar.config.util.ConfigException)2 DataSourceConfig (com.alibaba.cobar.config.model.DataSourceConfig)1 MySQLDataNode (com.alibaba.cobar.mysql.MySQLDataNode)1 IOException (java.io.IOException)1 SocketChannel (java.nio.channels.SocketChannel)1 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 Element (org.w3c.dom.Element)1 NodeList (org.w3c.dom.NodeList)1