Exemplo n.º 1
0
  private Set<String> getDataNodeDbTypeMap(String dataNode) {
    Set<String> dbTypes = new HashSet<>();
    String[] dataNodeArr = SplitUtil.split(dataNode, ',', '$', '-');
    for (String node : dataNodeArr) {
      DataNodeConfig datanode = dataNodes.get(node);
      DataHostConfig datahost = dataHosts.get(datanode.getDataHost());
      dbTypes.add(datahost.getDbType());
    }

    return dbTypes;
  }
Exemplo n.º 2
0
  private void loadDataHosts(Element root) {
    NodeList list = root.getElementsByTagName("dataHost");
    for (int i = 0, n = list.getLength(); i < n; ++i) {
      Element element = (Element) list.item(i);
      String name = element.getAttribute("name");
      if (dataHosts.containsKey(name)) {
        throw new ConfigException("dataHost name " + name + "duplicated!");
      }
      int maxCon = Integer.valueOf(element.getAttribute("maxCon"));
      int minCon = Integer.valueOf(element.getAttribute("minCon"));
      int balance = Integer.valueOf(element.getAttribute("balance"));
      String switchTypeStr = element.getAttribute("switchType");
      int switchType = switchTypeStr.equals("") ? -1 : Integer.valueOf(switchTypeStr);
      String slaveThresholdStr = element.getAttribute("slaveThreshold");
      int slaveThreshold = slaveThresholdStr.equals("") ? -1 : Integer.valueOf(slaveThresholdStr);
      String writeTypStr = element.getAttribute("writeType");
      int writeType =
          "".equals(writeTypStr) ? PhysicalDBPool.WRITE_ONLYONE_NODE : Integer.valueOf(writeTypStr);

      String dbDriver = element.getAttribute("dbDriver");
      String dbType = element.getAttribute("dbType");
      String filters = element.getAttribute("filters");
      String logTimeStr = element.getAttribute("logTime");
      long logTime = "".equals(logTimeStr) ? PhysicalDBPool.LONG_TIME : Long.valueOf(logTimeStr);
      String heartbeatSQL = element.getElementsByTagName("heartbeat").item(0).getTextContent();
      NodeList connectionInitSqlList = element.getElementsByTagName("connectionInitSql");
      String initConSQL = null;
      if (connectionInitSqlList.getLength() > 0) {
        initConSQL = connectionInitSqlList.item(0).getTextContent();
      }
      NodeList writeNodes = element.getElementsByTagName("writeHost");
      DBHostConfig[] writeDbConfs = new DBHostConfig[writeNodes.getLength()];
      Map<Integer, DBHostConfig[]> readHostsMap = new HashMap<Integer, DBHostConfig[]>(2);
      for (int w = 0; w < writeDbConfs.length; w++) {
        Element writeNode = (Element) writeNodes.item(w);
        writeDbConfs[w] =
            createDBHostConf(name, writeNode, dbType, dbDriver, maxCon, minCon, filters, logTime);
        NodeList readNodes = writeNode.getElementsByTagName("readHost");
        if (readNodes.getLength() != 0) {
          DBHostConfig[] readDbConfs = new DBHostConfig[readNodes.getLength()];
          for (int r = 0; r < readDbConfs.length; r++) {
            Element readNode = (Element) readNodes.item(r);
            readDbConfs[r] =
                createDBHostConf(
                    name, readNode, dbType, dbDriver, maxCon, minCon, filters, logTime);
          }
          readHostsMap.put(w, readDbConfs);
        }
      }

      DataHostConfig hostConf =
          new DataHostConfig(
              name, dbType, dbDriver, writeDbConfs, readHostsMap, switchType, slaveThreshold);
      hostConf.setMaxCon(maxCon);
      hostConf.setMinCon(minCon);
      hostConf.setBalance(balance);
      hostConf.setWriteType(writeType);
      hostConf.setHearbeatSQL(heartbeatSQL);
      hostConf.setConnectionInitSql(initConSQL);
      hostConf.setFilters(filters);
      hostConf.setLogTime(logTime);
      dataHosts.put(hostConf.getName(), hostConf);
    }
  }
Exemplo n.º 3
0
  private void loadSchemas(Element root) {
    NodeList list = root.getElementsByTagName("schema");
    for (int i = 0, n = list.getLength(); i < n; i++) {
      Element schemaElement = (Element) list.item(i);
      String name = schemaElement.getAttribute("name");
      String dataNode = schemaElement.getAttribute("dataNode");
      String checkSQLSchemaStr = schemaElement.getAttribute("checkSQLschema");
      String sqlMaxLimitStr = schemaElement.getAttribute("sqlMaxLimit");
      int sqlMaxLimit = -1;
      if (sqlMaxLimitStr != null && !sqlMaxLimitStr.isEmpty()) {
        sqlMaxLimit = Integer.valueOf(sqlMaxLimitStr);
      }
      // check dataNode already exists or not
      String defaultDbType = null;
      if (dataNode != null && !dataNode.isEmpty()) {
        List<String> dataNodeLst = new ArrayList<String>(1);
        dataNodeLst.add(dataNode);
        checkDataNodeExists(dataNodeLst);
        String dataHost = dataNodes.get(dataNode).getDataHost();
        defaultDbType = dataHosts.get(dataHost).getDbType();
      } else {
        dataNode = null;
      }
      Map<String, TableConfig> tables = loadTables(schemaElement);
      if (schemas.containsKey(name)) {
        throw new ConfigException("schema " + name + " duplicated!");
      }

      // 设置了table的不需要设置dataNode属性,没有设置table的必须设置dataNode属性
      if (dataNode == null && tables.size() == 0) {
        throw new ConfigException(
            "schema " + name + " didn't config tables,so you must set dataNode property!");
      }

      SchemaConfig schemaConfig =
          new SchemaConfig(
              name, dataNode, tables, sqlMaxLimit, "true".equalsIgnoreCase(checkSQLSchemaStr));
      if (defaultDbType != null) {
        schemaConfig.setDefaultDataNodeDbType(defaultDbType);
        if (!"mysql".equalsIgnoreCase(defaultDbType)) {
          schemaConfig.setNeedSupportMultiDBType(true);
        }
      }

      // 判断是否有不是mysql的数据库类型,方便解析判断是否启用多数据库分页语法解析

      for (String tableName : tables.keySet()) {
        TableConfig tableConfig = tables.get(tableName);
        if (isHasMultiDbType(tableConfig)) {
          schemaConfig.setNeedSupportMultiDBType(true);
          break;
        }
      }
      Map<String, String> dataNodeDbTypeMap = new HashMap<>();
      for (String dataNodeName : dataNodes.keySet()) {
        DataNodeConfig dataNodeConfig = dataNodes.get(dataNodeName);
        String dataHost = dataNodeConfig.getDataHost();
        DataHostConfig dataHostConfig = dataHosts.get(dataHost);
        if (dataHostConfig != null) {
          String dbType = dataHostConfig.getDbType();
          dataNodeDbTypeMap.put(dataNodeName, dbType);
        }
      }
      schemaConfig.setDataNodeDbTypeMap(dataNodeDbTypeMap);
      schemas.put(name, schemaConfig);
    }
  }